-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquaternion.h
93 lines (79 loc) · 2.57 KB
/
quaternion.h
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
86
87
88
89
90
91
92
93
/**
* Quaternion.cpp v1.0.0 30/08/2023
*
* Copyright (c) 2023, Robert Eisele (raw.org)
* Licensed under the MIT license.
**/
#ifndef QUATERNION_H
#define QUATERNION_H
#ifdef ARDUINO
#include "Arduino.h"
#else
#include <math.h>
#endif
#define QUATERNION_EULER_XYZ 1
#define QUATERNION_EULER_YXZ 2
#define QUATERNION_EULER_ZYX 3
#define QUATERNION_EULER_YZX 4
#define QUATERNION_EULER_XZY 5
#define QUATERNION_EULER_ZYZ 6
#define QUATERNION_EULER_ZXZ 7
#define QUATERNION_EULER_YXY 8
#define QUATERNION_EULER_YZY 9
#define QUATERNION_EULER_XYX 10
#define QUATERNION_EULER_XZX 11
#define QUATERNION_EULER_RPY QUATERNION_EULER_XYZ
#define QUATERNION_EULER_YPR QUATERNION_EULER_ZYX
// Default order that is used is ZYX
#ifndef QUATERNION_EULER_ORDER
#define QUATERNION_EULER_ORDER QUATERNION_EULER_ZYX
#endif
class Quaternion
{
public:
float w;
float x;
float y;
float z;
/* @TODO
union {
struct {
float x;
float y;
float z;
};
real_t vector[3] = { 0, 0, 0 };
};
*/
Quaternion() : w(1), x(0), y(0), z(0) {}
Quaternion(const Quaternion &q) : w(q.w), x(q.x), y(q.y), z(q.z) {}
Quaternion(float _x, float _y, float _z) : w(0), x(_x), y(_y), z(_z) {}
Quaternion(float _w, float _x, float _y, float _z) : w(_w), x(_x), y(_y), z(_z) {}
Quaternion &operator=(const Quaternion &rhs);
Quaternion &operator+=(const Quaternion &q);
Quaternion &operator-=(const Quaternion &q);
Quaternion &operator*=(float scale);
Quaternion &operator*=(const Quaternion &q);
const Quaternion operator-() const { return Quaternion(-w, -x, -y, -z); }
const Quaternion operator*(const Quaternion &q) const { return Quaternion(*this) *= q; }
const Quaternion operator*(float scale) const { return Quaternion(w * scale, x * scale, y * scale, z * scale); }
const Quaternion operator+(const Quaternion &q2) const
{
const Quaternion &q1 = *this;
return Quaternion(q1.w + q2.w, q1.x + q2.x, q1.y + q2.y, q1.z + q2.z);
}
const Quaternion operator-(const Quaternion &q2) const
{
const Quaternion &q1 = *this;
return Quaternion(q1.w - q2.w, q1.x - q2.x, q1.y - q2.y, q1.z - q2.z);
}
float dot(const Quaternion &q) const;
float norm() const;
float normSq() const;
Quaternion &normalize();
const Quaternion conjugate() const;
void rotateVector(float &vx, float &vy, float &vz);
static const Quaternion fromEuler(float _x, float _y, float _z);
static const Quaternion fromAxisAngle(float x, float y, float z, float angle);
};
#endif