-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuaternion.cpp
55 lines (44 loc) · 1.22 KB
/
Quaternion.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
#include <cmath>
#include "Quaternion.h"
Quaternion::Quaternion(float w, float x, float y, float z) :
w(w),
x(x),
y(y),
z(z)
{
}
Quaternion Quaternion::operator+(Quaternion const & other)
{
return Quaternion(this->w + other.w, this->x + other.x, this->y + other.y, this->z + other.z);
}
Quaternion Quaternion::operator*(float k)
{
return Quaternion(this->w * k, this->x * k, this->y * k, this->z * k);
}
Quaternion operator*(float k, Quaternion q)
{
return q * k;
}
Quaternion Quaternion::operator*(Quaternion const & other)
{
return Quaternion(
w * other.w - x * other.x - y * other.y - z * other.z,
w * other.x + x * other.w + y * other.z - z * other.y,
w * other.y - x * other.z + y * other.w + z * other.x,
w * other.z + x * other.y - y * other.x + z * other.w
);
}
Quaternion Quaternion::conjugate()
{
return Quaternion(w, -x, -y, -z);
}
Quaternion Quaternion::normalize()
{
float norm = std::sqrt(w * w + x * x + y * y + z * z);
return Quaternion(w / norm, x / norm, y / norm, z / norm);
}
std::ostream & operator<<(std::ostream & os, Quaternion q)
{
os << "Quaternion{" << q.w << ", " << q.x << ", " << q.y << ", " << q.z << "}";
return os;
}