Main MRPT website > C++ reference for MRPT 1.9.9
CPose2D.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "base-precomp.h" // Precompiled headers
11 
12 #include <mrpt/poses/CPose2D.h>
13 #include <mrpt/poses/CPoint2D.h>
14 #include <mrpt/poses/CPose3D.h>
15 #include <mrpt/poses/CPoint3D.h>
16 #include <mrpt/utils/CStream.h>
17 #include <mrpt/math/wrap2pi.h>
18 #include <limits>
19 
20 using namespace mrpt;
21 using namespace mrpt::math;
22 using namespace mrpt::poses;
23 using namespace mrpt::utils;
24 
26 
27 /*---------------------------------------------------------------
28  Constructors
29  ---------------------------------------------------------------*/
30 CPose2D::CPose2D() : m_phi(0), m_cossin_uptodate(false)
31 {
32  m_coords[0] = m_coords[1] = 0;
33 }
34 
35 CPose2D::CPose2D(const double x, const double y, const double _phi)
36  : m_phi(_phi), m_cossin_uptodate(false)
37 {
38  m_coords[0] = x;
39  m_coords[1] = y;
40  normalizePhi();
41 }
42 
43 CPose2D::CPose2D(const CPoint2D& p) : m_phi(0), m_cossin_uptodate(false)
44 {
45  m_coords[0] = p.x();
46  m_coords[1] = p.y();
47 }
48 
49 CPose2D::CPose2D(const CPose3D& p) : m_phi(p.yaw()), m_cossin_uptodate(false)
50 {
51  m_coords[0] = p.x();
52  m_coords[1] = p.y();
53 }
54 
55 /*---------------------------------------------------------------
56  Implements the writing to a CStream capability of
57  CSerializable objects
58  ---------------------------------------------------------------*/
59 void CPose2D::writeToStream(mrpt::utils::CStream& out, int* version) const
60 {
61  if (version)
62  *version = 1;
63  else
64  {
65  // The coordinates:
66  out << m_coords[0] << m_coords[1] << m_phi;
67  }
68 }
69 
70 /*---------------------------------------------------------------
71  Implements the reading from a CStream capability of
72  CSerializable objects
73  ---------------------------------------------------------------*/
75 {
76  switch (version)
77  {
78  case 0:
79  {
80  // The coordinates:
81  float x0, y0, phi0;
82  in >> x0 >> y0 >> phi0;
83  m_coords[0] = x0;
84  m_coords[1] = y0;
85  this->phi(phi0);
86  }
87  break;
88  case 1:
89  {
90  // The coordinates:
91  in >> m_coords[0] >> m_coords[1] >> m_phi;
92  m_cossin_uptodate = false;
93  }
94  break;
95  default:
97  };
98 }
99 
100 /** Textual output stream function.
101  */
102 std::ostream& mrpt::poses::operator<<(std::ostream& o, const CPose2D& p)
103 {
104  o << format("(%.03f,%.03f,%.02fdeg)", p.x(), p.y(), RAD2DEG(p.phi()));
105  return o;
106 }
107 
108 /*---------------------------------------------------------------
109 The operator a="this"+D is the pose compounding operator.
110  ---------------------------------------------------------------*/
112 {
114 
115  return CPose2D(
116  m_coords[0] + D.m_coords[0] * m_cosphi - D.m_coords[1] * m_sinphi,
117  m_coords[1] + D.m_coords[0] * m_sinphi + D.m_coords[1] * m_cosphi,
118  m_phi + D.m_phi);
119 }
120 
121 /*---------------------------------------------------------------
122  composeFrom
123  ---------------------------------------------------------------*/
124 void CPose2D::composeFrom(const CPose2D& A, const CPose2D& B)
125 {
127 
128  // Use temporary variables for the cases (A==this) or (B==this)
129  const double new_x =
130  A.m_coords[0] + B.m_coords[0] * A.m_cosphi - B.m_coords[1] * A.m_sinphi;
131  const double new_y =
132  A.m_coords[1] + B.m_coords[0] * A.m_sinphi + B.m_coords[1] * A.m_cosphi;
133  m_coords[0] = new_x;
134  m_coords[1] = new_y;
135 
136  m_phi = math::wrapToPi(A.m_phi + B.m_phi);
137  m_cossin_uptodate = false;
138 }
139 
140 /*---------------------------------------------------------------
141  getRotationMatrix
142  ---------------------------------------------------------------*/
144 {
146  R(0, 0) = m_cosphi;
147  R(0, 1) = -m_sinphi;
148  R(1, 0) = m_sinphi;
149  R(1, 1) = m_cosphi;
150 }
151 
153 {
155  R(0, 0) = m_cosphi;
156  R(0, 1) = -m_sinphi;
157  R(0, 2) = 0;
158  R(1, 0) = m_sinphi;
159  R(1, 1) = m_cosphi;
160  R(1, 2) = 0;
161  R(2, 0) = 0;
162  R(2, 1) = 0;
163  R(2, 2) = 1;
164 }
165 
166 /*---------------------------------------------------------------
167 The operator a="this"+D is the pose compounding operator.
168  ---------------------------------------------------------------*/
170 {
171  return CPose3D(*this) + D;
172 }
173 
174 /*---------------------------------------------------------------
175 The operator u'="this"+u is the pose/point compounding operator.
176  ---------------------------------------------------------------*/
178 {
180 
181  return CPoint2D(
182  m_coords[0] + u.x() * m_cosphi - u.y() * m_sinphi,
183  m_coords[1] + u.x() * m_sinphi + u.y() * m_cosphi);
184 }
185 
186 /** An alternative, slightly more efficient way of doing \f$ G = P \oplus L \f$
187  * with G and L being 2D points and P this 2D pose. */
188 void CPose2D::composePoint(double lx, double ly, double& gx, double& gy) const
189 {
191 
192  gx = m_coords[0] + lx * m_cosphi - ly * m_sinphi;
193  gy = m_coords[1] + lx * m_sinphi + ly * m_cosphi;
194 }
195 
198 {
199  this->composePoint(l.x, l.y, g.x, g.y);
200 }
201 
202 /** overload \f$ G = P \oplus L \f$ with G and L being 3D points and P this 2D
203  * pose (the "z" coordinate remains unmodified) */
206 {
207  this->composePoint(l.x, l.y, l.z, g.x, g.y, g.z);
208 }
209 
211  double lx, double ly, double lz, double& gx, double& gy, double& gz) const
212 {
214  gx = m_coords[0] + lx * m_cosphi - ly * m_sinphi;
215  gy = m_coords[1] + lx * m_sinphi + ly * m_cosphi;
216  gz = lz;
217 }
218 
220  const double gx, const double gy, double& lx, double& ly) const
221 {
223 
224  const double Ax = gx - m_coords[0];
225  const double Ay = gy - m_coords[1];
226 
227  lx = Ax * m_cosphi + Ay * m_sinphi;
228  ly = -Ax * m_sinphi + Ay * m_cosphi;
229 }
230 
231 /*---------------------------------------------------------------
232 The operator u'="this"+u is the pose/point compounding operator.
233  ---------------------------------------------------------------*/
235 {
237 
238  return CPoint3D(
239  m_coords[0] + u.x() * m_cosphi - u.y() * m_sinphi,
240  m_coords[1] + u.x() * m_sinphi + u.y() * m_cosphi, u.z());
241 }
242 
243 /*---------------------------------------------------------------
244 The operator D="this"-b is the pose inverse compounding operator.
245  The resulting pose "D" is the diference between this pose and "b"
246  ---------------------------------------------------------------*/
248 {
250 
251  m_coords[0] = (A.m_coords[0] - B.m_coords[0]) * B.m_cosphi +
252  (A.m_coords[1] - B.m_coords[1]) * B.m_sinphi;
253  m_coords[1] = -(A.m_coords[0] - B.m_coords[0]) * B.m_sinphi +
254  (A.m_coords[1] - B.m_coords[1]) * B.m_cosphi;
255  m_phi = math::wrapToPi(A.m_phi - B.m_phi);
256  m_cossin_uptodate = false;
257 }
258 
259 /*---------------------------------------------------------------
260  Scalar sum of components: This is diferent from poses
261  composition, which is implemented as "+" operators in "CPose" derived
262  classes.
263  ---------------------------------------------------------------*/
265 {
266  m_coords[0] += p.m_coords[0];
267  m_coords[1] += p.m_coords[1];
268  m_phi += p.m_phi;
269  m_cossin_uptodate = false;
270 }
271 
272 /*---------------------------------------------------------------
273  Scalar multiplication.
274  ---------------------------------------------------------------*/
275 void CPose2D::operator*=(const double s)
276 {
277  m_coords[0] *= s;
278  m_coords[1] *= s;
279  m_phi *= s;
280  m_cossin_uptodate = false;
281 }
282 
283 /*---------------------------------------------------------------
284  Returns the corresponding 4x4 homogeneous
285  transformation matrix for the point(translation),
286  or pose (translation+orientation).
287 ---------------------------------------------------------------*/
289 {
290  m.unit(4, 1.0);
291 
292  m.set_unsafe(0, 3, m_coords[0]);
293  m.set_unsafe(1, 3, m_coords[1]);
294 
296 
297  m.get_unsafe(0, 0) = m_cosphi;
298  m.get_unsafe(0, 1) = -m_sinphi;
299  m.get_unsafe(1, 0) = m_sinphi;
300  m.get_unsafe(1, 1) = m_cosphi;
301 }
302 
303 /** Forces "phi" to be in the range [-pi,pi];
304 */
306 {
308  m_cossin_uptodate = false;
309 }
310 
312  : m_phi(o.phi), m_cossin_uptodate(false)
313 {
314  m_coords[0] = o.x;
315  m_coords[1] = o.y;
316 }
317 
318 CPose2D::CPose2D(const CPoint3D& o) : m_phi(0), m_cossin_uptodate(false)
319 {
320  this->m_coords[0] = o.x();
321  this->m_coords[1] = o.y();
322  normalizePhi();
323 }
324 
325 /*---------------------------------------------------------------
326  unary -
327 ---------------------------------------------------------------*/
329 {
330  CPose2D ret = b;
331  ret.inverse();
332  return ret;
333 }
334 
335 /** Convert this pose into its inverse, saving the result in itself. \sa
336  * operator- */
338 {
340  const double x = m_coords[0];
341  const double y = m_coords[1];
342 
343  m_coords[0] = -x * m_cosphi - y * m_sinphi;
344  m_coords[1] = x * m_sinphi - y * m_cosphi;
345  m_phi = -m_phi;
346  m_cossin_uptodate = false;
347 }
348 
349 /*---------------------------------------------------------------
350  getAsVector
351 ---------------------------------------------------------------*/
353 {
354  v.resize(3);
355  v[0] = m_coords[0];
356  v[1] = m_coords[1];
357  v[2] = m_phi;
358 }
359 
361 {
362  v[0] = m_coords[0];
363  v[1] = m_coords[1];
364  v[2] = m_phi;
365 }
366 
367 bool mrpt::poses::operator==(const CPose2D& p1, const CPose2D& p2)
368 {
369  return (p1.x() == p2.x()) && (p1.y() == p2.y()) && (p1.phi() == p2.phi());
370 }
371 
372 bool mrpt::poses::operator!=(const CPose2D& p1, const CPose2D& p2)
373 {
374  return (p1.x() != p2.x()) || (p1.y() != p2.y()) || (p1.phi() != p2.phi());
375 }
376 
378 {
379  const double ccos = pose.phi_cos();
380  const double ssin = pose.phi_sin();
381  return TPoint2D(
382  pose.x() + u.x * ccos - u.y * ssin, pose.y() + u.x * ssin + u.y * ccos);
383 }
384 
385 /** Make \f$ this = this \oplus b \f$ */
387 {
388  composeFrom(*this, b);
389  return *this;
390 }
391 
393 {
394  CMatrixDouble m;
395  if (!m.fromMatlabStringFormat(s))
396  THROW_EXCEPTION("Malformed expression in ::fromString");
397  ASSERTMSG_(
398  mrpt::math::size(m, 1) == 1 && mrpt::math::size(m, 2) == 3,
399  "Wrong size of vector in ::fromString");
400  x(m.get_unsafe(0, 0));
401  y(m.get_unsafe(0, 1));
402  phi(DEG2RAD(m.get_unsafe(0, 2)));
403 }
404 
406 {
407  this->fromString("[" + s + "]");
408 }
409 
411 {
412  return std::sqrt(
413  square(p.x() - x()) + square(p.y() - y()) +
414  4 * (1 - cos(p.phi() - phi())));
415 }
416 
418 {
420  b.getInverseHomogeneousMatrix(B_INV);
422  this->getHomogeneousMatrix(HM);
424  RES.multiply(B_INV, HM);
425  return CPose3D(RES);
426 }
427 
429 {
430  return CPose2D(-m_coords[0], -m_coords[1], -m_phi);
431 }
432 
434 {
435  s = mrpt::format("[%f %f %fdeg]", x(), y(), mrpt::utils::RAD2DEG(m_phi));
436 }
437 
439 {
440  for (int i = 0; i < 3; i++)
441  (*this)[i] = std::numeric_limits<double>::quiet_NaN();
442 }
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:135
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
double phi_sin() const
Get a (cached) value of sin(phi), recomputing it only once when phi changes.
Definition: CPose2D.h:108
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
double x
X,Y coordinates.
double x
X,Y coordinates.
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:44
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
mrpt::math::TPoint2D operator+(const CPose2D &pose, const mrpt::math::TPoint2D &pnt)
Compose a 2D point from a new coordinate base given by a 2D pose.
Definition: CPose2D.cpp:377
#define THROW_EXCEPTION(msg)
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction...
Definition: eigen_frwds.h:42
void operator*=(const double s)
Scalar multiplication.
Definition: CPose2D.cpp:275
void AddComponents(const CPose2D &p)
Scalar sum of components: This is diferent from poses composition, which is implemented as "+" operat...
Definition: CPose2D.cpp:264
mrpt::math::CArrayDouble< 2 > m_coords
[x,y]
Definition: CPose2D.h:47
double m_cosphi
Precomputed cos() & sin() of phi.
Definition: CPose2D.h:53
GLdouble s
Definition: glext.h:3676
void composePoint(double lx, double ly, double &gx, double &gy) const
An alternative, slightly more efficient way of doing with G and L being 2D points and P this 2D pose...
Definition: CPose2D.cpp:188
T square(const T x)
Inline function for the square of a number.
Definition: bits.h:55
CPose2D operator-(const CPose2D &b) const
Compute .
Definition: CPose2D.h:213
void fromStringRaw(const std::string &s)
Same as fromString, but without requiring the square brackets in the string.
Definition: CPose2D.cpp:405
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
A numeric matrix of compile-time fixed size.
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
double RAD2DEG(const double x)
Radians to degrees.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
bool operator!=(const CPoint< DERIVEDCLASS > &p1, const CPoint< DERIVEDCLASS > &p2)
Definition: CPoint.h:171
double m_phi
The orientation of the pose, in radians.
Definition: CPose2D.h:51
CPose2D operator-(const CPose2D &p)
Unary - operator: return the inverse pose "-p" (Note that is NOT the same than a pose with negative x...
Definition: CPose2D.cpp:328
void setToNaN() override
Set all data fields to quiet NaN.
Definition: CPose2D.cpp:438
GLubyte g
Definition: glext.h:6279
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:19
GLubyte GLubyte b
Definition: glext.h:6279
double x
X,Y,Z coordinates.
void inverse()
Convert this pose into its inverse, saving the result in itself.
Definition: CPose2D.cpp:337
#define DEG2RAD
CPose2D operator+(const CPose2D &D) const
The operator is the pose compounding operator.
Definition: CPose2D.cpp:111
GLsizei const GLchar ** string
Definition: glext.h:4101
T wrapToPi(T a)
Modifies the given angle to translate it into the ]-pi,pi] range.
Definition: wrap2pi.h:53
A class used to store a 2D point.
Definition: CPoint2D.h:36
A class used to store a 3D point.
Definition: CPoint3D.h:32
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CPoint.h:17
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
Definition: CPose2D.cpp:59
std::string asString() const
Definition: CPose2D.h:251
#define RAD2DEG
const GLdouble * v
Definition: glext.h:3678
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
size_t size(const MATRIXLIKE &m, const int dim)
mrpt::math::CMatrixDouble22 getRotationMatrix() const
Definition: CPose2D.h:145
bool operator==(const CPoint< DERIVEDCLASS > &p1, const CPoint< DERIVEDCLASS > &p2)
Definition: CPoint.h:163
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle...
Definition: CPose2D.h:40
const float R
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
CPose2D & operator+=(const CPose2D &b)
Make .
Definition: CPose2D.cpp:386
CPose2D getOppositeScalar() const
Return the opposite of the current pose instance by taking the negative of all its components individ...
Definition: CPose2D.cpp:428
void getHomogeneousMatrix(mrpt::math::CMatrixDouble44 &out_HM) const
Returns the corresponding 4x4 homogeneous transformation matrix for the point(translation) or pose (t...
Definition: CPose2D.cpp:288
const double & phi() const
Get the phi angle of the 2D pose (in radians)
Definition: CPose2D.h:91
void composeFrom(const CPose2D &A, const CPose2D &B)
Makes .
Definition: CPose2D.cpp:124
double phi_cos() const
Get a (cached) value of cos(phi), recomputing it only once when phi changes.
Definition: CPose2D.h:101
void inverseComposePoint(const double gx, const double gy, double &lx, double &ly) const
Computes the 2D point L such as .
Definition: CPose2D.cpp:219
GLuint in
Definition: glext.h:7274
void fromString(const std::string &s)
Set the current object value from a string generated by &#39;asString&#39; (eg: "[0.02 1.04 -0...
Definition: CPose2D.cpp:392
Lightweight 2D pose.
GLenum GLint GLint y
Definition: glext.h:3538
double distance2DFrobeniusTo(const CPose2D &p) const
Returns the 2D distance from this pose/point to a 2D pose using the Frobenius distance.
Definition: CPose2D.cpp:410
GLenum GLint x
Definition: glext.h:3538
Lightweight 3D point.
void inverseComposeFrom(const CPose2D &A, const CPose2D &B)
Makes this method is slightly more efficient than "this= A - B;" since it avoids the temporary objec...
Definition: CPose2D.cpp:247
void readFromStream(mrpt::utils::CStream &in, int version) override
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
Definition: CPose2D.cpp:74
bool m_cossin_uptodate
Definition: CPose2D.h:54
void getAsVector(mrpt::math::CVectorDouble &v) const
Returns a 1x3 vector with [x y phi].
Definition: CPose2D.cpp:352
Lightweight 2D point.
#define ASSERTMSG_(f, __ERROR_MSG)
GLfloat GLfloat p
Definition: glext.h:6305
CPose2D()
Default constructor (all coordinates to 0)
Definition: CPose2D.cpp:30
void normalizePhi()
Forces "phi" to be in the range [-pi,pi];.
Definition: CPose2D.cpp:305
std::ostream & operator<<(std::ostream &o, const CPoint< DERIVEDCLASS > &p)
Dumps a point as a string [x,y] or [x,y,z].
Definition: CPoint.h:137
void update_cached_cos_sin() const
Definition: CPose2D.h:56



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019