Blender V5.3
BLI_math_quaternion_types.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
10
11#include <ostream>
12
14#include "BLI_math_base.hh"
18
19namespace blender::math {
20
21/* -------------------------------------------------------------------- */
24
36template<typename T> struct QuaternionBase {
37 T w, x, y, z;
38
39 QuaternionBase() = default;
40
41 QuaternionBase(const T &new_w, const T &new_x, const T &new_y, const T &new_z)
42 : w(new_w), x(new_x), y(new_y), z(new_z) {};
43
48 explicit QuaternionBase(const VecBase<T, 4> &vec) : QuaternionBase(UNPACK4(vec)) {};
49
53 QuaternionBase(const T &real, const VecBase<T, 3> &imaginary)
54 : QuaternionBase(real, UNPACK3(imaginary)) {};
55
57
59 {
60 return {1, 0, 0, 0};
61 }
62
65 {
66 return {0, 0, 0, 0};
67 }
68
74
76
77 explicit operator VecBase<T, 4>() const
78 {
79 return {this->w, this->x, this->y, this->z};
80 }
81
83
84 operator const T *() const
85 {
86 return reinterpret_cast<const T *>(this);
87 }
88
89 operator T *()
90 {
91 return reinterpret_cast<T *>(this);
92 }
93
99
105
110 QuaternionBase twist(const Axis axis) const;
111
117 QuaternionBase swing(const Axis axis) const;
118
123 {
124 return *reinterpret_cast<const VecBase<T, 3> *>(&x);
125 }
127 {
128 return *reinterpret_cast<VecBase<T, 3> *>(&x);
129 }
130
132
143
145
147 {
148 return {a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z,
149 a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
150 a.w * b.y + a.y * b.w + a.z * b.x - a.x * b.z,
151 a.w * b.z + a.z * b.w + a.x * b.y - a.y * b.x};
152 }
153
155 {
156 *this = *this * b;
157 return *this;
158 }
159
160 /* Scalar product. */
161 friend QuaternionBase operator*(const QuaternionBase &a, const T &b)
162 {
163 return {a.w * b, a.x * b, a.y * b, a.z * b};
164 }
165
166 /* Negate the quaternion. */
168 {
169 return {-a.w, -a.x, -a.y, -a.z};
170 }
171
172 friend bool operator==(const QuaternionBase &a, const QuaternionBase &b) = default;
173
174 constexpr uint64_t hash() const
175 {
176 return VecBase<T, 4>(*this).hash();
177 }
178
180 {
181 return VecBase<T, 4>(*this).hash_unique(hash);
182 }
183
184 friend std::ostream &operator<<(std::ostream &stream, const QuaternionBase &rot)
185 {
186 return stream << "Quaternion" << static_cast<VecBase<T, 4>>(rot);
187 }
188};
189
191
192/* -------------------------------------------------------------------- */
195
204template<typename T> struct DualQuaternionBase {
209
214 /* TODO(fclem): Can this be replaced by a Mat3x3 ?
215 * It currently holds some translation in some cases. Is this wanted?
216 * This would save some flops all along the way. */
225
227
232
237 const QuaternionBase<T> &dual,
238 const MatBase<T, 4, 4> &scale_mat);
239
241
246
248
250
253
256
259 {
260 DualQuaternionBase dq = a;
261 dq *= t;
262 return dq;
263 }
264
267 {
268 DualQuaternionBase dq = a;
269 dq *= t;
270 return dq;
271 }
272
275 {
276 DualQuaternionBase dq = a;
277 dq += b;
278 return dq;
279 }
280
281 friend bool operator==(const DualQuaternionBase &a, const DualQuaternionBase &b) = default;
282
283 friend std::ostream &operator<<(std::ostream &stream, const DualQuaternionBase &rot)
284 {
285 stream << "DualQuaternion(\n";
286 stream << " .quat = " << rot.quat << "\n";
287 stream << " .trans = " << rot.trans << "\n";
288 if (rot.scale_weight != T(0)) {
289 stream << " .scale = " << rot.scale;
290 stream << " .scale_weight = " << rot.scale_weight << "\n";
291 }
292 stream << " .quat_weight = " << rot.quat_weight << "\n)\n";
293 return stream;
294 }
295};
296
301template<typename T> [[nodiscard]] inline bool is_normalized(const DualQuaternionBase<T> &dq)
302{
303 return dq.quat_weight == T(1);
304}
305
307
308/* -------------------------------------------------------------------- */
311
312template<typename U> struct AssertUnitEpsilon<QuaternionBase<U>> {
313 static constexpr U value = AssertUnitEpsilon<U>::value * 10;
314};
315
317
318/* -------------------------------------------------------------------- */
323
324template<typename T> struct TypeTraits {
326};
327template<> struct TypeTraits<float> {
328 using DoublePrecision = double;
329};
330
333
335
336} // namespace blender::math
#define UNPACK4(a)
#define UNPACK3(a)
#define rot(x, k)
#define T
QuaternionBase< float > Quaternion
bool is_normalized(const DualQuaternionBase< T > &dq)
DualQuaternionBase< float > DualQuaternion
UserDef U
Definition blender.cc:58
unsigned __int64 uint64_t
Definition BKE_text.h:91
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
constexpr uint64_t hash() const
void hash_unique(UniqueHashBytes &hash) const
friend DualQuaternionBase operator*(const DualQuaternionBase &a, const T &t)
DualQuaternionBase & operator*=(const T &t) &
DualQuaternionBase & operator+=(const DualQuaternionBase &b) &
DualQuaternionBase(const QuaternionBase< T > &non_dual, const QuaternionBase< T > &dual)
friend std::ostream & operator<<(std::ostream &stream, const DualQuaternionBase &rot)
friend DualQuaternionBase operator+(const DualQuaternionBase &a, const DualQuaternionBase &b)
DualQuaternionBase(const QuaternionBase< T > &non_dual, const QuaternionBase< T > &dual, const MatBase< T, 4, 4 > &scale_mat)
friend DualQuaternionBase operator*(const T &t, const DualQuaternionBase &a)
friend bool operator==(const DualQuaternionBase &a, const DualQuaternionBase &b)=default
void hash_unique(UniqueHashBytes &hash) const
friend QuaternionBase operator*(const QuaternionBase &a, const T &b)
friend bool operator==(const QuaternionBase &a, const QuaternionBase &b)=default
const VecBase< T, 3 > & imaginary_part() const
QuaternionBase swing(const Axis axis) const
AngleRadianBase< T > twist_angle(const Axis axis) const
friend std::ostream & operator<<(std::ostream &stream, const QuaternionBase &rot)
QuaternionBase & operator*=(const QuaternionBase &b) &
QuaternionBase(const T &real, const VecBase< T, 3 > &imaginary)
QuaternionBase(const T &new_w, const T &new_x, const T &new_y, const T &new_z)
friend QuaternionBase operator*(const QuaternionBase &a, const QuaternionBase &b)
QuaternionBase twist(const Axis axis) const
QuaternionBase wrapped_around(const QuaternionBase &reference) const
QuaternionBase(const VecBase< T, 4 > &vec)
static QuaternionBase expmap(const VecBase< T, 3 > &expmap)
friend QuaternionBase operator-(const QuaternionBase &a)