odemath.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. #ifdef __GNUC__
  26. #define PURE_INLINE extern inline
  27. #else
  28. #define PURE_INLINE inline
  29. #endif
  30. /*
  31. * macro to access elements i,j in an NxM matrix A, independent of the
  32. * matrix storage convention.
  33. */
  34. #define dACCESS33(A,i,j) ((A)[(i)*4+(j)])
  35. /*
  36. * Macro to test for valid floating point values
  37. */
  38. #define dVALIDVEC3(v) (!(dIsNan(v[0]) || dIsNan(v[1]) || dIsNan(v[2])))
  39. #define dVALIDVEC4(v) (!(dIsNan(v[0]) || dIsNan(v[1]) || dIsNan(v[2]) || dIsNan(v[3])))
  40. #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])))
  41. #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]) ))
  42. /*
  43. * General purpose vector operations with other vectors or constants.
  44. */
  45. #define dOP(a,op,b,c) \
  46. (a)[0] = ((b)[0]) op ((c)[0]); \
  47. (a)[1] = ((b)[1]) op ((c)[1]); \
  48. (a)[2] = ((b)[2]) op ((c)[2]);
  49. #define dOPC(a,op,b,c) \
  50. (a)[0] = ((b)[0]) op (c); \
  51. (a)[1] = ((b)[1]) op (c); \
  52. (a)[2] = ((b)[2]) op (c);
  53. #define dOPE(a,op,b) \
  54. (a)[0] op ((b)[0]); \
  55. (a)[1] op ((b)[1]); \
  56. (a)[2] op ((b)[2]);
  57. #define dOPEC(a,op,c) \
  58. (a)[0] op (c); \
  59. (a)[1] op (c); \
  60. (a)[2] op (c);
  61. /// Define an equation with operatos
  62. /// For example this function can be used to replace
  63. /// <PRE>
  64. /// for (int i=0; i<3; ++i)
  65. /// a[i] += b[i] + c[i];
  66. /// </PRE>
  67. #define dOPE2(a,op1,b,op2,c) \
  68. (a)[0] op1 ((b)[0]) op2 ((c)[0]); \
  69. (a)[1] op1 ((b)[1]) op2 ((c)[1]); \
  70. (a)[2] op1 ((b)[2]) op2 ((c)[2]);
  71. /*
  72. * Length, and squared length helpers. dLENGTH returns the length of a dVector3.
  73. * dLENGTHSQUARED return the squared length of a dVector3.
  74. */
  75. #define dLENGTHSQUARED(a) (((a)[0])*((a)[0]) + ((a)[1])*((a)[1]) + ((a)[2])*((a)[2]))
  76. #ifdef __cplusplus
  77. PURE_INLINE dReal dLENGTH (const dReal *a) { return dSqrt(dLENGTHSQUARED(a)); }
  78. #else
  79. #define dLENGTH(a) ( dSqrt( ((a)[0])*((a)[0]) + ((a)[1])*((a)[1]) + ((a)[2])*((a)[2]) ) )
  80. #endif /* __cplusplus */
  81. /*
  82. * 3-way dot product. dDOTpq means that elements of `a' and `b' are spaced
  83. * p and q indexes apart respectively. dDOT() means dDOT11.
  84. * in C++ we could use function templates to get all the versions of these
  85. * functions - but on some compilers this will result in sub-optimal code.
  86. */
  87. #define dDOTpq(a,b,p,q) ((a)[0]*(b)[0] + (a)[p]*(b)[q] + (a)[2*(p)]*(b)[2*(q)])
  88. #ifdef __cplusplus
  89. PURE_INLINE dReal dDOT (const dReal *a, const dReal *b) { return dDOTpq(a,b,1,1); }
  90. PURE_INLINE dReal dDOT13 (const dReal *a, const dReal *b) { return dDOTpq(a,b,1,3); }
  91. PURE_INLINE dReal dDOT31 (const dReal *a, const dReal *b) { return dDOTpq(a,b,3,1); }
  92. PURE_INLINE dReal dDOT33 (const dReal *a, const dReal *b) { return dDOTpq(a,b,3,3); }
  93. PURE_INLINE dReal dDOT14 (const dReal *a, const dReal *b) { return dDOTpq(a,b,1,4); }
  94. PURE_INLINE dReal dDOT41 (const dReal *a, const dReal *b) { return dDOTpq(a,b,4,1); }
  95. PURE_INLINE dReal dDOT44 (const dReal *a, const dReal *b) { return dDOTpq(a,b,4,4); }
  96. #else
  97. #define dDOT(a,b) dDOTpq(a,b,1,1)
  98. #define dDOT13(a,b) dDOTpq(a,b,1,3)
  99. #define dDOT31(a,b) dDOTpq(a,b,3,1)
  100. #define dDOT33(a,b) dDOTpq(a,b,3,3)
  101. #define dDOT14(a,b) dDOTpq(a,b,1,4)
  102. #define dDOT41(a,b) dDOTpq(a,b,4,1)
  103. #define dDOT44(a,b) dDOTpq(a,b,4,4)
  104. #endif /* __cplusplus */
  105. /*
  106. * cross product, set a = b x c. dCROSSpqr means that elements of `a', `b'
  107. * and `c' are spaced p, q and r indexes apart respectively.
  108. * dCROSS() means dCROSS111. `op' is normally `=', but you can set it to
  109. * +=, -= etc to get other effects.
  110. */
  111. #define dCROSS(a,op,b,c) \
  112. do { \
  113. (a)[0] op ((b)[1]*(c)[2] - (b)[2]*(c)[1]); \
  114. (a)[1] op ((b)[2]*(c)[0] - (b)[0]*(c)[2]); \
  115. (a)[2] op ((b)[0]*(c)[1] - (b)[1]*(c)[0]); \
  116. } while(0)
  117. #define dCROSSpqr(a,op,b,c,p,q,r) \
  118. do { \
  119. (a)[ 0] op ((b)[ q]*(c)[2*r] - (b)[2*q]*(c)[ r]); \
  120. (a)[ p] op ((b)[2*q]*(c)[ 0] - (b)[ 0]*(c)[2*r]); \
  121. (a)[2*p] op ((b)[ 0]*(c)[ r] - (b)[ q]*(c)[ 0]); \
  122. } while(0)
  123. #define dCROSS114(a,op,b,c) dCROSSpqr(a,op,b,c,1,1,4)
  124. #define dCROSS141(a,op,b,c) dCROSSpqr(a,op,b,c,1,4,1)
  125. #define dCROSS144(a,op,b,c) dCROSSpqr(a,op,b,c,1,4,4)
  126. #define dCROSS411(a,op,b,c) dCROSSpqr(a,op,b,c,4,1,1)
  127. #define dCROSS414(a,op,b,c) dCROSSpqr(a,op,b,c,4,1,4)
  128. #define dCROSS441(a,op,b,c) dCROSSpqr(a,op,b,c,4,4,1)
  129. #define dCROSS444(a,op,b,c) dCROSSpqr(a,op,b,c,4,4,4)
  130. /*
  131. * set a 3x3 submatrix of A to a matrix such that submatrix(A)*b = a x b.
  132. * A is stored by rows, and has `skip' elements per row. the matrix is
  133. * assumed to be already zero, so this does not write zero elements!
  134. * if (plus,minus) is (+,-) then a positive version will be written.
  135. * if (plus,minus) is (-,+) then a negative version will be written.
  136. */
  137. #define dCROSSMAT(A,a,skip,plus,minus) \
  138. do { \
  139. (A)[1] = minus (a)[2]; \
  140. (A)[2] = plus (a)[1]; \
  141. (A)[(skip)+0] = plus (a)[2]; \
  142. (A)[(skip)+2] = minus (a)[0]; \
  143. (A)[2*(skip)+0] = minus (a)[1]; \
  144. (A)[2*(skip)+1] = plus (a)[0]; \
  145. } while(0)
  146. /*
  147. * compute the distance between two 3D-vectors
  148. */
  149. #ifdef __cplusplus
  150. PURE_INLINE dReal dDISTANCE (const dVector3 a, const dVector3 b)
  151. { return dSqrt( (a[0]-b[0])*(a[0]-b[0]) + (a[1]-b[1])*(a[1]-b[1]) + (a[2]-b[2])*(a[2]-b[2]) ); }
  152. #else
  153. #define dDISTANCE(a,b) \
  154. (dSqrt( ((a)[0]-(b)[0])*((a)[0]-(b)[0]) + ((a)[1]-(b)[1])*((a)[1]-(b)[1]) + ((a)[2]-(b)[2])*((a)[2]-(b)[2]) ))
  155. #endif
  156. /*
  157. * special case matrix multipication, with operator selection
  158. */
  159. #define dMULTIPLYOP0_331(A,op,B,C) \
  160. do { \
  161. (A)[0] op dDOT((B),(C)); \
  162. (A)[1] op dDOT((B+4),(C)); \
  163. (A)[2] op dDOT((B+8),(C)); \
  164. } while(0)
  165. #define dMULTIPLYOP1_331(A,op,B,C) \
  166. do { \
  167. (A)[0] op dDOT41((B),(C)); \
  168. (A)[1] op dDOT41((B+1),(C)); \
  169. (A)[2] op dDOT41((B+2),(C)); \
  170. } while(0)
  171. #define dMULTIPLYOP0_133(A,op,B,C) \
  172. do { \
  173. (A)[0] op dDOT14((B),(C)); \
  174. (A)[1] op dDOT14((B),(C+1)); \
  175. (A)[2] op dDOT14((B),(C+2)); \
  176. } while(0)
  177. #define dMULTIPLYOP0_333(A,op,B,C) \
  178. do { \
  179. (A)[0] op dDOT14((B),(C)); \
  180. (A)[1] op dDOT14((B),(C+1)); \
  181. (A)[2] op dDOT14((B),(C+2)); \
  182. (A)[4] op dDOT14((B+4),(C)); \
  183. (A)[5] op dDOT14((B+4),(C+1)); \
  184. (A)[6] op dDOT14((B+4),(C+2)); \
  185. (A)[8] op dDOT14((B+8),(C)); \
  186. (A)[9] op dDOT14((B+8),(C+1)); \
  187. (A)[10] op dDOT14((B+8),(C+2)); \
  188. } while(0)
  189. #define dMULTIPLYOP1_333(A,op,B,C) \
  190. do { \
  191. (A)[0] op dDOT44((B),(C)); \
  192. (A)[1] op dDOT44((B),(C+1)); \
  193. (A)[2] op dDOT44((B),(C+2)); \
  194. (A)[4] op dDOT44((B+1),(C)); \
  195. (A)[5] op dDOT44((B+1),(C+1)); \
  196. (A)[6] op dDOT44((B+1),(C+2)); \
  197. (A)[8] op dDOT44((B+2),(C)); \
  198. (A)[9] op dDOT44((B+2),(C+1)); \
  199. (A)[10] op dDOT44((B+2),(C+2)); \
  200. } while(0)
  201. #define dMULTIPLYOP2_333(A,op,B,C) \
  202. do { \
  203. (A)[0] op dDOT((B),(C)); \
  204. (A)[1] op dDOT((B),(C+4)); \
  205. (A)[2] op dDOT((B),(C+8)); \
  206. (A)[4] op dDOT((B+4),(C)); \
  207. (A)[5] op dDOT((B+4),(C+4)); \
  208. (A)[6] op dDOT((B+4),(C+8)); \
  209. (A)[8] op dDOT((B+8),(C)); \
  210. (A)[9] op dDOT((B+8),(C+4)); \
  211. (A)[10] op dDOT((B+8),(C+8)); \
  212. } while(0)
  213. #ifdef __cplusplus
  214. #define DECL template <class TA, class TB, class TC> PURE_INLINE void
  215. /*
  216. Note: NEVER call any of these functions/macros with the same variable for A and C,
  217. it is not equivalent to A*=B.
  218. */
  219. DECL dMULTIPLY0_331(TA *A, const TB *B, const TC *C) { dMULTIPLYOP0_331(A,=,B,C); }
  220. DECL dMULTIPLY1_331(TA *A, const TB *B, const TC *C) { dMULTIPLYOP1_331(A,=,B,C); }
  221. DECL dMULTIPLY0_133(TA *A, const TB *B, const TC *C) { dMULTIPLYOP0_133(A,=,B,C); }
  222. DECL dMULTIPLY0_333(TA *A, const TB *B, const TC *C) { dMULTIPLYOP0_333(A,=,B,C); }
  223. DECL dMULTIPLY1_333(TA *A, const TB *B, const TC *C) { dMULTIPLYOP1_333(A,=,B,C); }
  224. DECL dMULTIPLY2_333(TA *A, const TB *B, const TC *C) { dMULTIPLYOP2_333(A,=,B,C); }
  225. DECL dMULTIPLYADD0_331(TA *A, const TB *B, const TC *C) { dMULTIPLYOP0_331(A,+=,B,C); }
  226. DECL dMULTIPLYADD1_331(TA *A, const TB *B, const TC *C) { dMULTIPLYOP1_331(A,+=,B,C); }
  227. DECL dMULTIPLYADD0_133(TA *A, const TB *B, const TC *C) { dMULTIPLYOP0_133(A,+=,B,C); }
  228. DECL dMULTIPLYADD0_333(TA *A, const TB *B, const TC *C) { dMULTIPLYOP0_333(A,+=,B,C); }
  229. DECL dMULTIPLYADD1_333(TA *A, const TB *B, const TC *C) { dMULTIPLYOP1_333(A,+=,B,C); }
  230. DECL dMULTIPLYADD2_333(TA *A, const TB *B, const TC *C) { dMULTIPLYOP2_333(A,+=,B,C); }
  231. #undef DECL
  232. #else
  233. #define dMULTIPLY0_331(A,B,C) dMULTIPLYOP0_331(A,=,B,C)
  234. #define dMULTIPLY1_331(A,B,C) dMULTIPLYOP1_331(A,=,B,C)
  235. #define dMULTIPLY0_133(A,B,C) dMULTIPLYOP0_133(A,=,B,C)
  236. #define dMULTIPLY0_333(A,B,C) dMULTIPLYOP0_333(A,=,B,C)
  237. #define dMULTIPLY1_333(A,B,C) dMULTIPLYOP1_333(A,=,B,C)
  238. #define dMULTIPLY2_333(A,B,C) dMULTIPLYOP2_333(A,=,B,C)
  239. #define dMULTIPLYADD0_331(A,B,C) dMULTIPLYOP0_331(A,+=,B,C)
  240. #define dMULTIPLYADD1_331(A,B,C) dMULTIPLYOP1_331(A,+=,B,C)
  241. #define dMULTIPLYADD0_133(A,B,C) dMULTIPLYOP0_133(A,+=,B,C)
  242. #define dMULTIPLYADD0_333(A,B,C) dMULTIPLYOP0_333(A,+=,B,C)
  243. #define dMULTIPLYADD1_333(A,B,C) dMULTIPLYOP1_333(A,+=,B,C)
  244. #define dMULTIPLYADD2_333(A,B,C) dMULTIPLYOP2_333(A,+=,B,C)
  245. #endif
  246. #ifdef __cplusplus
  247. extern "C" {
  248. #endif
  249. /*
  250. * normalize 3x1 and 4x1 vectors (i.e. scale them to unit length)
  251. */
  252. #if defined(__ODE__)
  253. int _dSafeNormalize3 (dVector3 a);
  254. int _dSafeNormalize4 (dVector4 a);
  255. static __inline void _dNormalize3(dVector3 a)
  256. {
  257. int bNormalizationResult = _dSafeNormalize3(a);
  258. dIASSERT(bNormalizationResult);
  259. dVARIABLEUSED(bNormalizationResult);
  260. }
  261. static __inline void _dNormalize4(dVector4 a)
  262. {
  263. int bNormalizationResult = _dSafeNormalize4(a);
  264. dIASSERT(bNormalizationResult);
  265. dVARIABLEUSED(bNormalizationResult);
  266. }
  267. #endif // defined(__ODE__)
  268. // For DLL export
  269. ODE_API int dSafeNormalize3 (dVector3 a);
  270. ODE_API int dSafeNormalize4 (dVector4 a);
  271. ODE_API void dNormalize3 (dVector3 a); // Potentially asserts on zero vec
  272. ODE_API void dNormalize4 (dVector4 a); // Potentially asserts on zero vec
  273. #if defined(__ODE__)
  274. // For internal use
  275. #define dSafeNormalize3(a) _dSafeNormalize3(a)
  276. #define dSafeNormalize4(a) _dSafeNormalize4(a)
  277. #define dNormalize3(a) _dNormalize3(a)
  278. #define dNormalize4(a) _dNormalize4(a)
  279. #endif // defined(__ODE__)
  280. /*
  281. * given a unit length "normal" vector n, generate vectors p and q vectors
  282. * that are an orthonormal basis for the plane space perpendicular to n.
  283. * i.e. this makes p,q such that n,p,q are all perpendicular to each other.
  284. * q will equal n x p. if n is not unit length then p will be unit length but
  285. * q wont be.
  286. */
  287. ODE_API void dPlaneSpace (const dVector3 n, dVector3 p, dVector3 q);
  288. /* Makes sure the matrix is a proper rotation */
  289. ODE_API void dOrthogonalizeR(dMatrix3 m);
  290. #ifdef __cplusplus
  291. }
  292. #endif
  293. #endif