IntrOrientedBox2OrientedBox2.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <Mathematics/FIQuery.h>
  9. #include <Mathematics/TIQuery.h>
  10. #include <Mathematics/OrientedBox.h>
  11. #include <Mathematics/Vector2.h>
  12. #include <vector>
  13. // The queries consider the box to be a solid.
  14. //
  15. // The test-intersection query uses the method of separating axes.
  16. // https://www.geometrictools.com/Documentation/MethodOfSeparatingAxes.pdf
  17. // The set of potential separating directions includes the 2 edge normals of
  18. // box0 and the 2 edge normals of box1. The integer 'separating' identifies
  19. // the axis that reported separation; there may be more than one but only one
  20. // is reported. The value is 0 when box0.axis[0] separates, 1 when
  21. // box0.axis[1] separates, 2 when box1.axis[0] separates or 3 when
  22. // box1.axis[1] separates.
  23. namespace WwiseGTE
  24. {
  25. template <typename Real>
  26. class TIQuery<Real, OrientedBox2<Real>, OrientedBox2<Real>>
  27. {
  28. public:
  29. struct Result
  30. {
  31. bool intersect;
  32. int separating;
  33. };
  34. Result operator()(OrientedBox2<Real> const& box0, OrientedBox2<Real> const& box1)
  35. {
  36. Result result;
  37. // Convenience variables.
  38. Vector2<Real> const* A0 = &box0.axis[0];
  39. Vector2<Real> const* A1 = &box1.axis[0];
  40. Vector2<Real> const& E0 = box0.extent;
  41. Vector2<Real> const& E1 = box1.extent;
  42. // Compute difference of box centers, D = C1-C0.
  43. Vector2<Real> D = box1.center - box0.center;
  44. Real absA0dA1[2][2], rSum;
  45. // Test box0.axis[0].
  46. absA0dA1[0][0] = std::fabs(Dot(A0[0], A1[0]));
  47. absA0dA1[0][1] = std::fabs(Dot(A0[0], A1[1]));
  48. rSum = E0[0] + E1[0] * absA0dA1[0][0] + E1[1] * absA0dA1[0][1];
  49. if (std::fabs(Dot(A0[0], D)) > rSum)
  50. {
  51. result.intersect = false;
  52. result.separating = 0;
  53. return result;
  54. }
  55. // Test axis box0.axis[1].
  56. absA0dA1[1][0] = std::fabs(Dot(A0[1], A1[0]));
  57. absA0dA1[1][1] = std::fabs(Dot(A0[1], A1[1]));
  58. rSum = E0[1] + E1[0] * absA0dA1[1][0] + E1[1] * absA0dA1[1][1];
  59. if (std::fabs(Dot(A0[1], D)) > rSum)
  60. {
  61. result.intersect = false;
  62. result.separating = 1;
  63. return result;
  64. }
  65. // Test axis box1.axis[0].
  66. rSum = E1[0] + E0[0] * absA0dA1[0][0] + E0[1] * absA0dA1[1][0];
  67. if (std::fabs(Dot(A1[0], D)) > rSum)
  68. {
  69. result.intersect = false;
  70. result.separating = 2;
  71. return result;
  72. }
  73. // Test axis box1.axis[1].
  74. rSum = E1[1] + E0[0] * absA0dA1[0][1] + E0[1] * absA0dA1[1][1];
  75. if (std::fabs(Dot(A1[1], D)) > rSum)
  76. {
  77. result.intersect = false;
  78. result.separating = 3;
  79. return result;
  80. }
  81. result.intersect = true;
  82. return result;
  83. }
  84. };
  85. template <typename Real>
  86. class FIQuery<Real, OrientedBox2<Real>, OrientedBox2<Real>>
  87. {
  88. public:
  89. struct Result
  90. {
  91. bool intersect;
  92. // If 'intersect' is true, the boxes intersect in a convex
  93. // 'polygon'.
  94. std::vector<Vector2<Real>> polygon;
  95. };
  96. Result operator()(OrientedBox2<Real> const& box0, OrientedBox2<Real> const& box1)
  97. {
  98. Result result;
  99. result.intersect = true;
  100. // Initialize the intersection polygon to box0, listing the
  101. // vertices in counterclockwise order.
  102. std::array<Vector2<Real>, 4> vertex;
  103. box0.GetVertices(vertex);
  104. result.polygon.push_back(vertex[0]); // C - e0 * U0 - e1 * U1
  105. result.polygon.push_back(vertex[1]); // C + e0 * U0 - e1 * U1
  106. result.polygon.push_back(vertex[3]); // C + e0 * U0 + e1 * U1
  107. result.polygon.push_back(vertex[2]); // C - e0 * U0 + e1 * U1
  108. // Clip the polygon using the lines defining edges of box1. The
  109. // line normal points inside box1. The line origin is the first
  110. // vertex of the edge when traversing box1 counterclockwise.
  111. box1.GetVertices(vertex);
  112. std::array<Vector2<Real>, 4> normal =
  113. {
  114. box1.axis[1], -box1.axis[0], box1.axis[0], -box1.axis[1]
  115. };
  116. for (int i = 0; i < 4; ++i)
  117. {
  118. if (Outside(vertex[i], normal[i], result.polygon))
  119. {
  120. // The boxes are separated.
  121. result.intersect = false;
  122. result.polygon.clear();
  123. break;
  124. }
  125. }
  126. return result;
  127. }
  128. private:
  129. // The line normals are inner pointing. The function returns true
  130. // when the incoming polygon is outside the line, in which case the
  131. // boxes do not intersect. If the function returns false, the
  132. // outgoing polygon is the incoming polygon intersected with the
  133. // closed halfspacedefined by the line.
  134. bool Outside(Vector2<Real> const& origin, Vector2<Real> const& normal,
  135. std::vector<Vector2<Real>>& polygon)
  136. {
  137. // Determine whether the polygon vertices are outside the polygon,
  138. // inside the polygon, or on the polygon boundary.
  139. int const numVertices = static_cast<int>(polygon.size());
  140. std::vector<Real> distance(numVertices);
  141. int positive = 0, negative = 0, positiveIndex = -1;
  142. for (int i = 0; i < numVertices; ++i)
  143. {
  144. distance[i] = Dot(normal, polygon[i] - origin);
  145. if (distance[i] > (Real)0)
  146. {
  147. ++positive;
  148. if (positiveIndex == -1)
  149. {
  150. positiveIndex = i;
  151. }
  152. }
  153. else if (distance[i] < (Real)0)
  154. {
  155. ++negative;
  156. }
  157. }
  158. if (positive == 0)
  159. {
  160. // The polygon is strictly outside the line.
  161. return true;
  162. }
  163. if (negative == 0)
  164. {
  165. // The polygon is contained in the closed halfspace whose
  166. // boundary is the line. It is fully visible and no clipping
  167. // is necessary.
  168. return false;
  169. }
  170. // The line transversely intersects the polygon. Clip the polygon.
  171. std::vector<Vector2<Real>> clipPolygon;
  172. Vector2<Real> vertex;
  173. int curr, prev;
  174. Real t;
  175. if (positiveIndex > 0)
  176. {
  177. // Compute the first clip vertex on the line.
  178. curr = positiveIndex;
  179. prev = curr - 1;
  180. t = distance[curr] / (distance[curr] - distance[prev]);
  181. vertex = polygon[curr] + t * (polygon[prev] - polygon[curr]);
  182. clipPolygon.push_back(vertex);
  183. // Include the vertices on the positive side of line.
  184. while (curr < numVertices && distance[curr] >(Real)0)
  185. {
  186. clipPolygon.push_back(polygon[curr++]);
  187. }
  188. // Compute the kast clip vertex on the line.
  189. if (curr < numVertices)
  190. {
  191. prev = curr - 1;
  192. }
  193. else
  194. {
  195. curr = 0;
  196. prev = numVertices - 1;
  197. }
  198. t = distance[curr] / (distance[curr] - distance[prev]);
  199. vertex = polygon[curr] + t * (polygon[prev] - polygon[curr]);
  200. clipPolygon.push_back(vertex);
  201. }
  202. else // positiveIndex is 0
  203. {
  204. // Include the vertices on the positive side of line.
  205. curr = 0;
  206. while (curr < numVertices && distance[curr] >(Real)0)
  207. {
  208. clipPolygon.push_back(polygon[curr++]);
  209. }
  210. // Compute the last clip vertex on the line.
  211. prev = curr - 1;
  212. t = distance[curr] / (distance[curr] - distance[prev]);
  213. vertex = polygon[curr] + t * (polygon[prev] - polygon[curr]);
  214. clipPolygon.push_back(vertex);
  215. // Skip the vertices on the negative side of the line.
  216. while (curr < numVertices && distance[curr] <= (Real)0)
  217. {
  218. curr++;
  219. }
  220. // Compute the first clip vertex on the line.
  221. if (curr < numVertices)
  222. {
  223. prev = curr - 1;
  224. t = distance[curr] / (distance[curr] - distance[prev]);
  225. vertex = polygon[curr] + t * (polygon[prev] - polygon[curr]);
  226. clipPolygon.push_back(vertex);
  227. // Keep the vertices on the positive side of the line.
  228. while (curr < numVertices && distance[curr] >(Real)0)
  229. {
  230. clipPolygon.push_back(polygon[curr++]);
  231. }
  232. }
  233. else
  234. {
  235. curr = 0;
  236. prev = numVertices - 1;
  237. t = distance[curr] / (distance[curr] - distance[prev]);
  238. vertex = polygon[curr] + t * (polygon[prev] - polygon[curr]);
  239. clipPolygon.push_back(vertex);
  240. }
  241. }
  242. polygon = clipPolygon;
  243. return false;
  244. }
  245. };
  246. }