-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquaternion.cu
More file actions
77 lines (59 loc) · 2.4 KB
/
Copy pathquaternion.cu
File metadata and controls
77 lines (59 loc) · 2.4 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
74
75
76
77
#include "quaternion.h"
#include <cmath>
__host__ __device__ Quaternion::Quaternion(Vec3 Pos, Vec3 Up, Vec3 Right, Vec3 Look) : Pos(Pos), Up(Up), Right(Right), Look(Look) {}
__host__ __device__ const Vec3& Quaternion::getPos() const{
return this->Pos;
}
__host__ __device__ const Vec3& Quaternion::getRightVector() const{
return this->Right;
}
__host__ __device__ const Vec3& Quaternion::getUpVector() const{
return this->Up;
}
__host__ __device__ const Vec3& Quaternion::getLookVector() const{
return this->Look;
}
__host__ __device__ void Quaternion::setPos(Vec3 other) {
this->Pos = other;
}
__host__ __device__ void Quaternion::setRightVector(Vec3 other) {
this->Right = other;
}
__host__ __device__ void Quaternion::setUpVector(Vec3 other) {
this->Up = other;
}
__host__ __device__ void Quaternion::setLookVector(Vec3 other) {
this->Look = other;
}
__host__ __device__ Quaternion Quaternion::operator+(const Vec3& other) const{
Quaternion Out = *this;
Out.Pos += other;
return Out;
}
__host__ __device__ Quaternion& Quaternion::operator+=(const Vec3& other) {
this->Pos += other;
return *this;
}
__host__ __device__ Quaternion& Quaternion::rotate(const Vec3& other, double deg) {
Vec3 UnitVec = other.unitVector();
float ux = UnitVec.x;
float uy = UnitVec.y;
float uz = UnitVec.z;
float angleCos = cos(deg);
float angleSin = sin(deg);
Vec3 RotateX = Vec3(angleCos + ux*ux*(1-angleCos), ux*uy*(1-angleCos) - uz*angleSin, ux*uz*(1-angleCos)+uy*angleSin);
Vec3 RotateY = Vec3(uy*ux*(1-angleCos) + uz*angleSin,angleCos + uy*uy*(1-angleCos),uy*uz*(1-angleCos)-ux*angleSin);
Vec3 RotateZ = Vec3(uz*ux*(1-angleCos)-uy*angleSin,uz*uy*(1-angleCos)+ux*angleSin,angleCos+uz*uz*(1-angleCos));
this->Up = Vec3(this->Up.dot(RotateX),this->Up.dot(RotateY),this->Up.dot(RotateZ));
this->Right = Vec3(this->Right.dot(RotateX),this->Right.dot(RotateY),this->Right.dot(RotateZ));
this->Look = Vec3(this->Look.dot(RotateX),this->Look.dot(RotateY),this->Look.dot(RotateZ));
return *this;
}
std::ostream& operator<<(std::ostream& os, const Quaternion& CFrame) {
Vec3 Pos = CFrame.getPos();
Vec3 Up = CFrame.getUpVector();
Vec3 Right = CFrame.getRightVector();
Vec3 Look = CFrame.getLookVector();
os << "Position: (" << Pos << ", UpVector: " << Up << ", RightVector: " << Right << ", LookVector: " << Look << "\n";
return os;
}