DART 6.6.2
Loading...
Searching...
No Matches
Helpers.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2018, The DART development contributors
3 * All rights reserved.
4 *
5 * The list of contributors can be found at:
6 * https://github.com/dartsim/dart/blob/master/LICENSE
7 *
8 * This file is provided under the following "BSD-style" License:
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
11 * conditions are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#ifndef DART_MATH_HELPERS_HPP_
34#define DART_MATH_HELPERS_HPP_
35
36// Standard Libraries
37#include <cfloat>
38#include <climits>
39#include <cmath>
40#include <cstdlib>
41#include <ctime>
42#include <iomanip>
43#include <iostream>
44
45// External Libraries
46#include <Eigen/Dense>
47// Local Headers
50
51namespace dart {
52namespace math {
53
54//==============================================================================
55template <typename T>
56constexpr T toRadian(const T& degree)
57{
58 return degree * constants<T>::pi() / 180.0;
59}
60
61//==============================================================================
62template <typename T>
63constexpr T toDegree(const T& radian)
64{
65 return radian * 180.0 / constants<T>::pi();
66}
67
70const Eigen::Matrix2d CR((Eigen::Matrix2d() << 0.0, -1.0, 1.0, 0.0).finished());
71
72inline int delta(int _i, int _j) {
73 if (_i == _j)
74 return 1;
75 return 0;
76}
77
78template <typename T> inline constexpr
79int sign(T x, std::false_type)
80{
81 return static_cast<T>(0) < x;
82}
83
84template <typename T> inline constexpr
85int sign(T x, std::true_type)
86{
87 return (static_cast<T>(0) < x) - (x < static_cast<T>(0));
88}
89
90template <typename T> inline constexpr
91int sign(T x)
92{
93 return sign(x, std::is_signed<T>());
94}
95
96inline double sqr(double _x) {
97 return _x*_x;
98}
99
100inline double Tsinc(double _theta) {
101 return 0.5-sqrt(_theta)/48;
102}
103
104inline bool isZero(double _theta) {
105 return (std::abs(_theta) < 1e-6);
106}
107
108inline double asinh(double _X) {
109 return log(_X + sqrt(_X * _X + 1));
110}
111
112inline double acosh(double _X) {
113 return log(_X + sqrt(_X * _X - 1));
114}
115
116inline double atanh(double _X) {
117 return log((1 + _X)/(1 - _X))/ 2;
118}
119
120inline double asech(double _X) {
121 return log((sqrt(-_X * _X + 1) + 1) / _X);
122}
123
124inline double acosech(double _X) {
125 return log((sign(_X) * sqrt(_X * _X + 1) +1) / _X);
126}
127
128inline double acotanh(double _X) {
129 return log((_X + 1) / (_X - 1)) / 2;
130}
131
132inline double round(double _x) {
133 return floor(_x + 0.5);
134}
135
136inline double round2(double _x) {
137 int gintx = static_cast<int>(std::floor(_x));
138 if (_x - gintx < 0.5)
139 return static_cast<double>(gintx);
140 else
141 return static_cast<double>(gintx + 1.0);
142}
143
144template <typename T>
145inline T clip(const T& val, const T& lower, const T& upper)
146{
147 return std::max(lower, std::min(val, upper));
148}
149
150template <typename DerivedA, typename DerivedB>
151inline typename DerivedA::PlainObject clip(
152 const Eigen::MatrixBase<DerivedA>& val,
153 const Eigen::MatrixBase<DerivedB>& lower,
154 const Eigen::MatrixBase<DerivedB>& upper)
155{
156 return lower.cwiseMax(val.cwiseMin(upper));
157}
158
159inline bool isEqual(double _x, double _y) {
160 return (std::abs(_x - _y) < 1e-6);
161}
162
163// check if it is an integer
164inline bool isInt(double _x) {
165 if (isEqual(round(_x), _x))
166 return true;
167 return false;
168}
169
171inline bool isNan(double _v) {
172#ifdef _WIN32
173 return _isnan(_v) != 0;
174#else
175 return std::isnan(_v);
176#endif
177}
178
180inline bool isNan(const Eigen::MatrixXd& _m) {
181 for (int i = 0; i < _m.rows(); ++i)
182 for (int j = 0; j < _m.cols(); ++j)
183 if (isNan(_m(i, j)))
184 return true;
185
186 return false;
187}
188
191inline bool isInf(double _v) {
192#ifdef _WIN32
193 return !_finite(_v);
194#else
195 return std::isinf(_v);
196#endif
197}
198
201inline bool isInf(const Eigen::MatrixXd& _m) {
202 for (int i = 0; i < _m.rows(); ++i)
203 for (int j = 0; j < _m.cols(); ++j)
204 if (isInf(_m(i, j)))
205 return true;
206
207 return false;
208}
209
211inline bool isSymmetric(const Eigen::MatrixXd& _m, double _tol = 1e-6) {
212 std::size_t rows = _m.rows();
213 std::size_t cols = _m.cols();
214
215 if (rows != cols)
216 return false;
217
218 for (std::size_t i = 0; i < rows; ++i) {
219 for (std::size_t j = i + 1; j < cols; ++j) {
220 if (std::abs(_m(i, j) - _m(j, i)) > _tol) {
221 std::cout << "A: " << std::endl;
222 for (std::size_t k = 0; k < rows; ++k) {
223 for (std::size_t l = 0; l < cols; ++l)
224 std::cout << std::setprecision(4) << _m(k, l) << " ";
225 std::cout << std::endl;
226 }
227
228 std::cout << "A(" << i << ", " << j << "): " << _m(i, j) << std::endl;
229 std::cout << "A(" << j << ", " << i << "): " << _m(i, j) << std::endl;
230 return false;
231 }
232 }
233 }
234
235 return true;
236}
237
238inline unsigned seedRand() {
239 time_t now = time(0);
240 unsigned char* p = reinterpret_cast<unsigned char*>(&now);
241 unsigned seed = 0;
242 std::size_t i;
243
244 for (i = 0; i < sizeof(now); i++)
245 seed = seed * (UCHAR_MAX + 2U) + p[i];
246
247 srand(seed);
248 return seed;
249}
250
251inline double random(double _min, double _max) {
252 return _min + ((static_cast<double>(rand()) / (RAND_MAX + 1.0))
253 * (_max - _min));
254}
255
256template<int N>
257Eigen::Matrix<double, N, 1> randomVector(double _min, double _max)
258{
259 Eigen::Matrix<double, N, 1> v;
260 for(std::size_t i=0; i<N; ++i)
261 v[i] = random(_min, _max);
262
263 return v;
264}
265
266template<int N>
267Eigen::Matrix<double, N, 1> randomVector(double _limit)
268{
269 return randomVector<N>(-std::abs(_limit), std::abs(_limit));
270}
271
272//==============================================================================
273inline Eigen::VectorXd randomVectorXd(std::size_t size, double min, double max)
274{
275 Eigen::VectorXd v = Eigen::VectorXd::Zero(size);
276
277 for (std::size_t i = 0; i < size; ++i)
278 v[i] = random(min, max);
279
280 return v;
281}
282
283//==============================================================================
284inline Eigen::VectorXd randomVectorXd(std::size_t size, double limit)
285{
286 return randomVectorXd(size, -std::abs(limit), std::abs(limit));
287}
288
289namespace suffixes {
290
291//==============================================================================
292constexpr double operator"" _pi(long double x)
293{
294 return x * constants<double>::pi();
295}
296
297//==============================================================================
298constexpr double operator"" _pi(unsigned long long int x)
299{
300 return operator"" _pi(static_cast<long double>(x));
301}
302
303//==============================================================================
304constexpr double operator"" _rad(long double angle)
305{
306 return angle;
307}
308
309//==============================================================================
310constexpr double operator"" _rad(unsigned long long int angle)
311{
312 return operator"" _rad(static_cast<long double>(angle));
313}
314
315//==============================================================================
316constexpr double operator"" _deg(long double angle)
317{
318 return toRadian(angle);
319}
320
321//==============================================================================
322constexpr double operator"" _deg(unsigned long long int angle)
323{
324 return operator"" _deg(static_cast<long double>(angle));
325}
326
327} // namespace suffixes
328
329} // namespace math
330
331namespace Color
332{
333
334inline Eigen::Vector4d Red(double alpha)
335{
336 return Eigen::Vector4d(0.9, 0.1, 0.1, alpha);
337}
338
339inline Eigen::Vector3d Red()
340{
341 return Eigen::Vector3d(0.9, 0.1, 0.1);
342}
343
344inline Eigen::Vector3d Fuchsia()
345{
346 return Eigen::Vector3d(1.0, 0.0, 0.5);
347}
348
349inline Eigen::Vector4d Fuchsia(double alpha)
350{
351 return Eigen::Vector4d(1.0, 0.0, 0.5, alpha);
352}
353
354inline Eigen::Vector4d Orange(double alpha)
355{
356 return Eigen::Vector4d(1.0, 0.63, 0.0, alpha);
357}
358
359inline Eigen::Vector3d Orange()
360{
361 return Eigen::Vector3d(1.0, 0.63, 0.0);
362}
363
364inline Eigen::Vector4d Green(double alpha)
365{
366 return Eigen::Vector4d(0.1, 0.9, 0.1, alpha);
367}
368
369inline Eigen::Vector3d Green()
370{
371 return Eigen::Vector3d(0.1, 0.9, 0.1);
372}
373
374inline Eigen::Vector4d Blue(double alpha)
375{
376 return Eigen::Vector4d(0.1, 0.1, 0.9, alpha);
377}
378
379inline Eigen::Vector3d Blue()
380{
381 return Eigen::Vector3d(0.1, 0.1, 0.9);
382}
383
384inline Eigen::Vector4d White(double alpha)
385{
386 return Eigen::Vector4d(1.0, 1.0, 1.0, alpha);
387}
388
389inline Eigen::Vector3d White()
390{
391 return Eigen::Vector3d(1.0, 1.0, 1.0);
392}
393
394inline Eigen::Vector4d Black(double alpha)
395{
396 return Eigen::Vector4d(0.05, 0.05, 0.05, alpha);
397}
398
399inline Eigen::Vector3d Black()
400{
401 return Eigen::Vector3d(0.05, 0.05, 0.05);
402}
403
404inline Eigen::Vector4d Gray(double alpha)
405{
406 return Eigen::Vector4d(0.6, 0.6, 0.6, alpha);
407}
408
409inline Eigen::Vector3d Gray()
410{
411 return Eigen::Vector3d(0.6, 0.6, 0.6);
412}
413
414inline Eigen::Vector4d Random(double alpha)
415{
416 return Eigen::Vector4d(math::random(0.0, 1.0),
417 math::random(0.0, 1.0),
418 math::random(0.0, 1.0),
419 alpha);
420}
421
422inline Eigen::Vector3d Random()
423{
424 return Eigen::Vector3d(math::random(0.0, 1.0),
425 math::random(0.0, 1.0),
426 math::random(0.0, 1.0));
427}
428
429} // namespace Color
430
431} // namespace dart
432
433#endif // DART_MATH_HELPERS_HPP_
Eigen::Vector3d Red()
Definition Helpers.hpp:339
Eigen::Vector3d Blue()
Definition Helpers.hpp:379
Eigen::Vector3d White()
Definition Helpers.hpp:389
Eigen::Vector3d Fuchsia()
Definition Helpers.hpp:344
Eigen::Vector3d Gray()
Definition Helpers.hpp:409
Eigen::Vector3d Orange()
Definition Helpers.hpp:359
Eigen::Vector3d Black()
Definition Helpers.hpp:399
Eigen::Vector3d Green()
Definition Helpers.hpp:369
Eigen::Vector3d Random()
Definition Helpers.hpp:422
bool isZero(double _theta)
Definition Helpers.hpp:104
unsigned seedRand()
Definition Helpers.hpp:238
Eigen::Matrix< double, N, 1 > randomVector(double _min, double _max)
Definition Helpers.hpp:257
T clip(const T &val, const T &lower, const T &upper)
Definition Helpers.hpp:145
constexpr T toDegree(const T &radian)
Definition Helpers.hpp:63
bool isEqual(double _x, double _y)
Definition Helpers.hpp:159
double acosech(double _X)
Definition Helpers.hpp:124
double acosh(double _X)
Definition Helpers.hpp:112
double atanh(double _X)
Definition Helpers.hpp:116
double round2(double _x)
Definition Helpers.hpp:136
constexpr int sign(T x, std::false_type)
Definition Helpers.hpp:79
Eigen::VectorXd randomVectorXd(std::size_t size, double min, double max)
Definition Helpers.hpp:273
double Tsinc(double _theta)
Definition Helpers.hpp:100
const Eigen::Matrix2d CR((Eigen::Matrix2d()<< 0.0, -1.0, 1.0, 0.0).finished())
a cross b = (CR*a) dot b const Matd CR(2,2,0.0,-1.0,1.0,0.0);
constexpr T toRadian(const T &degree)
Definition Helpers.hpp:56
double round(double _x)
Definition Helpers.hpp:132
double random(double _min, double _max)
Definition Helpers.hpp:251
double sqr(double _x)
Definition Helpers.hpp:96
bool isInf(double _v)
Returns whether _v is an infinity value (either positive infinity or negative infinity).
Definition Helpers.hpp:191
bool isSymmetric(const Eigen::MatrixXd &_m, double _tol=1e-6)
Returns whether _m is symmetric or not.
Definition Helpers.hpp:211
double asech(double _X)
Definition Helpers.hpp:120
double acotanh(double _X)
Definition Helpers.hpp:128
double asinh(double _X)
Definition Helpers.hpp:108
int delta(int _i, int _j)
Definition Helpers.hpp:72
bool isInt(double _x)
Definition Helpers.hpp:164
bool isNan(double _v)
Returns whether _v is a NaN (Not-A-Number) value.
Definition Helpers.hpp:171
Definition BulletCollisionDetector.cpp:63
static constexpr T pi()
Definition Constants.hpp:44