IntrLine3Capsule3.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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/DistLineSegment.h>
  11. #include <Mathematics/Capsule.h>
  12. #include <Mathematics/Vector3.h>
  13. // The queries consider the capsule to be a solid.
  14. //
  15. // The test-intersection queries are based on distance computations.
  16. namespace WwiseGTE
  17. {
  18. template <typename Real>
  19. class TIQuery<Real, Line3<Real>, Capsule3<Real>>
  20. {
  21. public:
  22. struct Result
  23. {
  24. bool intersect;
  25. };
  26. Result operator()(Line3<Real> const& line, Capsule3<Real> const& capsule)
  27. {
  28. Result result;
  29. DCPQuery<Real, Line3<Real>, Segment3<Real>> lsQuery;
  30. auto lsResult = lsQuery(line, capsule.segment);
  31. result.intersect = (lsResult.distance <= capsule.radius);
  32. return result;
  33. }
  34. };
  35. template <typename Real>
  36. class FIQuery<Real, Line3<Real>, Capsule3<Real>>
  37. {
  38. public:
  39. struct Result
  40. {
  41. bool intersect;
  42. int numIntersections;
  43. std::array<Real, 2> parameter;
  44. std::array<Vector3<Real>, 2> point;
  45. };
  46. Result operator()(Line3<Real> const& line, Capsule3<Real> const& capsule)
  47. {
  48. Result result;
  49. DoQuery(line.origin, line.direction, capsule, result);
  50. for (int i = 0; i < result.numIntersections; ++i)
  51. {
  52. result.point[i] = line.origin + result.parameter[i] * line.direction;
  53. }
  54. return result;
  55. }
  56. protected:
  57. void DoQuery(Vector3<Real> const& lineOrigin,
  58. Vector3<Real> const& lineDirection, Capsule3<Real> const& capsule,
  59. Result& result)
  60. {
  61. // Initialize the result as if there is no intersection. If we
  62. // discover an intersection, these values will be modified
  63. // accordingly.
  64. result.intersect = false;
  65. result.numIntersections = 0;
  66. // Create a coordinate system for the capsule. In this system,
  67. // the capsule segment center C is the origin and the capsule axis
  68. // direction W is the z-axis. U and V are the other coordinate
  69. // axis directions. If P = x*U+y*V+z*W, the cylinder containing
  70. // the capsule wall is x^2 + y^2 = r^2, where r is the capsule
  71. // radius. The finite cylinder that makes up the capsule minus
  72. // its hemispherical end caps has z-values |z| <= e, where e is
  73. // the extent of the capsule segment. The top hemisphere cap is
  74. // x^2+y^2+(z-e)^2 = r^2 for z >= e, and the bottom hemisphere cap
  75. // is x^2+y^2+(z+e)^2 = r^2 for z <= -e.
  76. Vector3<Real> segOrigin, segDirection;
  77. Real segExtent;
  78. capsule.segment.GetCenteredForm(segOrigin, segDirection, segExtent);
  79. Vector3<Real> basis[3]; // {W, U, V}
  80. basis[0] = segDirection;
  81. ComputeOrthogonalComplement(1, basis);
  82. Real rSqr = capsule.radius * capsule.radius;
  83. // Convert incoming line origin to capsule coordinates.
  84. Vector3<Real> diff = lineOrigin - segOrigin;
  85. Vector3<Real> P{ Dot(basis[1], diff), Dot(basis[2], diff), Dot(basis[0], diff) };
  86. // Get the z-value, in capsule coordinates, of the incoming line's
  87. // unit-length direction.
  88. Real dz = Dot(basis[0], lineDirection);
  89. if (std::fabs(dz) == (Real)1)
  90. {
  91. // The line is parallel to the capsule axis. Determine
  92. // whether the line intersects the capsule hemispheres.
  93. Real radialSqrDist = rSqr - P[0] * P[0] - P[1] * P[1];
  94. if (radialSqrDist >= (Real)0)
  95. {
  96. // The line intersects the hemispherical caps.
  97. result.intersect = true;
  98. result.numIntersections = 2;
  99. Real zOffset = std::sqrt(radialSqrDist) + segExtent;
  100. if (dz > (Real)0)
  101. {
  102. result.parameter[0] = -P[2] - zOffset;
  103. result.parameter[1] = -P[2] + zOffset;
  104. }
  105. else
  106. {
  107. result.parameter[0] = P[2] - zOffset;
  108. result.parameter[1] = P[2] + zOffset;
  109. }
  110. }
  111. // else: The line outside the capsule's cylinder, no
  112. // intersection.
  113. return;
  114. }
  115. // Convert the incoming line unit-length direction to capsule
  116. // coordinates.
  117. Vector3<Real> D{ Dot(basis[1], lineDirection), Dot(basis[2], lineDirection), dz };
  118. // Test intersection of line P+t*D with infinite cylinder
  119. // x^2+y^2 = r^2. This reduces to computing the roots of a
  120. // quadratic equation. If P = (px,py,pz) and D = (dx,dy,dz), then
  121. // the quadratic equation is
  122. // (dx^2+dy^2)*t^2 + 2*(px*dx+py*dy)*t + (px^2+py^2-r^2) = 0
  123. Real a0 = P[0] * P[0] + P[1] * P[1] - rSqr;
  124. Real a1 = P[0] * D[0] + P[1] * D[1];
  125. Real a2 = D[0] * D[0] + D[1] * D[1];
  126. Real discr = a1 * a1 - a0 * a2;
  127. if (discr < (Real)0)
  128. {
  129. // The line does not intersect the infinite cylinder, so it
  130. // cannot intersect the capsule.
  131. return;
  132. }
  133. Real root, inv, tValue, zValue;
  134. if (discr > (Real)0)
  135. {
  136. // The line intersects the infinite cylinder in two places.
  137. root = std::sqrt(discr);
  138. inv = (Real)1 / a2;
  139. tValue = (-a1 - root) * inv;
  140. zValue = P[2] + tValue * D[2];
  141. if (std::fabs(zValue) <= segExtent)
  142. {
  143. result.intersect = true;
  144. result.parameter[result.numIntersections++] = tValue;
  145. }
  146. tValue = (-a1 + root) * inv;
  147. zValue = P[2] + tValue * D[2];
  148. if (std::fabs(zValue) <= segExtent)
  149. {
  150. result.intersect = true;
  151. result.parameter[result.numIntersections++] = tValue;
  152. }
  153. if (result.numIntersections == 2)
  154. {
  155. // The line intersects the capsule wall in two places.
  156. return;
  157. }
  158. }
  159. else
  160. {
  161. // The line is tangent to the infinite cylinder but intersects
  162. // the cylinder in a single point.
  163. tValue = -a1 / a2;
  164. zValue = P[2] + tValue * D[2];
  165. if (std::fabs(zValue) <= segExtent)
  166. {
  167. result.intersect = true;
  168. result.numIntersections = 1;
  169. result.parameter[0] = tValue;
  170. // Used by derived classes.
  171. result.parameter[1] = result.parameter[0];
  172. return;
  173. }
  174. }
  175. // Test intersection with bottom hemisphere. The quadratic
  176. // equation is
  177. // t^2 + 2*(px*dx+py*dy+(pz+e)*dz)*t
  178. // + (px^2+py^2+(pz+e)^2-r^2) = 0
  179. // Use the fact that currently a1 = px*dx+py*dy and
  180. // a0 = px^2+py^2-r^2. The leading coefficient is a2 = 1, so no
  181. // need to include in the construction.
  182. Real PZpE = P[2] + segExtent;
  183. a1 += PZpE * D[2];
  184. a0 += PZpE * PZpE;
  185. discr = a1 * a1 - a0;
  186. if (discr > (Real)0)
  187. {
  188. root = std::sqrt(discr);
  189. tValue = -a1 - root;
  190. zValue = P[2] + tValue * D[2];
  191. if (zValue <= -segExtent)
  192. {
  193. result.parameter[result.numIntersections++] = tValue;
  194. if (result.numIntersections == 2)
  195. {
  196. result.intersect = true;
  197. if (result.parameter[0] > result.parameter[1])
  198. {
  199. std::swap(result.parameter[0], result.parameter[1]);
  200. }
  201. return;
  202. }
  203. }
  204. tValue = -a1 + root;
  205. zValue = P[2] + tValue * D[2];
  206. if (zValue <= -segExtent)
  207. {
  208. result.parameter[result.numIntersections++] = tValue;
  209. if (result.numIntersections == 2)
  210. {
  211. result.intersect = true;
  212. if (result.parameter[0] > result.parameter[1])
  213. {
  214. std::swap(result.parameter[0], result.parameter[1]);
  215. }
  216. return;
  217. }
  218. }
  219. }
  220. else if (discr == (Real)0)
  221. {
  222. tValue = -a1;
  223. zValue = P[2] + tValue * D[2];
  224. if (zValue <= -segExtent)
  225. {
  226. result.parameter[result.numIntersections++] = tValue;
  227. if (result.numIntersections == 2)
  228. {
  229. result.intersect = true;
  230. if (result.parameter[0] > result.parameter[1])
  231. {
  232. std::swap(result.parameter[0], result.parameter[1]);
  233. }
  234. return;
  235. }
  236. }
  237. }
  238. // Test intersection with top hemisphere. The quadratic equation
  239. // is
  240. // t^2 + 2*(px*dx+py*dy+(pz-e)*dz)*t
  241. // + (px^2+py^2+(pz-e)^2-r^2) = 0
  242. // Use the fact that currently a1 = px*dx+py*dy+(pz+e)*dz and
  243. // a0 = px^2+py^2+(pz+e)^2-r^2. The leading coefficient is a2 = 1,
  244. // so no need to include in the construction.
  245. a1 -= ((Real)2) * segExtent * D[2];
  246. a0 -= ((Real)4) * segExtent * P[2];
  247. discr = a1 * a1 - a0;
  248. if (discr > (Real)0)
  249. {
  250. root = std::sqrt(discr);
  251. tValue = -a1 - root;
  252. zValue = P[2] + tValue * D[2];
  253. if (zValue >= segExtent)
  254. {
  255. result.parameter[result.numIntersections++] = tValue;
  256. if (result.numIntersections == 2)
  257. {
  258. result.intersect = true;
  259. if (result.parameter[0] > result.parameter[1])
  260. {
  261. std::swap(result.parameter[0], result.parameter[1]);
  262. }
  263. return;
  264. }
  265. }
  266. tValue = -a1 + root;
  267. zValue = P[2] + tValue * D[2];
  268. if (zValue >= segExtent)
  269. {
  270. result.parameter[result.numIntersections++] = tValue;
  271. if (result.numIntersections == 2)
  272. {
  273. result.intersect = true;
  274. if (result.parameter[0] > result.parameter[1])
  275. {
  276. std::swap(result.parameter[0], result.parameter[1]);
  277. }
  278. return;
  279. }
  280. }
  281. }
  282. else if (discr == (Real)0)
  283. {
  284. tValue = -a1;
  285. zValue = P[2] + tValue * D[2];
  286. if (zValue >= segExtent)
  287. {
  288. result.parameter[result.numIntersections++] = tValue;
  289. if (result.numIntersections == 2)
  290. {
  291. result.intersect = true;
  292. if (result.parameter[0] > result.parameter[1])
  293. {
  294. std::swap(result.parameter[0], result.parameter[1]);
  295. }
  296. return;
  297. }
  298. }
  299. }
  300. if (result.numIntersections == 1)
  301. {
  302. // Used by derived classes.
  303. result.parameter[1] = result.parameter[0];
  304. }
  305. }
  306. };
  307. }