Debian Codegraph @

src/thirdparty/VLFeat/rodrigues.c

raw
1 /** @file rodrigues.c
2 ** @brief Rodrigues formulas - Definition
3 ** @author Andrea Vedaldi
4 **/
5
6 /*
7 Copyright (C) 2007-13 Andrea Vedaldi and Brian Fulkerson.
8 All rights reserved.
9
10 This file is part of the VLFeat library and is made available under
11 the terms of the BSD license (see the COPYING file).
12 */
13
14 #include "generic.h"
15 #include "mathop.h"
16 #include "rodrigues.h"
17
18 #include <math.h>
19
20 /** @brief Rodrigues' formula
21 ** @param R_pt 3x3 matrix - array of 9 double (in) .
22 ** @param dR_pt 9x3 matrix - array of 27 double (in).
23 ** @param om_pt 3 vector - array of 3 dobule (out).
24 **/
25
26 void
27 vl_rodrigues(double* R_pt, double* dR_pt, const double* om_pt)
28 {
29 /*
30 Let
31
32 th = |om|, r=w/th,
33 sth=sin(th), cth=cos(th),
34 ^om = hat(om)
35
36 Then the rodrigues formula is an expansion of the exponential
37 function:
38
39 rodrigues(om) = exp ^om = I + ^r sth + ^r^2 (1 - cth).
40
41 The derivative can be computed by elementary means and
42 results:
43
44 d(vec rodrigues(om)) sth d ^r 1 - cth d (^r)^2
45 -------------------- = ---- ----- + ------- -------- +
46 d om^T th d r^T th d r^T
47
48 sth 1 - cth
49 + vec^r (cth - -----) + vec^r^2 (sth - 2-------)r^T
50 th th
51 */
52
53 #define OM(i) om_pt[(i)]
54 #define R(i,j) R_pt[(i)+3*(j)]
55 #define DR(i,j) dR_pt[(i)+9*(j)]
56 #undef small
57
58 const double small = 1e-6 ;
59
60 double th = sqrt( OM(0)*OM(0) +
61 OM(1)*OM(1) +
62 OM(2)*OM(2) ) ;
63
64 if( th < small ) {
65 R(0,0) = 1.0 ; R(0,1) = 0.0 ; R(0,2) = 0.0 ;
66 R(1,0) = 0.0 ; R(1,1) = 1.0 ; R(1,2) = 0.0 ;
67 R(2,0) = 0.0 ; R(2,1) = 0.0 ; R(2,2) = 1.0 ;
68
69 if(dR_pt) {
70 DR(0,0) = 0 ; DR(0,1) = 0 ; DR(0,2) = 0 ;
71 DR(1,0) = 0 ; DR(1,1) = 0 ; DR(1,2) = 1 ;
72 DR(2,0) = 0 ; DR(2,1) = -1 ; DR(2,2) = 0 ;
73
74 DR(3,0) = 0 ; DR(3,1) = 0 ; DR(3,2) = -1 ;
75 DR(4,0) = 0 ; DR(4,1) = 0 ; DR(4,2) = 0 ;
76 DR(5,0) = 1 ; DR(5,1) = 0 ; DR(5,2) = 0 ;
77
78 DR(6,0) = 0 ; DR(6,1) = 1 ; DR(6,2) = 0 ;
79 DR(7,0) = -1 ; DR(7,1) = 0 ; DR(7,2) = 0 ;
80 DR(8,0) = 0 ; DR(8,1) = 0 ; DR(8,2) = 0 ;
81 }
82 return ;
83 }
84
85 {
86 double x = OM(0) / th ;
87 double y = OM(1) / th ;
88 double z = OM(2) / th ;
89
90 double xx = x*x ;
91 double xy = x*y ;
92 double xz = x*z ;
93 double yy = y*y ;
94 double yz = y*z ;
95 double zz = z*z ;
96
97 const double yx = xy ;
98 const double zx = xz ;
99 const double zy = yz ;
100
101 double sth = sin(th) ;
102 double cth = cos(th) ;
103 double mcth = 1.0 - cth ;
104
105 R(0,0) = 1 - mcth * (yy+zz) ;
106 R(1,0) = sth*z + mcth * xy ;
107 R(2,0) = - sth*y + mcth * xz ;
108
109 R(0,1) = - sth*z + mcth * yx ;
110 R(1,1) = 1 - mcth * (zz+xx) ;
111 R(2,1) = sth*x + mcth * yz ;
112
113 R(0,2) = sth*y + mcth * xz ;
114 R(1,2) = - sth*x + mcth * yz ;
115 R(2,2) = 1 - mcth * (xx+yy) ;
116
117 if(dR_pt) {
118 double a = sth / th ;
119 double b = mcth / th ;
120 double c = cth - a ;
121 double d = sth - 2*b ;
122
123 DR(0,0) = - d * (yy+zz) * x ;
124 DR(1,0) = b*y + c * zx + d * xy * x ;
125 DR(2,0) = b*z - c * yx + d * xz * x ;
126
127 DR(3,0) = b*y - c * zx + d * xy * x ;
128 DR(4,0) = -2*b*x - d * (zz+xx) * x ;
129 DR(5,0) = a + c * xx + d * yz * x ;
130
131 DR(6,0) = b*z + c * yx + d * zx * x ;
132 DR(7,0) = -a - c * xx + d * zy * x ;
133 DR(8,0) = -2*b*x - d * (yy+xx) * x ;
134
135 DR(0,1) = -2*b*y - d * (yy+zz) * y ;
136 DR(1,1) = b*x + c * zy + d * xy * y ;
137 DR(2,1) = -a - c * yy + d * xz * y ;
138
139 DR(3,1) = b*x - c * zy + d * xy * y ;
140 DR(4,1) = - d * (zz+xx) * y ;
141 DR(5,1) = b*z + c * xy + d * yz * y ;
142
143 DR(6,1) = a + c * yy + d * zx * y ;
144 DR(7,1) = b*z - c * xy + d * zy * y ;
145 DR(8,1) = -2*b*y - d * (yy+xx) * y ;
146
147 DR(0,2) = -2*b*z - d * (yy+zz) * z ;
148 DR(1,2) = a + c * zz + d * xy * z ;
149 DR(2,2) = b*x - c * yz + d * xz * z ;
150
151 DR(3,2) = -a - c * zz + d * xy * z ;
152 DR(4,2) = -2*b*z - d * (zz+xx) * z ;
153 DR(5,2) = b*y + c * xz + d * yz * z ;
154
155 DR(6,2) = b*x + c * yz + d * zx * z ;
156 DR(7,2) = b*y - c * xz + d * zy * z ;
157 DR(8,2) = - d * (yy+xx) * z ;
158 }
159 }
160
161 #undef OM
162 #undef R
163 #undef DR
164
165 }
166
167 /** @brief Inverse Rodrigues formula
168 ** @param om_pt 3 vector - array of 3 dobule (out).
169 ** @param dom_pt 3x9 matrix - array of 3x9 dobule (out).
170 ** @param R_pt 3x3 matrix - array of 9 double (in).
171 **
172 ** This function computes the Rodrigues formula of the argument @a
173 ** om_pt. The result is stored int the matrix @a R_pt. If @a dR_pt is
174 ** non null, then the derivative of the Rodrigues formula is computed
175 ** and stored into the matrix @a dR_pt.
176 **/
177
178 VL_EXPORT
179 void vl_irodrigues(double* om_pt, double* dom_pt, const double* R_pt)
180 {
181 /*
182 tr R - 1 1 [ R32 - R23 ]
183 th = cos^{-1} --------, r = ------ [ R13 - R31 ], w = th r.
184 2 2 sth [ R12 - R21 ]
185
186 sth = sin(th)
187
188 dw th*cth-sth dw th [di3 dj2 - di2 dj3]
189 ---- = ---------- r, ---- = ----- [di1 dj3 - di3 dj1].
190 dRii 2 sth^2 dRij 2 sth [di1 dj2 - di2 dj1]
191
192 trace(A) < -1 only for small num. errors.
193 */
194
195 #define OM(i) om_pt[(i)]
196 #define DOM(i,j) dom_pt[(i)+3*(j)]
197 #define R(i,j) R_pt[(i)+3*(j)]
198 #define W(i,j) W_pt[(i)+3*(j)]
199
200 const double small = 1e-6 ;
201
202 double th = acos
203 (0.5*(VL_MAX(R(0,0)+R(1,1)+R(2,2),-1.0) - 1.0)) ;
204
205 double sth = sin(th) ;
206 double cth = cos(th) ;
207
208 if(fabs(sth) < small && cth < 0) {
209 /*
210 we have this singularity when the rotation is about pi (or -pi)
211 we use the fact that in this case
212
213 hat( sqrt(1-cth) * r )^2 = W = (0.5*(R+R') - eye(3))
214
215 which gives
216
217 (1-cth) rx^2 = 0.5 * (W(1,1)-W(2,2)-W(3,3))
218 (1-cth) ry^2 = 0.5 * (W(2,2)-W(3,3)-W(1,1))
219 (1-cth) rz^2 = 0.5 * (W(3,3)-W(1,1)-W(2,2))
220 */
221
222 double W_pt [9], x, y, z ;
223 W_pt[0] = 0.5*( R(0,0) + R(0,0) ) - 1.0 ;
224 W_pt[1] = 0.5*( R(1,0) + R(0,1) ) ;
225 W_pt[2] = 0.5*( R(2,0) + R(0,2) );
226
227 W_pt[3] = 0.5*( R(0,1) + R(1,0) );
228 W_pt[4] = 0.5*( R(1,1) + R(1,1) ) - 1.0;
229 W_pt[5] = 0.5*( R(2,1) + R(1,2) );
230
231 W_pt[6] = 0.5*( R(0,2) + R(2,0) ) ;
232 W_pt[7] = 0.5*( R(1,2) + R(2,1) ) ;
233 W_pt[8] = 0.5*( R(2,2) + R(2,2) ) - 1.0 ;
234
235 /* these are only absolute values */
236 x = sqrt( 0.5 * (W(0,0)-W(1,1)-W(2,2)) ) ;
237 y = sqrt( 0.5 * (W(1,1)-W(2,2)-W(0,0)) ) ;
238 z = sqrt( 0.5 * (W(2,2)-W(0,0)-W(1,1)) ) ;
239
240 /* set the biggest component to + and use the element of the
241 ** matrix W to determine the sign of the other components
242 ** then the solution is either (x,y,z) or its opposite */
243 if( x >= y && x >= z ) {
244 y = (W(1,0) >=0) ? y : -y ;
245 z = (W(2,0) >=0) ? z : -z ;
246 } else if( y >= x && y >= z ) {
247 z = (W(2,1) >=0) ? z : -z ;
248 x = (W(1,0) >=0) ? x : -x ;
249 } else {
250 x = (W(2,0) >=0) ? x : -x ;
251 y = (W(2,1) >=0) ? y : -y ;
252 }
253
254 /* we are left to chose between (x,y,z) and (-x,-y,-z)
255 ** unfortunately we cannot (as the rotation is too close to pi) and
256 ** we just keep what we have. */
257 {
258 double scale = th / sqrt( 1 - cth ) ;
259 OM(0) = scale * x ;
260 OM(1) = scale * y ;
261 OM(2) = scale * z ;
262
263 if( dom_pt ) {
264 int k ;
265 for(k=0; k<3*9; ++k)
266 dom_pt [k] = VL_NAN_D ;
267 }
268 return ;
269 }
270
271 } else {
272 double a = (fabs(sth) < small) ? 1 : th/sin(th) ;
273 double b ;
274 OM(0) = 0.5*a*(R(2,1) - R(1,2)) ;
275 OM(1) = 0.5*a*(R(0,2) - R(2,0)) ;
276 OM(2) = 0.5*a*(R(1,0) - R(0,1)) ;
277
278 if( dom_pt ) {
279 if( fabs(sth) < small ) {
280 a = 0.5 ;
281 b = 0 ;
282 } else {
283 a = th/(2*sth) ;
284 b = (th*cth - sth)/(2*sth*sth)/th ;
285 }
286
287 DOM(0,0) = b*OM(0) ;
288 DOM(1,0) = b*OM(1) ;
289 DOM(2,0) = b*OM(2) ;
290
291 DOM(0,1) = 0 ;
292 DOM(1,1) = 0 ;
293 DOM(2,1) = a ;
294
295 DOM(0,2) = 0 ;
296 DOM(1,2) = -a ;
297 DOM(2,2) = 0 ;
298
299 DOM(0,3) = 0 ;
300 DOM(1,3) = 0 ;
301 DOM(2,3) = -a ;
302
303 DOM(0,4) = b*OM(0) ;
304 DOM(1,4) = b*OM(1) ;
305 DOM(2,4) = b*OM(2) ;
306
307 DOM(0,5) = a ;
308 DOM(1,5) = 0 ;
309 DOM(2,5) = 0 ;
310
311 DOM(0,6) = 0 ;
312 DOM(1,6) = a ;
313 DOM(2,6) = 0 ;
314
315 DOM(0,7) = -a ;
316 DOM(1,7) = 0 ;
317 DOM(2,7) = 0 ;
318
319 DOM(0,8) = b*OM(0) ;
320 DOM(1,8) = b*OM(1) ;
321 DOM(2,8) = b*OM(2) ;
322 }
323 }
324
325 #undef OM
326 #undef DOM
327 #undef R
328 #undef W
329 }