adafruit_bno055 / utility / vector.h @ 3cae40b9
History | View | Annotate | Download (4.745 KB)
1 |
/*
|
---|---|
2 |
Inertial Measurement Unit Maths Library
|
3 |
Copyright (C) 2013-2014 Samuel Cowen
|
4 |
www.camelsoftware.com
|
5 |
|
6 |
This program is free software: you can redistribute it and/or modify
|
7 |
it under the terms of the GNU General Public License as published by
|
8 |
the Free Software Foundation, either version 3 of the License, or
|
9 |
(at your option) any later version.
|
10 |
|
11 |
This program is distributed in the hope that it will be useful,
|
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
GNU General Public License for more details.
|
15 |
|
16 |
You should have received a copy of the GNU General Public License
|
17 |
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
18 |
*/
|
19 |
|
20 |
#ifndef IMUMATH_VECTOR_HPP
|
21 |
#define IMUMATH_VECTOR_HPP
|
22 |
|
23 |
#include <string.h> |
24 |
#include <stdint.h> |
25 |
#include <math.h> |
26 |
|
27 |
|
28 |
namespace imu |
29 |
{ |
30 |
|
31 |
template <uint8_t N> class Vector |
32 |
{ |
33 |
public:
|
34 |
Vector() |
35 |
{ |
36 |
memset(p_vec, 0, sizeof(double)*N); |
37 |
} |
38 |
|
39 |
Vector(double a)
|
40 |
{ |
41 |
memset(p_vec, 0, sizeof(double)*N); |
42 |
p_vec[0] = a;
|
43 |
} |
44 |
|
45 |
Vector(double a, double b) |
46 |
{ |
47 |
memset(p_vec, 0, sizeof(double)*N); |
48 |
p_vec[0] = a;
|
49 |
p_vec[1] = b;
|
50 |
} |
51 |
|
52 |
Vector(double a, double b, double c) |
53 |
{ |
54 |
memset(p_vec, 0, sizeof(double)*N); |
55 |
p_vec[0] = a;
|
56 |
p_vec[1] = b;
|
57 |
p_vec[2] = c;
|
58 |
} |
59 |
|
60 |
Vector(double a, double b, double c, double d) |
61 |
{ |
62 |
memset(p_vec, 0, sizeof(double)*N); |
63 |
p_vec[0] = a;
|
64 |
p_vec[1] = b;
|
65 |
p_vec[2] = c;
|
66 |
p_vec[3] = d;
|
67 |
} |
68 |
|
69 |
Vector(const Vector<N> &v)
|
70 |
{ |
71 |
for (int x = 0; x < N; x++) |
72 |
p_vec[x] = v.p_vec[x]; |
73 |
} |
74 |
|
75 |
~Vector() |
76 |
{ |
77 |
} |
78 |
|
79 |
uint8_t n() { return N; }
|
80 |
|
81 |
double magnitude() const |
82 |
{ |
83 |
double res = 0; |
84 |
for (int i = 0; i < N; i++) |
85 |
res += p_vec[i] * p_vec[i]; |
86 |
|
87 |
return sqrt(res);
|
88 |
} |
89 |
|
90 |
void normalize()
|
91 |
{ |
92 |
double mag = magnitude();
|
93 |
if (isnan(mag) || mag == 0.0) |
94 |
return;
|
95 |
|
96 |
for (int i = 0; i < N; i++) |
97 |
p_vec[i] /= mag; |
98 |
} |
99 |
|
100 |
double dot(const Vector& v) const |
101 |
{ |
102 |
double ret = 0; |
103 |
for (int i = 0; i < N; i++) |
104 |
ret += p_vec[i] * v.p_vec[i]; |
105 |
|
106 |
return ret;
|
107 |
} |
108 |
|
109 |
// The cross product is only valid for vectors with 3 dimensions,
|
110 |
// with the exception of higher dimensional stuff that is beyond
|
111 |
// the intended scope of this library.
|
112 |
// Only a definition for N==3 is given below this class, using
|
113 |
// cross() with another value for N will result in a link error.
|
114 |
Vector cross(const Vector& v) const; |
115 |
|
116 |
Vector scale(double scalar) const |
117 |
{ |
118 |
Vector ret; |
119 |
for(int i = 0; i < N; i++) |
120 |
ret.p_vec[i] = p_vec[i] * scalar; |
121 |
return ret;
|
122 |
} |
123 |
|
124 |
Vector invert() const
|
125 |
{ |
126 |
Vector ret; |
127 |
for(int i = 0; i < N; i++) |
128 |
ret.p_vec[i] = -p_vec[i]; |
129 |
return ret;
|
130 |
} |
131 |
|
132 |
Vector& operator=(const Vector& v)
|
133 |
{ |
134 |
for (int x = 0; x < N; x++ ) |
135 |
p_vec[x] = v.p_vec[x]; |
136 |
return *this;
|
137 |
} |
138 |
|
139 |
double& operator [](int n) |
140 |
{ |
141 |
return p_vec[n];
|
142 |
} |
143 |
|
144 |
double operator [](int n) const |
145 |
{ |
146 |
return p_vec[n];
|
147 |
} |
148 |
|
149 |
double& operator ()(int n) |
150 |
{ |
151 |
return p_vec[n];
|
152 |
} |
153 |
|
154 |
double operator ()(int n) const |
155 |
{ |
156 |
return p_vec[n];
|
157 |
} |
158 |
|
159 |
Vector operator+(const Vector& v) const |
160 |
{ |
161 |
Vector ret; |
162 |
for(int i = 0; i < N; i++) |
163 |
ret.p_vec[i] = p_vec[i] + v.p_vec[i]; |
164 |
return ret;
|
165 |
} |
166 |
|
167 |
Vector operator-(const Vector& v) const |
168 |
{ |
169 |
Vector ret; |
170 |
for(int i = 0; i < N; i++) |
171 |
ret.p_vec[i] = p_vec[i] - v.p_vec[i]; |
172 |
return ret;
|
173 |
} |
174 |
|
175 |
Vector operator * (double scalar) const |
176 |
{ |
177 |
return scale(scalar);
|
178 |
} |
179 |
|
180 |
Vector operator / (double scalar) const |
181 |
{ |
182 |
Vector ret; |
183 |
for(int i = 0; i < N; i++) |
184 |
ret.p_vec[i] = p_vec[i] / scalar; |
185 |
return ret;
|
186 |
} |
187 |
|
188 |
void toDegrees()
|
189 |
{ |
190 |
for(int i = 0; i < N; i++) |
191 |
p_vec[i] *= 57.2957795131; //180/pi |
192 |
} |
193 |
|
194 |
void toRadians()
|
195 |
{ |
196 |
for(int i = 0; i < N; i++) |
197 |
p_vec[i] *= 0.01745329251; //pi/180 |
198 |
} |
199 |
|
200 |
double& x() { return p_vec[0]; } |
201 |
double& y() { return p_vec[1]; } |
202 |
double& z() { return p_vec[2]; } |
203 |
double x() const { return p_vec[0]; } |
204 |
double y() const { return p_vec[1]; } |
205 |
double z() const { return p_vec[2]; } |
206 |
|
207 |
|
208 |
private:
|
209 |
double p_vec[N];
|
210 |
}; |
211 |
|
212 |
|
213 |
template <> |
214 |
inline Vector<3> Vector<3>::cross(const Vector& v) const |
215 |
{ |
216 |
return Vector(
|
217 |
p_vec[1] * v.p_vec[2] - p_vec[2] * v.p_vec[1], |
218 |
p_vec[2] * v.p_vec[0] - p_vec[0] * v.p_vec[2], |
219 |
p_vec[0] * v.p_vec[1] - p_vec[1] * v.p_vec[0] |
220 |
); |
221 |
} |
222 |
|
223 |
} // namespace
|
224 |
|
225 |
#endif
|