Blender V5.3
gpu_shader_math_rotation_conversion.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
14
15namespace detail {
16
18{
19 /* Caller must ensure matrices aren't negative for valid results, see: #24291, #94231. */
20 Quaternion q;
21
22 /* Method outlined by Mike Day, ref: https://math.stackexchange.com/a/3183435/220949
23 * with an additional `sqrtf(..)` for higher precision result.
24 * Removing the `sqrt` causes tests to fail unless the precision is set to 1e-6f or larger. */
25
26 if (mat[2][2] < 0.0f) {
27 if (mat[0][0] > mat[1][1]) {
28 float trace = 1.0f + mat[0][0] - mat[1][1] - mat[2][2];
29 float s = 2.0f * sqrt(trace);
30 if (mat[1][2] < mat[2][1]) {
31 /* Ensure W is non-negative for a canonical result. */
32 s = -s;
33 }
34 q.y = 0.25f * s;
35 s = 1.0f / s;
36 q.x = (mat[1][2] - mat[2][1]) * s;
37 q.z = (mat[0][1] + mat[1][0]) * s;
38 q.w = (mat[2][0] + mat[0][2]) * s;
39 if ((trace == 1.0f) && (q.x == 0.0f && q.z == 0.0f && q.w == 0.0f)) {
40 /* Avoids the need to normalize the degenerate case. */
41 q.y = 1.0f;
42 }
43 }
44 else {
45 float trace = 1.0f - mat[0][0] + mat[1][1] - mat[2][2];
46 float s = 2.0f * sqrt(trace);
47 if (mat[2][0] < mat[0][2]) {
48 /* Ensure W is non-negative for a canonical result. */
49 s = -s;
50 }
51 q.z = 0.25f * s;
52 s = 1.0f / s;
53 q.x = (mat[2][0] - mat[0][2]) * s;
54 q.y = (mat[0][1] + mat[1][0]) * s;
55 q.w = (mat[1][2] + mat[2][1]) * s;
56 if ((trace == 1.0f) && (q.x == 0.0f && q.y == 0.0f && q.w == 0.0f)) {
57 /* Avoids the need to normalize the degenerate case. */
58 q.z = 1.0f;
59 }
60 }
61 }
62 else {
63 if (mat[0][0] < -mat[1][1]) {
64 float trace = 1.0f - mat[0][0] - mat[1][1] + mat[2][2];
65 float s = 2.0f * sqrt(trace);
66 if (mat[0][1] < mat[1][0]) {
67 /* Ensure W is non-negative for a canonical result. */
68 s = -s;
69 }
70 q.w = 0.25f * s;
71 s = 1.0f / s;
72 q.x = (mat[0][1] - mat[1][0]) * s;
73 q.y = (mat[2][0] + mat[0][2]) * s;
74 q.z = (mat[1][2] + mat[2][1]) * s;
75 if ((trace == 1.0f) && (q.x == 0.0f && q.y == 0.0f && q.z == 0.0f)) {
76 /* Avoids the need to normalize the degenerate case. */
77 q.w = 1.0f;
78 }
79 }
80 else {
81 /* NOTE(@ideasman42): A zero matrix will fall through to this block,
82 * needed so a zero scaled matrices to return a quaternion without rotation, see: #101848. */
83 float trace = 1.0f + mat[0][0] + mat[1][1] + mat[2][2];
84 float s = 2.0f * sqrt(trace);
85 q.x = 0.25f * s;
86 s = 1.0f / s;
87 q.y = (mat[1][2] - mat[2][1]) * s;
88 q.z = (mat[2][0] - mat[0][2]) * s;
89 q.w = (mat[0][1] - mat[1][0]) * s;
90 if ((trace == 1.0f) && (q.y == 0.0f && q.z == 0.0f && q.w == 0.0f)) {
91 /* Avoids the need to normalize the degenerate case. */
92 q.x = 1.0f;
93 }
94 }
95 }
96 return q;
97}
98
100{
101 float det = determinant(mat);
102 if (isinf(det) || isnan(det)) {
103 return Quaternion::identity();
104 }
105 if (det < 0.0f) {
106 return normalized_to_quat_fast(-mat);
107 }
108 return normalized_to_quat_fast(mat);
109}
110
112{
113 float cy = hypot(mat[0][0], mat[0][1]);
114 if (cy > 16.0f * FLT_EPSILON) {
115 eul1.x = atan(mat[1][2], mat[2][2]);
116 eul1.y = atan(-mat[0][2], cy);
117 eul1.z = atan(mat[0][1], mat[0][0]);
118
119 eul2.x = atan(-mat[1][2], -mat[2][2]);
120 eul2.y = atan(-mat[0][2], -cy);
121 eul2.z = atan(-mat[0][1], -mat[0][0]);
122 }
123 else {
124 eul1.x = atan(-mat[2][1], mat[1][1]);
125 eul1.y = atan(-mat[0][2], cy);
126 eul1.z = 0.0f;
127
128 eul2 = eul1;
129 }
130}
131
132} // namespace detail
133
134/* -------------------------------------------------------------------- */
137
139{
140 float ti = eul.x * 0.5f;
141 float tj = eul.y * 0.5f;
142 float th = eul.z * 0.5f;
143 float ci = cos(ti);
144 float cj = cos(tj);
145 float ch = cos(th);
146 float si = sin(ti);
147 float sj = sin(tj);
148 float sh = sin(th);
149 float cc = ci * ch;
150 float cs = ci * sh;
151 float sc = si * ch;
152 float ss = si * sh;
153
154 Quaternion quat;
155 quat.x = cj * cc + sj * ss;
156 quat.y = cj * sc - sj * cs;
157 quat.z = cj * ss + sj * cc;
158 quat.w = cj * cs - sj * sc;
159 return quat;
160}
161
170
175{
176 if (!normalized) {
177 mat = normalize(mat);
178 }
179 return to_quaternion(mat);
180}
181
186{
187 return to_quaternion(to_float3x3(mat));
188}
189
194{
195 return to_quaternion(to_float3x3(mat), normalized);
196}
197
199{
200 float angle_cos = cos(axis_angle.angle);
202 float sine = sqrt(0.5f - angle_cos * 0.5f);
203 float cosine = sqrt(0.5f + angle_cos * 0.5f);
204
205 /* TODO(fclem): Optimize. */
206 float angle_sin = sin(axis_angle.angle);
207 if (angle_sin < 0.0f) {
208 sine = -sine;
209 }
210
211 Quaternion quat;
212 quat.x = cosine;
213 quat.y = axis_angle.axis.x * sine;
214 quat.z = axis_angle.axis.y * sine;
215 quat.w = axis_angle.axis.z * sine;
216 return quat;
217}
218
220{
221 if (is_zero(axis)) {
222 return Quaternion::identity();
223 }
224 AxisAngle aa;
225 aa.axis = normalize(axis);
226 aa.angle = angle;
227 return to_quaternion(aa);
228}
229
231
232/* -------------------------------------------------------------------- */
235
241{
242 if (!normalized) {
243 mat = normalize(mat);
244 }
245 EulerXYZ eul1, eul2;
246 detail::normalized_to_eul2(mat, eul1, eul2);
247 /* Return best, which is just the one with lowest values it in. */
248 return (length_manhattan(eul1.as_float3()) > length_manhattan(eul2.as_float3())) ? eul2 : eul1;
249}
250
255{
256 return to_euler(mat, true);
257}
258
263{
264 return to_euler(to_float3x3(mat), normalized);
265}
266
271{
272 return to_euler(to_float3x3(mat));
273}
274
276
277/* -------------------------------------------------------------------- */
280
282{
283 /* Calculate angle/2, and sin(angle/2). */
284 float ha = acos(quat.x);
285 float si = sin(ha);
286
287 /* From half-angle to angle. */
288 float angle = ha * 2;
289 /* Prevent division by zero for axis conversion. */
290 if (abs(si) < 0.0005f) {
291 si = 1.0f;
292 }
293
294 float3 axis = float3(quat.y, quat.z, quat.w) / si;
295 if (is_zero(axis)) {
296 axis[1] = 1.0f;
297 }
298 return {axis, angle};
299}
300
302{
303 /* Use quaternions as intermediate representation for now... */
304 return to_axis_angle(to_quaternion(eul));
305}
306
308
309/* -------------------------------------------------------------------- */
312
319void to_rot_scale(float3x3 mat, EulerXYZ &r_rotation, float3 &r_scale)
320{
321 r_scale = to_scale(mat);
322 r_rotation = to_euler(mat, true);
323}
324
332 EulerXYZ &r_rotation,
333 float3 &r_scale,
334 const bool allow_negative_scale)
335{
336 float3x3 normalized_mat = normalize_and_get_size(mat, r_scale);
337 if (allow_negative_scale) {
338 if (is_negative(normalized_mat)) {
339 normalized_mat = -normalized_mat;
340 r_scale = -r_scale;
341 }
342 }
343 r_rotation = to_euler(normalized_mat, true);
344}
345void to_rot_scale(float3x3 mat, Quaternion &r_rotation, float3 &r_scale)
346{
347 r_scale = to_scale(mat);
348 r_rotation = to_quaternion(mat, true);
349}
351 Quaternion &r_rotation,
352 float3 &r_scale,
353 const bool allow_negative_scale)
354{
355 float3x3 normalized_mat = normalize_and_get_size(mat, r_scale);
356 if (allow_negative_scale) {
357 if (is_negative(normalized_mat)) {
358 normalized_mat = -normalized_mat;
359 r_scale = -r_scale;
360 }
361 }
362 r_rotation = to_quaternion(normalized_mat, true);
363}
364
365void to_loc_rot_scale(float4x4 mat, float3 &r_location, EulerXYZ &r_rotation, float3 &r_scale)
366{
367 r_location = mat[3].xyz;
368 to_rot_scale(to_float3x3(mat), r_rotation, r_scale);
369}
371 float3 &r_location,
372 EulerXYZ &r_rotation,
373 float3 &r_scale,
374 const bool allow_negative_scale)
375{
376 r_location = mat[3].xyz;
377 to_rot_scale(to_float3x3(mat), r_rotation, r_scale, allow_negative_scale);
378}
379void to_loc_rot_scale(float4x4 mat, float3 &r_location, Quaternion &r_rotation, float3 &r_scale)
380{
381 r_location = mat[3].xyz;
382 to_rot_scale(to_float3x3(mat), r_rotation, r_scale);
383}
385 float3 &r_location,
386 Quaternion &r_rotation,
387 float3 &r_scale,
388 const bool allow_negative_scale)
389{
390 r_location = mat[3].xyz;
391 to_rot_scale(to_float3x3(mat), r_rotation, r_scale, allow_negative_scale);
392}
393
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
#define FLT_EPSILON
#define atan
#define isnan
#define isinf
#define sin
b[1] b &&[1] b &&[1] b &&[1] b &&[1] b[3] b &&[1] b[3] float determinant(MatBase< C, R >) RET
#define cos
VecBase< float, D > normalize(VecOp< float, D >) RET
#define abs
#define sqrt
#define acos
float hypot(float x, float y)
bool is_zero(float3x3 a)
bool is_negative(float3x3 mat)
float3 to_scale(float3x3 mat)
float3x3 normalize_and_get_size(float3x3 mat, float3 &r_size)
Quaternion to_quaternion(EulerXYZ eul)
AxisAngle to_axis_angle(Quaternion quat)
void to_rot_scale(float3x3 mat, EulerXYZ &r_rotation, float3 &r_scale)
EulerXYZ to_euler(float3x3 mat, const bool normalized)
void to_loc_rot_scale(float4x4 mat, float3 &r_location, EulerXYZ &r_rotation, float3 &r_scale)
float length_manhattan(VecT a)
ccl_device_inline Quaternion normalized(const Quaternion q)
MatBase< float, 4, 4 > float4x4
Quaternion normalized_to_quat_fast(float3x3 mat)
void normalized_to_eul2(float3x3 mat, EulerXYZ &eul1, EulerXYZ &eul2)
Quaternion normalized_to_quat_with_checks(float3x3 mat)
float3 as_float3() const
static Quaternion identity()