33 #ifndef DART_MATH_HELPERS_HPP_
34 #define DART_MATH_HELPERS_HPP_
47 #include <Eigen/Dense>
72 const Eigen::Matrix2d
CR((Eigen::Matrix2d() << 0.0, -1.0, 1.0, 0.0).finished());
74 inline int delta(
int _i,
int _j) {
80 template <
typename T>
inline constexpr
81 int sign(T x, std::false_type)
83 return static_cast<T
>(0) < x;
86 template <
typename T>
inline constexpr
87 int sign(T x, std::true_type)
89 return (
static_cast<T
>(0) < x) - (x < static_cast<T>(0));
92 template <
typename T>
inline constexpr
95 return sign(x, std::is_signed<T>());
98 inline double sqr(
double _x) {
102 inline double Tsinc(
double _theta) {
103 return 0.5-sqrt(_theta)/48;
107 return (std::abs(_theta) < 1e-6);
111 return log(_X + sqrt(_X * _X + 1));
115 return log(_X + sqrt(_X * _X - 1));
119 return log((1 + _X)/(1 - _X))/ 2;
123 return log((sqrt(-_X * _X + 1) + 1) / _X);
127 return log((
sign(_X) * sqrt(_X * _X + 1) +1) / _X);
131 return log((_X + 1) / (_X - 1)) / 2;
135 return floor(_x + 0.5);
139 int gintx =
static_cast<int>(std::floor(_x));
140 if (_x - gintx < 0.5)
141 return static_cast<double>(gintx);
143 return static_cast<double>(gintx + 1.0);
146 template <
typename T>
147 inline T
clip(
const T& val,
const T& lower,
const T& upper)
149 return std::max(lower, std::min(val, upper));
152 template <
typename DerivedA,
typename DerivedB>
153 inline typename DerivedA::PlainObject
clip(
154 const Eigen::MatrixBase<DerivedA>& val,
155 const Eigen::MatrixBase<DerivedB>& lower,
156 const Eigen::MatrixBase<DerivedB>& upper)
158 return lower.cwiseMax(val.cwiseMin(upper));
162 return (std::abs(_x - _y) < 1e-6);
175 return _isnan(_v) != 0;
177 return std::isnan(_v);
182 inline bool isNan(
const Eigen::MatrixXd& _m) {
183 for (
int i = 0; i < _m.rows(); ++i)
184 for (
int j = 0; j < _m.cols(); ++j)
197 return std::isinf(_v);
203 inline bool isInf(
const Eigen::MatrixXd& _m) {
204 for (
int i = 0; i < _m.rows(); ++i)
205 for (
int j = 0; j < _m.cols(); ++j)
213 inline bool isSymmetric(
const Eigen::MatrixXd& _m,
double _tol = 1e-6) {
214 std::size_t rows = _m.rows();
215 std::size_t cols = _m.cols();
220 for (std::size_t i = 0; i < rows; ++i) {
221 for (std::size_t j = i + 1; j < cols; ++j) {
222 if (std::abs(_m(i, j) - _m(j, i)) > _tol) {
223 std::cout <<
"A: " << std::endl;
224 for (std::size_t k = 0; k < rows; ++k) {
225 for (std::size_t l = 0; l < cols; ++l)
226 std::cout << std::setprecision(4) << _m(k, l) <<
" ";
227 std::cout << std::endl;
230 std::cout <<
"A(" << i <<
", " << j <<
"): " << _m(i, j) << std::endl;
231 std::cout <<
"A(" << j <<
", " << i <<
"): " << _m(i, j) << std::endl;
241 time_t now = time(0);
242 unsigned char* p =
reinterpret_cast<unsigned char*
>(&now);
246 for (i = 0; i <
sizeof(now); i++)
247 seed = seed * (UCHAR_MAX + 2U) + p[i];
255 inline
double random(
double _min,
double _max) {
256 return _min + ((
static_cast<double>(rand()) / (RAND_MAX + 1.0))
265 Eigen::Matrix<double, N, 1> v;
266 DART_SUPPRESS_DEPRECATED_BEGIN
267 for(std::size_t i=0; i<N; ++i)
268 v[i] =
random(_min, _max);
269 DART_SUPPRESS_DEPRECATED_END
279 DART_SUPPRESS_DEPRECATED_BEGIN
280 return randomVector<N>(-std::abs(_limit), std::abs(_limit));
281 DART_SUPPRESS_DEPRECATED_END
289 Eigen::VectorXd v = Eigen::VectorXd::Zero(size);
291 DART_SUPPRESS_DEPRECATED_BEGIN
292 for (std::size_t i = 0; i < size; ++i)
294 DART_SUPPRESS_DEPRECATED_END
304 DART_SUPPRESS_DEPRECATED_BEGIN
306 DART_SUPPRESS_DEPRECATED_END
312 constexpr
double operator"" _pi(
long double x)
318 constexpr
double operator"" _pi(
unsigned long long int x)
320 return operator"" _pi(
static_cast<long double>(x));
324 constexpr
double operator"" _rad(
long double angle)
330 constexpr
double operator"" _rad(
unsigned long long int angle)
332 return operator"" _rad(
static_cast<long double>(angle));
336 constexpr
double operator"" _deg(
long double angle)
342 constexpr
double operator"" _deg(
unsigned long long int angle)
344 return operator"" _deg(
static_cast<long double>(angle));
354 inline Eigen::Vector4d
Red(
double alpha)
356 return Eigen::Vector4d(0.9, 0.1, 0.1, alpha);
359 inline Eigen::Vector3d
Red()
361 return Eigen::Vector3d(0.9, 0.1, 0.1);
366 return Eigen::Vector3d(1.0, 0.0, 0.5);
371 return Eigen::Vector4d(1.0, 0.0, 0.5, alpha);
374 inline Eigen::Vector4d
Orange(
double alpha)
376 return Eigen::Vector4d(1.0, 0.63, 0.0, alpha);
381 return Eigen::Vector3d(1.0, 0.63, 0.0);
384 inline Eigen::Vector4d
Green(
double alpha)
386 return Eigen::Vector4d(0.1, 0.9, 0.1, alpha);
391 return Eigen::Vector3d(0.1, 0.9, 0.1);
394 inline Eigen::Vector4d
Blue(
double alpha)
396 return Eigen::Vector4d(0.1, 0.1, 0.9, alpha);
401 return Eigen::Vector3d(0.1, 0.1, 0.9);
404 inline Eigen::Vector4d
White(
double alpha)
406 return Eigen::Vector4d(1.0, 1.0, 1.0, alpha);
411 return Eigen::Vector3d(1.0, 1.0, 1.0);
414 inline Eigen::Vector4d
Black(
double alpha)
416 return Eigen::Vector4d(0.05, 0.05, 0.05, alpha);
421 return Eigen::Vector3d(0.05, 0.05, 0.05);
426 return Eigen::Vector4d(0.9, 0.9, 0.9, alpha);
431 return Eigen::Vector3d(0.9, 0.9, 0.9);
434 inline Eigen::Vector4d
Gray(
double alpha)
436 return Eigen::Vector4d(0.6, 0.6, 0.6, alpha);
441 return Eigen::Vector3d(0.6, 0.6, 0.6);
444 inline Eigen::Vector4d
Random(
double alpha)
#define DART_DEPRECATED(version)
Definition: Deprecated.hpp:51
static S uniform(S min, S max)
Returns a random number from an uniform distribution.
Definition: Random-impl.hpp:412
Definition: Random-impl.hpp:92
Eigen::Vector4d Gray(double alpha)
Definition: Helpers.hpp:434
Eigen::Vector4d LightGray(double alpha)
Definition: Helpers.hpp:424
Eigen::Vector4d Green(double alpha)
Definition: Helpers.hpp:384
Eigen::Vector4d Red(double alpha)
Definition: Helpers.hpp:354
Eigen::Vector4d Blue(double alpha)
Definition: Helpers.hpp:394
Eigen::Vector3d Fuchsia()
Definition: Helpers.hpp:364
Eigen::Vector4d White(double alpha)
Definition: Helpers.hpp:404
Eigen::Vector4d Orange(double alpha)
Definition: Helpers.hpp:374
Eigen::Vector4d Random(double alpha)
Definition: Helpers.hpp:444
Eigen::Vector4d Black(double alpha)
Definition: Helpers.hpp:414
bool isZero(double _theta)
Definition: Helpers.hpp:106
unsigned seedRand()
Definition: Helpers.hpp:240
T clip(const T &val, const T &lower, const T &upper)
Definition: Helpers.hpp:147
constexpr T toDegree(const T &radian)
Definition: Helpers.hpp:65
bool isEqual(double _x, double _y)
Definition: Helpers.hpp:161
double acosech(double _X)
Definition: Helpers.hpp:126
Eigen::Matrix< double, N, 1 > randomVector(double _min, double _max)
Definition: Helpers.hpp:263
double acosh(double _X)
Definition: Helpers.hpp:114
double atanh(double _X)
Definition: Helpers.hpp:118
double round2(double _x)
Definition: Helpers.hpp:138
constexpr int sign(T x, std::false_type)
Definition: Helpers.hpp:81
Eigen::VectorXd randomVectorXd(std::size_t size, double min, double max)
Definition: Helpers.hpp:287
double Tsinc(double _theta)
Definition: Helpers.hpp:102
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 °ree)
Definition: Helpers.hpp:58
double round(double _x)
Definition: Helpers.hpp:134
double random(double _min, double _max)
Definition: Helpers.hpp:255
double sqr(double _x)
Definition: Helpers.hpp:98
bool isInf(double _v)
Returns whether _v is an infinity value (either positive infinity or negative infinity).
Definition: Helpers.hpp:193
bool isSymmetric(const Eigen::MatrixXd &_m, double _tol=1e-6)
Returns whether _m is symmetric or not.
Definition: Helpers.hpp:213
double asech(double _X)
Definition: Helpers.hpp:122
double acotanh(double _X)
Definition: Helpers.hpp:130
double asinh(double _X)
Definition: Helpers.hpp:110
int delta(int _i, int _j)
Definition: Helpers.hpp:74
bool isInt(double _x)
Definition: Helpers.hpp:166
bool isNan(double _v)
Returns whether _v is a NaN (Not-A-Number) value.
Definition: Helpers.hpp:173
Definition: BulletCollisionDetector.cpp:63
Definition: SharedLibraryManager.hpp:43
static constexpr T pi()
Definition: Constants.hpp:44