UnitTestHelper.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 <UnitTestHelper.h>
  9. #include <AzCore/Interface/Interface.h>
  10. #include <TriangleInputHelper.h>
  11. #include <NvCloth/IFabricCooker.h>
  12. namespace UnitTest
  13. {
  14. void ExpectEq(const AZ::Vector3& azVec, const physx::PxVec3& pxVec, const float tolerance)
  15. {
  16. EXPECT_NEAR(azVec.GetX(), pxVec.x, tolerance);
  17. EXPECT_NEAR(azVec.GetY(), pxVec.y, tolerance);
  18. EXPECT_NEAR(azVec.GetZ(), pxVec.z, tolerance);
  19. }
  20. void ExpectEq(const AZ::Vector4& azVec, const physx::PxVec4& pxVec, const float tolerance)
  21. {
  22. EXPECT_NEAR(azVec.GetX(), pxVec.x, tolerance);
  23. EXPECT_NEAR(azVec.GetY(), pxVec.y, tolerance);
  24. EXPECT_NEAR(azVec.GetZ(), pxVec.z, tolerance);
  25. EXPECT_NEAR(azVec.GetW(), pxVec.w, tolerance);
  26. }
  27. void ExpectEq(const AZ::Quaternion& azQuat, const physx::PxQuat& pxQuat, const float tolerance)
  28. {
  29. EXPECT_NEAR(azQuat.GetX(), pxQuat.x, tolerance);
  30. EXPECT_NEAR(azQuat.GetY(), pxQuat.y, tolerance);
  31. EXPECT_NEAR(azQuat.GetZ(), pxQuat.z, tolerance);
  32. EXPECT_NEAR(azQuat.GetW(), pxQuat.w, tolerance);
  33. }
  34. void ExpectEq(const physx::PxVec4& pxVecA, const physx::PxVec4& pxVecB, const float tolerance)
  35. {
  36. EXPECT_NEAR(pxVecA.x, pxVecB.x, tolerance);
  37. EXPECT_NEAR(pxVecA.y, pxVecB.y, tolerance);
  38. EXPECT_NEAR(pxVecA.z, pxVecB.z, tolerance);
  39. EXPECT_NEAR(pxVecA.w, pxVecB.w, tolerance);
  40. }
  41. void ExpectEq(const NvCloth::FabricCookedData::InternalCookedData& azCookedData, const nv::cloth::CookedData& nvCookedData, const float tolerance)
  42. {
  43. EXPECT_EQ(azCookedData.m_numParticles, nvCookedData.mNumParticles);
  44. ExpectEq(azCookedData.m_phaseIndices, nvCookedData.mPhaseIndices);
  45. ExpectEq(azCookedData.m_phaseTypes, nvCookedData.mPhaseTypes);
  46. ExpectEq(azCookedData.m_sets, nvCookedData.mSets);
  47. ExpectEq(azCookedData.m_restValues, nvCookedData.mRestvalues, tolerance);
  48. ExpectEq(azCookedData.m_stiffnessValues, nvCookedData.mStiffnessValues, tolerance);
  49. ExpectEq(azCookedData.m_indices, nvCookedData.mIndices);
  50. ExpectEq(azCookedData.m_anchors, nvCookedData.mAnchors);
  51. ExpectEq(azCookedData.m_tetherLengths, nvCookedData.mTetherLengths, tolerance);
  52. ExpectEq(azCookedData.m_triangles, nvCookedData.mTriangles);
  53. }
  54. void ExpectEq(const NvCloth::FabricCookedData::InternalCookedData& azCookedDataA, const NvCloth::FabricCookedData::InternalCookedData& azCookedDataB, const float tolerance)
  55. {
  56. EXPECT_EQ(azCookedDataA.m_numParticles, azCookedDataB.m_numParticles);
  57. EXPECT_EQ(azCookedDataA.m_phaseIndices, azCookedDataB.m_phaseIndices);
  58. EXPECT_EQ(azCookedDataA.m_phaseTypes, azCookedDataB.m_phaseTypes);
  59. EXPECT_EQ(azCookedDataA.m_sets, azCookedDataB.m_sets);
  60. ExpectEq(azCookedDataA.m_restValues, azCookedDataB.m_restValues, tolerance);
  61. ExpectEq(azCookedDataA.m_stiffnessValues, azCookedDataB.m_stiffnessValues, tolerance);
  62. EXPECT_EQ(azCookedDataA.m_indices, azCookedDataB.m_indices);
  63. EXPECT_EQ(azCookedDataA.m_anchors, azCookedDataB.m_anchors);
  64. ExpectEq(azCookedDataA.m_tetherLengths, azCookedDataB.m_tetherLengths, tolerance);
  65. EXPECT_EQ(azCookedDataA.m_triangles, azCookedDataB.m_triangles);
  66. }
  67. void ExpectEq(const NvCloth::FabricCookedData& fabricCookedDataA, const NvCloth::FabricCookedData& fabricCookedDataB, const float tolerance)
  68. {
  69. EXPECT_EQ(fabricCookedDataA.m_id, fabricCookedDataB.m_id);
  70. EXPECT_THAT(fabricCookedDataA.m_particles, ::testing::Pointwise(ContainerIsCloseTolerance(tolerance), fabricCookedDataB.m_particles));
  71. EXPECT_EQ(fabricCookedDataA.m_indices, fabricCookedDataB.m_indices);
  72. EXPECT_THAT(fabricCookedDataA.m_gravity, IsCloseTolerance(fabricCookedDataB.m_gravity, tolerance));
  73. EXPECT_EQ(fabricCookedDataA.m_useGeodesicTether, fabricCookedDataB.m_useGeodesicTether);
  74. ExpectEq(fabricCookedDataA.m_internalData, fabricCookedDataB.m_internalData, tolerance);
  75. }
  76. void ExpectEq(const AZStd::vector<float>& azVectorA, const AZStd::vector<float> azVectorB, const float tolerance)
  77. {
  78. EXPECT_EQ(azVectorA.size(), azVectorB.size());
  79. if (azVectorA.size() == azVectorB.size())
  80. {
  81. for (size_t i = 0; i < azVectorA.size(); ++i)
  82. {
  83. EXPECT_NEAR(azVectorA[i], azVectorB[i], tolerance);
  84. }
  85. }
  86. }
  87. void ExpectEq(const AZStd::vector<AZ::s32>& azVector, const nv::cloth::Range<const AZ::s32>& nvRange)
  88. {
  89. EXPECT_EQ(azVector.size(), nvRange.size());
  90. if (azVector.size() == nvRange.size())
  91. {
  92. for (AZ::u32 i = 0; i < nvRange.size(); ++i)
  93. {
  94. EXPECT_EQ(azVector[i], nvRange[i]);
  95. }
  96. }
  97. }
  98. void ExpectEq(const AZStd::vector<AZ::u32>& azVector, const nv::cloth::Range<const AZ::u32>& nvRange)
  99. {
  100. EXPECT_EQ(azVector.size(), nvRange.size());
  101. if (azVector.size() == nvRange.size())
  102. {
  103. for (AZ::u32 i = 0; i < nvRange.size(); ++i)
  104. {
  105. EXPECT_EQ(azVector[i], nvRange[i]);
  106. }
  107. }
  108. }
  109. void ExpectEq(const AZStd::vector<float>& azVector, const nv::cloth::Range<const float>& nvRange, const float tolerance)
  110. {
  111. EXPECT_EQ(azVector.size(), nvRange.size());
  112. if (azVector.size() == nvRange.size())
  113. {
  114. for (AZ::u32 i = 0; i < nvRange.size(); ++i)
  115. {
  116. EXPECT_NEAR(azVector[i], nvRange[i], tolerance);
  117. }
  118. }
  119. }
  120. void ExpectEq(const AZStd::vector<AZ::Vector4>& azVector, const nv::cloth::Range<const physx::PxVec4>& nvRange, const float tolerance)
  121. {
  122. EXPECT_EQ(azVector.size(), nvRange.size());
  123. if (azVector.size() == nvRange.size())
  124. {
  125. for (AZ::u32 i = 0; i < nvRange.size(); ++i)
  126. {
  127. ExpectEq(azVector[i], nvRange[i], tolerance);
  128. }
  129. }
  130. }
  131. NvCloth::FabricCookedData CreateTestFabricCookedData()
  132. {
  133. const float width = 1.0f;
  134. const float height = 1.0f;
  135. const AZ::u32 segmentsX = 5;
  136. const AZ::u32 segmentsY = 5;
  137. const TriangleInput planeXY = CreatePlane(width, height, segmentsX, segmentsY);
  138. const AZStd::optional<const NvCloth::FabricCookedData> fabricCookedData =
  139. AZ::Interface<NvCloth::IFabricCooker>::Get()->CookFabric(
  140. planeXY.m_vertices, planeXY.m_indices);
  141. EXPECT_TRUE(fabricCookedData.has_value());
  142. return *fabricCookedData;
  143. }
  144. } // namespace UnitTest