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 
27 
28 #include <Eigen/Geometry>
29 #include <cmath>
30 #include <map>
31 #include <vector>
32 #include <limits>
33 
34 namespace armarx
35 {
36  template<class T>
37  inline void checkValue(const T& value)
38  {
39  if (std::numeric_limits<T>::infinity() == value)
40  {
41  throw LocalException("result value is inf");
42  }
43 
44  if (value != value)
45  {
46  throw LocalException("result value is nan");
47  }
48  }
49 
50  template<class T>
51  inline void checkValues(const std::vector<T >& values)
52  {
53  for (size_t i = 0; i < values.size(); ++i)
54  {
55  checkValue(values[i]);
56  }
57  }
58 
59 
60 
61  template <class T> std::vector<T> operator -(const std::vector<T>& v1, const std::vector<T>& v2)
62  {
63  std::vector<T> res(std::min(v1.size(), v2.size()), T());
64  int j = 0;
65 
66  for (typename std::vector<T>::iterator i = res.begin(); i != res.end(); ++i)
67  {
68  *i = v1[j] - v2[j];
69  ++j;
70  }
71 
72  return res;
73  }
74 
75  template <class T> std::vector<T> operator +(const std::vector<T>& v1, const std::vector<T>& v2)
76  {
77  std::vector<T> res(std::min(v1.size(), v2.size()), T());
78  int j = 0;
79 
80  for (typename std::vector<T>::iterator i = res.begin(); i != res.end(); ++i)
81  {
82  *i = v1[j] + v2[j];
83  ++j;
84  }
85 
86  return res;
87  }
88 
89  template <class T> std::vector<T>& operator +=(std::vector<T>& v1, const std::vector<T>& v2)
90  {
91  int j = 0;
92 
93  for (typename std::vector<T>::iterator i = v1.begin(); i != v1.end(); ++i)
94  {
95  *i += v2[j];
96  ++j;
97  }
98 
99  return v1;
100  }
101 
102 
103  template <class T> std::vector<T>& operator -=(std::vector<T>& v1, const std::vector<T>& v2)
104  {
105  int j = 0;
106 
107  for (typename std::vector<T>::iterator i = v1.begin(); i != v1.end(); ++i)
108  {
109  *i -= v2[j];
110  ++j;
111  }
112 
113  return v1;
114  }
115 
116  template <class T> std::vector<T>& operator *=(std::vector<T>& v1, double c)
117  {
118  int j = 0;
119 
120  for (typename std::vector<T>::iterator i = v1.begin(); i != v1.end(); ++i)
121  {
122  *i *= c;
123  ++j;
124  }
125 
126  return v1;
127  }
128 
129 
130 
131  template <class T> std::vector<T>& operator /=(std::vector<T>& v1, double c)
132  {
133  int j = 0;
134 
135  for (typename std::vector<T>::iterator i = v1.begin(); i != v1.end(); ++i)
136  {
137  *i /= c;
138  ++j;
139  }
140 
141  return v1;
142  }
143 
144  template <class T> std::vector<T> operator *(double c, const std::vector<T>& v)
145  {
146  std::vector<T> res(v.size(), T());
147  int j = 0;
148 
149  for (typename std::vector<T>::iterator i = res.begin(); i != res.end(); ++i)
150  {
151  *i = c * v[j];
152  ++j;
153  }
154 
155  return res;
156  }
157 
158  template <class T> double operator *(const std::vector<T>& v, const std::vector<T>& v2)
159  {
160  double res = 0;
161 
162  if (v.size() != v2.size())
163  {
164  throw LocalException("vectors do not match in size: ") << v.size() << " vs. " << v2.size();
165  }
166 
167  int j = 0;
168 
169  for (typename std::vector<T>::const_iterator i = v.begin(); i != v.end(); ++i)
170  {
171  res += *i * v2[j];
172  ++j;
173  }
174 
175  return res;
176  }
177 
178 
179  template <class T> std::vector<T> operator +(const std::vector<T>& v, double s)
180  {
181  std::vector<T> res(v.size(), T());
182  int j = 0;
183 
184  for (typename std::vector<T>::iterator i = res.begin(); i != res.end(); ++i)
185  {
186  *i = v[j] + s;
187  ++j;
188  }
189 
190  return res;
191  }
192 
193  template <class T> std::vector<T> operator -(const std::vector<T>& v, double s)
194  {
195  std::vector<T> res(v.size(), T());
196  int j = 0;
197 
198  for (typename std::vector<T>::iterator i = res.begin(); i != res.end(); ++i)
199  {
200  *i = v[j] - s;
201  ++j;
202  }
203 
204  return res;
205  }
206 
207  template <class T> double vecLength(const std::vector<T>& v)
208  {
209  double result = 0.0;
210 
211  for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); it++)
212  {
213  result += *it** it;
214  }
215 
216  return sqrt(result);
217  }
218 
219  template <class T> double vecSum(const std::vector<T>& v)
220  {
221  double result = 0.0;
222 
223  for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); it++)
224  {
225  result += *it;
226  }
227 
228  return result;
229  }
230 
231  template <class T> std::vector<T> normalizedVec(const std::vector<T>& v)
232  {
233  double length = vecLength(v);
234  std::vector<T> result = v;
235  result /= length;
236  return result;
237  }
238 
239  template <class T> std::vector<T> operator /(const std::vector<T>& v, double c)
240  {
241  std::vector<T> res(v.size(), T());
242  int j = 0;
243 
244  for (typename std::vector<T>::iterator i = res.begin(); i != res.end(); ++i)
245  {
246  *i = v[j] / c;
247  ++j;
248  }
249 
250  return res;
251  }
252 
253  template <class T> std::vector<T> abs(const std::vector<T>& v)
254  {
255  std::vector<T> res(v.size(), T());
256  int j = 0;
257 
258  for (typename std::vector<T>::iterator i = res.begin(); i != res.end(); ++i)
259  {
260  *i = fabs(v[j]);
261  ++j;
262  }
263 
264  return res;
265  }
266 
267  template <class T> std::vector<T> max(const std::vector<T>& v1, const std::vector<T>& v2)
268  {
269  std::vector<T> res(std::min(v1.size(), v2.size()), T());
270  int j = 0;
271 
272  for (typename std::vector<T>::iterator i = res.begin(); i != res.end(); ++i)
273  {
274  *i = std::max(v1[j], v2[j]);
275  ++j;
276  }
277 
278  return res;
279  }
280 
281  template <class T> T max(const std::vector<T>& v1)
282  {
283  T maxValue = std::numeric_limits<T>::min();
284 
285  for (typename std::vector<T>::const_iterator i = v1.begin(); i != v1.end(); ++i)
286  {
287  maxValue = std::max(maxValue, *i);
288 
289  }
290 
291  return maxValue;
292  }
293 
294  template <class T> std::vector<T> min(const std::vector<T>& v1, const std::vector<T>& v2)
295  {
296  std::vector<T> res(std::min(v1.size(), v2.size()));
297  int j = 0;
298 
299  for (typename std::vector<T>::iterator i = res.begin(); i != res.end(); ++i)
300  {
301  *i = std::min(v1[j], v2[j]);
302  ++j;
303  }
304 
305  return res;
306  }
307 
308  template <class T> T min(const std::vector<T>& v1)
309  {
310  T minValue = std::numeric_limits<T>::max();
311 
312  for (typename std::vector<T>::const_iterator i = v1.begin(); i != v1.end(); ++i)
313  {
314  minValue = std::min(minValue, *i);
315 
316  }
317 
318  return minValue;
319  }
320 }
GfxTL::sqrt
VectorXD< D, T > sqrt(const VectorXD< D, T > &a)
Definition: VectorXD.h:662
armarx::normalizedVec
std::vector< T > normalizedVec(const std::vector< T > &v)
Definition: VectorHelpers.h:231
armarx::operator-=
std::vector< T > & operator-=(std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:103
armarx::operator/=
std::vector< T > & operator/=(std::vector< T > &v1, double c)
Definition: VectorHelpers.h:131
armarx::checkValues
void checkValues(const std::vector< T > &values)
Definition: VectorHelpers.h:51
armarx::vecLength
double vecLength(const std::vector< T > &v)
Definition: VectorHelpers.h:207
armarx::max
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:267
armarx::operator*=
std::vector< T > & operator*=(std::vector< T > &v1, double c)
Definition: VectorHelpers.h:116
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::operator-
std::vector< T > operator-(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:61
armarx::operator*
std::vector< T > operator*(double c, const std::vector< T > &v)
Definition: VectorHelpers.h:144
armarx::operator+=
std::vector< T > & operator+=(std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:89
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::abs
std::vector< T > abs(const std::vector< T > &v)
Definition: VectorHelpers.h:253
armarx::checkValue
void checkValue(const T &value)
Definition: VectorHelpers.h:37
armarx::operator/
std::vector< T > operator/(const std::vector< T > &v, double c)
Definition: VectorHelpers.h:239
max
T max(T t1, T t2)
Definition: gdiam.h:48
armarx::operator+
std::vector< T > operator+(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:75
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::min
std::vector< T > min(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:294
armarx::vecSum
double vecSum(const std::vector< T > &v)
Definition: VectorHelpers.h:219
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
min
T min(T t1, T t2)
Definition: gdiam.h:42
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
Exception.h