ShapeGeometryUtilTest.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/Component/ComponentApplication.h>
  9. #include <AzCore/Math/Vector3.h>
  10. #include <AzCore/UnitTest/TestTypes.h>
  11. #include <AzFramework/Entity/EntityDebugDisplayBus.h>
  12. #include <AzTest/AzTest.h>
  13. #include <LmbrCentral/Shape/ShapeComponentBus.h>
  14. #include <Shape/ShapeGeometryUtil.h>
  15. namespace UnitTest
  16. {
  17. class ShapeGeometryUtilTest
  18. : public LeakDetectionFixture
  19. {
  20. public:
  21. AZStd::unique_ptr<AZ::SerializeContext> m_serializeContext;
  22. void SetUp() override
  23. {
  24. LeakDetectionFixture::SetUp();
  25. m_serializeContext = AZStd::make_unique<AZ::SerializeContext>();
  26. }
  27. void TearDown() override
  28. {
  29. m_serializeContext.reset();
  30. LeakDetectionFixture::TearDown();
  31. }
  32. };
  33. // ccw
  34. TEST_F(ShapeGeometryUtilTest, GenerateTrianglesCCW)
  35. {
  36. AZStd::vector<AZ::Vector3> triangles =
  37. LmbrCentral::GenerateTriangles(
  38. {
  39. AZ::Vector2(0.0f, 0.0f), AZ::Vector2(1.0f, 0.0f),
  40. AZ::Vector2(1.0f, 1.0f), AZ::Vector2(0.0f, 1.0f)
  41. }
  42. );
  43. EXPECT_TRUE(triangles.size() == 6);
  44. }
  45. // cw
  46. TEST_F(ShapeGeometryUtilTest, GenerateTrianglesCW)
  47. {
  48. AZStd::vector<AZ::Vector3> triangles =
  49. LmbrCentral::GenerateTriangles(
  50. {
  51. AZ::Vector2(0.0f, 0.0f), AZ::Vector2(0.0f, 1.0f),
  52. AZ::Vector2(1.0f, 1.0f), AZ::Vector2(1.0f, 0.0f)
  53. }
  54. );
  55. EXPECT_TRUE(triangles.size() == 6);
  56. }
  57. // non-simple
  58. TEST_F(ShapeGeometryUtilTest, GenerateTrianglesFailureNonSimple)
  59. {
  60. AZStd::vector<AZ::Vector3> triangles =
  61. LmbrCentral::GenerateTriangles(
  62. {
  63. AZ::Vector2(0.0f, -2.0f), AZ::Vector2(2.0f, -2.0f),
  64. AZ::Vector2(2.0f, 1.0f), AZ::Vector2(4.0f, 1.0f)
  65. }
  66. );
  67. EXPECT_TRUE(triangles.size() == 0);
  68. }
  69. // simple-concave
  70. TEST_F(ShapeGeometryUtilTest, GenerateTrianglesSimpleConcave)
  71. {
  72. AZStd::vector<AZ::Vector3> triangles =
  73. LmbrCentral::GenerateTriangles(
  74. {
  75. AZ::Vector2(1.0f, -1.0f), AZ::Vector2(0.0f, -2.0f),
  76. AZ::Vector2(1.0f, -2.0f), AZ::Vector2(2.0f, -2.0f),
  77. AZ::Vector2(2.0f, 0.0f), AZ::Vector2(2.0f, 1.0f),
  78. AZ::Vector2(1.0f, 0.0f), AZ::Vector2(-1.0f, 0.0f)
  79. }
  80. );
  81. EXPECT_TRUE(triangles.size() == 18);
  82. }
  83. // thin
  84. TEST_F(ShapeGeometryUtilTest, GenerateTrianglesThin)
  85. {
  86. // given a series of vertices that are known to cause an infinite loop in the past
  87. // due to numerical precision issues with very thin triangles
  88. AZStd::vector<AZ::Vector3> triangles =
  89. LmbrCentral::GenerateTriangles(
  90. {
  91. AZ::Vector2( 2.00000000f, -1.50087357f),
  92. AZ::Vector2( 2.00000000f, -1.24706364f),
  93. AZ::Vector2( 1.99930608f, -0.999682188f),
  94. AZ::Vector2( 1.99859631f, -0.746669292f),
  95. AZ::Vector2( 1.99789453f, -0.496492654f),
  96. AZ::Vector2( 1.89999998f, 34.4000015f),
  97. AZ::Vector2( 1.95483327f, 0.787139893f),
  98. AZ::Vector2( 1.95505607f, 0.650562286f),
  99. AZ::Vector2( 1.95553458f, 0.357242584f),
  100. AZ::Vector2( 1.95596826f, 0.0913925171f),
  101. AZ::Vector2( 1.95620418f, -0.0532035828f),
  102. AZ::Vector2( 1.95642424f, -0.188129425f),
  103. AZ::Vector2( 1.95684254f, -0.444545746f),
  104. AZ::Vector2( 1.95693028f, -0.498298645f),
  105. AZ::Vector2( 1.95734584f, -0.753005981f),
  106. AZ::Vector2( 1.95775008f, -1.00079727f),
  107. AZ::Vector2( 1.95814919f, -1.24542999f),
  108. AZ::Vector2( 1.95856297f, -1.49910200f)
  109. }
  110. );
  111. // expect the algorithm completes and produces triangles (num verts - 2) * 3
  112. EXPECT_TRUE(triangles.size() == 48);
  113. }
  114. // test double to record if DrawTrianglesIndexed or DrawLines are called
  115. class DebugShapeDebugDisplayRequests : public AzFramework::DebugDisplayRequests
  116. {
  117. public:
  118. void DrawTrianglesIndexed(
  119. [[maybe_unused]] const AZStd::vector<AZ::Vector3>& vertices,
  120. [[maybe_unused]] const AZStd::vector<AZ::u32>& indices,
  121. [[maybe_unused]] const AZ::Color& color) override
  122. {
  123. m_drawTrianglesIndexedCalled = true;
  124. }
  125. void DrawLines([[maybe_unused]] const AZStd::vector<AZ::Vector3>& lines, [[maybe_unused]] const AZ::Color& color) override
  126. {
  127. m_drawLinesCalled = true;
  128. }
  129. bool m_drawTrianglesIndexedCalled = false;
  130. bool m_drawLinesCalled = false;
  131. };
  132. // DrawShape internally calls DrawTrianglesIndexed and DrawLines - with no geometry
  133. // we want to make sure the shape is not submitted to be drawn
  134. TEST(ShapeGeometry, Shape_not_attempted_to_be_drawn_with_no_geometry)
  135. {
  136. using ::testing::Eq;
  137. // given
  138. DebugShapeDebugDisplayRequests debugDisplayRequests;
  139. // when
  140. LmbrCentral::DrawShape(
  141. debugDisplayRequests, LmbrCentral::ShapeDrawParams{ AZ::Colors::White, AZ::Colors::White, true }, LmbrCentral::ShapeMesh{});
  142. // then
  143. EXPECT_THAT(debugDisplayRequests.m_drawTrianglesIndexedCalled, Eq(false));
  144. EXPECT_THAT(debugDisplayRequests.m_drawLinesCalled, Eq(false));
  145. }
  146. }