VectorHelpers.h
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarX
19 * @author Mirko Waechter( mirko.waechter at kit dot edu)
20 * @date 2016
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24#pragma once
25
26#include <cmath>
27#include <limits>
28#include <map>
29#include <vector>
30
31#include <Eigen/Geometry>
32
34
35namespace armarx
36{
37 template <class T>
38 inline void
39 checkValue(const T& value)
40 {
41 if (std::numeric_limits<T>::infinity() == value)
42 {
43 throw LocalException("result value is inf");
44 }
45
46 if (value != value)
47 {
48 throw LocalException("result value is nan");
49 }
50 }
51
52 template <class T>
53 inline void
54 checkValues(const std::vector<T>& values)
55 {
56 for (size_t i = 0; i < values.size(); ++i)
57 {
58 checkValue(values[i]);
59 }
60 }
61
62 template <class T>
63 std::vector<T>
64 operator-(const std::vector<T>& v1, const std::vector<T>& v2)
65 {
66 std::vector<T> res(std::min(v1.size(), v2.size()), T());
67 int j = 0;
68
69 for (typename std::vector<T>::iterator i = res.begin(); i != res.end(); ++i)
70 {
71 *i = v1[j] - v2[j];
72 ++j;
73 }
74
75 return res;
76 }
77
78 template <class T>
79 std::vector<T>
80 operator+(const std::vector<T>& v1, const std::vector<T>& v2)
81 {
82 std::vector<T> res(std::min(v1.size(), v2.size()), T());
83 int j = 0;
84
85 for (typename std::vector<T>::iterator i = res.begin(); i != res.end(); ++i)
86 {
87 *i = v1[j] + v2[j];
88 ++j;
89 }
90
91 return res;
92 }
93
94 template <class T>
95 std::vector<T>&
96 operator+=(std::vector<T>& v1, const std::vector<T>& v2)
97 {
98 int j = 0;
99
100 for (typename std::vector<T>::iterator i = v1.begin(); i != v1.end(); ++i)
101 {
102 *i += v2[j];
103 ++j;
104 }
105
106 return v1;
107 }
108
109 template <class T>
110 std::vector<T>&
111 operator-=(std::vector<T>& v1, const std::vector<T>& v2)
112 {
113 int j = 0;
114
115 for (typename std::vector<T>::iterator i = v1.begin(); i != v1.end(); ++i)
116 {
117 *i -= v2[j];
118 ++j;
119 }
120
121 return v1;
122 }
123
124 template <class T>
125 std::vector<T>&
126 operator*=(std::vector<T>& v1, double c)
127 {
128 int j = 0;
129
130 for (typename std::vector<T>::iterator i = v1.begin(); i != v1.end(); ++i)
131 {
132 *i *= c;
133 ++j;
134 }
135
136 return v1;
137 }
138
139 template <class T>
140 std::vector<T>&
141 operator/=(std::vector<T>& v1, double c)
142 {
143 int j = 0;
144
145 for (typename std::vector<T>::iterator i = v1.begin(); i != v1.end(); ++i)
146 {
147 *i /= c;
148 ++j;
149 }
150
151 return v1;
152 }
153
154 template <class T>
155 std::vector<T>
156 operator*(double c, const std::vector<T>& v)
157 {
158 std::vector<T> res(v.size(), T());
159 int j = 0;
160
161 for (typename std::vector<T>::iterator i = res.begin(); i != res.end(); ++i)
162 {
163 *i = c * v[j];
164 ++j;
165 }
166
167 return res;
168 }
169
170 template <class T>
171 double
172 operator*(const std::vector<T>& v, const std::vector<T>& v2)
173 {
174 double res = 0;
175
176 if (v.size() != v2.size())
177 {
178 throw LocalException("vectors do not match in size: ")
179 << v.size() << " vs. " << v2.size();
180 }
181
182 int j = 0;
183
184 for (typename std::vector<T>::const_iterator i = v.begin(); i != v.end(); ++i)
185 {
186 res += *i * v2[j];
187 ++j;
188 }
189
190 return res;
191 }
192
193 template <class T>
194 std::vector<T>
195 operator+(const std::vector<T>& v, double s)
196 {
197 std::vector<T> res(v.size(), T());
198 int j = 0;
199
200 for (typename std::vector<T>::iterator i = res.begin(); i != res.end(); ++i)
201 {
202 *i = v[j] + s;
203 ++j;
204 }
205
206 return res;
207 }
208
209 template <class T>
210 std::vector<T>
211 operator-(const std::vector<T>& v, double s)
212 {
213 std::vector<T> res(v.size(), T());
214 int j = 0;
215
216 for (typename std::vector<T>::iterator i = res.begin(); i != res.end(); ++i)
217 {
218 *i = v[j] - s;
219 ++j;
220 }
221
222 return res;
223 }
224
225 template <class T>
226 double
227 vecLength(const std::vector<T>& v)
228 {
229 double result = 0.0;
230
231 for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); it++)
232 {
233 result += *it * *it;
234 }
235
236 return sqrt(result);
237 }
238
239 template <class T>
240 double
241 vecSum(const std::vector<T>& v)
242 {
243 double result = 0.0;
244
245 for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); it++)
246 {
247 result += *it;
248 }
249
250 return result;
251 }
252
253 template <class T>
254 std::vector<T>
255 normalizedVec(const std::vector<T>& v)
256 {
257 double length = vecLength(v);
258 std::vector<T> result = v;
259 result /= length;
260 return result;
261 }
262
263 template <class T>
264 std::vector<T>
265 operator/(const std::vector<T>& v, double c)
266 {
267 std::vector<T> res(v.size(), T());
268 int j = 0;
269
270 for (typename std::vector<T>::iterator i = res.begin(); i != res.end(); ++i)
271 {
272 *i = v[j] / c;
273 ++j;
274 }
275
276 return res;
277 }
278
279 template <class T>
280 std::vector<T>
281 abs(const std::vector<T>& v)
282 {
283 std::vector<T> res(v.size(), T());
284 int j = 0;
285
286 for (typename std::vector<T>::iterator i = res.begin(); i != res.end(); ++i)
287 {
288 *i = fabs(v[j]);
289 ++j;
290 }
291
292 return res;
293 }
294
295 template <class T>
296 std::vector<T>
297 max(const std::vector<T>& v1, const std::vector<T>& v2)
298 {
299 std::vector<T> res(std::min(v1.size(), v2.size()), T());
300 int j = 0;
301
302 for (typename std::vector<T>::iterator i = res.begin(); i != res.end(); ++i)
303 {
304 *i = std::max(v1[j], v2[j]);
305 ++j;
306 }
307
308 return res;
309 }
310
311 template <class T>
312 T
313 max(const std::vector<T>& v1)
314 {
315 T maxValue = std::numeric_limits<T>::min();
316
317 for (typename std::vector<T>::const_iterator i = v1.begin(); i != v1.end(); ++i)
318 {
319 maxValue = std::max(maxValue, *i);
320 }
321
322 return maxValue;
323 }
324
325 template <class T>
326 std::vector<T>
327 min(const std::vector<T>& v1, const std::vector<T>& v2)
328 {
329 std::vector<T> res(std::min(v1.size(), v2.size()));
330 int j = 0;
331
332 for (typename std::vector<T>::iterator i = res.begin(); i != res.end(); ++i)
333 {
334 *i = std::min(v1[j], v2[j]);
335 ++j;
336 }
337
338 return res;
339 }
340
341 template <class T>
342 T
343 min(const std::vector<T>& v1)
344 {
345 T minValue = std::numeric_limits<T>::max();
346
347 for (typename std::vector<T>::const_iterator i = v1.begin(); i != v1.end(); ++i)
348 {
349 minValue = std::min(minValue, *i);
350 }
351
352 return minValue;
353 }
354} // namespace armarx
constexpr T c
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::vector< T > & operator-=(std::vector< T > &v1, const std::vector< T > &v2)
std::vector< T > & operator/=(std::vector< T > &v1, double c)
std::vector< T > operator*(double c, const std::vector< T > &v)
std::vector< T > operator/(const std::vector< T > &v, double c)
void checkValue(const T &value)
std::vector< T > & operator*=(std::vector< T > &v1, double c)
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
double vecLength(const std::vector< T > &v)
std::vector< T > abs(const std::vector< T > &v)
double vecSum(const std::vector< T > &v)
void checkValues(const std::vector< T > &values)
std::vector< T > & operator+=(std::vector< T > &v1, const std::vector< T > &v2)
std::vector< T > operator-(const std::vector< T > &v1, const std::vector< T > &v2)
std::vector< T > operator+(const std::vector< T > &v1, const std::vector< T > &v2)
std::vector< T > min(const std::vector< T > &v1, const std::vector< T > &v2)
std::vector< T > normalizedVec(const std::vector< T > &v)