btImplicitQRSVD.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /**
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2019 Google Inc. http://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. Copyright (c) 2016 Theodore Gast, Chuyuan Fu, Chenfanfu Jiang, Joseph Teran
  13. Permission is hereby granted, free of charge, to any person obtaining a copy of
  14. this software and associated documentation files (the "Software"), to deal in
  15. the Software without restriction, including without limitation the rights to
  16. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  17. of the Software, and to permit persons to whom the Software is furnished to do
  18. so, subject to the following conditions:
  19. The above copyright notice and this permission notice shall be included in all
  20. copies or substantial portions of the Software.
  21. If the code is used in an article, the following paper shall be cited:
  22. @techreport{qrsvd:2016,
  23. title={Implicit-shifted Symmetric QR Singular Value Decomposition of 3x3 Matrices},
  24. author={Gast, Theodore and Fu, Chuyuan and Jiang, Chenfanfu and Teran, Joseph},
  25. year={2016},
  26. institution={University of California Los Angeles}
  27. }
  28. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  29. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  30. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  31. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  32. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  33. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  34. SOFTWARE.
  35. **/
  36. #ifndef btImplicitQRSVD_h
  37. #define btImplicitQRSVD_h
  38. #include <limits>
  39. #include "btMatrix3x3.h"
  40. class btMatrix2x2
  41. {
  42. public:
  43. btScalar m_00, m_01, m_10, m_11;
  44. btMatrix2x2(): m_00(0), m_10(0), m_01(0), m_11(0)
  45. {
  46. }
  47. btMatrix2x2(const btMatrix2x2& other): m_00(other.m_00),m_01(other.m_01),m_10(other.m_10),m_11(other.m_11)
  48. {}
  49. btScalar& operator()(int i, int j)
  50. {
  51. if (i == 0 && j == 0)
  52. return m_00;
  53. if (i == 1 && j == 0)
  54. return m_10;
  55. if (i == 0 && j == 1)
  56. return m_01;
  57. if (i == 1 && j == 1)
  58. return m_11;
  59. btAssert(false);
  60. return m_00;
  61. }
  62. const btScalar& operator()(int i, int j) const
  63. {
  64. if (i == 0 && j == 0)
  65. return m_00;
  66. if (i == 1 && j == 0)
  67. return m_10;
  68. if (i == 0 && j == 1)
  69. return m_01;
  70. if (i == 1 && j == 1)
  71. return m_11;
  72. btAssert(false);
  73. return m_00;
  74. }
  75. void setIdentity()
  76. {
  77. m_00 = 1;
  78. m_11 = 1;
  79. m_01 = 0;
  80. m_10 = 0;
  81. }
  82. };
  83. static inline btScalar copySign(btScalar x, btScalar y) {
  84. if ((x < 0 && y > 0) || (x > 0 && y < 0))
  85. return -x;
  86. return x;
  87. }
  88. /**
  89. Class for givens rotation.
  90. Row rotation G*A corresponds to something like
  91. c -s 0
  92. ( s c 0 ) A
  93. 0 0 1
  94. Column rotation A G' corresponds to something like
  95. c -s 0
  96. A ( s c 0 )
  97. 0 0 1
  98. c and s are always computed so that
  99. ( c -s ) ( a ) = ( * )
  100. s c b ( 0 )
  101. Assume rowi<rowk.
  102. */
  103. class GivensRotation {
  104. public:
  105. int rowi;
  106. int rowk;
  107. btScalar c;
  108. btScalar s;
  109. inline GivensRotation(int rowi_in, int rowk_in)
  110. : rowi(rowi_in)
  111. , rowk(rowk_in)
  112. , c(1)
  113. , s(0)
  114. {
  115. }
  116. inline GivensRotation(btScalar a, btScalar b, int rowi_in, int rowk_in)
  117. : rowi(rowi_in)
  118. , rowk(rowk_in)
  119. {
  120. compute(a, b);
  121. }
  122. ~GivensRotation() {}
  123. inline void transposeInPlace()
  124. {
  125. s = -s;
  126. }
  127. /**
  128. Compute c and s from a and b so that
  129. ( c -s ) ( a ) = ( * )
  130. s c b ( 0 )
  131. */
  132. inline void compute(const btScalar a, const btScalar b)
  133. {
  134. btScalar d = a * a + b * b;
  135. c = 1;
  136. s = 0;
  137. if (d > SIMD_EPSILON) {
  138. btScalar sqrtd = btSqrt(d);
  139. if (sqrtd>SIMD_EPSILON)
  140. {
  141. btScalar t = btScalar(1.0)/sqrtd;
  142. c = a * t;
  143. s = -b * t;
  144. }
  145. }
  146. }
  147. /**
  148. This function computes c and s so that
  149. ( c -s ) ( a ) = ( 0 )
  150. s c b ( * )
  151. */
  152. inline void computeUnconventional(const btScalar a, const btScalar b)
  153. {
  154. btScalar d = a * a + b * b;
  155. c = 0;
  156. s = 1;
  157. if (d > SIMD_EPSILON) {
  158. btScalar t = btScalar(1.0)/btSqrt(d);
  159. s = a * t;
  160. c = b * t;
  161. }
  162. }
  163. /**
  164. Fill the R with the entries of this rotation
  165. */
  166. inline void fill(const btMatrix3x3& R) const
  167. {
  168. btMatrix3x3& A = const_cast<btMatrix3x3&>(R);
  169. A.setIdentity();
  170. A[rowi][rowi] = c;
  171. A[rowk][rowi] = -s;
  172. A[rowi][rowk] = s;
  173. A[rowk][rowk] = c;
  174. }
  175. inline void fill(const btMatrix2x2& R) const
  176. {
  177. btMatrix2x2& A = const_cast<btMatrix2x2&>(R);
  178. A(rowi,rowi) = c;
  179. A(rowk,rowi) = -s;
  180. A(rowi,rowk) = s;
  181. A(rowk,rowk) = c;
  182. }
  183. /**
  184. This function does something like
  185. c -s 0
  186. ( s c 0 ) A -> A
  187. 0 0 1
  188. It only affects row i and row k of A.
  189. */
  190. inline void rowRotation(btMatrix3x3& A) const
  191. {
  192. for (int j = 0; j < 3; j++) {
  193. btScalar tau1 = A[rowi][j];
  194. btScalar tau2 = A[rowk][j];
  195. A[rowi][j] = c * tau1 - s * tau2;
  196. A[rowk][j] = s * tau1 + c * tau2;
  197. }
  198. }
  199. inline void rowRotation(btMatrix2x2& A) const
  200. {
  201. for (int j = 0; j < 2; j++) {
  202. btScalar tau1 = A(rowi,j);
  203. btScalar tau2 = A(rowk,j);
  204. A(rowi,j) = c * tau1 - s * tau2;
  205. A(rowk,j) = s * tau1 + c * tau2;
  206. }
  207. }
  208. /**
  209. This function does something like
  210. c s 0
  211. A ( -s c 0 ) -> A
  212. 0 0 1
  213. It only affects column i and column k of A.
  214. */
  215. inline void columnRotation(btMatrix3x3& A) const
  216. {
  217. for (int j = 0; j < 3; j++) {
  218. btScalar tau1 = A[j][rowi];
  219. btScalar tau2 = A[j][rowk];
  220. A[j][rowi] = c * tau1 - s * tau2;
  221. A[j][rowk] = s * tau1 + c * tau2;
  222. }
  223. }
  224. inline void columnRotation(btMatrix2x2& A) const
  225. {
  226. for (int j = 0; j < 2; j++) {
  227. btScalar tau1 = A(j,rowi);
  228. btScalar tau2 = A(j,rowk);
  229. A(j,rowi) = c * tau1 - s * tau2;
  230. A(j,rowk) = s * tau1 + c * tau2;
  231. }
  232. }
  233. /**
  234. Multiply givens must be for same row and column
  235. **/
  236. inline void operator*=(const GivensRotation& A)
  237. {
  238. btScalar new_c = c * A.c - s * A.s;
  239. btScalar new_s = s * A.c + c * A.s;
  240. c = new_c;
  241. s = new_s;
  242. }
  243. /**
  244. Multiply givens must be for same row and column
  245. **/
  246. inline GivensRotation operator*(const GivensRotation& A) const
  247. {
  248. GivensRotation r(*this);
  249. r *= A;
  250. return r;
  251. }
  252. };
  253. /**
  254. \brief zero chasing the 3X3 matrix to bidiagonal form
  255. original form of H: x x 0
  256. x x x
  257. 0 0 x
  258. after zero chase:
  259. x x 0
  260. 0 x x
  261. 0 0 x
  262. */
  263. inline void zeroChase(btMatrix3x3& H, btMatrix3x3& U, btMatrix3x3& V)
  264. {
  265. /**
  266. Reduce H to of form
  267. x x +
  268. 0 x x
  269. 0 0 x
  270. */
  271. GivensRotation r1(H[0][0], H[1][0], 0, 1);
  272. /**
  273. Reduce H to of form
  274. x x 0
  275. 0 x x
  276. 0 + x
  277. Can calculate r2 without multiplying by r1 since both entries are in first two
  278. rows thus no need to divide by sqrt(a^2+b^2)
  279. */
  280. GivensRotation r2(1, 2);
  281. if (H[1][0] != 0)
  282. r2.compute(H[0][0] * H[0][1] + H[1][0] * H[1][1], H[0][0] * H[0][2] + H[1][0] * H[1][2]);
  283. else
  284. r2.compute(H[0][1], H[0][2]);
  285. r1.rowRotation(H);
  286. /* GivensRotation<T> r2(H(0, 1), H(0, 2), 1, 2); */
  287. r2.columnRotation(H);
  288. r2.columnRotation(V);
  289. /**
  290. Reduce H to of form
  291. x x 0
  292. 0 x x
  293. 0 0 x
  294. */
  295. GivensRotation r3(H[1][1], H[2][1], 1, 2);
  296. r3.rowRotation(H);
  297. // Save this till end for better cache coherency
  298. // r1.rowRotation(u_transpose);
  299. // r3.rowRotation(u_transpose);
  300. r1.columnRotation(U);
  301. r3.columnRotation(U);
  302. }
  303. /**
  304. \brief make a 3X3 matrix to upper bidiagonal form
  305. original form of H: x x x
  306. x x x
  307. x x x
  308. after zero chase:
  309. x x 0
  310. 0 x x
  311. 0 0 x
  312. */
  313. inline void makeUpperBidiag(btMatrix3x3& H, btMatrix3x3& U, btMatrix3x3& V)
  314. {
  315. U.setIdentity();
  316. V.setIdentity();
  317. /**
  318. Reduce H to of form
  319. x x x
  320. x x x
  321. 0 x x
  322. */
  323. GivensRotation r(H[1][0], H[2][0], 1, 2);
  324. r.rowRotation(H);
  325. // r.rowRotation(u_transpose);
  326. r.columnRotation(U);
  327. // zeroChase(H, u_transpose, V);
  328. zeroChase(H, U, V);
  329. }
  330. /**
  331. \brief make a 3X3 matrix to lambda shape
  332. original form of H: x x x
  333. * x x x
  334. * x x x
  335. after :
  336. * x 0 0
  337. * x x 0
  338. * x 0 x
  339. */
  340. inline void makeLambdaShape(btMatrix3x3& H, btMatrix3x3& U, btMatrix3x3& V)
  341. {
  342. U.setIdentity();
  343. V.setIdentity();
  344. /**
  345. Reduce H to of form
  346. * x x 0
  347. * x x x
  348. * x x x
  349. */
  350. GivensRotation r1(H[0][1], H[0][2], 1, 2);
  351. r1.columnRotation(H);
  352. r1.columnRotation(V);
  353. /**
  354. Reduce H to of form
  355. * x x 0
  356. * x x 0
  357. * x x x
  358. */
  359. r1.computeUnconventional(H[1][2], H[2][2]);
  360. r1.rowRotation(H);
  361. r1.columnRotation(U);
  362. /**
  363. Reduce H to of form
  364. * x x 0
  365. * x x 0
  366. * x 0 x
  367. */
  368. GivensRotation r2(H[2][0], H[2][1], 0, 1);
  369. r2.columnRotation(H);
  370. r2.columnRotation(V);
  371. /**
  372. Reduce H to of form
  373. * x 0 0
  374. * x x 0
  375. * x 0 x
  376. */
  377. r2.computeUnconventional(H[0][1], H[1][1]);
  378. r2.rowRotation(H);
  379. r2.columnRotation(U);
  380. }
  381. /**
  382. \brief 2x2 polar decomposition.
  383. \param[in] A matrix.
  384. \param[out] R Robustly a rotation matrix.
  385. \param[out] S_Sym Symmetric. Whole matrix is stored
  386. Polar guarantees negative sign is on the small magnitude singular value.
  387. S is guaranteed to be the closest one to identity.
  388. R is guaranteed to be the closest rotation to A.
  389. */
  390. inline void polarDecomposition(const btMatrix2x2& A,
  391. GivensRotation& R,
  392. const btMatrix2x2& S_Sym)
  393. {
  394. btScalar a = (A(0, 0) + A(1, 1)), b = (A(1, 0) - A(0, 1));
  395. btScalar denominator = btSqrt(a*a+b*b);
  396. R.c = (btScalar)1;
  397. R.s = (btScalar)0;
  398. if (denominator > SIMD_EPSILON) {
  399. /*
  400. No need to use a tolerance here because x(0) and x(1) always have
  401. smaller magnitude then denominator, therefore overflow never happens.
  402. In Bullet, we use a tolerance anyway.
  403. */
  404. R.c = a / denominator;
  405. R.s = -b / denominator;
  406. }
  407. btMatrix2x2& S = const_cast<btMatrix2x2&>(S_Sym);
  408. S = A;
  409. R.rowRotation(S);
  410. }
  411. inline void polarDecomposition(const btMatrix2x2& A,
  412. const btMatrix2x2& R,
  413. const btMatrix2x2& S_Sym)
  414. {
  415. GivensRotation r(0, 1);
  416. polarDecomposition(A, r, S_Sym);
  417. r.fill(R);
  418. }
  419. /**
  420. \brief 2x2 SVD (singular value decomposition) A=USV'
  421. \param[in] A Input matrix.
  422. \param[out] U Robustly a rotation matrix in Givens form
  423. \param[out] Sigma matrix of singular values sorted with decreasing magnitude. The second one can be negative.
  424. \param[out] V Robustly a rotation matrix in Givens form
  425. */
  426. inline void singularValueDecomposition(
  427. const btMatrix2x2& A,
  428. GivensRotation& U,
  429. const btMatrix2x2& Sigma,
  430. GivensRotation& V,
  431. const btScalar tol = 64 * std::numeric_limits<btScalar>::epsilon())
  432. {
  433. btMatrix2x2& sigma = const_cast<btMatrix2x2&>(Sigma);
  434. sigma.setIdentity();
  435. btMatrix2x2 S_Sym;
  436. polarDecomposition(A, U, S_Sym);
  437. btScalar cosine, sine;
  438. btScalar x = S_Sym(0, 0);
  439. btScalar y = S_Sym(0, 1);
  440. btScalar z = S_Sym(1, 1);
  441. if (y == 0) {
  442. // S is already diagonal
  443. cosine = 1;
  444. sine = 0;
  445. sigma(0,0) = x;
  446. sigma(1,1) = z;
  447. }
  448. else {
  449. btScalar tau = 0.5 * (x - z);
  450. btScalar val = tau * tau + y * y;
  451. if (val > SIMD_EPSILON)
  452. {
  453. btScalar w = btSqrt(val);
  454. // w > y > 0
  455. btScalar t;
  456. if (tau > 0) {
  457. // tau + w > w > y > 0 ==> division is safe
  458. t = y / (tau + w);
  459. }
  460. else {
  461. // tau - w < -w < -y < 0 ==> division is safe
  462. t = y / (tau - w);
  463. }
  464. cosine = btScalar(1) / btSqrt(t * t + btScalar(1));
  465. sine = -t * cosine;
  466. /*
  467. V = [cosine -sine; sine cosine]
  468. Sigma = V'SV. Only compute the diagonals for efficiency.
  469. Also utilize symmetry of S and don't form V yet.
  470. */
  471. btScalar c2 = cosine * cosine;
  472. btScalar csy = 2 * cosine * sine * y;
  473. btScalar s2 = sine * sine;
  474. sigma(0,0) = c2 * x - csy + s2 * z;
  475. sigma(1,1) = s2 * x + csy + c2 * z;
  476. } else
  477. {
  478. cosine = 1;
  479. sine = 0;
  480. sigma(0,0) = x;
  481. sigma(1,1) = z;
  482. }
  483. }
  484. // Sorting
  485. // Polar already guarantees negative sign is on the small magnitude singular value.
  486. if (sigma(0,0) < sigma(1,1)) {
  487. std::swap(sigma(0,0), sigma(1,1));
  488. V.c = -sine;
  489. V.s = cosine;
  490. }
  491. else {
  492. V.c = cosine;
  493. V.s = sine;
  494. }
  495. U *= V;
  496. }
  497. /**
  498. \brief 2x2 SVD (singular value decomposition) A=USV'
  499. \param[in] A Input matrix.
  500. \param[out] U Robustly a rotation matrix.
  501. \param[out] Sigma Vector of singular values sorted with decreasing magnitude. The second one can be negative.
  502. \param[out] V Robustly a rotation matrix.
  503. */
  504. inline void singularValueDecomposition(
  505. const btMatrix2x2& A,
  506. const btMatrix2x2& U,
  507. const btMatrix2x2& Sigma,
  508. const btMatrix2x2& V,
  509. const btScalar tol = 64 * std::numeric_limits<btScalar>::epsilon())
  510. {
  511. GivensRotation gv(0, 1);
  512. GivensRotation gu(0, 1);
  513. singularValueDecomposition(A, gu, Sigma, gv);
  514. gu.fill(U);
  515. gv.fill(V);
  516. }
  517. /**
  518. \brief compute wilkinsonShift of the block
  519. a1 b1
  520. b1 a2
  521. based on the wilkinsonShift formula
  522. mu = c + d - sign (d) \ sqrt (d*d + b*b), where d = (a-c)/2
  523. */
  524. inline btScalar wilkinsonShift(const btScalar a1, const btScalar b1, const btScalar a2)
  525. {
  526. btScalar d = (btScalar)0.5 * (a1 - a2);
  527. btScalar bs = b1 * b1;
  528. btScalar val = d * d + bs;
  529. if (val>SIMD_EPSILON)
  530. {
  531. btScalar denom = btFabs(d) + btSqrt(val);
  532. btScalar mu = a2 - copySign(bs / (denom), d);
  533. // T mu = a2 - bs / ( d + sign_d*sqrt (d*d + bs));
  534. return mu;
  535. }
  536. return a2;
  537. }
  538. /**
  539. \brief Helper function of 3X3 SVD for processing 2X2 SVD
  540. */
  541. template <int t>
  542. inline void process(btMatrix3x3& B, btMatrix3x3& U, btVector3& sigma, btMatrix3x3& V)
  543. {
  544. int other = (t == 1) ? 0 : 2;
  545. GivensRotation u(0, 1);
  546. GivensRotation v(0, 1);
  547. sigma[other] = B[other][other];
  548. btMatrix2x2 B_sub, sigma_sub;
  549. if (t == 0)
  550. {
  551. B_sub.m_00 = B[0][0];
  552. B_sub.m_10 = B[1][0];
  553. B_sub.m_01 = B[0][1];
  554. B_sub.m_11 = B[1][1];
  555. sigma_sub.m_00 = sigma[0];
  556. sigma_sub.m_11 = sigma[1];
  557. // singularValueDecomposition(B.template block<2, 2>(t, t), u, sigma.template block<2, 1>(t, 0), v);
  558. singularValueDecomposition(B_sub, u, sigma_sub, v);
  559. B[0][0] = B_sub.m_00;
  560. B[1][0] = B_sub.m_10;
  561. B[0][1] = B_sub.m_01;
  562. B[1][1] = B_sub.m_11;
  563. sigma[0] = sigma_sub.m_00;
  564. sigma[1] = sigma_sub.m_11;
  565. }
  566. else
  567. {
  568. B_sub.m_00 = B[1][1];
  569. B_sub.m_10 = B[2][1];
  570. B_sub.m_01 = B[1][2];
  571. B_sub.m_11 = B[2][2];
  572. sigma_sub.m_00 = sigma[1];
  573. sigma_sub.m_11 = sigma[2];
  574. // singularValueDecomposition(B.template block<2, 2>(t, t), u, sigma.template block<2, 1>(t, 0), v);
  575. singularValueDecomposition(B_sub, u, sigma_sub, v);
  576. B[1][1] = B_sub.m_00;
  577. B[2][1] = B_sub.m_10;
  578. B[1][2] = B_sub.m_01;
  579. B[2][2] = B_sub.m_11;
  580. sigma[1] = sigma_sub.m_00;
  581. sigma[2] = sigma_sub.m_11;
  582. }
  583. u.rowi += t;
  584. u.rowk += t;
  585. v.rowi += t;
  586. v.rowk += t;
  587. u.columnRotation(U);
  588. v.columnRotation(V);
  589. }
  590. /**
  591. \brief Helper function of 3X3 SVD for flipping signs due to flipping signs of sigma
  592. */
  593. inline void flipSign(int i, btMatrix3x3& U, btVector3& sigma)
  594. {
  595. sigma[i] = -sigma[i];
  596. U[0][i] = -U[0][i];
  597. U[1][i] = -U[1][i];
  598. U[2][i] = -U[2][i];
  599. }
  600. inline void flipSign(int i, btMatrix3x3& U)
  601. {
  602. U[0][i] = -U[0][i];
  603. U[1][i] = -U[1][i];
  604. U[2][i] = -U[2][i];
  605. }
  606. inline void swapCol(btMatrix3x3& A, int i, int j)
  607. {
  608. for (int d = 0; d < 3; ++d)
  609. std::swap(A[d][i], A[d][j]);
  610. }
  611. /**
  612. \brief Helper function of 3X3 SVD for sorting singular values
  613. */
  614. inline void sort(btMatrix3x3& U, btVector3& sigma, btMatrix3x3& V, int t)
  615. {
  616. if (t == 0)
  617. {
  618. // Case: sigma(0) > |sigma(1)| >= |sigma(2)|
  619. if (btFabs(sigma[1]) >= btFabs(sigma[2])) {
  620. if (sigma[1] < 0) {
  621. flipSign(1, U, sigma);
  622. flipSign(2, U, sigma);
  623. }
  624. return;
  625. }
  626. //fix sign of sigma for both cases
  627. if (sigma[2] < 0) {
  628. flipSign(1, U, sigma);
  629. flipSign(2, U, sigma);
  630. }
  631. //swap sigma(1) and sigma(2) for both cases
  632. std::swap(sigma[1], sigma[2]);
  633. // swap the col 1 and col 2 for U,V
  634. swapCol(U,1,2);
  635. swapCol(V,1,2);
  636. // Case: |sigma(2)| >= sigma(0) > |simga(1)|
  637. if (sigma[1] > sigma[0]) {
  638. std::swap(sigma[0], sigma[1]);
  639. swapCol(U,0,1);
  640. swapCol(V,0,1);
  641. }
  642. // Case: sigma(0) >= |sigma(2)| > |simga(1)|
  643. else {
  644. flipSign(2, U);
  645. flipSign(2, V);
  646. }
  647. }
  648. else if (t == 1)
  649. {
  650. // Case: |sigma(0)| >= sigma(1) > |sigma(2)|
  651. if (btFabs(sigma[0]) >= sigma[1]) {
  652. if (sigma[0] < 0) {
  653. flipSign(0, U, sigma);
  654. flipSign(2, U, sigma);
  655. }
  656. return;
  657. }
  658. //swap sigma(0) and sigma(1) for both cases
  659. std::swap(sigma[0], sigma[1]);
  660. swapCol(U, 0, 1);
  661. swapCol(V, 0, 1);
  662. // Case: sigma(1) > |sigma(2)| >= |sigma(0)|
  663. if (btFabs(sigma[1]) < btFabs(sigma[2])) {
  664. std::swap(sigma[1], sigma[2]);
  665. swapCol(U, 1, 2);
  666. swapCol(V, 1, 2);
  667. }
  668. // Case: sigma(1) >= |sigma(0)| > |sigma(2)|
  669. else {
  670. flipSign(1, U);
  671. flipSign(1, V);
  672. }
  673. // fix sign for both cases
  674. if (sigma[1] < 0) {
  675. flipSign(1, U, sigma);
  676. flipSign(2, U, sigma);
  677. }
  678. }
  679. }
  680. /**
  681. \brief 3X3 SVD (singular value decomposition) A=USV'
  682. \param[in] A Input matrix.
  683. \param[out] U is a rotation matrix.
  684. \param[out] sigma Diagonal matrix, sorted with decreasing magnitude. The third one can be negative.
  685. \param[out] V is a rotation matrix.
  686. */
  687. inline int singularValueDecomposition(const btMatrix3x3& A,
  688. btMatrix3x3& U,
  689. btVector3& sigma,
  690. btMatrix3x3& V,
  691. btScalar tol = 128*std::numeric_limits<btScalar>::epsilon())
  692. {
  693. // using std::fabs;
  694. btMatrix3x3 B = A;
  695. U.setIdentity();
  696. V.setIdentity();
  697. makeUpperBidiag(B, U, V);
  698. int count = 0;
  699. btScalar mu = (btScalar)0;
  700. GivensRotation r(0, 1);
  701. btScalar alpha_1 = B[0][0];
  702. btScalar beta_1 = B[0][1];
  703. btScalar alpha_2 = B[1][1];
  704. btScalar alpha_3 = B[2][2];
  705. btScalar beta_2 = B[1][2];
  706. btScalar gamma_1 = alpha_1 * beta_1;
  707. btScalar gamma_2 = alpha_2 * beta_2;
  708. btScalar val = alpha_1 * alpha_1 + alpha_2 * alpha_2 + alpha_3 * alpha_3 + beta_1 * beta_1 + beta_2 * beta_2;
  709. if (val > SIMD_EPSILON)
  710. {
  711. tol *= btMax((btScalar)0.5 * btSqrt(val), (btScalar)1);
  712. }
  713. /**
  714. Do implicit shift QR until A^T A is block diagonal
  715. */
  716. int max_count = 100;
  717. while (btFabs(beta_2) > tol && btFabs(beta_1) > tol
  718. && btFabs(alpha_1) > tol && btFabs(alpha_2) > tol
  719. && btFabs(alpha_3) > tol
  720. && count < max_count) {
  721. mu = wilkinsonShift(alpha_2 * alpha_2 + beta_1 * beta_1, gamma_2, alpha_3 * alpha_3 + beta_2 * beta_2);
  722. r.compute(alpha_1 * alpha_1 - mu, gamma_1);
  723. r.columnRotation(B);
  724. r.columnRotation(V);
  725. zeroChase(B, U, V);
  726. alpha_1 = B[0][0];
  727. beta_1 = B[0][1];
  728. alpha_2 = B[1][1];
  729. alpha_3 = B[2][2];
  730. beta_2 = B[1][2];
  731. gamma_1 = alpha_1 * beta_1;
  732. gamma_2 = alpha_2 * beta_2;
  733. count++;
  734. }
  735. /**
  736. Handle the cases of one of the alphas and betas being 0
  737. Sorted by ease of handling and then frequency
  738. of occurrence
  739. If B is of form
  740. x x 0
  741. 0 x 0
  742. 0 0 x
  743. */
  744. if (btFabs(beta_2) <= tol) {
  745. process<0>(B, U, sigma, V);
  746. sort(U, sigma, V,0);
  747. }
  748. /**
  749. If B is of form
  750. x 0 0
  751. 0 x x
  752. 0 0 x
  753. */
  754. else if (btFabs(beta_1) <= tol) {
  755. process<1>(B, U, sigma, V);
  756. sort(U, sigma, V,1);
  757. }
  758. /**
  759. If B is of form
  760. x x 0
  761. 0 0 x
  762. 0 0 x
  763. */
  764. else if (btFabs(alpha_2) <= tol) {
  765. /**
  766. Reduce B to
  767. x x 0
  768. 0 0 0
  769. 0 0 x
  770. */
  771. GivensRotation r1(1, 2);
  772. r1.computeUnconventional(B[1][2], B[2][2]);
  773. r1.rowRotation(B);
  774. r1.columnRotation(U);
  775. process<0>(B, U, sigma, V);
  776. sort(U, sigma, V, 0);
  777. }
  778. /**
  779. If B is of form
  780. x x 0
  781. 0 x x
  782. 0 0 0
  783. */
  784. else if (btFabs(alpha_3) <= tol) {
  785. /**
  786. Reduce B to
  787. x x +
  788. 0 x 0
  789. 0 0 0
  790. */
  791. GivensRotation r1(1, 2);
  792. r1.compute(B[1][1], B[1][2]);
  793. r1.columnRotation(B);
  794. r1.columnRotation(V);
  795. /**
  796. Reduce B to
  797. x x 0
  798. + x 0
  799. 0 0 0
  800. */
  801. GivensRotation r2(0, 2);
  802. r2.compute(B[0][0], B[0][2]);
  803. r2.columnRotation(B);
  804. r2.columnRotation(V);
  805. process<0>(B, U, sigma, V);
  806. sort(U, sigma, V, 0);
  807. }
  808. /**
  809. If B is of form
  810. 0 x 0
  811. 0 x x
  812. 0 0 x
  813. */
  814. else if (btFabs(alpha_1) <= tol) {
  815. /**
  816. Reduce B to
  817. 0 0 +
  818. 0 x x
  819. 0 0 x
  820. */
  821. GivensRotation r1(0, 1);
  822. r1.computeUnconventional(B[0][1], B[1][1]);
  823. r1.rowRotation(B);
  824. r1.columnRotation(U);
  825. /**
  826. Reduce B to
  827. 0 0 0
  828. 0 x x
  829. 0 + x
  830. */
  831. GivensRotation r2(0, 2);
  832. r2.computeUnconventional(B[0][2], B[2][2]);
  833. r2.rowRotation(B);
  834. r2.columnRotation(U);
  835. process<1>(B, U, sigma, V);
  836. sort(U, sigma, V, 1);
  837. }
  838. return count;
  839. }
  840. #endif /* btImplicitQRSVD_h */