-
Notifications
You must be signed in to change notification settings - Fork 0
/
myQuat.cpp
85 lines (76 loc) · 2.43 KB
/
myQuat.cpp
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
#include "MyQuat.h"
#define PI 3.14159265
MyQuat::MyQuat(void) {
}
MyQuat::MyQuat(float angleDeg, MyVector &axis) {
float angleRad = angleDeg * PI / 180.0;
this->w = cos(angleRad / 2.0);
this->v = axis;
this->v.normalise();
this->v = this->v.multiplyScalar(sin(angleRad / 2.0));
}
MyQuat::MyQuat(MyPosition &p) {
this->w = 0.0;
this->v.x = p.x;
this->v.y = p.y;
this->v.z = p.z;
this->v.normalise();
}
MyQuat::MyQuat(const MyQuat * q) {
this->w = q->w;
this->v = MyVector(q->v);
}
MyQuat MyQuat::addTo(const MyQuat &other) const {
MyQuat sum;
sum.w = w + other.w;
sum.v = v.addTo(other.v);
return sum;
}
MyQuat MyQuat::multiplyBy(const MyQuat &other) const {
MyQuat prod;
prod.w = this->w * other.w - this->v.getDotProduct(other.v);
prod.v = this->v.getCrossProduct(other.v).addTo(other.v.multiplyScalar(this->w).addTo(this->v.multiplyScalar(other.w)));
return prod;
}
float MyQuat::getMagnitude(void) const {
return sqrt(w*w + pow(v.getMagnitude(), 2));
}
void MyQuat::normalise(void) {
float magnitude = getMagnitude();
w /= magnitude;
v = v.multiplyScalar(1.0 / magnitude);
}
MyQuat MyQuat::getConjugate(void) const {
MyQuat conj;
conj.w = this->w;
conj.v = v.multiplyScalar(-1.0);
return conj;
}
MyQuat MyQuat::getInverse(void) const {
float denom = pow(getMagnitude(), 2);
MyQuat inverse;
inverse.w = w / denom;
inverse.v = v.multiplyScalar(-1.0 / denom);
return inverse;
}
MyMatrix MyQuat::convertToRotationMatrix(void) const {
MyQuat normalized(this);
normalized.normalise();
float coeffs[16] = {
pow(normalized.w, 2) + pow(normalized.v.x, 2) - pow(normalized.v.y, 2) - pow(normalized.v.z, 2),
2.0 * normalized.v.x * normalized.v.y - 2.0 * normalized.w * normalized.v.z,
2.0 * normalized.v.x * normalized.v.z + 2.0 * normalized.w * normalized.v.y,
0.0,
2.0 * normalized.v.x * normalized.v.y + 2.0 * normalized.w * normalized.v.z,
pow(normalized.w, 2) - pow(normalized.v.x, 2) + pow(normalized.v.y, 2) - pow(normalized.v.z, 2),
2.0 * normalized.v.y * normalized.v.z - 2.0 * normalized.w * normalized.v.x,
0.0,
2.0 * normalized.v.x * normalized.v.z - 2.0 * normalized.w * normalized.v.y,
2.0 * normalized.v.y * normalized.v.z + 2.0 * normalized.w * normalized.v.x,
pow(normalized.w, 2) - pow(normalized.v.x, 2) - pow(normalized.v.y, 2) + pow(normalized.v.z, 2),
0.0,
0.0, 0.0, 0.0, 1.0
};
MyMatrix matrix(coeffs);
return matrix;
}