FunctionsTestPlane.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include <AnKi/Collision/Functions.h>
  2. #include <AnKi/Collision/ConvexHullShape.h>
  3. #include <AnKi/Collision/Obb.h>
  4. #include <AnKi/Collision/LineSegment.h>
  5. #include <AnKi/Collision/Cone.h>
  6. #include <AnKi/Collision/Sphere.h>
  7. namespace anki
  8. {
  9. F32 testPlane(const Plane& plane, const Aabb& aabb)
  10. {
  11. #if ANKI_SIMD_SSE
  12. __m128 gezero = _mm_cmpge_ps(plane.getNormal().getSimd(), _mm_setzero_ps());
  13. Vec4 diagMin;
  14. diagMin.getSimd() =
  15. _mm_or_ps(_mm_and_ps(gezero, aabb.getMin().getSimd()), _mm_andnot_ps(gezero, aabb.getMax().getSimd()));
  16. #else
  17. Vec4 diagMin(0.0f), diagMax(0.0f);
  18. // set min/max values for x,y,z direction
  19. for(U i = 0; i < 3; i++)
  20. {
  21. if(plane.getNormal()[i] >= 0.0f)
  22. {
  23. diagMin[i] = aabb.getMin()[i];
  24. diagMax[i] = aabb.getMax()[i];
  25. }
  26. else
  27. {
  28. diagMin[i] = aabb.getMax()[i];
  29. diagMax[i] = aabb.getMin()[i];
  30. }
  31. }
  32. #endif
  33. // minimum on positive side of plane, box on positive side
  34. ANKI_ASSERT(diagMin.w() == 0.0f);
  35. F32 test = testPlane(plane, diagMin);
  36. if(test > 0.0f)
  37. {
  38. return test;
  39. }
  40. #if ANKI_SIMD_SSE
  41. Vec4 diagMax;
  42. diagMax.getSimd() =
  43. _mm_or_ps(_mm_and_ps(gezero, aabb.getMax().getSimd()), _mm_andnot_ps(gezero, aabb.getMin().getSimd()));
  44. #endif
  45. ANKI_ASSERT(diagMax.w() == 0.0f);
  46. test = testPlane(plane, diagMax);
  47. if(test >= 0.0f)
  48. {
  49. // min on non-positive side, max on non-negative side, intersection
  50. return 0.0f;
  51. }
  52. else
  53. {
  54. // max on negative side, box on negative side
  55. return test;
  56. }
  57. }
  58. F32 testPlane(const Plane& plane, const Sphere& sphere)
  59. {
  60. const F32 dist = testPlane(plane, sphere.getCenter());
  61. F32 out = dist - sphere.getRadius();
  62. if(out > 0.0f)
  63. {
  64. // Do nothing
  65. }
  66. else
  67. {
  68. out = dist + sphere.getRadius();
  69. if(out < 0.0f)
  70. {
  71. // Do nothing
  72. }
  73. else
  74. {
  75. out = 0.0f;
  76. }
  77. }
  78. return out;
  79. }
  80. F32 testPlane(const Plane& plane, const Obb& obb)
  81. {
  82. Mat3x4 transposedRotation = obb.getRotation();
  83. transposedRotation.transposeRotationPart();
  84. const Vec4 xNormal = (transposedRotation * plane.getNormal()).xyz0();
  85. // maximum extent in direction of plane normal
  86. const Vec4 rv = obb.getExtend() * xNormal;
  87. const Vec4 rvabs = rv.abs();
  88. const F32 r = rvabs.x() + rvabs.y() + rvabs.z();
  89. // signed distance between box center and plane
  90. const F32 d = testPlane(plane, obb.getCenter());
  91. // return signed distance
  92. if(absolute(d) < r)
  93. {
  94. return 0.0f;
  95. }
  96. else if(d < 0.0f)
  97. {
  98. return d + r;
  99. }
  100. else
  101. {
  102. return d - r;
  103. }
  104. }
  105. F32 testPlane(const Plane& plane, const ConvexHullShape& hull)
  106. {
  107. // Compute the invert transformation of the plane instead
  108. const Plane pa = (hull.isTransformIdentity()) ? plane : plane.getTransformed(hull.getInvertTransform());
  109. F32 minDist = MAX_F32;
  110. F32 maxDist = MIN_F32;
  111. ConstWeakArray<Vec4> points = hull.getPoints();
  112. for(const Vec4& point : points)
  113. {
  114. const F32 test = testPlane(pa, point);
  115. if(ANKI_UNLIKELY(test == 0.0f))
  116. {
  117. // Early exit
  118. return 0.0f;
  119. }
  120. minDist = min(minDist, test);
  121. maxDist = max(maxDist, test);
  122. }
  123. if(minDist > 0.0f && maxDist > 0.0f)
  124. {
  125. return minDist;
  126. }
  127. else if(minDist < 0.0f && maxDist < 0.0f)
  128. {
  129. return maxDist;
  130. }
  131. else
  132. {
  133. return 0.0f;
  134. }
  135. }
  136. F32 testPlane(const Plane& plane, const LineSegment& ls)
  137. {
  138. const Vec4& p0 = ls.getOrigin();
  139. Vec4 p1 = ls.getOrigin() + ls.getDirection();
  140. const F32 dist0 = testPlane(plane, p0);
  141. const F32 dist1 = testPlane(plane, p1);
  142. if(dist0 > 0.0f)
  143. {
  144. if(dist1 > 0.0f)
  145. {
  146. return min(dist0, dist1);
  147. }
  148. else
  149. {
  150. return 0.0f;
  151. }
  152. }
  153. else
  154. {
  155. if(dist1 < 0.0f)
  156. {
  157. return max(dist0, dist1);
  158. }
  159. else
  160. {
  161. return 0.0f;
  162. }
  163. }
  164. }
  165. } // end namespace anki