MathConversion.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #pragma once
  9. #include <AzCore/Math/Aabb.h>
  10. #include <AzCore/Math/Obb.h>
  11. #include <AzCore/Math/Vector2.h>
  12. #include <AzCore/Math/Vector3.h>
  13. #include <AzCore/Math/Vector4.h>
  14. #include <AzCore/Math/Color.h>
  15. #include <AzCore/Math/Transform.h>
  16. #include <AzCore/Math/Matrix3x3.h>
  17. #include <AzCore/Math/Matrix3x4.h>
  18. #include <AzCore/Math/Quaternion.h>
  19. #include <AzCore/Component/EntityId.h>
  20. #include <AzCore/Math/Plane.h>
  21. #include <Cry_Math.h>
  22. #include <Cry_Geo.h>
  23. #include <Cry_Color.h>
  24. inline AZ::Vector2 LYVec2ToAZVec2(const Vec2& source)
  25. {
  26. return AZ::Vector2(source.x, source.y);
  27. }
  28. inline Vec2 AZVec2ToLYVec2(const AZ::Vector2& source)
  29. {
  30. return Vec2(source.GetX(), source.GetY());
  31. }
  32. inline AZ::Vector3 LYVec3ToAZVec3(const Vec3& source)
  33. {
  34. return AZ::Vector3(source.x, source.y, source.z);
  35. }
  36. inline Vec3 AZVec3ToLYVec3(const AZ::Vector3& source)
  37. {
  38. return Vec3(source.GetX(), source.GetY(), source.GetZ());
  39. }
  40. inline AZ::Vector4 LYVec4ToAZVec4(const Vec4& source)
  41. {
  42. return AZ::Vector4(source.x, source.y, source.z, source.w);
  43. }
  44. inline Vec4 AZVec4ToLYVec4(const AZ::Vector4& source)
  45. {
  46. return Vec4(source.GetX(), source.GetY(), source.GetZ(), source.GetW());
  47. }
  48. inline AZ::Color LYVec3ToAZColor(const Vec3& source)
  49. {
  50. return AZ::Color(source.x, source.y, source.z, 1.0f);
  51. }
  52. inline Vec3 AZColorToLYVec3(const AZ::Color& source)
  53. {
  54. return Vec3(source.GetR(), source.GetG(), source.GetB());
  55. }
  56. inline Vec4 AZColorToLYVec4(const AZ::Color& source)
  57. {
  58. return Vec4(source.GetR(), source.GetG(), source.GetB(), source.GetA());
  59. }
  60. inline ColorF AZColorToLYColorF(const AZ::Color& source)
  61. {
  62. return ColorF(source.ToU32());
  63. }
  64. inline AZ::Color LYColorFToAZColor(const ColorF& source)
  65. {
  66. return AZ::Color(source.r, source.g, source.b, source.a);
  67. }
  68. inline ColorB AZColorToLYColorB(const AZ::Color& source)
  69. {
  70. return ColorB(source.ToU32());
  71. }
  72. inline AZ::Color LYColorBToAZColor(const ColorB& source)
  73. {
  74. return AZ::Color(source.r, source.g, source.b, source.a);
  75. }
  76. inline AZ::Quaternion LYQuaternionToAZQuaternion(const Quat& source)
  77. {
  78. const float f4[4] = { source.v.x, source.v.y, source.v.z, source.w };
  79. return AZ::Quaternion::CreateFromFloat4(f4);
  80. }
  81. inline Quat AZQuaternionToLYQuaternion(const AZ::Quaternion& source)
  82. {
  83. float f4[4];
  84. source.StoreToFloat4(f4);
  85. return Quat(f4[3], f4[0], f4[1], f4[2]);
  86. }
  87. inline Matrix34 AZTransformToLYTransform(const AZ::Transform& source)
  88. {
  89. return Matrix34::CreateFromVectors(
  90. AZVec3ToLYVec3(source.GetBasisX()),
  91. AZVec3ToLYVec3(source.GetBasisY()),
  92. AZVec3ToLYVec3(source.GetBasisZ()),
  93. AZVec3ToLYVec3(source.GetTranslation()));
  94. }
  95. inline Matrix33 AZMatrix3x3ToLYMatrix3x3(const AZ::Matrix3x3& source)
  96. {
  97. return Matrix33::CreateFromVectors(
  98. AZVec3ToLYVec3(source.GetColumn(0)),
  99. AZVec3ToLYVec3(source.GetColumn(1)),
  100. AZVec3ToLYVec3(source.GetColumn(2)));
  101. }
  102. inline AZ::Matrix3x3 LyMatrix3x3ToAzMatrix3x3(const Matrix33& source)
  103. {
  104. return AZ::Matrix3x3::CreateFromColumns(
  105. LYVec3ToAZVec3(source.GetColumn(0)),
  106. LYVec3ToAZVec3(source.GetColumn(1)),
  107. LYVec3ToAZVec3(source.GetColumn(2)));
  108. }
  109. inline Matrix34 AZMatrix3x4ToLYMatrix3x4(const AZ::Matrix3x4& source)
  110. {
  111. AZ::Vector3 col0;
  112. AZ::Vector3 col1;
  113. AZ::Vector3 col2;
  114. AZ::Vector3 col3;
  115. source.GetBasisAndTranslation(&col0, &col1, &col2, &col3);
  116. return Matrix34(
  117. col0.GetX(), col1.GetX(), col2.GetX(), col3.GetX(),
  118. col0.GetY(), col1.GetY(), col2.GetY(), col3.GetY(),
  119. col0.GetZ(), col1.GetZ(), col2.GetZ(), col3.GetZ());
  120. }
  121. inline AZ::Transform LYTransformToAZTransform(const Matrix34& source)
  122. {
  123. const AZ::Matrix3x4 matrix3x4 = AZ::Matrix3x4::CreateFromRowMajorFloat12(source.GetData());
  124. return AZ::Transform::CreateFromMatrix3x4(matrix3x4);
  125. }
  126. inline AZ::Matrix3x4 LYTransformToAZMatrix3x4(const Matrix34& source)
  127. {
  128. return AZ::Matrix3x4::CreateFromRowMajorFloat12(source.GetData());
  129. }
  130. inline AABB AZAabbToLyAABB(const AZ::Aabb& source)
  131. {
  132. return AABB(AZVec3ToLYVec3(source.GetMin()), AZVec3ToLYVec3(source.GetMax()));
  133. }
  134. inline AZ::Aabb LyAABBToAZAabb(const AABB& source)
  135. {
  136. return AZ::Aabb::CreateFromMinMax(LYVec3ToAZVec3(source.min), LYVec3ToAZVec3(source.max));
  137. }
  138. inline AZ::Obb LyOBBtoAZObb(const OBB& source)
  139. {
  140. const AZ::Vector3 position = LYVec3ToAZVec3(source.c);
  141. const AZ::Quaternion rotation = AZ::Quaternion::CreateFromMatrix3x3(LyMatrix3x3ToAzMatrix3x3(source.m33));
  142. const AZ::Vector3 halfLengths(source.h.x, source.h.y, source.h.z);
  143. return AZ::Obb::CreateFromPositionRotationAndHalfLengths(position, rotation, halfLengths);
  144. }
  145. inline OBB AZObbToLyOBB(const AZ::Obb& source)
  146. {
  147. return OBB::CreateOBB(
  148. Matrix33::CreateFromVectors(
  149. AZVec3ToLYVec3(source.GetAxisX()),
  150. AZVec3ToLYVec3(source.GetAxisY()),
  151. AZVec3ToLYVec3(source.GetAxisZ())),
  152. Vec3(source.GetHalfLengthX(), source.GetHalfLengthY(), source.GetHalfLengthZ()),
  153. AZVec3ToLYVec3(source.GetPosition()));
  154. }
  155. inline AZ::Plane LyPlaneToAZPlane(const ::Plane& source)
  156. {
  157. return AZ::Plane::CreateFromNormalAndDistance(LYVec3ToAZVec3(source.n), source.d);
  158. }
  159. inline ::Plane AZPlaneToLyPlane(const AZ::Plane& source)
  160. {
  161. ::Plane resultPlane;
  162. resultPlane.Set(AZVec3ToLYVec3(source.GetNormal()), source.GetDistance());
  163. return resultPlane;
  164. }