IntersectionTests.cpp 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/Math/Vector2.h>
  9. #include <AzCore/Math/Vector3.h>
  10. #include <AzCore/Math/IntersectSegment.h>
  11. #include <AzCore/Math/MathUtils.h>
  12. #include <AzCore/Math/Random.h>
  13. #include <AzCore/Math/Geometry3DUtils.h>
  14. #include <AzCore/UnitTest/TestTypes.h>
  15. #include <AZTestShared/Math/MathTestHelpers.h>
  16. #include <random>
  17. #include <Tests/Math/IntersectionTestHelpers.h>
  18. using namespace AZ;
  19. namespace UnitTest
  20. {
  21. TEST(MATH_Intersection, ClosestSegmentSegment)
  22. {
  23. // line2 right and above line1 (no overlap, parallel)
  24. {
  25. Vector3 line1Start(0.0f, 0.0f, 0.0f);
  26. Vector3 line1End(4.0f, 0.0f, 0.0f);
  27. Vector3 line2Start(7.0f, 0.0f, 4.0f);
  28. Vector3 line2End(10.0f, 0.0f, 4.0f);
  29. Vector3 line1ClosestPoint;
  30. Vector3 line2ClosestPoint;
  31. float line1Proportion;
  32. float line2Proportion;
  33. Intersect::ClosestSegmentSegment(line1Start, line1End, line2Start, line2End, line1Proportion, line2Proportion, line1ClosestPoint, line2ClosestPoint);
  34. float pointDifference = (line2ClosestPoint - line1ClosestPoint).GetLength();
  35. EXPECT_NEAR(pointDifference, 5.0f, AZ::Constants::Tolerance);
  36. EXPECT_NEAR(line1Proportion, 1.0f, AZ::Constants::Tolerance);
  37. EXPECT_NEAR(line2Proportion, 0.0f, AZ::Constants::Tolerance);
  38. }
  39. // line2 halfway over the top of the line1 (overlap, parallel)
  40. {
  41. Vector3 line1Start(0.0f, 0.0f, 0.0f);
  42. Vector3 line1End(4.0f, 0.0f, 0.0f);
  43. Vector3 line2Start(2.0f, 0.0f, 3.0f);
  44. Vector3 line2End(6.0f, 0.0f, 3.0f);
  45. Vector3 line1ClosestPoint;
  46. Vector3 line2ClosestPoint;
  47. float line1Proportion;
  48. float line2Proportion;
  49. Intersect::ClosestSegmentSegment(line1Start, line1End, line2Start, line2End, line1Proportion, line2Proportion, line1ClosestPoint, line2ClosestPoint);
  50. float pointDifference = (line2ClosestPoint - line1ClosestPoint).GetLength();
  51. EXPECT_NEAR(pointDifference, 3.0f, AZ::Constants::Tolerance);
  52. EXPECT_NEAR(line1Proportion, 0.5f, AZ::Constants::Tolerance);
  53. EXPECT_NEAR(line2Proportion, 0.0f, AZ::Constants::Tolerance);
  54. }
  55. // line2 over the top of the line1 (inside, parallel)
  56. {
  57. Vector3 line1Start(0.0f, 0.0f, 0.0f);
  58. Vector3 line1End(8.0f, 0.0f, 0.0f);
  59. Vector3 line2Start(2.0f, 0.0f, 3.0f);
  60. Vector3 line2End(6.0f, 0.0f, 3.0f);
  61. Vector3 line1ClosestPoint;
  62. Vector3 line2ClosestPoint;
  63. float line1Proportion;
  64. float line2Proportion;
  65. Intersect::ClosestSegmentSegment(line1Start, line1End, line2Start, line2End, line1Proportion, line2Proportion, line1ClosestPoint, line2ClosestPoint);
  66. float pointDifference = (line2ClosestPoint - line1ClosestPoint).GetLength();
  67. EXPECT_NEAR(pointDifference, 3.0f, AZ::Constants::Tolerance);
  68. EXPECT_NEAR(line1Proportion, 0.25f, AZ::Constants::Tolerance);
  69. EXPECT_NEAR(line2Proportion, 0.0f, AZ::Constants::Tolerance);
  70. }
  71. // line2 over the top of the line1 (overlap, skew (cross))
  72. {
  73. Vector3 line1Start(0.0f, 0.0f, 0.0f);
  74. Vector3 line1End(8.0f, 0.0f, 0.0f);
  75. Vector3 line2Start(4.0f, 4.0f, 4.0f);
  76. Vector3 line2End(4.0f, -4.0f, 4.0f);
  77. Vector3 line1ClosestPoint;
  78. Vector3 line2ClosestPoint;
  79. float line1Proportion;
  80. float line2Proportion;
  81. Intersect::ClosestSegmentSegment(line1Start, line1End, line2Start, line2End, line1Proportion, line2Proportion, line1ClosestPoint, line2ClosestPoint);
  82. float pointDifference = (line2ClosestPoint - line1ClosestPoint).GetLength();
  83. EXPECT_NEAR(pointDifference, 4.0f, AZ::Constants::Tolerance);
  84. EXPECT_NEAR(line1Proportion, 0.5f, AZ::Constants::Tolerance);
  85. EXPECT_NEAR(line2Proportion, 0.5f, AZ::Constants::Tolerance);
  86. }
  87. // line2 flat diagonal to line1 (no overlap, skew)
  88. {
  89. Vector3 line1Start(0.0f, 0.0f, 0.0f);
  90. Vector3 line1End(4.0f, 4.0f, 0.0f);
  91. Vector3 line2Start(10.0f, 0.0f, 0.0f);
  92. Vector3 line2End(6.0f, 4.0f, 0.0f);
  93. Vector3 line1ClosestPoint;
  94. Vector3 line2ClosestPoint;
  95. float line1Proportion;
  96. float line2Proportion;
  97. Intersect::ClosestSegmentSegment(line1Start, line1End, line2Start, line2End, line1Proportion, line2Proportion, line1ClosestPoint, line2ClosestPoint);
  98. float pointDifference = (line2ClosestPoint - line1ClosestPoint).GetLength();
  99. EXPECT_NEAR(pointDifference, 2.0f, AZ::Constants::Tolerance);
  100. EXPECT_NEAR(line1Proportion, 1.0f, AZ::Constants::Tolerance);
  101. EXPECT_NEAR(line2Proportion, 1.0f, AZ::Constants::Tolerance);
  102. }
  103. // line2 perpendicular to line1 (skew, no overlap)
  104. {
  105. Vector3 line1Start(0.0f, 0.0f, 0.0f);
  106. Vector3 line1End(4.0f, 0.0f, 0.0f);
  107. Vector3 line2Start(2.0f, 1.0f, 0.0f);
  108. Vector3 line2End(2.0f, 4.0f, 0.0f);
  109. Vector3 line1ClosestPoint;
  110. Vector3 line2ClosestPoint;
  111. float line1Proportion;
  112. float line2Proportion;
  113. Intersect::ClosestSegmentSegment(line1Start, line1End, line2Start, line2End, line1Proportion, line2Proportion, line1ClosestPoint, line2ClosestPoint);
  114. float pointDifference = (line2ClosestPoint - line1ClosestPoint).GetLength();
  115. EXPECT_NEAR(pointDifference, 1.0f, AZ::Constants::Tolerance);
  116. EXPECT_NEAR(line1Proportion, 0.5f, AZ::Constants::Tolerance);
  117. EXPECT_NEAR(line2Proportion, 0.0f, AZ::Constants::Tolerance);
  118. }
  119. // line 1 degenerates to point
  120. {
  121. Vector3 line1Start(4.0f, 2.0f, 0.0f);
  122. Vector3 line1End(4.0f, 2.0f, 0.0f);
  123. Vector3 line2Start(2.0f, 0.0f, 0.0f);
  124. Vector3 line2End(2.0f, 4.0f, 0.0f);
  125. Vector3 line1ClosestPoint;
  126. Vector3 line2ClosestPoint;
  127. float line1Proportion;
  128. float line2Proportion;
  129. Intersect::ClosestSegmentSegment(line1Start, line1End, line2Start, line2End, line1Proportion, line2Proportion, line1ClosestPoint, line2ClosestPoint);
  130. float pointDifference = (line2ClosestPoint - line1ClosestPoint).GetLength();
  131. EXPECT_NEAR(pointDifference, 2.0f, AZ::Constants::Tolerance);
  132. EXPECT_NEAR(line1Proportion, 0.0f, AZ::Constants::Tolerance);
  133. EXPECT_NEAR(line2Proportion, 0.5f, AZ::Constants::Tolerance);
  134. }
  135. // line 2 degenerates to point
  136. {
  137. Vector3 line1Start(0.0f, 0.0f, 0.0f);
  138. Vector3 line1End(4.0f, 0.0f, 0.0f);
  139. Vector3 line2Start(2.0f, 1.0f, 0.0f);
  140. Vector3 line2End(2.0f, 1.0f, 0.0f);
  141. Vector3 line1ClosestPoint;
  142. Vector3 line2ClosestPoint;
  143. float line1Proportion;
  144. float line2Proportion;
  145. Intersect::ClosestSegmentSegment(line1Start, line1End, line2Start, line2End, line1Proportion, line2Proportion, line1ClosestPoint, line2ClosestPoint);
  146. float pointDifference = (line2ClosestPoint - line1ClosestPoint).GetLength();
  147. EXPECT_NEAR(pointDifference, 1.0f, AZ::Constants::Tolerance);
  148. EXPECT_NEAR(line1Proportion, 0.5f, AZ::Constants::Tolerance);
  149. EXPECT_NEAR(line2Proportion, 0.0f, AZ::Constants::Tolerance);
  150. }
  151. // both lines degenerate to points
  152. {
  153. Vector3 line1Start(5.0f, 5.0f, 5.0f);
  154. Vector3 line1End(5.0f, 5.0f, 5.0f);
  155. Vector3 line2Start(10.0f, 10.0f, 10.0f);
  156. Vector3 line2End(10.0f, 10.0f, 10.0f);
  157. Vector3 line1ClosestPoint;
  158. Vector3 line2ClosestPoint;
  159. float line1Proportion;
  160. float line2Proportion;
  161. Intersect::ClosestSegmentSegment(line1Start, line1End, line2Start, line2End, line1Proportion, line2Proportion, line1ClosestPoint, line2ClosestPoint);
  162. float pointDifference = (line2ClosestPoint - line1ClosestPoint).GetLength();
  163. // (10, 10, 10) - (5, 5, 5) == (5, 5, 5)
  164. // |(5,5,5)| == sqrt(5*5+5*5+5*5) == sqrt(75)
  165. EXPECT_NEAR(pointDifference, sqrtf(75.0f), AZ::Constants::Tolerance);
  166. EXPECT_NEAR(line1Proportion, 0.0f, AZ::Constants::Tolerance);
  167. EXPECT_NEAR(line2Proportion, 0.0f, AZ::Constants::Tolerance);
  168. }
  169. }
  170. TEST(MATH_Intersection, ClosestPointSegment)
  171. {
  172. // point above center of line
  173. {
  174. Vector3 lineStart(0.0f, 0.0f, 0.0f);
  175. Vector3 lineEnd(4.0f, 0.0f, 0.0f);
  176. Vector3 point(2.0f, 2.0f, 0.0f);
  177. Vector3 lineClosestPoint;
  178. float lineProportion;
  179. Intersect::ClosestPointSegment(point, lineStart, lineEnd, lineProportion, lineClosestPoint);
  180. float pointDifference = (lineClosestPoint - point).GetLength();
  181. EXPECT_NEAR(pointDifference, 2.0f, AZ::Constants::Tolerance);
  182. EXPECT_NEAR(lineProportion, 0.5f, AZ::Constants::Tolerance);
  183. }
  184. // point same height behind line
  185. {
  186. Vector3 lineStart(0.0f, 0.0f, 0.0f);
  187. Vector3 lineEnd(0.0f, 4.0f, 0.0f);
  188. Vector3 point(0.0f, -2.0f, 0.0f);
  189. Vector3 lineClosestPoint;
  190. float lineProportion;
  191. Intersect::ClosestPointSegment(point, lineStart, lineEnd, lineProportion, lineClosestPoint);
  192. float pointDifference = (lineClosestPoint - point).GetLength();
  193. EXPECT_NEAR(pointDifference, 2.0f, AZ::Constants::Tolerance);
  194. EXPECT_NEAR(lineProportion, 0.0f, AZ::Constants::Tolerance);
  195. }
  196. // point passed end of line
  197. {
  198. Vector3 lineStart(0.0f, 0.0f, 0.0f);
  199. Vector3 lineEnd(0.0f, 0.0f, 10.0f);
  200. Vector3 point(0.0f, 0.0f, 15.0f);
  201. Vector3 lineClosestPoint;
  202. float lineProportion;
  203. Intersect::ClosestPointSegment(point, lineStart, lineEnd, lineProportion, lineClosestPoint);
  204. float pointDifference = (lineClosestPoint - point).GetLength();
  205. EXPECT_NEAR(pointDifference, 5.0f, AZ::Constants::Tolerance);
  206. EXPECT_NEAR(lineProportion, 1.0f, AZ::Constants::Tolerance);
  207. }
  208. // point above part way along line
  209. {
  210. Vector3 lineStart(0.0f, 0.0f, 0.0f);
  211. Vector3 lineEnd(4.0f, 4.0f, 0.0f);
  212. Vector3 point(3.0f, 3.0f, -1.0f);
  213. Vector3 lineClosestPoint;
  214. float lineProportion;
  215. Intersect::ClosestPointSegment(point, lineStart, lineEnd, lineProportion, lineClosestPoint);
  216. float pointDifference = (lineClosestPoint - point).GetLength();
  217. EXPECT_NEAR(pointDifference, 1.0f, AZ::Constants::Tolerance);
  218. EXPECT_NEAR(lineProportion, 0.75f, AZ::Constants::Tolerance);
  219. }
  220. }
  221. class MATH_IntersectSegmentTriangleTest : public LeakDetectionFixture
  222. {
  223. public:
  224. void ClearResultsOnMiss(bool didHit, AZ::Vector3& outNormal, float& outDistance)
  225. {
  226. if (!didHit)
  227. {
  228. outDistance = 0.0f;
  229. outNormal = AZ::Vector3::CreateZero();
  230. }
  231. }
  232. };
  233. TEST_F(MATH_IntersectSegmentTriangleTest, SegmentThroughSphereCenterHasCorrectIntersectionCount)
  234. {
  235. // This verifies the general correctness of the segment/triangle intersections by generating a mesh sphere with ~20k triangles
  236. // and a large quantity of segments that start at random locations outside the sphere and pass directly through the center of
  237. // the sphere to end at the same location on the opposite side of the sphere.
  238. // Each segment should have exactly one triangle collision (the sphere entry point) for one-sided tests, and two triangle
  239. // collisions (the sphere entry and exit points) for two-sided tests.
  240. // Segment/triangle algorithms that aren't "watertight" (such as Arensen and Moller Trumbore) will fail this test
  241. // when run with a high enough quantity of generated segments.
  242. // Generate a ~20k triangle sphere of unit size centered at the origin.
  243. constexpr uint8_t SubdivisionDepth = 5;
  244. AZStd::vector<AZ::Vector3> sphereGeometry = AZ::Geometry3dUtils::GenerateIcoSphere(SubdivisionDepth);
  245. // Create random number generators in the positive 2-50 range so that we're outside the sphere bounds.
  246. constexpr unsigned int Seed = 1;
  247. std::mt19937_64 rng(Seed);
  248. std::uniform_real_distribution<float> unif(2.0f, 50.0f);
  249. // When getting the random number, return numbers in the [-50, -2] or [2, 50] ranges so that we cover both negative and positive
  250. // positional values, but they're all outside the sphere bounds.
  251. auto GetRandomPosition = [&rng, &unif]() -> float
  252. {
  253. float num = unif(rng);
  254. return (rand() & 0x01) ? num : (num * -1.0f);
  255. };
  256. // This is enough iterations to generate a failing result on both the Arenberg and the Moller Trumbore algorithms.
  257. // (Specific triangles causing their failures have been captured and tested in a separate regression unit test)
  258. constexpr int NumTests = 2500;
  259. // We'll keep track of the specific segment/triangle information for any hit so that it's easier to add false negatives
  260. // and false positives to the regression unit test.
  261. AZStd::vector<float> hitDistanceCCW;
  262. AZStd::vector<float> hitDistance;
  263. AZStd::vector<AZ::Vector3> hitNormalCCW;
  264. AZStd::vector<AZ::Vector3> hitNormal;
  265. AZStd::vector<AZ::Vector3> vertsForFalseResultCCW;
  266. AZStd::vector<AZ::Vector3> vertsForFalseResult;
  267. for (int test = 0; test < NumTests; test++)
  268. {
  269. // Generate a random ray segment that crosses through the center of the sphere and out the other side.
  270. AZ::Vector3 rayStart(GetRandomPosition(), GetRandomPosition(), GetRandomPosition());
  271. AZ::Vector3 rayEnd = -rayStart;
  272. AZ::Intersect::SegmentTriangleHitTester hitTester(rayStart, rayEnd);
  273. float outDistance = 0.0f;
  274. AZ::Vector3 outNormal = AZ::Vector3::CreateZero();
  275. hitDistanceCCW.clear();
  276. hitDistance.clear();
  277. hitNormalCCW.clear();
  278. hitNormal.clear();
  279. vertsForFalseResultCCW.clear();
  280. vertsForFalseResult.clear();
  281. // Run through all the triangles and look for hits. For each segment that we test, there should be exactly
  282. // one hit for one-sided tests (sphere entry), and two hits for two-sided tests (sphere entry and exit).
  283. for (int triangle = 0; triangle < sphereGeometry.size(); triangle += 3)
  284. {
  285. // One-sided triangle hit test
  286. if (hitTester.IntersectSegmentTriangleCCW(
  287. sphereGeometry[triangle], sphereGeometry[triangle + 1], sphereGeometry[triangle + 2], outNormal, outDistance))
  288. {
  289. hitDistanceCCW.emplace_back(outDistance);
  290. hitNormalCCW.emplace_back(outNormal);
  291. vertsForFalseResultCCW.emplace_back(sphereGeometry[triangle]);
  292. vertsForFalseResultCCW.emplace_back(sphereGeometry[triangle + 1]);
  293. vertsForFalseResultCCW.emplace_back(sphereGeometry[triangle + 2]);
  294. }
  295. // Two-sided triangle hit test
  296. if (hitTester.IntersectSegmentTriangle(
  297. sphereGeometry[triangle], sphereGeometry[triangle + 1], sphereGeometry[triangle + 2], outNormal, outDistance))
  298. {
  299. hitDistance.emplace_back(outDistance);
  300. hitNormal.emplace_back(outNormal);
  301. vertsForFalseResult.emplace_back(sphereGeometry[triangle]);
  302. vertsForFalseResult.emplace_back(sphereGeometry[triangle + 1]);
  303. vertsForFalseResult.emplace_back(sphereGeometry[triangle + 2]);
  304. }
  305. }
  306. // Check results of one-sided test
  307. if (hitDistanceCCW.size() < 1)
  308. {
  309. EXPECT_EQ(hitDistanceCCW.size(), 1)
  310. << "One-sided test: False negative, no triangles hit by segment (" << rayStart.GetX() << ", " << rayStart.GetY()
  311. << ", " << rayStart.GetZ() << ") - (" << rayEnd.GetX() << ", " << rayEnd.GetY() << ", " << rayEnd.GetZ() << ")\n";
  312. }
  313. else if (hitDistanceCCW.size() > 1)
  314. {
  315. EXPECT_EQ(hitDistanceCCW.size(), 1)
  316. << "One-sided test: False positive, too many triangles hit by segment (" << rayStart.GetX() << ", " << rayStart.GetY()
  317. << ", " << rayStart.GetZ() << ") - (" << rayEnd.GetX() << ", " << rayEnd.GetY() << ", " << rayEnd.GetZ() << ")\n";
  318. }
  319. // Check results of two-sided test
  320. if (hitDistance.size() < 2)
  321. {
  322. EXPECT_EQ(hitDistance.size(), 2)
  323. << "Two-sided test: False negative, too few triangles hit by segment (" << rayStart.GetX() << ", " << rayStart.GetY()
  324. << ", " << rayStart.GetZ() << ") - (" << rayEnd.GetX() << ", " << rayEnd.GetY() << ", " << rayEnd.GetZ() << ")\n";
  325. }
  326. else if (hitDistance.size() > 2)
  327. {
  328. EXPECT_EQ(hitDistance.size(), 2)
  329. << "Two-sided test: False positive, too many triangles hit by segment (" << rayStart.GetX() << ", " << rayStart.GetY()
  330. << ", " << rayStart.GetZ() << ") - (" << rayEnd.GetX() << ", " << rayEnd.GetY() << ", " << rayEnd.GetZ() << ")\n";
  331. }
  332. }
  333. }
  334. TEST_F(MATH_IntersectSegmentTriangleTest, CompareSegmentTriangleIntersectionMethods)
  335. {
  336. // This verifies "relative correctness" of the segment/triangle intersection code by comparing the results against
  337. // two other segment/triangle intersection methods and verifying that all three produce the same answers. The test uses
  338. // randomly-generated (but deterministic) segments and triangles, so the vast majority will miss, and some will hit. The
  339. // test tracks counts of hits and misses just to verify that we've gotten at least one hit and one miss.
  340. //
  341. // Note that this test only works for smaller quantities of test iterations. If it is run too many times, some differences
  342. // will show up because the algorithms we're comparing against have non-zero failure rates of false positives and false
  343. // negatives when the segments intersect edges and vertices. The current seed and iteration count have been chosen so that
  344. // all three produce identical correct results, but have previously been run at > 10M iterations. The few failures that showed up
  345. // across those iterations have been cross-checked for accuracy and then added to specific-case regression unit test below.
  346. constexpr unsigned int Seed = 1;
  347. std::mt19937_64 rng(Seed);
  348. std::uniform_real_distribution<float> unif(-1000.0f, 1000.0f);
  349. // Track the number of hits, misses, and differences we have across the iterations.
  350. // We'll use the hit / miss counts to verify that we've successfully tested at least one of each type of case.
  351. int numDifferentHits = 0;
  352. int numHits = 0;
  353. int numMisses = 0;
  354. // We're comparing 3 different algorithms against each other.
  355. bool didHit[3];
  356. float outDistance[3];
  357. AZ::Vector3 outNormal[3];
  358. constexpr int NumTests = 10000;
  359. for (int test = 0; test < NumTests; test++)
  360. {
  361. AZ::Vector3 rayStart(unif(rng), unif(rng), unif(rng));
  362. AZ::Vector3 rayEnd(unif(rng), unif(rng), unif(rng));
  363. AZ::Vector3 triVerts0(unif(rng), unif(rng), unif(rng));
  364. AZ::Vector3 triVerts1(unif(rng), unif(rng), unif(rng));
  365. AZ::Vector3 triVerts2(unif(rng), unif(rng), unif(rng));
  366. // Test our current watertight algorithm implementation
  367. didHit[0] = AZ::Intersect::IntersectSegmentTriangle(
  368. rayStart, rayEnd, triVerts0, triVerts1, triVerts2, outNormal[0], outDistance[0]);
  369. ClearResultsOnMiss(didHit[0], outNormal[0], outDistance[0]);
  370. // Test using a Moller Trumbore algorithm implementation
  371. didHit[1] = IntersectTest::MollerTrumboreIntersectSegmentTriangle(
  372. rayStart, rayEnd, triVerts0, triVerts1, triVerts2, outNormal[1], outDistance[1]);
  373. ClearResultsOnMiss(didHit[1], outNormal[1], outDistance[1]);
  374. // Test using an Arenberg algorithm implementation
  375. didHit[2] = IntersectTest::ArenbergIntersectSegmentTriangle(
  376. rayStart, rayEnd, triVerts0, triVerts1, triVerts2, outNormal[2], outDistance[2]);
  377. ClearResultsOnMiss(didHit[2], outNormal[2], outDistance[2]);
  378. if ((didHit[0] != didHit[1]) || (didHit[0] != didHit[2]))
  379. {
  380. // At least one of our three implementations produced different results.
  381. // If this triggers, the specific segment and triangle should get examined further.
  382. // This likely happened because of different behaviors in the algorithms on edge or vertex collisions.
  383. numDifferentHits++;
  384. EXPECT_EQ(didHit[0], didHit[1]);
  385. EXPECT_EQ(didHit[0], didHit[2]);
  386. }
  387. else
  388. {
  389. // If all three algorithms agree on a hit, verify that the results of the hit match across all three.
  390. if (didHit[0] && didHit[1] && didHit[2])
  391. {
  392. // These case should never fail. A failure means that all the algorithms agree there was a hit, but
  393. // somehow disagree on the location of the hit or the normal of the triangle that was hit.
  394. if (!AZ::IsClose(outDistance[0], outDistance[1]) || !AZ::IsClose(outDistance[0], outDistance[2]))
  395. {
  396. numDifferentHits++;
  397. EXPECT_NEAR(outDistance[0], outDistance[1], 0.00001f);
  398. EXPECT_NEAR(outDistance[0], outDistance[2], 0.00001f);
  399. }
  400. else if (!outNormal[0].IsClose(outNormal[1]) || !outNormal[0].IsClose(outNormal[2]))
  401. {
  402. numDifferentHits++;
  403. EXPECT_THAT(outNormal[0], IsClose(outNormal[1]));
  404. EXPECT_THAT(outNormal[0], IsClose(outNormal[2]));
  405. }
  406. }
  407. }
  408. if (didHit[0])
  409. {
  410. numHits++;
  411. }
  412. else
  413. {
  414. numMisses++;
  415. }
  416. }
  417. // Verify that all three algorithms produced the same results across all our iterations.
  418. EXPECT_EQ(numDifferentHits, 0);
  419. // Verify that we tested at least one test case each of a hit and a miss.
  420. EXPECT_GT(numHits, 0);
  421. EXPECT_GT(numMisses, 0);
  422. }
  423. struct RayTriangleTest
  424. {
  425. AZ::Vector3 m_rayStart;
  426. AZ::Vector3 m_rayEnd;
  427. AZ::Vector3 m_triVerts[3];
  428. bool m_shouldHit;
  429. float m_hitDistance;
  430. AZ::Vector3 m_hitNormal;
  431. };
  432. static const RayTriangleTest RayTriangleTestParams[] =
  433. {
  434. // This failure came from a vegetation system raycast to a triangle in a sphere model.
  435. {
  436. { 0.0f, 0.16f, 1.01948643f }, { 0.0f, 0.16f, -0.0194873810f }, // segment
  437. { { 0.0294727981f, 0.18608f, 0.9605f }, { 0.0f, 0.18842f, 0.9605f }, { 0.0f, 0.15215f, 0.9741f } }, // triangle
  438. true, 0.0465169623f, { 0.0278645027f, 0.350958735f, 0.935976326f } // expected results
  439. },
  440. // These failures were generated from purely random segment/triangle combinations that were cross-checked against
  441. // multiple algorithms in the "CompareSegmentTriangleIntersectionMethods" unit test.
  442. {
  443. { 962.503540f, -788.401978f, -320.390747f }, { -838.229004f, 555.508301f, 748.038086f }, // segment
  444. { { -353.655212f, 579.209229f, -827.134216f }, { 358.185913f, 346.845093f, 729.328979f }, { 222.018555f, -358.014160f, -4.521179f } }, // triangle
  445. true, 0.399751157f, { 0.899101138f, 0.220195562f, -0.378326714f } // expected results
  446. },
  447. {
  448. { 444.587769f, 667.560425f, -30.933167f }, { -701.140015f, -835.056152f, -521.363892f }, // segment
  449. { { -711.895020f, -679.238892f, -727.331543f }, { -269.888733f, 985.126099f, 592.807617f }, { 752.411377f, 504.572876f, 772.386597f } }, // triangle
  450. true, 0.714566f, { 0.376415f, 0.512304f, -0.771917f } // expected results
  451. },
  452. {
  453. { -441.883545f, -278.100342f, 903.960938f }, { -475.418335f, 334.792114f, 420.302734f }, // segment
  454. { { 717.479004f, -116.081299f, -610.493530f }, { 603.540039f, -305.713684f, -86.320618f }, { -466.514709f, 160.187012f, 558.720825f } }, // triangle
  455. false, 0.0f, { 0.0f, 0.0f, 0.0f } // expected results
  456. },
  457. // These failures were generated from combinations of random segments and a generated icosphere in the
  458. // "SegmentThroughSphereCenterHasCorrectIntersectionCount" unit test
  459. {
  460. {42.6055908f, 31.7755775f, -13.6140671f}, {-42.6055908f, -31.7755775f, 13.6140671f}, // segment
  461. { { 0.797190964f, 0.557785630f, -0.231001750f }, { 0.775978208f, 0.576951861f, -0.254920423f }, { 0.778470039f, 0.588076711f, -0.219431564f } }, // triangle
  462. true, 0.490887880f, { 0.784085453f, 0.574396968f, -0.235112920f } // expected results
  463. },
  464. {
  465. {42.6055908f, 31.7755775f, -13.6140671f}, {-42.6055908f, -31.7755775f, 13.6140671f}, // segment
  466. { { 0.756726921f, 0.606817961f, -0.243179753f }, { 0.778470039f, 0.588076711f, -0.219431564f }, { 0.775978208f, 0.576951861f, -0.254920423f } }, // triangle
  467. false, 0.0f, {0.0f, 0.0f, 0.0f} // expected results
  468. },
  469. {
  470. { -46.8041611f, -7.23778582f, 18.3756104f }, { 46.8041611f, 7.23778582f, -18.3756104f }, // segment
  471. {{ -0.935130417f, -0.122800261f, 0.332341939f },{ -0.921611011f, -0.143026009f, 0.360800087f }, { -0.910672069f, -0.122609399f, 0.394516677f } }, // triangle
  472. true, 0.490157783f, {-0.925243855f, -0.105879672f, 0.364298195f} // expected results
  473. },
  474. {
  475. { 20.5424690f, -33.8884621f, 26.2357140f }, { -20.5424690f, 33.8884621f, -26.2357140f }, // segment
  476. { { 0.448259473f, -0.725299001f, 0.522498608f }, { 0.437129468f, -0.707290292f, 0.555570245f }, { 0.415217489f, -0.732416809f, 0.539592505f } }, // triangle
  477. true, 0.489480674f, { 0.434347391f, -0.721596122f, 0.539111614f } // expected results
  478. },
  479. {
  480. { 20.5424690f, -33.8884621f, 26.2357140f }, { -20.5424690f, 33.8884621f, -26.2357140f }, // segment
  481. { { 0.403728217f, -0.713824689f, 0.572239339f }, { 0.415217489f, -0.732416809f, 0.539592505f }, { 0.437129468f, -0.707290292f, 0.555570245f } }, // triangle
  482. false, 0.0f, {0.0f, 0.0f, 0.0f} // expected results
  483. },
  484. // These failures previously came from the vegetation system and the Editor using FLT_MAX as segment start or end values.
  485. {
  486. { 5.0f, -15.0f, 1.0f }, { 5.0f, -15.0f, -FLT_MAX }, // segment
  487. { { 0.0f, -10.0f, 0.0f }, { 0.0f, -20.0f, 0.0f }, { 10.0f, -10.0f, 0.0f } }, // triangle
  488. true, 0.0f, {0.0f, 0.0f, 1.0f} // expected results
  489. },
  490. {
  491. { 5.0f, -15.0f, FLT_MAX }, { 5.0f, -15.0f, -1.0f }, // segment
  492. { { 0.0f, -10.0f, 0.0f }, { 0.0f, -20.0f, 0.0f }, { 10.0f, -10.0f, 0.0f } }, // triangle
  493. true, 1.0f, {0.0f, 0.0f, 1.0f} // expected results
  494. },
  495. };
  496. using RayTriangleTests = ::testing::TestWithParam<RayTriangleTest>;
  497. TEST_P(RayTriangleTests, RegressionTestForSpecificSegmentsAndTriangles)
  498. {
  499. // This unit test is set up to validate regressions of erroneous segment/triangle intersection test results.
  500. // All the failures that have been found so far have been false negatives or false positives that occur on triangle
  501. // edges or vertices.
  502. const RayTriangleTest test = GetParam();
  503. AZ::Vector3 outNormal = AZ::Vector3::CreateZero();
  504. float outDistance = 0.0f;
  505. bool result = AZ::Intersect::IntersectSegmentTriangleCCW(
  506. test.m_rayStart, test.m_rayEnd, test.m_triVerts[0], test.m_triVerts[1], test.m_triVerts[2], outNormal, outDistance);
  507. EXPECT_EQ(test.m_shouldHit, result);
  508. if (test.m_shouldHit && result)
  509. {
  510. EXPECT_NEAR(test.m_hitDistance, outDistance, 0.0001f);
  511. EXPECT_THAT(test.m_hitNormal, IsClose(outNormal));
  512. }
  513. }
  514. INSTANTIATE_TEST_SUITE_P(MATH_IntersectSegmentTriangleTest, RayTriangleTests, ::testing::ValuesIn(RayTriangleTestParams));
  515. class MATH_IntersectRayCappedCylinderTest
  516. : public LeakDetectionFixture
  517. {
  518. protected:
  519. void SetUp() override
  520. {
  521. m_cylinderEnd1 = Vector3(1.1f, 2.2f, 3.3f);
  522. m_cylinderDir = Vector3(1.0, 1.0f, 1.0f);
  523. m_cylinderDir.Normalize();
  524. m_radiusDir = Vector3(-1.0f, 1.0f, 0.0f);
  525. m_radiusDir.Normalize();
  526. m_cylinderHeight = 5.5f;
  527. m_cylinderRadius = 3.768f;
  528. }
  529. void TearDown() override
  530. {
  531. }
  532. Vector3 m_cylinderEnd1;
  533. Vector3 m_cylinderDir;
  534. Vector3 m_radiusDir;
  535. float m_cylinderHeight;
  536. float m_cylinderRadius;
  537. };
  538. TEST_F(MATH_IntersectRayCappedCylinderTest, RayOverlapCylinderAxis)
  539. {
  540. Vector3 rayOrigin = m_cylinderEnd1 - 0.5f * m_cylinderHeight * m_cylinderDir;
  541. Vector3 rayDir = m_cylinderDir;
  542. float t1 = AZ::Constants::FloatMax;
  543. float t2 = AZ::Constants::FloatMax;
  544. int hits = Intersect::IntersectRayCappedCylinder(rayOrigin, rayDir, m_cylinderEnd1, m_cylinderDir, m_cylinderHeight, m_cylinderRadius, t1, t2);
  545. EXPECT_EQ(hits, 2);
  546. }
  547. TEST_F(MATH_IntersectRayCappedCylinderTest, RayParallelToAndInsideCylinder)
  548. {
  549. Vector3 rayOrigin = m_cylinderEnd1 - 0.5f * m_cylinderHeight * m_cylinderDir + 0.5f * m_cylinderRadius * m_radiusDir;
  550. Vector3 rayDir = m_cylinderDir;
  551. float t1 = AZ::Constants::FloatMax;
  552. float t2 = AZ::Constants::FloatMax;
  553. int hits = Intersect::IntersectRayCappedCylinder(rayOrigin, rayDir, m_cylinderEnd1, m_cylinderDir, m_cylinderHeight, m_cylinderRadius, t1, t2);
  554. EXPECT_EQ(hits, 2);
  555. }
  556. TEST_F(MATH_IntersectRayCappedCylinderTest, RayAlongCylinderSuface)
  557. {
  558. Vector3 rayOrigin = m_cylinderEnd1 - 0.5f * m_cylinderHeight * m_cylinderDir + m_cylinderRadius * m_radiusDir;
  559. Vector3 rayDir = m_cylinderDir;
  560. float t1 = AZ::Constants::FloatMax;
  561. float t2 = AZ::Constants::FloatMax;
  562. int hits = Intersect::IntersectRayCappedCylinder(rayOrigin, rayDir, m_cylinderEnd1, m_cylinderDir, m_cylinderHeight, m_cylinderRadius, t1, t2);
  563. EXPECT_EQ(hits, 2);
  564. }
  565. TEST_F(MATH_IntersectRayCappedCylinderTest, RayParallelAndRayOriginInsideCylinder)
  566. {
  567. Vector3 rayOrigin = m_cylinderEnd1 + 0.2f * m_cylinderHeight * m_cylinderDir + 0.5f * m_cylinderRadius * m_radiusDir;
  568. Vector3 rayDir = m_cylinderDir;
  569. float t1 = AZ::Constants::FloatMax;
  570. float t2 = AZ::Constants::FloatMax;
  571. int hits = Intersect::IntersectRayCappedCylinder(rayOrigin, rayDir, m_cylinderEnd1, m_cylinderDir, m_cylinderHeight, m_cylinderRadius, t1, t2);
  572. EXPECT_EQ(hits, 1);
  573. }
  574. TEST_F(MATH_IntersectRayCappedCylinderTest, RayParallelToButOutsideCylinder)
  575. {
  576. Vector3 rayOrigin = m_cylinderEnd1 - 0.5f * m_cylinderHeight * m_cylinderDir + 1.1f * m_cylinderRadius * m_radiusDir;
  577. Vector3 rayDir = m_cylinderDir;
  578. float t1 = AZ::Constants::FloatMax;
  579. float t2 = AZ::Constants::FloatMax;
  580. int hits = Intersect::IntersectRayCappedCylinder(rayOrigin, rayDir, m_cylinderEnd1, m_cylinderDir, m_cylinderHeight, m_cylinderRadius, t1, t2);
  581. EXPECT_EQ(hits, 0);
  582. }
  583. TEST_F(MATH_IntersectRayCappedCylinderTest, RayOriginOuside_RayDirParallelButPointingAwayCylinder)
  584. {
  585. Vector3 rayOrigin = m_cylinderEnd1 + 1.5f * m_cylinderHeight * m_cylinderDir + 0.5f * m_cylinderRadius * m_radiusDir;
  586. Vector3 rayDir = m_cylinderDir;
  587. rayDir.Normalize();
  588. float t1 = AZ::Constants::FloatMax;
  589. float t2 = AZ::Constants::FloatMax;
  590. int hits = Intersect::IntersectRayCappedCylinder(rayOrigin, rayDir, m_cylinderEnd1, m_cylinderDir, m_cylinderHeight, m_cylinderRadius, t1, t2);
  591. EXPECT_EQ(hits, 0);
  592. }
  593. TEST_F(MATH_IntersectRayCappedCylinderTest, RayOriginOutsideEnds_BothIntersectionsInbetweenEnds)
  594. {
  595. Vector3 intersection2 = m_cylinderEnd1 + 0.8f * m_cylinderHeight * m_cylinderDir - m_cylinderRadius * m_radiusDir;
  596. Vector3 intersection1 = m_cylinderEnd1 + 0.2f * m_cylinderHeight * m_cylinderDir + m_cylinderRadius * m_radiusDir;
  597. Vector3 rayDir = intersection2 - intersection1;
  598. Vector3 rayOrigin = intersection1 - 2.0f * rayDir;
  599. rayDir.Normalize();
  600. float t1 = AZ::Constants::FloatMax;
  601. float t2 = AZ::Constants::FloatMax;
  602. int hits = Intersect::IntersectRayCappedCylinder(rayOrigin, rayDir, m_cylinderEnd1, m_cylinderDir, m_cylinderHeight, m_cylinderRadius, t1, t2);
  603. EXPECT_EQ(hits, 2);
  604. }
  605. TEST_F(MATH_IntersectRayCappedCylinderTest, RayOriginOutsideEnds_Intersection1OnEnd1_Intersection2InbetweenEnds)
  606. {
  607. Vector3 intersection2 = m_cylinderEnd1 + 0.6f * m_cylinderHeight * m_cylinderDir - m_cylinderRadius * m_radiusDir;
  608. Vector3 intersection1 = m_cylinderEnd1 + 0.6f * m_cylinderRadius * m_radiusDir;
  609. Vector3 rayDir = intersection2 - intersection1;
  610. Vector3 rayOrigin = intersection1 - 2.0f * rayDir;
  611. rayDir.Normalize();
  612. float t1 = AZ::Constants::FloatMax;
  613. float t2 = AZ::Constants::FloatMax;
  614. int hits = Intersect::IntersectRayCappedCylinder(rayOrigin, rayDir, m_cylinderEnd1, m_cylinderDir, m_cylinderHeight, m_cylinderRadius, t1, t2);
  615. EXPECT_EQ(hits, 2);
  616. }
  617. TEST_F(MATH_IntersectRayCappedCylinderTest, RayOriginInside_RayGoThroughCylinder)
  618. {
  619. Vector3 rayOrigin = m_cylinderEnd1 + 0.3f * m_cylinderHeight * m_cylinderDir - 0.4f * m_cylinderRadius * m_radiusDir;
  620. Vector3 intersection = m_cylinderEnd1 + 0.8f * m_cylinderHeight * m_cylinderDir + m_cylinderRadius * m_radiusDir;
  621. Vector3 rayDir = (intersection - rayOrigin).GetNormalized();
  622. float t1 = AZ::Constants::FloatMax;
  623. float t2 = AZ::Constants::FloatMax;
  624. int hits = Intersect::IntersectRayCappedCylinder(rayOrigin, rayDir, m_cylinderEnd1, m_cylinderDir, m_cylinderHeight, m_cylinderRadius, t1, t2);
  625. EXPECT_EQ(hits, 1);
  626. }
  627. TEST_F(MATH_IntersectRayCappedCylinderTest, RayOriginOutsideCylinderButInBetweenEnds_RayDirPointingAwayCylinder)
  628. {
  629. Vector3 rayOrigin = m_cylinderEnd1 + 0.4f * m_cylinderHeight * m_cylinderDir - 1.6f * m_cylinderRadius * m_radiusDir;
  630. Vector3 rayDir = rayOrigin - m_cylinderEnd1;
  631. rayDir.Normalize();
  632. float t1 = AZ::Constants::FloatMax;
  633. float t2 = AZ::Constants::FloatMax;
  634. int hits = Intersect::IntersectRayCappedCylinder(rayOrigin, rayDir, m_cylinderEnd1, m_cylinderDir, m_cylinderHeight, m_cylinderRadius, t1, t2);
  635. EXPECT_EQ(hits, 0);
  636. }
  637. TEST_F(MATH_IntersectRayCappedCylinderTest, RayOriginOutsideCylinderButInBetweenEnds_RayDirMissingCylinderEnds)
  638. {
  639. Vector3 rayOrigin = m_cylinderEnd1 + 0.4f * m_cylinderHeight * m_cylinderDir - 1.6f * m_cylinderRadius * m_radiusDir;
  640. Vector3 rayDir = (m_cylinderEnd1 + m_cylinderHeight * m_cylinderDir - 1.2f * m_cylinderRadius * m_radiusDir) - rayOrigin;
  641. rayDir.Normalize();
  642. float t1 = AZ::Constants::FloatMax;
  643. float t2 = AZ::Constants::FloatMax;
  644. int hits = Intersect::IntersectRayCappedCylinder(rayOrigin, rayDir, m_cylinderEnd1, m_cylinderDir, m_cylinderHeight, m_cylinderRadius, t1, t2);
  645. EXPECT_EQ(hits, 0);
  646. }
  647. TEST_F(MATH_IntersectRayCappedCylinderTest, RayOriginOutsideCylinderButInBetweenEnds_RayDirShootingOutEnd1)
  648. {
  649. Vector3 rayOrigin = m_cylinderEnd1 + 0.4f * m_cylinderHeight * m_cylinderDir - 1.6f * m_cylinderRadius * m_radiusDir;
  650. Vector3 rayDir = (m_cylinderEnd1 - 0.2f * m_cylinderRadius * m_radiusDir) - rayOrigin;
  651. rayDir.Normalize();
  652. float t1 = AZ::Constants::FloatMax;
  653. float t2 = AZ::Constants::FloatMax;
  654. int hits = Intersect::IntersectRayCappedCylinder(rayOrigin, rayDir, m_cylinderEnd1, m_cylinderDir, m_cylinderHeight, m_cylinderRadius, t1, t2);
  655. EXPECT_EQ(hits, 2);
  656. }
  657. TEST_F(MATH_IntersectRayCappedCylinderTest, RayOriginOutsideCylinderButInBetweenEnds_RayDirShootingOutEnd2)
  658. {
  659. Vector3 rayOrigin = m_cylinderEnd1 + 0.4f * m_cylinderHeight * m_cylinderDir - 1.6f * m_cylinderRadius * m_radiusDir;
  660. Vector3 rayDir = (m_cylinderEnd1 + 0.67f * m_cylinderHeight * m_cylinderDir) - rayOrigin;
  661. rayDir.Normalize();
  662. float t1 = AZ::Constants::FloatMax;
  663. float t2 = AZ::Constants::FloatMax;
  664. int hits = Intersect::IntersectRayCappedCylinder(rayOrigin, rayDir, m_cylinderEnd1, m_cylinderDir, m_cylinderHeight, m_cylinderRadius, t1, t2);
  665. EXPECT_EQ(hits, 2);
  666. }
  667. TEST_F(MATH_IntersectRayCappedCylinderTest, RayOriginOutsideCylinderButInBetweenEnds_RayDirShootingOutCylinder)
  668. {
  669. Vector3 perpDir(1.0f, 0, 0);
  670. float parallelDiff = 0.453f;
  671. float perpendicularDiff = 2.54f;
  672. Vector3 rayOrigin = (m_cylinderEnd1 + parallelDiff * m_cylinderHeight * m_cylinderDir) + perpendicularDiff * m_cylinderRadius * perpDir;
  673. Vector3 rayDir = (m_cylinderEnd1 + m_cylinderHeight * m_cylinderDir - 2.6f * m_cylinderRadius * perpDir) - rayOrigin;
  674. rayDir.Normalize();
  675. float t1 = AZ::Constants::FloatMax;
  676. float t2 = AZ::Constants::FloatMax;
  677. int hits = Intersect::IntersectRayCappedCylinder(rayOrigin, rayDir, m_cylinderEnd1, m_cylinderDir, m_cylinderHeight, m_cylinderRadius, t1, t2);
  678. EXPECT_EQ(hits, 2);
  679. }
  680. class MATH_IntersectRayConeTest
  681. : public LeakDetectionFixture
  682. {
  683. protected:
  684. void SetUp() override
  685. {
  686. m_coneApex = Vector3(1.264f, 6.773f, 9.612f);
  687. m_coneDir = Vector3(-64.0f, -82.783f, -12.97f);
  688. m_radiusDir = Vector3(m_coneDir.GetY(), -m_coneDir.GetX(), 0.0f);
  689. m_coneDir.Normalize();
  690. m_coneHeight = 1.643f;
  691. m_coneRadius = m_coneHeight;
  692. m_radiusDir.Normalize();
  693. m_tangentDir = m_coneHeight * m_coneDir + m_coneRadius * m_radiusDir;
  694. m_tangentDir.Normalize();
  695. }
  696. void TearDown() override
  697. {
  698. }
  699. Vector3 m_coneApex;
  700. Vector3 m_coneDir;
  701. Vector3 m_radiusDir;
  702. Vector3 m_tangentDir; // direction of tangent line on the surface from the apex to the base
  703. float m_coneHeight;
  704. float m_coneRadius;
  705. };
  706. TEST_F(MATH_IntersectRayConeTest, RayOriginAtApex_RayDirParallelConeSurface)
  707. {
  708. Vector3 rayOrigin = m_coneApex;
  709. Vector3 rayDir = m_tangentDir;
  710. float t1 = 0.0f;
  711. float t2 = 0.0f;
  712. int hits = Intersect::IntersectRayCone(rayOrigin, rayDir, m_coneApex, m_coneDir, m_coneHeight, m_coneRadius, t1, t2);
  713. EXPECT_EQ(hits, 2);
  714. }
  715. TEST_F(MATH_IntersectRayConeTest, RayOriginOutsideConeBelowApex_RayDirParallelConeSurface)
  716. {
  717. Vector3 rayOrigin = m_coneApex + 0.5f * m_coneHeight * m_coneDir - m_coneRadius * m_radiusDir;
  718. Vector3 rayDir = m_tangentDir;
  719. float t1 = 0.0f;
  720. float t2 = 0.0f;
  721. int hits = Intersect::IntersectRayCone(rayOrigin, rayDir, m_coneApex, m_coneDir, m_coneHeight, m_coneRadius, t1, t2);
  722. EXPECT_EQ(hits, 2);
  723. }
  724. TEST_F(MATH_IntersectRayConeTest, RayOriginOutsideConeBelowBase_RayDirParallelConeSurface)
  725. {
  726. Vector3 rayOrigin = m_coneApex + 1.5f * m_coneHeight * m_coneDir - m_coneRadius * m_radiusDir;
  727. Vector3 rayDir = m_tangentDir;
  728. float t1 = 0.0f;
  729. float t2 = 0.0f;
  730. int hits = Intersect::IntersectRayCone(rayOrigin, rayDir, m_coneApex, m_coneDir, m_coneHeight, m_coneRadius, t1, t2);
  731. EXPECT_EQ(hits, 0);
  732. }
  733. TEST_F(MATH_IntersectRayConeTest, RayOriginOutsideConeAboveApex_RayDirParallelConeSurface)
  734. {
  735. Vector3 rayOrigin = m_coneApex - 1.5f * m_coneHeight * m_coneDir - m_coneRadius * m_radiusDir;
  736. Vector3 rayDir = m_tangentDir;
  737. float t1 = 0.0f;
  738. float t2 = 0.0f;
  739. int hits = Intersect::IntersectRayCone(rayOrigin, rayDir, m_coneApex, m_coneDir, m_coneHeight, m_coneRadius, t1, t2);
  740. EXPECT_EQ(hits, 0);
  741. }
  742. TEST_F(MATH_IntersectRayConeTest, RayOriginInsideCone_RayDirParallelConeSurface)
  743. {
  744. Vector3 rayOrigin = m_coneApex + 0.5f * m_coneHeight * m_coneDir;
  745. Vector3 rayDir = m_tangentDir;
  746. float t1 = 0.0f;
  747. float t2 = 0.0f;
  748. int hits = Intersect::IntersectRayCone(rayOrigin, rayDir, m_coneApex, m_coneDir, m_coneHeight, m_coneRadius, t1, t2);
  749. EXPECT_EQ(hits, 1);
  750. }
  751. TEST_F(MATH_IntersectRayConeTest, RayOriginInsideCone_RayDirParallelConeSurfaceOppositeDirection)
  752. {
  753. Vector3 rayOrigin = m_coneApex + 0.5f * m_coneHeight * m_coneDir;
  754. Vector3 rayDir = -m_tangentDir;
  755. float t1 = 0.0f;
  756. float t2 = 0.0f;
  757. int hits = Intersect::IntersectRayCone(rayOrigin, rayDir, m_coneApex, m_coneDir, m_coneHeight, m_coneRadius, t1, t2);
  758. EXPECT_EQ(hits, 1);
  759. }
  760. TEST_F(MATH_IntersectRayConeTest, RayOriginOutsideCone_RayDirThroughApex)
  761. {
  762. Vector3 rayOrigin = m_coneApex + 0.03f * m_coneDir + m_coneRadius * m_radiusDir;
  763. Vector3 rayDir = (m_coneApex - rayOrigin).GetNormalized();
  764. float t1 = 0.0f;
  765. float t2 = 0.0f;
  766. int hits = Intersect::IntersectRayCone(rayOrigin, rayDir, m_coneApex, m_coneDir, m_coneHeight, m_coneRadius, t1, t2);
  767. EXPECT_EQ(hits, 1);
  768. }
  769. TEST_F(MATH_IntersectRayConeTest, RayOriginInsideMirrorCone_RayDirThroughApex)
  770. {
  771. Vector3 rayOrigin = m_coneApex - 0.5f * m_coneHeight * m_coneDir + 0.25f * m_coneRadius * m_radiusDir;
  772. Vector3 rayDir = (m_coneApex - rayOrigin).GetNormalized();
  773. float t1 = 0.0f;
  774. float t2 = 0.0f;
  775. int hits = Intersect::IntersectRayCone(rayOrigin, rayDir, m_coneApex, m_coneDir, m_coneHeight, m_coneRadius, t1, t2);
  776. EXPECT_EQ(hits, 2);
  777. }
  778. TEST_F(MATH_IntersectRayConeTest, RayOriginOutsideBase_RayDirThroughApex)
  779. {
  780. Vector3 rayOrigin = m_coneApex + 1.3f * m_coneHeight * m_coneDir + 0.3f * m_coneRadius * m_radiusDir;
  781. Vector3 rayDir = (m_coneApex - rayOrigin).GetNormalized();
  782. float t1 = 0.0f;
  783. float t2 = 0.0f;
  784. int hits = Intersect::IntersectRayCone(rayOrigin, rayDir, m_coneApex, m_coneDir, m_coneHeight, m_coneRadius, t1, t2);
  785. EXPECT_EQ(hits, 2);
  786. }
  787. TEST_F(MATH_IntersectRayConeTest, RayOriginOutsideBase_RayDirThroughConeSurface)
  788. {
  789. Vector3 rayOrigin = m_coneApex + 1.3f * m_coneHeight * m_coneDir + 0.3f * m_coneRadius * m_radiusDir;
  790. Vector3 rayDir = (m_coneApex - 0.3f * m_coneRadius * m_radiusDir - rayOrigin).GetNormalized();
  791. float t1 = 0.0f;
  792. float t2 = 0.0f;
  793. int hits = Intersect::IntersectRayCone(rayOrigin, rayDir, m_coneApex, m_coneDir, m_coneHeight, m_coneRadius, t1, t2);
  794. EXPECT_EQ(hits, 2);
  795. }
  796. TEST_F(MATH_IntersectRayConeTest, RayOriginOutsideConeApexSide_RayDirThroughConeSurface)
  797. {
  798. Vector3 rayOrigin = m_coneApex + 1.5f * m_coneHeight * m_coneDir + 0.7f * m_coneRadius * m_radiusDir;
  799. Vector3 rayDir = (m_coneApex + 0.7f * m_coneHeight * m_coneDir - 0.8f * m_coneRadius * m_radiusDir - rayOrigin).GetNormalized();
  800. float t1 = 0.0f;
  801. float t2 = 0.0f;
  802. int hits = Intersect::IntersectRayCone(rayOrigin, rayDir, m_coneApex, m_coneDir, m_coneHeight, m_coneRadius, t1, t2);
  803. EXPECT_EQ(hits, 2);
  804. }
  805. TEST_F(MATH_IntersectRayConeTest, RayOriginOutsideConeApexSide_RayDirMissCone)
  806. {
  807. Vector3 rayOrigin = m_coneApex - 0.5f * m_coneHeight * m_coneDir + 0.7f * m_coneRadius * m_radiusDir;
  808. Vector3 rayDir = (m_coneApex + 1.1f * m_coneHeight * m_coneDir + 1.1f * m_coneRadius * m_radiusDir - rayOrigin).GetNormalized();
  809. float t1 = 0.0f;
  810. float t2 = 0.0f;
  811. int hits = Intersect::IntersectRayCone(rayOrigin, rayDir, m_coneApex, m_coneDir, m_coneHeight, m_coneRadius, t1, t2);
  812. EXPECT_EQ(hits, 0);
  813. }
  814. TEST_F(MATH_IntersectRayConeTest, RayOriginInsideMirrorConeApexSide_RayDirMissCone)
  815. {
  816. Vector3 rayOrigin = m_coneApex - 0.7f * m_coneHeight * m_coneDir + 0.6f * m_coneRadius * m_radiusDir;
  817. Vector3 rayDir = (m_coneApex + 1.5f * m_coneHeight * m_coneDir + 1.5f * m_coneRadius * m_radiusDir - rayOrigin).GetNormalized();
  818. float t1 = 0.0f;
  819. float t2 = 0.0f;
  820. int hits = Intersect::IntersectRayCone(rayOrigin, rayDir, m_coneApex, m_coneDir, m_coneHeight, m_coneRadius, t1, t2);
  821. EXPECT_EQ(hits, 0);
  822. }
  823. // replicates a scenario in the Editor using a cone and a pick ray which should have failed but passed
  824. // note: To replicate this, select an entity so the default translation manipulator appears, move very close to the
  825. // entity, hover the mouse over one of the manipulator linear manipulator arrows (cone part) and move the mouse away
  826. // notice the manipulator will remain highlighted as a successful intersection is still reported
  827. TEST(MATH_IntersectRayConeTestEditor, DISABLED_RayConeEditorScenarioTest)
  828. {
  829. auto rayOrigin = Vector3(0.0f, -0.808944702f, 0.0f);
  830. auto rayDir = Vector3(0.301363617f, 0.939044654f, 0.165454566f);
  831. float t1 = 0.0f;
  832. float t2 = 0.0f;
  833. int hits = Intersect::IntersectRayCone(
  834. rayOrigin, rayDir, AZ::Vector3(0.0f, 0.0f, 0.161788940f), AZ::Vector3(0.0f, 0.0f, -1.0f), 0.0453009047f, 0.0113252262f, t1, t2);
  835. EXPECT_EQ(hits, 0);
  836. }
  837. class MATH_IntersectRayQuadTest
  838. : public LeakDetectionFixture
  839. {
  840. protected:
  841. void SetUp() override
  842. {
  843. m_vertexA = Vector3(1.04f, 2.46f, 5.26f);
  844. m_axisB = Vector3(1.0f, 2.0f, 3.0f);
  845. m_axisD = Vector3(3.0f, -2.0f, 1.0f);
  846. m_axisB.Normalize();
  847. m_axisD.Normalize();
  848. m_lengthAxisB = 4.56f;
  849. m_lengthAxisD = 7.19f;
  850. m_normal = m_axisB.Cross(m_axisD);
  851. m_normal.Normalize();
  852. m_vertexB = m_vertexA + m_lengthAxisB * m_axisB;
  853. m_vertexC = m_vertexA + m_lengthAxisB * m_axisB + m_lengthAxisD * m_axisD;
  854. m_vertexD = m_vertexA + m_lengthAxisD * m_axisD;
  855. }
  856. void TearDown() override
  857. {
  858. }
  859. Vector3 m_vertexA;
  860. Vector3 m_vertexB;
  861. Vector3 m_vertexC;
  862. Vector3 m_vertexD;
  863. // two axes defining the quad plane, originating from m_vertexA
  864. Vector3 m_axisB;
  865. Vector3 m_axisD;
  866. float m_lengthAxisB;
  867. float m_lengthAxisD;
  868. Vector3 m_normal;
  869. };
  870. TEST_F(MATH_IntersectRayQuadTest, RayShootingAway_CCW)
  871. {
  872. Vector3 rayOrigin = (m_vertexA + 0.23f * m_axisB + 0.75f * m_axisD) + 2.0f * m_normal;
  873. Vector3 rayDir = m_normal;
  874. rayDir.Normalize();
  875. float t = 0.0f;
  876. int hit = Intersect::IntersectRayQuad(rayOrigin, rayDir, m_vertexA, m_vertexB, m_vertexC, m_vertexD, t);
  877. EXPECT_EQ(hit, 0);
  878. }
  879. TEST_F(MATH_IntersectRayQuadTest, RayShootingAway_CW)
  880. {
  881. Vector3 rayOrigin = (m_vertexA + 0.23f * m_axisB + 0.75f * m_axisD) + 2.0f * m_normal;
  882. Vector3 rayDir = m_normal;
  883. rayDir.Normalize();
  884. float t = 0.0f;
  885. int hit = Intersect::IntersectRayQuad(rayOrigin, rayDir, m_vertexC, m_vertexB, m_vertexA, m_vertexD, t);
  886. EXPECT_EQ(hit, 0);
  887. }
  888. TEST_F(MATH_IntersectRayQuadTest, RayIntersectTriangleABC_CCW)
  889. {
  890. Vector3 rayOrigin = m_vertexA + 2.0f * m_normal;
  891. Vector3 rayDir = ((0.5f * (0.5f * m_vertexA + 0.5f * m_vertexC)) + 0.5f * m_vertexB) - rayOrigin;
  892. rayDir.Normalize();
  893. float t = 0.0f;
  894. int hit = Intersect::IntersectRayQuad(rayOrigin, rayDir, m_vertexA, m_vertexB, m_vertexC, m_vertexD, t);
  895. EXPECT_EQ(hit, 1);
  896. }
  897. TEST_F(MATH_IntersectRayQuadTest, RayIntersectTriangleABC_CW)
  898. {
  899. Vector3 rayOrigin = m_vertexA + 2.0f * m_normal;
  900. Vector3 rayDir = ((0.5f * (0.5f * m_vertexA + 0.5f * m_vertexC)) + 0.5f * m_vertexB) - rayOrigin;
  901. rayDir.Normalize();
  902. float t = 0.0f;
  903. int hit = Intersect::IntersectRayQuad(rayOrigin, rayDir, m_vertexD, m_vertexC, m_vertexB, m_vertexA, t);
  904. EXPECT_EQ(hit, 1);
  905. }
  906. TEST_F(MATH_IntersectRayQuadTest, RayIntersectTriangleACD_CCW)
  907. {
  908. Vector3 rayOrigin = m_vertexA + 2.0f * m_normal;
  909. Vector3 rayDir = ((0.5f * (0.5f * m_vertexA + 0.5f * m_vertexC)) + 0.5f * m_vertexD) - rayOrigin;
  910. rayDir.Normalize();
  911. float t = 0.0f;
  912. int hit = Intersect::IntersectRayQuad(rayOrigin, rayDir, m_vertexA, m_vertexB, m_vertexC, m_vertexD, t);
  913. EXPECT_EQ(hit, 1);
  914. }
  915. TEST_F(MATH_IntersectRayQuadTest, RayIntersectTriangleACD_CW)
  916. {
  917. Vector3 rayOrigin = m_vertexA + 2.0f * m_normal;
  918. Vector3 rayDir = ((0.5f * (0.5f * m_vertexA + 0.5f * m_vertexC)) + 0.5f * m_vertexD) - rayOrigin;
  919. rayDir.Normalize();
  920. float t = 0.0f;
  921. int hit = Intersect::IntersectRayQuad(rayOrigin, rayDir, m_vertexA, m_vertexD, m_vertexC, m_vertexB, t);
  922. EXPECT_EQ(hit, 1);
  923. }
  924. TEST_F(MATH_IntersectRayQuadTest, RayIntersectLineAC_CCW)
  925. {
  926. Vector3 rayOrigin = m_vertexA + 2.0f * m_normal;
  927. Vector3 rayDir = (0.5f * m_vertexA + 0.5f * m_vertexC) - rayOrigin;
  928. rayDir.Normalize();
  929. float t = 0.0f;
  930. int hit = Intersect::IntersectRayQuad(rayOrigin, rayDir, m_vertexA, m_vertexB, m_vertexC, m_vertexD, t);
  931. EXPECT_EQ(hit, 1);
  932. }
  933. TEST_F(MATH_IntersectRayQuadTest, RayShootOverAB_CCW)
  934. {
  935. Vector3 rayOrigin = m_vertexA + 2.0f * m_normal;
  936. Vector3 rayDir = m_vertexA + 1.7f * m_lengthAxisB * m_axisB - rayOrigin;
  937. rayDir.Normalize();
  938. float t = 0.0f;
  939. int hit = Intersect::IntersectRayQuad(rayOrigin, rayDir, m_vertexA, m_vertexB, m_vertexC, m_vertexD, t);
  940. EXPECT_EQ(hit, 0);
  941. }
  942. TEST_F(MATH_IntersectRayQuadTest, RayShootOverAC_CW)
  943. {
  944. Vector3 rayOrigin = m_vertexA + 2.0f * m_normal;
  945. Vector3 rayDir = m_vertexA + 1.3f * (m_vertexD - m_vertexA) - rayOrigin;
  946. rayDir.Normalize();
  947. float t = 0.0f;
  948. int hit = Intersect::IntersectRayQuad(rayOrigin, rayDir, m_vertexA, m_vertexD, m_vertexC, m_vertexB, t);
  949. EXPECT_EQ(hit, 0);
  950. }
  951. TEST(MATH_Intersection, RaySmallQuad)
  952. {
  953. Vector3 rayOrigin = AZ::Vector3::CreateAxisX(-10.0f);
  954. Vector3 rayDir = AZ::Vector3::CreateAxisX();
  955. Vector3 vertexA(0.0f, -0.001f, 0.001f);
  956. Vector3 vertexB(0.0f, 0.001f, 0.001f);
  957. Vector3 vertexC(0.0f, 0.001f, -0.001f);
  958. Vector3 vertexD(0.0f, -0.001f, -0.001f);
  959. float t = 0.0f;
  960. int hit = Intersect::IntersectRayQuad(rayOrigin, rayDir, vertexA, vertexB, vertexC, vertexD, t);
  961. EXPECT_EQ(hit, 1);
  962. }
  963. class MATH_IntersectRayBoxTest
  964. : public LeakDetectionFixture
  965. {
  966. protected:
  967. void SetUp() override
  968. {
  969. m_boxCenter = Vector3(1.234f, 2.345f, 9.824f);
  970. m_boxAxis1 = Vector3(1.0f, 2.0f, 3.0f);
  971. m_boxAxis2 = Vector3(-3.0f, 2.0f, -1.0f);
  972. m_boxAxis3 = m_boxAxis1.Cross(m_boxAxis2);
  973. m_boxAxis1.Normalize();
  974. m_boxAxis2.Normalize();
  975. m_boxAxis3.Normalize();
  976. m_boxHalfExtentAxis1 = 4.775f;
  977. m_boxHalfExtentAxis2 = 8.035f;
  978. m_boxHalfExtentAxis3 = 14.007f;
  979. }
  980. void TearDown() override
  981. {
  982. }
  983. Vector3 m_boxCenter;
  984. Vector3 m_boxAxis1;
  985. Vector3 m_boxAxis2;
  986. Vector3 m_boxAxis3;
  987. float m_boxHalfExtentAxis1;
  988. float m_boxHalfExtentAxis2;
  989. float m_boxHalfExtentAxis3;
  990. };
  991. TEST_F(MATH_IntersectRayBoxTest, RayOriginOutside_HitBoxAxis1Side)
  992. {
  993. Vector3 rayOrigin = m_boxCenter + 2.0f * m_boxHalfExtentAxis1 * m_boxAxis1 + 2.0f * m_boxHalfExtentAxis2 * m_boxAxis2;
  994. Vector3 rayDir = m_boxCenter + m_boxHalfExtentAxis1 * m_boxAxis1 - rayOrigin;
  995. rayDir.Normalize();
  996. float t = 0.0f;
  997. int hit = Intersect::IntersectRayBox(rayOrigin, rayDir, m_boxCenter, m_boxAxis1, m_boxAxis2, m_boxAxis3,
  998. m_boxHalfExtentAxis1, m_boxHalfExtentAxis2, m_boxHalfExtentAxis3, t);
  999. EXPECT_EQ(hit, 1);
  1000. }
  1001. TEST_F(MATH_IntersectRayBoxTest, RayOriginOutside_ShootAwayFromBox)
  1002. {
  1003. Vector3 rayOrigin = m_boxCenter + 2.0f * m_boxHalfExtentAxis3 * m_boxAxis3 + 2.0f * m_boxHalfExtentAxis2 * m_boxAxis2;
  1004. Vector3 rayDir = rayOrigin - m_boxCenter + m_boxHalfExtentAxis3 * m_boxAxis3;
  1005. rayDir.Normalize();
  1006. float t = 0.0f;
  1007. int hit = Intersect::IntersectRayBox(rayOrigin, rayDir, m_boxCenter, m_boxAxis1, m_boxAxis2, m_boxAxis3,
  1008. m_boxHalfExtentAxis1, m_boxHalfExtentAxis2, m_boxHalfExtentAxis3, t);
  1009. EXPECT_EQ(hit, 0);
  1010. }
  1011. TEST_F(MATH_IntersectRayBoxTest, RayOriginOutside_RayParallelToAxis2MissBox)
  1012. {
  1013. Vector3 rayOrigin = m_boxCenter + 2.0f * m_boxHalfExtentAxis3 * m_boxAxis3 + 2.0f * m_boxHalfExtentAxis2 * m_boxAxis2;
  1014. Vector3 rayDir = m_boxAxis2;
  1015. float t = 0.0f;
  1016. int hit = Intersect::IntersectRayBox(rayOrigin, rayDir, m_boxCenter, m_boxAxis1, m_boxAxis2, m_boxAxis3,
  1017. m_boxHalfExtentAxis1, m_boxHalfExtentAxis2, m_boxHalfExtentAxis3, t);
  1018. EXPECT_EQ(hit, 0);
  1019. }
  1020. TEST_F(MATH_IntersectRayBoxTest, RayOriginInside_ShootToConner)
  1021. {
  1022. Vector3 rayOrigin = m_boxCenter + 0.5f * m_boxHalfExtentAxis1 * m_boxAxis1 +
  1023. 0.5f * m_boxHalfExtentAxis2 * m_boxAxis2 + 0.5f * m_boxHalfExtentAxis3 * m_boxAxis3;
  1024. Vector3 rayDir = (m_boxCenter - m_boxHalfExtentAxis1 * m_boxAxis1 -
  1025. m_boxHalfExtentAxis2 * m_boxAxis2 - m_boxHalfExtentAxis3 * m_boxAxis3) - rayOrigin;
  1026. rayDir.Normalize();
  1027. float t = 0.0f;
  1028. int hit = Intersect::IntersectRayBox(rayOrigin, rayDir, m_boxCenter, m_boxAxis1, m_boxAxis2, m_boxAxis3,
  1029. m_boxHalfExtentAxis1, m_boxHalfExtentAxis2, m_boxHalfExtentAxis3, t);
  1030. EXPECT_EQ(hit, 1);
  1031. }
  1032. class MATH_IntersectRayPolyhedronTest
  1033. : public LeakDetectionFixture
  1034. {
  1035. protected:
  1036. void SetUp() override
  1037. {
  1038. // base of cube
  1039. m_vertices[0] = Vector3(0.0f, 0.0f, 0.0f);
  1040. m_vertices[1] = Vector3(10.0f, 0.0f, 0.0f);
  1041. m_vertices[2] = Vector3(10.0f, 10.0f, 0.0f);
  1042. m_vertices[3] = Vector3(0.0f, 10.0f, 0.0f);
  1043. // setup planes
  1044. for (size_t i = 0; i < 4; ++i)
  1045. {
  1046. const Vector3 start = m_vertices[i];
  1047. const Vector3 end = m_vertices[(i + 1) % 4];
  1048. const Vector3 top = start + Vector3::CreateAxisZ();
  1049. const Vector3 normal = (end - start).Cross(top - start).GetNormalizedSafe();
  1050. m_planes[i] = Plane::CreateFromNormalAndPoint(normal, start);
  1051. }
  1052. const Vector3 normalTop =
  1053. (m_vertices[2] - m_vertices[0]).Cross(m_vertices[0] - m_vertices[1]).GetNormalizedSafe();
  1054. const Vector3 normalBottom = -normalTop;
  1055. const float height = 10.0f;
  1056. m_planes[4] = Plane::CreateFromNormalAndPoint(normalTop, m_vertices[0] + Vector3::CreateAxisZ(height));
  1057. m_planes[5] = Plane::CreateFromNormalAndPoint(normalBottom, m_vertices[0]);
  1058. }
  1059. void TearDown() override
  1060. {
  1061. }
  1062. Vector3 m_vertices[4];
  1063. Plane m_planes[6];
  1064. };
  1065. TEST_F(MATH_IntersectRayPolyhedronTest, RayParallelHit)
  1066. {
  1067. const Vector3 src = Vector3(0.0f, -1.0f, 1.0f);
  1068. const Vector3 dir = Vector3(0.0f, 1.0f, 0.0f);
  1069. const Vector3 end = (src + dir * 100.0f) - src;
  1070. float f, l;
  1071. int firstPlane, lastPlane;
  1072. const int intersections = Intersect::IntersectSegmentPolyhedron(src, end, m_planes, 6, f, l, firstPlane, lastPlane);
  1073. EXPECT_EQ(intersections, 1);
  1074. }
  1075. TEST_F(MATH_IntersectRayPolyhedronTest, RayAboveMiss)
  1076. {
  1077. const Vector3 src = Vector3(5.0f, 11.0f, 11.0f);
  1078. const Vector3 dir = Vector3(0.0f, -1.0f, 0.0f);
  1079. const Vector3 end = (src + dir * 100.0f) - src;
  1080. float f, l;
  1081. int firstPlane, lastPlane;
  1082. const int intersections = Intersect::IntersectSegmentPolyhedron(src, end, m_planes, 6, f, l, firstPlane, lastPlane);
  1083. EXPECT_EQ(intersections, 0);
  1084. }
  1085. TEST_F(MATH_IntersectRayPolyhedronTest, RayDiagonalDownHit)
  1086. {
  1087. const Vector3 src = Vector3(5.0f, -1.0f, 11.0f);
  1088. const Vector3 end = Vector3(5.0f, 11.0f, -11.0f);
  1089. float f, l;
  1090. int firstPlane, lastPlane;
  1091. const int intersections = Intersect::IntersectSegmentPolyhedron(src, end, m_planes, 6, f, l, firstPlane, lastPlane);
  1092. EXPECT_EQ(intersections, 1);
  1093. }
  1094. TEST_F(MATH_IntersectRayPolyhedronTest, RayDiagonalAcrossHit)
  1095. {
  1096. const Vector3 src = Vector3(-5.0f, -5.0f, 5.0f);
  1097. const Vector3 end = Vector3(15.0f, 15.0f, 5.0f);
  1098. float f, l;
  1099. int firstPlane, lastPlane;
  1100. const int intersections = Intersect::IntersectSegmentPolyhedron(src, end, m_planes, 6, f, l, firstPlane, lastPlane);
  1101. EXPECT_EQ(intersections, 1);
  1102. }
  1103. TEST_F(MATH_IntersectRayPolyhedronTest, RayDiagonalAcrossMiss)
  1104. {
  1105. const Vector3 src = Vector3(-5.0f, -15.0f, 5.0f);
  1106. const Vector3 end = Vector3(15.0f, 5.0f, 5.0f);
  1107. float f, l;
  1108. int firstPlane, lastPlane;
  1109. const int intersections = Intersect::IntersectSegmentPolyhedron(src, end, m_planes, 6, f, l, firstPlane, lastPlane);
  1110. EXPECT_EQ(intersections, 0);
  1111. }
  1112. TEST_F(MATH_IntersectRayPolyhedronTest, RayStartInside)
  1113. {
  1114. const Vector3 src = Vector3(5.0f, 5.0f, 5.0f);
  1115. const Vector3 end = Vector3(5.0f, 5.0f, 5.0f);
  1116. float f, l;
  1117. int firstPlane, lastPlane;
  1118. const int intersections = Intersect::IntersectSegmentPolyhedron(src, end, m_planes, 6, f, l, firstPlane, lastPlane);
  1119. EXPECT_EQ(intersections, 0);
  1120. }
  1121. class MATH_ClipRayWithAabbTest : public LeakDetectionFixture
  1122. {
  1123. };
  1124. TEST_F(MATH_ClipRayWithAabbTest, RayDoesNotGetClippedWhenMissingAabb)
  1125. {
  1126. // Test a ray that points straight down and completely misses a box.
  1127. const Aabb aabb = Aabb::CreateCenterRadius(AZ::Vector3::CreateZero(), 10.0f);
  1128. Vector3 rayStart(50.0f, 50.0f, 20.0f);
  1129. Vector3 rayEnd(50.0f, 50.0f, -20.0f);
  1130. float tClipStart = AZStd::numeric_limits<float>::max();
  1131. float tClipEnd = AZStd::numeric_limits<float>::max();
  1132. bool clipped = Intersect::ClipRayWithAabb(aabb, rayStart, rayEnd, tClipStart, tClipEnd);
  1133. // We expect the ray not to get clipped. There are no guarantees on the validity of any of the output values
  1134. // (rayStart, rayEnd, tClipStart, tClipEnd), so we won't validate them here.
  1135. EXPECT_FALSE(clipped);
  1136. }
  1137. TEST_F(MATH_ClipRayWithAabbTest, RayIsClippedWithAabb)
  1138. {
  1139. // Test a ray that starts 10 above a box, points straight down, and continues to 10 below the box.
  1140. // The z values should get clipped to the box.
  1141. const Aabb aabb = Aabb::CreateCenterRadius(AZ::Vector3::CreateZero(), 10.0f);
  1142. Vector3 rayStart(5.0f, 5.0f, 20.0f);
  1143. Vector3 rayEnd(5.0f, 5.0f, -20.0f);
  1144. float tClipStart = AZStd::numeric_limits<float>::max();
  1145. float tClipEnd = AZStd::numeric_limits<float>::max();
  1146. bool clipped = Intersect::ClipRayWithAabb(aabb, rayStart, rayEnd, tClipStart, tClipEnd);
  1147. // We expect the ray to get clipped.
  1148. EXPECT_TRUE(clipped);
  1149. EXPECT_THAT(rayStart, IsClose(AZ::Vector3(5.0f, 5.0f, 10.0f)));
  1150. EXPECT_THAT(rayEnd, IsClose(AZ::Vector3(5.0f, 5.0f, -10.0f)));
  1151. EXPECT_NEAR(tClipStart, 0.25f, 0.001f);
  1152. EXPECT_NEAR(tClipEnd, 0.75f, 0.001f);
  1153. }
  1154. TEST_F(MATH_ClipRayWithAabbTest, RayStartingInsideAabbGetsClipped)
  1155. {
  1156. // Test a ray that starts inside a box, points straight down, and continues to 10 below the box.
  1157. // The z values should get clipped to the box.
  1158. const Aabb aabb = Aabb::CreateCenterRadius(AZ::Vector3::CreateZero(), 10.0f);
  1159. Vector3 rayStart(5.0f, 5.0f, 0.0f);
  1160. Vector3 rayEnd(5.0f, 5.0f, -20.0f);
  1161. float tClipStart = AZStd::numeric_limits<float>::max();
  1162. float tClipEnd = AZStd::numeric_limits<float>::max();
  1163. bool clipped = Intersect::ClipRayWithAabb(aabb, rayStart, rayEnd, tClipStart, tClipEnd);
  1164. // We expect the ray to get clipped.
  1165. EXPECT_TRUE(clipped);
  1166. EXPECT_THAT(rayStart, IsClose(AZ::Vector3(5.0f, 5.0f, 0.0f)));
  1167. EXPECT_THAT(rayEnd, IsClose(AZ::Vector3(5.0f, 5.0f, -10.0f)));
  1168. EXPECT_NEAR(tClipStart, 0.0f, 0.001f);
  1169. EXPECT_NEAR(tClipEnd, 0.5f, 0.001f);
  1170. }
  1171. TEST_F(MATH_ClipRayWithAabbTest, RayEndingInsideAabbGetsClipped)
  1172. {
  1173. // Test a ray that starts 10 above a box, points straight down, and ends inside the box.
  1174. // The z values should get clipped to the box.
  1175. const Aabb aabb = Aabb::CreateCenterRadius(AZ::Vector3::CreateZero(), 10.0f);
  1176. Vector3 rayStart(5.0f, 5.0f, 20.0f);
  1177. Vector3 rayEnd(5.0f, 5.0f, 0.0f);
  1178. float tClipStart = AZStd::numeric_limits<float>::max();
  1179. float tClipEnd = AZStd::numeric_limits<float>::max();
  1180. bool clipped = Intersect::ClipRayWithAabb(aabb, rayStart, rayEnd, tClipStart, tClipEnd);
  1181. // We expect the ray to get clipped.
  1182. EXPECT_TRUE(clipped);
  1183. EXPECT_THAT(rayStart, IsClose(AZ::Vector3(5.0f, 5.0f, 10.0f)));
  1184. EXPECT_THAT(rayEnd, IsClose(AZ::Vector3(5.0f, 5.0f, 0.0f)));
  1185. EXPECT_NEAR(tClipStart, 0.5f, 0.001f);
  1186. EXPECT_NEAR(tClipEnd, 1.0f, 0.001f);
  1187. }
  1188. } // namespace UnitTest