Matrix4x3.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. setRotation(rotation.getRotationMatrix() * scale);
  44. setTranslation(translation);
  45. }
  46. void Matrix4x3::define(const Vector3& translation, const Quaternion& rotation, const Vector3& scale)
  47. {
  48. setRotation(rotation.getRotationMatrix().getScaled(scale));
  49. setTranslation(translation);
  50. }
  51. void Matrix4x3::getDecomposition(Vector3& translation, Quaternion& rotation, Vector3& scale) const
  52. {
  53. translation.mX = m03;
  54. translation.mY = m13;
  55. translation.mZ = m23;
  56. Vector3 row1(m00, m10, m20);
  57. Vector3 row2(m01, m11, m21);
  58. Vector3 row3(m02, m12, m22);
  59. scale.mX = row1.getLength();
  60. scale.mY = row2.getLength();
  61. scale.mZ = row3.getLength();
  62. // Remove scaling from the 3x3 matrix to get rotation
  63. row1 /= scale.mX;
  64. row2 /= scale.mY;
  65. row3 /= scale.mZ;
  66. rotation.define(Matrix3(row1.mX, row2.mX, row3.mX, row1.mY, row2.mY, row3.mY, row1.mZ, row2.mZ, row3.mZ));
  67. }
  68. Matrix4x3 Matrix4x3::getInverse() const
  69. {
  70. float det = m00 * m11 * m22 +
  71. m10 * m21 * m02 +
  72. m20 * m01 * m12 -
  73. m20 * m11 * m02 -
  74. m10 * m01 * m22 -
  75. m00 * m21 * m12;
  76. float invDet = 1.0f / det;
  77. Matrix4x3 out;
  78. out.m00 = (m11 * m22 - m21 * m12) * invDet;
  79. out.m01 = -(m01 * m22 - m21 * m02) * invDet;
  80. out.m02 = (m01 * m12 - m11 * m02) * invDet;
  81. out.m03 = -(m03 * out.m00 + m13 * out.m01 + m23 * out.m02);
  82. out.m10 = -(m10 * m22 - m20 * m12) * invDet;
  83. out.m11 = (m00 * m22 - m20 * m02) * invDet;
  84. out.m12 = -(m00 * m12 - m10 * m02) * invDet;
  85. out.m13 = -(m03 * out.m10 + m13 * out.m11 + m23 * out.m12);
  86. out.m20 = (m10 * m21 - m20 * m11) * invDet;
  87. out.m21 = -(m00 * m21 - m20 * m01) * invDet;
  88. out.m22 = (m00 * m11 - m10 * m01) * invDet;
  89. out.m23 = -(m03 * out.m20 + m13 * out.m21 + m23 * out.m22);
  90. return out;
  91. }