se2ez
math.cpp
Go to the documentation of this file.
1 /* Author: Zachary Kingston */
2 
3 #include <iostream>
4 #include <iomanip>
5 
6 #include <se2ez/core/math.h>
7 #include <se2ez/core/log.h>
8 
9 using namespace se2ez;
10 
11 Eigen::Isometry2d tf::toIsometry(double x, double y, double theta)
12 {
13  Eigen::Isometry2d iso;
14  iso.translation() = Eigen::Vector2d(x, y);
15  iso.linear() = Eigen::Rotation2D<double>(theta).toRotationMatrix();
16 
17  return iso;
18 }
19 
20 Eigen::Isometry2d tf::toIsometry(const Eigen::Vector3d &vec)
21 {
22  return toIsometry(vec[0], vec[1], vec[2]);
23 }
24 
25 double tf::getRotation(const Eigen::Isometry2d &frame)
26 {
27  return Eigen::Rotation2D<double>(frame.linear()).angle();
28 }
29 
30 Eigen::Vector3d tf::flattenIsometry(const Eigen::Isometry2d &frame)
31 {
32  const auto &t = frame.translation();
33  return {t[0], t[1], getRotation(frame)};
34 }
35 
36 double tf::transformDistance(const Eigen::Isometry2d &t1, const Eigen::Isometry2d &t2, double alpha)
37 {
38  if (alpha > 1 || alpha < 0)
39  {
40  SE2EZ_WARN("Parameter alpha must be in [0,1] defaulting to 0.75");
41  alpha = 0.75;
42  }
43 
44  return alpha * (t1.translation() - t2.translation()).norm() //
45  + (1 - alpha) * math::angleDistance(getRotation(t1), getRotation(t2));
46 }
47 
48 KDL::Frame tf::EigenToKDLFrame(const Eigen::Isometry2d &iso)
49 
50 {
51  const auto &v = flattenIsometry(iso);
52  return KDL::Frame(KDL::Rotation::RPY(0., 0., v[2]), KDL::Vector(v[0], v[1], 0));
53 }
54 
55 Eigen::Isometry2d tf::KDLToEigenFrame(const KDL::Frame &frame)
56 {
57  Eigen::Isometry2d iso;
58  auto pos = frame.p;
59  auto rot = frame.M;
60 
61  double r, p, y;
62  rot.GetRPY(r, p, y);
63  return toIsometry(pos.x(), pos.y(), y);
64 }
65 
66 KDL::JntArray tf::EigenToKDLVec(const Eigen::VectorXd &vec)
67 {
68  KDL::JntArray jnt(vec.size());
69  for (unsigned int i = 0; i < vec.size(); ++i)
70  jnt(i) = vec[i];
71  return jnt;
72 }
73 
74 Eigen::VectorXd tf::KDLToEigenVec(const KDL::JntArray &frame)
75 {
76  return frame.data;
77 }
78 
79 std::string tf::printFrame(const Eigen::Isometry2d &frame)
80 {
81  const auto &v = flattenIsometry(frame);
82  return printVector(v);
83 }
84 
85 std::string tf::printFrame(const KDL::Frame &frame)
86 {
87  return printFrame(KDLToEigenFrame(frame));
88 }
89 
90 std::string tf::printVector(const Eigen::VectorXd &v, unsigned int precision)
91 {
93  ss.setf(std::ios::fixed, std::ios::floatfield);
94 
95  ss.precision(precision);
96 
97  ss << "[ ";
98  for (unsigned int i = 0; i < v.size(); ++i)
99  {
100  ss << std::setw(precision + 4) << v[i];
101  if (i < v.size() - 1)
102  ss << ", ";
103  }
104  ss << " ]";
105 
106  return ss.str();
107 }
108 
109 std::string tf::printMatrix(const Eigen::MatrixXd &v, unsigned int precision)
110 {
112  ss.setf(std::ios::fixed, std::ios::floatfield);
113  ss.precision(precision);
114 
115  for (unsigned int j = 0; j < v.rows(); ++j)
116  {
117  ss << ((j) ? " " : "[ ");
118  for (unsigned int i = 0; i < v.cols(); ++i)
119  {
120  ss << std::setw(precision + 4) << v(j, i);
121  if (i < v.cols() - 1)
122  ss << ", ";
123  }
124 
125  if (j < v.rows() - 1)
126  ss << std::endl;
127  else
128  ss << " ]";
129  }
130 
131  return ss.str();
132 }
133 
134 Eigen::VectorXd math::toVector(double x)
135 {
136  return Eigen::VectorXd::Constant(1, x);
137 }
138 
140 {
141  Eigen::VectorXd r(x.size());
142 
143  auto it = x.begin();
144  for (unsigned int i = 0; i < x.size(); ++i)
145  r[i] = *it++;
146 
147  return r;
148 }
149 
150 Eigen::MatrixXd math::toMatrix(const std::vector<Eigen::VectorXd> &vlist)
151 {
152  Eigen::MatrixXd M(vlist.size(), vlist[0].size());
153 
154  for (unsigned int i = 0; i < M.rows(); i++)
155  M.row(i) = vlist[i];
156 
157  return M;
158 }
159 
160 double math::remap(double a1, double a2, double av, double b1, double b2)
161 {
162  double dat = fabs(a2 - a1);
163  double dav = fabs(av - a1);
164 
165  double dbt = fabs(b2 - b1);
166  double dbv = dbt * (dav / dat);
167 
168  double bv = b1 + dbv;
169 
170  // SE2EZ_DEBUG("%1% - %2% - %3% (%6%/%7%) -> %4% - %10% - %5% (%8%/%9%)", //
171  // a1, av, a2, //
172  // b1, b2, //
173  // dav, dat, //
174  // dbv, dbt, bv);
175 
176  return bv;
177 }
178 
179 double math::clamp(double v, double a, double b)
180 {
181  return (v > b) ? b : ((v < a) ? a : v);
182 }
183 
184 double math::angleDistance(double v1, double v2)
185 {
186  return angleNormalize(std::fabs(v1 - v2));
187 }
188 
189 double math::angleNormalize(double v)
190 {
191  return (v > math::pi) ? math::pipi - v : v;
192 }
193 
194 double math::toDegrees(double v)
195 {
196  double n = math::angleNormalize(v);
197  double d = n * 180. / math::pi;
198  if (n >= 0)
199  return d;
200  else
201  return 360. + d;
202 }
203 
204 double math::toRadians(double v)
205 {
206  if (v < 0)
207  v += 360.;
208  if (v >= 360.)
209  v -= 360.;
210 
211  if (v <= 180.)
212  return v * math::pi / 180.;
213  else
214  return -(360. - v) * math::pi / 180.;
215 }
216 
217 double math::uniformReal(double lower, double upper)
218 {
219  if (lower > upper)
220  {
221  SE2EZ_ERROR("Lower bound must be lower than upper bound");
222  return 0;
223  }
224 
225  return rng.uniformReal(lower, upper);
226 }
227 
228 int math::uniformInteger(int lower, int upper)
229 {
230  if (lower > upper)
231  SE2EZ_ERROR("Lower bound must be lower than upper bound");
232 
233  return rng.uniformInt(lower, upper);
234 }
235 
236 double math::gaussianReal(double mean, double std)
237 {
238  return rng.gaussian(mean, std);
239 }
Eigen::Isometry2d KDLToEigenFrame(const KDL::Frame &frame)
Converts an KDL transformation into a Eigen transformation.
Definition: math.cpp:55
T setf(T... args)
double gaussianReal(double mean, double std)
Return a random number sampled from a gaussain with given std and mean.
Definition: math.cpp:236
double toRadians(double v)
Convert an angle to radians.
Definition: math.cpp:204
Eigen::Vector3d flattenIsometry(const Eigen::Isometry2d &frame)
Converts a transformation frame into a vector [x, y, t] composed of a translation (x...
Definition: math.cpp:30
Eigen::MatrixXd toMatrix(const std::vector< Eigen::VectorXd > &vlist)
Converts a vector of lists into a Matrix.
Definition: math.cpp:150
static const double pi
Definition: math.h:32
std::string printFrame(const Eigen::Isometry2d &frame)
Returns a string "[x, y, t]" of the translation (x, y) and rotation (t) component of a transform...
Definition: math.cpp:79
double uniformReal(double lower, double upper)
Return a uniform random number between lower and upper.
Definition: math.cpp:217
T endl(T... args)
T precision(T... args)
double getRotation(const Eigen::Isometry2d &frame)
Gets rotation from a frame from the X-axis (t)
Definition: math.cpp:25
double clamp(double v, double a, double b)
Clamp a value v between a range [a, b].
Definition: math.cpp:179
std::string printMatrix(const Eigen::MatrixXd &v, unsigned int precision=4)
Returns a string of a matrix&#39;s contents.
Definition: math.cpp:109
Eigen::VectorXd KDLToEigenVec(const KDL::JntArray &array)
Converts an KDL vector into a Eigen vector.
Definition: math.cpp:74
double transformDistance(const Eigen::Isometry2d &t1, const Eigen::Isometry2d &t2=Eigen::Isometry2d::Identity(), double alpha=0.75)
Return the transform distance between two transformations, w is the weight of the euclidean distance...
Definition: math.cpp:36
T setw(T... args)
static const double pipi
Definition: math.h:33
Eigen::Isometry2d toIsometry(double x, double y, double theta)
Converts a translation (x, y) and rotation from X-axis (theta) into a transform.
Definition: math.cpp:11
#define SE2EZ_WARN(fmt,...)
Definition: log.h:41
T str(T... args)
Eigen::VectorXd toVector(double x)
Converts a single double value into a 1 x 1 matrix.
Definition: math.cpp:134
T fabs(T... args)
double angleNormalize(double v)
Normalize an angle between -pi to pi.
Definition: math.cpp:189
KDL::JntArray EigenToKDLVec(const Eigen::VectorXd &vec)
Converts an Eigen vector into a KDL vector.
Definition: math.cpp:66
std::string printVector(const Eigen::VectorXd &v, unsigned int precision=4)
Returns a string of a vector&#39;s contents.
Definition: math.cpp:90
KDL::Frame EigenToKDLFrame(const Eigen::Isometry2d &iso)
Converts an Eigen transformation into a KDL transformation.
Definition: math.cpp:48
int uniformInteger(int lower, int upper)
Return a uniform random integer between lower and upper (inclusive).
Definition: math.cpp:228
double toDegrees(double v)
Convert an angle to degrees.
Definition: math.cpp:194
Main namespace.
Definition: collision.h:11
#define SE2EZ_ERROR(fmt,...)
Definition: log.h:39
double remap(double a1, double a2, double av, double b1, double b2)
Remap a value av in the interval a1, a2 to the interval b1, b2.
Definition: math.cpp:160
static ompl::RNG rng
Random number generation from OMPL. An instance of this class cannot be used by multiple threads at o...
Definition: math.h:30
double angleDistance(double v1, double v2)
Return the minimum distance between two angles (wrapping from -pi to pi).
Definition: math.cpp:184