matrix3.h
1 /*************************************************************************/
2 /* matrix3.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef MATRIX3_H
30 #define MATRIX3_H
31 
32 #include "vector3.h"
33 #include "quat.h"
34 
38 class Matrix3 {
39 public:
40 
41  Vector3 elements[3];
42 
43  _FORCE_INLINE_ const Vector3& operator[](int axis) const {
44 
45  return elements[axis];
46  }
47  _FORCE_INLINE_ Vector3& operator[](int axis) {
48 
49  return elements[axis];
50  }
51 
52  void invert();
53  void transpose();
54 
55  Matrix3 inverse() const;
56  Matrix3 transposed() const;
57 
58  _FORCE_INLINE_ float determinant() const;
59 
60  void from_z(const Vector3& p_z);
61 
62  _FORCE_INLINE_ Vector3 get_axis(int p_axis) const {
63  // get actual basis axis (elements is transposed for performance)
64  return Vector3( elements[0][p_axis], elements[1][p_axis], elements[2][p_axis] );
65  }
66  _FORCE_INLINE_ void set_axis(int p_axis, const Vector3& p_value) {
67  // get actual basis axis (elements is transposed for performance)
68  elements[0][p_axis]=p_value.x;
69  elements[1][p_axis]=p_value.y;
70  elements[2][p_axis]=p_value.z;
71  }
72 
73  void rotate(const Vector3& p_axis, real_t p_phi);
74  Matrix3 rotated(const Vector3& p_axis, real_t p_phi) const;
75 
76  void scale( const Vector3& p_scale );
77  Matrix3 scaled( const Vector3& p_scale ) const;
78  Vector3 get_scale() const;
79 
80  Vector3 get_euler() const;
81  void set_euler(const Vector3& p_euler);
82 
83  // transposed dot products
84  _FORCE_INLINE_ real_t tdotx(const Vector3& v) const {
85  return elements[0][0] * v[0] + elements[1][0] * v[1] + elements[2][0] * v[2];
86  }
87  _FORCE_INLINE_ real_t tdoty(const Vector3& v) const {
88  return elements[0][1] * v[0] + elements[1][1] * v[1] + elements[2][1] * v[2];
89  }
90  _FORCE_INLINE_ real_t tdotz(const Vector3& v) const {
91  return elements[0][2] * v[0] + elements[1][2] * v[1] + elements[2][2] * v[2];
92  }
93 
94  bool operator==(const Matrix3& p_matrix) const;
95  bool operator!=(const Matrix3& p_matrix) const;
96 
97  _FORCE_INLINE_ Vector3 xform(const Vector3& p_vector) const;
98  _FORCE_INLINE_ Vector3 xform_inv(const Vector3& p_vector) const;
99  _FORCE_INLINE_ void operator*=(const Matrix3& p_matrix);
100  _FORCE_INLINE_ Matrix3 operator*(const Matrix3& p_matrix) const;
101 
102  int get_orthogonal_index() const;
103  void set_orthogonal_index(int p_index);
104 
105  operator String() const;
106 
107  void get_axis_and_angle(Vector3 &r_axis,real_t& r_angle) const;
108 
109  /* create / set */
110 
111 
112  _FORCE_INLINE_ void set(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz) {
113 
114  elements[0][0]=xx;
115  elements[0][1]=xy;
116  elements[0][2]=xz;
117  elements[1][0]=yx;
118  elements[1][1]=yy;
119  elements[1][2]=yz;
120  elements[2][0]=zx;
121  elements[2][1]=zy;
122  elements[2][2]=zz;
123  }
124  _FORCE_INLINE_ Vector3 get_column(int i) const {
125 
126  return Vector3(elements[0][i],elements[1][i],elements[2][i]);
127  }
128 
129  _FORCE_INLINE_ Vector3 get_row(int i) const {
130 
131  return Vector3(elements[i][0],elements[i][1],elements[i][2]);
132  }
133  _FORCE_INLINE_ void set_row(int i, const Vector3& p_row) {
134  elements[i][0]=p_row.x;
135  elements[i][1]=p_row.y;
136  elements[i][2]=p_row.z;
137  }
138 
139  _FORCE_INLINE_ void set_zero() {
140  elements[0].zero();
141  elements[1].zero();
142  elements[2].zero();
143  }
144 
145  _FORCE_INLINE_ Matrix3 transpose_xform(const Matrix3& m) const
146  {
147  return Matrix3(
148  elements[0].x * m[0].x + elements[1].x * m[1].x + elements[2].x * m[2].x,
149  elements[0].x * m[0].y + elements[1].x * m[1].y + elements[2].x * m[2].y,
150  elements[0].x * m[0].z + elements[1].x * m[1].z + elements[2].x * m[2].z,
151  elements[0].y * m[0].x + elements[1].y * m[1].x + elements[2].y * m[2].x,
152  elements[0].y * m[0].y + elements[1].y * m[1].y + elements[2].y * m[2].y,
153  elements[0].y * m[0].z + elements[1].y * m[1].z + elements[2].y * m[2].z,
154  elements[0].z * m[0].x + elements[1].z * m[1].x + elements[2].z * m[2].x,
155  elements[0].z * m[0].y + elements[1].z * m[1].y + elements[2].z * m[2].y,
156  elements[0].z * m[0].z + elements[1].z * m[1].z + elements[2].z * m[2].z);
157  }
158  Matrix3(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz) {
159 
160  set(xx, xy, xz, yx, yy, yz, zx, zy, zz);
161  }
162 
163  void orthonormalize();
164  Matrix3 orthonormalized() const;
165 
166  operator Quat() const;
167 
168  Matrix3(const Quat& p_quat); // euler
169  Matrix3(const Vector3& p_euler); // euler
170  Matrix3(const Vector3& p_axis, real_t p_phi);
171 
172  _FORCE_INLINE_ Matrix3() {
173 
174  elements[0][0]=1;
175  elements[0][1]=0;
176  elements[0][2]=0;
177  elements[1][0]=0;
178  elements[1][1]=1;
179  elements[1][2]=0;
180  elements[2][0]=0;
181  elements[2][1]=0;
182  elements[2][2]=1;
183  }
184 
185 
186 };
187 
188 _FORCE_INLINE_ void Matrix3::operator*=(const Matrix3& p_matrix) {
189 
190  set(
191  p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]),
192  p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]),
193  p_matrix.tdotx(elements[2]), p_matrix.tdoty(elements[2]), p_matrix.tdotz(elements[2]));
194 
195 }
196 
197 _FORCE_INLINE_ Matrix3 Matrix3::operator*(const Matrix3& p_matrix) const {
198 
199  return Matrix3(
200  p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]),
201  p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]),
202  p_matrix.tdotx(elements[2]), p_matrix.tdoty(elements[2]), p_matrix.tdotz(elements[2]) );
203 
204 }
205 
206 Vector3 Matrix3::xform(const Vector3& p_vector) const {
207 
208  return Vector3(
209  elements[0].dot(p_vector),
210  elements[1].dot(p_vector),
211  elements[2].dot(p_vector)
212  );
213 }
214 
215 Vector3 Matrix3::xform_inv(const Vector3& p_vector) const {
216 
217  return Vector3(
218  (elements[0][0]*p_vector.x ) + ( elements[1][0]*p_vector.y ) + ( elements[2][0]*p_vector.z ),
219  (elements[0][1]*p_vector.x ) + ( elements[1][1]*p_vector.y ) + ( elements[2][1]*p_vector.z ),
220  (elements[0][2]*p_vector.x ) + ( elements[1][2]*p_vector.y ) + ( elements[2][2]*p_vector.z )
221  );
222 }
223 
224 float Matrix3::determinant() const {
225 
226  return elements[0][0]*(elements[1][1]*elements[2][2] - elements[2][1]*elements[1][2]) -
227  elements[1][0]*(elements[0][1]*elements[2][2] - elements[2][1]*elements[0][2]) +
228  elements[2][0]*(elements[0][1]*elements[1][2] - elements[1][1]*elements[0][2]);
229 }
230 #endif
Definition: quat.h:40
Definition: matrix3.h:38
Definition: vector3.h:38
Definition: ustring.h:64