OpenMoHAA 0.82.0
Loading...
Searching...
No Matches
SkelQuat.h
1/*
2===========================================================================
3Copyright (C) 2023 the OpenMoHAA team
4
5This file is part of OpenMoHAA source code.
6
7OpenMoHAA source code is free software; you can redistribute it
8and/or modify it under the terms of the GNU General Public License as
9published by the Free Software Foundation; either version 2 of the License,
10or (at your option) any later version.
11
12OpenMoHAA source code is distributed in the hope that it will be
13useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with OpenMoHAA source code; if not, write to the Free Software
19Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20===========================================================================
21*/
22
23// SkelQuat.h : Skeletor Quat
24
25#pragma once
26
27#ifdef __cplusplus
28
29# include "SkelVec3.h"
30# include "SkelVec4.h"
31# include "SkelMat3.h"
32# include "SkelMat4.h"
33
34class SkelQuat
35{
36public:
37 union {
38 float val[4];
39
40 struct {
41 float x;
42 float y;
43 float z;
44 float w;
45 };
46 };
47
48 SkelQuat();
49 SkelQuat(SkelVec3 vec);
50 SkelQuat(SkelMat3 mat3);
51 SkelQuat(SkelMat4 mat4);
52 SkelQuat(float x, float y, float z, float w);
53 SkelQuat(float *quat);
54 SkelQuat(const float *quat);
55 SkelQuat(float w, const SkelVec3 *vec);
56
57 float& operator[](int index);
58 float operator[](int index) const;
59
60 operator float *();
61 operator float *() const;
62
63 void Set(float x, float y, float z, float w);
64 void Set(SkelVec4 vec4);
65 void Set(float *quat);
66 void SetAngle(float a);
67 void SetAxis(SkelVec3 vec);
68
69 void Invert();
70 float Length() const;
71 float LengthSquared() const;
72 void Normalize();
73
74 void GetMat3(SkelMat3& mat3) const;
75 void GetMat4(SkelMat4& mat4) const;
76 void GetEulerAngles(float *angles) const;
77 bool IsUnit() const;
78 void MakeIdentity();
79 bool IsIdentity();
80 bool IsValid() const;
81};
82
83inline SkelQuat::SkelQuat()
84{
85 QuatClear(val);
86}
87
88inline SkelQuat::SkelQuat(SkelVec3 vec)
89{
90 EulerToQuat(vec.val, val);
91}
92
93inline SkelQuat::SkelQuat(SkelMat3 mat3)
94{
95 MatToQuat(mat3.val, val);
96}
97
98inline SkelQuat::SkelQuat(SkelMat4 mat4)
99{
100 MatToQuat(mat4.val, val);
101}
102
103inline SkelQuat::SkelQuat(float x, float y, float z, float w)
104{
105 Set(x, y, z, w);
106}
107
108inline SkelQuat::SkelQuat(float *quat)
109{
110 val[0] = quat[0];
111 val[1] = quat[1];
112 val[2] = quat[2];
113 val[3] = quat[3];
114}
115
116inline SkelQuat::SkelQuat(const float *quat)
117{
118 val[0] = quat[0];
119 val[1] = quat[1];
120 val[2] = quat[2];
121 val[3] = quat[3];
122}
123
124inline SkelQuat::SkelQuat(float w, const SkelVec3 *vec)
125{
126 val[0] = vec->x;
127 val[1] = vec->y;
128 val[2] = vec->z;
129 val[3] = w;
130}
131
132inline void SkelQuat::Set(float x, float y, float z, float w)
133{
134 this->x = x;
135 this->y = y;
136 this->z = z;
137 this->w = w;
138}
139
140inline void SkelQuat::Set(SkelVec4 vec4)
141{
142 val[0] = vec4.val[0];
143 val[1] = vec4.val[1];
144 val[2] = vec4.val[2];
145 val[3] = vec4.val[3];
146}
147
148inline void SkelQuat::Set(float *quat)
149{
150 val[0] = quat[0];
151 val[1] = quat[1];
152 val[2] = quat[2];
153 val[3] = quat[3];
154}
155
156inline void SkelQuat::SetAngle(float a)
157{
158 SkelVec3 vec;
159 vec.y = a;
160 EulerToQuat(vec.val, val);
161}
162
163inline void SkelQuat::SetAxis(SkelVec3 vec)
164{
165 EulerToQuat(vec.val, val);
166}
167
168inline void SkelQuat::Invert()
169{
170 QuatInverse(val);
171}
172
173inline float SkelQuat::Length() const
174{
175 return (float)sqrt(val[0] * val[0] + val[1] * val[1] + val[2] * val[2] + val[3] * val[3]);
176}
177
178inline float SkelQuat::LengthSquared() const
179{
180 return val[0] * val[0] + val[1] * val[1] + val[2] * val[2] + val[3] * val[3];
181}
182
183inline void SkelQuat::Normalize()
184{
185 VectorNormalize(val);
186}
187
188inline void SkelQuat::GetMat3(SkelMat3& mat3) const
189{
190 QuatToMat(val, mat3.val);
191}
192
193inline void SkelQuat::GetMat4(SkelMat4& mat4) const
194{
195 QuatToMat(val, mat4.val);
196}
197
198inline void SkelQuat::GetEulerAngles(float *angles) const
199{
200 QuatToAngles(val, angles);
201}
202
203inline bool SkelQuat::IsUnit() const
204{
205 // FIXME: stub
206 return false;
207}
208
209inline void SkelQuat::MakeIdentity()
210{
211 x = 0.0f;
212 y = 0.0f;
213 z = 0.0f;
214 w = 1.0f;
215}
216
217inline bool SkelQuat::IsIdentity()
218{
219 return (x == 0.0f) && (y == 0.0f) && (z == 0.0f) && (w == 1.0f);
220}
221
222inline bool SkelQuat::IsValid() const
223{
224 // FIXME: stub
225 return true;
226}
227
228inline void Slerp(SkelQuat& from, SkelQuat& to, float t, SkelQuat *out)
229{
230 static_assert(sizeof(float) == sizeof(int), "Float must be the same size as Int");
231 float f;
232 float f2;
233
234 f = from.x * to.x + from.y * to.y + from.z * to.z + from.w * to.w;
235 f2 = 1.0 - t;
236
237 // little hack because I don't know how can this operation be converted to float...
238 *(int *)&f = (*(int *)&f & 0x80000000) ^ *(int *)&f2;
239
240 out->x = to.x * t + from.x * f;
241 out->y = to.y * t + from.y * f;
242 out->z = to.z * t + from.z * f;
243 out->w = to.w * t + from.w * f;
244
245 f = 1.0 / out->Length();
246 out->x = out->x * f;
247 out->y = out->y * f;
248 out->z = out->z * f;
249 out->w = out->w * f;
250}
251
252inline SkelQuat::operator float *()
253{
254 return val;
255}
256
257inline SkelQuat::operator float *() const
258{
259 return (float *)val;
260}
261
262inline float& SkelQuat::operator[](int index)
263{
264 return val[index];
265}
266
267inline float SkelQuat::operator[](int index) const
268{
269 return val[index];
270}
271
272#else
273
274typedef struct {
275 union {
276 float val[4];
277
278 struct {
279 float x;
280 float y;
281 float z;
282 float w;
283 };
284 };
285} SkelQuat;
286
287#endif
Definition SkelMat3.h:192
Definition SkelMat4.h:355
Definition SkelQuat.h:274
Definition SkelVec3.h:197
Definition SkelVec4.h:59