IntersectPointTest.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/IntersectPoint.h>
  9. #include <AzCore/UnitTest/TestTypes.h>
  10. #include <AZTestShared/Math/MathTestHelpers.h>
  11. using namespace AZ;
  12. namespace UnitTest::IntersectTest
  13. {
  14. // Tests a point inside sphere
  15. TEST(MATH_IntersectPointSphere, TestPointInside)
  16. {
  17. Vector3 sphereCenter(1.f, 2.f, 3.f);
  18. float sphereRadius = 5.f;
  19. Vector3 testPoint(2.f, 3.f, 4.f);
  20. EXPECT_TRUE(Intersect::PointSphere(sphereCenter, sphereRadius * sphereRadius, testPoint));
  21. }
  22. // Tests a point outside sphere
  23. TEST(MATH_IntersectPointSphere, TestPointOutside)
  24. {
  25. Vector3 sphereCenter(1.f, 2.f, 3.f);
  26. float sphereRadius = 5.f;
  27. Vector3 testPoint(10.f, 10.f, 10.f);
  28. EXPECT_FALSE(Intersect::PointSphere(sphereCenter, sphereRadius * sphereRadius, testPoint));
  29. }
  30. // Tests a point just inside the border of the sphere
  31. TEST(MATH_IntersectPointSphere, TestPointInsideBorder)
  32. {
  33. Vector3 sphereCenter(1.f, 2.f, 3.f);
  34. float sphereRadius = 5.f;
  35. Vector3 testPoint(5.99f, 2.f, 3.f);
  36. EXPECT_TRUE(Intersect::PointSphere(sphereCenter, sphereRadius * sphereRadius, testPoint));
  37. }
  38. // Tests a point just outside the border of the sphere
  39. TEST(MATH_IntersectPointSphere, TestPointOutsideBorder)
  40. {
  41. Vector3 sphereCenter(1.f, 2.f, 3.f);
  42. float sphereRadius = 5.f;
  43. Vector3 testPoint(6.01f, 2.f, 3.f);
  44. EXPECT_FALSE(Intersect::PointSphere(sphereCenter, sphereRadius * sphereRadius, testPoint));
  45. }
  46. // Tests a point inside cylinder
  47. TEST(MATH_IntersectPointCylinder, TestPointInside)
  48. {
  49. Vector3 cylinderBase(1.f, 2.f, 3.f);
  50. Vector3 cylinderAxis(0.f, 5.f, 0.f);
  51. float cylinderRadius = 1.f;
  52. float cylinderLength = cylinderAxis.GetLength();
  53. Vector3 testPoint(1.f, 5.f, 3.f);
  54. EXPECT_TRUE(Intersect::PointCylinder(cylinderBase, cylinderAxis, cylinderLength * cylinderLength, cylinderRadius * cylinderRadius, testPoint));
  55. }
  56. // Tests a point outside cylinder
  57. TEST(MATH_IntersectPointCylinder, TestPointOutside)
  58. {
  59. Vector3 cylinderBase(1.f, 2.f, 3.f);
  60. Vector3 cylinderAxis(0.f, 1.f, 0.f);
  61. float cylinderRadius = 1.f;
  62. float cylinderLength = 2.f;
  63. Vector3 testPoint(9.f, 9.f, 9.f);
  64. EXPECT_FALSE(Intersect::PointCylinder(cylinderBase, cylinderAxis, cylinderLength * cylinderLength, cylinderRadius * cylinderRadius, testPoint));
  65. }
  66. // Tests a point just inside border at the top of the cylinder
  67. TEST(MATH_IntersectPointCylinder, TestPointInsideBorderTop)
  68. {
  69. Vector3 cylinderBase(1.f, 2.f, 3.f);
  70. Vector3 cylinderAxis(0.f, 5.f, 0.f);
  71. float cylinderRadius = 1.f;
  72. float cylinderLength = cylinderAxis.GetLength();
  73. Vector3 testPoint(1.f, 6.99f, 3.f);
  74. EXPECT_TRUE(Intersect::PointCylinder(cylinderBase, cylinderAxis, cylinderLength * cylinderLength, cylinderRadius * cylinderRadius, testPoint));
  75. }
  76. // Tests a point just outside border at the top of the cylinder
  77. TEST(MATH_IntersectPointCylinder, TestPointOutsideBorderTop)
  78. {
  79. Vector3 cylinderBase(1.f, 2.f, 3.f);
  80. Vector3 cylinderAxis(0.f, 5.f, 0.f);
  81. float cylinderRadius = 1.f;
  82. float cylinderLength = cylinderAxis.GetLength();
  83. Vector3 testPoint(1.f, 7.01f, 3.f);
  84. EXPECT_FALSE(Intersect::PointCylinder(cylinderBase, cylinderAxis, cylinderLength * cylinderLength, cylinderRadius * cylinderRadius, testPoint));
  85. }
  86. // Tests a point just inside border at the bottom of the cylinder
  87. TEST(MATH_IntersectPointCylinder, TestPointInsideBorderBottom)
  88. {
  89. Vector3 cylinderBase(1.f, 2.f, 3.f);
  90. Vector3 cylinderAxis(0.f, 5.f, 0.f);
  91. float cylinderRadius = 1.f;
  92. float cylinderLength = cylinderAxis.GetLength();
  93. Vector3 testPoint(1.f, 2.01f, 3.f);
  94. EXPECT_TRUE(Intersect::PointCylinder(cylinderBase, cylinderAxis, cylinderLength * cylinderLength, cylinderRadius * cylinderRadius, testPoint));
  95. }
  96. // Tests a point just outside border at the bottom of the cylinder
  97. TEST(MATH_IntersectPointCylinder, TestPointOutsideBorderBottom)
  98. {
  99. Vector3 cylinderBase(1.f, 2.f, 3.f);
  100. Vector3 cylinderAxis(0.f, 5.f, 0.f);
  101. float cylinderRadius = 1.f;
  102. float cylinderLength = cylinderAxis.GetLength();
  103. Vector3 testPoint(1.f, 1.99f, 3.f);
  104. EXPECT_FALSE(Intersect::PointCylinder(cylinderBase, cylinderAxis, cylinderLength * cylinderLength, cylinderRadius * cylinderRadius, testPoint));
  105. }
  106. // Tests a point just inside border at the side of the cylinder
  107. TEST(MATH_IntersectPointCylinder, TestPointInsideBorderSide)
  108. {
  109. Vector3 cylinderBase(1.f, 2.f, 3.f);
  110. Vector3 cylinderAxis(0.f, 5.f, 0.f);
  111. float cylinderRadius = 1.f;
  112. float cylinderLength = cylinderAxis.GetLength();
  113. Vector3 testPoint(1.99f, 2.f, 3.f);
  114. EXPECT_TRUE(Intersect::PointCylinder(cylinderBase, cylinderAxis, cylinderLength * cylinderLength, cylinderRadius * cylinderRadius, testPoint));
  115. }
  116. // Tests a point just outside border at the side of the cylinder
  117. TEST(MATH_IntersectPointCylinder, TestPointOutsideBorderSide)
  118. {
  119. Vector3 cylinderBase(1.f, 2.f, 3.f);
  120. Vector3 cylinderAxis(0.f, 5.f, 0.f);
  121. float cylinderRadius = 1.f;
  122. float cylinderLength = cylinderAxis.GetLength();
  123. Vector3 testPoint(2.01f, 2.f, 3.f);
  124. EXPECT_FALSE(Intersect::PointCylinder(cylinderBase, cylinderAxis, cylinderLength * cylinderLength, cylinderRadius * cylinderRadius, testPoint));
  125. }
  126. TEST(MATH_IntersectBarycentric, TestBarycentric)
  127. {
  128. EXPECT_THAT(AZ::Intersect::Barycentric(AZ::Vector3(1.0f,0.0f,0.0f), AZ::Vector3(0.0f,1,0.0f), AZ::Vector3(0.0f,0.0f,1), AZ::Vector3(1,0.0f,0.0f)),
  129. IsClose(AZ::Vector3(1.0f,0.0f,0.0f)));
  130. EXPECT_THAT(AZ::Intersect::Barycentric(AZ::Vector3(1.0f,0.0f,0.0f), AZ::Vector3(0.0f,1.0f,0.0f), AZ::Vector3(0.0f,0.0f,1.0f), AZ::Vector3(0.0f,1.0f,0.0f)),
  131. IsClose(AZ::Vector3(0,1,0)));
  132. EXPECT_THAT(AZ::Intersect::Barycentric(AZ::Vector3(1.0f,0.0f,0.0f), AZ::Vector3(0.0f,1.0f,0.0f), AZ::Vector3(0.0f,0.0f,1.0f), AZ::Vector3(0.0f,0.0f,1.0f)),
  133. IsClose(AZ::Vector3(0,0,1)));
  134. EXPECT_THAT(AZ::Intersect::Barycentric(AZ::Vector3(1.0f,0.0f,0.0f), AZ::Vector3(0.0f,1.0f,0.0f), AZ::Vector3(0.0f,0.0f,1.0f), AZ::Vector3(0.3f,0.3f,0.3f)),
  135. IsClose(AZ::Vector3(0.333333f,0.333333f,0.333333f)));
  136. }
  137. }