Mathematica Communications 9(2004), 27-34 27 Fitting affine and orthogona transformations between two sets of points Hemuth Späth Abstract. Let two point sets P and Q be given in R n. We determine a transation and an affine transformation or an isometry such that the image of Q approximates P as best as possibe in the east squares sense. Key words: fitting inear transformations, east squares AMS subject cassifications: 65D10 Received May 28, 2003 Accepted February 20, 2004 1. Introduction Let the two sets of points P and Q by given by p i =(p 1i,...,p ni ) T, q i =(a 1i,...,a ni ) T (i =1,...,m). (1) We are ooking for some matrix and some transation vector A =(a jk ) j,k=1,...,n (2) t =(t 1,...,t n ) T (3) such that p i Aq i + t (i =1,...,m). (4) Asthe number of unknownsisn 2 +n, werequirem>n 2 +n. One possibe objective function to be minimized is the sum of squared Eucidean distances, i. e. S(A, t) = p i Aq i t 2 2. (5) The first soution method wi consist of a generaization of the east square fitting by a straight ine (n = 1). The second method is an iteration method that coud aso be appied but shoud not. Department of Mathematics, University of Odenburg, Postfach 2 503, D-26111 Odenburg, Germany, e-mai: spaeth@mathematik.uni-odenburg.de
28 H. Späth If A isorthogona, we wi have the n(n +1)/2 side conditions A T A = I, i. e. a T j a k = δ jk (j k) (6) where a j (j =1,...,n) are the coumnsof A. It wi turn out in Section 3 that it is easier to manage them impicity and that the mentioned second method can be used here. Other methods are discussed in [1, 2, 4, 5]. 2. Fitting affine transformations The necessary and because of inearity aso sufficient conditions for (5) to be minimized are S = 2 (E jk q i ) T (p i Aq i t) =0 (j, k =1,...,n) (7) a jk S t = 2 (p i Aq i t) =0 (8) In (7) we set E jk = A a jk,i.e.then n matrix that contains1 in the j-th row and the k-th coumn and 0 esewhere. As (E jk q i ) T =(0,...,0,q ki, 0,...,0), (9) where q ki isin the j-the position, the conditions (7) wi read ( m ) ( m ) ( m ) q 1i q ki a j1 +...+ q ni q ki a jn + q ki t j = q ki p ji (10) (j, k =1,...,n). Additionay, (8) gives ( m ) ( m ) q 1i a j1 +...+ q ni a jn + mt j = p ji (11) (j =1,...,n). In order to integrate (10) and (11) we introduce q n+1,i =1 (i =1,...,m) (12) and put q i =(q 1i,...,q ni,q n+1,i ) T (i =1,...,m), (13) Q = q i q T i, (14) ãa j =(a j1,...,a jn,t j ) T, (15) c j =( c j1,..., c j,n+1 ) T with c jk = q ki p ji (k =1,...,n+1). (16)
Fitting transformations 29 Then (10) and (11) can be integrated within Qãa j = c j (j =1,...,n). (17) Note that Q iscommon to a n systems of inear equations with n + 1 unknowns, i. e. you need just one LU-decomposition to sove a systems. Q wi normay be positivey definite for m>n 2 + n and thusnonsinguar. Exampe 1. For Q we used m =20points q i R 3 given by 0 1 0 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1 0 0 Q = 0 0 1 1 0 1 1 0 0 0 1 1 0 1 0 1 1 0 1 1 1 0 0 0 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 Then we used A = 1 0 1 0 1 1, t = 1 0 1 1 0 1 to produce p i = Aq i + t (i =1,...,20). In this case the minima soution of (5) must give S(A, t) =0. Of course, this was done, and A and t were retrieved. Exampe 2. We disturbed a ot of coefficients of p i and q i randomy by ±1 to receive 0 1 0 0 1 1 1 1 1 1 1 0 0 1 0 1 0 1 1 0 Q = 1 1 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 0 1 1 0 1 1 0 0 1 1 1 0 1 0 1 2 2 3 1 3 1 3 1 2 1 1 1 1 2 0 P = 1 1 2 1 1 0 2 0 2 1 1 1 1 1 1 3 1 0 1 1 0 1 1 2 2 3 1 1 1 1 1 1 0 0 2 0 1 2 1 1 We received A =.6564.1728.5658.0028.7831 1.0776.7316.3747.1107, t = 1.1058.2724 1.0702 and S(A, t) =32.25425. Now we wi mention an iterative descent method that, it is true, does not exceed the straightforward and direct soution of (13) but wi aso be abe to be appied in the next section. We can write (8) as t = 1 m (p i Aq i ) (18) and (7) as (E jk q i ) T Aq i = (E jk q i ) T (p i t) (19) We consider the foowing agorithm:
30 H. Späth Step 0: Give some starting vaue A (0) for A, e.g.a (0) = I. Setw =0. Step 1: Use (18) to cacuate t (w+1) = 1 m (p i A (w) q i ). (20) Step 2: Use (19) to cacuate A (w+1),i.e.tosove (E jk q i ) T A (w+1) q i = (E jk q i ) T (p i t (w+1) ). (21) Step 3: STOP, if some convergence criterion is fufied, otherwise set w := w +1 and go back to Step 1. Thisaternating method givesa descent within every iteration. For the above two exampes it converged to 6 correct digits within 6 and 9 iterations, respectivey, and gave the same soution as (17). 3. Fitting orthogona transformations Orthogona transformations are affine transformations with the property (6). Thus to the objective function (5) one coud add the side conditions (6). Instead of trying to sove a noninear system after introducing the Lagrangian function we wi describe a more effective way. It is we-known [3] that every orthogona matrix A of size n n can be written asa product A = R 1 R N B, (22) where R = R(ϕ jk ) ( =1,...,N = and where R(ϕ jk )=(r is ) i,s=1,...,n hasthe eements (n 1)n ) (23) 2 r jj = r kk =cos(ϕ jk ) r jk = r kj = sin(ϕ jk ) (24) r ii =1 (i j, k), r is = 0 ese. The indices uniquey correspond to pairs (j, k) with j < k asindicated in the foowing tabe: 1 2 n 1 n n+1 2n 32n 2 N (j, k) (1, 2) (1, 3) (1,n)(2, 3) (2, 4) (2,n) (3, 4) (n 1,n)
Fitting transformations 31 Finay, B = I (identity) for det A = +1 [3, 6] and B =diag(1, 1,...,1, 1) for det A = 1 [3]. The anges ϕ ik are caed the Euer rotation anges. We wi aso write ϕ =(ϕ 1,...,ϕ N ) T With thisnotation the objective function (5) to be minimized here reads S(ϕ, t) = p i R 1 R 2 R N Bq i t 2 2. (25) The necessary conditions for a minimum are S m t = 2 (p i R 1 R N Bq i t) =0, (26) S ϕ = 2 i?1 (p i R 1 R N Bq i t) T R 1 R 1 R R +1 R N Bq i = 0 (27) where the matrix hasthe coefficients ( =1,...,N), R = R ϕ jk (28) r jj = r kk = sin(ϕ jk ), r jk = r kj = cos(ϕ jk), (29) r is = 0 ese. Note that the matrix C = R T R =(c is ) i,s=1,...,n (30) hasthe eements c jk = c kj = 1, c is = 0 ese. (31) Now(26)canbewrittenas t = t(ϕ) = 1 (p i R 1 R N Bq i ) m (32) and (27) as (p i t) T R 1 R R N q i = q T i RT N R T 1 R 1 R R N q i = = s T i RT R s i (33) s T i C s i i=2
32 H. Späth where s i = R +1 R N Bq i. But because of (31) the right-hand side of (33) is zero. Thus(27) reducesto Putting (p i t) T R 1 R 1 R R +1 R N Bq i =0. (34) u ()T i = (p i t) T R 1 R 1, v () i = R +1 R n Bq i, (i =1,...,n; =1,...,N) (35) (34) reads Using (29) this resuts in u ()T i R v () i = 0 (36) g jk sin(ϕ jk ) f jk cos(ϕ jk )=0, (37) where f jk = g jk = v () ij u() ik v() ik u() ij, v () ij u() ij + v() ik u() ik. (38) Thus ϕ ik = ϕ isgiven by ϕ ik =atan ( fjk A minimum of S w.r.t. ϕ jk isgiven by (39) if g jk ). (39) 1 2 2 S ϕ 2 ik = g jk cos(ϕ jk )+f jk sin(ϕ jk ) > 0. (40) Otherwise ϕ jk hasto be repaced by ϕ jk +π. Additionay, in order to avoid negative anges, we repace ϕ jk by ϕ jk +2π. Atogether we can now design the foowing descent agorithm: Step 1: Give starting vaues ϕ (0) ( =1,...,N), e.g. ϕ (0) = 0. Decide whether to use B = I or B =diag(1,...,1, 1). Set w =0. Step 2: Step 3: Cacuate R (w) = R(ϕ (w) )( =1,...,N) corresponding to (23) and (24). Set F := R (w) 2 R (w) N and G := I.
Fitting transformations 33 Step 4: Corresponding to (32) cacuate t (w) = 1 m (p i R (w) 1 Fq i ) Step 5: do =1,N end g := 0; f := 0; if = N then F := I do i =1,m u T := (p i t (w) ) T G v := Fq i g := g + v j u k v k u j f := f + v j u j + v k u k end i ϕ =atan(g/f) if (g cos(ϕ)+f sin(ϕ)) 0thenϕ := ϕ + π if (ϕ <0) then ϕ := ϕ +2π ϕ (w+1) := ϕ; R (w+1) if ( <n)then end if := R(ϕ) G := GR (w+1) ; F := R (w)t +1 F Step 6: Set w := w +1. If some convergence criterion is not fufied, then go back to Step 3; otherwise set t := t (w) and A := R (w) B and stop. 1 R (w) N Convergence of thisagorithm to a goba minimum cannot be proved. However, empiricay, the goba minimum wasawaysattained independenty of the starting vauesfor ϕ (0). We wi give some exampes where we aways used ϕ (0) =0( = 1,...,N). Starting with a set Q of m =20pointsa i R 4,namey Q = 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 0 0 0 2 2 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 2 2 2 2 0 0 1 1 0 0 0 0 1 1 0 1 1 1 1 2 0 2 2 2 0 0 0 1 1 1 1 1 1 0 0 0 1 1 0 0 0 0 2 0 and anges ϕ = ( =1,...,6) and a transation t =( 1, 0, 1, 2) T we produced p i = R 1 R 6 q i + t (i =1,...,m= 20). Exampe 3. Using those data after 206 iterations we got S =0.00000 (as expected) and t =( 1.0000, 0.0000, 1.0000, 2.0000) T ; instead of ϕ = ( =1,...,6) weendedupwithϕ 1 =1.0000, ϕ 2 =5.1416, ϕ 3 =.1416, ϕ 4 =5.4248, ϕ 5 =
34 H. Späth 1.8584, ϕ 6 =6.0000. This resut is right because with (ϕ 1,...,ϕ 6 ) T (among others) (ϕ 1,ϕ 2 + π, π ϕ 3, 3π ϕ 4,ϕ 5 π, ϕ 6 ) T is aso a goba minimum. Exampe 4. We used the same data as before but dropped a components of p i from d 1.d 2 d 3 d 4 d 5 d 6 to d 1.d 2 thus introducing sma errors. After 202 iterations we received S = 0.07328, t =(.9644,.0459,.9469, 1.9441) T and ϕ = (.9614, 5.1497,.1431, 5.3838, 1.8640, 5.9972) T. As expected, these resuts do not differ very much from those of Exampe 3. Exampe 5. We further dropped the components of p i from d 1.d 2 to integer vaues d 1 getting P = 0 0 1 0 0 0 0 0 0 1 0 1 0 0 1 2 0 1 0 1 0 0 1 1 0 0 0 0 1 0 0 0 1 1 1 1 0 1 3 2 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 1 1 0 0 2 3 3 2 2 1 1 2 2 3 2 2 1 1 2 2 3 4 3 4 After 258 iterations we got S =5.66304, t =(.5893,.5366,.6593, 1.6014) T and ϕ =(1.3334, 5.0294,.1224, 5.5977, 2.0565, 6.0189) T. Of course, these resuts differ more than before. Varying the starting vaues ϕ (0) we aways received the same resuts, i.e. hopefuy a goba minimum. References [1] J. C. Gower, Mutivariabe anaysis: Ordination, mutidimensiona scaing and aied topics, in: Statistics, Handbook of Appicabe Mathematics, Vo. VI, (E. H. Loyd, Ed.), Wiey, Chichester, 1984, 727-781. [2] R. J. Hanson, M. J. Norris, Anaysis of measurements based on the singuar vaue decomposition, SIAM J. Sci. Statist. Comput. 2(1981), 363 373. [3] F. Neiss, Determinanten und Matrizen, 2. Aufage, Springer, 1967. [4] P. H. Schönemann, R. M. Carro, Fitting one matrix to another under choice of a centra diation and a rigid motion, Psychometrika35(1970), 245 255. [5] I. Söderkvist, P.-Å. Wedin, On condition numbers and agorithms for determining a rigid body movement, BIT 34(1994), 424 436. [6] N. JA. Vienkin, Specia Functions and the Theory of Group Representations, AMS, Providence 1968.