acme
Foundational cross-platform layer of the ca2 framework
Loading...
Searching...
No Matches
quaternions.h
1#pragma once
2
3
4#include <math.h>
5
6struct Q4
7{
8
9 ::f64 w;
10 ::f64 x;
11 ::f64 y;
12 ::f64 z;
13
14};
15
16
17template < prototype_number NUMBER >
18class q4 :
19 public sequence_type < NUMBER, 4 >
20{
21public:
22
23
24 using UNIT_TYPE = NUMBER;
25
26
27 q4()
28 {
29
30 // randomly filled
31
32 }
33
34 q4(const q4 & q)
35 {
36
37 operator = (q);
38
39 }
40
41 template < prototype_number W, prototype_number X, prototype_number Y, prototype_number Z >
42 q4(W w, X x, Y y, Z z)
43 {
44
45 this->w = (UNIT_TYPE) w;
46 this->x = (UNIT_TYPE) x;
47 this->y = (UNIT_TYPE) y;
48 this->z = (UNIT_TYPE) z;
49
50 }
51
52
53 ::f64 sqsize()
54 {
55
56 return this->w * this->w + this->x * this->x + this->y * this->y + this->z * this->z;
57
58 }
59
60
61 ::f64 size()
62 {
63
64 return sqrt(sqsize());
65
66 }
67
68
69 q4 operator * (const q4 & q2)
70 {
71 if(this == &q2)
72 {
73 return operator *(q4(q2));
74 }
75
76 q4 q;
77
78 q.w = this->w * q2.w - this->x * q2.x - this->y * q2.y - this->z * q2.z;
79 q.x = this->w * q2.x + this->x * q2.w + this->y * q2.z - this->z * q2.y;
80 q.y = this->w * q2.y - this->x * q2.z + this->y * q2.w + this->z * q2.x;
81 q.z = this->w * q2.z + this->x * q2.y - this->y * q2.x + this->z * q2.w;
82
83 return q;
84
85 }
86
87 q4 & operator *= (const q4 & q2)
88 {
89
90 q4 q1(*this);
91
92 return operator = (q1 * q2);
93
94 }
95
96
97 q4 & operator = (const q4 & q2)
98 {
99
100 this->w = q2.w;
101 this->x = q2.x;
102 this->y = q2.y;
103 this->z = q2.z;
104
105 return *this;
106
107 }
108
109};
Definition quaternions.h:7