Matrix4x3.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Matrix4x3.h"
  25. const Matrix4x3 Matrix4x3::sZero(
  26. 0.0f, 0.0f, 0.0f, 0.0f,
  27. 0.0f, 0.0f, 0.0f, 0.0f,
  28. 0.0f, 0.0f, 0.0f, 0.0f);
  29. const Matrix4x3 Matrix4x3::sIdentity(
  30. 1.0f, 0.0f, 0.0f, 0.0f,
  31. 0.0f, 1.0f, 0.0f, 0.0f,
  32. 0.0f, 0.0f, 1.0f, 0.0f);
  33. Matrix4x3::Matrix4x3(const Vector3& translation, const Quaternion& rotation, float scale)
  34. {
  35. define(translation, rotation, scale);
  36. }
  37. Matrix4x3::Matrix4x3(const Vector3& translation, const Quaternion& rotation, const Vector3& scale)
  38. {
  39. define(translation, rotation, scale);
  40. }
  41. void Matrix4x3::define(const Vector3& translation, const Quaternion& rotation, float scale)
  42. {
  43. Matrix3 scaleMatrix(Matrix3::sIdentity);
  44. scaleMatrix.setScale(scale);
  45. *this = rotation.getRotationMatrix() * scaleMatrix;
  46. setTranslation(translation);
  47. }
  48. void Matrix4x3::define(const Vector3& translation, const Quaternion& rotation, const Vector3& scale)
  49. {
  50. Matrix3 scaleMatrix(Matrix3::sIdentity);
  51. scaleMatrix.setScale(scale);
  52. *this = rotation.getRotationMatrix() * scaleMatrix;
  53. setTranslation(translation);
  54. }
  55. void Matrix4x3::getDecomposition(Vector3& translation, Quaternion& rotation, Vector3& scale) const
  56. {
  57. translation.mX = m03;
  58. translation.mY = m13;
  59. translation.mZ = m23;
  60. Vector3 row1(m00, m10, m20);
  61. Vector3 row2(m01, m11, m21);
  62. Vector3 row3(m02, m12, m22);
  63. scale.mX = row1.getLength();
  64. scale.mY = row2.getLength();
  65. scale.mZ = row3.getLength();
  66. // Remove scaling from the 3x3 matrix to get rotation
  67. row1 /= scale.mX;
  68. row2 /= scale.mY;
  69. row3 /= scale.mZ;
  70. rotation.fromRotationMatrix(Matrix3(row1.mX, row2.mX, row3.mX, row1.mY, row2.mY, row3.mY, row1.mZ, row2.mZ, row3.mZ));
  71. }
  72. Matrix4x3 Matrix4x3::getInverse() const
  73. {
  74. float det = m00 * m11 * m22 +
  75. m10 * m21 * m02 +
  76. m20 * m01 * m12 -
  77. m20 * m11 * m02 -
  78. m10 * m01 * m22 -
  79. m00 * m21 * m12;
  80. float invDet = 1.0f / det;
  81. Matrix4x3 out;
  82. out.m00 = (m11 * m22 - m21 * m12) * invDet;
  83. out.m01 = -(m01 * m22 - m21 * m02) * invDet;
  84. out.m02 = (m01 * m12 - m11 * m02) * invDet;
  85. out.m03 = -(m03 * out.m00 + m13 * out.m01 + m23 * out.m02);
  86. out.m10 = -(m10 * m22 - m20 * m12) * invDet;
  87. out.m11 = (m00 * m22 - m20 * m02) * invDet;
  88. out.m12 = -(m00 * m12 - m10 * m02) * invDet;
  89. out.m13 = -(m03 * out.m10 + m13 * out.m11 + m23 * out.m12);
  90. out.m20 = (m10 * m21 - m20 * m11) * invDet;
  91. out.m21 = -(m00 * m21 - m20 * m01) * invDet;
  92. out.m22 = (m00 * m11 - m10 * m01) * invDet;
  93. out.m23 = -(m03 * out.m20 + m13 * out.m21 + m23 * out.m22);
  94. return out;
  95. }