1 | /**************************************************************************/ |
2 | /* basis.h */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #ifndef BASIS_H |
32 | #define BASIS_H |
33 | |
34 | #include "core/math/quaternion.h" |
35 | #include "core/math/vector3.h" |
36 | |
37 | struct _NO_DISCARD_ Basis { |
38 | Vector3 rows[3] = { |
39 | Vector3(1, 0, 0), |
40 | Vector3(0, 1, 0), |
41 | Vector3(0, 0, 1) |
42 | }; |
43 | |
44 | _FORCE_INLINE_ const Vector3 &operator[](int axis) const { |
45 | return rows[axis]; |
46 | } |
47 | _FORCE_INLINE_ Vector3 &operator[](int axis) { |
48 | return rows[axis]; |
49 | } |
50 | |
51 | void invert(); |
52 | void transpose(); |
53 | |
54 | Basis inverse() const; |
55 | Basis transposed() const; |
56 | |
57 | _FORCE_INLINE_ real_t determinant() const; |
58 | |
59 | void rotate(const Vector3 &p_axis, real_t p_angle); |
60 | Basis rotated(const Vector3 &p_axis, real_t p_angle) const; |
61 | |
62 | void rotate_local(const Vector3 &p_axis, real_t p_angle); |
63 | Basis rotated_local(const Vector3 &p_axis, real_t p_angle) const; |
64 | |
65 | void rotate(const Vector3 &p_euler, EulerOrder p_order = EulerOrder::YXZ); |
66 | Basis rotated(const Vector3 &p_euler, EulerOrder p_order = EulerOrder::YXZ) const; |
67 | |
68 | void rotate(const Quaternion &p_quaternion); |
69 | Basis rotated(const Quaternion &p_quaternion) const; |
70 | |
71 | Vector3 get_euler_normalized(EulerOrder p_order = EulerOrder::YXZ) const; |
72 | void get_rotation_axis_angle(Vector3 &p_axis, real_t &p_angle) const; |
73 | void get_rotation_axis_angle_local(Vector3 &p_axis, real_t &p_angle) const; |
74 | Quaternion get_rotation_quaternion() const; |
75 | |
76 | void rotate_to_align(Vector3 p_start_direction, Vector3 p_end_direction); |
77 | |
78 | Vector3 rotref_posscale_decomposition(Basis &rotref) const; |
79 | |
80 | Vector3 get_euler(EulerOrder p_order = EulerOrder::YXZ) const; |
81 | void set_euler(const Vector3 &p_euler, EulerOrder p_order = EulerOrder::YXZ); |
82 | static Basis from_euler(const Vector3 &p_euler, EulerOrder p_order = EulerOrder::YXZ) { |
83 | Basis b; |
84 | b.set_euler(p_euler, p_order); |
85 | return b; |
86 | } |
87 | |
88 | Quaternion get_quaternion() const; |
89 | void set_quaternion(const Quaternion &p_quaternion); |
90 | |
91 | void get_axis_angle(Vector3 &r_axis, real_t &r_angle) const; |
92 | void set_axis_angle(const Vector3 &p_axis, real_t p_angle); |
93 | |
94 | void scale(const Vector3 &p_scale); |
95 | Basis scaled(const Vector3 &p_scale) const; |
96 | |
97 | void scale_local(const Vector3 &p_scale); |
98 | Basis scaled_local(const Vector3 &p_scale) const; |
99 | |
100 | void scale_orthogonal(const Vector3 &p_scale); |
101 | Basis scaled_orthogonal(const Vector3 &p_scale) const; |
102 | float get_uniform_scale() const; |
103 | |
104 | Vector3 get_scale() const; |
105 | Vector3 get_scale_abs() const; |
106 | Vector3 get_scale_local() const; |
107 | |
108 | void set_axis_angle_scale(const Vector3 &p_axis, real_t p_angle, const Vector3 &p_scale); |
109 | void set_euler_scale(const Vector3 &p_euler, const Vector3 &p_scale, EulerOrder p_order = EulerOrder::YXZ); |
110 | void set_quaternion_scale(const Quaternion &p_quaternion, const Vector3 &p_scale); |
111 | |
112 | // transposed dot products |
113 | _FORCE_INLINE_ real_t tdotx(const Vector3 &v) const { |
114 | return rows[0][0] * v[0] + rows[1][0] * v[1] + rows[2][0] * v[2]; |
115 | } |
116 | _FORCE_INLINE_ real_t tdoty(const Vector3 &v) const { |
117 | return rows[0][1] * v[0] + rows[1][1] * v[1] + rows[2][1] * v[2]; |
118 | } |
119 | _FORCE_INLINE_ real_t tdotz(const Vector3 &v) const { |
120 | return rows[0][2] * v[0] + rows[1][2] * v[1] + rows[2][2] * v[2]; |
121 | } |
122 | |
123 | bool is_equal_approx(const Basis &p_basis) const; |
124 | bool is_finite() const; |
125 | |
126 | bool operator==(const Basis &p_matrix) const; |
127 | bool operator!=(const Basis &p_matrix) const; |
128 | |
129 | _FORCE_INLINE_ Vector3 xform(const Vector3 &p_vector) const; |
130 | _FORCE_INLINE_ Vector3 xform_inv(const Vector3 &p_vector) const; |
131 | _FORCE_INLINE_ void operator*=(const Basis &p_matrix); |
132 | _FORCE_INLINE_ Basis operator*(const Basis &p_matrix) const; |
133 | _FORCE_INLINE_ void operator+=(const Basis &p_matrix); |
134 | _FORCE_INLINE_ Basis operator+(const Basis &p_matrix) const; |
135 | _FORCE_INLINE_ void operator-=(const Basis &p_matrix); |
136 | _FORCE_INLINE_ Basis operator-(const Basis &p_matrix) const; |
137 | _FORCE_INLINE_ void operator*=(const real_t p_val); |
138 | _FORCE_INLINE_ Basis operator*(const real_t p_val) const; |
139 | |
140 | bool is_orthogonal() const; |
141 | bool is_diagonal() const; |
142 | bool is_rotation() const; |
143 | |
144 | Basis lerp(const Basis &p_to, const real_t &p_weight) const; |
145 | Basis slerp(const Basis &p_to, const real_t &p_weight) const; |
146 | void rotate_sh(real_t *p_values); |
147 | |
148 | operator String() const; |
149 | |
150 | /* create / set */ |
151 | |
152 | _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) { |
153 | rows[0][0] = xx; |
154 | rows[0][1] = xy; |
155 | rows[0][2] = xz; |
156 | rows[1][0] = yx; |
157 | rows[1][1] = yy; |
158 | rows[1][2] = yz; |
159 | rows[2][0] = zx; |
160 | rows[2][1] = zy; |
161 | rows[2][2] = zz; |
162 | } |
163 | _FORCE_INLINE_ void set_columns(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z) { |
164 | set_column(0, p_x); |
165 | set_column(1, p_y); |
166 | set_column(2, p_z); |
167 | } |
168 | |
169 | _FORCE_INLINE_ Vector3 get_column(int p_index) const { |
170 | // Get actual basis axis column (we store transposed as rows for performance). |
171 | return Vector3(rows[0][p_index], rows[1][p_index], rows[2][p_index]); |
172 | } |
173 | |
174 | _FORCE_INLINE_ void set_column(int p_index, const Vector3 &p_value) { |
175 | // Set actual basis axis column (we store transposed as rows for performance). |
176 | rows[0][p_index] = p_value.x; |
177 | rows[1][p_index] = p_value.y; |
178 | rows[2][p_index] = p_value.z; |
179 | } |
180 | |
181 | _FORCE_INLINE_ Vector3 get_main_diagonal() const { |
182 | return Vector3(rows[0][0], rows[1][1], rows[2][2]); |
183 | } |
184 | |
185 | _FORCE_INLINE_ void set_zero() { |
186 | rows[0].zero(); |
187 | rows[1].zero(); |
188 | rows[2].zero(); |
189 | } |
190 | |
191 | _FORCE_INLINE_ Basis transpose_xform(const Basis &m) const { |
192 | return Basis( |
193 | rows[0].x * m[0].x + rows[1].x * m[1].x + rows[2].x * m[2].x, |
194 | rows[0].x * m[0].y + rows[1].x * m[1].y + rows[2].x * m[2].y, |
195 | rows[0].x * m[0].z + rows[1].x * m[1].z + rows[2].x * m[2].z, |
196 | rows[0].y * m[0].x + rows[1].y * m[1].x + rows[2].y * m[2].x, |
197 | rows[0].y * m[0].y + rows[1].y * m[1].y + rows[2].y * m[2].y, |
198 | rows[0].y * m[0].z + rows[1].y * m[1].z + rows[2].y * m[2].z, |
199 | rows[0].z * m[0].x + rows[1].z * m[1].x + rows[2].z * m[2].x, |
200 | rows[0].z * m[0].y + rows[1].z * m[1].y + rows[2].z * m[2].y, |
201 | rows[0].z * m[0].z + rows[1].z * m[1].z + rows[2].z * m[2].z); |
202 | } |
203 | Basis(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) { |
204 | set(xx, xy, xz, yx, yy, yz, zx, zy, zz); |
205 | } |
206 | |
207 | void orthonormalize(); |
208 | Basis orthonormalized() const; |
209 | |
210 | void orthogonalize(); |
211 | Basis orthogonalized() const; |
212 | |
213 | #ifdef MATH_CHECKS |
214 | bool is_symmetric() const; |
215 | #endif |
216 | Basis diagonalize(); |
217 | |
218 | operator Quaternion() const { return get_quaternion(); } |
219 | |
220 | static Basis looking_at(const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0), bool p_use_model_front = false); |
221 | |
222 | Basis(const Quaternion &p_quaternion) { set_quaternion(p_quaternion); }; |
223 | Basis(const Quaternion &p_quaternion, const Vector3 &p_scale) { set_quaternion_scale(p_quaternion, p_scale); } |
224 | |
225 | Basis(const Vector3 &p_axis, real_t p_angle) { set_axis_angle(p_axis, p_angle); } |
226 | Basis(const Vector3 &p_axis, real_t p_angle, const Vector3 &p_scale) { set_axis_angle_scale(p_axis, p_angle, p_scale); } |
227 | static Basis from_scale(const Vector3 &p_scale); |
228 | |
229 | _FORCE_INLINE_ Basis(const Vector3 &p_x_axis, const Vector3 &p_y_axis, const Vector3 &p_z_axis) { |
230 | set_columns(p_x_axis, p_y_axis, p_z_axis); |
231 | } |
232 | |
233 | _FORCE_INLINE_ Basis() {} |
234 | |
235 | private: |
236 | // Helper method. |
237 | void _set_diagonal(const Vector3 &p_diag); |
238 | }; |
239 | |
240 | _FORCE_INLINE_ void Basis::operator*=(const Basis &p_matrix) { |
241 | set( |
242 | p_matrix.tdotx(rows[0]), p_matrix.tdoty(rows[0]), p_matrix.tdotz(rows[0]), |
243 | p_matrix.tdotx(rows[1]), p_matrix.tdoty(rows[1]), p_matrix.tdotz(rows[1]), |
244 | p_matrix.tdotx(rows[2]), p_matrix.tdoty(rows[2]), p_matrix.tdotz(rows[2])); |
245 | } |
246 | |
247 | _FORCE_INLINE_ Basis Basis::operator*(const Basis &p_matrix) const { |
248 | return Basis( |
249 | p_matrix.tdotx(rows[0]), p_matrix.tdoty(rows[0]), p_matrix.tdotz(rows[0]), |
250 | p_matrix.tdotx(rows[1]), p_matrix.tdoty(rows[1]), p_matrix.tdotz(rows[1]), |
251 | p_matrix.tdotx(rows[2]), p_matrix.tdoty(rows[2]), p_matrix.tdotz(rows[2])); |
252 | } |
253 | |
254 | _FORCE_INLINE_ void Basis::operator+=(const Basis &p_matrix) { |
255 | rows[0] += p_matrix.rows[0]; |
256 | rows[1] += p_matrix.rows[1]; |
257 | rows[2] += p_matrix.rows[2]; |
258 | } |
259 | |
260 | _FORCE_INLINE_ Basis Basis::operator+(const Basis &p_matrix) const { |
261 | Basis ret(*this); |
262 | ret += p_matrix; |
263 | return ret; |
264 | } |
265 | |
266 | _FORCE_INLINE_ void Basis::operator-=(const Basis &p_matrix) { |
267 | rows[0] -= p_matrix.rows[0]; |
268 | rows[1] -= p_matrix.rows[1]; |
269 | rows[2] -= p_matrix.rows[2]; |
270 | } |
271 | |
272 | _FORCE_INLINE_ Basis Basis::operator-(const Basis &p_matrix) const { |
273 | Basis ret(*this); |
274 | ret -= p_matrix; |
275 | return ret; |
276 | } |
277 | |
278 | _FORCE_INLINE_ void Basis::operator*=(const real_t p_val) { |
279 | rows[0] *= p_val; |
280 | rows[1] *= p_val; |
281 | rows[2] *= p_val; |
282 | } |
283 | |
284 | _FORCE_INLINE_ Basis Basis::operator*(const real_t p_val) const { |
285 | Basis ret(*this); |
286 | ret *= p_val; |
287 | return ret; |
288 | } |
289 | |
290 | Vector3 Basis::xform(const Vector3 &p_vector) const { |
291 | return Vector3( |
292 | rows[0].dot(p_vector), |
293 | rows[1].dot(p_vector), |
294 | rows[2].dot(p_vector)); |
295 | } |
296 | |
297 | Vector3 Basis::xform_inv(const Vector3 &p_vector) const { |
298 | return Vector3( |
299 | (rows[0][0] * p_vector.x) + (rows[1][0] * p_vector.y) + (rows[2][0] * p_vector.z), |
300 | (rows[0][1] * p_vector.x) + (rows[1][1] * p_vector.y) + (rows[2][1] * p_vector.z), |
301 | (rows[0][2] * p_vector.x) + (rows[1][2] * p_vector.y) + (rows[2][2] * p_vector.z)); |
302 | } |
303 | |
304 | real_t Basis::determinant() const { |
305 | return rows[0][0] * (rows[1][1] * rows[2][2] - rows[2][1] * rows[1][2]) - |
306 | rows[1][0] * (rows[0][1] * rows[2][2] - rows[2][1] * rows[0][2]) + |
307 | rows[2][0] * (rows[0][1] * rows[1][2] - rows[1][1] * rows[0][2]); |
308 | } |
309 | |
310 | #endif // BASIS_H |
311 | |