CapsuleTests.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/Capsule.h>
  9. #include <AzCore/UnitTest/TestTypes.h>
  10. #include <AZTestShared/Math/MathTestHelpers.h>
  11. namespace UnitTest
  12. {
  13. TEST(MATH_Capsule, Constructor)
  14. {
  15. const AZ::Vector3 firstHemisphereCenter(1.0f, 2.0f, 3.0f);
  16. const AZ::Vector3 secondHemisphereCenter(3.0f, 5.0f, 7.0f);
  17. const float radius = 2.0f;
  18. AZ::Capsule capsule(firstHemisphereCenter, secondHemisphereCenter, radius);
  19. EXPECT_THAT(capsule.GetFirstHemisphereCenter(), IsClose(firstHemisphereCenter));
  20. EXPECT_THAT(capsule.GetSecondHemisphereCenter(), IsClose(secondHemisphereCenter));
  21. EXPECT_NEAR(capsule.GetRadius(), radius, AZ::Constants::Tolerance);
  22. }
  23. TEST(MATH_Capsule, GetCenter)
  24. {
  25. const AZ::Vector3 firstHemisphereCenter(2.0f, 4.0f, 1.0f);
  26. const AZ::Vector3 secondHemisphereCenter(6.0f, 6.0f, -3.0f);
  27. const float radius = 1.0f;
  28. AZ::Capsule capsule(firstHemisphereCenter, secondHemisphereCenter, radius);
  29. EXPECT_THAT(capsule.GetCenter(), IsClose(AZ::Vector3(4.0f, 5.0f, -1.0f)));
  30. }
  31. TEST(MATH_Capsule, GetCylinderHeight)
  32. {
  33. const AZ::Vector3 firstHemisphereCenter(1.0f, -1.0f, 4.0f);
  34. const AZ::Vector3 secondHemisphereCenter(3.0f, 2.0f, -2.0f);
  35. const float radius = 1.0f;
  36. AZ::Capsule capsule(firstHemisphereCenter, secondHemisphereCenter, radius);
  37. EXPECT_NEAR(capsule.GetCylinderHeight(), 7.0f, AZ::Constants::Tolerance);
  38. }
  39. TEST(MATH_Capsule, GetTotalHeight)
  40. {
  41. const AZ::Vector3 firstHemisphereCenter(-1.0f, -2.0f, 4.0f);
  42. const AZ::Vector3 secondHemisphereCenter(-3.0f, 4.0f, -5.0f);
  43. const float radius = 2.0f;
  44. AZ::Capsule capsule(firstHemisphereCenter, secondHemisphereCenter, radius);
  45. EXPECT_NEAR(capsule.GetTotalHeight(), 15.0f, AZ::Constants::Tolerance);
  46. }
  47. TEST(MATH_Capsule, IsCloseIdenticalCapsules)
  48. {
  49. const AZ::Vector3 firstHemisphereCenter(2.0f, -1.0f, 2.0f);
  50. const AZ::Vector3 secondHemisphereCenter(-1.0f, -4.0f, 3.0f);
  51. const float radius = 2.0f;
  52. AZ::Capsule capsuleA(firstHemisphereCenter, secondHemisphereCenter, radius);
  53. AZ::Capsule capsuleB(firstHemisphereCenter, secondHemisphereCenter, radius);
  54. EXPECT_TRUE(capsuleA.IsClose(capsuleB));
  55. }
  56. TEST(MATH_Capsule, IsCloseReversedEnds)
  57. {
  58. const AZ::Vector3 firstHemisphereCenter(2.0f, -1.0f, 2.0f);
  59. const AZ::Vector3 secondHemisphereCenter(-1.0f, -4.0f, 3.0f);
  60. const float radius = 2.0f;
  61. AZ::Capsule capsuleA(firstHemisphereCenter, secondHemisphereCenter, radius);
  62. AZ::Capsule capsuleB(secondHemisphereCenter, firstHemisphereCenter, radius);
  63. EXPECT_TRUE(capsuleA.IsClose(capsuleB));
  64. }
  65. TEST(MATH_Capsule, IsCloseDifferentRadius)
  66. {
  67. const AZ::Vector3 firstHemisphereCenter(2.0f, -1.0f, 2.0f);
  68. const AZ::Vector3 secondHemisphereCenter(-1.0f, -4.0f, 3.0f);
  69. const float radiusA = 2.0f;
  70. const float radiusB = 1.5f;
  71. AZ::Capsule capsuleA(firstHemisphereCenter, secondHemisphereCenter, radiusA);
  72. AZ::Capsule capsuleB(firstHemisphereCenter, secondHemisphereCenter, radiusB);
  73. EXPECT_FALSE(capsuleA.IsClose(capsuleB));
  74. }
  75. TEST(MATH_Capsule, IsCloseDifferentCenter)
  76. {
  77. const AZ::Vector3 firstHemisphereCenterA(2.0f, -1.0f, 2.0f);
  78. const AZ::Vector3 firstHemisphereCenterB(3.0f, -1.0f, 2.0f);
  79. const AZ::Vector3 secondHemisphereCenter(-1.0f, -4.0f, 3.0f);
  80. const float radius = 2.0f;
  81. AZ::Capsule capsuleA(firstHemisphereCenterA, secondHemisphereCenter, radius);
  82. AZ::Capsule capsuleB(firstHemisphereCenterB, secondHemisphereCenter, radius);
  83. EXPECT_FALSE(capsuleA.IsClose(capsuleB));
  84. }
  85. TEST(MATH_Capsule, IsCloseAllDifferent)
  86. {
  87. const AZ::Vector3 firstHemisphereCenterA(2.0f, -1.0f, 2.0f);
  88. const AZ::Vector3 firstHemisphereCenterB(3.0f, -1.0f, 2.0f);
  89. const AZ::Vector3 secondHemisphereCenterA(-1.0f, -4.0f, 3.0f);
  90. const AZ::Vector3 secondHemisphereCenterB(-2.0f, -4.0f, 3.0f);
  91. const float radiusA = 1.0f;
  92. const float radiusB = 3.0f;
  93. AZ::Capsule capsuleA(firstHemisphereCenterA, secondHemisphereCenterA, radiusA);
  94. AZ::Capsule capsuleB(firstHemisphereCenterB, secondHemisphereCenterB, radiusB);
  95. EXPECT_FALSE(capsuleA.IsClose(capsuleB));
  96. }
  97. TEST(MATH_Capsule, LineSegmentConstructor)
  98. {
  99. const AZ::Vector3 start(1.0f, 2.0f, 3.0f);
  100. const AZ::Vector3 end(2.0f, 3.0f, 4.0f);
  101. const AZ::LineSegment lineSegment(start, end);
  102. const float radius = 2.0f;
  103. const AZ::Capsule capsuleFromLineSegment(lineSegment, radius);
  104. const AZ::Capsule capsule(start, end, radius);
  105. EXPECT_THAT(capsuleFromLineSegment, IsClose(capsule));
  106. }
  107. } // namespace UnitTest