ScriptMath.cpp 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872
  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/Script/ScriptContext.h>
  9. #include <AzCore/Script/lua/lua.h>
  10. #include <AzCore/RTTI/BehaviorContext.h>
  11. #include <AzCore/Math/MathReflection.h>
  12. #include <AzCore/UnitTest/TestTypes.h>
  13. #include <AzCore/Math/Obb.h>
  14. #include <AzCore/Math/Aabb.h>
  15. #include <Math/MathTest.h>
  16. using namespace AZ;
  17. namespace UnitTest
  18. {
  19. // Implemented in script.cpp
  20. void AZTestAssert(bool check);
  21. void AZTestAssertFloatClose(float v1, float v2)
  22. {
  23. AZ_TEST_ASSERT_FLOAT_CLOSE(v1, v2);
  24. }
  25. static void Crc32PropertySet(Crc32 crc)
  26. {
  27. (void)crc;
  28. AZ_TracePrintf("Aha", "%d\n", static_cast<u32>(crc));
  29. }
  30. static Crc32 Crc32PropertyGet()
  31. {
  32. return Crc32("Test");
  33. }
  34. class ScriptMathTest
  35. : public LeakDetectionFixture
  36. {
  37. public:
  38. void SetUp() override
  39. {
  40. LeakDetectionFixture::SetUp();
  41. behavior = aznew BehaviorContext();
  42. behavior->Method("AZTestAssert", &AZTestAssert);
  43. behavior->Method("AZTestAssertFloatClose", &AZTestAssertFloatClose);
  44. MathReflect(behavior);
  45. behavior->Property("globalCrc32Prop", &Crc32PropertyGet, &Crc32PropertySet);
  46. script = aznew ScriptContext();
  47. script->BindTo(behavior);
  48. }
  49. void TearDown() override
  50. {
  51. delete script;
  52. delete behavior;
  53. LeakDetectionFixture::TearDown();
  54. }
  55. BehaviorContext* behavior = nullptr;
  56. ScriptContext* script = nullptr;
  57. };
  58. TEST_F(ScriptMathTest, LuaVector2Test)
  59. {
  60. ////Constructors
  61. script->Execute("vA = Vector2(0)");
  62. script->Execute("AZTestAssert((vA.x == 0) and (vA.y == 0))");
  63. script->Execute("vB = Vector2(5)");
  64. script->Execute("AZTestAssert((vB.x == 5) and (vB.y == 5))");
  65. script->Execute("vC = Vector2(1, 2)");
  66. script->Execute("AZTestAssert((vC.x == 1) and (vC.y == 2))");
  67. ////Create functions
  68. script->Execute("AZTestAssert(Vector2.CreateOne() == Vector2(1, 1))");
  69. script->Execute("AZTestAssert(Vector2.CreateZero() == Vector2(0, 0))");
  70. script->Execute("AZTestAssert(Vector2.CreateAxisX() == Vector2(1, 0))");
  71. script->Execute("AZTestAssert(Vector2.CreateAxisY() == Vector2(0, 1))");
  72. script->Execute("AZTestAssert(Vector2.CreateFromAngle() == Vector2(0, 1))");
  73. script->Execute("fromAngle4 = Vector2.CreateFromAngle(4.0)");
  74. script->Execute("AZTestAssert(fromAngle4:IsClose(Vector2(-0.7568024953, -0.6536436208), 0.01))");
  75. //// Create - Comparison functions
  76. script->Execute("vA:Set(-100, 10)");
  77. script->Execute("vB:Set(35, 10)");
  78. script->Execute("vC:Set(35, 20)");
  79. script->Execute("vD = Vector2(15, 30)");
  80. //// Compare equal
  81. //// operation r.x = (cmp1.x == cmp2.x) ? vA:x : vB:x per component
  82. script->Execute("compareEqualAB = Vector2.CreateSelectCmpEqual(vA, vB, Vector2(1), Vector2(0))");
  83. script->Execute("AZTestAssert(compareEqualAB:IsClose(Vector2(0, 1)))");
  84. script->Execute("compareEqualBC = Vector2.CreateSelectCmpEqual(vB, vC, Vector2(1), Vector2(0))");
  85. script->Execute("AZTestAssert(compareEqualBC:IsClose(Vector2(1, 0)))");
  86. //// Compare greater equal
  87. //// operation ( r.x = (cmp1.x >= cmp2.x) ? vA:x : vB:x ) per component
  88. script->Execute("compareGreaterEqualAB = Vector2.CreateSelectCmpGreaterEqual(vA, vB, Vector2(1), Vector2(0))");
  89. script->Execute("AZTestAssert(compareGreaterEqualAB:IsClose(Vector2(0, 1)))");
  90. script->Execute("compareGreaterEqualBD = Vector2.CreateSelectCmpGreaterEqual(vB, vD, Vector2(1), Vector2(0))");
  91. script->Execute("AZTestAssert(compareGreaterEqualBD:IsClose(Vector2(1, 0)))");
  92. //// Compare greater
  93. /////operation ( r.x = (cmp1.x > cmp2.x) ? vA:x : vB:x ) per component
  94. script->Execute("compareGreaterAB = Vector2.CreateSelectCmpGreater(vA, vB, Vector2(1), Vector2(0))");
  95. script->Execute("AZTestAssert(compareGreaterAB:IsClose(Vector2(0, 0)))");
  96. script->Execute("compareGreaterCA = Vector2.CreateSelectCmpGreater(vC, vA, Vector2(1), Vector2(0))");
  97. script->Execute("AZTestAssert(compareGreaterCA:IsClose(Vector2(1, 1)))");
  98. ////Get/Set
  99. script->Execute("vA:Set(2, 3)");
  100. script->Execute("AZTestAssert(vA == Vector2(2, 3))");
  101. script->Execute("AZTestAssert(vA.x == 2 and vA.y == 3)");
  102. script->Execute("vA.x = 10");
  103. script->Execute("AZTestAssert(vA == Vector2(10, 3))");
  104. script->Execute("vA.y = 11");
  105. script->Execute("AZTestAssert(vA == Vector2(10, 11))");
  106. script->Execute("vA:Set(15)");
  107. script->Execute("AZTestAssert(vA == Vector2(15))");
  108. script->Execute("vA:SetElement(0, 20)");
  109. script->Execute("AZTestAssert(vA.x == 20 and vA.y == 15)");
  110. script->Execute("AZTestAssert(vA:GetElement(0) == 20 and vA:GetElement(1) == 15)");
  111. script->Execute("vA:SetElement(1, 21)");
  112. script->Execute("AZTestAssert(vA.x == 20 and vA.y == 21)");
  113. script->Execute("AZTestAssert(vA:GetElement(0) == 20 and vA:GetElement(1) == 21)");
  114. ////Length
  115. script->Execute("AZTestAssertFloatClose(Vector2(3, 4):GetLengthSq(), 25)");
  116. script->Execute("AZTestAssertFloatClose(Vector2(4, -3):GetLength(), 5)");
  117. ////Normalize
  118. script->Execute("AZTestAssert(Vector2(3, 4):GetNormalized():IsClose(Vector2(3 / 5, 4 / 5)))");
  119. script->Execute("vA:Set(4, 3)");
  120. script->Execute("vA:Normalize()");
  121. script->Execute("AZTestAssert(vA:IsClose(Vector2(4 / 5, 3 / 5)))");
  122. script->Execute("vA:Set(4, 3)");
  123. script->Execute("length = vA:NormalizeWithLength()");
  124. script->Execute("AZTestAssertFloatClose(length, 5)");
  125. script->Execute("AZTestAssert(vA:IsClose(Vector2(4 / 5, 3 / 5)))");
  126. script->Execute("vA:Set(4, 3)");
  127. script->Execute("vA:NormalizeSafe()");
  128. script->Execute("AZTestAssert(vA:IsClose(Vector2(4 / 5, 3 / 5)))");
  129. script->Execute("vA:Set(0)");
  130. script->Execute("vA:NormalizeSafe()");
  131. script->Execute("AZTestAssert(vA:IsClose(Vector2(0, 0)))");
  132. script->Execute("vA:Set(4, 3)");
  133. script->Execute("length = vA:NormalizeSafeWithLength()");
  134. script->Execute("AZTestAssertFloatClose(length, 5)");
  135. script->Execute("AZTestAssert(vA:IsClose(Vector2(4 / 5, 3 / 5)))");
  136. script->Execute("vA:Set(0)");
  137. script->Execute("length = vA:NormalizeSafeWithLength()");
  138. script->Execute("AZTestAssertFloatClose(length, 0)");
  139. script->Execute("AZTestAssert(vA:IsClose(Vector2(0, 0)))");
  140. script->Execute("AZTestAssert(Vector2(1, 0):IsNormalized())");
  141. script->Execute("AZTestAssert(Vector2(0.7071, 0.7071):IsNormalized())");
  142. script->Execute("AZTestAssert( not Vector2(1, 1):IsNormalized())");
  143. ////Set Length
  144. script->Execute("vA:Set(3, 4)");
  145. script->Execute("vA:SetLength(10)");
  146. script->Execute("AZTestAssert(vA:IsClose(Vector2(6, 8)))");
  147. ////Distance
  148. script->Execute("vA:Set(1, 2)");
  149. script->Execute("AZTestAssertFloatClose(vA:GetDistanceSq(Vector2(-2, 6)), 25)");
  150. script->Execute("AZTestAssertFloatClose(vA:GetDistance(Vector2(5, -1)), 5)");
  151. ////Lerp/slerp
  152. script->Execute("AZTestAssert(Vector2(3, 4):Lerp(Vector2(10, 2), 0.5) == Vector2(6.5, 3))");
  153. script->Execute("slerped = Vector2(1, 0):Slerp(Vector2(0, 1), 0.5)");
  154. script->Execute("AZTestAssert(slerped:IsClose(Vector2(0.7071, 0.7071)))");
  155. ////GetPerpendicular
  156. script->Execute("AZTestAssert(Vector2(1, 2):GetPerpendicular() == Vector2(-2, 1))");
  157. ////Close
  158. script->Execute("AZTestAssert(Vector2(1, 2):IsClose(Vector2(1, 2)))");
  159. script->Execute("AZTestAssert( not Vector2(1, 2):IsClose(Vector2(1, 3)))");
  160. ////Verify the closeness tolerance works
  161. script->Execute("AZTestAssert(Vector2(1, 2):IsClose(Vector2(1, 2.4), 0.5))");
  162. ////IsZero
  163. script->Execute("AZTestAssert(Vector2(0):IsZero())");
  164. script->Execute("AZTestAssert( not Vector2(1):IsZero())");
  165. ////Verify the IsZero tolerance works
  166. script->Execute("AZTestAssert(Vector2(0.05):IsZero(0.1))");
  167. ////Operator== and Operator!=
  168. script->Execute("vA:Set(1, 2)");
  169. script->Execute("AZTestAssert(vA == Vector2(1, 2))");
  170. script->Execute("AZTestAssert( not (vA == Vector2(5, 6)))");
  171. script->Execute("AZTestAssert(vA ~= Vector2(7, 8))");
  172. script->Execute("AZTestAssert( not (vA ~= Vector2(1, 2)))");
  173. ////Comparisons - returns true only if all the components pass
  174. ////IsLessThan
  175. script->Execute("AZTestAssert(Vector2(1, 2):IsLessThan(Vector2(2, 3)))");
  176. script->Execute("AZTestAssert( not Vector2(1, 2):IsLessThan(Vector2(0, 3)))");
  177. script->Execute("AZTestAssert( not Vector2(1, 2):IsLessThan(Vector2(2, 2)))");
  178. ////IsLessEqualThan
  179. script->Execute("AZTestAssert(Vector2(1, 2):IsLessEqualThan(Vector2(2, 3)))");
  180. script->Execute("AZTestAssert( not Vector2(1, 2):IsLessEqualThan(Vector2(0, 3)))");
  181. script->Execute("AZTestAssert(Vector2(1, 2):IsLessEqualThan(Vector2(2, 2)))");
  182. ////isGreaterThan
  183. script->Execute("AZTestAssert(Vector2(1, 2):IsGreaterThan(Vector2(0, 1)))");
  184. script->Execute("AZTestAssert( not Vector2(1, 2):IsGreaterThan(Vector2(0, 3)))");
  185. script->Execute("AZTestAssert( not Vector2(1, 2):IsGreaterThan(Vector2(0, 2)))");
  186. ////isGreaterEqualThan
  187. script->Execute("AZTestAssert(Vector2(1, 2):IsGreaterEqualThan(Vector2(0, 1)))");
  188. script->Execute("AZTestAssert( not Vector2(1, 2):IsGreaterEqualThan(Vector2(0, 3)))");
  189. script->Execute("AZTestAssert(Vector2(1, 2):IsGreaterEqualThan(Vector2(0, 2)))");
  190. ////min,max,clamp
  191. script->Execute("AZTestAssert(Vector2(2, 3):GetMin(Vector2(1, 4)) == Vector2(1, 3))");
  192. script->Execute("AZTestAssert(Vector2(2, 3):GetMax(Vector2(1, 4)) == Vector2(2, 4))");
  193. script->Execute("AZTestAssert(Vector2(1, 2):GetClamp(Vector2(5, -10), Vector2(10, -5)) == Vector2(5, -5))");
  194. script->Execute("AZTestAssert(Vector2(1, 2):GetClamp(Vector2(0, 0), Vector2(10, 10)) == Vector2(1, 2))");
  195. ////Select
  196. script->Execute("vA:Set(1)");
  197. script->Execute("vB:Set(2)");
  198. script->Execute("AZTestAssert(vA:GetSelect(Vector2(0, 1), vB) == Vector2(1, 2))");
  199. script->Execute("vA:Select(Vector2(1, 0), vB)");
  200. script->Execute("AZTestAssert(vA == Vector2(2, 1))");
  201. ////Abs
  202. script->Execute("AZTestAssert(Vector2(-1, 2):GetAbs() == Vector2(1, 2))");
  203. script->Execute("AZTestAssert(Vector2(3, -4):GetAbs() == Vector2(3, 4))");
  204. ////Operators
  205. script->Execute("AZTestAssert((-Vector2(1, -2)) == Vector2(-1, 2))");
  206. script->Execute("AZTestAssert((Vector2(1, 2) + Vector2(2, -1)) == Vector2(3, 1))");
  207. script->Execute("AZTestAssert((Vector2(1, 2) - Vector2(2, -1)) == Vector2(-1, 3))");
  208. script->Execute("AZTestAssert((Vector2(3, 2) * Vector2(2, -4)) == Vector2(6, -8))");
  209. script->Execute("AZTestAssert((Vector2(3, 2) * 2) == Vector2(6, 4))");
  210. script->Execute("AZTestAssert((Vector2(30, 20) / Vector2(10, -4)) == Vector2(3, -5))");
  211. script->Execute("AZTestAssert((Vector2(30, 20) / 10) == Vector2(3, 2))");
  212. ////Dot product
  213. script->Execute("AZTestAssertFloatClose(Vector2(1, 2):Dot(Vector2(-1, 5)), 9)");
  214. ////Madd : Multiply and add
  215. script->Execute("AZTestAssert(Vector2(1, 2):GetMadd(Vector2(2, 6), Vector2(3, 4)) == Vector2(5, 16))");
  216. script->Execute("vA:Set(1, 2)");
  217. script->Execute("vA:Madd(Vector2(2, 6), Vector2(3, 4))");
  218. script->Execute("AZTestAssert(vA == Vector2(5, 16))");
  219. ////Project
  220. script->Execute("vA:Set(0.5)");
  221. script->Execute("vA:Project(Vector2(0, 2))");
  222. script->Execute("AZTestAssert(vA == Vector2(0, 0.5))");
  223. script->Execute("vA:Set(0.5)");
  224. script->Execute("vA:ProjectOnNormal(Vector2(0, 1))");
  225. script->Execute("AZTestAssert(vA == Vector2(0, 0.5))");
  226. script->Execute("vA:Set(2, 4)");
  227. script->Execute("AZTestAssert(vA:GetProjected(Vector2(1, 1)) == Vector2(3, 3))");
  228. script->Execute("AZTestAssert(vA:GetProjectedOnNormal(Vector2(1, 0)) == Vector2(2, 0))");
  229. ////IsFinite
  230. script->Execute("AZTestAssert(Vector2(1, 1):IsFinite())");
  231. script->Execute("AZTestAssert( not Vector2(math.huge, math.huge):IsFinite())");
  232. }
  233. TEST_F(ScriptMathTest, LuaVector3Test)
  234. {
  235. // Not all Vector3 C++ functions are reflected into Lua:
  236. // Compound assignment operators are not supported in Lua (+=, -=, /=, *=)
  237. // Estimate functions are not specified, the performance benefits of estimates
  238. // are are dominated by marshalling between Lua and C++.
  239. // Constructors
  240. script->Execute("v1 = Vector3(0) AZTestAssert(v1.x == 0 and v1.y == 0 and v1.x == 0)");
  241. script->Execute("v2 = Vector3(5) AZTestAssert(v2.x == 5 and v2.y == 5 and v2.x == 5)");
  242. script->Execute("v3 = Vector3(1,2,3) AZTestAssert(v3.x == 1 and v3.y == 2 and v3.z == 3)");
  243. // Create functions
  244. script->Execute("AZTestAssert(Vector3.CreateOne() == Vector3(1, 1, 1))");
  245. script->Execute("AZTestAssert(Vector3.CreateZero() == Vector3(0, 0, 0))");
  246. script->Execute("AZTestAssert(Vector3.CreateAxisX() == Vector3(1, 0, 0))");
  247. script->Execute("AZTestAssert(Vector3.CreateAxisY() == Vector3(0, 1, 0))");
  248. script->Execute("AZTestAssert(Vector3.CreateAxisZ() == Vector3(0, 0, 1))");
  249. // Set
  250. script->Execute("v1:Set(0) AZTestAssert(v1.x == 0 and v1.y == 0 and v1.z == 0)");
  251. script->Execute("v1:Set(2,3,4) AZTestAssert(v1.x == 2 and v1.y == 3 and v1.z == 4)");
  252. script->Execute("v1.x = 11 AZTestAssert(v1.x == 11)");
  253. script->Execute("v1.y = 12 AZTestAssert(v1.y == 12)");
  254. script->Execute("v1.z = 13 AZTestAssert(v1.z == 13)");
  255. // index element
  256. script->Execute("v1:Set(1,2,3) AZTestAssert(v1:GetElement(0) == 1 and v1:GetElement(1) == 2 and v1:GetElement(2) == 3)");
  257. script->Execute("v1:SetElement(0,5) v1:SetElement(1,6) v1:SetElement(2,7) AZTestAssert(v1.x == 5 and v1.y == 6 and v1.z == 7)");
  258. //length
  259. script->Execute("AZTestAssertFloatClose(Vector3(3,4,0):GetLengthSq(),25)");
  260. script->Execute("AZTestAssertFloatClose(Vector3(3.0, 4.0, 0.0):GetLengthSq(), 25.0)");
  261. script->Execute("AZTestAssertFloatClose(Vector3(0.0, 4.0, -3.0):GetLength(), 5.0)");
  262. script->Execute("AZTestAssertFloatClose(Vector3(0.0, 4.0, -3.0):GetLengthReciprocal(), 0.2)");
  263. //normalize
  264. script->Execute("AZTestAssert(Vector3(3, 0, 4):GetNormalized():IsClose(Vector3(3 / 5, 0, 4 / 5)))");
  265. script->Execute("AZTestAssert(Vector3(3, 0, 4):GetNormalizedSafe():IsClose(Vector3(3 / 5, 0, 4 / 5)))");
  266. script->Execute("AZTestAssert(Vector3(0):GetNormalizedSafe() == Vector3(0, 0, 0))");
  267. script->Execute("v1:Set(4, 3, 0)");
  268. script->Execute("v1:Normalize()");
  269. script->Execute("AZTestAssert(v1:IsClose(Vector3(4 / 5, 3 / 5, 0)))");
  270. script->Execute("v1:Set(4, 3, 0)");
  271. script->Execute("length = v1:NormalizeWithLength()");
  272. script->Execute("AZTestAssertFloatClose(length, 5)");
  273. script->Execute("AZTestAssert(v1:IsClose(Vector3(4 / 5, 3 / 5, 0)))");
  274. script->Execute("v1:Set(0, 3, 4)");
  275. script->Execute("v1:NormalizeSafe()");
  276. script->Execute("AZTestAssert(v1:IsClose(Vector3(0, 3 / 5, 4 / 5)))");
  277. script->Execute("v1 = Vector3(0)");
  278. script->Execute("v1:NormalizeSafe()");
  279. script->Execute("AZTestAssert(v1 == Vector3(0, 0, 0))");
  280. script->Execute("v1:Set(0, 3, 4)");
  281. script->Execute("length = v1:NormalizeSafeWithLength()");
  282. script->Execute("AZTestAssertFloatClose(length, 5)");
  283. script->Execute("AZTestAssert(v1:IsClose(Vector3(0, 3 / 5, 4 / 5)))");
  284. script->Execute("v1 = Vector3(0)");
  285. script->Execute("length = v1:NormalizeSafeWithLength()");
  286. script->Execute("AZTestAssert(length == 0)");
  287. script->Execute("AZTestAssert(v1 == Vector3(0, 0, 0))");
  288. script->Execute("v1:Set(0, 3, 4)");
  289. script->Execute("AZTestAssert(Vector3(1, 0, 0):IsNormalized())");
  290. script->Execute("AZTestAssert(Vector3(0.7071, 0.7071, 0):IsNormalized())");
  291. script->Execute("AZTestAssert(not Vector3(1, 1, 0):IsNormalized())");
  292. ////setlength
  293. script->Execute("v1:Set(3, 4, 0)");
  294. script->Execute("v1:SetLength(10)");
  295. script->Execute("AZTestAssert(v1:IsClose(Vector3(6, 8, 0)))");
  296. script->Execute("v1:Set(3, 4, 0)");
  297. script->Execute("v1:SetLengthEstimate(10)");
  298. #if AZ_TRAIT_USE_PLATFORM_SIMD_NEON
  299. #define LUA_VECTOR3_TEST_LENGTH_ESTIMATE_TOLERANCE "0.0195" // 10*2^(-9) ~= 0.0195 <- NEON Reciprocal ~ 9 bits
  300. #else
  301. #define LUA_VECTOR3_TEST_LENGTH_ESTIMATE_TOLERANCE "0.0049" // 10*2^(-11) ~= 0.0049 <- x86 Reciprocal ~ 11 bits
  302. #endif
  303. script->Execute("AZTestAssert(v1:IsClose(Vector3(6, 8, 0), " LUA_VECTOR3_TEST_LENGTH_ESTIMATE_TOLERANCE "))");
  304. ////distance
  305. script->Execute("v1:Set(1, 2, 3)");
  306. script->Execute("AZTestAssertFloatClose(v1:GetDistanceSq(Vector3(-2, 6, 3)), 25)");
  307. script->Execute("AZTestAssertFloatClose(v1:GetDistance(Vector3(-2, 2, -1)), 5)");
  308. ////lerp/slerp
  309. script->Execute("AZTestAssert(Vector3(3, 4, 5):Lerp(Vector3(10, 2, 1), 0.5) == Vector3(6.5, 3, 3))");
  310. script->Execute("AZTestAssert(Vector3(1, 0, 0):Slerp(Vector3(0, 1, 0), 0.5):IsClose(Vector3(0.7071, 0.7071, 0)))");
  311. ////dot product
  312. script->Execute("AZTestAssertFloatClose(Vector3(1, 2, 3):Dot(Vector3(-1, 5, 3)), 18)");
  313. ////cross products
  314. script->Execute("AZTestAssert(Vector3(1, 2, 3):Cross(Vector3(3, 1, -1)) == Vector3(-5, 10, -5))");
  315. script->Execute("AZTestAssert(Vector3(1, 2, 3):CrossXAxis() == Vector3(0, 3, -2))");
  316. script->Execute("AZTestAssert(Vector3(1, 2, 3):CrossYAxis() == Vector3(-3, 0, 1))");
  317. script->Execute("AZTestAssert(Vector3(1, 2, 3):CrossZAxis() == Vector3(2, -1, 0))");
  318. script->Execute("AZTestAssert(Vector3(1, 2, 3):XAxisCross() == Vector3(0, -3, 2))");
  319. script->Execute("AZTestAssert(Vector3(1, 2, 3):YAxisCross() == Vector3(3, 0, -1))");
  320. script->Execute("AZTestAssert(Vector3(1, 2, 3):ZAxisCross() == Vector3(-2, 1, 0))");
  321. ////close
  322. script->Execute("AZTestAssert(Vector3(1, 2, 3):IsClose(Vector3(1, 2, 3)))");
  323. script->Execute("AZTestAssert( not Vector3(1, 2, 3):IsClose(Vector3(1, 2, 4)))");
  324. script->Execute("AZTestAssert(Vector3(1, 2, 3):IsClose(Vector3(1, 2, 3.4), 0.5))");
  325. ////operator==
  326. script->Execute("v3:Set(1, 2, 3)");
  327. script->Execute("AZTestAssert(v3 == Vector3(1, 2, 3))");
  328. script->Execute("AZTestAssert( not (v3 == Vector3(1, 2, 4)))");
  329. script->Execute("AZTestAssert(v3 ~= Vector3(1, 2, 5))");
  330. script->Execute("AZTestAssert( not (v3 ~= Vector3(1, 2, 3)))");
  331. ////comparisons
  332. script->Execute("AZTestAssert(Vector3(1, 2, 3):IsLessThan(Vector3(2, 3, 4)))");
  333. script->Execute("AZTestAssert( not Vector3(1, 2, 3):IsLessThan(Vector3(0, 3, 4)))");
  334. script->Execute("AZTestAssert( not Vector3(1, 2, 3):IsLessThan(Vector3(2, 2, 4)))");
  335. script->Execute("AZTestAssert(Vector3(1, 2, 3):IsLessEqualThan(Vector3(2, 3, 4)))");
  336. script->Execute("AZTestAssert( not Vector3(1, 2, 3):IsLessEqualThan(Vector3(0, 3, 4)))");
  337. script->Execute("AZTestAssert(Vector3(1, 2, 3):IsLessEqualThan(Vector3(2, 2, 4)))");
  338. script->Execute("AZTestAssert(Vector3(1, 2, 3):IsGreaterThan(Vector3(0, 1, 2)))");
  339. script->Execute("AZTestAssert( not Vector3(1, 2, 3):IsGreaterThan(Vector3(0, 3, 2)))");
  340. script->Execute("AZTestAssert( not Vector3(1, 2, 3):IsGreaterThan(Vector3(0, 2, 2)))");
  341. script->Execute("AZTestAssert(Vector3(1, 2, 3):IsGreaterEqualThan(Vector3(0, 1, 2)))");
  342. script->Execute("AZTestAssert( not Vector3(1, 2, 3):IsGreaterEqualThan(Vector3(0, 3, 2)))");
  343. script->Execute("AZTestAssert(Vector3(1, 2, 3):IsGreaterEqualThan(Vector3(0, 2, 2)))");
  344. ////min,max,clamp
  345. script->Execute("AZTestAssert(Vector3(2, 5, 6):GetMin(Vector3(1, 6, 5)) == Vector3(1, 5, 5))");
  346. script->Execute("AZTestAssert(Vector3(2, 5, 6):GetMax(Vector3(1, 6, 5)) == Vector3(2, 6, 6))");
  347. script->Execute("AZTestAssert(Vector3(1, 2, 3):GetClamp(Vector3(0, -1, 4), Vector3(2, 1, 10)) == Vector3(1, 1, 4))");
  348. ////trig
  349. script->Execute("AZTestAssert(Vector3(Math.DegToRad(78), Math.DegToRad(-150), Math.DegToRad(190)):GetAngleMod():IsClose(Vector3(Math.DegToRad(78), Math.DegToRad(-150), Math.DegToRad(-170))))");
  350. script->Execute("AZTestAssert(Vector3(Math.DegToRad(390), Math.DegToRad(-190), Math.DegToRad(-400)):GetAngleMod():IsClose(Vector3(Math.DegToRad(30), Math.DegToRad(170), Math.DegToRad(-40))))");
  351. script->Execute("AZTestAssert(Vector3(Math.DegToRad(60), Math.DegToRad(105), Math.DegToRad(-174)):GetSin():IsClose(Vector3(0.866, 0.966, -0.105), 0.005))");
  352. script->Execute("AZTestAssert(Vector3(Math.DegToRad(60), Math.DegToRad(105), Math.DegToRad(-174)):GetCos():IsClose(Vector3(0.5, -0.259, -0.995), 0.005))");
  353. script->Execute("v1:Set(Math.DegToRad(60), Math.DegToRad(105), Math.DegToRad(-174))");
  354. script->Execute("sin, cos = v1:GetSinCos()");
  355. script->Execute("AZTestAssert(sin:IsClose(Vector3(0.866, 0.966, -0.105), 0.005))");
  356. script->Execute("AZTestAssert(cos:IsClose(Vector3(0.5, -0.259, -0.995), 0.005))");
  357. ////abs
  358. script->Execute("AZTestAssert(Vector3(-1, 2, -5):GetAbs() == Vector3(1, 2, 5))");
  359. ////reciprocal
  360. script->Execute("AZTestAssert(Vector3(2, 4, 5):GetReciprocal():IsClose(Vector3(0.5, 0.25, 0.2)))");
  361. ////operators
  362. script->Execute("AZTestAssert((-Vector3(1, 2, -3)) == Vector3(-1, -2, 3))");
  363. script->Execute("AZTestAssert((Vector3(1, 2, 3) + Vector3(-1, 4, 5)) == Vector3(0, 6, 8))");
  364. script->Execute("AZTestAssert((Vector3(1, 2, 3) - Vector3(-1, 4, 5)) == Vector3(2, -2, -2))");
  365. script->Execute("AZTestAssert((Vector3(1, 2, 3)*Vector3(-1, 4, 5)) == Vector3(-1, 8, 15))");
  366. script->Execute("AZTestAssert((Vector3(1, 2, 3) / Vector3(-1, 4, 5)):IsClose(Vector3(-1, 0.5, 3 / 5)))");
  367. script->Execute("AZTestAssert((Vector3(1, 2, 3)*2) == Vector3(2, 4, 6))");
  368. //TODO: float * Vector3 is not functional yet.
  369. //script->Execute("AZTestAssert((2*Vector3(1, 2, 3)) == Vector3(2, 4, 6))");
  370. script->Execute("AZTestAssert((Vector3(1, 2, 3) / 2):IsClose(Vector3(0.5, 1, 1.5)))");
  371. ////build tangent basis
  372. script->Execute("v1 = Vector3(1, 1, 2):GetNormalized()");
  373. script->Execute("v2, v3 = v1:BuildTangentBasis()");
  374. script->Execute("AZTestAssert(v2:IsNormalized())");
  375. script->Execute("AZTestAssert(v3:IsNormalized())");
  376. script->Execute("AZTestAssert(math.abs(v2:Dot(v1)) < 0.001)");
  377. script->Execute("AZTestAssert(math.abs(v3:Dot(v1)) < 0.001)");
  378. script->Execute("AZTestAssert(math.abs(v2:Dot(v3)) < 0.001)");
  379. ////madd
  380. script->Execute("AZTestAssert(Vector3(1, 2, 3):GetMadd(Vector3(2, 1, 4), Vector3(1, 2, 4)) == Vector3(3, 4, 16))");
  381. script->Execute("v1:Set(1, 2, 3)");
  382. script->Execute("v1:Madd(Vector3(2, 1, 4), Vector3(1, 2, 4))");
  383. script->Execute("AZTestAssert(v1 == Vector3(3, 4, 16))");
  384. ////isPerpendicular
  385. script->Execute("AZTestAssert(Vector3(1, 2, 0):IsPerpendicular(Vector3(0, 0, 1)))");
  386. script->Execute("AZTestAssert( not Vector3(1, 2, 0):IsPerpendicular(Vector3(0, 1, 1)))");
  387. }
  388. TEST_F(ScriptMathTest, LuaVector4Test)
  389. {
  390. //script->Execute("constructors
  391. script->Execute("v1 = Vector4(0)");
  392. script->Execute("AZTestAssert((v1.x == 0) and (v1.y == 0) and (v1.z == 0) and (v1.w == 0))");
  393. script->Execute("v2 = Vector4(5)");
  394. script->Execute("AZTestAssert((v2.x == 5) and (v2.y == 5) and (v2.z == 5) and (v2.w == 5))");
  395. script->Execute("v3 = Vector4(1, 2, 3, 4)");
  396. script->Execute("AZTestAssert((v3.x == 1) and (v3.y == 2) and (v3.z == 3) and (v3.w == 4))");
  397. script->Execute("v5 = Vector4.CreateFromVector3(Vector3(2, 3, 4))");
  398. script->Execute("AZTestAssert((v5.x == 2) and (v5.y == 3) and (v5.z == 4) and (v5.w == 1))");
  399. script->Execute("v6 = Vector4.CreateFromVector3AndFloat(Vector3(2, 3, 4), 5)");
  400. script->Execute("AZTestAssert((v6.x == 2) and (v6.y == 3) and (v6.z == 4) and (v6.w == 5))");
  401. ////Create functions
  402. script->Execute("AZTestAssert(Vector4.CreateOne() == Vector4(1, 1, 1, 1))");
  403. script->Execute("AZTestAssert(Vector4.CreateZero() == Vector4(0))");
  404. script->Execute("AZTestAssert(Vector4.CreateAxisX() == Vector4(1, 0, 0, 0))");
  405. script->Execute("AZTestAssert(Vector4.CreateAxisY() == Vector4(0, 1, 0, 0))");
  406. script->Execute("AZTestAssert(Vector4.CreateAxisZ() == Vector4(0, 0, 1, 0))");
  407. script->Execute("AZTestAssert(Vector4.CreateAxisW() == Vector4(0, 0, 0, 1))");
  408. ////Get/Set
  409. script->Execute("v1:Set(2, 3, 4, 5)");
  410. script->Execute("AZTestAssert(v1 == Vector4(2, 3, 4, 5))");
  411. script->Execute("v1.x = 10");
  412. script->Execute("AZTestAssert(v1 == Vector4(10, 3, 4, 5))");
  413. script->Execute("v1.y = 11");
  414. script->Execute("AZTestAssert(v1 == Vector4(10, 11, 4, 5))");
  415. script->Execute("v1.z = 12");
  416. script->Execute("AZTestAssert(v1 == Vector4(10, 11, 12, 5))");
  417. script->Execute("v1.w = 13");
  418. script->Execute("AZTestAssert(v1 == Vector4(10, 11, 12, 13))");
  419. script->Execute("v1:Set(1)");
  420. script->Execute("AZTestAssert((v1.x == 1) and (v1.y == 1) and (v1.z == 1) and (v1.w == 1))");
  421. script->Execute("v1:Set(Vector3(2, 3, 4))");
  422. script->Execute("AZTestAssert((v1.x == 2) and (v1.y == 3) and (v1.z == 4) and (v1.w == 1))");
  423. script->Execute("v1:Set(Vector3(2, 3, 4), 5)");
  424. script->Execute("AZTestAssert((v1.x == 2) and (v1.y == 3) and (v1.z == 4) and (v1.w == 5))");
  425. ////index operators
  426. script->Execute("v1:Set(1, 2, 3, 4)");
  427. script->Execute("AZTestAssert(v1:GetElement(0) == 1)");
  428. script->Execute("AZTestAssert(v1:GetElement(1) == 2)");
  429. script->Execute("AZTestAssert(v1:GetElement(2) == 3)");
  430. script->Execute("AZTestAssert(v1:GetElement(3) == 4)");
  431. script->Execute("v1:SetElement(0, 5)");
  432. script->Execute("v1:SetElement(1, 6)");
  433. script->Execute("v1:SetElement(2, 7)");
  434. script->Execute("v1:SetElement(3, 8)");
  435. script->Execute("AZTestAssert(v1:GetElement(0) == 5)");
  436. script->Execute("AZTestAssert(v1:GetElement(1) == 6)");
  437. script->Execute("AZTestAssert(v1:GetElement(2) == 7)");
  438. script->Execute("AZTestAssert(v1:GetElement(3) == 8)");
  439. ////operator==
  440. script->Execute("v3:Set(1, 2, 3, 4)");
  441. script->Execute("AZTestAssert(v3 == Vector4(1, 2, 3, 4.0))");
  442. script->Execute("AZTestAssert( not (v3 == Vector4(1, 2, 3, 5)))");
  443. script->Execute("AZTestAssert(v3 ~= Vector4(1, 2, 3, 5))");
  444. script->Execute("AZTestAssert( not (v3 ~= Vector4(1, 2, 3, 4)))");
  445. ////comparisons
  446. script->Execute("AZTestAssert(Vector4(1, 2, 3, 4):IsLessThan(Vector4(2, 3, 4, 5)))");
  447. script->Execute("AZTestAssert( not Vector4(1, 2, 3, 4):IsLessThan(Vector4(0, 3, 4, 5)))");
  448. script->Execute("AZTestAssert( not Vector4(1, 2, 3, 4):IsLessThan(Vector4(1, 2, 4, 5)))");
  449. script->Execute("AZTestAssert(Vector4(1, 2, 3, 4):IsLessEqualThan(Vector4(2, 3, 4, 5)))");
  450. script->Execute("AZTestAssert( not Vector4(1, 2, 3, 4):IsLessEqualThan(Vector4(0, 3, 4, 5)))");
  451. script->Execute("AZTestAssert(Vector4(1, 2, 3, 4):IsLessEqualThan(Vector4(2, 2, 4, 5)))");
  452. script->Execute("AZTestAssert(Vector4(1, 2, 3, 4):IsGreaterThan(Vector4(0, 1, 2, 3)))");
  453. script->Execute("AZTestAssert( not Vector4(1, 2, 3, 4):IsGreaterThan(Vector4(0, 3, 2, 3)))");
  454. script->Execute("AZTestAssert( not Vector4(1, 2, 3, 4):IsGreaterThan(Vector4(0, 2, 2, 3)))");
  455. script->Execute("AZTestAssert(Vector4(1, 2, 3, 4):IsGreaterEqualThan(Vector4(0, 1, 2, 3)))");
  456. script->Execute("AZTestAssert( not Vector4(1, 2, 3, 4):IsGreaterEqualThan(Vector4(0, 3, 2, 3)))");
  457. script->Execute("AZTestAssert(Vector4(1, 2, 3, 4):IsGreaterEqualThan(Vector4(0, 2, 2, 3)))");
  458. ////dot product
  459. script->Execute("AZTestAssertFloatClose(Vector4(1, 2, 3, 4):Dot(Vector4(-1, 5, 3, 2)), 26)");
  460. script->Execute("AZTestAssertFloatClose(Vector4(1, 2, 3, 4):Dot3(Vector3(-1, 5, 3)), 18)");
  461. ////close
  462. script->Execute("AZTestAssert(Vector4(1, 2, 3, 4):IsClose(Vector4(1, 2, 3, 4)))");
  463. script->Execute("AZTestAssert( not Vector4(1, 2, 3, 4):IsClose(Vector4(1, 2, 3, 5)))");
  464. script->Execute("AZTestAssert(Vector4(1, 2, 3, 4):IsClose(Vector4(1, 2, 3, 4.4), 0.5))");
  465. ////homogenize
  466. script->Execute("v1:Set(1, 2, 3, 4)");
  467. script->Execute("AZTestAssert(v1:GetHomogenized():IsClose(Vector3(0.25, 0.5, 0.75)))");
  468. script->Execute("v1:Homogenize()");
  469. script->Execute("AZTestAssert(v1:IsClose(Vector4(0.25, 0.5, 0.75, 1)))");
  470. ////abs
  471. script->Execute("AZTestAssert(Vector4(-1, 2, -5, 1):GetAbs() == Vector4(1, 2, 5, 1))");
  472. script->Execute("AZTestAssert(Vector4(1, -2, 5, -1):GetAbs() == Vector4(1, 2, 5, 1))");
  473. ////reciprocal
  474. script->Execute("AZTestAssert(Vector4(2, 4, 5, 10):GetReciprocal():IsClose(Vector4(0.5, 0.25, 0.2, 0.1)))");
  475. //// operators
  476. script->Execute("AZTestAssert((-Vector4(1, 2, -3, -1)) == Vector4(-1, -2, 3, 1))");
  477. script->Execute("AZTestAssert((Vector4(1, 2, 3, 4) + Vector4(-1, 4, 5, 2)) == Vector4(0, 6, 8, 6))");
  478. script->Execute("AZTestAssert((Vector4(1, 2, 3, 4) - Vector4(-1, 4, 5, 2)) == Vector4(2, -2, -2, 2))");
  479. script->Execute("AZTestAssert((Vector4(1, 2, 3, 4)*Vector4(-1, 4, 5, 2)) == Vector4(-1, 8, 15, 8))");
  480. script->Execute("AZTestAssert((Vector4(1, 2, 3, 4) / Vector4(-1, 4, 5, 2)):IsClose(Vector4(-1, 0.5, 3 / 5, 2)))");
  481. script->Execute("AZTestAssert((Vector4(1, 2, 3, 4)*2) == Vector4(2, 4, 6, 8))");
  482. //TODO float * Vector4 is not functional yet
  483. //script->Execute("AZTestAssert((2*Vector4(1, 2, 3, 4)) == Vector4(2, 4, 6, 8))");
  484. script->Execute("AZTestAssert((Vector4(1, 2, 3, 4) / 2):IsClose(Vector4(0.5, 1, 1.5, 2)))");
  485. script->Execute("v1:Set(1, 2, 3, 4)");
  486. }
  487. TEST_F(ScriptMathTest, LuaQuaternionTest)
  488. {
  489. //constructors
  490. script->Execute("q1 = Quaternion(0, 0, 0, 1)");
  491. script->Execute("AZTestAssert((q1.x == 0) and (q1.y == 0) and (q1.z == 0) and (q1.w == 1))");
  492. script->Execute("q2 = Quaternion(5)");
  493. script->Execute("AZTestAssert((q2.x == 5) and (q2.y == 5) and (q2.z == 5) and (q2.w == 5))");
  494. script->Execute("q3 = Quaternion(1, 2, 3, 4)");
  495. script->Execute("AZTestAssert((q3.x == 1) and (q3.y == 2) and (q3.z == 3) and (q3.w == 4))");
  496. script->Execute("q4 = Quaternion.CreateFromVector3AndValue(Vector3(1, 2, 3), 4)");
  497. script->Execute("AZTestAssert((q4.x == 1) and (q4.y == 2) and (q4.z == 3) and (q4.w == 4))");
  498. script->Execute("q7 = Quaternion.CreateFromMatrix3x3(Matrix3x3.CreateRotationX(Math.DegToRad(120)))");
  499. script->Execute("AZTestAssert(q7:IsClose(Quaternion(0.866, 0, 0, 0.5)))");
  500. script->Execute("q8 = Quaternion.CreateFromMatrix4x4(Matrix4x4.CreateRotationX(Math.DegToRad(-60)))");
  501. script->Execute("AZTestAssert(q8:IsClose(Quaternion(-0.5, 0, 0, 0.866)))");
  502. script->Execute("q9 = Quaternion.CreateFromVector3(Vector3(1, 2, 3))");
  503. script->Execute("AZTestAssert((q9.x == 1) and (q9.y == 2) and (q9.z == 3) and (q9.w == 0))");
  504. script->Execute("q10 = Quaternion.CreateFromAxisAngle(Vector3.CreateAxisZ(), Math.DegToRad(45))");
  505. script->Execute("AZTestAssert(q10:IsClose(Quaternion.CreateRotationZ(Math.DegToRad(45))))");
  506. //Create functions
  507. script->Execute("AZTestAssert(Quaternion.CreateIdentity() == Quaternion(0, 0, 0, 1))");
  508. script->Execute("AZTestAssert(Quaternion.CreateZero() == Quaternion(0))");
  509. script->Execute("AZTestAssert(Quaternion.CreateRotationX(Math.DegToRad(60)):IsClose(Quaternion(0.5, 0, 0, 0.866)))");
  510. script->Execute("AZTestAssert(Quaternion.CreateRotationY(Math.DegToRad(60)):IsClose(Quaternion(0, 0.5, 0, 0.866)))");
  511. script->Execute("AZTestAssert(Quaternion.CreateRotationZ(Math.DegToRad(60)):IsClose(Quaternion(0, 0, 0.5, 0.866)))");
  512. //test shortest arc
  513. script->Execute("v1 = Vector3(1, 2, 3):GetNormalized()");
  514. script->Execute("v2 = Vector3(-2, 7, -1):GetNormalized()");
  515. script->Execute("q3 = Quaternion.CreateShortestArc(v1, v2)"); //q3 should transform v1 into v2
  516. script->Execute("AZTestAssert(v2:IsClose(q3*v1, 0.001))");
  517. script->Execute("q4 = Quaternion.CreateShortestArc(Vector3(1, 0, 0), Vector3(0, 1, 0))");
  518. script->Execute("AZTestAssert((q4*Vector3(0, 0, 1)):IsClose(Vector3(0, 0, 1), 0.001))"); //perpendicular vector should be unaffected
  519. script->Execute("AZTestAssert((q4*Vector3(0, -1, 0)):IsClose(Vector3(1, 0, 0), 0.001))"); //make sure we rotate the right direction, i.e. actually shortest arc
  520. //Get/Set
  521. script->Execute("q1.x = 10");
  522. script->Execute("AZTestAssert(q1.x == 10)");
  523. script->Execute("q1.y = 11");
  524. script->Execute("AZTestAssert(q1.y == 11)");
  525. script->Execute("q1.z = 12");
  526. script->Execute("AZTestAssert(q1.z == 12)");
  527. script->Execute("q1.w = 13");
  528. script->Execute("AZTestAssert(q1.w == 13)");
  529. script->Execute("q1:Set(15)");
  530. script->Execute("AZTestAssert(q1 == Quaternion(15))");
  531. script->Execute("q1:Set(2, 3, 4, 5)");
  532. script->Execute("AZTestAssert(q1 == Quaternion(2, 3, 4, 5))");
  533. script->Execute("q1:Set(Vector3(5, 6, 7), 8)");
  534. script->Execute("AZTestAssert(q1 == Quaternion(5, 6, 7, 8))");
  535. //GetElement/SetElement
  536. script->Execute("q1:SetElement(0, 1)");
  537. script->Execute("q1:SetElement(1, 2)");
  538. script->Execute("q1:SetElement(2, 3)");
  539. script->Execute("q1:SetElement(3, 4)");
  540. script->Execute("AZTestAssert(q1:GetElement(0) == 1)");
  541. script->Execute("AZTestAssert(q1:GetElement(1) == 2)");
  542. script->Execute("AZTestAssert(q1:GetElement(2) == 3)");
  543. script->Execute("AZTestAssert(q1:GetElement(3) == 4)");
  544. //IsIdentity
  545. script->Execute("q1:Set(0, 0, 0, 1)");
  546. script->Execute("AZTestAssert(q1:IsIdentity())");
  547. //conjugate
  548. script->Execute("q1:Set(1, 2, 3, 4)");
  549. script->Execute("AZTestAssert(q1:GetConjugate() == Quaternion(-1, -2, -3, 4))");
  550. //inverse
  551. script->Execute("q1 = Quaternion.CreateRotationX(Math.DegToRad(25)) * Quaternion.CreateRotationY(Math.DegToRad(70))");
  552. script->Execute("AZTestAssert((q1*q1:GetInverseFast()):IsClose(Quaternion.CreateIdentity()))");
  553. script->Execute("q2 = q1:Clone()");
  554. script->Execute("q2:InvertFast()");
  555. script->Execute("AZTestAssert(q1.x == -q2.x)");
  556. script->Execute("AZTestAssert(q1.y == -q2.y)");
  557. script->Execute("AZTestAssert(q1.z == -q2.z)");
  558. script->Execute("AZTestAssert(q1.w == q2.w)");
  559. script->Execute("AZTestAssert((q1*q2):IsClose(Quaternion.CreateIdentity()))");
  560. script->Execute("q1:Set(1, 2, 3, 4)");
  561. script->Execute("AZTestAssert((q1*q1:GetInverseFull()):IsClose(Quaternion.CreateIdentity()))");
  562. //dot product
  563. script->Execute("AZTestAssertFloatClose(Quaternion(1, 2, 3, 4):Dot(Quaternion(-1, 5, 3, 2)), 26)");
  564. //length
  565. script->Execute("AZTestAssertFloatClose(Quaternion(-1, 2, 1, 3):GetLengthSq(), 15)");
  566. script->Execute("AZTestAssertFloatClose(Quaternion(-4, 2, 0, 4):GetLength(), 6)");
  567. //normalize
  568. // TODO This asserted for some reason
  569. script->Execute("AZTestAssert(Quaternion(0, -4, 2, 4):GetNormalized():IsClose(Quaternion(0, -0.6666, 0.3333, 0.6666)))");
  570. script->Execute("q1:Set(2, 0, 4, -4)");
  571. script->Execute("q1:Normalize()");
  572. script->Execute("AZTestAssert(q1:IsClose(Quaternion(0.3333, 0, 0.6666, -0.6666)))");
  573. script->Execute("q1:Set(2, 0, 4, -4)");
  574. script->Execute("length = q1:NormalizeWithLength()");
  575. script->Execute("AZTestAssertFloatClose(length, 6)");
  576. script->Execute("AZTestAssert(q1:IsClose(Quaternion(0.3333, 0, 0.6666, -0.6666)))");
  577. //interpolation
  578. script->Execute("AZTestAssert(Quaternion(1, 2, 3, 4):Lerp(Quaternion(2, 3, 4, 5), 0.5):IsClose(Quaternion(1.5, 2.5, 3.5, 4.5)))");
  579. script->Execute("AZTestAssert(Quaternion.CreateRotationX(Math.DegToRad(10)):Slerp(Quaternion.CreateRotationY(Math.DegToRad(60)), 0.5):IsClose(Quaternion(0.045, 0.259, 0, 0.965), 0.001))");
  580. script->Execute("AZTestAssert(Quaternion.CreateRotationX(Math.DegToRad(10)):Squad(Quaternion.CreateRotationY(Math.DegToRad(60)), Quaternion.CreateRotationZ(Math.DegToRad(35)), Quaternion.CreateRotationX(Math.DegToRad(80)), 0.5):IsClose(Quaternion(0.2, 0.132, 0.083, 0.967), 0.001))");
  581. //close
  582. script->Execute("AZTestAssert(Quaternion(1, 2, 3, 4):IsClose(Quaternion(1, 2, 3, 4)))");
  583. script->Execute("AZTestAssert( not Quaternion(1, 2, 3, 4):IsClose(Quaternion(1, 2, 3, 5)))");
  584. script->Execute("AZTestAssert(Quaternion(1, 2, 3, 4):IsClose(Quaternion(1, 2, 3, 4.4), 0.5))");
  585. //operators
  586. script->Execute("AZTestAssert((-Quaternion(1, 2, 3, 4)) == Quaternion(-1, -2, -3, -4))");
  587. script->Execute("AZTestAssert((Quaternion(1, 2, 3, 4) + Quaternion(2, 3, 5, -1)):IsClose(Quaternion(3, 5, 8, 3)))");
  588. script->Execute("AZTestAssert((Quaternion(1, 2, 3, 4) - Quaternion(2, 3, 5, -1)):IsClose(Quaternion(-1, -1, -2, 5)))");
  589. script->Execute("AZTestAssert((Quaternion(1, 2, 3, 4)*Quaternion(2, 3, 5, -1)):IsClose(Quaternion(8, 11, 16, -27)))");
  590. script->Execute("AZTestAssert((Quaternion(1, 2, 3, 4)*2):IsClose(Quaternion(2, 4, 6, 8)))");
  591. //TODO float * Quaternion is not functional yet
  592. //script->Execute("AZTestAssert((2*Quaternion(1, 2, 3, 4)):IsClose(Quaternion(2, 4, 6, 8)))");
  593. script->Execute("AZTestAssert((Quaternion(1, 2, 3, 4) / 2):IsClose(Quaternion(0.5, 1, 1.5, 2)))");
  594. //operator==
  595. script->Execute("q3:Set(1, 2, 3, 4)");
  596. script->Execute("AZTestAssert(q3 == Quaternion(1, 2, 3, 4.0))");
  597. script->Execute("AZTestAssert( not (q3 == Quaternion(1, 2, 3, 5)))");
  598. script->Execute("AZTestAssert(q3 ~= Quaternion(1, 2, 3, 5))");
  599. script->Execute("AZTestAssert( not (q3 ~= Quaternion(1, 2, 3, 4)))");
  600. //vector transform
  601. script->Execute("AZTestAssert((Quaternion.CreateRotationX(Math.DegToRad(45))*Vector3(4, 1, 0)):IsClose(Vector3(4, 0.7071, 0.7071)))");
  602. //GetImaginary
  603. script->Execute("q1:Set(21, 22, 23, 24)");
  604. script->Execute("AZTestAssert(q1:GetImaginary() == Vector3(21, 22, 23))");
  605. //GetAngle
  606. script->Execute("q1 = Quaternion.CreateRotationX(Math.DegToRad(35))");
  607. script->Execute("AZTestAssertFloatClose(q1:GetAngle(),Math.DegToRad(35))");
  608. //make sure that our transformations concatenate in the correct order
  609. script->Execute("q1 = Quaternion.CreateRotationZ(Math.DegToRad(90))");
  610. script->Execute("q2 = Quaternion.CreateRotationX(Math.DegToRad(90))");
  611. script->Execute("v = (q2 * q1) * Vector3(1, 0, 0)");
  612. script->Execute("AZTestAssert(v:IsClose(Vector3(0, 0, 1)))");
  613. }
  614. TEST_F(ScriptMathTest, LuaMatrix3x3Test)
  615. {
  616. script->Execute("testIndices = { 0, 1, 2, 3 }");
  617. //create functions
  618. script->Execute("m1 = Matrix3x3.CreateIdentity()");
  619. script->Execute("AZTestAssert(m1:GetRow(0) == Vector3(1, 0, 0))");
  620. script->Execute("AZTestAssert(m1:GetRow(1) == Vector3(0, 1, 0))");
  621. script->Execute("AZTestAssert(m1:GetRow(2) == Vector3(0, 0, 1))");
  622. script->Execute("m1 = Matrix3x3.CreateZero()");
  623. script->Execute("AZTestAssert(m1:GetRow(0) == Vector3(0))");
  624. script->Execute("AZTestAssert(m1:GetRow(1) == Vector3(0))");
  625. script->Execute("AZTestAssert(m1:GetRow(2) == Vector3(0))");
  626. script->Execute("m1 = Matrix3x3.CreateFromValue(2)");
  627. script->Execute("AZTestAssert(m1:GetRow(0) == Vector3(2))");
  628. script->Execute("AZTestAssert(m1:GetRow(1) == Vector3(2))");
  629. script->Execute("AZTestAssert(m1:GetRow(2) == Vector3(2))");
  630. script->Execute("m1 = Matrix3x3.CreateRotationX(Math.DegToRad(30))");
  631. script->Execute("AZTestAssert(m1:GetRow(0):IsClose(Vector3(1, 0, 0)))");
  632. script->Execute("AZTestAssert(m1:GetRow(1):IsClose(Vector3(0, 0.866, -0.5)))");
  633. script->Execute("AZTestAssert(m1:GetRow(2):IsClose(Vector3(0, 0.5, 0.866)))");
  634. script->Execute("m1 = Matrix3x3.CreateRotationY(Math.DegToRad(30))");
  635. script->Execute("AZTestAssert(m1:GetRow(0):IsClose(Vector3(0.866, 0, 0.5)))");
  636. script->Execute("AZTestAssert(m1:GetRow(1):IsClose(Vector3(0, 1, 0)))");
  637. script->Execute("AZTestAssert(m1:GetRow(2):IsClose(Vector3(-0.5, 0, 0.866)))");
  638. script->Execute("m1 = Matrix3x3.CreateRotationZ(Math.DegToRad(30))");
  639. script->Execute("AZTestAssert(m1:GetRow(0):IsClose(Vector3(0.866, -0.5, 0)))");
  640. script->Execute("AZTestAssert(m1:GetRow(1):IsClose(Vector3(0.5, 0.866, 0)))");
  641. script->Execute("AZTestAssert(m1:GetRow(2):IsClose(Vector3(0, 0, 1)))");
  642. script->Execute("m1 = Matrix3x3.CreateFromTransform(Transform.CreateRotationX(Math.DegToRad(30)))");
  643. script->Execute("AZTestAssert(m1:GetRow(0):IsClose(Vector3(1, 0, 0)))");
  644. script->Execute("AZTestAssert(m1:GetRow(1):IsClose(Vector3(0, 0.866, -0.5)))");
  645. script->Execute("AZTestAssert(m1:GetRow(2):IsClose(Vector3(0, 0.5, 0.866)))");
  646. script->Execute("m1 = Matrix3x3.CreateFromMatrix4x4(Matrix4x4.CreateRotationX(Math.DegToRad(30)))");
  647. script->Execute("AZTestAssert(m1:GetRow(0):IsClose(Vector3(1, 0, 0)))");
  648. script->Execute("AZTestAssert(m1:GetRow(1):IsClose(Vector3(0, 0.866, -0.5)))");
  649. script->Execute("AZTestAssert(m1:GetRow(2):IsClose(Vector3(0, 0.5, 0.866)))");
  650. script->Execute("m1 = Matrix3x3.CreateFromQuaternion(Quaternion.CreateRotationX(Math.DegToRad(30)))");
  651. script->Execute("AZTestAssert(m1:GetRow(0):IsClose(Vector3(1, 0, 0)))");
  652. script->Execute("AZTestAssert(m1:GetRow(1):IsClose(Vector3(0, 0.866, -0.5)))");
  653. script->Execute("AZTestAssert(m1:GetRow(2):IsClose(Vector3(0, 0.5, 0.866)))");
  654. script->Execute("m1 = Matrix3x3.CreateScale(Vector3(1, 2, 3))");
  655. script->Execute("AZTestAssert(m1:GetRow(0) == Vector3(1, 0, 0))");
  656. script->Execute("AZTestAssert(m1:GetRow(1) == Vector3(0, 2, 0))");
  657. script->Execute("AZTestAssert(m1:GetRow(2) == Vector3(0, 0, 3))");
  658. script->Execute("m1 = Matrix3x3.CreateDiagonal(Vector3(2, 3, 4))");
  659. script->Execute("AZTestAssert(m1:GetRow(0) == Vector3(2, 0, 0))");
  660. script->Execute("AZTestAssert(m1:GetRow(1) == Vector3(0, 3, 0))");
  661. script->Execute("AZTestAssert(m1:GetRow(2) == Vector3(0, 0, 4))");
  662. script->Execute("m1 = Matrix3x3.CreateCrossProduct(Vector3(1, 2, 3))");
  663. script->Execute("AZTestAssert(m1:GetRow(0) == Vector3(0, -3, 2))");
  664. script->Execute("AZTestAssert(m1:GetRow(1) == Vector3(3, 0, -1))");
  665. script->Execute("AZTestAssert(m1:GetRow(2) == Vector3(-2, 1, 0))");
  666. //element access
  667. script->Execute("m1 = Matrix3x3.CreateRotationX(Math.DegToRad(30))");
  668. script->Execute("AZTestAssertFloatClose(m1:GetElement(1, 2), -0.5)");
  669. script->Execute("AZTestAssertFloatClose(m1:GetElement(2, 2), 0.866)");
  670. script->Execute("AZTestAssertFloatClose(m1.basisZ.y, -0.5)");
  671. script->Execute("m1:SetElement(2, 1, 5)");
  672. script->Execute("AZTestAssert(m1:GetElement(2, 1) == 5)");
  673. //index accessors
  674. script->Execute("AZTestAssertFloatClose(m1.basisZ.y, -0.5)");
  675. script->Execute("AZTestAssertFloatClose(m1.basisZ.z, 0.866)");
  676. script->Execute("m1:SetElement(2, 1, 15)");
  677. script->Execute("AZTestAssert(m1.basisY.z == 15)");
  678. //row access
  679. script->Execute("m1 = Matrix3x3.CreateRotationX(Math.DegToRad(30))");
  680. script->Execute("AZTestAssert(m1:GetRow(2):IsClose(Vector3(0, 0.5, 0.866)))");
  681. script->Execute("m1:SetRow(0, 1, 2, 3)");
  682. script->Execute("AZTestAssert(m1:GetRow(0):IsClose(Vector3(1, 2, 3)))");
  683. script->Execute("m1:SetRow(1, Vector3(4, 5, 6))");
  684. script->Execute("AZTestAssert(m1:GetRow(1):IsClose(Vector3(4, 5, 6)))");
  685. script->Execute("m1:SetRow(2, Vector3(7, 8, 9))");
  686. script->Execute("AZTestAssert(m1:GetRow(2):IsClose(Vector3(7, 8, 9)))");
  687. //test GetRow with non - constant, we have different implementations for constants and variables
  688. script->Execute("AZTestAssert(m1:GetRow(testIndices[1]):IsClose(Vector3(1, 2, 3)))");
  689. script->Execute("AZTestAssert(m1:GetRow(testIndices[2]):IsClose(Vector3(4, 5, 6)))");
  690. script->Execute("AZTestAssert(m1:GetRow(testIndices[3]):IsClose(Vector3(7, 8, 9)))");
  691. script->Execute("row0, row1, row2 = m1:GetRows()");
  692. script->Execute("AZTestAssert(row0:IsClose(Vector3(1, 2, 3)))");
  693. script->Execute("AZTestAssert(row1:IsClose(Vector3(4, 5, 6)))");
  694. script->Execute("AZTestAssert(row2:IsClose(Vector3(7, 8, 9)))");
  695. script->Execute("m1:SetRows(Vector3(10, 11, 12), Vector3(13, 14, 15), Vector3(16, 17, 18))");
  696. script->Execute("AZTestAssert(m1:GetRow(0):IsClose(Vector3(10, 11, 12)))");
  697. script->Execute("AZTestAssert(m1:GetRow(1):IsClose(Vector3(13, 14, 15)))");
  698. script->Execute("AZTestAssert(m1:GetRow(2):IsClose(Vector3(16, 17, 18)))");
  699. //column access
  700. script->Execute("m1 = Matrix3x3.CreateRotationX(Math.DegToRad(30))");
  701. script->Execute("AZTestAssert(m1:GetColumn(1):IsClose(Vector3(0, 0.866, 0.5)))");
  702. script->Execute("m1:SetColumn(2, 1, 2, 3)");
  703. script->Execute("AZTestAssert(m1:GetColumn(2):IsClose(Vector3(1, 2, 3)))");
  704. script->Execute("AZTestAssert(m1:GetRow(0):IsClose(Vector3(1, 0, 1)))"); //checking all components in case others get messed up with the shuffling
  705. script->Execute("AZTestAssert(m1:GetRow(1):IsClose(Vector3(0, 0.866, 2)))");
  706. script->Execute("AZTestAssert(m1:GetRow(2):IsClose(Vector3(0, 0.5, 3)))");
  707. script->Execute("m1:SetColumn(0, Vector3(2, 3, 4))");
  708. script->Execute("AZTestAssert(m1:GetColumn(0):IsClose(Vector3(2, 3, 4)))");
  709. script->Execute("AZTestAssert(m1:GetRow(0):IsClose(Vector3(2, 0, 1)))");
  710. script->Execute("AZTestAssert(m1:GetRow(1):IsClose(Vector3(3, 0.866, 2)))");
  711. script->Execute("AZTestAssert(m1:GetRow(2):IsClose(Vector3(4, 0.5, 3)))");
  712. //test GetColumn with non - constant, we have different implementations for constants and variables
  713. script->Execute("AZTestAssert(m1:GetColumn(testIndices[1]):IsClose(Vector3(2, 3, 4)))");
  714. script->Execute("AZTestAssert(m1:GetColumn(testIndices[2]):IsClose(Vector3(0, 0.866, 0.5)))");
  715. script->Execute("AZTestAssert(m1:GetColumn(testIndices[3]):IsClose(Vector3(1, 2, 3)))");
  716. script->Execute("col0, col1, col2 = m1:GetColumns()");
  717. script->Execute("AZTestAssert(col0:IsClose(Vector3(2, 3, 4)))");
  718. script->Execute("AZTestAssert(col1:IsClose(Vector3(0, 0.866, 0.5)))");
  719. script->Execute("AZTestAssert(col2:IsClose(Vector3(1, 2, 3)))");
  720. script->Execute("m1:SetColumns(Vector3(10, 11, 12), Vector3(13, 14, 15), Vector3(16, 17, 18))");
  721. script->Execute("AZTestAssert(m1:GetColumn(0):IsClose(Vector3(10, 11, 12)))");
  722. script->Execute("AZTestAssert(m1:GetColumn(1):IsClose(Vector3(13, 14, 15)))");
  723. script->Execute("AZTestAssert(m1:GetColumn(2):IsClose(Vector3(16, 17, 18)))");
  724. //basis access
  725. script->Execute("m1 = Matrix3x3.CreateRotationX(Math.DegToRad(30))");
  726. script->Execute("AZTestAssert(m1.basisX:IsClose(Vector3(1, 0, 0)))");
  727. script->Execute("AZTestAssert(m1.basisY:IsClose(Vector3(0, 0.866, 0.5)))");
  728. script->Execute("AZTestAssert(m1.basisZ:IsClose(Vector3(0, -0.5, 0.866)))");
  729. script->Execute("m1.basisX = Vector3(1, 2, 3)");
  730. script->Execute("AZTestAssert(m1.basisX:IsClose(Vector3(1, 2, 3)))");
  731. script->Execute("m1.basisY = Vector3(4, 5, 6)");
  732. script->Execute("AZTestAssert(m1.basisY:IsClose(Vector3(4, 5, 6)))");
  733. script->Execute("m1.basisZ = Vector3(7, 8, 9)");
  734. script->Execute("AZTestAssert(m1.basisZ:IsClose(Vector3(7, 8, 9)))");
  735. script->Execute("m1.basisX = Vector3(10, 11, 12)");
  736. script->Execute("AZTestAssert(m1.basisX:IsClose(Vector3(10, 11, 12)))");
  737. script->Execute("m1.basisY = Vector3(13, 14, 15)");
  738. script->Execute("AZTestAssert(m1.basisY:IsClose(Vector3(13, 14, 15)))");
  739. script->Execute("m1.basisZ = Vector3(16, 17, 18)");
  740. script->Execute("AZTestAssert(m1.basisZ:IsClose(Vector3(16, 17, 18)))");
  741. script->Execute("basisX, basisY, basisZ = m1:GetBasis()");
  742. script->Execute("AZTestAssert(basisX:IsClose(Vector3(10, 11, 12)))");
  743. script->Execute("AZTestAssert(basisY:IsClose(Vector3(13, 14, 15)))");
  744. script->Execute("AZTestAssert(basisZ:IsClose(Vector3(16, 17, 18)))");
  745. script->Execute("m1:SetBasis(Vector3(1, 2, 3), Vector3(4, 5, 6), Vector3(7, 8, 9))");
  746. script->Execute("AZTestAssert(m1.basisX:IsClose(Vector3(1, 2, 3)))");
  747. script->Execute("AZTestAssert(m1.basisY:IsClose(Vector3(4, 5, 6)))");
  748. script->Execute("AZTestAssert(m1.basisZ:IsClose(Vector3(7, 8, 9)))");
  749. //multiplication by matrices
  750. script->Execute("m1:SetRow(0, 1, 2, 3)");
  751. script->Execute("m1:SetRow(1, 4, 5, 6)");
  752. script->Execute("m1:SetRow(2, 7, 8, 9)");
  753. script->Execute("m2 = Matrix3x3()");
  754. script->Execute("m2:SetRow(0, 7, 8, 9)");
  755. script->Execute("m2:SetRow(1, 10, 11, 12)");
  756. script->Execute("m2:SetRow(2, 13, 14, 15)");
  757. script->Execute("m3 = m1 * m2");
  758. script->Execute("AZTestAssert(m3:GetRow(0):IsClose(Vector3(66, 72, 78)))");
  759. script->Execute("AZTestAssert(m3:GetRow(1):IsClose(Vector3(156, 171, 186)))");
  760. script->Execute("AZTestAssert(m3:GetRow(2):IsClose(Vector3(246, 270, 294)))");
  761. script->Execute("m3 = m1:TransposedMultiply(m2)");
  762. script->Execute("AZTestAssert(m3:GetRow(0):IsClose(Vector3(138, 150, 162)))");
  763. script->Execute("AZTestAssert(m3:GetRow(1):IsClose(Vector3(168, 183, 198)))");
  764. script->Execute("AZTestAssert(m3:GetRow(2):IsClose(Vector3(198, 216, 234)))");
  765. //multiplication by vectors
  766. script->Execute("AZTestAssert((m1*Vector3(1, 2, 3)):IsClose(Vector3(14, 32, 50)))");
  767. //other matrix operations
  768. script->Execute("m3 = m1 + m2");
  769. script->Execute("AZTestAssert(m3:GetRow(0):IsClose(Vector3(8, 10, 12)))");
  770. script->Execute("AZTestAssert(m3:GetRow(1):IsClose(Vector3(14, 16, 18)))");
  771. script->Execute("AZTestAssert(m3:GetRow(2):IsClose(Vector3(20, 22, 24)))");
  772. script->Execute("m3 = m1 - m2");
  773. script->Execute("AZTestAssert(m3:GetRow(0):IsClose(Vector3(-6, -6, -6)))");
  774. script->Execute("AZTestAssert(m3:GetRow(1):IsClose(Vector3(-6, -6, -6)))");
  775. script->Execute("AZTestAssert(m3:GetRow(2):IsClose(Vector3(-6, -6, -6)))");
  776. script->Execute("m3 = m1 * 2");
  777. script->Execute("AZTestAssert(m3:GetRow(0):IsClose(Vector3(2, 4, 6)))");
  778. script->Execute("AZTestAssert(m3:GetRow(1):IsClose(Vector3(8, 10, 12)))");
  779. script->Execute("AZTestAssert(m3:GetRow(2):IsClose(Vector3(14, 16, 18)))");
  780. //TODO float * matrix is not functional
  781. //script->Execute("m3 = 2 * m1");
  782. //script->Execute("AZTestAssert(m3:GetRow(0):IsClose(Vector3(2, 4, 6)))");
  783. //script->Execute("AZTestAssert(m3:GetRow(1):IsClose(Vector3(8, 10, 12)))");
  784. //script->Execute("AZTestAssert(m3:GetRow(2):IsClose(Vector3(14, 16, 18)))");
  785. script->Execute("m3 = m1 / 0.5");
  786. script->Execute("AZTestAssert(m3:GetRow(0):IsClose(Vector3(2, 4, 6)))");
  787. script->Execute("AZTestAssert(m3:GetRow(1):IsClose(Vector3(8, 10, 12)))");
  788. script->Execute("AZTestAssert(m3:GetRow(2):IsClose(Vector3(14, 16, 18)))");
  789. script->Execute("m3 = -m1");
  790. script->Execute("AZTestAssert(m3:GetRow(0):IsClose(Vector3(-1, -2, -3)))");
  791. script->Execute("AZTestAssert(m3:GetRow(1):IsClose(Vector3(-4, -5, -6)))");
  792. script->Execute("AZTestAssert(m3:GetRow(2):IsClose(Vector3(-7, -8, -9)))");
  793. //transpose
  794. script->Execute("m1:SetRow(0, 1, 2, 3)");
  795. script->Execute("m1:SetRow(1, 4, 5, 6)");
  796. script->Execute("m1:SetRow(2, 7, 8, 9)");
  797. script->Execute("m2 = m1:GetTranspose()");
  798. script->Execute("AZTestAssert(m2:GetRow(0):IsClose(Vector3(1, 4, 7)))");
  799. script->Execute("AZTestAssert(m2:GetRow(1):IsClose(Vector3(2, 5, 8)))");
  800. script->Execute("AZTestAssert(m2:GetRow(2):IsClose(Vector3(3, 6, 9)))");
  801. script->Execute("m2 = m1:Clone()");
  802. script->Execute("m2:Transpose()");
  803. script->Execute("AZTestAssert(m2:GetRow(0):IsClose(Vector3(1, 4, 7)))");
  804. script->Execute("AZTestAssert(m2:GetRow(1):IsClose(Vector3(2, 5, 8)))");
  805. script->Execute("AZTestAssert(m2:GetRow(2):IsClose(Vector3(3, 6, 9)))");
  806. //test fast inverse, orthogonal matrix only
  807. script->Execute("m1 = Matrix3x3.CreateRotationX(1)");
  808. script->Execute("AZTestAssert((m1*m1:GetInverseFast()):IsClose(Matrix3x3.CreateIdentity(), 0.02))");
  809. script->Execute("m2 = Matrix3x3.CreateRotationZ(2) * Matrix3x3.CreateRotationX(1)");
  810. script->Execute("m3 = m2:GetInverseFast()");
  811. // allow a little bigger threshold, because of the 2 rot matrices(sin, cos differences)
  812. script->Execute("AZTestAssert((m2*m3):IsClose(Matrix3x3.CreateIdentity(), 0.1))");
  813. script->Execute("AZTestAssert(m3:GetRow(0):IsClose(Vector3(-0.420, 0.909, 0), 0.06))");
  814. script->Execute("AZTestAssert(m3:GetRow(1):IsClose(Vector3(-0.493, -0.228, 0.841), 0.06))");
  815. script->Execute("AZTestAssert(m3:GetRow(2):IsClose(Vector3(0.765, 0.353, 0.542), 0.06))");
  816. //test full inverse, any arbitrary matrix
  817. script->Execute("m1:SetRow(0, -1, 2, 3)");
  818. script->Execute("m1:SetRow(1, 4, 5, 6)");
  819. script->Execute("m1:SetRow(2, 7, 8, -9)");
  820. script->Execute("AZTestAssert((m1*m1:GetInverseFull()):IsClose(Matrix3x3.CreateIdentity()))");
  821. //scale access
  822. script->Execute("m1 = Matrix3x3.CreateRotationX(Math.DegToRad(40)) * Matrix3x3.CreateScale(Vector3(2, 3, 4))");
  823. script->Execute("AZTestAssert(m1:RetrieveScale():IsClose(Vector3(2, 3, 4)))");
  824. script->Execute("AZTestAssert(m1:ExtractScale():IsClose(Vector3(2, 3, 4)))");
  825. script->Execute("AZTestAssert(m1:RetrieveScale():IsClose(Vector3.CreateOne()))");
  826. script->Execute("m1:MultiplyByScale(Vector3(3, 4, 5))");
  827. script->Execute("AZTestAssert(m1:RetrieveScale():IsClose(Vector3(3, 4, 5)))");
  828. //polar decomposition
  829. script->Execute("m1 = Matrix3x3.CreateRotationX(Math.DegToRad(30))");
  830. script->Execute("m2 = Matrix3x3.CreateScale(Vector3(5, 6, 7))");
  831. script->Execute("m3 = m1 * m2");
  832. script->Execute("AZTestAssert(m3:GetPolarDecomposition():IsClose(m1))");
  833. script->Execute("m4, m5 = m3:GetPolarDecomposition()");
  834. script->Execute("AZTestAssert((m4*m5):IsClose(m3))");
  835. script->Execute("AZTestAssert(m4:IsClose(m1))");
  836. script->Execute("AZTestAssert(m5:IsClose(m2, 0.01))");
  837. //orthogonalize
  838. script->Execute("m1 = Matrix3x3.CreateRotationX(Math.DegToRad(30)) * Matrix3x3.CreateScale(Vector3(2, 3, 4))");
  839. script->Execute("m1:SetElement(0, 1, 0.2)");
  840. script->Execute("m2 = m1:GetOrthogonalized()");
  841. script->Execute("AZTestAssertFloatClose(m2:GetRow(0):GetLength(),1)");
  842. script->Execute("AZTestAssertFloatClose(m2:GetRow(1):GetLength(),1)");
  843. script->Execute("AZTestAssertFloatClose(m2:GetRow(2):GetLength(),1)");
  844. script->Execute("AZTestAssertFloatClose(m2:GetRow(0):Dot(m2:GetRow(1)),0)");
  845. script->Execute("AZTestAssertFloatClose(m2:GetRow(0):Dot(m2:GetRow(2)),0)");
  846. script->Execute("AZTestAssertFloatClose(m2:GetRow(1):Dot(m2:GetRow(2)),0)");
  847. script->Execute("AZTestAssert(m2:GetRow(0):Cross(m2:GetRow(1)):IsClose(m2:GetRow(2)))");
  848. script->Execute("AZTestAssert(m2:GetRow(1):Cross(m2:GetRow(2)):IsClose(m2:GetRow(0)))");
  849. script->Execute("AZTestAssert(m2:GetRow(2):Cross(m2:GetRow(0)):IsClose(m2:GetRow(1)))");
  850. script->Execute("m1:Orthogonalize()");
  851. script->Execute("AZTestAssert(m1:IsClose(m2))");
  852. //orthogonal
  853. script->Execute("m1 = Matrix3x3.CreateRotationX(Math.DegToRad(30))");
  854. script->Execute("AZTestAssert(m1:IsOrthogonal(0.05))");
  855. script->Execute("m1:SetRow(1, m1:GetRow(1)*2)");
  856. script->Execute("AZTestAssert( not m1:IsOrthogonal(0.05))");
  857. script->Execute("m1 = Matrix3x3.CreateRotationX(Math.DegToRad(30))");
  858. script->Execute("m1:SetRow(1, Vector3(0, 1, 0))");
  859. script->Execute("AZTestAssert( not m1:IsOrthogonal(0.05))");
  860. //IsClose
  861. script->Execute("m1 = Matrix3x3.CreateRotationX(Math.DegToRad(30))");
  862. script->Execute("m2 = m1:Clone()");
  863. script->Execute("AZTestAssert(m1:IsClose(m2))");
  864. script->Execute("m2:SetElement(0, 0, 2)");
  865. script->Execute("AZTestAssert( not m1:IsClose(m2))");
  866. script->Execute("m2 = m1:Clone()");
  867. script->Execute("m2:SetElement(0, 2, 2)");
  868. script->Execute("AZTestAssert( not m1:IsClose(m2))");
  869. //get diagonal
  870. script->Execute("m1:SetRow(0, 1, 2, 3)");
  871. script->Execute("m1:SetRow(1, 4, 5, 6)");
  872. script->Execute("m1:SetRow(2, 7, 8, 9)");
  873. script->Execute("AZTestAssert(m1:GetDiagonal() == Vector3(1, 5, 9))");
  874. //determinant
  875. script->Execute("m1:SetRow(0, -1, 2, 3)");
  876. script->Execute("m1:SetRow(1, 4, 5, 6)");
  877. script->Execute("m1:SetRow(2, 7, 8, -9)");
  878. script->Execute("AZTestAssertFloatClose(m1:GetDeterminant(),240)");
  879. //adjugate
  880. script->Execute("m1:SetRow(0, 1, 2, 3)");
  881. script->Execute("m1:SetRow(1, 4, 5, 6)");
  882. script->Execute("m1:SetRow(2, 7, 8, 9)");
  883. script->Execute("m2 = m1:GetAdjugate()");
  884. script->Execute("AZTestAssert(m2:GetRow(0):IsClose(Vector3(-3, 6, -3)))");
  885. script->Execute("AZTestAssert(m2:GetRow(1):IsClose(Vector3(6, -12, 6)))");
  886. script->Execute("AZTestAssert(m2:GetRow(2):IsClose(Vector3(-3, 6, -3)))");
  887. }
  888. TEST_F(ScriptMathTest, LuaMatrix4x4Test)
  889. {
  890. script->Execute("testIndices = { 0, 1, 2, 3 }");
  891. ////create functions
  892. script->Execute("m1 = Matrix4x4.CreateIdentity()");
  893. script->Execute("AZTestAssert(m1:GetRow(0) == Vector4(1, 0, 0, 0))");
  894. script->Execute("AZTestAssert(m1:GetRow(1) == Vector4(0, 1, 0, 0))");
  895. script->Execute("AZTestAssert(m1:GetRow(2) == Vector4(0, 0, 1, 0))");
  896. script->Execute("AZTestAssert(m1:GetRow(3) == Vector4(0, 0, 0, 1))");
  897. script->Execute("m1 = Matrix4x4.CreateZero()");
  898. script->Execute("AZTestAssert(m1:GetRow(0) == Vector4(0))");
  899. script->Execute("AZTestAssert(m1:GetRow(1) == Vector4(0))");
  900. script->Execute("AZTestAssert(m1:GetRow(2) == Vector4(0))");
  901. script->Execute("AZTestAssert(m1:GetRow(3) == Vector4(0))");
  902. script->Execute("m1 = Matrix4x4.CreateFromValue(2)");
  903. script->Execute("AZTestAssert(m1:GetRow(0) == Vector4(2))");
  904. script->Execute("AZTestAssert(m1:GetRow(1) == Vector4(2))");
  905. script->Execute("AZTestAssert(m1:GetRow(2) == Vector4(2))");
  906. script->Execute("AZTestAssert(m1:GetRow(3) == Vector4(2))");
  907. script->Execute("m1 = Matrix4x4.CreateRotationX(Math.DegToRad(30))");
  908. script->Execute("AZTestAssert(m1:GetRow(0):IsClose(Vector4(1, 0, 0, 0)))");
  909. script->Execute("AZTestAssert(m1:GetRow(1):IsClose(Vector4(0, 0.866, -0.5, 0)))");
  910. script->Execute("AZTestAssert(m1:GetRow(2):IsClose(Vector4(0, 0.5, 0.866, 0)))");
  911. script->Execute("AZTestAssert(m1:GetRow(3):IsClose(Vector4(0, 0, 0, 1)))");
  912. script->Execute("m1 = Matrix4x4.CreateRotationY(Math.DegToRad(30))");
  913. script->Execute("AZTestAssert(m1:GetRow(0):IsClose(Vector4(0.866, 0, 0.5, 0)))");
  914. script->Execute("AZTestAssert(m1:GetRow(1):IsClose(Vector4(0, 1, 0, 0)))");
  915. script->Execute("AZTestAssert(m1:GetRow(2):IsClose(Vector4(-0.5, 0, 0.866, 0)))");
  916. script->Execute("AZTestAssert(m1:GetRow(3):IsClose(Vector4(0, 0, 0, 1)))");
  917. script->Execute("m1 = Matrix4x4.CreateRotationZ(Math.DegToRad(30))");
  918. script->Execute("AZTestAssert(m1:GetRow(0):IsClose(Vector4(0.866, -0.5, 0, 0)))");
  919. script->Execute("AZTestAssert(m1:GetRow(1):IsClose(Vector4(0.5, 0.866, 0, 0)))");
  920. script->Execute("AZTestAssert(m1:GetRow(2):IsClose(Vector4(0, 0, 1, 0)))");
  921. script->Execute("AZTestAssert(m1:GetRow(3):IsClose(Vector4(0, 0, 0, 1)))");
  922. script->Execute("m1 = Matrix4x4.CreateFromQuaternion(Quaternion.CreateRotationX(Math.DegToRad(30)))");
  923. script->Execute("AZTestAssert(m1:GetRow(0):IsClose(Vector4(1, 0, 0, 0)))");
  924. script->Execute("AZTestAssert(m1:GetRow(1):IsClose(Vector4(0, 0.866, -0.5, 0)))");
  925. script->Execute("AZTestAssert(m1:GetRow(2):IsClose(Vector4(0, 0.5, 0.866, 0)))");
  926. script->Execute("AZTestAssert(m1:GetRow(3):IsClose(Vector4(0, 0, 0, 1)))");
  927. script->Execute("m1 = Matrix4x4.CreateFromQuaternionAndTranslation(Quaternion.CreateRotationX(Math.DegToRad(30)), Vector3(1, 2, 3))");
  928. script->Execute("AZTestAssert(m1:GetRow(0):IsClose(Vector4(1, 0, 0, 1)))");
  929. script->Execute("AZTestAssert(m1:GetRow(1):IsClose(Vector4(0, 0.866, -0.5, 2)))");
  930. script->Execute("AZTestAssert(m1:GetRow(2):IsClose(Vector4(0, 0.5, 0.866, 3)))");
  931. script->Execute("AZTestAssert(m1:GetRow(3):IsClose(Vector4(0, 0, 0, 1)))");
  932. script->Execute("m1 = Matrix4x4.CreateScale(Vector3(1, 2, 3))");
  933. script->Execute("AZTestAssert(m1:GetRow(0) == Vector4(1, 0, 0, 0))");
  934. script->Execute("AZTestAssert(m1:GetRow(1) == Vector4(0, 2, 0, 0))");
  935. script->Execute("AZTestAssert(m1:GetRow(2) == Vector4(0, 0, 3, 0))");
  936. script->Execute("AZTestAssert(m1:GetRow(3) == Vector4(0, 0, 0, 1))");
  937. script->Execute("m1 = Matrix4x4.CreateDiagonal(Vector4(2, 3, 4, 5))");
  938. script->Execute("AZTestAssert(m1:GetRow(0) == Vector4(2, 0, 0, 0))");
  939. script->Execute("AZTestAssert(m1:GetRow(1) == Vector4(0, 3, 0, 0))");
  940. script->Execute("AZTestAssert(m1:GetRow(2) == Vector4(0, 0, 4, 0))");
  941. script->Execute("AZTestAssert(m1:GetRow(3) == Vector4(0, 0, 0, 5))");
  942. script->Execute("m1 = Matrix4x4.CreateTranslation(Vector3(1, 2, 3))");
  943. script->Execute("AZTestAssert(m1:GetRow(0) == Vector4(1, 0, 0, 1))");
  944. script->Execute("AZTestAssert(m1:GetRow(1) == Vector4(0, 1, 0, 2))");
  945. script->Execute("AZTestAssert(m1:GetRow(2) == Vector4(0, 0, 1, 3))");
  946. script->Execute("AZTestAssert(m1:GetRow(3) == Vector4(0, 0, 0, 1))");
  947. script->Execute("m1 = Matrix4x4.CreateProjection(Math.DegToRad(30), 16 / 9, 1, 1000)");
  948. script->Execute("AZTestAssert(m1:GetRow(0):IsClose(Vector4(-2.0993, 0, 0, 0)))");
  949. script->Execute("AZTestAssert(m1:GetRow(1):IsClose(Vector4(0, 3.732, 0, 0)))");
  950. script->Execute("AZTestAssert(m1:GetRow(2):IsClose(Vector4(0, 0, 1.002, -2.002)))");
  951. script->Execute("AZTestAssert(m1:GetRow(3):IsClose(Vector4(0, 0, 1, 0)))");
  952. script->Execute("m1 = Matrix4x4.CreateProjectionFov(Math.DegToRad(30), Math.DegToRad(60), 1, 1000)");
  953. script->Execute("AZTestAssert(m1:GetRow(0):IsClose(Vector4(-3.732, 0, 0, 0)))");
  954. script->Execute("AZTestAssert(m1:GetRow(1):IsClose(Vector4(0, 1.732, 0, 0)))");
  955. script->Execute("AZTestAssert(m1:GetRow(2):IsClose(Vector4(0, 0, 1.002, -2.002)))");
  956. script->Execute("AZTestAssert(m1:GetRow(3):IsClose(Vector4(0, 0, 1, 0)))");
  957. script->Execute("m1 = Matrix4x4.CreateProjectionOffset(0.5, 1, 0, 0.5, 1, 1000)");
  958. script->Execute("AZTestAssert(m1:GetRow(0):IsClose(Vector4(-4, 0, -3, 0)))");
  959. script->Execute("AZTestAssert(m1:GetRow(1):IsClose(Vector4(0, 4, -1, 0)))");
  960. script->Execute("AZTestAssert(m1:GetRow(2):IsClose(Vector4(0, 0, 1.002, -2.002)))");
  961. script->Execute("AZTestAssert(m1:GetRow(3):IsClose(Vector4(0, 0, 1, 0)))");
  962. ////element access
  963. script->Execute("m1 = Matrix4x4.CreateRotationX(Math.DegToRad(30))");
  964. script->Execute("AZTestAssertFloatClose(m1:GetElement(1, 2), -0.5)");
  965. script->Execute("AZTestAssertFloatClose(m1:GetElement(2, 2), 0.866)");
  966. script->Execute("m1:SetElement(2, 1, 5)");
  967. script->Execute("AZTestAssert(m1:GetElement(2, 1) == 5)");
  968. ////row access
  969. script->Execute("m1 = Matrix4x4.CreateRotationX(Math.DegToRad(30))");
  970. script->Execute("AZTestAssert(m1:GetRow(2):IsClose(Vector4(0, 0.5, 0.866, 0)))");
  971. script->Execute("AZTestAssert(m1:GetRowAsVector3(2):IsClose(Vector3(0, 0.5, 0.866)))");
  972. script->Execute("m1:SetRow(0, 1, 2, 3, 4)");
  973. script->Execute("AZTestAssert(m1:GetRow(0):IsClose(Vector4(1, 2, 3, 4)))");
  974. script->Execute("m1:SetRow(1, Vector3(5, 6, 7), 8)");
  975. script->Execute("AZTestAssert(m1:GetRow(1):IsClose(Vector4(5, 6, 7, 8)))");
  976. script->Execute("m1:SetRow(2, Vector4(3, 4, 5, 6))");
  977. script->Execute("AZTestAssert(m1:GetRow(2):IsClose(Vector4(3, 4, 5, 6)))");
  978. script->Execute("m1:SetRow(3, Vector4(7, 8, 9, 10))");
  979. script->Execute("AZTestAssert(m1:GetRow(3):IsClose(Vector4(7, 8, 9, 10)))");
  980. ////test GetRow with non-constant, we have different implementations for constants and variables
  981. script->Execute("AZTestAssert(m1:GetRow(testIndices[1]):IsClose(Vector4(1, 2, 3, 4)))");
  982. script->Execute("AZTestAssert(m1:GetRow(testIndices[2]):IsClose(Vector4(5, 6, 7, 8)))");
  983. script->Execute("AZTestAssert(m1:GetRow(testIndices[3]):IsClose(Vector4(3, 4, 5, 6)))");
  984. script->Execute("AZTestAssert(m1:GetRow(testIndices[4]):IsClose(Vector4(7, 8, 9, 10)))");
  985. ////column access
  986. script->Execute("m1 = Matrix4x4.CreateRotationX(Math.DegToRad(30))");
  987. script->Execute("AZTestAssert(m1:GetColumn(1):IsClose(Vector4(0, 0.866, 0.5, 0)))");
  988. script->Execute("m1:SetColumn(3, 1, 2, 3, 4)");
  989. script->Execute("AZTestAssert(m1:GetColumn(3):IsClose(Vector4(1, 2, 3, 4)))");
  990. script->Execute("AZTestAssert(m1:GetColumnAsVector3(3):IsClose(Vector3(1, 2, 3)))");
  991. script->Execute("AZTestAssert(m1:GetRow(0):IsClose(Vector4(1, 0, 0, 1)))"); //checking all components in case others get messed up with the shuffling
  992. script->Execute("AZTestAssert(m1:GetRow(1):IsClose(Vector4(0, 0.866, -0.5, 2)))");
  993. script->Execute("AZTestAssert(m1:GetRow(2):IsClose(Vector4(0, 0.5, 0.866, 3)))");
  994. script->Execute("AZTestAssert(m1:GetRow(3):IsClose(Vector4(0, 0, 0, 4)))");
  995. script->Execute("m1:SetColumn(0, Vector4(2, 3, 4, 5))");
  996. script->Execute("AZTestAssert(m1:GetColumn(0):IsClose(Vector4(2, 3, 4, 5)))");
  997. script->Execute("AZTestAssert(m1:GetRow(0):IsClose(Vector4(2, 0, 0, 1)))");
  998. script->Execute("AZTestAssert(m1:GetRow(1):IsClose(Vector4(3, 0.866, -0.5, 2)))");
  999. script->Execute("AZTestAssert(m1:GetRow(2):IsClose(Vector4(4, 0.5, 0.866, 3)))");
  1000. script->Execute("AZTestAssert(m1:GetRow(3):IsClose(Vector4(5, 0, 0, 4)))");
  1001. ////test GetColumn with non-constant, we have different implementations for constants and variables
  1002. script->Execute("AZTestAssert(m1:GetColumn(testIndices[1]):IsClose(Vector4(2, 3, 4, 5)))");
  1003. script->Execute("AZTestAssert(m1:GetColumn(testIndices[2]):IsClose(Vector4(0, 0.866, 0.5, 0)))");
  1004. script->Execute("AZTestAssert(m1:GetColumn(testIndices[3]):IsClose(Vector4(0, -0.5, 0.866, 0)))");
  1005. script->Execute("AZTestAssert(m1:GetColumn(testIndices[4]):IsClose(Vector4(1, 2, 3, 4)))");
  1006. ////translation access
  1007. script->Execute("m1 = Matrix4x4.CreateTranslation(Vector3(5, 6, 7))");
  1008. script->Execute("AZTestAssert(m1:GetTranslation():IsClose(Vector3(5, 6, 7)))");
  1009. script->Execute("m1:SetTranslation(1, 2, 3)");
  1010. script->Execute("AZTestAssert(m1:GetTranslation():IsClose(Vector3(1, 2, 3)))");
  1011. script->Execute("m1:SetTranslation(Vector3(2, 3, 4))");
  1012. script->Execute("AZTestAssert(m1:GetTranslation():IsClose(Vector3(2, 3, 4)))");
  1013. script->Execute("AZTestAssert(m1:GetTranslation():IsClose(Vector3(2, 3, 4)))");
  1014. script->Execute("m1:SetTranslation(4, 5, 6)");
  1015. script->Execute("AZTestAssert(m1:GetTranslation():IsClose(Vector3(4, 5, 6)))");
  1016. script->Execute("m1:SetTranslation(Vector3(2, 3, 4))");
  1017. script->Execute("AZTestAssert(m1:GetTranslation():IsClose(Vector3(2, 3, 4)))");
  1018. ////multiplication by matrices
  1019. script->Execute("m1:SetRow(0, 1, 2, 3, 4)");
  1020. script->Execute("m1:SetRow(1, 5, 6, 7, 8)");
  1021. script->Execute("m1:SetRow(2, 9, 10, 11, 12)");
  1022. script->Execute("m1:SetRow(3, 13, 14, 15, 16)");
  1023. script->Execute("m2 = Matrix4x4()");
  1024. script->Execute("m2:SetRow(0, 7, 8, 9, 10)");
  1025. script->Execute("m2:SetRow(1, 11, 12, 13, 14)");
  1026. script->Execute("m2:SetRow(2, 15, 16, 17, 18)");
  1027. script->Execute("m2:SetRow(3, 19, 20, 21, 22)");
  1028. script->Execute("m3 = m1 * m2");
  1029. script->Execute("AZTestAssert(m3:GetRow(0):IsClose(Vector4(150, 160, 170, 180)))");
  1030. script->Execute("AZTestAssert(m3:GetRow(1):IsClose(Vector4(358, 384, 410, 436)))");
  1031. script->Execute("AZTestAssert(m3:GetRow(2):IsClose(Vector4(566, 608, 650, 692)))");
  1032. script->Execute("AZTestAssert(m3:GetRow(3):IsClose(Vector4(774, 832, 890, 948)))");
  1033. ////multiplication by vectors
  1034. script->Execute("AZTestAssert((m1*Vector3(1, 2, 3)):IsClose(Vector3(18, 46, 74)))");
  1035. script->Execute("AZTestAssert((m1*Vector4(1, 2, 3, 4)):IsClose(Vector4(30, 70, 110, 150)))");
  1036. script->Execute("AZTestAssert(m1:TransposedMultiply3x3(Vector3(1, 2, 3)):IsClose(Vector3(38, 44, 50)))");
  1037. script->Execute("AZTestAssert(m1:Multiply3x3(Vector3(1, 2, 3)):IsClose(Vector3(14, 38, 62)))");
  1038. ////transpose
  1039. script->Execute("m1:SetRow(0, 1, 2, 3, 4)");
  1040. script->Execute("m1:SetRow(1, 5, 6, 7, 8)");
  1041. script->Execute("m1:SetRow(2, 9, 10, 11, 12)");
  1042. script->Execute("m1:SetRow(3, 13, 14, 15, 16)");
  1043. script->Execute("m2 = m1:GetTranspose()");
  1044. script->Execute("AZTestAssert(m2:GetRow(0):IsClose(Vector4(1, 5, 9, 13)))");
  1045. script->Execute("AZTestAssert(m2:GetRow(1):IsClose(Vector4(2, 6, 10, 14)))");
  1046. script->Execute("AZTestAssert(m2:GetRow(2):IsClose(Vector4(3, 7, 11, 15)))");
  1047. script->Execute("AZTestAssert(m2:GetRow(3):IsClose(Vector4(4, 8, 12, 16)))");
  1048. script->Execute("m2 = m1:Clone()");
  1049. script->Execute("m2:Transpose()");
  1050. script->Execute("AZTestAssert(m2:GetRow(0):IsClose(Vector4(1, 5, 9, 13)))");
  1051. script->Execute("AZTestAssert(m2:GetRow(1):IsClose(Vector4(2, 6, 10, 14)))");
  1052. script->Execute("AZTestAssert(m2:GetRow(2):IsClose(Vector4(3, 7, 11, 15)))");
  1053. script->Execute("AZTestAssert(m2:GetRow(3):IsClose(Vector4(4, 8, 12, 16)))");
  1054. ////test fast inverse, orthogonal matrix with translation only
  1055. script->Execute("m1 = Matrix4x4.CreateRotationX(1)");
  1056. script->Execute("m1:SetTranslation(Vector3(10, -3, 5))");
  1057. script->Execute("AZTestAssert((m1*m1:GetInverseFast()):IsClose(Matrix4x4.CreateIdentity(), 0.02))");
  1058. script->Execute("m2 = Matrix4x4.CreateRotationZ(2) * Matrix4x4.CreateRotationX(1)");
  1059. script->Execute("m2:SetTranslation(Vector3(-5, 4.2, -32))");
  1060. script->Execute("m3 = m2:GetInverseFast()");
  1061. //// allow a little bigger threshold, because of the 2 rot matrices (sin,cos differences)
  1062. script->Execute("AZTestAssert((m2*m3):IsClose(Matrix4x4.CreateIdentity(), 0.1))");
  1063. script->Execute("AZTestAssert(m3:GetRow(0):IsClose(Vector4(-0.420, 0.909, 0, -5.920), 0.06))");
  1064. script->Execute("AZTestAssert(m3:GetRow(1):IsClose(Vector4(-0.493, -0.228, 0.841, 25.418), 0.06))");
  1065. script->Execute("AZTestAssert(m3:GetRow(2):IsClose(Vector4(0.765, 0.353, 0.542, 19.703), 0.06))");
  1066. script->Execute("AZTestAssert(m3:GetRow(3):IsClose(Vector4(0, 0, 0, 1)))");
  1067. ////test transform inverse, should handle non-orthogonal matrices, last row must still be (0,0,0,1) though
  1068. script->Execute("m1:SetElement(0, 1, 23.1234)");
  1069. script->Execute("AZTestAssert((m1*m1:GetInverseTransform()):IsClose(Matrix4x4.CreateIdentity(), 0.01))");
  1070. ////test full inverse, any arbitrary matrix
  1071. script->Execute("m1:SetRow(0, -1, 2, 3, 4)");
  1072. script->Execute("m1:SetRow(1, 3, 4, 5, 6)");
  1073. script->Execute("m1:SetRow(2, 9, 10, 11, 12)");
  1074. script->Execute("m1:SetRow(3, 13, 14, 15, -16)");
  1075. script->Execute("AZTestAssert((m1*m1:GetInverseFull()):IsClose(Matrix4x4.CreateIdentity()))");
  1076. ////IsClose
  1077. script->Execute("m1 = Matrix4x4.CreateRotationX(Math.DegToRad(30))");
  1078. script->Execute("m2 = m1:Clone()");
  1079. script->Execute("AZTestAssert(m1:IsClose(m2))");
  1080. script->Execute("m2:SetElement(0, 0, 2)");
  1081. script->Execute("AZTestAssert( not m1:IsClose(m2))");
  1082. script->Execute("m2 = m1:Clone()");
  1083. script->Execute("m2:SetElement(0, 3, 2)");
  1084. script->Execute("AZTestAssert( not m1:IsClose(m2))");
  1085. ////set rotation part
  1086. script->Execute("m1 = Matrix4x4.CreateTranslation(Vector3(1, 2, 3))");
  1087. script->Execute("m1:SetRow(3, 5, 6, 7, 8)");
  1088. script->Execute("m1:SetRotationPartFromQuaternion(Quaternion.CreateRotationX(Math.DegToRad(30)))");
  1089. script->Execute("AZTestAssert(m1:GetRow(0):IsClose(Vector4(1, 0, 0, 1)))");
  1090. script->Execute("AZTestAssert(m1:GetRow(1):IsClose(Vector4(0, 0.866, -0.5, 2)))");
  1091. script->Execute("AZTestAssert(m1:GetRow(2):IsClose(Vector4(0, 0.5, 0.866, 3)))");
  1092. script->Execute("AZTestAssert(m1:GetRow(3):IsClose(Vector4(5, 6, 7, 8)))");
  1093. ////get diagonal
  1094. script->Execute("m1:SetRow(0, 1, 2, 3, 4)");
  1095. script->Execute("m1:SetRow(1, 5, 6, 7, 8)");
  1096. script->Execute("m1:SetRow(2, 9, 10, 11, 12)");
  1097. script->Execute("m1:SetRow(3, 13, 14, 15, 16)");
  1098. script->Execute("AZTestAssert(m1:GetDiagonal() == Vector4(1, 6, 11, 16))");
  1099. }
  1100. TEST_F(ScriptMathTest, LuaTransformTest)
  1101. {
  1102. script->Execute("testIndices = { 0, 1, 2, 3 }");
  1103. ////create functions
  1104. script->Execute("t1 = Transform.CreateIdentity()");
  1105. script->Execute("AZTestAssert(t1:TransformVector(Vector3(1, 0, 0)):IsClose(Vector3(1, 0, 0)))");
  1106. script->Execute("AZTestAssert(t1:TransformVector(Vector3(0, 1, 0)):IsClose(Vector3(0 ,1, 0)))");
  1107. script->Execute("AZTestAssert(t1:TransformVector(Vector3(0, 0, 1)):IsClose(Vector3(0, 0, 1)))");
  1108. script->Execute("t1 = Transform.CreateRotationX(Math.DegToRad(30))");
  1109. script->Execute("AZTestAssert(t1:TransformVector(Vector3(1, 0, 0)):IsClose(Vector3(1, 0, 0)))");
  1110. script->Execute("AZTestAssert(t1:TransformVector(Vector3(0, 1, 0)):IsClose(Vector3(0, 0.866, 0.5)))");
  1111. script->Execute("AZTestAssert(t1:TransformVector(Vector3(0, 0, 1)):IsClose(Vector3(0, -0.5, 0.866)))");
  1112. script->Execute("t1 = Transform.CreateRotationY(Math.DegToRad(30))");
  1113. script->Execute("AZTestAssert(t1:TransformVector(Vector3(1, 0, 0)):IsClose(Vector3(0.866, 0, -0.5)))");
  1114. script->Execute("AZTestAssert(t1:TransformVector(Vector3(0, 1, 0)):IsClose(Vector3(0, 1, 0)))");
  1115. script->Execute("AZTestAssert(t1:TransformVector(Vector3(0, 0, 1)):IsClose(Vector3(0.5, 0, 0.866)))");
  1116. script->Execute("t1 = Transform.CreateRotationZ(Math.DegToRad(30))");
  1117. script->Execute("AZTestAssert(t1:TransformVector(Vector3(1, 0, 0)):IsClose(Vector3(0.866, 0.5, 0)))");
  1118. script->Execute("AZTestAssert(t1:TransformVector(Vector3(0, 1, 0)):IsClose(Vector3(-0.5, 0.866, 0)))");
  1119. script->Execute("AZTestAssert(t1:TransformVector(Vector3(0, 0, 1)):IsClose(Vector3(0, 0, 1)))");
  1120. script->Execute("t1 = Transform.CreateFromQuaternion(Quaternion.CreateRotationX(Math.DegToRad(30)))");
  1121. script->Execute("AZTestAssert(t1:TransformVector(Vector3(1, 0, 0)):IsClose(Vector3(1, 0, 0)))");
  1122. script->Execute("AZTestAssert(t1:TransformVector(Vector3(0, 1, 0)):IsClose(Vector3(0, 0.866, 0.5)))");
  1123. script->Execute("AZTestAssert(t1:TransformVector(Vector3(0, 0, 1)):IsClose(Vector3(0, -0.5, 0.866)))");
  1124. script->Execute("t1 = Transform.CreateFromQuaternionAndTranslation(Quaternion.CreateRotationX(Math.DegToRad(30)), Vector3(1, 2, 3))");
  1125. script->Execute("AZTestAssert(t1:TransformVector(Vector3(1, 0, 0)):IsClose(Vector3(1, 0, 0)))");
  1126. script->Execute("AZTestAssert(t1:TransformVector(Vector3(0, 1, 0)):IsClose(Vector3(0, 0.866, 0.5)))");
  1127. script->Execute("AZTestAssert(t1:TransformVector(Vector3(0, 0, 1)):IsClose(Vector3(0, -0.5, 0.866)))");
  1128. script->Execute("t1 = Transform.CreateUniformScale(2)");
  1129. script->Execute("AZTestAssert(t1:TransformVector(Vector3(1, 0, 0)):IsClose(Vector3(2, 0, 0)))");
  1130. script->Execute("AZTestAssert(t1:TransformVector(Vector3(0, 1, 0)):IsClose(Vector3(0, 2, 0)))");
  1131. script->Execute("AZTestAssert(t1:TransformVector(Vector3(0, 0, 1)):IsClose(Vector3(0, 0, 2)))");
  1132. script->Execute("t1 = Transform.CreateTranslation(Vector3(1, 2, 3))");
  1133. script->Execute("AZTestAssert(t1:TransformVector(Vector3(1, 0, 0)):IsClose(Vector3(1, 0, 0)))");
  1134. script->Execute("AZTestAssert(t1:TransformVector(Vector3(0, 1, 0)):IsClose(Vector3(0, 1, 0)))");
  1135. script->Execute("AZTestAssert(t1:TransformVector(Vector3(0, 0, 1)):IsClose(Vector3(0, 0, 1)))");
  1136. script->Execute("AZTestAssert(t1:GetTranslation():IsClose(Vector3(1, 2, 3)))");
  1137. ////basis access
  1138. script->Execute("t1 = Transform.CreateRotationX(Math.DegToRad(30))");
  1139. script->Execute("t1:SetTranslation(1, 2, 3)");
  1140. script->Execute("AZTestAssert(t1:GetBasisX():IsClose(Vector3(1, 0, 0)))");
  1141. script->Execute("AZTestAssert(t1:GetBasisY():IsClose(Vector3(0, 0.866, 0.5)))");
  1142. script->Execute("AZTestAssert(t1:GetBasisZ():IsClose(Vector3(0, -0.5, 0.866)))");
  1143. script->Execute("basis0, basis1, basis2, pos = t1:GetBasisAndTranslation()");
  1144. script->Execute("AZTestAssert(basis0:IsClose(Vector3(1, 0, 0)))");
  1145. script->Execute("AZTestAssert(basis1:IsClose(Vector3(0, 0.866, 0.5)))");
  1146. script->Execute("AZTestAssert(basis2:IsClose(Vector3(0, -0.5, 0.866)))");
  1147. script->Execute("AZTestAssert(pos:IsClose(Vector3(1, 2, 3)))");
  1148. ////translation access
  1149. script->Execute("t1 = Transform.CreateTranslation(Vector3(5, 6, 7))");
  1150. script->Execute("AZTestAssert(t1:GetTranslation():IsClose(Vector3(5, 6, 7)))");
  1151. script->Execute("t1:SetTranslation(1, 2, 3)");
  1152. script->Execute("AZTestAssert(t1:GetTranslation():IsClose(Vector3(1, 2, 3)))");
  1153. script->Execute("t1:SetTranslation(Vector3(2, 3, 4))");
  1154. script->Execute("AZTestAssert(t1:GetTranslation():IsClose(Vector3(2, 3, 4)))");
  1155. script->Execute("AZTestAssert(t1:GetTranslation():IsClose(Vector3(2, 3, 4)))");
  1156. script->Execute("t1:SetTranslation(4, 5, 6)");
  1157. script->Execute("AZTestAssert(t1:GetTranslation():IsClose(Vector3(4, 5, 6)))");
  1158. script->Execute("t1:SetTranslation(Vector3(2, 3, 4))");
  1159. script->Execute("AZTestAssert(t1:GetTranslation():IsClose(Vector3(2, 3, 4)))");
  1160. ////multiplication by transforms
  1161. script->Execute("t1 = Transform.CreateRotationX(Math.DegToRad(30))");
  1162. script->Execute("t1:SetTranslation(Vector3(1, 2, 3))");
  1163. script->Execute("t2 = Transform.CreateRotationY(Math.DegToRad(30))");
  1164. script->Execute("t2:SetTranslation(Vector3(2, 3, 4))");
  1165. script->Execute("t3 = t1 * t2");
  1166. script->Execute("AZTestAssert(t3:TransformVector(Vector3(1, 0, 0)):IsClose(Vector3(0.866, 0.25, -0.433)))");
  1167. script->Execute("AZTestAssert(t3:TransformVector(Vector3(0, 1, 0)):IsClose(Vector3(0, 0.866, 0.5)))");
  1168. script->Execute("AZTestAssert(t3:TransformVector(Vector3(0, 0, 1)):IsClose(Vector3(0.5, -0.433, 0.75)))");
  1169. script->Execute("AZTestAssert(t3:GetTranslation():IsClose(Vector3(3, 2.598, 7.964), 0.001))");
  1170. ////multiplication by vectors
  1171. script->Execute("AZTestAssert((t1*Vector3(1, 2, 3)):IsClose(Vector3(2, 2.224, 6.598), 0.02))");
  1172. script->Execute("AZTestAssert((t1*Vector4(1, 2, 3, 4)):IsClose(Vector4(5, 8.224, 15.598, 4), 0.02))");
  1173. script->Execute("AZTestAssert(t1:TransformVector(Vector3(1, 2, 3)):IsClose(Vector3(1, 0.224, 3.598), 0.02))");
  1174. ////test fast inverse, orthogonal matrix with translation only
  1175. script->Execute("t1 = Transform.CreateRotationX(1)");
  1176. script->Execute("t1:SetTranslation(Vector3(10, -3, 5))");
  1177. script->Execute("AZTestAssert((t1*t1:GetInverse()):IsClose(Transform.CreateIdentity(), 0.02))");
  1178. script->Execute("t2 = Transform.CreateRotationZ(2) * Transform.CreateRotationX(1)");
  1179. script->Execute("t2:SetTranslation(Vector3(-5, 4.2, -32))");
  1180. script->Execute("t3 = t2:GetInverse()");
  1181. //// allow a little bigger threshold, because of the 2 rot matrices (sin,cos differences)
  1182. script->Execute("AZTestAssert((t2*t3):IsClose(Transform.CreateIdentity(), 0.1))");
  1183. script->Execute("AZTestAssert(t3:TransformVector(Vector3(1, 0, 0)):IsClose(Vector3(-0.420, -0.493, 0.765), 0.06))");
  1184. script->Execute("AZTestAssert(t3:TransformVector(Vector3(0, 1, 0)):IsClose(Vector3(0.909,-0.228, 0.353), 0.06))");
  1185. script->Execute("AZTestAssert(t3:TransformVector(Vector3(0, 0, 1)):IsClose(Vector3(0, 0.841, 0.542), 0.06))");
  1186. script->Execute("AZTestAssert(t3:GetTranslation():IsClose(Vector3(-5.90, 25.415, 19.645), 0.001))");
  1187. ////test inverse, should handle non-orthogonal matrices
  1188. script->Execute("t1 = Transform.CreateRotationX(1) * Transform.CreateUniformScale(2)");
  1189. script->Execute("AZTestAssert((t1*t1:GetInverse()):IsClose(Transform.CreateIdentity()))");
  1190. ////scale access
  1191. script->Execute("t1 = Transform.CreateRotationX(Math.DegToRad(40)) * Transform.CreateUniformScale(3)");
  1192. script->Execute("AZTestAssertFloatClose(t1:GetUniformScale(), 3)");
  1193. script->Execute("AZTestAssertFloatClose(t1:ExtractUniformScale(), 3)");
  1194. script->Execute("AZTestAssertFloatClose(t1:GetUniformScale(), 1)");
  1195. script->Execute("t1:MultiplyByUniformScale(2)");
  1196. script->Execute("AZTestAssertFloatClose(t1:GetUniformScale(), 2)");
  1197. ////orthogonalize
  1198. script->Execute("t1 = Transform.CreateRotationX(Math.DegToRad(30)) * Transform.CreateUniformScale(3)");
  1199. script->Execute("t1:SetTranslation(Vector3(1,2,3))");
  1200. script->Execute("t2 = t1:GetOrthogonalized()");
  1201. script->Execute("AZTestAssertFloatClose(t2:GetBasisX():GetLength(), 1)");
  1202. script->Execute("AZTestAssertFloatClose(t2:GetBasisY():GetLength(), 1)");
  1203. script->Execute("AZTestAssertFloatClose(t2:GetBasisZ():GetLength(), 1)");
  1204. script->Execute("AZTestAssertFloatClose(t2:GetBasisX():Dot(t2:GetBasisY()), 0)");
  1205. script->Execute("AZTestAssertFloatClose(t2:GetBasisX():Dot(t2:GetBasisZ()), 0)");
  1206. script->Execute("AZTestAssertFloatClose(t2:GetBasisY():Dot(t2:GetBasisZ()), 0)");
  1207. script->Execute("AZTestAssert(t2:GetBasisX():Cross(t2:GetBasisY()):IsClose(t2:GetBasisZ()))");
  1208. script->Execute("AZTestAssert(t2:GetBasisY():Cross(t2:GetBasisZ()):IsClose(t2:GetBasisX()))");
  1209. script->Execute("AZTestAssert(t2:GetBasisZ():Cross(t2:GetBasisX()):IsClose(t2:GetBasisY()))");
  1210. script->Execute("t1:Orthogonalize()");
  1211. script->Execute("AZTestAssert(t1:IsClose(t2))");
  1212. ////orthogonal
  1213. script->Execute("t1 = Transform.CreateRotationX(Math.DegToRad(30))");
  1214. script->Execute("t1:SetTranslation(Vector3(1, 2, 3))");
  1215. script->Execute("AZTestAssert(t1:IsOrthogonal(0.05))");
  1216. script->Execute("t1 = Transform.CreateRotationX(Math.DegToRad(30)) * Transform.CreateUniformScale(2)");
  1217. script->Execute("AZTestAssert( not t1:IsOrthogonal(0.05))");
  1218. ////IsClose
  1219. script->Execute("t1 = Transform.CreateRotationX(Math.DegToRad(30))");
  1220. script->Execute("t2 = t1:Clone()");
  1221. script->Execute("AZTestAssert(t1:IsClose(t2))");
  1222. script->Execute("t2 = Transform.CreateRotationX(Math.DegToRad(30.01))");
  1223. script->Execute("AZTestAssert(t1:IsClose(t2))");
  1224. script->Execute("t2 = Transform.CreateRotationX(Math.DegToRad(32))");
  1225. script->Execute("AZTestAssert( not t1:IsClose(t2))");
  1226. ////set rotation part
  1227. script->Execute("t1 = Transform.CreateTranslation(Vector3(1,2,3))");
  1228. script->Execute("t1:SetRotation(Quaternion.CreateRotationX(Math.DegToRad(30)))");
  1229. script->Execute("AZTestAssert(t1:TransformVector(Vector3(1, 0, 0)):IsClose(Vector3(1, 0, 0)))");
  1230. script->Execute("AZTestAssert(t1:TransformVector(Vector3(0, 1, 0)):IsClose(Vector3(0, 0.866, 0.5)))");
  1231. script->Execute("AZTestAssert(t1:TransformVector(Vector3(0, 0, 1)):IsClose(Vector3(0, -0.5, 0.866)))");
  1232. script->Execute("AZTestAssert(t1:GetTranslation():IsClose(Vector3(1, 2, 3)))");
  1233. }
  1234. TEST_F(ScriptMathTest, LuaPlaneTest)
  1235. {
  1236. script->Execute("pl = Plane.CreateFromNormalAndDistance(Vector3(1,0,0),-100)");
  1237. script->Execute("AZTestAssertFloatClose(pl:GetDistance(),-100)");
  1238. script->Execute("AZTestAssertFloatClose(pl:GetNormal().x,1)");
  1239. script->Execute("AZTestAssertFloatClose(pl:GetNormal().y,0)");
  1240. script->Execute("AZTestAssertFloatClose(pl:GetNormal().z,0)");
  1241. script->Execute("pl = Plane.CreateFromNormalAndPoint(Vector3(0, 1, 0), Vector3(10, 10, 10))");
  1242. script->Execute("AZTestAssertFloatClose(pl:GetDistance(), -10)");
  1243. script->Execute("AZTestAssertFloatClose(pl:GetNormal().x, 0)");
  1244. script->Execute("AZTestAssertFloatClose(pl:GetNormal().y, 1)");
  1245. script->Execute("AZTestAssertFloatClose(pl:GetNormal().z, 0)");
  1246. script->Execute("pl = Plane.CreateFromCoefficients(0,-1,0,-5)");
  1247. script->Execute("AZTestAssertFloatClose(pl:GetDistance(),-5)");
  1248. script->Execute("AZTestAssertFloatClose(pl:GetNormal().x,0)");
  1249. script->Execute("AZTestAssertFloatClose(pl:GetNormal().y,-1)");
  1250. script->Execute("AZTestAssertFloatClose(pl:GetNormal().z,0)");
  1251. AZ_MATH_TEST_START_TRACE_SUPPRESSION;
  1252. script->Execute("pl:Set(12, 13, 14, 15)");
  1253. AZ_MATH_TEST_STOP_TRACE_SUPPRESSION(1);
  1254. script->Execute("AZTestAssertFloatClose(pl:GetDistance(), 15)");
  1255. script->Execute("AZTestAssertFloatClose(pl:GetNormal().x, 12)");
  1256. script->Execute("AZTestAssertFloatClose(pl:GetNormal().y, 13)");
  1257. script->Execute("AZTestAssertFloatClose(pl:GetNormal().z, 14)");
  1258. AZ_MATH_TEST_START_TRACE_SUPPRESSION;
  1259. script->Execute("pl:Set(Vector3(22, 23, 24), 25)");
  1260. AZ_MATH_TEST_STOP_TRACE_SUPPRESSION(1);
  1261. script->Execute("AZTestAssertFloatClose(pl:GetDistance(), 25)");
  1262. script->Execute("AZTestAssertFloatClose(pl:GetNormal().x, 22)");
  1263. script->Execute("AZTestAssertFloatClose(pl:GetNormal().y, 23)");
  1264. script->Execute("AZTestAssertFloatClose(pl:GetNormal().z, 24)");
  1265. script->Execute("pl:Set(Vector4(32, 33, 34, 35))");
  1266. script->Execute("AZTestAssertFloatClose(pl:GetDistance(), 35)");
  1267. script->Execute("AZTestAssertFloatClose(pl:GetNormal().x, 32)");
  1268. script->Execute("AZTestAssertFloatClose(pl:GetNormal().y, 33)");
  1269. script->Execute("AZTestAssertFloatClose(pl:GetNormal().z, 34)");
  1270. script->Execute("pl:SetNormal(Vector3(0,0,1))");
  1271. script->Execute("AZTestAssertFloatClose(pl:GetNormal().x,0)");
  1272. script->Execute("AZTestAssertFloatClose(pl:GetNormal().y,0)");
  1273. script->Execute("AZTestAssertFloatClose(pl:GetNormal().z,1)");
  1274. script->Execute("pl:SetDistance(55)");
  1275. script->Execute("AZTestAssertFloatClose(pl:GetDistance(),55)");
  1276. script->Execute("AZTestAssertFloatClose(pl:GetPlaneEquationCoefficients().w,55)");
  1277. script->Execute("AZTestAssertFloatClose(pl:GetPlaneEquationCoefficients().x,0)");
  1278. script->Execute("AZTestAssertFloatClose(pl:GetPlaneEquationCoefficients().y,0)");
  1279. script->Execute("AZTestAssertFloatClose(pl:GetPlaneEquationCoefficients().z,1)");
  1280. script->Execute("tm = Transform.CreateRotationY(Math.DegToRad(90))");
  1281. script->Execute("pl = pl:GetTransform(tm)");
  1282. script->Execute("AZTestAssertFloatClose(pl:GetPlaneEquationCoefficients().w,55)");
  1283. script->Execute("AZTestAssertFloatClose(pl:GetPlaneEquationCoefficients().x,1)");
  1284. script->Execute("AZTestAssertFloatClose(pl:GetPlaneEquationCoefficients().y,0)");
  1285. script->Execute("AZTestAssertFloatClose(pl:GetPlaneEquationCoefficients().z, 0)");
  1286. script->Execute("dist = pl:GetPointDist(Vector3(10, 0, 0))");
  1287. script->Execute("AZTestAssertFloatClose(dist, 65)");
  1288. script->Execute("tm = Transform.CreateRotationZ(Math.DegToRad(45))");
  1289. script->Execute("pl:ApplyTransform(tm)");
  1290. script->Execute("AZTestAssertFloatClose(pl:GetPlaneEquationCoefficients().w, 55)");
  1291. script->Execute("AZTestAssertFloatClose(pl:GetPlaneEquationCoefficients().x, 0.7071)");
  1292. script->Execute("AZTestAssertFloatClose(pl:GetPlaneEquationCoefficients().y, 0.7071)");
  1293. script->Execute("AZTestAssertFloatClose(pl:GetPlaneEquationCoefficients().z, 0)");
  1294. script->Execute("pl:SetNormal(Vector3(0, 0, 1))");
  1295. script->Execute("v1 = pl:GetProjected(Vector3(10, 15, 20))");
  1296. script->Execute("AZTestAssert(v1 == Vector3(10, 15, 0))");
  1297. script->Execute("pl:Set(0, 0, 1, 10)");
  1298. script->Execute("hit, v1, time = pl:CastRay(Vector3(0, 0, 0), Vector3(0, 0, 1))");
  1299. script->Execute("AZTestAssert(hit == true)");
  1300. script->Execute("AZTestAssert(v1:IsClose(Vector3(0, 0, -10)))");
  1301. script->Execute("pl:Set(0, 1, 0, 10)");
  1302. script->Execute("hit, v1, time = pl:CastRay(Vector3(0, 1, 0), Vector3(0, -1, 0))");
  1303. script->Execute("AZTestAssert(hit == true)");
  1304. script->Execute("AZTestAssertFloatClose(time,10.999)");
  1305. script->Execute("pl:Set(1, 0, 0, 5)");
  1306. script->Execute("hit, v1, time = pl:CastRay(Vector3(0, 1, 0), Vector3(0, -1, 0))");
  1307. script->Execute("AZTestAssert(hit == false)");
  1308. script->Execute("pl:Set(1, 0, 0, 0)");
  1309. script->Execute("hit, v1, time = pl:IntersectSegment(Vector3(-1, 0, 0), Vector3(1, 0, 0))");
  1310. script->Execute("AZTestAssert(hit == true)");
  1311. script->Execute("AZTestAssert(v1:IsClose(Vector3(0, 0, 0)))");
  1312. script->Execute("AZTestAssertFloatClose(time, 0.5)");
  1313. script->Execute("pl:Set(0, 1, 0, 0)");
  1314. script->Execute("hit, v1, time = pl:IntersectSegment(Vector3(0, -1, 0), Vector3(0, 9, 0))");
  1315. script->Execute("AZTestAssert(hit == true)");
  1316. script->Execute("AZTestAssert(v1:IsClose(Vector3(0, 0, 0)))");
  1317. script->Execute("AZTestAssertFloatClose(time, 0.1)");
  1318. script->Execute("pl:Set(0, 1, 0, 20)");
  1319. script->Execute("hit, v1, time = pl:IntersectSegment(Vector3(-1, 0, 0), Vector3(1, 0, 0))");
  1320. script->Execute("AZTestAssert(hit == false)");
  1321. script->Execute("pl:Set(1, 0, 0, 0)");
  1322. script->Execute("AZTestAssert(pl:IsFinite())");
  1323. AZ_MATH_TEST_START_TRACE_SUPPRESSION;
  1324. script->Execute("pl:Set(math.huge, math.huge, math.huge, math.huge)");
  1325. AZ_MATH_TEST_STOP_TRACE_SUPPRESSION(1);
  1326. script->Execute("AZTestAssert( not pl:IsFinite())");
  1327. }
  1328. TEST_F(ScriptMathTest, LuaUuidTest)
  1329. {
  1330. // null
  1331. script->Execute("id = Uuid.CreateNull()");
  1332. script->Execute("AZTestAssert(id:IsNull())");
  1333. script->Execute("defId = Uuid.CreateString(\"{B5700F2E-661B-4AC0-9335-817CB4C09CCB}\")");
  1334. script->Execute("idStr1 = \"{B5700F2E-661B-4AC0-9335-817CB4C09CCB}\"");
  1335. script->Execute("idStr2 = \"{B5700F2E661B4AC09335817CB4C09CCB}\"");
  1336. script->Execute("idStr3 = \"B5700F2E-661B-4AC0-9335-817CB4C09CCB\"");
  1337. script->Execute("idStr4 = \"B5700F2E661B4AC09335817CB4C09CCB\"");
  1338. // create from string
  1339. script->Execute("id = Uuid.CreateString(idStr1)");
  1340. script->Execute("AZTestAssert(id==defId)");
  1341. script->Execute("id = Uuid.CreateString(idStr2)");
  1342. script->Execute("AZTestAssert(id==defId)");
  1343. script->Execute("id = Uuid.CreateString(idStr3)");
  1344. script->Execute("AZTestAssert(id==defId)");
  1345. script->Execute("id = Uuid.CreateString(idStr4)");
  1346. script->Execute("AZTestAssert(id==defId)");
  1347. // tostring
  1348. script->Execute("id = Uuid.CreateString(idStr1)");
  1349. script->Execute("asString = tostring(id)");
  1350. script->Execute("AZTestAssert(asString == idStr1)");
  1351. // operators
  1352. script->Execute("idBigger = Uuid(\"C5700F2E661B4ac09335817CB4C09CCB\")");
  1353. script->Execute("AZTestAssert(id < idBigger)");
  1354. script->Execute("AZTestAssert(id ~= idBigger)");
  1355. script->Execute("AZTestAssert(idBigger > id)");
  1356. // test the name function
  1357. script->Execute("uuidName = Uuid.CreateName(\"BlaBla\")");
  1358. // check id
  1359. script->Execute("AZTestAssert(uuidName == Uuid.CreateName(\"BlaBla\"))");
  1360. }
  1361. TEST_F(ScriptMathTest, LuaCrc32Test)
  1362. {
  1363. // constructor, value, tostring.
  1364. script->Execute("stringToCrc = \"blabla\" crc32 = Crc32(stringToCrc)");
  1365. script->Execute("AZTestAssert(crc32.value == 0x93086ff3)");
  1366. script->Execute("AZTestAssert(tostring(crc32) == \"0x93086ff3\")");
  1367. // == and ~= operators.
  1368. script->Execute("crc32 = Crc32(stringToCrc)");
  1369. script->Execute("AZTestAssert(crc32 == Crc32(stringToCrc))");
  1370. script->Execute("AZTestAssert(crc32 ~= Crc32(\"SomeOtherString\"))");
  1371. // TODO : calling any functions implemented with a generic
  1372. // script context on Crc32 causes an error.
  1373. // add(string), add(data, size)
  1374. /*script->Execute("stringHello = \"Hello\" stringWorld = \"World\"");
  1375. script->Execute("crcAdded = Crc32(stringHello)");
  1376. script->Execute("crcAdded:Add(stringWorld)");
  1377. script->Execute("crc32 = Crc32(\"HelloWorld\")");
  1378. script->Execute("AZTestAssert(crcAdded == crc32");*/
  1379. // tostring
  1380. script->Execute("crc32 = Crc32(0x12345680)");
  1381. script->Execute("AZTestAssert(tostring(crc32) == \"0x12345680\")");
  1382. AZ_TEST_START_TRACE_SUPPRESSION;
  1383. script->Execute("globalCrc32Prop = crc32.value");
  1384. AZ_TEST_STOP_TRACE_SUPPRESSION(1);
  1385. }
  1386. TEST_F(ScriptMathTest, LuaAabbTest)
  1387. {
  1388. ////CreateNull
  1389. ////IsValid
  1390. script->Execute("aabb = Aabb.CreateNull()");
  1391. script->Execute("AZTestAssert( not aabb:IsValid())");
  1392. script->Execute("AZTestAssert(aabb.min:IsGreaterThan(aabb.max))");
  1393. ////CreateFromPoint
  1394. script->Execute("aabb = Aabb.CreateFromPoint(Vector3(0))");
  1395. script->Execute("AZTestAssert(aabb:IsValid())");
  1396. script->Execute("AZTestAssert(aabb:IsFinite())");
  1397. script->Execute("AZTestAssert(aabb.min:IsClose(aabb.max))");
  1398. ////CreateFromMinMax
  1399. script->Execute("aabb = Aabb.CreateFromMinMax(Vector3(0),Vector3(1))");
  1400. script->Execute("AZTestAssert(aabb:IsValid())");
  1401. script->Execute("AZTestAssert(aabb.min:IsClose(Vector3(0)))");
  1402. script->Execute("AZTestAssert(aabb.max:IsClose(Vector3(1)))");
  1403. ////CreateCenterHalfExtents
  1404. script->Execute("aabb = Aabb.CreateCenterHalfExtents(Vector3(1), Vector3(1))");
  1405. script->Execute("AZTestAssert(aabb:IsValid())");
  1406. script->Execute("AZTestAssert(aabb.min:IsClose(Vector3(0)))");
  1407. script->Execute("AZTestAssert(aabb.max:IsClose(Vector3(2)))");
  1408. script->Execute("AZTestAssert(aabb:GetCenter():IsClose(Vector3(1)))");
  1409. ////CreateCenterRadius
  1410. ////GetExtents
  1411. script->Execute("aabb = Aabb.CreateCenterRadius(Vector3(4), 2)");
  1412. script->Execute("AZTestAssert(aabb.min:IsClose(Vector3(2)))");
  1413. script->Execute("AZTestAssert(aabb.max:IsClose(Vector3(6)))");
  1414. script->Execute("AZTestAssert(aabb:GetExtents():IsClose(Vector3(4)))");
  1415. ////CreateFromObb
  1416. script->Execute("position = Vector3(1, 1, 1)");
  1417. script->Execute("rotation = Quaternion.CreateRotationZ(Math.DegToRad(45))");
  1418. script->Execute("halfLengths = Vector3(1, 1, 1)");
  1419. script->Execute("obb = Obb.CreateFromPositionRotationAndHalfLengths(position, rotation, halfLengths)");
  1420. script->Execute("aabb = Aabb.CreateFromObb(obb)");
  1421. script->Execute("AZTestAssert(aabb.min:IsClose(Vector3(-0.414, -0.414, 0.0), 0.001))");
  1422. script->Execute("AZTestAssert(aabb.max:IsClose(Vector3(2.414, 2.414, 2.0), 0.001))");
  1423. script->Execute("AZTestAssert(aabb:GetCenter():IsClose(Vector3(1, 1, 1)))");
  1424. ////Set(min, max)
  1425. ////GetMin
  1426. ////GetMax
  1427. script->Execute("aabb:Set(Vector3(1), Vector3(4))");
  1428. script->Execute("AZTestAssert(aabb.min:IsClose(Vector3(1)))");
  1429. script->Execute("AZTestAssert(aabb.max:IsClose(Vector3(4)))");
  1430. ////SetMin
  1431. script->Execute("aabb.min = Vector3(0)");
  1432. script->Execute("AZTestAssert(aabb.min:IsClose(Vector3(0)))");
  1433. script->Execute("AZTestAssert(aabb:GetCenter():IsClose(Vector3(2)))");
  1434. ////SetMax
  1435. script->Execute("aabb.max = Vector3(6)");
  1436. script->Execute("AZTestAssert(aabb.max:IsClose(Vector3(6)))");
  1437. script->Execute("AZTestAssert(aabb:GetCenter():IsClose(Vector3(3)))");
  1438. ////GetXExtent
  1439. ////GetYExtent
  1440. ////GetZExtent
  1441. script->Execute("aabb:Set(Vector3(0), Vector3(1, 2, 3))");
  1442. script->Execute("AZTestAssertFloatClose(aabb:GetXExtent(), 1)");
  1443. script->Execute("AZTestAssertFloatClose(aabb:GetYExtent(), 2)");
  1444. script->Execute("AZTestAssertFloatClose(aabb:GetZExtent(), 3)");
  1445. ////GetAsSphere
  1446. script->Execute("aabb:Set(Vector3(-2, 0, 0), Vector3(2, 0, 0))");
  1447. script->Execute("center, radius = aabb:GetAsSphere()");
  1448. script->Execute("AZTestAssert(center:IsClose(Vector3(0)))");
  1449. script->Execute("AZTestAssertFloatClose(radius,2)");
  1450. ////Contains(Vector3)
  1451. script->Execute("aabb:Set(Vector3(-2), Vector3(2))");
  1452. script->Execute("AZTestAssert(aabb:Contains(Vector3(1)))");
  1453. ////Contains(Aabb)
  1454. script->Execute("aabb:Set(Vector3(0), Vector3(3))");
  1455. script->Execute("aabb2 = Aabb.CreateFromMinMax(Vector3(1), Vector3(2))");
  1456. script->Execute("AZTestAssert(aabb:Contains(aabb2))");
  1457. script->Execute("AZTestAssert( not aabb2:Contains(aabb))");
  1458. ////Overlaps
  1459. script->Execute("aabb:Set(Vector3(0), Vector3(2))");
  1460. script->Execute("aabb2:Set(Vector3(1), Vector3(3))");
  1461. script->Execute("AZTestAssert(aabb:Overlaps(aabb2))");
  1462. script->Execute("aabb2:Set(Vector3(5), Vector3(6))");
  1463. script->Execute("AZTestAssert( not aabb:Overlaps(aabb2))");
  1464. ////Expand
  1465. script->Execute("aabb:Set(Vector3(0), Vector3(2))");
  1466. script->Execute("aabb:Expand(Vector3(1))");
  1467. script->Execute("AZTestAssert(aabb:GetCenter():IsClose(Vector3(1)))");
  1468. script->Execute("AZTestAssert(aabb.min:IsClose(Vector3(-1)))");
  1469. script->Execute("AZTestAssert(aabb.max:IsClose(Vector3(3)))");
  1470. ////GetExpanded
  1471. script->Execute("aabb:Set(Vector3(-1), Vector3(1))");
  1472. script->Execute("aabb = aabb:GetExpanded(Vector3(9))");
  1473. script->Execute("AZTestAssert(aabb.min:IsClose(Vector3(-10)))");
  1474. script->Execute("AZTestAssert(aabb.max:IsClose(Vector3(10)))");
  1475. ////AddPoint
  1476. script->Execute("aabb:Set(Vector3(-1), Vector3(1))");
  1477. script->Execute("aabb:AddPoint(Vector3(1))");
  1478. script->Execute("AZTestAssert(aabb.min:IsClose(Vector3(-1)))");
  1479. script->Execute("AZTestAssert(aabb.max:IsClose(Vector3(1)))");
  1480. script->Execute("aabb:AddPoint(Vector3(2))");
  1481. script->Execute("AZTestAssert(aabb.min:IsClose(Vector3(-1)))");
  1482. script->Execute("AZTestAssert(aabb.max:IsClose(Vector3(2)))");
  1483. ////AddAabb
  1484. script->Execute("aabb:Set(Vector3(0), Vector3(2))");
  1485. script->Execute("aabb2:Set(Vector3(-2), Vector3(0))");
  1486. script->Execute("aabb:AddAabb(aabb2)");
  1487. script->Execute("AZTestAssert(aabb.min:IsClose(Vector3(-2)))");
  1488. script->Execute("AZTestAssert(aabb.max:IsClose(Vector3(2)))");
  1489. ////GetDistance
  1490. script->Execute("aabb:Set(Vector3(-1), Vector3(1))");
  1491. script->Execute("AZTestAssertFloatClose(aabb:GetDistance(Vector3(2, 0, 0)),1)");
  1492. //// make sure a point inside the box returns zero, even if that point isn't the center.
  1493. script->Execute("AZTestAssertFloatClose(aabb:GetDistance(Vector3(0.5, 0, 0)),0)");
  1494. ////GetClamped
  1495. script->Execute("aabb:Set(Vector3(0), Vector3(2))");
  1496. script->Execute("aabb2:Set(Vector3(1), Vector3(4))");
  1497. script->Execute("aabb = aabb:GetClamped(aabb2)");
  1498. script->Execute("AZTestAssert(aabb.min:IsClose(Vector3(1)))");
  1499. script->Execute("AZTestAssert(aabb.max:IsClose(Vector3(2)))");
  1500. ////Clamp
  1501. script->Execute("aabb:Set(Vector3(0), Vector3(2))");
  1502. script->Execute("aabb2:Set(Vector3(-2), Vector3(1))");
  1503. script->Execute("aabb:Clamp(aabb2)");
  1504. script->Execute("AZTestAssert(aabb.min:IsClose(Vector3(0)))");
  1505. script->Execute("AZTestAssert(aabb.max:IsClose(Vector3(1)))");
  1506. ////SetNull
  1507. script->Execute("aabb:SetNull()");
  1508. script->Execute("AZTestAssert( not aabb:IsValid())");
  1509. ////Translate
  1510. script->Execute("aabb:Set(Vector3(-1), Vector3(1))");
  1511. script->Execute("aabb:Translate(Vector3(2))");
  1512. script->Execute("AZTestAssert(aabb.min:IsClose(Vector3(1)))");
  1513. script->Execute("AZTestAssert(aabb.max:IsClose(Vector3(3)))");
  1514. ////GetTranslated
  1515. script->Execute("aabb:Set(Vector3(1), Vector3(3))");
  1516. script->Execute("aabb = aabb:GetTranslated(Vector3(-2))");
  1517. script->Execute("AZTestAssert(aabb.min:IsClose(Vector3(-1)))");
  1518. script->Execute("AZTestAssert(aabb.max:IsClose(Vector3(1)))");
  1519. ////GetSurfaceArea
  1520. script->Execute("aabb:Set(Vector3(0), Vector3(1))");
  1521. script->Execute("AZTestAssertFloatClose(aabb:GetSurfaceArea(), 6)");
  1522. ////IsFinite
  1523. script->Execute("infinity = math.huge");
  1524. script->Execute("infiniteV3 = Vector3(infinity, infinity, infinity)");
  1525. script->Execute("aabb:Set(Vector3(0),infiniteV3)");
  1526. //// A bounding box is only invalid if the min is greater than the max.
  1527. //// A bounding box with an infinite min or max is valid as long as this is true.
  1528. script->Execute("AZTestAssert(aabb:IsValid())");
  1529. script->Execute("AZTestAssert( not aabb:IsFinite())");
  1530. }
  1531. TEST_F(ScriptMathTest, LuaAabbTransformCompareTest)
  1532. {
  1533. // create aabb
  1534. script->Execute("min = Vector3(-100,50,0)");
  1535. script->Execute("max = Vector3(120,300,50)");
  1536. script->Execute("aabb = Aabb.CreateFromMinMax(min,max)");
  1537. // make the transformation matrix
  1538. script->Execute("tm = Transform.CreateRotationX(1)");
  1539. script->Execute("tm:SetTranslation(100,0,-50)");
  1540. // transform
  1541. script->Execute("obb = aabb:GetTransformedObb(tm)");
  1542. script->Execute("transAabb = aabb:GetTransformedAabb(tm)");
  1543. script->Execute("transAabb2 = Aabb.CreateFromObb(obb)");
  1544. script->Execute("aabb:ApplyTransform(tm)");
  1545. // compare the transformations
  1546. script->Execute("AZTestAssert(transAabb.min:IsClose(transAabb2.min))");
  1547. script->Execute("AZTestAssert(transAabb.max:IsClose(transAabb2.max))");
  1548. script->Execute("AZTestAssert(aabb.min:IsClose(transAabb.min))");
  1549. script->Execute("AZTestAssert(aabb.max:IsClose(transAabb.max))");
  1550. }
  1551. TEST_F(ScriptMathTest, LuaObbTest)
  1552. {
  1553. //CreateFromPositionRotationAndHalfLengths
  1554. //GetPosition
  1555. //GetAxisX
  1556. //GetAxisY
  1557. //GetAxisZ
  1558. //GetAxis
  1559. //GetHalfLengthX
  1560. //GetHalfLengthY
  1561. //GetHalfLengthZ
  1562. //GetHalfLength
  1563. script->Execute("position = Vector3(1, 2, 3)");
  1564. script->Execute("rotation = Quaternion.CreateRotationZ(Math.DegToRad(45))");
  1565. script->Execute("halfLengths = Vector3(0.5)");
  1566. script->Execute("obb = Obb.CreateFromPositionRotationAndHalfLengths(position, rotation, halfLengths)");
  1567. script->Execute("AZTestAssert(obb.position:IsClose(position))");
  1568. script->Execute("AZTestAssert(obb:GetAxis(0):IsClose(Vector3(0.7071, 0.7071, 0.0)))");
  1569. script->Execute("AZTestAssert(obb:GetAxis(1):IsClose(Vector3(-0.7071, 0.7071, 0.0)))");
  1570. script->Execute("AZTestAssert(obb:GetAxis(2):IsClose(Vector3(0.0, 0.0, 1.0)))");
  1571. script->Execute("AZTestAssert(obb.halfLengths:IsClose(halfLengths))");
  1572. script->Execute("AZTestAssertFloatClose(obb:GetHalfLengthX(), 0.5)");
  1573. script->Execute("AZTestAssertFloatClose(obb:GetHalfLengthY(), 0.5)");
  1574. script->Execute("AZTestAssertFloatClose(obb:GetHalfLengthZ(), 0.5)");
  1575. //CreateFromAabb
  1576. script->Execute("min = Vector3(-100, 50, 0)");
  1577. script->Execute("max = Vector3(120, 300, 50)");
  1578. script->Execute("aabb = Aabb.CreateFromMinMax(min, max)");
  1579. script->Execute("obb = Obb.CreateFromAabb(aabb)");
  1580. script->Execute("AZTestAssert(obb.position:IsClose(Vector3(10, 175, 25)))");
  1581. script->Execute("AZTestAssert(obb:GetAxisX():IsClose(Vector3(1, 0, 0)))");
  1582. script->Execute("AZTestAssert(obb:GetAxisY():IsClose(Vector3(0, 1, 0)))");
  1583. script->Execute("AZTestAssert(obb:GetAxisZ():IsClose(Vector3(0, 0, 1)))");
  1584. // Transform * Obb
  1585. script->Execute("obb = Obb.CreateFromPositionRotationAndHalfLengths(position, rotation, halfLengths)");
  1586. script->Execute("transform = Transform.CreateRotationY(Math.DegToRad(90))");
  1587. script->Execute("obb = transform * obb");
  1588. script->Execute("AZTestAssert(obb.position:IsClose(Vector3(3, 2, -1)))");
  1589. script->Execute("AZTestAssert(obb:GetAxisX():IsClose(Vector3(0.0, 0.7071, -0.7071)))");
  1590. script->Execute("AZTestAssert(obb:GetAxisY():IsClose(Vector3(0.0, 0.7071, 0.7071)))");
  1591. script->Execute("AZTestAssert(obb:GetAxisZ():IsClose(Vector3(1.0, 0.0, 0.0)))");
  1592. //SetPosition
  1593. //SetHalfLengthX
  1594. //SetHalfLengthY
  1595. //SetHalfLengthZ
  1596. //SetHalfLength
  1597. script->Execute("obb.position = position");
  1598. script->Execute("AZTestAssert(obb.position:IsClose(position))");
  1599. script->Execute("obb:SetHalfLengthX(2)");
  1600. script->Execute("AZTestAssertFloatClose(obb:GetHalfLengthX(), 2)");
  1601. script->Execute("obb:SetHalfLengthY(3)");
  1602. script->Execute("AZTestAssertFloatClose(obb:GetHalfLengthY(), 3)");
  1603. script->Execute("obb:SetHalfLengthZ(4)");
  1604. script->Execute("AZTestAssertFloatClose(obb:GetHalfLengthZ(), 4)");
  1605. script->Execute("obb:SetHalfLength(2, 5)");
  1606. script->Execute("AZTestAssertFloatClose(obb:GetHalfLength(2), 5)");
  1607. //IsFinite
  1608. script->Execute("obb = Obb.CreateFromPositionRotationAndHalfLengths(position, rotation, halfLengths)");
  1609. script->Execute("AZTestAssert(obb:IsFinite())");
  1610. script->Execute("infinity = math.huge");
  1611. script->Execute("infiniteV3 = Vector3(infinity, infinity, infinity)");
  1612. // Test to make sure that setting properties of the bounding box
  1613. // properly mark it as infinite, and when reset it becomes finite again.
  1614. script->Execute("obb.position = infiniteV3");
  1615. script->Execute("AZTestAssert( not obb:IsFinite())");
  1616. script->Execute("obb.position = position");
  1617. script->Execute("AZTestAssert(obb:IsFinite())");
  1618. script->Execute("obb:SetHalfLengthX(infinity)");
  1619. script->Execute("AZTestAssert( not obb:IsFinite())");
  1620. script->Execute("obb:SetHalfLengthX(2.0)");
  1621. script->Execute("AZTestAssert(obb:IsFinite())");
  1622. }
  1623. TEST_F(ScriptMathTest, LuaMathHelpersTest)
  1624. {
  1625. script->Execute(R"LUA(
  1626. AZTestAssert(Math.IsClose(3.0, 3.0, FloatEpsilon))
  1627. AZTestAssert(Math.IsClose(3.0, 3.0))
  1628. AZTestAssert(Math.IsClose(3.20345678, 3.20345679))
  1629. AZTestAssert(not Math.IsClose(3.0, 4.0, FloatEpsilon))
  1630. AZTestAssert(not Math.IsClose(3.0, 4.0))
  1631. AZTestAssert(Math.IsClose(1.0, 5.0, 10.0))
  1632. )LUA");
  1633. }
  1634. }