Blender V5.3
math_quaternion.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
7#include "util/math_base.h"
8#include "util/types_float3.h"
9#include "util/types_float4.h"
11
13
15{
16 return make_quaternion(-q.w, -q.x, -q.y, -q.z);
17}
18
20{
21 return make_quaternion(a * q.w, a * q.x, a * q.y, a * q.z);
22}
24{
25 return make_quaternion(q.w * a, q.x * a, q.y * a, q.z * a);
26}
27
29{
30 return make_quaternion(a.w + b.w, a.x + b.x, a.y + b.y, a.z + b.z);
31}
32
34{
35 return make_quaternion(a.w - b.w, a.x - b.x, a.y - b.y, a.z - b.z);
36}
37
43
49
51{
52 return a.w * b.w + a.x * b.x + a.y * b.y + a.z * b.z;
53}
54
56{
57 const float len = sqrtf(dot(q, q));
58 if (len != 0.0f) {
59 const float len_inv = 1.0f / len;
60 return make_quaternion(q.w * len_inv, q.x * len_inv, q.y * len_inv, q.z * len_inv);
61 }
62 return identity_quaternion();
63}
64
65/* Linear interpolation around the shortest angle.
66 * The caller is responsible for ensuring the input quaternions are normalized. */
67ccl_device_inline Quaternion lerp(const Quaternion q1, const Quaternion q2, const float t)
68{
69 /* TODO(sergey): Add assert that the quaternions are normalized? */
70 /* Ensure rotation around shortest angle, negated quaternions are the same. */
71 const Quaternion q2_shortest_angle = dot(q1, q2) >= 0.0f ? q2 : -q2;
72 return normalized((1.0f - t) * q1 + t * q2_shortest_angle);
73}
74
76{
77 return make_quaternion(0.0f, 0.0f, 0.0f, 0.0f);
78}
79
81{
82 /* Matches legacy storage and attribute access: make_float3(make_float4(q.w, q.x, q.y, q.z)) */
83 return make_float3(q.w, q.x, q.y);
84}
85
87{
88 /* Matches legacy storage and attribute access: make_float4(q.w, q.x, q.y, q.z) */
89 return make_float4(q.w, q.x, q.y, q.z);
90}
91
92/* mix() on quaternions is used from generic attribute sampling and interpolation.
93 * Define it as a float4 mix for the compatibility until we can change the way quaternion
94 * attributes behave. */
95ccl_device_inline Quaternion mix(const Quaternion a, const Quaternion b, const float t)
96{
97 return a + t * (b - a);
98}
99
101{
102 r[0] = val.w;
103 r[1] = val.x;
104 r[2] = val.y;
105 r[3] = val.z;
106}
107
#define ccl_private
#define ccl_device_inline
#define ccl_device_template_spec
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
float dot(VecOp< float, D >, VecOp< float, D >) RET
__forceinline LightTreeMeasure operator+(const LightTreeMeasure &a, const LightTreeMeasure &b)
ccl_device_inline Extrema< T > operator*(const Extrema< T > a, const T b)
Definition math_base.h:967
ccl_device_inline Extrema< T > operator+=(ccl_private Extrema< T > &a, const ccl_private Extrema< T > &b)
Definition math_base.h:980
ccl_device_inline T make_zero()
Definition math_dual.h:20
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
ccl_device_inline dual< T1 > operator-(const dual< T1 > a, T2 b)
Definition math_dual.h:113
ccl_device_inline float3 operator-=(float3 &a, const float3 b)
ccl_device_inline void copy_v4_qt(ccl_private float *r, const Quaternion val)
ccl_device_inline Quaternion normalized(const Quaternion q)
#define mix
#define sqrtf
#define make_float4
#define lerp(a, b, t)
Definition smaa.cc:318
ccl_device_inline Quaternion identity_quaternion()
ccl_device_inline Quaternion make_quaternion(const float w, const float x, const float y, const float z)