MathConversion.h 4.3 KB

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