You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
8.8 KiB

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