EmotionFXMathLibTests.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 <AzTest/AzTest.h>
  9. #include <AzCore/Math/Quaternion.h>
  10. #include <AzCore/Math/Vector3.h>
  11. #include <AzCore/Math/Matrix4x4.h>
  12. #include <AzCore/Math/MathUtils.h>
  13. #include <AZTestShared/Math/MathTestHelpers.h>
  14. #include <MCore/Source/Vector.h>
  15. #include <MCore/Source/AzCoreConversions.h>
  16. //Right Hand - counterclockwise looking down axis from positive side
  17. TEST(EmotionFXMathLibTests, AZQuaternion_Rotation1ComponentAxisX_Success)
  18. {
  19. AZ::Vector3 axis = AZ::Vector3(1.0f, 0.0f, 0.0f);
  20. axis.Normalize();
  21. AZ::Quaternion azQuaternion1 = AZ::Quaternion::CreateFromAxisAngle(axis, AZ::Constants::HalfPi);
  22. AZ::Vector3 vertexIn(0.0f, 0.0f, 0.1f);
  23. AZ::Vector3 vertexOut;
  24. vertexOut = azQuaternion1.TransformVector(vertexIn);
  25. EXPECT_THAT(vertexOut, UnitTest::IsClose(AZ::Vector3(0.0f, -0.1f, 0.0f)));
  26. }
  27. TEST(EmotionFXMathLibTests, AZQuaternion_Rotation1ComponentAxisY_Success)
  28. {
  29. AZ::Vector3 axis = AZ::Vector3(0.0f, 1.0f, 0.0f);
  30. axis.Normalize();
  31. AZ::Quaternion azQuaternion1 = AZ::Quaternion::CreateFromAxisAngle(axis, AZ::Constants::HalfPi);
  32. AZ::Vector3 vertexIn(0.1f, 0.0f, 0.0f);
  33. AZ::Vector3 vertexOut;
  34. vertexOut = azQuaternion1.TransformVector(vertexIn);
  35. EXPECT_THAT(vertexOut, UnitTest::IsClose(AZ::Vector3(0.0f, 0.0f, -0.1f)));
  36. }
  37. TEST(EmotionFXMathLibTests, AZQuaternion_Rotation1ComponentAxisZ_Success)
  38. {
  39. AZ::Vector3 axis = AZ::Vector3(0.0f, 0.0f, 1.0f);
  40. axis.Normalize();
  41. AZ::Quaternion azQuaternion1 = AZ::Quaternion::CreateFromAxisAngle(axis, AZ::Constants::HalfPi);
  42. AZ::Vector3 vertexIn(0.1f, 0.0f, 0.0f);
  43. AZ::Vector3 vertexOut;
  44. vertexOut = azQuaternion1.TransformVector(vertexIn);
  45. EXPECT_THAT(vertexOut, UnitTest::IsClose(AZ::Vector3(0.0f, 0.1f, 0.0f)));
  46. }
  47. //AZ Quaternion Normalize Vertex test
  48. TEST(EmotionFXMathLibTests, AZazQuaternion_NormalizedQuaternionRotationTest3DAxis_Success)
  49. {
  50. AZ::Vector3 axis = AZ::Vector3(1.0f, 0.7f, 0.3f);
  51. axis.Normalize();
  52. AZ::Quaternion azQuaternion1 = AZ::Quaternion::CreateFromAxisAngle(axis, AZ::Constants::HalfPi);
  53. AZ::Quaternion azQuaternion1Normalized = azQuaternion1.GetNormalized();
  54. AZ::Vector3 vertexIn(0.1f, 0.2f, 0.3f);
  55. AZ::Vector3 vertexOut1, vertexOut1FromNormalizedQuaternion;
  56. //generate value 1
  57. vertexOut1 = azQuaternion1.TransformVector(vertexIn);
  58. vertexOut1FromNormalizedQuaternion = azQuaternion1Normalized.TransformVector(vertexIn);
  59. EXPECT_THAT(vertexOut1, UnitTest::IsClose(vertexOut1FromNormalizedQuaternion));
  60. }
  61. ///////////////////////////////////////////////////////////////////////////////
  62. // Euler AZ
  63. ///////////////////////////////////////////////////////////////////////////////
  64. // AZ Quaternion <-> euler conversion Vertex test 1 component axis
  65. TEST(EmotionFXMathLibTests, AZQuaternion_EulerGetSet1ComponentAxis_Success)
  66. {
  67. AZ::Vector3 axis = AZ::Vector3(0.0f, 0.0f, 1.0f);
  68. axis.Normalize();
  69. AZ::Quaternion azQuaternion1 = AZ::Quaternion::CreateFromAxisAngle(axis, AZ::Constants::HalfPi);
  70. AZ::Vector3 vertexIn(0.1f, 0.2f, 0.3f);
  71. AZ::Vector3 vertexOut1, vertexOut2;
  72. AZ::Vector3 euler1;
  73. AZ::Quaternion test1;
  74. vertexOut1 = azQuaternion1.TransformVector(vertexIn);
  75. //euler out and in
  76. euler1 = AZ::ConvertQuaternionToEulerRadians(azQuaternion1);
  77. test1 = AZ::ConvertEulerRadiansToQuaternion(euler1);
  78. //generate vertex value 2
  79. vertexOut2 = test1.TransformVector(vertexIn);
  80. EXPECT_THAT(vertexOut1, UnitTest::IsClose(vertexOut2));
  81. }
  82. // AZ Quaternion <-> euler conversion Vertex test 2 component axis
  83. TEST(EmotionFXMathLibTests, AZQuaternion_EulerGetSet2ComponentAxis_Success)
  84. {
  85. AZ::Vector3 axis = AZ::Vector3(0.0f, 0.7f, 0.3f);
  86. axis.Normalize();
  87. AZ::Quaternion azQuaternion1 = AZ::Quaternion::CreateFromAxisAngle(axis, AZ::Constants::HalfPi);
  88. AZ::Vector3 vertexIn(0.1f, 0.2f, 0.3f);
  89. AZ::Vector3 vertexOut1, vertexOut2;
  90. AZ::Vector3 euler1;
  91. AZ::Quaternion test1;
  92. //generate vertex value 1
  93. vertexOut1 = azQuaternion1.TransformVector(vertexIn);
  94. //euler out and in
  95. euler1 = AZ::ConvertQuaternionToEulerRadians(azQuaternion1);
  96. test1 = AZ::ConvertEulerRadiansToQuaternion(euler1);
  97. //generate vertex value 2
  98. vertexOut2 = test1.TransformVector(vertexIn);
  99. EXPECT_THAT(vertexOut1, UnitTest::IsClose(vertexOut2));
  100. }
  101. // AZ Quaternion <-> euler conversion Vertex test 3 component axis
  102. TEST(EmotionFXMathLibTests, AZQuaternion_EulerInOutRotationTest3DAxis_Success)
  103. {
  104. AZ::Vector3 axis = AZ::Vector3(1.0f, 0.7f, 0.3f);
  105. axis.Normalize();
  106. AZ::Quaternion azQuaternion1 = AZ::Quaternion::CreateFromAxisAngle(axis, AZ::Constants::HalfPi);
  107. AZ::Vector3 vertexIn(0.1f, 0.2f, 0.3f);
  108. AZ::Vector3 vertexOut1, vertexOut2;
  109. AZ::Vector3 euler1;
  110. AZ::Quaternion test1;
  111. //generate vertex value 1
  112. vertexOut1 = azQuaternion1.TransformVector(vertexIn);
  113. //euler out and in
  114. euler1 = AZ::ConvertQuaternionToEulerRadians(azQuaternion1);
  115. test1 = AZ::ConvertEulerRadiansToQuaternion(euler1);
  116. //generate vertex value 2
  117. vertexOut2 = test1.TransformVector(vertexIn);
  118. EXPECT_THAT(vertexOut1, UnitTest::IsClose(vertexOut2));
  119. }
  120. // Quaternion -> Transform -> Euler conversion is same as Quaternion -> Euler
  121. // AZ Euler get set Transform Compare test 3 dim axis
  122. TEST(EmotionFXMathLibTests, AZQuaternion_EulerGetSet3ComponentAxisCompareTransform_Success)
  123. {
  124. AZ::Vector3 axis = AZ::Vector3(1.0f, 0.7f, 0.3f);
  125. axis.Normalize();
  126. AZ::Quaternion azQuaternion1 = AZ::Quaternion::CreateFromAxisAngle(axis, AZ::Constants::HalfPi);
  127. AZ::Vector3 vertexIn(0.1f, 0.2f, 0.3f);
  128. AZ::Vector3 vertexOut1, vertexOut2, vertexTransform;
  129. AZ::Vector3 euler1, eulerVectorFromTransform;
  130. AZ::Quaternion test1, testTransformQuat;
  131. //generate vertex value 1
  132. vertexOut1 = azQuaternion1.TransformVector(vertexIn);
  133. //use Transform to generate euler
  134. AZ::Transform TransformFromQuat = AZ::Transform::CreateFromQuaternion(azQuaternion1);
  135. eulerVectorFromTransform = AZ::ConvertTransformToEulerRadians(TransformFromQuat);
  136. testTransformQuat = AZ::ConvertEulerRadiansToQuaternion(eulerVectorFromTransform);
  137. vertexTransform = testTransformQuat.TransformVector(vertexIn);
  138. //use existing convert function
  139. euler1 = AZ::ConvertQuaternionToEulerRadians(azQuaternion1);
  140. test1 = AZ::ConvertEulerRadiansToQuaternion(euler1);
  141. //generate vertex value 2
  142. vertexOut2 = test1.TransformVector(vertexIn);
  143. EXPECT_THAT(vertexOut1, UnitTest::IsClose(vertexTransform));
  144. EXPECT_THAT(vertexOut1, UnitTest::IsClose(vertexOut2));
  145. EXPECT_THAT(vertexOut2, UnitTest::IsClose(vertexTransform));
  146. }
  147. // AZ Quaternion to Euler test
  148. //only way to test Quaternions sameness is to apply it to a vector and measure result
  149. TEST(EmotionFXMathLibTests, AZQuaternionConversion_ToEulerEquivalent_Success)
  150. {
  151. AZ::Vector3 eulerIn(0.1f, 0.2f, 0.3f);
  152. AZ::Vector3 testVertex(0.1f, 0.2f, 0.3f);
  153. AZ::Vector3 outVertex1, outVertex2;
  154. AZ::Quaternion test, test2;
  155. AZ::Vector3 eulerOut1, eulerOut2;
  156. test = AZ::ConvertEulerRadiansToQuaternion(eulerIn);
  157. test.Normalize();
  158. outVertex1 = test.TransformVector(testVertex);
  159. eulerOut1 = AZ::ConvertQuaternionToEulerRadians(test);
  160. test2 = AZ::ConvertEulerRadiansToQuaternion(eulerOut1);
  161. test2.Normalize();
  162. outVertex2 = test2.TransformVector(testVertex);
  163. eulerOut2 = AZ::ConvertQuaternionToEulerRadians(test2);
  164. EXPECT_THAT(eulerOut1, UnitTest::IsClose(eulerOut2));
  165. }
  166. ///////////////////////////////////////////////////////////////////////////////
  167. // Quaternion Matrix
  168. ///////////////////////////////////////////////////////////////////////////////
  169. // Test EM Quaternion made from Matrix X
  170. TEST(EmotionFXMathLibTests, AZQuaternionConversion_FromAZTransformXRot_Success)
  171. {
  172. AZ::Transform azTransform = AZ::Transform::CreateRotationX(AZ::Constants::HalfPi);
  173. AZ::Quaternion azQuaternion = azTransform.GetRotation();
  174. AZ::Vector3 emVertexIn(0.0f, 0.1f, 0.0f);
  175. AZ::Vector3 emVertexOut = azQuaternion.TransformVector(emVertexIn);
  176. EXPECT_THAT(emVertexOut, UnitTest::IsClose(AZ::Vector3(0.0f, 0.0f, 0.1f)));
  177. }
  178. // Test EM Quaternion made from Matrix Y
  179. TEST(EmotionFXMathLibTests, AZQuaternionConversion_FromAZTransformYRot_Success)
  180. {
  181. AZ::Transform azTransform = AZ::Transform::CreateRotationY(AZ::Constants::HalfPi);
  182. AZ::Quaternion azQuaternion = azTransform.GetRotation();
  183. AZ::Vector3 emVertexIn(0.0f, 0.0f, 0.1f);
  184. AZ::Vector3 emVertexOut = azQuaternion.TransformVector(emVertexIn);
  185. EXPECT_THAT(emVertexOut, UnitTest::IsClose(AZ::Vector3(0.1f, 0.0f, 0.0f)));
  186. }
  187. // Compare Quaternion made from Matrix X
  188. TEST(EmotionFXMathLibTests, AZQuaternionConversion_FromMatrixXRot_Success)
  189. {
  190. AZ::Matrix4x4 azMatrix = AZ::Matrix4x4::CreateRotationX(AZ::Constants::HalfPi);
  191. AZ::Quaternion azQuaternion = AZ::Quaternion::CreateFromMatrix4x4(azMatrix);
  192. AZ::Transform azTransform = AZ::Transform::CreateRotationX(AZ::Constants::HalfPi);
  193. AZ::Quaternion azQuaternionFromTransform = azTransform.GetRotation();
  194. AZ::Vector3 azVertexIn(0.0f, 0.1f, 0.0f);
  195. AZ::Vector3 azVertexOut = azQuaternion.TransformVector(azVertexIn);
  196. AZ::Vector3 emVertexOut = azQuaternionFromTransform.TransformVector(azVertexIn);
  197. EXPECT_THAT(azVertexOut, UnitTest::IsClose(emVertexOut));
  198. }
  199. // Compare Quaternion made from Matrix Y
  200. TEST(EmotionFXMathLibTests, AZQuaternionConversion_FromMatrixYRot_Success)
  201. {
  202. AZ::Matrix4x4 azMatrix = AZ::Matrix4x4::CreateRotationY(AZ::Constants::HalfPi);
  203. AZ::Quaternion azQuaternion = AZ::Quaternion::CreateFromMatrix4x4(azMatrix);
  204. AZ::Transform azTransform = AZ::Transform::CreateRotationY(AZ::Constants::HalfPi);
  205. AZ::Quaternion azQuaternionFromTransform = azTransform.GetRotation();
  206. AZ::Vector3 azVertexIn(0.1f, 0.0f, 0.0f);
  207. AZ::Vector3 azVertexOut = azQuaternion.TransformVector(azVertexIn);
  208. AZ::Vector3 emVertexOut = azQuaternionFromTransform.TransformVector(azVertexIn);
  209. EXPECT_THAT(azVertexOut, UnitTest::IsClose(emVertexOut));
  210. }
  211. // Compare Quaternion made from Matrix Z
  212. TEST(EmotionFXMathLibTests, AZQuaternionConversion_FromMatrixZRot_Success)
  213. {
  214. AZ::Matrix4x4 azMatrix = AZ::Matrix4x4::CreateRotationZ(AZ::Constants::HalfPi);
  215. AZ::Quaternion azQuaternion = AZ::Quaternion::CreateFromMatrix4x4(azMatrix);
  216. AZ::Transform azTransform = AZ::Transform::CreateRotationZ(AZ::Constants::HalfPi);
  217. AZ::Quaternion azQuaternionFromTransform = azTransform.GetRotation();
  218. AZ::Vector3 azVertexIn(0.1f, 0.0f, 0.0f);
  219. AZ::Vector3 azVertexOut = azQuaternion.TransformVector(azVertexIn);
  220. AZ::Vector3 emVertexOut = azQuaternionFromTransform.TransformVector(azVertexIn);
  221. EXPECT_THAT(azVertexOut, UnitTest::IsClose(emVertexOut));
  222. }
  223. // Compare Quaternion -> Matrix conversion
  224. // AZ - column major
  225. // Emfx - row major
  226. TEST(EmotionFXMathLibTests, AZQuaternionConversion_ToMatrix_Success)
  227. {
  228. AZ::Vector3 axis = AZ::Vector3(1.0f, 0.7f, 0.3f);
  229. axis.Normalize();
  230. AZ::Quaternion azQuaternion = AZ::Quaternion::CreateFromAxisAngle(axis, AZ::Constants::HalfPi);
  231. AZ::Matrix4x4 azMatrix = AZ::Matrix4x4::CreateFromQuaternion(azQuaternion);
  232. AZ::Transform azTransform = AZ::Transform::CreateFromQuaternionAndTranslation(azQuaternion, AZ::Vector3::CreateZero());
  233. bool same = true;
  234. AZ::Vector3 azTransformBasis[4];
  235. azTransform.GetBasisAndTranslation(&azTransformBasis[0], &azTransformBasis[1], &azTransformBasis[2], &azTransformBasis[3]);
  236. for (int i = 0; i < 3; ++i)
  237. {
  238. for (int j = 0; j < 4; ++j)
  239. {
  240. float emValue = azTransformBasis[j].GetElement(i);
  241. float azValue = azMatrix.GetElement(i, j);
  242. if (!AZ::IsClose(emValue, azValue, 0.001f))
  243. {
  244. same = false;
  245. break;
  246. }
  247. }
  248. if (!same)
  249. {
  250. break;
  251. }
  252. }
  253. ASSERT_TRUE(same);
  254. EXPECT_NEAR(azMatrix.GetElement(3, 0), 0.0f, 0.001f);
  255. EXPECT_NEAR(azMatrix.GetElement(3, 1), 0.0f, 0.001f);
  256. EXPECT_NEAR(azMatrix.GetElement(3, 2), 0.0f, 0.001f);
  257. EXPECT_NEAR(azMatrix.GetElement(3, 3), 1.0f, 0.001f);
  258. }
  259. //////////////////////////////////////////////////////////////////
  260. // Skinning
  261. //////////////////////////////////////////////////////////////////
  262. TEST(EmotionFXMathLibTests, AZTransform_Skin_Success)
  263. {
  264. const AZ::Quaternion rotation(0.40f, 0.08f, 0.44f, 0.80f);
  265. const AZ::Vector3 translation(0.2f, 0.1f, -0.1f);
  266. const AZ::Matrix3x4 inMat = AZ::Matrix3x4::CreateFromQuaternionAndTranslation(rotation, translation);
  267. const AZ::Vector3 inPos(0.5f, 0.6f, 0.7f);
  268. const AZ::Vector3 inNormal(0.36f, -0.352f, 0.864f);
  269. AZ::Vector3 outPos = AZ::Vector3::CreateZero();
  270. AZ::Vector3 outNormal = AZ::Vector3::CreateZero();
  271. float weight = 0.123f;
  272. MCore::Skin(inMat, &inPos, &inNormal, &outPos, &outNormal, weight);
  273. EXPECT_THAT(outPos, UnitTest::IsCloseTolerance(AZ::Vector3(0.055596f, 0.032098f, 0.111349f), 0.00001f));
  274. EXPECT_THAT(outNormal, UnitTest::IsCloseTolerance(AZ::Vector3(0.105288f, -0.039203f, 0.050066f), 0.00001f));
  275. }
  276. TEST(EmotionFXMathLibTests, AZTransform_SkinWithTangent_Success)
  277. {
  278. const AZ::Quaternion rotation(0.72f, 0.48f, 0.24f, 0.44f);
  279. const AZ::Vector3 translation(0.3f, -0.2f, 0.2f);
  280. const AZ::Matrix3x4 inMat = AZ::Matrix3x4::CreateFromQuaternionAndTranslation(rotation, translation);
  281. const AZ::Vector3 inPos(0.4f, 0.7f, 0.4f);
  282. const AZ::Vector3 inNormal(0.096f, 0.36f, 0.928f);
  283. const AZ::Vector4 inTangent = AZ::Vector4::CreateFromVector3AndFloat(AZ::Vector3::CreateAxisX().Cross(inNormal).GetNormalized(), 0.8f);
  284. AZ::Vector3 outPos = AZ::Vector3::CreateZero();
  285. AZ::Vector3 outNormal = AZ::Vector3::CreateZero();
  286. AZ::Vector4 outTangent = AZ::Vector4::CreateZero();
  287. float weight = 0.234f;
  288. MCore::Skin(inMat, &inPos, &inNormal, &inTangent, &outPos, &outNormal, &outTangent, weight);
  289. EXPECT_THAT(outPos, UnitTest::IsCloseTolerance(AZ::Vector3(0.260395f, -0.024972f, 0.134559f), 0.00001f));
  290. EXPECT_THAT(outNormal, UnitTest::IsCloseTolerance(AZ::Vector3(0.216733f, -0.080089f, -0.036997f), 0.00001f));
  291. EXPECT_THAT(outTangent.GetAsVector3(), UnitTest::IsCloseTolerance(AZ::Vector3(-0.039720f, -0.000963f, -0.230602f), 0.00001f));
  292. EXPECT_NEAR(outTangent.GetW(), inTangent.GetW(), 0.00001f);
  293. }
  294. TEST(EmotionFXMathLibTests, AZTransform_SkinWithTangentAndBitangent_Success)
  295. {
  296. const AZ::Quaternion rotation(0.72f, 0.64f, 0.12f, 0.24f);
  297. const AZ::Vector3 translation(0.1f, 0.2f, -0.1f);
  298. const AZ::Matrix3x4 inMat = AZ::Matrix3x4::CreateFromQuaternionAndTranslation(rotation, translation);
  299. const AZ::Vector3 inPos(0.2f, -0.3f, 0.5f);
  300. const AZ::Vector3 inNormal(0.768f, 0.024f, 0.64f);
  301. const AZ::Vector4 inTangent = AZ::Vector4::CreateFromVector3AndFloat(AZ::Vector3::CreateAxisX().Cross(inNormal).GetNormalized(), 0.6f);
  302. const AZ::Vector3 inBitangent = inNormal.Cross(inTangent.GetAsVector3());
  303. AZ::Vector3 outPos = AZ::Vector3::CreateZero();
  304. AZ::Vector3 outNormal = AZ::Vector3::CreateZero();
  305. AZ::Vector4 outTangent = AZ::Vector4::CreateZero();
  306. AZ::Vector3 outBitangent = AZ::Vector3::CreateZero();
  307. float weight = 0.345f;
  308. MCore::Skin(inMat, &inPos, &inNormal, &inTangent, &inBitangent, &outPos, &outNormal, &outTangent, &outBitangent, weight);
  309. EXPECT_THAT(outPos, UnitTest::IsCloseTolerance(AZ::Vector3(0.038364f, 0.110234f, -0.243101f), 0.00001f));
  310. EXPECT_THAT(outNormal, UnitTest::IsCloseTolerance(AZ::Vector3(0.153412f, 0.216512f, -0.220482f), 0.00001f));
  311. EXPECT_THAT(outTangent.GetAsVector3(), UnitTest::IsCloseTolerance(AZ::Vector3(-0.291665f, 0.020134f, -0.183170f), 0.00001f));
  312. EXPECT_NEAR(outTangent.GetW(), inTangent.GetW(), 0.00001f);
  313. EXPECT_THAT(outBitangent, UnitTest::IsCloseTolerance(AZ::Vector3(-0.102085f, 0.267847f, 0.191994f), 0.00001f));
  314. }