Debian Codegraph @

core/math/transform_3d.h

raw symbols SCIP
1 /**************************************************************************/
2 /* transform_3d.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 #pragma once
32
33 #include "core/math/aabb.h"
34 #include "core/math/basis.h"
35 #include "core/math/plane.h"
36 #include "core/templates/vector.h"
37
38 struct [[nodiscard]] Transform3D {
39 static const Transform3D FLIP_X;
40 static const Transform3D FLIP_Y;
41 static const Transform3D FLIP_Z;
42
43 Basis basis;
44 Vector3 origin;
45
46 void invert();
47 Transform3D inverse() const;
48
49 void affine_invert();
50 Transform3D affine_inverse() const;
51
52 Transform3D rotated(const Vector3 &p_axis, real_t p_angle) const;
53 Transform3D rotated_local(const Vector3 &p_axis, real_t p_angle) const;
54
55 void rotate(const Vector3 &p_axis, real_t p_angle);
56 void rotate_basis(const Vector3 &p_axis, real_t p_angle);
57
58 void set_look_at(const Vector3 &p_eye, const Vector3 &p_target, const Vector3 &p_up = Vector3::UP, bool p_use_model_front = false);
59 Transform3D looking_at(const Vector3 &p_target, const Vector3 &p_up = Vector3::UP, bool p_use_model_front = false) const;
60
61 void scale(const Vector3 &p_scale);
62 Transform3D scaled(const Vector3 &p_scale) const;
63 Transform3D scaled_local(const Vector3 &p_scale) const;
64 void scale_basis(const Vector3 &p_scale);
65 void translate_local(real_t p_tx, real_t p_ty, real_t p_tz);
66 void translate_local(const Vector3 &p_translation);
67 Transform3D translated(const Vector3 &p_translation) const;
68 Transform3D translated_local(const Vector3 &p_translation) const;
69
70 const Basis &get_basis() const { return basis; }
71 void set_basis(const Basis &p_basis) { basis = p_basis; }
72
73 const Vector3 &get_origin() const { return origin; }
74 void set_origin(const Vector3 &p_origin) { origin = p_origin; }
75
76 void orthonormalize();
77 Transform3D orthonormalized() const;
78 void orthogonalize();
79 Transform3D orthogonalized() const;
80 bool is_equal_approx(const Transform3D &p_transform) const;
81 bool is_same(const Transform3D &p_transform) const;
82 bool is_finite() const;
83
84 constexpr bool operator==(const Transform3D &p_transform) const;
85 constexpr bool operator!=(const Transform3D &p_transform) const;
86
87 _FORCE_INLINE_ Vector3 xform(const Vector3 &p_vector) const;
88 _FORCE_INLINE_ AABB xform(const AABB &p_aabb) const;
89 _FORCE_INLINE_ Vector<Vector3> xform(const Vector<Vector3> &p_array) const;
90
91 // NOTE: These are UNSAFE with non-uniform scaling, and will produce incorrect results.
92 // They use the transpose.
93 // For safe inverse transforms, xform by the affine_inverse.
94 _FORCE_INLINE_ Vector3 xform_inv(const Vector3 &p_vector) const;
95 _FORCE_INLINE_ AABB xform_inv(const AABB &p_aabb) const;
96 _FORCE_INLINE_ Vector<Vector3> xform_inv(const Vector<Vector3> &p_array) const;
97
98 // Safe with non-uniform scaling (uses affine_inverse).
99 _FORCE_INLINE_ Plane xform(const Plane &p_plane) const;
100 _FORCE_INLINE_ Plane xform_inv(const Plane &p_plane) const;
101
102 // These fast versions use precomputed affine inverse, and should be used in bottleneck areas where
103 // multiple planes are to be transformed.
104 _FORCE_INLINE_ Plane xform_fast(const Plane &p_plane, const Basis &p_basis_inverse_transpose) const;
105 static _FORCE_INLINE_ Plane xform_inv_fast(const Plane &p_plane, const Transform3D &p_inverse, const Basis &p_basis_transpose);
106
107 void operator*=(const Transform3D &p_transform);
108 Transform3D operator*(const Transform3D &p_transform) const;
109 constexpr void operator*=(real_t p_val);
110 constexpr Transform3D operator*(real_t p_val) const;
111 constexpr void operator/=(real_t p_val);
112 constexpr Transform3D operator/(real_t p_val) const;
113
114 Transform3D interpolate_with(const Transform3D &p_transform, real_t p_c) const;
115
116 _FORCE_INLINE_ Transform3D inverse_xform(const Transform3D &t) const {
117 Vector3 v = t.origin - origin;
118 return Transform3D(basis.transpose_xform(t.basis),
119 basis.xform(v));
120 }
121
122 void set(real_t p_xx, real_t p_xy, real_t p_xz, real_t p_yx, real_t p_yy, real_t p_yz, real_t p_zx, real_t p_zy, real_t p_zz, real_t p_tx, real_t p_ty, real_t p_tz) {
123 basis.set(p_xx, p_xy, p_xz, p_yx, p_yy, p_yz, p_zx, p_zy, p_zz);
124 origin.x = p_tx;
125 origin.y = p_ty;
126 origin.z = p_tz;
127 }
128
129 explicit operator String() const;
130
131 Transform3D() = default;
132 constexpr Transform3D(const Basis &p_basis, const Vector3 &p_origin = Vector3()) :
133 basis(p_basis),
134 origin(p_origin) {}
135 constexpr Transform3D(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z, const Vector3 &p_origin) :
136 basis(p_x, p_y, p_z),
137 origin(p_origin) {}
138 constexpr Transform3D(real_t p_xx, real_t p_xy, real_t p_xz, real_t p_yx, real_t p_yy, real_t p_yz, real_t p_zx, real_t p_zy, real_t p_zz, real_t p_ox, real_t p_oy, real_t p_oz) :
139 basis(p_xx, p_xy, p_xz, p_yx, p_yy, p_yz, p_zx, p_zy, p_zz),
140 origin(p_ox, p_oy, p_oz) {}
141 };
142
143 inline constexpr Transform3D Transform3D::FLIP_X = { Basis::FLIP_X };
144 inline constexpr Transform3D Transform3D::FLIP_Y = { Basis::FLIP_Y };
145 inline constexpr Transform3D Transform3D::FLIP_Z = { Basis::FLIP_Z };
146
147 constexpr bool Transform3D::operator==(const Transform3D &p_transform) const {
148 return (basis == p_transform.basis && origin == p_transform.origin);
149 }
150
151 constexpr bool Transform3D::operator!=(const Transform3D &p_transform) const {
152 return (basis != p_transform.basis || origin != p_transform.origin);
153 }
154
155 constexpr void Transform3D::operator*=(real_t p_val) {
156 origin *= p_val;
157 basis *= p_val;
158 }
159
160 constexpr Transform3D Transform3D::operator*(real_t p_val) const {
161 Transform3D ret(*this);
162 ret *= p_val;
163 return ret;
164 }
165
166 constexpr void Transform3D::operator/=(real_t p_val) {
167 basis /= p_val;
168 origin /= p_val;
169 }
170
171 constexpr Transform3D Transform3D::operator/(real_t p_val) const {
172 Transform3D ret(*this);
173 ret /= p_val;
174 return ret;
175 }
176
177 _FORCE_INLINE_ Vector3 Transform3D::xform(const Vector3 &p_vector) const {
178 return Vector3(
179 basis[0].dot(p_vector) + origin.x,
180 basis[1].dot(p_vector) + origin.y,
181 basis[2].dot(p_vector) + origin.z);
182 }
183
184 _FORCE_INLINE_ Vector3 Transform3D::xform_inv(const Vector3 &p_vector) const {
185 Vector3 v = p_vector - origin;
186
187 return Vector3(
188 (basis.rows[0][0] * v.x) + (basis.rows[1][0] * v.y) + (basis.rows[2][0] * v.z),
189 (basis.rows[0][1] * v.x) + (basis.rows[1][1] * v.y) + (basis.rows[2][1] * v.z),
190 (basis.rows[0][2] * v.x) + (basis.rows[1][2] * v.y) + (basis.rows[2][2] * v.z));
191 }
192
193 // Neither the plane regular xform or xform_inv are particularly efficient,
194 // as they do a basis inverse. For xforming a large number
195 // of planes it is better to pre-calculate the inverse transpose basis once
196 // and reuse it for each plane, by using the 'fast' version of the functions.
197 _FORCE_INLINE_ Plane Transform3D::xform(const Plane &p_plane) const {
198 Basis b = basis.inverse();
199 b.transpose();
200 return xform_fast(p_plane, b);
201 }
202
203 _FORCE_INLINE_ Plane Transform3D::xform_inv(const Plane &p_plane) const {
204 Transform3D inv = affine_inverse();
205 Basis basis_transpose = basis.transposed();
206 return xform_inv_fast(p_plane, inv, basis_transpose);
207 }
208
209 _FORCE_INLINE_ AABB Transform3D::xform(const AABB &p_aabb) const {
210 /* https://dev.theomader.com/transform-bounding-boxes/ */
211 Vector3 min = p_aabb.position;
212 Vector3 max = p_aabb.position + p_aabb.size;
213 Vector3 tmin, tmax;
214 for (int i = 0; i < 3; i++) {
215 tmin[i] = tmax[i] = origin[i];
216 for (int j = 0; j < 3; j++) {
217 real_t e = basis[i][j] * min[j];
218 real_t f = basis[i][j] * max[j];
219 if (e < f) {
220 tmin[i] += e;
221 tmax[i] += f;
222 } else {
223 tmin[i] += f;
224 tmax[i] += e;
225 }
226 }
227 }
228 AABB r_aabb;
229 r_aabb.position = tmin;
230 r_aabb.size = tmax - tmin;
231 return r_aabb;
232 }
233
234 _FORCE_INLINE_ AABB Transform3D::xform_inv(const AABB &p_aabb) const {
235 /* define vertices */
236 Vector3 vertices[8] = {
237 Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z),
238 Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z),
239 Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z),
240 Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z),
241 Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z),
242 Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z),
243 Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z),
244 Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z)
245 };
246
247 AABB ret;
248
249 ret.position = xform_inv(vertices[0]);
250
251 for (int i = 1; i < 8; i++) {
252 ret.expand_to(xform_inv(vertices[i]));
253 }
254
255 return ret;
256 }
257
258 Vector<Vector3> Transform3D::xform(const Vector<Vector3> &p_array) const {
259 Vector<Vector3> array;
260 array.resize(p_array.size());
261
262 const Vector3 *r = p_array.ptr();
263 Vector3 *w = array.ptrw();
264
265 for (int i = 0; i < p_array.size(); ++i) {
266 w[i] = xform(r[i]);
267 }
268 return array;
269 }
270
271 Vector<Vector3> Transform3D::xform_inv(const Vector<Vector3> &p_array) const {
272 Vector<Vector3> array;
273 array.resize(p_array.size());
274
275 const Vector3 *r = p_array.ptr();
276 Vector3 *w = array.ptrw();
277
278 for (int i = 0; i < p_array.size(); ++i) {
279 w[i] = xform_inv(r[i]);
280 }
281 return array;
282 }
283
284 _FORCE_INLINE_ Plane Transform3D::xform_fast(const Plane &p_plane, const Basis &p_basis_inverse_transpose) const {
285 // Transform a single point on the plane.
286 Vector3 point = p_plane.normal * p_plane.d;
287 point = xform(point);
288
289 // Use inverse transpose for correct normals with non-uniform scaling.
290 Vector3 normal = p_basis_inverse_transpose.xform(p_plane.normal);
291 normal.normalize();
292
293 real_t d = normal.dot(point);
294 return Plane(normal, d);
295 }
296
297 _FORCE_INLINE_ Plane Transform3D::xform_inv_fast(const Plane &p_plane, const Transform3D &p_inverse, const Basis &p_basis_transpose) {
298 // Transform a single point on the plane.
299 Vector3 point = p_plane.normal * p_plane.d;
300 point = p_inverse.xform(point);
301
302 // Note that instead of precalculating the transpose, an alternative
303 // would be to use the transpose for the basis transform.
304 // However that would be less SIMD friendly (requiring a swizzle).
305 // So the cost is one extra precalced value in the calling code.
306 // This is probably worth it, as this could be used in bottleneck areas. And
307 // where it is not a bottleneck, the non-fast method is fine.
308
309 // Use transpose for correct normals with non-uniform scaling.
310 Vector3 normal = p_basis_transpose.xform(p_plane.normal);
311 normal.normalize();
312
313 real_t d = normal.dot(point);
314 return Plane(normal, d);
315 }