b2_math.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // MIT License
  2. // Copyright (c) 2019 Erin Catto
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. // SOFTWARE.
  18. #include "box2d/b2_math.h"
  19. const b2Vec2 b2Vec2_zero(0.0f, 0.0f);
  20. /// Solve A * x = b, where b is a column vector. This is more efficient
  21. /// than computing the inverse in one-shot cases.
  22. b2Vec3 b2Mat33::Solve33(const b2Vec3& b) const
  23. {
  24. float det = b2Dot(ex, b2Cross(ey, ez));
  25. if (det != 0.0f)
  26. {
  27. det = 1.0f / det;
  28. }
  29. b2Vec3 x;
  30. x.x = det * b2Dot(b, b2Cross(ey, ez));
  31. x.y = det * b2Dot(ex, b2Cross(b, ez));
  32. x.z = det * b2Dot(ex, b2Cross(ey, b));
  33. return x;
  34. }
  35. /// Solve A * x = b, where b is a column vector. This is more efficient
  36. /// than computing the inverse in one-shot cases.
  37. b2Vec2 b2Mat33::Solve22(const b2Vec2& b) const
  38. {
  39. float a11 = ex.x, a12 = ey.x, a21 = ex.y, a22 = ey.y;
  40. float det = a11 * a22 - a12 * a21;
  41. if (det != 0.0f)
  42. {
  43. det = 1.0f / det;
  44. }
  45. b2Vec2 x;
  46. x.x = det * (a22 * b.x - a12 * b.y);
  47. x.y = det * (a11 * b.y - a21 * b.x);
  48. return x;
  49. }
  50. ///
  51. void b2Mat33::GetInverse22(b2Mat33* M) const
  52. {
  53. float a = ex.x, b = ey.x, c = ex.y, d = ey.y;
  54. float det = a * d - b * c;
  55. if (det != 0.0f)
  56. {
  57. det = 1.0f / det;
  58. }
  59. M->ex.x = det * d; M->ey.x = -det * b; M->ex.z = 0.0f;
  60. M->ex.y = -det * c; M->ey.y = det * a; M->ey.z = 0.0f;
  61. M->ez.x = 0.0f; M->ez.y = 0.0f; M->ez.z = 0.0f;
  62. }
  63. /// Returns the zero matrix if singular.
  64. void b2Mat33::GetSymInverse33(b2Mat33* M) const
  65. {
  66. float det = b2Dot(ex, b2Cross(ey, ez));
  67. if (det != 0.0f)
  68. {
  69. det = 1.0f / det;
  70. }
  71. float a11 = ex.x, a12 = ey.x, a13 = ez.x;
  72. float a22 = ey.y, a23 = ez.y;
  73. float a33 = ez.z;
  74. M->ex.x = det * (a22 * a33 - a23 * a23);
  75. M->ex.y = det * (a13 * a23 - a12 * a33);
  76. M->ex.z = det * (a12 * a23 - a13 * a22);
  77. M->ey.x = M->ex.y;
  78. M->ey.y = det * (a11 * a33 - a13 * a13);
  79. M->ey.z = det * (a13 * a12 - a11 * a23);
  80. M->ez.x = M->ex.z;
  81. M->ez.y = M->ey.z;
  82. M->ez.z = det * (a11 * a22 - a12 * a12);
  83. }