Debian Codegraph @

core/math/transform_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* transform_3d.cpp */
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 #include "transform_3d.h"
32
33 #include "core/string/ustring.h"
34
35 void Transform3D::affine_invert() {
36 basis.invert();
37 origin = basis.xform(-origin);
38 }
39
40 Transform3D Transform3D::affine_inverse() const {
41 Transform3D ret = *this;
42 ret.affine_invert();
43 return ret;
44 }
45
46 void Transform3D::invert() {
47 basis.transpose();
48 origin = basis.xform(-origin);
49 }
50
51 Transform3D Transform3D::inverse() const {
52 // FIXME: this function assumes the basis is a rotation matrix, with no scaling.
53 // Transform3D::affine_inverse can handle matrices with scaling, so GDScript should eventually use that.
54 Transform3D ret = *this;
55 ret.invert();
56 return ret;
57 }
58
59 void Transform3D::rotate(const Vector3 &p_axis, real_t p_angle) {
60 *this = rotated(p_axis, p_angle);
61 }
62
63 Transform3D Transform3D::rotated(const Vector3 &p_axis, real_t p_angle) const {
64 // Equivalent to left multiplication
65 Basis p_basis(p_axis, p_angle);
66 return Transform3D(p_basis * basis, p_basis.xform(origin));
67 }
68
69 Transform3D Transform3D::rotated_local(const Vector3 &p_axis, real_t p_angle) const {
70 // Equivalent to right multiplication
71 Basis p_basis(p_axis, p_angle);
72 return Transform3D(basis * p_basis, origin);
73 }
74
75 void Transform3D::rotate_basis(const Vector3 &p_axis, real_t p_angle) {
76 basis.rotate(p_axis, p_angle);
77 }
78
79 Transform3D Transform3D::looking_at(const Vector3 &p_target, const Vector3 &p_up, bool p_use_model_front) const {
80 #ifdef MATH_CHECKS
81 ERR_FAIL_COND_V_MSG(origin.is_equal_approx(p_target), Transform3D(), "The transform's origin and target can't be equal.");
82 #endif
83 Transform3D t = *this;
84 t.basis = Basis::looking_at(p_target - origin, p_up, p_use_model_front);
85 return t;
86 }
87
88 void Transform3D::set_look_at(const Vector3 &p_eye, const Vector3 &p_target, const Vector3 &p_up, bool p_use_model_front) {
89 #ifdef MATH_CHECKS
90 ERR_FAIL_COND_MSG(p_eye.is_equal_approx(p_target), "The eye and target vectors can't be equal.");
91 #endif
92 basis = Basis::looking_at(p_target - p_eye, p_up, p_use_model_front);
93 origin = p_eye;
94 }
95
96 Transform3D Transform3D::interpolate_with(const Transform3D &p_transform, real_t p_c) const {
97 Transform3D interp;
98
99 Vector3 src_scale = basis.get_scale();
100 Quaternion src_rot = basis.get_rotation_quaternion();
101 Vector3 src_loc = origin;
102
103 Vector3 dst_scale = p_transform.basis.get_scale();
104 Quaternion dst_rot = p_transform.basis.get_rotation_quaternion();
105 Vector3 dst_loc = p_transform.origin;
106
107 interp.basis.set_quaternion_scale(src_rot.slerp(dst_rot, p_c).normalized(), src_scale.lerp(dst_scale, p_c));
108 interp.origin = src_loc.lerp(dst_loc, p_c);
109
110 return interp;
111 }
112
113 void Transform3D::scale(const Vector3 &p_scale) {
114 basis.scale(p_scale);
115 origin *= p_scale;
116 }
117
118 Transform3D Transform3D::scaled(const Vector3 &p_scale) const {
119 // Equivalent to left multiplication
120 return Transform3D(basis.scaled(p_scale), origin * p_scale);
121 }
122
123 Transform3D Transform3D::scaled_local(const Vector3 &p_scale) const {
124 // Equivalent to right multiplication
125 return Transform3D(basis.scaled_local(p_scale), origin);
126 }
127
128 void Transform3D::scale_basis(const Vector3 &p_scale) {
129 basis.scale(p_scale);
130 }
131
132 void Transform3D::translate_local(real_t p_tx, real_t p_ty, real_t p_tz) {
133 translate_local(Vector3(p_tx, p_ty, p_tz));
134 }
135
136 void Transform3D::translate_local(const Vector3 &p_translation) {
137 for (int i = 0; i < 3; i++) {
138 origin[i] += basis[i].dot(p_translation);
139 }
140 }
141
142 Transform3D Transform3D::translated(const Vector3 &p_translation) const {
143 // Equivalent to left multiplication
144 return Transform3D(basis, origin + p_translation);
145 }
146
147 Transform3D Transform3D::translated_local(const Vector3 &p_translation) const {
148 // Equivalent to right multiplication
149 return Transform3D(basis, origin + basis.xform(p_translation));
150 }
151
152 void Transform3D::orthonormalize() {
153 basis.orthonormalize();
154 }
155
156 Transform3D Transform3D::orthonormalized() const {
157 Transform3D _copy = *this;
158 _copy.orthonormalize();
159 return _copy;
160 }
161
162 void Transform3D::orthogonalize() {
163 basis.orthogonalize();
164 }
165
166 Transform3D Transform3D::orthogonalized() const {
167 Transform3D _copy = *this;
168 _copy.orthogonalize();
169 return _copy;
170 }
171
172 bool Transform3D::is_equal_approx(const Transform3D &p_transform) const {
173 return basis.is_equal_approx(p_transform.basis) && origin.is_equal_approx(p_transform.origin);
174 }
175
176 bool Transform3D::is_same(const Transform3D &p_transform) const {
177 return basis.is_same(p_transform.basis) && origin.is_same(p_transform.origin);
178 }
179
180 bool Transform3D::is_finite() const {
181 return basis.is_finite() && origin.is_finite();
182 }
183
184 void Transform3D::operator*=(const Transform3D &p_transform) {
185 origin = xform(p_transform.origin);
186 basis *= p_transform.basis;
187 }
188
189 Transform3D Transform3D::operator*(const Transform3D &p_transform) const {
190 Transform3D t = *this;
191 t *= p_transform;
192 return t;
193 }
194
195 Transform3D::operator String() const {
196 return "[X: " + basis.get_column(0).operator String() +
197 ", Y: " + basis.get_column(1).operator String() +
198 ", Z: " + basis.get_column(2).operator String() +
199 ", O: " + origin.operator String() + "]";
200 }