Mat3.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Types.h"
  25. namespace crown
  26. {
  27. class Mat4;
  28. class Quat;
  29. class Vec3;
  30. /// Column major 3x3 matrix.
  31. ///
  32. /// The engine uses column vectors for coordinate space transformations
  33. /// so you'll have to specify transformations in reverse order.
  34. /// e.g. (rotation translation vector) will produce the result of first translating
  35. /// and then rotating the vector.
  36. /// Also note that a column major matrix needs to be placed to the left of a
  37. /// vector by matrix multiplication, so, to multiply a vector by a matrix you'll have
  38. /// to write something like: matrix vector. Since we are also using column vectors, inverting
  39. /// the operands would result in an impossible operation.
  40. ///
  41. /// @a verbatim:
  42. /// X base vector
  43. /// | Y base vector
  44. /// | | Z base vector
  45. /// | | |
  46. /// 1 [ Xx Yx Zx ]
  47. /// 2 | Xy Yy Zy |
  48. /// 3 [ Xz Yz Zz ]
  49. /// 1 2 3
  50. class Mat3
  51. {
  52. public:
  53. float m[9];
  54. /// Does nothing for efficiency.
  55. Mat3();
  56. /// Constructs from a set of float
  57. Mat3(float r1c1, float r2c1, float r3c1, float r1c2, float r2c2, float r3c2, float r1c3, float r2c3, float r3c3);
  58. /// Constructs from the @a v array
  59. Mat3(const float v[9]);
  60. Mat3(const Mat3& a);
  61. /// Assignment operator (copies the data)
  62. Mat3& operator=(const Mat3& a);
  63. /// Random access by index
  64. float operator[](uint32_t i) const;
  65. /// Random access by index
  66. float& operator[](uint32_t i);
  67. /// Random access by row/column pair
  68. float operator()(uint32_t row, uint32_t column) const;
  69. Mat3 operator+(const Mat3& a) const;
  70. Mat3& operator+=(const Mat3& a);
  71. Mat3 operator-(const Mat3& a) const;
  72. Mat3& operator-=(const Mat3& a);
  73. Mat3 operator*(float k) const;
  74. Mat3& operator*=(float k);
  75. Mat3 operator/(float k) const;
  76. Mat3& operator/=(float k);
  77. Vec3 operator*(const Vec3& v) const;
  78. Mat3 operator*(const Mat3& a) const;
  79. Mat3& operator*=(const Mat3& a);
  80. /// For simmetry
  81. friend Mat3 operator*(float k, const Mat3& a);
  82. /// Builds a rotation matrix about the X axis of @a radians radians
  83. void build_rotation_x(float radians);
  84. /// Builds a rotation matrix about the Y axis of @a radians radians
  85. void build_rotation_y(float radians);
  86. /// Builds a rotation matrix about the Z axis of @a radians radians
  87. void build_rotation_z(float radians);
  88. /// Builds a rotation matrix about an arbitrary axis of "radians" radians
  89. void build_rotation(const Vec3& n, float radians);
  90. Mat3& transpose();
  91. Mat3 get_transposed() const;
  92. float get_determinant() const;
  93. Mat3& invert();
  94. Mat3 get_inverted() const;
  95. /// Builds the identity matrix
  96. void load_identity();
  97. /// Returns a Vec3 containing the matrix's x base vector.
  98. Vec3 x() const;
  99. /// Returns a Vec3 containing the matrix's y base vector.
  100. Vec3 y() const;
  101. /// Returns a Vec3 containing the matrix's z base vector.
  102. Vec3 z() const;
  103. /// Sets the matrix's x base vector.
  104. void set_x(const Vec3& x);
  105. /// Sets the matrix's y base vector.
  106. void set_y(const Vec3& y);
  107. /// Sets the matrix's z base vector.
  108. void set_z(const Vec3& z);
  109. /// Returns a Vec3 containing the matrix's scale portion
  110. Vec3 get_scale() const;
  111. /// Fills the matrix's scale portion with the values contained in @a scale
  112. void set_scale(const Vec3& scale);
  113. /// Returns the pointer to the matrix's data
  114. float* to_float_ptr();
  115. /// Returns the pointer to the matrix's data
  116. const float* to_float_ptr() const;
  117. /// Returns a 4x4 matrix according to the matrix's rotation portion
  118. Mat4 to_mat4() const;
  119. /// Returns a quaternion according to the matrix's rotation portion
  120. Quat to_quat() const;
  121. static const Mat3 IDENTITY;
  122. };
  123. } // namespace crown