adafruit_bno055 / utility / vector.h @ 651c5f56
History | View | Annotate | Download (4.889 KB)
1 | 4bc1c0c1 | Kevin Townsend | /*
|
---|---|---|---|
2 | Inertial Measurement Unit Maths Library
|
||
3 | Copyright (C) 2013-2014 Samuel Cowen
|
||
4 | d964148c | Tony DiCola | www.camelsoftware.com
|
5 | 4bc1c0c1 | Kevin Townsend | |
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 | d964148c | Tony DiCola | Vector() |
35 | { |
||
36 | 4bc1c0c1 | Kevin Townsend | memset(p_vec, 0, sizeof(double)*N); |
37 | d964148c | Tony DiCola | } |
38 | 4bc1c0c1 | Kevin Townsend | |
39 | d964148c | Tony DiCola | Vector(double a)
|
40 | { |
||
41 | 4bc1c0c1 | Kevin Townsend | memset(p_vec, 0, sizeof(double)*N); |
42 | d964148c | Tony DiCola | p_vec[0] = a;
|
43 | } |
||
44 | 4bc1c0c1 | Kevin Townsend | |
45 | d964148c | Tony DiCola | Vector(double a, double b) |
46 | { |
||
47 | 4bc1c0c1 | Kevin Townsend | memset(p_vec, 0, sizeof(double)*N); |
48 | d964148c | Tony DiCola | p_vec[0] = a;
|
49 | p_vec[1] = b;
|
||
50 | } |
||
51 | 4bc1c0c1 | Kevin Townsend | |
52 | d964148c | Tony DiCola | Vector(double a, double b, double c) |
53 | { |
||
54 | 4bc1c0c1 | Kevin Townsend | memset(p_vec, 0, sizeof(double)*N); |
55 | d964148c | Tony DiCola | p_vec[0] = a;
|
56 | p_vec[1] = b;
|
||
57 | p_vec[2] = c;
|
||
58 | } |
||
59 | 4bc1c0c1 | Kevin Townsend | |
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 | d964148c | Tony DiCola | p_vec[1] = b;
|
65 | p_vec[2] = c;
|
||
66 | p_vec[3] = d;
|
||
67 | 4bc1c0c1 | Kevin Townsend | } |
68 | |||
69 | Vector(const Vector<N> &v)
|
||
70 | { |
||
71 | b2d499c7 | Paul Du Bois (laptop) | for (int x = 0; x < N; x++) |
72 | 4bc1c0c1 | Kevin Townsend | p_vec[x] = v.p_vec[x]; |
73 | } |
||
74 | |||
75 | ~Vector() |
||
76 | { |
||
77 | } |
||
78 | |||
79 | uint8_t n() { return N; }
|
||
80 | |||
81 | d7b28532 | Gé Vissers | double magnitude() const |
82 | 4bc1c0c1 | Kevin Townsend | { |
83 | double res = 0; |
||
84 | 651c5f56 | Gé Vissers | for (int i = 0; i < N; i++) |
85 | res += p_vec[i] * p_vec[i]; |
||
86 | 4bc1c0c1 | Kevin Townsend | |
87 | if(isnan(res))
|
||
88 | return 0; |
||
89 | abce6607 | Paul Du Bois (laptop) | if((fabs(res-1)) >= 0.000001) // Avoid a sqrt if possible. |
90 | 4bc1c0c1 | Kevin Townsend | return sqrt(res);
|
91 | return 1; |
||
92 | } |
||
93 | |||
94 | void normalize()
|
||
95 | { |
||
96 | double mag = magnitude();
|
||
97 | a61c970f | Gé Vissers | if (fabs(mag) <= 0.0001) |
98 | 4bc1c0c1 | Kevin Townsend | return;
|
99 | |||
100 | int i;
|
||
101 | for(i = 0; i < N; i++) |
||
102 | p_vec[i] = p_vec[i]/mag; |
||
103 | } |
||
104 | |||
105 | d7b28532 | Gé Vissers | double dot(const Vector& v) const |
106 | 4bc1c0c1 | Kevin Townsend | { |
107 | double ret = 0; |
||
108 | 651c5f56 | Gé Vissers | for (int i = 0; i < N; i++) |
109 | 4bc1c0c1 | Kevin Townsend | ret += p_vec[i] * v.p_vec[i]; |
110 | |||
111 | return ret;
|
||
112 | } |
||
113 | |||
114 | fd9de024 | Gé Vissers | // The cross product is only valid for vectors with 3 dimensions,
|
115 | // with the exception of higher dimensional stuff that is beyond
|
||
116 | // the intended scope of this library.
|
||
117 | // Only a definition for N==3 is given below this class, using
|
||
118 | // cross() with another value for N will result in a link error.
|
||
119 | Vector cross(const Vector& v) const; |
||
120 | 4bc1c0c1 | Kevin Townsend | |
121 | 0695bf91 | Paul Du Bois (laptop) | Vector scale(double scalar) const |
122 | 4bc1c0c1 | Kevin Townsend | { |
123 | Vector ret; |
||
124 | for(int i = 0; i < N; i++) |
||
125 | ret.p_vec[i] = p_vec[i] * scalar; |
||
126 | return ret;
|
||
127 | } |
||
128 | |||
129 | 0695bf91 | Paul Du Bois (laptop) | Vector invert() const
|
130 | 4bc1c0c1 | Kevin Townsend | { |
131 | Vector ret; |
||
132 | for(int i = 0; i < N; i++) |
||
133 | ret.p_vec[i] = -p_vec[i]; |
||
134 | return ret;
|
||
135 | } |
||
136 | |||
137 | d7b28532 | Gé Vissers | Vector& operator=(const Vector& v)
|
138 | 4bc1c0c1 | Kevin Townsend | { |
139 | for (int x = 0; x < N; x++ ) |
||
140 | p_vec[x] = v.p_vec[x]; |
||
141 | d964148c | Tony DiCola | return *this;
|
142 | 4bc1c0c1 | Kevin Townsend | } |
143 | |||
144 | double& operator [](int n) |
||
145 | { |
||
146 | return p_vec[n];
|
||
147 | } |
||
148 | |||
149 | 0695bf91 | Paul Du Bois (laptop) | double operator [](int n) const |
150 | { |
||
151 | return p_vec[n];
|
||
152 | } |
||
153 | |||
154 | 4bc1c0c1 | Kevin Townsend | double& operator ()(int n) |
155 | { |
||
156 | return p_vec[n];
|
||
157 | } |
||
158 | |||
159 | 0695bf91 | Paul Du Bois (laptop) | double operator ()(int n) const |
160 | { |
||
161 | return p_vec[n];
|
||
162 | } |
||
163 | |||
164 | d7b28532 | Gé Vissers | Vector operator+(const Vector& v) const |
165 | 4bc1c0c1 | Kevin Townsend | { |
166 | Vector ret; |
||
167 | for(int i = 0; i < N; i++) |
||
168 | ret.p_vec[i] = p_vec[i] + v.p_vec[i]; |
||
169 | return ret;
|
||
170 | } |
||
171 | |||
172 | d7b28532 | Gé Vissers | Vector operator-(const Vector& v) const |
173 | 4bc1c0c1 | Kevin Townsend | { |
174 | Vector ret; |
||
175 | for(int i = 0; i < N; i++) |
||
176 | ret.p_vec[i] = p_vec[i] - v.p_vec[i]; |
||
177 | return ret;
|
||
178 | } |
||
179 | |||
180 | 0695bf91 | Paul Du Bois (laptop) | Vector operator * (double scalar) const |
181 | 4bc1c0c1 | Kevin Townsend | { |
182 | return scale(scalar);
|
||
183 | } |
||
184 | |||
185 | 0695bf91 | Paul Du Bois (laptop) | Vector operator / (double scalar) const |
186 | 4bc1c0c1 | Kevin Townsend | { |
187 | Vector ret; |
||
188 | for(int i = 0; i < N; i++) |
||
189 | ret.p_vec[i] = p_vec[i] / scalar; |
||
190 | return ret;
|
||
191 | } |
||
192 | |||
193 | void toDegrees()
|
||
194 | { |
||
195 | for(int i = 0; i < N; i++) |
||
196 | p_vec[i] *= 57.2957795131; //180/pi |
||
197 | } |
||
198 | |||
199 | void toRadians()
|
||
200 | { |
||
201 | for(int i = 0; i < N; i++) |
||
202 | p_vec[i] *= 0.01745329251; //pi/180 |
||
203 | } |
||
204 | |||
205 | double& x() { return p_vec[0]; } |
||
206 | double& y() { return p_vec[1]; } |
||
207 | double& z() { return p_vec[2]; } |
||
208 | 0695bf91 | Paul Du Bois (laptop) | double x() const { return p_vec[0]; } |
209 | double y() const { return p_vec[1]; } |
||
210 | double z() const { return p_vec[2]; } |
||
211 | 4bc1c0c1 | Kevin Townsend | |
212 | |||
213 | private:
|
||
214 | 651c5f56 | Gé Vissers | double p_vec[N];
|
215 | 4bc1c0c1 | Kevin Townsend | }; |
216 | |||
217 | |||
218 | fd9de024 | Gé Vissers | template <> |
219 | inline Vector<3> Vector<3>::cross(const Vector& v) const |
||
220 | { |
||
221 | return Vector(
|
||
222 | p_vec[1] * v.p_vec[2] - p_vec[2] * v.p_vec[1], |
||
223 | p_vec[2] * v.p_vec[0] - p_vec[0] * v.p_vec[2], |
||
224 | p_vec[0] * v.p_vec[1] - p_vec[1] * v.p_vec[0] |
||
225 | ); |
||
226 | } |
||
227 | |||
228 | } // namespace
|
||
229 | 4bc1c0c1 | Kevin Townsend | |
230 | #endif |