collision_util.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. /*
  23. some useful collision utility stuff.
  24. */
  25. #ifndef _ODE_COLLISION_UTIL_H_
  26. #define _ODE_COLLISION_UTIL_H_
  27. #include <ode/common.h>
  28. #include <ode/contact.h>
  29. #include <ode/rotation.h>
  30. #include "odemath.h"
  31. // given a pointer `p' to a dContactGeom, return the dContactGeom at
  32. // p + skip bytes.
  33. #define CONTACT(p,skip) ((dContactGeom*) (((char*)p) + (skip)))
  34. #if 1
  35. #include "collision_kernel.h"
  36. // Fetches a contact
  37. static inline dContactGeom* SAFECONTACT(int Flags, dContactGeom* Contacts, int Index, int Stride){
  38. dIASSERT(Index >= 0 && Index < (Flags & NUMC_MASK));
  39. return ((dContactGeom*)(((char*)Contacts) + (Index * Stride)));
  40. }
  41. #endif
  42. // if the spheres (p1,r1) and (p2,r2) collide, set the contact `c' and
  43. // return 1, else return 0.
  44. int dCollideSpheres (dVector3 p1, dReal r1,
  45. dVector3 p2, dReal r2, dContactGeom *c);
  46. // given two lines
  47. // qa = pa + alpha* ua
  48. // qb = pb + beta * ub
  49. // where pa,pb are two points, ua,ub are two unit length vectors, and alpha,
  50. // beta go from [-inf,inf], return alpha and beta such that qa and qb are
  51. // as close as possible
  52. void dLineClosestApproach (const dVector3 pa, const dVector3 ua,
  53. const dVector3 pb, const dVector3 ub,
  54. dReal *alpha, dReal *beta);
  55. // given a line segment p1-p2 and a box (center 'c', rotation 'R', side length
  56. // vector 'side'), compute the points of closest approach between the box
  57. // and the line. return these points in 'lret' (the point on the line) and
  58. // 'bret' (the point on the box). if the line actually penetrates the box
  59. // then the solution is not unique, but only one solution will be returned.
  60. // in this case the solution points will coincide.
  61. void dClosestLineBoxPoints (const dVector3 p1, const dVector3 p2,
  62. const dVector3 c, const dMatrix3 R,
  63. const dVector3 side,
  64. dVector3 lret, dVector3 bret);
  65. // 20 Apr 2004
  66. // Start code by Nguyen Binh
  67. int dClipEdgeToPlane(dVector3 &vEpnt0, dVector3 &vEpnt1, const dVector4& plPlane);
  68. // clip polygon with plane and generate new polygon points
  69. void dClipPolyToPlane(const dVector3 avArrayIn[], const int ctIn, dVector3 avArrayOut[], int &ctOut, const dVector4 &plPlane );
  70. void dClipPolyToCircle(const dVector3 avArrayIn[], const int ctIn, dVector3 avArrayOut[], int &ctOut, const dVector4 &plPlane ,dReal fRadius);
  71. // Some vector math
  72. static inline void dVector3Subtract(const dVector3& a,const dVector3& b,dVector3& c)
  73. {
  74. dSubtractVectors3(c, a, b);
  75. }
  76. static inline void dVector3Scale(dVector3& a,dReal nScale)
  77. {
  78. dScaleVector3(a, nScale);
  79. }
  80. static inline void dVector3Add(const dVector3& a,const dVector3& b,dVector3& c)
  81. {
  82. dAddVectors3(c, a, b);
  83. }
  84. static inline void dVector3Copy(const dVector3& a,dVector3& c)
  85. {
  86. dCopyVector3(c, a);
  87. }
  88. static inline void dVector4Copy(const dVector4& a,dVector4& c)
  89. {
  90. dCopyVector4(c, a);
  91. }
  92. static inline void dVector3Cross(const dVector3& a,const dVector3& b,dVector3& c)
  93. {
  94. dCalcVectorCross3(c, a, b);
  95. }
  96. static inline dReal dVector3Length(const dVector3& a)
  97. {
  98. return dCalcVectorLength3(a);
  99. }
  100. static inline dReal dVector3LengthSquare(const dVector3& a)
  101. {
  102. return dCalcVectorLengthSquare3(a);
  103. }
  104. static inline dReal dVector3Dot(const dVector3& a,const dVector3& b)
  105. {
  106. return dCalcVectorDot3(a, b);
  107. }
  108. static inline void dVector3Inv(dVector3& a)
  109. {
  110. dNegateVector3(a);
  111. }
  112. static inline void dMat3GetCol(const dMatrix3& m,const int col, dVector3& v)
  113. {
  114. dGetMatrixColumn3(v, m, col);
  115. }
  116. static inline void dVector3CrossMat3Col(const dMatrix3& m,const int col,const dVector3& v,dVector3& r)
  117. {
  118. dCalcVectorCross3_114(r, v, m + col);
  119. }
  120. static inline void dMat3ColCrossVector3(const dMatrix3& m,const int col,const dVector3& v,dVector3& r)
  121. {
  122. dCalcVectorCross3_141(r, m + col, v);
  123. }
  124. static inline void dMultiplyMat3Vec3(const dMatrix3& m,const dVector3& v, dVector3& r)
  125. {
  126. dMultiply0_331(r, m, v);
  127. }
  128. static inline dReal dPointPlaneDistance(const dVector3& point,const dVector4& plane)
  129. {
  130. return (plane[0]*point[0] + plane[1]*point[1] + plane[2]*point[2] + plane[3]);
  131. }
  132. static inline void dConstructPlane(const dVector3& normal,const dReal& distance, dVector4& plane)
  133. {
  134. plane[0] = normal[0];
  135. plane[1] = normal[1];
  136. plane[2] = normal[2];
  137. plane[3] = distance;
  138. }
  139. static inline void dMatrix3Copy(const dReal* source,dMatrix3& dest)
  140. {
  141. dCopyMatrix4x3(dest, source);
  142. }
  143. static inline dReal dMatrix3Det( const dMatrix3& mat )
  144. {
  145. dReal det;
  146. det = mat[0] * ( mat[5]*mat[10] - mat[9]*mat[6] )
  147. - mat[1] * ( mat[4]*mat[10] - mat[8]*mat[6] )
  148. + mat[2] * ( mat[4]*mat[9] - mat[8]*mat[5] );
  149. return( det );
  150. }
  151. inline void dMatrix3Inv( const dMatrix3& ma, dMatrix3& dst )
  152. {
  153. dReal det = dMatrix3Det( ma );
  154. if ( dFabs( det ) < REAL(0.0005) )
  155. {
  156. dRSetIdentity( dst );
  157. return;
  158. }
  159. double detRecip = REAL(1.0) / det;
  160. dst[0] = ( ma[5]*ma[10] - ma[6]*ma[9] ) * detRecip;
  161. dst[1] = ( ma[9]*ma[2] - ma[1]*ma[10] ) * detRecip;
  162. dst[2] = ( ma[1]*ma[6] - ma[5]*ma[2] ) * detRecip;
  163. dst[4] = ( ma[6]*ma[8] - ma[4]*ma[10] ) * detRecip;
  164. dst[5] = ( ma[0]*ma[10] - ma[8]*ma[2] ) * detRecip;
  165. dst[6] = ( ma[4]*ma[2] - ma[0]*ma[6] ) * detRecip;
  166. dst[8] = ( ma[4]*ma[9] - ma[8]*ma[5] ) * detRecip;
  167. dst[9] = ( ma[8]*ma[1] - ma[0]*ma[9] ) * detRecip;
  168. dst[10] = ( ma[0]*ma[5] - ma[1]*ma[4] ) * detRecip;
  169. }
  170. inline void dQuatTransform(const dQuaternion& quat,const dVector3& source,dVector3& dest)
  171. {
  172. // Nguyen Binh : this code seem to be the fastest.
  173. dReal x0 = source[0] * quat[0] + source[2] * quat[2] - source[1] * quat[3];
  174. dReal x1 = source[1] * quat[0] + source[0] * quat[3] - source[2] * quat[1];
  175. dReal x2 = source[2] * quat[0] + source[1] * quat[1] - source[0] * quat[2];
  176. dReal x3 = source[0] * quat[1] + source[1] * quat[2] + source[2] * quat[3];
  177. dest[0] = quat[0] * x0 + quat[1] * x3 + quat[2] * x2 - quat[3] * x1;
  178. dest[1] = quat[0] * x1 + quat[2] * x3 + quat[3] * x0 - quat[1] * x2;
  179. dest[2] = quat[0] * x2 + quat[3] * x3 + quat[1] * x1 - quat[2] * x0;
  180. /*
  181. // nVidia SDK implementation
  182. dVector3 uv, uuv;
  183. dVector3 qvec;
  184. qvec[0] = quat[1];
  185. qvec[1] = quat[2];
  186. qvec[2] = quat[3];
  187. dVector3Cross(qvec,source,uv);
  188. dVector3Cross(qvec,uv,uuv);
  189. dVector3Scale(uv,REAL(2.0)*quat[0]);
  190. dVector3Scale(uuv,REAL(2.0));
  191. dest[0] = source[0] + uv[0] + uuv[0];
  192. dest[1] = source[1] + uv[1] + uuv[1];
  193. dest[2] = source[2] + uv[2] + uuv[2];
  194. */
  195. }
  196. inline void dQuatInvTransform(const dQuaternion& quat,const dVector3& source,dVector3& dest)
  197. {
  198. dReal norm = quat[0]*quat[0] + quat[1]*quat[1] + quat[2]*quat[2] + quat[3]*quat[3];
  199. if (norm > REAL(0.0))
  200. {
  201. dQuaternion invQuat;
  202. invQuat[0] = quat[0] / norm;
  203. invQuat[1] = -quat[1] / norm;
  204. invQuat[2] = -quat[2] / norm;
  205. invQuat[3] = -quat[3] / norm;
  206. dQuatTransform(invQuat,source,dest);
  207. }
  208. else
  209. {
  210. // Singular -> return identity
  211. dVector3Copy(source,dest);
  212. }
  213. }
  214. inline void dGetEulerAngleFromRot(const dMatrix3& mRot,dReal& rX,dReal& rY,dReal& rZ)
  215. {
  216. rY = asin(mRot[0 * 4 + 2]);
  217. if (rY < M_PI /2)
  218. {
  219. if (rY > -M_PI /2)
  220. {
  221. rX = atan2(-mRot[1*4 + 2], mRot[2*4 + 2]);
  222. rZ = atan2(-mRot[0*4 + 1], mRot[0*4 + 0]);
  223. }
  224. else
  225. {
  226. // not unique
  227. rX = -atan2(mRot[1*4 + 0], mRot[1*4 + 1]);
  228. rZ = REAL(0.0);
  229. }
  230. }
  231. else
  232. {
  233. // not unique
  234. rX = atan2(mRot[1*4 + 0], mRot[1*4 + 1]);
  235. rZ = REAL(0.0);
  236. }
  237. }
  238. inline void dQuatInv(const dQuaternion& source, dQuaternion& dest)
  239. {
  240. dReal norm = source[0]*source[0] + source[1]*source[1] + source[2]*source[2] + source[3]*source[3];
  241. if (norm > 0.0f)
  242. {
  243. dest[0] = source[0] / norm;
  244. dest[1] = -source[1] / norm;
  245. dest[2] = -source[2] / norm;
  246. dest[3] = -source[3] / norm;
  247. }
  248. else
  249. {
  250. // Singular -> return identity
  251. dest[0] = REAL(1.0);
  252. dest[1] = REAL(0.0);
  253. dest[2] = REAL(0.0);
  254. dest[3] = REAL(0.0);
  255. }
  256. }
  257. #endif