Privacy
An open-source, flexible 3D physical simulation framework
mathUtils.h
Go to the documentation of this file.
1 /*
2  * Copyright 2012, DFKI GmbH Robotics Innovation Center
3  *
4  * This file is part of the MARS simulation framework.
5  *
6  * MARS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation, either version 3
9  * of the License, or (at your option) any later version.
10  *
11  * MARS 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 Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with MARS. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef MARS_UTILS_MATHUTILS_H
22 #define MARS_UTILS_MATHUTILS_H
23 
24 #include "Vector.h"
25 #include "Quaternion.h"
26 #include <configmaps/ConfigData.h>
27 
28 using namespace configmaps;
29 
30 namespace mars {
31  namespace utils {
32 
33  // approximation functions
34 
42  FUNCTION_BETA_DISTRIBUTION,
43  FUNCTION_GAMMA_DISTRIBUTION*/
45  };
46 
51  //FUNCTION_POLYNOM2D3
52  };
53 
54 
57  const double SQRT2PI = 2.5066282746310002;
58 
59  double pipe(double* x, std::vector<double>* c);
60  double polynom2(double* x, std::vector<double>* c);
61  double polynom3(double* x, std::vector<double>* c);
62  double polynom4(double* x, std::vector<double>* c);
63  double polynom5(double* x, std::vector<double>* c);
64  double gaussian(double* x, std::vector<double>* c);
65  double beta_distribution(double* x, std::vector<double>* c);
66  double gamma_distribution(double* x, std::vector<double>* c);
67  double polynom2D1(double* x, double* y, std::vector<double>* c);
68  double polynom2D2(double* x, double* y, std::vector<double>* c);
69 
70  const double EPSILON = Eigen::NumTraits<double>::epsilon();
71 
72  inline bool isNormalized(const Vector &vec)
73  { return (fabs(vec.squaredNorm() - 1.) < EPSILON*EPSILON); }
74 
75  inline Vector scaleVectorToLength(const Vector &vec, const double &length)
76  { return vec * length / vec.norm(); }
77 
84  double angleBetween(const Vector &v1, const Vector &v2, Vector *axis=NULL);
85 
86  double getYaw(const mars::utils::Quaternion &);
87 
88  inline Quaternion angleAxisToQuaternion(double angle, const Vector &axis) {
89  return Quaternion(Eigen::AngleAxis<double>(angle, axis));
90  }
91 
92  Quaternion eulerToQuaternion(const Vector &euler_v);
94  { return eulerToQuaternion(Vector(rot.alpha, rot.beta, rot.gamma)); }
95 
97 
98  Vector lerp(const Vector &from, const Vector &to, double t);
99  Vector slerp(const Vector &from, const Vector &to, double t);
100 
101  inline double distanceSquaredBetween(const Vector &v1, const Vector &v2)
102  { return (v2.array() - v1.array()).square().sum(); }
103  inline double distanceBetween(const Vector &v1, const Vector &v2)
104  { return sqrt(distanceSquaredBetween(v1, v2)); }
105 
106  void vectorToSpherical(const Vector &v, double *r, double *theta, double *phi);
107 
108  Vector getProjection(const Vector &v1, const Vector &v2);
109 
110  Vector vectorFromSpherical(double r, double theta, double phi);
111 
113  void vectorToConfigItem(ConfigItem *item, Vector *v);
114 
117 
118  template <typename T>
120  return Quaternion(q.w, q.x, q.y, q.z);
121  }
122 
123  template <typename T>
125  return Quaternion(q[3], q[0], q[1], q[2]);
126  }
127 
128  template <typename T>
130  return Quaternion(q[0], q[1], q[2], q[3]);
131  }
132 
133  double random_normal_number(double mean, double std, double min, double max);
134  double random_number(double min, double max, int digits);
135 
136  }; // end of namespace utils
137 }; // end of namespace mars
138 
139 #endif /* MARS_UTILS_MATHUTILS_H */
Vector slerp(const Vector &from, const Vector &to, double t)
Definition: mathUtils.cpp:195
Vector lerp(const Vector &from, const Vector &to, double t)
Definition: mathUtils.cpp:235
double angleBetween(const Vector &v1, const Vector &v2, Vector *axis)
Angle between two vectors.
Definition: mathUtils.cpp:107
Vector vectorFromSpherical(double r, double theta, double phi)
Definition: mathUtils.cpp:261
double random_number(double min, double max, int digits)
Definition: mathUtils.cpp:307
double distanceSquaredBetween(const Vector &v1, const Vector &v2)
Definition: mathUtils.h:101
Definition: tsort.c:44
ApproximationFunction getApproximationFunctionFromString(std::string s)
Definition: mathUtils.cpp:33
sRotation quaternionTosRotation(const Quaternion &value)
Definition: mathUtils.cpp:163
bool vectorFromConfigItem(ConfigItem *item, Vector *v)
Definition: mathUtils.cpp:267
u_int8_t r
Definition: CameraSensor.h:41
bool isNormalized(const Vector &vec)
Definition: mathUtils.h:72
bool quaternionFromConfigItem(ConfigItem *item, Quaternion *q)
Definition: mathUtils.cpp:280
void vectorToConfigItem(ConfigItem *item, Vector *v)
Definition: mathUtils.cpp:274
Base object of the configmaps library!
Definition: ConfigItem.hpp:58
void vectorToSpherical(const Vector &v, double *r, double *theta, double *phi)
Definition: mathUtils.cpp:242
Quaternion eulerToQuaternion(const sRotation &rot)
Definition: mathUtils.h:93
Quaternion quaternionFromMembers(T q)
Definition: mathUtils.h:119
double polynom3(double *x, std::vector< double > *c)
Definition: mathUtils.cpp:58
Quaternion quaternionFromXYZWArray(T q)
Definition: mathUtils.h:124
double gamma_distribution(double *x, std::vector< double > *c)
double polynom2(double *x, std::vector< double > *c)
Definition: mathUtils.cpp:54
Quaternion angleAxisToQuaternion(double angle, const Vector &axis)
Definition: mathUtils.h:88
ApproximationFunction2D getApproximationFunction2DFromString(std::string s)
Definition: mathUtils.cpp:43
ApproximationFunction2D
Definition: mathUtils.h:47
Vector getProjection(const Vector &v1, const Vector &v2)
Definition: mathUtils.cpp:253
void quaternionToConfigItem(ConfigItem *item, Quaternion *q)
Definition: mathUtils.cpp:288
double getYaw(const Quaternion &q)
Definition: mathUtils.cpp:102
Copyright 2012, DFKI GmbH Robotics Innovation Center.
Eigen::Matrix< double, 3, 1, Eigen::DontAlign > Vector
Definition: Vector.h:43
double distanceBetween(const Vector &v1, const Vector &v2)
Definition: mathUtils.h:103
ApproximationFunction
Definition: mathUtils.h:35
Eigen::Quaternion< double, Eigen::DontAlign > Quaternion
Definition: Quaternion.h:56
#define max(x, y)
Definition: envmap.cpp:33
double random_normal_number(double mean, double std, double low, double high)
Definition: mathUtils.cpp:313
double polynom2D1(double *x, double *y, std::vector< double > *c)
Definition: mathUtils.cpp:87
double beta_distribution(double *x, std::vector< double > *c)
const double EPSILON
Definition: mathUtils.h:70
double polynom5(double *x, std::vector< double > *c)
Definition: mathUtils.cpp:67
const double SQRT2PI
Definition: mathUtils.h:57
Vector scaleVectorToLength(const Vector &vec, const double &length)
Definition: mathUtils.h:75
double pipe(double *x, std::vector< double > *c)
Definition: mathUtils.cpp:50
double gaussian(double *x, std::vector< double > *c)
Definition: mathUtils.cpp:74
double polynom4(double *x, std::vector< double > *c)
Definition: mathUtils.cpp:62
double polynom2D2(double *x, double *y, std::vector< double > *c)
Definition: mathUtils.cpp:92
Quaternion quaternionFromWXYZArray(T q)
Definition: mathUtils.h:129