Matrix4x3.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 Matrix3& rotation, float scale)
  38. {
  39. define(translation, rotation, scale);
  40. }
  41. Matrix4x3::Matrix4x3(const Vector3& translation, const Quaternion& rotation, const Vector3& scale)
  42. {
  43. define(translation, rotation, scale);
  44. }
  45. Matrix4x3::Matrix4x3(const Vector3& translation, const Matrix3& rotation, const Vector3& scale)
  46. {
  47. define(translation, rotation, scale);
  48. }
  49. void Matrix4x3::define(const Vector3& translation, const Quaternion& rotation, float scale)
  50. {
  51. Matrix3 scaleMatrix(Matrix3::sIdentity);
  52. scaleMatrix.setScale(scale);
  53. *this = rotation.getRotationMatrix() * scaleMatrix;
  54. setTranslation(translation);
  55. }
  56. void Matrix4x3::define(const Vector3& translation, const Matrix3& rotation, float scale)
  57. {
  58. Matrix3 scaleMatrix(Matrix3::sIdentity);
  59. scaleMatrix.setScale(scale);
  60. *this = rotation * scaleMatrix;
  61. setTranslation(translation);
  62. }
  63. void Matrix4x3::define(const Vector3& translation, const Quaternion& rotation, const Vector3& scale)
  64. {
  65. Matrix3 scaleMatrix(Matrix3::sIdentity);
  66. scaleMatrix.setScale(scale);
  67. *this = rotation.getRotationMatrix() * scaleMatrix;
  68. setTranslation(translation);
  69. }
  70. void Matrix4x3::define(const Vector3& translation, const Matrix3& rotation, const Vector3& scale)
  71. {
  72. Matrix3 scaleMatrix(Matrix3::sIdentity);
  73. scaleMatrix.setScale(scale);
  74. *this = rotation * scaleMatrix;
  75. setTranslation(translation);
  76. }
  77. Matrix4x3 Matrix4x3::getInverse() const
  78. {
  79. float det = m00 * m11 * m22 +
  80. m10 * m21 * m02 +
  81. m20 * m01 * m12 -
  82. m20 * m11 * m02 -
  83. m10 * m01 * m22 -
  84. m00 * m21 * m12;
  85. float invDet = 1.0f / det;
  86. Matrix4x3 out;
  87. out.m00 = (m11 * m22 - m21 * m12) * invDet;
  88. out.m01 = -(m01 * m22 - m21 * m02) * invDet;
  89. out.m02 = (m01 * m12 - m11 * m02) * invDet;
  90. out.m03 = -(m03 * out.m00 + m13 * out.m01 + m23 * out.m02);
  91. out.m10 = -(m10 * m22 - m20 * m12) * invDet;
  92. out.m11 = (m00 * m22 - m20 * m02) * invDet;
  93. out.m12 = -(m00 * m12 - m10 * m02) * invDet;
  94. out.m13 = -(m03 * out.m10 + m13 * out.m11 + m23 * out.m12);
  95. out.m20 = (m10 * m21 - m20 * m11) * invDet;
  96. out.m21 = -(m00 * m21 - m20 * m01) * invDet;
  97. out.m22 = (m00 * m11 - m10 * m01) * invDet;
  98. out.m23 = -(m03 * out.m20 + m13 * out.m21 + m23 * out.m22);
  99. return out;
  100. }