-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquaternion.hpp
167 lines (162 loc) · 4.63 KB
/
quaternion.hpp
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#ifndef __QUATERNION_HPP__
#define __QUATERNION_HPP__
#include<stdexcept>
#include<ostream>
#include<cmath>
#include"common.hpp"
struct Quaternion {
typedef float realq;
static const realq eps_norm;
realq a, b, c, d;
inline Quaternion()
:a(0.f), b(0.f), c(0.f), d(0.f) {};
inline Quaternion(realq a, realq b, realq c, realq d)
:a(a), b(b), c(c), d(d) {};
template<typename T>
inline Quaternion(const cv::Mat_<T> & rotmat)
:a(0.f), d(0.5f * sqrt(1.f - rotmat(0,0) - rotmat(1,1) + rotmat(2,2))) {
if (abs(d) < 1e-2)
a = 0.5f * sqrt(1.f + rotmat(0,0) + rotmat(1,1) + rotmat(2,2));
if (abs(a) < abs(d)) {
//std::cout << "1" << std::endl;
realq f = 0.25f / d;
a = f * (rotmat(1,0) - rotmat(0,1));
b = f * (rotmat(0,2) + rotmat(2,0));
c = f * (rotmat(1,2) + rotmat(2,1));
} else {
//std::cout << "2" << std::endl;
realq f = 0.25f / a;
b = f * (rotmat(2,1) - rotmat(1,2));
c = f * (rotmat(0,2) - rotmat(2,0));
d = f * (rotmat(1,0) - rotmat(0,1));
}
/*
d = 0.5f * sqrt(1.f + rotmat(0,0) + rotmat(1,1) + rotmat(2,2));
if (abs(d) < 1e-2)
a = 0.5f * sqrt(1.f + rotmat(0,0) - rotmat(1,1) - rotmat(2,2));
if (abs(a) < abs(d)) {
std::cout << "1" << std::endl;
realq f = 0.25f / d;
a = f * (rotmat(2,1) - rotmat(1,2));
b = f * (rotmat(0,2) - rotmat(2,0));
c = f * (rotmat(1,0) - rotmat(0,1));
} else {
std::cout << "2" << std::endl;
realq f = 0.25f / a;
b = f * (rotmat(0,1) + rotmat(1,0));
c = f * (rotmat(0,2) + rotmat(2,0));
d = f * (rotmat(2,1) - rotmat(1,2));
}
*/
}
inline Quaternion & operator=(const Quaternion & q) {
a = q.a;
b = q.b;
c = q.c;
d = q.d;
return *this;
}
inline static bool approxeq(realq a, realq b) {
return (a - eps_norm < b) && (b < a + eps_norm);
}
inline bool operator==(const Quaternion & q) const {
return approxeq(a, q.a) && approxeq(b, q.b) && approxeq(c, q.c) && approxeq(d, q.d);
}
inline bool operator!=(const Quaternion & q) const {
return !(operator==(q));
}
inline Quaternion operator+(const Quaternion & q) const {
return Quaternion(a+q.a, b+q.b, c+q.c, d+q.d);
}
inline Quaternion & operator+=(const Quaternion & q) {
a += q.a;
b += q.b;
c += q.c;
d += q.d;
return *this;
}
inline Quaternion operator-(const Quaternion & q) const {
return Quaternion(a-q.a, b-q.b, c-q.c, d-q.d);
}
inline Quaternion & operator-=(const Quaternion & q) {
a -= q.a;
b -= q.b;
c -= q.c;
d -= q.d;
return *this;
}
inline Quaternion operator-() const {
return Quaternion(-a, -b, -c, -d);
}
inline Quaternion operator*(realq s) const {
return Quaternion(s*a, s*b, s*c, s*d);
}
inline Quaternion & operator*=(realq s) {
a *= s;
b *= s;
c *= s;
d *= s;
return *this;
}
inline Quaternion operator*(const Quaternion & q) const {
return Quaternion(a*q.a - b*q.b - c*q.c - d*q.d,
a*q.b + b*q.a + c*q.d - d*q.c,
a*q.c - b*q.d + c*q.a + d*q.b,
a*q.d + b*q.c - c*q.b + d*q.a);
}
inline Quaternion & operator*=(const Quaternion & q) {
return operator=(operator*(q));
}
inline realq norm2() const {
return a*a+b*b+c*c+d*d;
}
inline realq norm() const {
return sqrt(a*a+b*b+c*c+d*d);
}
inline Quaternion conjugate() const {
return Quaternion(a, -b, -c, -d);
}
inline Quaternion inv() const {
const realq n = norm();
if (n < eps_norm)
throw std::overflow_error("Quaternion: division by zero");
const realq in = 1.f / n;
return Quaternion(a*in, -b*in, -c*in, -d*in);
}
inline Quaternion operator/(realq s) const {
return operator*(1./s);
}
inline Quaternion operator/=(realq s) {
return operator*=(1./s);
}
inline Quaternion operator/(const Quaternion & q) const {
return operator*(q.inv());
}
inline Quaternion & operator/=(const Quaternion & q) {
return operator*=(q.inv());
}
inline realq & operator[](int i) {
return (&a)[i];
}
inline realq operator[](int i) const {
return (&a)[i];
}
inline matf toMat() const {
matf out(3,3);
out(0,0) = a*a + b*b - c*c - d*d;
out(0,1) = 2.f * (b*c - a*d);
out(0,2) = 2.f * (b*d + a*c);
out(1,0) = 2.f * (b*c + a*d);
out(1,1) = a*a - b*b + c*c - d*d;
out(1,2) = 2.f * (c*d - a*b);
out(2,0) = 2.f * (b*d - a*c);
out(2,1) = 2.f * (c*d + a*b);
out(2,2) = a*a - b*b - c*c + d*d;
return out;
}
};
inline std::ostream & operator<<(std::ostream & cout_v, const Quaternion & q) {
cout_v << "(" << q.a << "," << q.b << "," << q.c << "," << q.d << ")";
return cout_v;
}
#endif