odemath.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*************************************************************************
  2. * *
  3. * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
  4. * All rights reserved. Email: [email protected] Web: www.q12.org *
  5. * *
  6. * This library is free software; you can redistribute it and/or *
  7. * modify it under the terms of EITHER: *
  8. * (1) The GNU Lesser General Public License as published by the Free *
  9. * Software Foundation; either version 2.1 of the License, or (at *
  10. * your option) any later version. The text of the GNU Lesser *
  11. * General Public License is included with this library in the *
  12. * file LICENSE.TXT. *
  13. * (2) The BSD-style license that is included with this library in *
  14. * the file LICENSE-BSD.TXT. *
  15. * *
  16. * This library is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
  19. * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
  20. * *
  21. *************************************************************************/
  22. #ifndef _ODE_ODEMATH_H_
  23. #define _ODE_ODEMATH_H_
  24. #include <ode/common.h>
  25. /*
  26. * macro to access elements i,j in an NxM matrix A, independent of the
  27. * matrix storage convention.
  28. */
  29. #define dACCESS33(A,i,j) ((A)[(i)*4+(j)])
  30. /*
  31. * Macro to test for valid floating point values
  32. */
  33. #define dVALIDVEC3(v) (!(dIsNan(v[0]) || dIsNan(v[1]) || dIsNan(v[2])))
  34. #define dVALIDVEC4(v) (!(dIsNan(v[0]) || dIsNan(v[1]) || dIsNan(v[2]) || dIsNan(v[3])))
  35. #define dVALIDMAT3(m) (!(dIsNan(m[0]) || dIsNan(m[1]) || dIsNan(m[2]) || dIsNan(m[3]) || dIsNan(m[4]) || dIsNan(m[5]) || dIsNan(m[6]) || dIsNan(m[7]) || dIsNan(m[8]) || dIsNan(m[9]) || dIsNan(m[10]) || dIsNan(m[11])))
  36. #define dVALIDMAT4(m) (!(dIsNan(m[0]) || dIsNan(m[1]) || dIsNan(m[2]) || dIsNan(m[3]) || dIsNan(m[4]) || dIsNan(m[5]) || dIsNan(m[6]) || dIsNan(m[7]) || dIsNan(m[8]) || dIsNan(m[9]) || dIsNan(m[10]) || dIsNan(m[11]) || dIsNan(m[12]) || dIsNan(m[13]) || dIsNan(m[14]) || dIsNan(m[15]) ))
  37. ODE_PURE_INLINE void dZeroVector3(dVector3 res)
  38. {
  39. res[dV3E_X] = REAL(0.0);
  40. res[dV3E_Y] = REAL(0.0);
  41. res[dV3E_Z] = REAL(0.0);
  42. }
  43. ODE_PURE_INLINE void dAssignVector3(dVector3 res, dReal x, dReal y, dReal z)
  44. {
  45. res[dV3E_X] = x;
  46. res[dV3E_Y] = y;
  47. res[dV3E_Z] = z;
  48. }
  49. ODE_PURE_INLINE void dZeroMatrix3(dMatrix3 res)
  50. {
  51. res[dM3E_XX] = REAL(0.0); res[dM3E_XY] = REAL(0.0); res[dM3E_XZ] = REAL(0.0);
  52. res[dM3E_YX] = REAL(0.0); res[dM3E_YY] = REAL(0.0); res[dM3E_YZ] = REAL(0.0);
  53. res[dM3E_ZX] = REAL(0.0); res[dM3E_ZY] = REAL(0.0); res[dM3E_ZZ] = REAL(0.0);
  54. }
  55. ODE_PURE_INLINE void dZeroMatrix4(dMatrix4 res)
  56. {
  57. res[dM4E_XX] = REAL(0.0); res[dM4E_XY] = REAL(0.0); res[dM4E_XZ] = REAL(0.0); res[dM4E_XO] = REAL(0.0);
  58. res[dM4E_YX] = REAL(0.0); res[dM4E_YY] = REAL(0.0); res[dM4E_YZ] = REAL(0.0); res[dM4E_YO] = REAL(0.0);
  59. res[dM4E_ZX] = REAL(0.0); res[dM4E_ZY] = REAL(0.0); res[dM4E_ZZ] = REAL(0.0); res[dM4E_ZO] = REAL(0.0);
  60. res[dM4E_OX] = REAL(0.0); res[dM4E_OY] = REAL(0.0); res[dM4E_OZ] = REAL(0.0); res[dM4E_OO] = REAL(0.0);
  61. }
  62. /* Some vector math */
  63. ODE_PURE_INLINE void dAddVectors3(dReal *res, const dReal *a, const dReal *b)
  64. {
  65. const dReal res_0 = a[0] + b[0];
  66. const dReal res_1 = a[1] + b[1];
  67. const dReal res_2 = a[2] + b[2];
  68. /* Only assign after all the calculations are over to avoid incurring memory aliasing*/
  69. res[0] = res_0; res[1] = res_1; res[2] = res_2;
  70. }
  71. ODE_PURE_INLINE void dSubtractVectors3(dReal *res, const dReal *a, const dReal *b)
  72. {
  73. const dReal res_0 = a[0] - b[0];
  74. const dReal res_1 = a[1] - b[1];
  75. const dReal res_2 = a[2] - b[2];
  76. /* Only assign after all the calculations are over to avoid incurring memory aliasing*/
  77. res[0] = res_0; res[1] = res_1; res[2] = res_2;
  78. }
  79. ODE_PURE_INLINE void dAddVectorScaledVector3(dReal *res, const dReal *a, const dReal *b, dReal b_scale)
  80. {
  81. const dReal res_0 = a[0] + b_scale * b[0];
  82. const dReal res_1 = a[1] + b_scale * b[1];
  83. const dReal res_2 = a[2] + b_scale * b[2];
  84. /* Only assign after all the calculations are over to avoid incurring memory aliasing*/
  85. res[0] = res_0; res[1] = res_1; res[2] = res_2;
  86. }
  87. ODE_PURE_INLINE void dAddScaledVectors3(dReal *res, const dReal *a, const dReal *b, dReal a_scale, dReal b_scale)
  88. {
  89. const dReal res_0 = a_scale * a[0] + b_scale * b[0];
  90. const dReal res_1 = a_scale * a[1] + b_scale * b[1];
  91. const dReal res_2 = a_scale * a[2] + b_scale * b[2];
  92. /* Only assign after all the calculations are over to avoid incurring memory aliasing*/
  93. res[0] = res_0; res[1] = res_1; res[2] = res_2;
  94. }
  95. ODE_PURE_INLINE void dAddThreeScaledVectors3(dReal *res, const dReal *a, const dReal *b, const dReal *c, dReal a_scale, dReal b_scale, dReal c_scale)
  96. {
  97. const dReal res_0 = a_scale * a[0] + b_scale * b[0] + c_scale * c[0];
  98. const dReal res_1 = a_scale * a[1] + b_scale * b[1] + c_scale * c[1];
  99. const dReal res_2 = a_scale * a[2] + b_scale * b[2] + c_scale * c[2];
  100. /* Only assign after all the calculations are over to avoid incurring memory aliasing*/
  101. res[0] = res_0; res[1] = res_1; res[2] = res_2;
  102. }
  103. ODE_PURE_INLINE void dScaleVector3(dReal *res, dReal nScale)
  104. {
  105. res[0] *= nScale ;
  106. res[1] *= nScale ;
  107. res[2] *= nScale ;
  108. }
  109. ODE_PURE_INLINE void dNegateVector3(dReal *res)
  110. {
  111. res[0] = -res[0];
  112. res[1] = -res[1];
  113. res[2] = -res[2];
  114. }
  115. ODE_PURE_INLINE void dCopyVector3(dReal *res, const dReal *a)
  116. {
  117. const dReal res_0 = a[0];
  118. const dReal res_1 = a[1];
  119. const dReal res_2 = a[2];
  120. /* Only assign after all the calculations are over to avoid incurring memory aliasing*/
  121. res[0] = res_0; res[1] = res_1; res[2] = res_2;
  122. }
  123. ODE_PURE_INLINE void dCopyScaledVector3(dReal *res, const dReal *a, dReal nScale)
  124. {
  125. const dReal res_0 = a[0] * nScale;
  126. const dReal res_1 = a[1] * nScale;
  127. const dReal res_2 = a[2] * nScale;
  128. /* Only assign after all the calculations are over to avoid incurring memory aliasing*/
  129. res[0] = res_0; res[1] = res_1; res[2] = res_2;
  130. }
  131. ODE_PURE_INLINE void dCopyNegatedVector3(dReal *res, const dReal *a)
  132. {
  133. const dReal res_0 = -a[0];
  134. const dReal res_1 = -a[1];
  135. const dReal res_2 = -a[2];
  136. /* Only assign after all the calculations are over to avoid incurring memory aliasing*/
  137. res[0] = res_0; res[1] = res_1; res[2] = res_2;
  138. }
  139. ODE_PURE_INLINE void dCopyVector4(dReal *res, const dReal *a)
  140. {
  141. const dReal res_0 = a[0];
  142. const dReal res_1 = a[1];
  143. const dReal res_2 = a[2];
  144. const dReal res_3 = a[3];
  145. /* Only assign after all the calculations are over to avoid incurring memory aliasing*/
  146. res[0] = res_0; res[1] = res_1; res[2] = res_2; res[3] = res_3;
  147. }
  148. ODE_PURE_INLINE void dCopyMatrix4x4(dReal *res, const dReal *a)
  149. {
  150. dCopyVector4(res + 0, a + 0);
  151. dCopyVector4(res + 4, a + 4);
  152. dCopyVector4(res + 8, a + 8);
  153. }
  154. ODE_PURE_INLINE void dCopyMatrix4x3(dReal *res, const dReal *a)
  155. {
  156. dCopyVector3(res + 0, a + 0);
  157. dCopyVector3(res + 4, a + 4);
  158. dCopyVector3(res + 8, a + 8);
  159. }
  160. ODE_PURE_INLINE void dGetMatrixColumn3(dReal *res, const dReal *a, unsigned n)
  161. {
  162. const dReal res_0 = a[n + 0];
  163. const dReal res_1 = a[n + 4];
  164. const dReal res_2 = a[n + 8];
  165. /* Only assign after all the calculations are over to avoid incurring memory aliasing*/
  166. res[0] = res_0; res[1] = res_1; res[2] = res_2;
  167. }
  168. ODE_PURE_INLINE dReal dCalcVectorLength3(const dReal *a)
  169. {
  170. return dSqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
  171. }
  172. ODE_PURE_INLINE dReal dCalcVectorLengthSquare3(const dReal *a)
  173. {
  174. return (a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
  175. }
  176. ODE_PURE_INLINE dReal dCalcPointDepth3(const dReal *test_p, const dReal *plane_p, const dReal *plane_n)
  177. {
  178. return (plane_p[0] - test_p[0]) * plane_n[0] + (plane_p[1] - test_p[1]) * plane_n[1] + (plane_p[2] - test_p[2]) * plane_n[2];
  179. }
  180. /*
  181. * 3-way dot product. _dCalcVectorDot3 means that elements of `a' and `b' are spaced
  182. * step_a and step_b indexes apart respectively. dCalcVectorDot3() means dDot311.
  183. */
  184. ODE_PURE_INLINE dReal _dCalcVectorDot3(const dReal *a, const dReal *b, unsigned step_a, unsigned step_b)
  185. {
  186. return a[0] * b[0] + a[step_a] * b[step_b] + a[2 * step_a] * b[2 * step_b];
  187. }
  188. ODE_PURE_INLINE dReal dCalcVectorDot3 (const dReal *a, const dReal *b) { return _dCalcVectorDot3(a,b,1,1); }
  189. ODE_PURE_INLINE dReal dCalcVectorDot3_13 (const dReal *a, const dReal *b) { return _dCalcVectorDot3(a,b,1,3); }
  190. ODE_PURE_INLINE dReal dCalcVectorDot3_31 (const dReal *a, const dReal *b) { return _dCalcVectorDot3(a,b,3,1); }
  191. ODE_PURE_INLINE dReal dCalcVectorDot3_33 (const dReal *a, const dReal *b) { return _dCalcVectorDot3(a,b,3,3); }
  192. ODE_PURE_INLINE dReal dCalcVectorDot3_14 (const dReal *a, const dReal *b) { return _dCalcVectorDot3(a,b,1,4); }
  193. ODE_PURE_INLINE dReal dCalcVectorDot3_41 (const dReal *a, const dReal *b) { return _dCalcVectorDot3(a,b,4,1); }
  194. ODE_PURE_INLINE dReal dCalcVectorDot3_44 (const dReal *a, const dReal *b) { return _dCalcVectorDot3(a,b,4,4); }
  195. /*
  196. * cross product, set res = a x b. _dCalcVectorCross3 means that elements of `res', `a'
  197. * and `b' are spaced step_res, step_a and step_b indexes apart respectively.
  198. * dCalcVectorCross3() means dCross3111.
  199. */
  200. ODE_PURE_INLINE void _dCalcVectorCross3(dReal *res, const dReal *a, const dReal *b, unsigned step_res, unsigned step_a, unsigned step_b)
  201. {
  202. const dReal res_0 = a[ step_a]*b[2*step_b] - a[2*step_a]*b[ step_b];
  203. const dReal res_1 = a[2*step_a]*b[ 0] - a[ 0]*b[2*step_b];
  204. const dReal res_2 = a[ 0]*b[ step_b] - a[ step_a]*b[ 0];
  205. /* Only assign after all the calculations are over to avoid incurring memory aliasing*/
  206. res[ 0] = res_0;
  207. res[ step_res] = res_1;
  208. res[2*step_res] = res_2;
  209. }
  210. ODE_PURE_INLINE void dCalcVectorCross3 (dReal *res, const dReal *a, const dReal *b) { _dCalcVectorCross3(res, a, b, 1, 1, 1); }
  211. ODE_PURE_INLINE void dCalcVectorCross3_114(dReal *res, const dReal *a, const dReal *b) { _dCalcVectorCross3(res, a, b, 1, 1, 4); }
  212. ODE_PURE_INLINE void dCalcVectorCross3_141(dReal *res, const dReal *a, const dReal *b) { _dCalcVectorCross3(res, a, b, 1, 4, 1); }
  213. ODE_PURE_INLINE void dCalcVectorCross3_144(dReal *res, const dReal *a, const dReal *b) { _dCalcVectorCross3(res, a, b, 1, 4, 4); }
  214. ODE_PURE_INLINE void dCalcVectorCross3_411(dReal *res, const dReal *a, const dReal *b) { _dCalcVectorCross3(res, a, b, 4, 1, 1); }
  215. ODE_PURE_INLINE void dCalcVectorCross3_414(dReal *res, const dReal *a, const dReal *b) { _dCalcVectorCross3(res, a, b, 4, 1, 4); }
  216. ODE_PURE_INLINE void dCalcVectorCross3_441(dReal *res, const dReal *a, const dReal *b) { _dCalcVectorCross3(res, a, b, 4, 4, 1); }
  217. ODE_PURE_INLINE void dCalcVectorCross3_444(dReal *res, const dReal *a, const dReal *b) { _dCalcVectorCross3(res, a, b, 4, 4, 4); }
  218. ODE_PURE_INLINE void dAddVectorCross3(dReal *res, const dReal *a, const dReal *b)
  219. {
  220. dReal tmp[3];
  221. dCalcVectorCross3(tmp, a, b);
  222. dAddVectors3(res, res, tmp);
  223. }
  224. ODE_PURE_INLINE void dSubtractVectorCross3(dReal *res, const dReal *a, const dReal *b)
  225. {
  226. dReal tmp[3];
  227. dCalcVectorCross3(tmp, a, b);
  228. dSubtractVectors3(res, res, tmp);
  229. }
  230. /*
  231. * set a 3x3 submatrix of A to a matrix such that submatrix(A)*b = a x b.
  232. * A is stored by rows, and has `skip' elements per row. the matrix is
  233. * assumed to be already zero, so this does not write zero elements!
  234. * if (plus,minus) is (+,-) then a positive version will be written.
  235. * if (plus,minus) is (-,+) then a negative version will be written.
  236. */
  237. ODE_PURE_INLINE void dSetCrossMatrixPlus(dReal *res, const dReal *a, unsigned skip)
  238. {
  239. const dReal a_0 = a[0], a_1 = a[1], a_2 = a[2];
  240. res[1] = -a_2;
  241. res[2] = +a_1;
  242. res[skip+0] = +a_2;
  243. res[skip+2] = -a_0;
  244. res[2*skip+0] = -a_1;
  245. res[2*skip+1] = +a_0;
  246. }
  247. ODE_PURE_INLINE void dSetCrossMatrixMinus(dReal *res, const dReal *a, unsigned skip)
  248. {
  249. const dReal a_0 = a[0], a_1 = a[1], a_2 = a[2];
  250. res[1] = +a_2;
  251. res[2] = -a_1;
  252. res[skip+0] = -a_2;
  253. res[skip+2] = +a_0;
  254. res[2*skip+0] = +a_1;
  255. res[2*skip+1] = -a_0;
  256. }
  257. /*
  258. * compute the distance between two 3D-vectors
  259. */
  260. ODE_PURE_INLINE dReal dCalcPointsDistance3(const dReal *a, const dReal *b)
  261. {
  262. dReal res;
  263. dReal tmp[3];
  264. dSubtractVectors3(tmp, a, b);
  265. res = dCalcVectorLength3(tmp);
  266. return res;
  267. }
  268. /*
  269. * special case matrix multiplication, with operator selection
  270. */
  271. ODE_PURE_INLINE void dMultiplyHelper0_331(dReal *res, const dReal *a, const dReal *b)
  272. {
  273. const dReal res_0 = dCalcVectorDot3(a, b);
  274. const dReal res_1 = dCalcVectorDot3(a + 4, b);
  275. const dReal res_2 = dCalcVectorDot3(a + 8, b);
  276. /* Only assign after all the calculations are over to avoid incurring memory aliasing*/
  277. res[0] = res_0; res[1] = res_1; res[2] = res_2;
  278. }
  279. ODE_PURE_INLINE void dMultiplyHelper1_331(dReal *res, const dReal *a, const dReal *b)
  280. {
  281. const dReal res_0 = dCalcVectorDot3_41(a, b);
  282. const dReal res_1 = dCalcVectorDot3_41(a + 1, b);
  283. const dReal res_2 = dCalcVectorDot3_41(a + 2, b);
  284. /* Only assign after all the calculations are over to avoid incurring memory aliasing*/
  285. res[0] = res_0; res[1] = res_1; res[2] = res_2;
  286. }
  287. ODE_PURE_INLINE void dMultiplyHelper0_133(dReal *res, const dReal *a, const dReal *b)
  288. {
  289. dMultiplyHelper1_331(res, b, a);
  290. }
  291. ODE_PURE_INLINE void dMultiplyHelper1_133(dReal *res, const dReal *a, const dReal *b)
  292. {
  293. const dReal res_0 = dCalcVectorDot3_44(a, b);
  294. const dReal res_1 = dCalcVectorDot3_44(a + 1, b);
  295. const dReal res_2 = dCalcVectorDot3_44(a + 2, b);
  296. /* Only assign after all the calculations are over to avoid incurring memory aliasing*/
  297. res[0] = res_0; res[1] = res_1; res[2] = res_2;
  298. }
  299. /*
  300. Note: NEVER call any of these functions/macros with the same variable for A and C,
  301. it is not equivalent to A*=B.
  302. */
  303. ODE_PURE_INLINE void dMultiply0_331(dReal *res, const dReal *a, const dReal *b)
  304. {
  305. dMultiplyHelper0_331(res, a, b);
  306. }
  307. ODE_PURE_INLINE void dMultiply1_331(dReal *res, const dReal *a, const dReal *b)
  308. {
  309. dMultiplyHelper1_331(res, a, b);
  310. }
  311. ODE_PURE_INLINE void dMultiply0_133(dReal *res, const dReal *a, const dReal *b)
  312. {
  313. dMultiplyHelper0_133(res, a, b);
  314. }
  315. ODE_PURE_INLINE void dMultiply0_333(dReal *res, const dReal *a, const dReal *b)
  316. {
  317. dMultiplyHelper0_133(res + 0, a + 0, b);
  318. dMultiplyHelper0_133(res + 4, a + 4, b);
  319. dMultiplyHelper0_133(res + 8, a + 8, b);
  320. }
  321. ODE_PURE_INLINE void dMultiply1_333(dReal *res, const dReal *a, const dReal *b)
  322. {
  323. dMultiplyHelper1_133(res + 0, b, a + 0);
  324. dMultiplyHelper1_133(res + 4, b, a + 1);
  325. dMultiplyHelper1_133(res + 8, b, a + 2);
  326. }
  327. ODE_PURE_INLINE void dMultiply2_333(dReal *res, const dReal *a, const dReal *b)
  328. {
  329. dMultiplyHelper0_331(res + 0, b, a + 0);
  330. dMultiplyHelper0_331(res + 4, b, a + 4);
  331. dMultiplyHelper0_331(res + 8, b, a + 8);
  332. }
  333. ODE_PURE_INLINE void dMultiplyAdd0_331(dReal *res, const dReal *a, const dReal *b)
  334. {
  335. dReal tmp[3];
  336. dMultiplyHelper0_331(tmp, a, b);
  337. dAddVectors3(res, res, tmp);
  338. }
  339. ODE_PURE_INLINE void dMultiplyAdd1_331(dReal *res, const dReal *a, const dReal *b)
  340. {
  341. dReal tmp[3];
  342. dMultiplyHelper1_331(tmp, a, b);
  343. dAddVectors3(res, res, tmp);
  344. }
  345. ODE_PURE_INLINE void dMultiplyAdd0_133(dReal *res, const dReal *a, const dReal *b)
  346. {
  347. dReal tmp[3];
  348. dMultiplyHelper0_133(tmp, a, b);
  349. dAddVectors3(res, res, tmp);
  350. }
  351. ODE_PURE_INLINE void dMultiplyAdd0_333(dReal *res, const dReal *a, const dReal *b)
  352. {
  353. dReal tmp[3];
  354. dMultiplyHelper0_133(tmp, a + 0, b);
  355. dAddVectors3(res+ 0, res + 0, tmp);
  356. dMultiplyHelper0_133(tmp, a + 4, b);
  357. dAddVectors3(res + 4, res + 4, tmp);
  358. dMultiplyHelper0_133(tmp, a + 8, b);
  359. dAddVectors3(res + 8, res + 8, tmp);
  360. }
  361. ODE_PURE_INLINE void dMultiplyAdd1_333(dReal *res, const dReal *a, const dReal *b)
  362. {
  363. dReal tmp[3];
  364. dMultiplyHelper1_133(tmp, b, a + 0);
  365. dAddVectors3(res + 0, res + 0, tmp);
  366. dMultiplyHelper1_133(tmp, b, a + 1);
  367. dAddVectors3(res + 4, res + 4, tmp);
  368. dMultiplyHelper1_133(tmp, b, a + 2);
  369. dAddVectors3(res + 8, res + 8, tmp);
  370. }
  371. ODE_PURE_INLINE void dMultiplyAdd2_333(dReal *res, const dReal *a, const dReal *b)
  372. {
  373. dReal tmp[3];
  374. dMultiplyHelper0_331(tmp, b, a + 0);
  375. dAddVectors3(res + 0, res + 0, tmp);
  376. dMultiplyHelper0_331(tmp, b, a + 4);
  377. dAddVectors3(res + 4, res + 4, tmp);
  378. dMultiplyHelper0_331(tmp, b, a + 8);
  379. dAddVectors3(res + 8, res + 8, tmp);
  380. }
  381. ODE_PURE_INLINE dReal dCalcMatrix3Det( const dReal* mat )
  382. {
  383. dReal det;
  384. det = mat[0] * ( mat[5]*mat[10] - mat[9]*mat[6] )
  385. - mat[1] * ( mat[4]*mat[10] - mat[8]*mat[6] )
  386. + mat[2] * ( mat[4]*mat[9] - mat[8]*mat[5] );
  387. return( det );
  388. }
  389. /**
  390. Closed form matrix inversion, copied from
  391. collision_util.h for use in the stepper.
  392. Returns the determinant.
  393. returns 0 and does nothing
  394. if the matrix is singular.
  395. */
  396. ODE_PURE_INLINE dReal dInvertMatrix3(dReal *dst, const dReal *ma)
  397. {
  398. dReal det;
  399. dReal detRecip;
  400. det = dCalcMatrix3Det( ma );
  401. /* Setting an arbitrary non-zero threshold
  402. for the determinant doesn't do anyone
  403. any favors. The condition number is the
  404. important thing. If all the eigen-values
  405. of the matrix are small, so is the
  406. determinant, but it can still be well
  407. conditioned.
  408. A single extremely large eigen-value could
  409. push the determinant over threshold, but
  410. produce a very unstable result if the other
  411. eigen-values are small. So we just say that
  412. the determinant must be non-zero and trust the
  413. caller to provide well-conditioned matrices.
  414. */
  415. if ( det == 0 )
  416. {
  417. return 0;
  418. }
  419. detRecip = dRecip(det);
  420. dst[0] = ( ma[5]*ma[10] - ma[6]*ma[9] ) * detRecip;
  421. dst[1] = ( ma[9]*ma[2] - ma[1]*ma[10] ) * detRecip;
  422. dst[2] = ( ma[1]*ma[6] - ma[5]*ma[2] ) * detRecip;
  423. dst[4] = ( ma[6]*ma[8] - ma[4]*ma[10] ) * detRecip;
  424. dst[5] = ( ma[0]*ma[10] - ma[8]*ma[2] ) * detRecip;
  425. dst[6] = ( ma[4]*ma[2] - ma[0]*ma[6] ) * detRecip;
  426. dst[8] = ( ma[4]*ma[9] - ma[8]*ma[5] ) * detRecip;
  427. dst[9] = ( ma[8]*ma[1] - ma[0]*ma[9] ) * detRecip;
  428. dst[10] = ( ma[0]*ma[5] - ma[1]*ma[4] ) * detRecip;
  429. return det;
  430. }
  431. /* Include legacy macros here */
  432. #include <ode/odemath_legacy.h>
  433. #ifdef __cplusplus
  434. extern "C" {
  435. #endif
  436. /*
  437. * normalize 3x1 and 4x1 vectors (i.e. scale them to unit length)
  438. */
  439. /* For DLL export*/
  440. ODE_API int dSafeNormalize3 (dVector3 a);
  441. ODE_API int dSafeNormalize4 (dVector4 a);
  442. ODE_API void dNormalize3 (dVector3 a); /* Potentially asserts on zero vec*/
  443. ODE_API void dNormalize4 (dVector4 a); /* Potentially asserts on zero vec*/
  444. /*
  445. * given a unit length "normal" vector n, generate vectors p and q vectors
  446. * that are an orthonormal basis for the plane space perpendicular to n.
  447. * i.e. this makes p,q such that n,p,q are all perpendicular to each other.
  448. * q will equal n x p. if n is not unit length then p will be unit length but
  449. * q wont be.
  450. */
  451. ODE_API void dPlaneSpace (const dVector3 n, dVector3 p, dVector3 q);
  452. /* Makes sure the matrix is a proper rotation, returns a boolean status */
  453. ODE_API int dOrthogonalizeR(dMatrix3 m);
  454. #ifdef __cplusplus
  455. }
  456. #endif
  457. #endif