FMatrix2x2.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2017 to 2019 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #ifndef DFPSR_GEOMETRY_FMATRIX2x2
  24. #define DFPSR_GEOMETRY_FMATRIX2x2
  25. #include <cassert>
  26. #include "FVector.h"
  27. namespace dsr {
  28. struct FMatrix2x2 {
  29. FVector2D xAxis, yAxis;
  30. FMatrix2x2() :
  31. xAxis(FVector2D(1.0f, 0.0f)),
  32. yAxis(FVector2D(0.0f, 1.0f)) {}
  33. explicit FMatrix2x2(float uniformScale) :
  34. xAxis(FVector2D(uniformScale, 0.0f)),
  35. yAxis(FVector2D(0.0f, uniformScale)) {}
  36. FMatrix2x2(const FVector2D &xAxis, const FVector2D &yAxis) :
  37. xAxis(xAxis),
  38. yAxis(yAxis) {}
  39. // Transform the a vector by multiplying with the matrix
  40. FVector2D transform(const FVector2D &p) const {
  41. return FVector2D(
  42. p.x * this->xAxis.x + p.y * this->yAxis.x,
  43. p.x * this->xAxis.y + p.y * this->yAxis.y
  44. );
  45. }
  46. // Transform the a vector by multiplying with the transpose of the matrix
  47. // The transpose is the inverse for axis aligned normalized matrices
  48. // Axis aligned: Each non-self axis dot-product equals zero.
  49. // Normalized: The length of each axis equals one.
  50. FVector2D transformTransposed(const FVector2D &p) const {
  51. return FVector2D(
  52. p.x * this->xAxis.x + p.y * this->xAxis.y,
  53. p.x * this->yAxis.x + p.y * this->yAxis.y
  54. );
  55. }
  56. };
  57. inline FMatrix2x2 operator*(const FMatrix2x2 &m, const float &scale) {
  58. return FMatrix2x2(m.xAxis * scale, m.yAxis * scale);
  59. }
  60. inline FMatrix2x2 operator*(const FMatrix2x2 &left, const FMatrix2x2 &right) {
  61. return FMatrix2x2(right.transform(left.xAxis), right.transform(left.yAxis));
  62. }
  63. inline float determinant(const FMatrix2x2& m) {
  64. return m.xAxis.x * m.yAxis.y - m.xAxis.y * m.yAxis.x;
  65. }
  66. // The full matrix inverse for any matrix where the determinant is not zero
  67. inline FMatrix2x2 inverse(const FMatrix2x2& m) {
  68. return FMatrix2x2(FVector2D(m.yAxis.y, -m.xAxis.y), FVector2D(-m.yAxis.x, m.xAxis.x)) * (1.0f / determinant(m));
  69. }
  70. }
  71. #endif