btGeometryOperations.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #ifndef BT_BASIC_GEOMETRY_OPERATIONS_H_INCLUDED
  2. #define BT_BASIC_GEOMETRY_OPERATIONS_H_INCLUDED
  3. /*! \file btGeometryOperations.h
  4. *\author Francisco Leon Najera
  5. */
  6. /*
  7. This source file is part of GIMPACT Library.
  8. For the latest info, see http://gimpact.sourceforge.net/
  9. Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
  10. email: projectileman@yahoo.com
  11. This software is provided 'as-is', without any express or implied warranty.
  12. In no event will the authors be held liable for any damages arising from the use of this software.
  13. Permission is granted to anyone to use this software for any purpose,
  14. including commercial applications, and to alter it and redistribute it freely,
  15. subject to the following restrictions:
  16. 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.
  17. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  18. 3. This notice may not be removed or altered from any source distribution.
  19. */
  20. #include "btBoxCollision.h"
  21. #define PLANEDIREPSILON 0.0000001f
  22. #define PARALELENORMALS 0.000001f
  23. #define BT_CLAMP(number,minval,maxval) (number<minval?minval:(number>maxval?maxval:number))
  24. /// Calc a plane from a triangle edge an a normal. plane is a vec4f
  25. SIMD_FORCE_INLINE void bt_edge_plane(const btVector3 & e1,const btVector3 & e2, const btVector3 & normal,btVector4 & plane)
  26. {
  27. btVector3 planenormal = (e2-e1).cross(normal);
  28. planenormal.normalize();
  29. plane.setValue(planenormal[0],planenormal[1],planenormal[2],e2.dot(planenormal));
  30. }
  31. //***************** SEGMENT and LINE FUNCTIONS **********************************///
  32. /*! Finds the closest point(cp) to (v) on a segment (e1,e2)
  33. */
  34. SIMD_FORCE_INLINE void bt_closest_point_on_segment(
  35. btVector3 & cp, const btVector3 & v,
  36. const btVector3 &e1,const btVector3 &e2)
  37. {
  38. btVector3 n = e2-e1;
  39. cp = v - e1;
  40. btScalar _scalar = cp.dot(n)/n.dot(n);
  41. if(_scalar <0.0f)
  42. {
  43. cp = e1;
  44. }
  45. else if(_scalar >1.0f)
  46. {
  47. cp = e2;
  48. }
  49. else
  50. {
  51. cp = _scalar*n + e1;
  52. }
  53. }
  54. //! line plane collision
  55. /*!
  56. *\return
  57. -0 if the ray never intersects
  58. -1 if the ray collides in front
  59. -2 if the ray collides in back
  60. */
  61. SIMD_FORCE_INLINE int bt_line_plane_collision(
  62. const btVector4 & plane,
  63. const btVector3 & vDir,
  64. const btVector3 & vPoint,
  65. btVector3 & pout,
  66. btScalar &tparam,
  67. btScalar tmin, btScalar tmax)
  68. {
  69. btScalar _dotdir = vDir.dot(plane);
  70. if(btFabs(_dotdir)<PLANEDIREPSILON)
  71. {
  72. tparam = tmax;
  73. return 0;
  74. }
  75. btScalar _dis = bt_distance_point_plane(plane,vPoint);
  76. char returnvalue = _dis<0.0f? 2:1;
  77. tparam = -_dis/_dotdir;
  78. if(tparam<tmin)
  79. {
  80. returnvalue = 0;
  81. tparam = tmin;
  82. }
  83. else if(tparam>tmax)
  84. {
  85. returnvalue = 0;
  86. tparam = tmax;
  87. }
  88. pout = tparam*vDir + vPoint;
  89. return returnvalue;
  90. }
  91. //! Find closest points on segments
  92. SIMD_FORCE_INLINE void bt_segment_collision(
  93. const btVector3 & vA1,
  94. const btVector3 & vA2,
  95. const btVector3 & vB1,
  96. const btVector3 & vB2,
  97. btVector3 & vPointA,
  98. btVector3 & vPointB)
  99. {
  100. btVector3 AD = vA2 - vA1;
  101. btVector3 BD = vB2 - vB1;
  102. btVector3 N = AD.cross(BD);
  103. btScalar tp = N.length2();
  104. btVector4 _M;//plane
  105. if(tp<SIMD_EPSILON)//ARE PARALELE
  106. {
  107. //project B over A
  108. bool invert_b_order = false;
  109. _M[0] = vB1.dot(AD);
  110. _M[1] = vB2.dot(AD);
  111. if(_M[0]>_M[1])
  112. {
  113. invert_b_order = true;
  114. BT_SWAP_NUMBERS(_M[0],_M[1]);
  115. }
  116. _M[2] = vA1.dot(AD);
  117. _M[3] = vA2.dot(AD);
  118. //mid points
  119. N[0] = (_M[0]+_M[1])*0.5f;
  120. N[1] = (_M[2]+_M[3])*0.5f;
  121. if(N[0]<N[1])
  122. {
  123. if(_M[1]<_M[2])
  124. {
  125. vPointB = invert_b_order?vB1:vB2;
  126. vPointA = vA1;
  127. }
  128. else if(_M[1]<_M[3])
  129. {
  130. vPointB = invert_b_order?vB1:vB2;
  131. bt_closest_point_on_segment(vPointA,vPointB,vA1,vA2);
  132. }
  133. else
  134. {
  135. vPointA = vA2;
  136. bt_closest_point_on_segment(vPointB,vPointA,vB1,vB2);
  137. }
  138. }
  139. else
  140. {
  141. if(_M[3]<_M[0])
  142. {
  143. vPointB = invert_b_order?vB2:vB1;
  144. vPointA = vA2;
  145. }
  146. else if(_M[3]<_M[1])
  147. {
  148. vPointA = vA2;
  149. bt_closest_point_on_segment(vPointB,vPointA,vB1,vB2);
  150. }
  151. else
  152. {
  153. vPointB = invert_b_order?vB1:vB2;
  154. bt_closest_point_on_segment(vPointA,vPointB,vA1,vA2);
  155. }
  156. }
  157. return;
  158. }
  159. N = N.cross(BD);
  160. _M.setValue(N[0],N[1],N[2],vB1.dot(N));
  161. // get point A as the plane collision point
  162. bt_line_plane_collision(_M,AD,vA1,vPointA,tp,btScalar(0), btScalar(1));
  163. /*Closest point on segment*/
  164. vPointB = vPointA - vB1;
  165. tp = vPointB.dot(BD);
  166. tp/= BD.dot(BD);
  167. tp = BT_CLAMP(tp,0.0f,1.0f);
  168. vPointB = tp*BD + vB1;
  169. }
  170. #endif // GIM_VECTOR_H_INCLUDED