Blender V5.3
math_float3x3.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2026 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
8
10
12{
13 float3x3 t;
14 t.x = a * m.x;
15 t.y = a * m.y;
16 t.z = a * m.z;
17 return t;
18}
20{
21 float3x3 t;
22 t.x = m.x * a;
23 t.y = m.y * a;
24 t.z = m.z * a;
25 return t;
26}
27
29{
30#if defined(__KERNEL_SSE__)
31 auto multiply_row = [](const float3 &a_row, const float3x3 &b) -> float3 {
32 const float4 ax = shuffle<0, 0, 0, 0>(float4(a_row));
33 const float4 ay = shuffle<1, 1, 1, 1>(float4(a_row));
34 const float4 az = shuffle<2, 2, 2, 2>(float4(a_row));
35
36 float4 acc = ax * float4(b.x);
37 acc = madd(ay, float4(b.y), acc);
38 acc = madd(az, float4(b.z), acc);
39 return float3(acc);
40 };
41
42 float3x3 result;
43 result.x = multiply_row(a.x, b);
44 result.y = multiply_row(a.y, b);
45 result.z = multiply_row(a.z, b);
46 return result;
47#else
48 const float3 c_x = make_float3(b.x.x, b.y.x, b.z.x);
49 const float3 c_y = make_float3(b.x.y, b.y.y, b.z.y);
50 const float3 c_z = make_float3(b.x.z, b.y.z, b.z.z);
51
52 float3x3 r;
53 r.x = make_float3(dot(a.x, c_x), dot(a.x, c_y), dot(a.x, c_z));
54 r.y = make_float3(dot(a.y, c_x), dot(a.y, c_y), dot(a.y, c_z));
55 r.z = make_float3(dot(a.z, c_x), dot(a.z, c_y), dot(a.z, c_z));
56 return r;
57#endif
58}
59
60/* Multiply 3x3 matrix by a 3-component column-vector.
61 * The result is a 3-component column vector. */
63{
64 float3 r;
65 r.x = dot(m.x, v);
66 r.y = dot(m.y, v);
67 r.z = dot(m.z, v);
68 return r;
69}
70
72{
73 const float inv_a = 1.0f / a;
74 float3x3 t;
75 t.x = m.x * inv_a;
76 t.y = m.y * inv_a;
77 t.z = m.z * inv_a;
78 return t;
79}
80
81/* Construct 3x3 scale matrix:
82 * [ s.x 0.0 0.0 ]
83 * Result = [ 0.0 s.y 0.0 ]
84 * [ 0.0 0.0 s.z ] */
86{
87 return make_float3x3(
88 make_float3(s.x, 0.0f, 0.0f), make_float3(0.0f, s.y, 0.0f), make_float3(0.0f, 0.0f, s.z));
89}
90
92{
93 return make_float3x3(make_float3(m.x.x, m.y.x, m.z.x),
94 make_float3(m.x.y, m.y.y, m.z.y),
95 make_float3(m.x.z, m.y.z, m.z.z));
96}
97
99{
100 return (m.x.x * (m.y.y * m.z.z - m.z.y * m.y.z) - m.x.y * (m.y.x * m.z.z - m.z.x * m.y.z) +
101 m.x.z * (m.y.x * m.z.y - m.z.x * m.y.y));
102}
103
104/* The adjugate or classical adjoint adj(m) */
106{
107 float3x3 r;
108
109 r.x = make_float3(m.y.y * m.z.z - m.z.y * m.y.z,
110 -m.x.y * m.z.z + m.z.y * m.x.z,
111 m.x.y * m.y.z - m.y.y * m.x.z);
112 r.y = make_float3(-m.y.x * m.z.z + m.z.x * m.y.z,
113 m.x.x * m.z.z - m.z.x * m.x.z,
114 -m.x.x * m.y.z + m.y.x * m.x.z);
115 r.z = make_float3(m.y.x * m.z.y - m.z.x * m.y.y,
116 -m.x.x * m.z.y + m.z.x * m.x.y,
117 m.x.x * m.y.y - m.y.x * m.x.y);
118
119 return r;
120}
121
123{
124 const float det = determinant(m);
125 if (det != 0.0f) {
126 return adjoint(m) / det;
127 }
128 return zero_float3x3();
129}
130
131/* Convert quaternion to a 3x3 scaled rotation matrix.
132 *
133 * The choice of rotation is such that the quaternion [1 0 0 0] goes to an identity matrix and for
134 * small a, b, c the quaternion [1 a b c] goes to the matrix
135 *
136 * [ 0 -c b ]
137 * I + 2 [ c 0 -a ] + higher order terms
138 * [ -b a 0 ]
139 *
140 * which corresponds to a Rodrigues approximation, the last matrix being the cross-product matrix
141 * of [a b c]. Together with the property that R(q1 * q2) = R(q1) * R(q2) this uniquely defines the
142 * mapping from q to R.
143 *
144 * No normalization of the quaternion is performed, i.e. R = ||q||^2 * Q, where Q is an orthonormal
145 * matrix such that det(Q) = 1 and Q*Q' = I.
146 *
147 * Adopted from Ceres-solver. */
149{
150 /* Make convenient names for elements of q. */
151 const float a = q.w;
152 const float b = q.x;
153 const float c = q.y;
154 const float d = q.z;
155
156 /* Define common terms. */
157 const float aa = a * a;
158 const float ab = a * b;
159 const float ac = a * c;
160 const float ad = a * d;
161 const float bb = b * b;
162 const float bc = b * c;
163 const float bd = b * d;
164 const float cc = c * c;
165 const float cd = c * d;
166 const float dd = d * d;
167
168 float3x3 r;
169 r.x = make_float3(aa + bb - cc - dd, 2.0f * (bc - ad), 2.0f * (ac + bd));
170 r.y = make_float3(2.0f * (ad + bc), aa - bb + cc - dd, 2.0f * (cd - ab));
171 r.z = make_float3(2.0f * (bd - ac), 2.0f * (ab + cd), aa - bb - cc + dd);
172 return r;
173}
174
175/* Same as above except that the rotation matrix is normalized by the Frobenius norm,
176 * so that R * R' = I (and det(R) = 1).
177 *
178 * Adopted from Ceres-solver. */
180{
181 const float normalizer = 1.0f / (q.w * q.w + q.x * q.x + q.y * q.y + q.z * q.z);
182 return quaternion_to_scaled_rotation(q) * normalizer;
183}
184
#define ccl_device_inline
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
b[1] b &&[1] b &&[1] b &&[1] b &&[1] b[3] b &&[1] b[3] float determinant(MatBase< C, R >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
void quaternion_to_rotation(float w, float x, float y, float z, float4 &rotation)
float2x2 adjoint(float2x2 mat)
ccl_device_inline Extrema< T > operator*(const Extrema< T > a, const T b)
Definition math_base.h:967
ccl_device_inline dual< T > operator/(const dual< T > a, T b)
Definition math_dual.h:71
CCL_NAMESPACE_BEGIN ccl_device_inline float madd(const float a, const float b, const float c)
Definition math_fast.h:35
ccl_device_inline float3x3 transposed(const float3x3 m)
ccl_device_inline float3x3 inverted(const float3x3 m)
ccl_device_inline float3x3 quaternion_to_scaled_rotation(const Quaternion q)
ccl_device_inline float3x3 float3x3_scale(const float3 s)
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
ccl_device_inline float3x3 zero_float3x3()
ccl_device_inline float3x3 make_float3x3(const float3 x, const float3 y, const float3 z)