Blender V5.3
gpu_shader_math_rotation.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
11
12/* -------------------------------------------------------------------- */
15
24float2 interpolate_dot_slerp(float t, float cosom)
25{
26 float2 w = float2(1.0f - t, t);
27 /* Within [-1..1] range, avoid aligned axis. */
28 constexpr float eps = 1e-4f;
29 if (abs(cosom) < 1.0f - eps) {
30 float omega = acos(cosom);
31 w = sin(w * omega) / sin(omega);
32 }
33 return w;
34}
35
37{
38 float4 quat = a.as_float4();
39 float cosom = dot(a.as_float4(), b.as_float4());
40 /* Rotate around shortest angle. */
41 if (cosom < 0.0f) {
42 cosom = -cosom;
43 quat = -quat;
44 }
45 float2 w = interpolate_dot_slerp(t, cosom);
46 quat = w.x * quat + w.y * b.as_float4();
47 return Quaternion::from_float4(quat);
48}
49
50/* Assumes a and b are unit vectors. */
52{
53 float cosom = dot(a, b);
54 float2 w = interpolate_dot_slerp(t, cosom);
55 return w.x * a + w.y * b;
56}
57
59
60/* -------------------------------------------------------------------- */
63
69{
70 float3x3 result;
71 /* axis_vec is given to be normalized. */
72 if (rotation.axis.x == 1.0f) {
73 float angle_cos = cos(rotation.angle);
74 float angle_sin = sin(rotation.angle);
75 for (int c = 0; c < 3; c++) {
76 result[0][c] = mat[0][c];
77 result[1][c] = angle_cos * mat[1][c] + angle_sin * mat[2][c];
78 result[2][c] = -angle_sin * mat[1][c] + angle_cos * mat[2][c];
79 }
80 }
81 else if (rotation.axis.y == 1.0f) {
82 float angle_cos = cos(rotation.angle);
83 float angle_sin = sin(rotation.angle);
84 for (int c = 0; c < 3; c++) {
85 result[0][c] = angle_cos * mat[0][c] - angle_sin * mat[2][c];
86 result[1][c] = mat[1][c];
87 result[2][c] = angle_sin * mat[0][c] + angle_cos * mat[2][c];
88 }
89 }
90 else if (rotation.axis.z == 1.0f) {
91 float angle_cos = cos(rotation.angle);
92 float angle_sin = sin(rotation.angle);
93 for (int c = 0; c < 3; c++) {
94 result[0][c] = angle_cos * mat[0][c] + angle_sin * mat[1][c];
95 result[1][c] = -angle_sin * mat[0][c] + angle_cos * mat[1][c];
96 result[2][c] = mat[2][c];
97 }
98 }
99 else {
100 /* Un-optimized case. Arbitrary rotation. */
101 result = mat * from_rotation(rotation);
102 }
103 return result;
104}
105
110{
111 AxisAngle axis_angle;
112 if (rotation.y == 0.0f && rotation.z == 0.0f) {
113 axis_angle = AxisAngle{float3(1.0f, 0.0f, 0.0f), rotation.x};
114 }
115 else if (rotation.x == 0.0f && rotation.z == 0.0f) {
116 axis_angle = AxisAngle{float3(0.0f, 1.0f, 0.0f), rotation.y};
117 }
118 else if (rotation.x == 0.0f && rotation.y == 0.0f) {
119 axis_angle = AxisAngle{float3(0.0f, 0.0f, 1.0f), rotation.z};
120 }
121 else {
122 /* Un-optimized case. Arbitrary rotation. */
123 return mat * from_rotation(rotation);
124 }
125 return rotate(mat, axis_angle);
126}
127
132{
133 float4x4 result = to_float4x4(rotate(to_float3x3(mat), rotation));
134 result[0][3] = mat[0][3];
135 result[1][3] = mat[1][3];
136 result[2][3] = mat[2][3];
137 result[3][0] = mat[3][0];
138 result[3][1] = mat[3][1];
139 result[3][2] = mat[3][2];
140 result[3][3] = mat[3][3];
141 return result;
142}
143
148{
149 float4x4 result = to_float4x4(rotate(to_float3x3(mat), rotation));
150 result[0][3] = mat[0][3];
151 result[1][3] = mat[1][3];
152 result[2][3] = mat[2][3];
153 result[3][0] = mat[3][0];
154 result[3][1] = mat[3][1];
155 result[3][2] = mat[3][2];
156 result[3][3] = mat[3][3];
157 return result;
158}
159
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define sin
#define cos
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define abs
#define acos
float2x2 from_rotation(AngleRadian rotation)
float2 interpolate_dot_slerp(float t, float cosom)
Quaternion interpolate(Quaternion a, Quaternion b, float t)
float3x3 rotate(float3x3 mat, AxisAngle rotation)
float3 spherical_interpolate(float3 a, float3 b, float t)
MatBase< float, 4, 4 > float4x4
const btScalar eps
Definition poly34.cpp:11
static Quaternion from_float4(float4 v)