-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuaternion.cpp
More file actions
73 lines (56 loc) · 2.28 KB
/
Quaternion.cpp
File metadata and controls
73 lines (56 loc) · 2.28 KB
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
//
// Created by redkc on 23/11/2023.
//
#include <valarray>
#include "Quaternion.h"
#include "Utilis.h"
Quaternion Quaternion::operator+(const Quaternion &q) const {
return {a + q.a, v + v};
}
Quaternion Quaternion::operator-(const Quaternion &q) const {
return {a - q.a, v - v};
}
Quaternion Quaternion::operator*(const Quaternion &q) const {
return {a * q.a - v.dotProduct(q.v), q.v*a + v*q.a + v.cross(q.v)};
}
Quaternion Quaternion::operator/(const Quaternion &q) const {
return {(a*q.a + v.dotProduct(q.v))/((q.a * q.a) + q.v.dotProduct(q.v)), ((v*q.a) - v.cross(q.v) - (q.v*a))*((q.a * q.a) + q.v.dotProduct(q.v))};
}
bool Quaternion::operator==(const Quaternion &q) const {
if(a == q.a && v == q.v){
return true;
}
return false;
}
std::string Quaternion::toString() const {
std::ostringstream oss;
oss << "(" << a << ",( " << v.toString() << ")";
return oss.str();
}
Quaternion Quaternion::conjugate() {
return(Quaternion( a,Vector3(-v.x,-v.y,-v.z)));
}
Vector3 Quaternion::rotationX(const Vector3 &vector, float angle) {
float fixedAngle = angle * M_PI / 180.0f;
Quaternion rotatioQuaternion = Quaternion(cos(fixedAngle / 2), Vector3(sin(fixedAngle / 2),0.0f,0.0f));
Quaternion vectorQuaternion(0.0f,Vector3(-1,-1,-1));
Quaternion conjugated = rotatioQuaternion.conjugate();
Quaternion rotated = rotatioQuaternion * vectorQuaternion * conjugated;
return rotated.v;
}
Vector3 Quaternion::rotationY(const Vector3 &vector, float angle) {
float fixedAngle = angle * M_PI / 180.0f;
Quaternion rotatioQuaternion = Quaternion(cos(fixedAngle / 2), Vector3(0.0f,sin(fixedAngle / 2),0.0f));
Quaternion vectorQuaternion(0.0f,Vector3(-1,-1,-1));
Quaternion conjugated = rotatioQuaternion.conjugate();
Quaternion rotated = rotatioQuaternion * vectorQuaternion * conjugated;
return rotated.v;
}
Vector3 Quaternion::rotationZ(const Vector3 &vector, float angle) {
float fixedAngle = angle * M_PI / 180.0f;
Quaternion rotatioQuaternion = Quaternion(cos(fixedAngle / 2), Vector3(0.0f,0.0f,sin(fixedAngle / 2)));
Quaternion vectorQuaternion(0.0f,vector);
Quaternion conjugated = rotatioQuaternion.conjugate();
Quaternion rotated = rotatioQuaternion * vectorQuaternion * conjugated;
return rotated.v;
}