-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQuaternion.c
270 lines (234 loc) · 8.85 KB
/
Quaternion.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright (C) 2019 Martin Weigel <mail@MartinWeigel.com>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/**
* @file Quaternion.c
* @brief A basic quaternion library written in C
* @date 2019-11-28
*/
#include "Quaternion.h"
#include <stdlib.h>
#include <assert.h>
#include <math.h>
void Quaternion_set(double w, double v1, double v2, double v3, Quaternion* output)
{
assert(output != NULL);
output->w = w;
output->v[0] = v1;
output->v[1] = v2;
output->v[2] = v3;
}
void Quaternion_setIdentity(Quaternion* q)
{
assert(q != NULL);
Quaternion_set(1, 0, 0, 0, q);
}
void Quaternion_copy(Quaternion* q, Quaternion* output)
{
Quaternion_set(q->w, q->v[0], q->v[1], q->v[2], output);
}
bool Quaternion_equal(Quaternion* q1, Quaternion* q2)
{
bool equalW = fabs(q1->w - q2->w) <= QUATERNION_EPS;
bool equalV0 = fabs(q1->v[0] - q2->v[0]) <= QUATERNION_EPS;
bool equalV1 = fabs(q1->v[1] - q2->v[1]) <= QUATERNION_EPS;
bool equalV2 = fabs(q1->v[2] - q2->v[2]) <= QUATERNION_EPS;
return equalW && equalV0 && equalV1 && equalV2;
}
void Quaternion_fprint(FILE* file, Quaternion* q)
{
fprintf(file, "(%.3f, %.3f, %.3f, %.3f)",
q->w, q->v[0], q->v[1], q->v[2]);
}
void Quaternion_fromAxisAngle(double axis[3], double angle, Quaternion* output)
{
assert(output != NULL);
// Formula from http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/
output->w = cos(angle / 2.0);
double c = sin(angle / 2.0);
output->v[0] = c * axis[0];
output->v[1] = c * axis[1];
output->v[2] = c * axis[2];
}
double Quaternion_toAxisAngle(Quaternion* q, double output[3])
{
assert(output != NULL);
// Formula from http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/
double angle = 2.0 * acos(q->w);
double divider = sqrt(1.0 - q->w * q->w);
if(divider != 0.0) {
// Calculate the axis
output[0] = q->v[0] / divider;
output[1] = q->v[1] / divider;
output[2] = q->v[2] / divider;
} else {
// Arbitrary normalized axis
output[0] = 1;
output[1] = 0;
output[2] = 0;
}
return angle;
}
void Quaternion_fromXRotation(double angle, Quaternion* output)
{
assert(output != NULL);
double axis[3] = {1.0, 0, 0};
Quaternion_fromAxisAngle(axis, angle, output);
}
void Quaternion_fromYRotation(double angle, Quaternion* output)
{
assert(output != NULL);
double axis[3] = {0, 1.0, 0};
Quaternion_fromAxisAngle(axis, angle, output);
}
void Quaternion_fromZRotation(double angle, Quaternion* output)
{
assert(output != NULL);
double axis[3] = {0, 0, 1.0};
Quaternion_fromAxisAngle(axis, angle, output);
}
void Quaternion_fromEulerZYX(double eulerZYX[3], Quaternion* output)
{
assert(output != NULL);
// Based on https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
double cy = cos(eulerZYX[2] * 0.5);
double sy = sin(eulerZYX[2] * 0.5);
double cr = cos(eulerZYX[0] * 0.5);
double sr = sin(eulerZYX[0] * 0.5);
double cp = cos(eulerZYX[1] * 0.5);
double sp = sin(eulerZYX[1] * 0.5);
output->w = cy * cr * cp + sy * sr * sp;
output->v[0] = cy * sr * cp - sy * cr * sp;
output->v[1] = cy * cr * sp + sy * sr * cp;
output->v[2] = sy * cr * cp - cy * sr * sp;
}
void Quaternion_toEulerZYX(Quaternion* q, double output[3])
{
assert(output != NULL);
// Roll (x-axis rotation)
double sinr_cosp = +2.0 * (q->w * q->v[0] + q->v[1] * q->v[2]);
double cosr_cosp = +1.0 - 2.0 * (q->v[0] * q->v[0] + q->v[1] * q->v[1]);
output[0] = atan2(sinr_cosp, cosr_cosp);
// Pitch (y-axis rotation)
double sinp = +2.0 * (q->w * q->v[1] - q->v[2] * q->v[0]);
if (fabs(sinp) >= 1)
output[1] = copysign(M_PI / 2, sinp); // use 90 degrees if out of range
else
output[1] = asin(sinp);
// Yaw (z-axis rotation)
double siny_cosp = +2.0 * (q->w * q->v[2] + q->v[0] * q->v[1]);
double cosy_cosp = +1.0 - 2.0 * (q->v[1] * q->v[1] + q->v[2] * q->v[2]);
output[2] = atan2(siny_cosp, cosy_cosp);
}
void Quaternion_conjugate(Quaternion* q, Quaternion* output)
{
assert(output != NULL);
output->w = q->w;
output->v[0] = -q->v[0];
output->v[1] = -q->v[1];
output->v[2] = -q->v[2];
}
double Quaternion_norm(Quaternion* q)
{
assert(q != NULL);
return sqrt(q->w*q->w + q->v[0]*q->v[0] + q->v[1]*q->v[1] + q->v[2]*q->v[2]);
}
void Quaternion_normalize(Quaternion* q, Quaternion* output)
{
assert(output != NULL);
double len = Quaternion_norm(q);
Quaternion_set(
q->w / len,
q->v[0] / len,
q->v[1] / len,
q->v[2] / len,
output);
}
void Quaternion_multiply(Quaternion* q1, Quaternion* q2, Quaternion* output)
{
assert(output != NULL);
Quaternion result;
/*
Formula from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/arithmetic/index.htm
a*e - b*f - c*g - d*h
+ i (b*e + a*f + c*h- d*g)
+ j (a*g - b*h + c*e + d*f)
+ k (a*h + b*g - c*f + d*e)
*/
result.w = q1->w *q2->w - q1->v[0]*q2->v[0] - q1->v[1]*q2->v[1] - q1->v[2]*q2->v[2];
result.v[0] = q1->v[0]*q2->w + q1->w *q2->v[0] + q1->v[1]*q2->v[2] - q1->v[2]*q2->v[1];
result.v[1] = q1->w *q2->v[1] - q1->v[0]*q2->v[2] + q1->v[1]*q2->w + q1->v[2]*q2->v[0];
result.v[2] = q1->w *q2->v[2] + q1->v[0]*q2->v[1] - q1->v[1]*q2->v[0] + q1->v[2]*q2->w ;
*output = result;
}
void Quaternion_rotate(Quaternion* q, double v[3], double output[3])
{
assert(output != NULL);
double result[3];
double ww = q->w * q->w;
double xx = q->v[0] * q->v[0];
double yy = q->v[1] * q->v[1];
double zz = q->v[2] * q->v[2];
double wx = q->w * q->v[0];
double wy = q->w * q->v[1];
double wz = q->w * q->v[2];
double xy = q->v[0] * q->v[1];
double xz = q->v[0] * q->v[2];
double yz = q->v[1] * q->v[2];
// Formula from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/transforms/index.htm
// p2.x = w*w*p1.x + 2*y*w*p1.z - 2*z*w*p1.y + x*x*p1.x + 2*y*x*p1.y + 2*z*x*p1.z - z*z*p1.x - y*y*p1.x;
// p2.y = 2*x*y*p1.x + y*y*p1.y + 2*z*y*p1.z + 2*w*z*p1.x - z*z*p1.y + w*w*p1.y - 2*x*w*p1.z - x*x*p1.y;
// p2.z = 2*x*z*p1.x + 2*y*z*p1.y + z*z*p1.z - 2*w*y*p1.x - y*y*p1.z + 2*w*x*p1.y - x*x*p1.z + w*w*p1.z;
result[0] = ww*v[0] + 2*wy*v[2] - 2*wz*v[1] +
xx*v[0] + 2*xy*v[1] + 2*xz*v[2] -
zz*v[0] - yy*v[0];
result[1] = 2*xy*v[0] + yy*v[1] + 2*yz*v[2] +
2*wz*v[0] - zz*v[1] + ww*v[1] -
2*wx*v[2] - xx*v[1];
result[2] = 2*xz*v[0] + 2*yz*v[1] + zz*v[2] -
2*wy*v[0] - yy*v[2] + 2*wx*v[1] -
xx*v[2] + ww*v[2];
// Copy result to output
output[0] = result[0];
output[1] = result[1];
output[2] = result[2];
}
void Quaternion_slerp(Quaternion* q1, Quaternion* q2, double t, Quaternion* output)
{
Quaternion result;
// Based on http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/index.htm
double cosHalfTheta = q1->w*q2->w + q1->v[0]*q2->v[0] + q1->v[1]*q2->v[1] + q1->v[2]*q2->v[2];
// if q1=q2 or qa=-q2 then theta = 0 and we can return qa
if (abs(cosHalfTheta) >= 1.0) {
Quaternion_copy(q1, output);
return;
}
double halfTheta = acos(cosHalfTheta);
double sinHalfTheta = sqrt(1.0 - cosHalfTheta*cosHalfTheta);
// If theta = 180 degrees then result is not fully defined
// We could rotate around any axis normal to q1 or q2
if (fabs(sinHalfTheta) < QUATERNION_EPS) {
result.w = (q1->w * 0.5 + q2->w * 0.5);
result.v[0] = (q1->v[0] * 0.5 + q2->v[0] * 0.5);
result.v[1] = (q1->v[1] * 0.5 + q2->v[1] * 0.5);
result.v[2] = (q1->v[2] * 0.5 + q2->v[2] * 0.5);
}
// Calculate Quaternion
double ratioA = sin((1 - t) * halfTheta) / sinHalfTheta;
double ratioB = sin(t * halfTheta) / sinHalfTheta;
result.w = (q1->w * ratioA + q2->w * ratioB);
result.v[0] = (q1->v[0] * ratioA + q2->v[0] * ratioB);
result.v[1] = (q1->v[1] * ratioA + q2->v[1] * ratioB);
result.v[2] = (q1->v[2] * ratioA + q2->v[2] * ratioB);
*output = result;
}