UdonLibrary 1.0.0
機械システム研究部 C++ ライブラリ
読み取り中…
検索中…
一致する文字列を見つけられません
Quaternion.hpp
[詳解]
1//
2// クオータニオン 前方定義
3//
4// Copyright (c) 2022-2024 udonrobo
5//
6
7
8#pragma once
9
12
13namespace Udon
14{
15
16 struct Euler;
17
18
21 {
22 bool x;
23 bool y;
24 bool z;
25 };
26
27
30 {
31
33 using ValueType = double;
34
37
40
43
46
48 constexpr Quaternion() noexcept
49 : x()
50 , y()
51 , z()
52 , w()
53 {
54 }
55
62 : x(x)
63 , y(y)
64 , z(z)
65 , w(w)
66 {
67 }
68
70 Quaternion(const Quaternion&) = default;
71
73 Quaternion& operator=(const Quaternion&) = default;
74
78 constexpr Quaternion operator*(const Quaternion& rhs) const noexcept
79 {
80 return {
81 w * rhs.x + x * rhs.w + y * rhs.z - z * rhs.y,
82 w * rhs.y - x * rhs.z + y * rhs.w + z * rhs.x,
83 w * rhs.z + x * rhs.y - y * rhs.x + z * rhs.w,
84 w * rhs.w - x * rhs.x - y * rhs.y - z * rhs.z,
85 };
86 }
87
91 Quaternion& operator*=(const Quaternion& rhs) noexcept { return *this = *this * rhs; };
92
96 constexpr bool operator==(const Quaternion& rhs) const noexcept
97 {
98 return x == rhs.x &&
99 y == rhs.y &&
100 z == rhs.z &&
101 w == rhs.w;
102 };
103 constexpr bool operator!=(const Quaternion& rhs) const noexcept
104 {
105 return !(*this == rhs);
106 };
107
110 explicit constexpr operator bool() const noexcept
111 {
112 return x || y || z || w;
113 }
114
117 constexpr Quaternion inverse() const noexcept
118 {
119 return { -x, -y, -z, w };
120 }
121
125 constexpr Quaternion directionRevision(const QuaternionDirection& direction) const noexcept
126 {
127 return {
128 x * (direction.x ? 1 : -1),
129 y * (direction.y ? 1 : -1),
130 z * (direction.z ? 1 : -1),
131 w
132 };
133 }
134
137 static Quaternion Identity() noexcept
138 {
139 return { 0, 0, 0, 1 };
140 }
141
145 static Quaternion RotateX(ValueType angle) noexcept
146 {
147 return { sin(angle / 2), 0, 0, cos(angle / 2) };
148 }
149
153 static Quaternion RotateY(ValueType angle) noexcept
154 {
155 return { 0, sin(angle / 2), 0, cos(angle / 2) };
156 }
157
161 static Quaternion RotateZ(ValueType angle) noexcept
162 {
163 return { 0, 0, sin(angle / 2), cos(angle / 2) };
164 }
165
167 constexpr bool isZero() const noexcept
168 {
169 return !operator bool();
170 }
171
173 void clear() noexcept
174 {
175 *this = {};
176 }
177
180 Euler toEuler() const noexcept;
181
183 double toYaw() const noexcept
184 {
185 return atan2(2 * (w * z + x * y), 1 - 2 * (y * y + z * z));
186 }
187
189 double toPitch() const noexcept
190 {
191 return asin(2 * (w * y - z * x));
192 }
193
195 double toRoll() const noexcept
196 {
197 return atan2(2 * (w * x + y * z), 1 - 2 * (x * x + y * y));
198 }
199
200#ifdef SIV3D_INCLUDED
201
203 Quaternion(const s3d::Quaternion& q) noexcept
204 : x(q.getX())
205 , y(q.getY())
206 , z(q.getZ())
207 , w(q.getW())
208 {
209 }
210
213 operator s3d::Quaternion() const noexcept
214 {
215 return s3d::Quaternion(x, y, z, w);
216 }
217#endif
218
219#ifdef ARDUINO
221 void show() const
222 {
223 Serial.print(F("x: ")), Serial.print(x), Serial.print('\t');
224 Serial.print(F("y: ")), Serial.print(y), Serial.print('\t');
225 Serial.print(F("z: ")), Serial.print(z), Serial.print('\t');
226 Serial.print(F("w: ")), Serial.print(w), Serial.print('\t');
227 }
228#endif
229
230 UDON_ENUMERABLE(x, y, z, w)
231 };
232} // namespace Udon
#define UDON_ENUMERABLE(...)
メンバ変数の列挙を可能にする
Definition EnumerableMacro.hpp:11
#define F(x)
Definition Show.hpp:17
Definition Bit.hpp:12
オイラー角
Definition Euler.hpp:31
クオータニオンの各成分の正負を表す構造体
Definition Quaternion.hpp:21
bool x
Definition Quaternion.hpp:22
bool y
Definition Quaternion.hpp:23
bool z
Definition Quaternion.hpp:24
クオータニオン
Definition Quaternion.hpp:30
constexpr Quaternion inverse() const noexcept
逆クオータニオン
Definition Quaternion.hpp:117
constexpr Quaternion() noexcept
デフォルトコンストラクタ
Definition Quaternion.hpp:48
static Quaternion RotateY(ValueType angle) noexcept
Y軸回転クオータニオン
Definition Quaternion.hpp:153
constexpr Quaternion directionRevision(const QuaternionDirection &direction) const noexcept
回転方向を修正したクオータニオンを取得する
Definition Quaternion.hpp:125
ValueType x
x成分
Definition Quaternion.hpp:36
constexpr bool operator==(const Quaternion &rhs) const noexcept
比較演算子
Definition Quaternion.hpp:96
constexpr Quaternion operator*(const Quaternion &rhs) const noexcept
内積
Definition Quaternion.hpp:78
double toPitch() const noexcept
ピッチ角を取得
Definition Quaternion.hpp:189
double ValueType
要素の型
Definition Quaternion.hpp:33
ValueType z
z成分
Definition Quaternion.hpp:42
double toYaw() const noexcept
ヨー角を取得
Definition Quaternion.hpp:183
Quaternion & operator*=(const Quaternion &rhs) noexcept
複合代入演算子
Definition Quaternion.hpp:91
void clear() noexcept
値クリア
Definition Quaternion.hpp:173
static Quaternion RotateX(ValueType angle) noexcept
X軸回転クオータニオン
Definition Quaternion.hpp:145
Quaternion & operator=(const Quaternion &)=default
デフォルトコピー代入演算子
constexpr bool isZero() const noexcept
要素がゼロであるか
Definition Quaternion.hpp:167
constexpr Quaternion(ValueType x, ValueType y, ValueType z, ValueType w) noexcept
コンストラクタ
Definition Quaternion.hpp:61
Euler toEuler() const noexcept
オイラー角に変換
Definition Quaternion.hpp:12
static Quaternion RotateZ(ValueType angle) noexcept
Z軸回転クオータニオン
Definition Quaternion.hpp:161
constexpr bool operator!=(const Quaternion &rhs) const noexcept
Definition Quaternion.hpp:103
ValueType w
w成分
Definition Quaternion.hpp:45
ValueType y
y成分
Definition Quaternion.hpp:39
static Quaternion Identity() noexcept
単位クオータニオン
Definition Quaternion.hpp:137
double toRoll() const noexcept
ロール角を取得
Definition Quaternion.hpp:195
Quaternion(const Quaternion &)=default
デフォルトコピーコンストラクタ