Robot Dynamics Library
FramePoint.hpp
Go to the documentation of this file.
1 // Copyright (c) 2017 Jordan Lack <jlack1987@gmail.com>
2 // RDL - Robot Dynamics Library
3 // Licensed under the zlib license. See LICENSE for more details.
4 
10 #ifndef __RDL_FRAME_POINT_HPP__
11 #define __RDL_FRAME_POINT_HPP__
12 
15 #include "rdl_dynamics/Point3.hpp"
17 
24 namespace RobotDynamics
25 {
26 namespace Math
27 {
37 class FramePoint : public FrameObject, public Math::Point3d
38 {
39  public:
47  FramePoint(ReferenceFramePtr referenceFrame, const double x, const double y, const double z) : FrameObject(referenceFrame), Math::Point3d(x, y, z)
48  {
49  }
50 
57  {
58  }
59 
66  {
67  }
68 
73  FramePoint(const FramePoint& framePoint) : FrameObject(framePoint.getReferenceFrame()), Math::Point3d(framePoint.x(), framePoint.y(), framePoint.z())
74  {
75  }
76 
82  {
83  }
84 
88  FramePoint() : FrameObject(nullptr), Math::Point3d()
89  {
90  }
91 
93  {
95  this->x() = p.x();
96  this->y() = p.y();
97  this->z() = p.z();
98  return *this;
99  }
100 
105  {
106  }
107 
112  inline Math::Point3d point() const
113  {
114  return Math::Point3d(x(), y(), z());
115  }
116 
123  {
124  return this;
125  }
126 
133  {
134  FramePoint p = *this;
136  return p;
137  }
138 
145  {
146  setIncludingFrame(v(0), v(1), v(2), referenceFrame);
147  }
148 
156  void setIncludingFrame(const double x, const double y, const double z, ReferenceFramePtr referenceFrame)
157  {
158  if (!referenceFrame)
159  {
160  throw ReferenceFrameException("Reference frame is nullptr!");
161  }
162 
163  this->set(x, y, z);
164  this->referenceFrame = referenceFrame;
165  }
166 
173  {
174  if (!referenceFrame)
175  {
176  throw ReferenceFrameException("Reference frame cannot be nullptr!");
177  }
178 
179  this->x() = point.x();
180  this->y() = point.y();
181  this->z() = point.z();
182  this->referenceFrame = referenceFrame;
183  }
184 
191  double distanceSquared(const FramePoint& point) const
192  {
194 
195  double dx = this->x() - point.x();
196  double dy = this->y() - point.y();
197  double dz = this->z() - point.z();
198  return dx * dx + dy * dy + dz * dz;
199  }
200 
209  double distance2DSquared(const FramePoint& point, int plane = 2) const
210  {
212  if (plane < 0 || plane > 2)
213  {
214  throw std::runtime_error("FramePoint.distance2DSquared: Plane argument must be either 0, 1, or 2");
215  }
216 
217  double dx = plane == 0 ? 0. : this->x() - point.x();
218  double dy = plane == 1 ? 0. : this->y() - point.y();
219  double dz = plane == 2 ? 0. : this->z() - point.z();
220  return dx * dx + dy * dy + dz * dz;
221  }
222 
230  double distance2DSquared(const Point3d& point, int plane = 2) const
231  {
232  if (plane < 0 || plane > 2)
233  {
234  throw std::runtime_error("FramePoint.distance2DSquared: Plane argument must be either 0, 1, or 2");
235  }
236 
237  double dx = plane == 0 ? 0. : this->x() - point.x();
238  double dy = plane == 1 ? 0. : this->y() - point.y();
239  double dz = plane == 2 ? 0. : this->z() - point.z();
240  return dx * dx + dy * dy + dz * dz;
241  }
242 
248  double distanceSquared(const Point3d& point) const
249  {
250  double dx = this->x() - point.x();
251  double dy = this->y() - point.y();
252  double dz = this->z() - point.z();
253  return dx * dx + dy * dy + dz * dz;
254  }
255 
262  double distance2D(const FramePoint& point, int plane = 2) const
263  {
265 
266  return sqrt(distance2DSquared(point, plane));
267  }
268 
276  double distance(const FramePoint& point) const
277  {
279 
280  return sqrt(distanceSquared(point));
281  }
282 
289  double distance2D(const Point3d& point, int plane = 2) const
290  {
291  return sqrt(distance2DSquared(point, plane));
292  }
293 
299  double distance(const Point3d& point) const
300  {
301  return sqrt(distanceSquared(point));
302  }
303 
310  double distanceL1(const FramePoint& point) const
311  {
313 
314  return fabs(this->x() - point.x()) + fabs(this->y() - point.y()) + fabs(this->z() - point.z());
315  }
316 
323  double distanceLinf(const FramePoint& point) const
324  {
326 
327  double dx = this->x() - point.x();
328  double dy = this->y() - point.y();
329  double dz = this->z() - point.z();
330 
331  double tmp = fabs(dx) > fabs(dy) ? fabs(dx) : fabs(dy);
332 
333  return tmp > fabs(dz) ? tmp : fabs(dz);
334  }
335 
344  [[deprecated("Use isApprox(FramePoint, double) instead.")]] bool epsilonEquals(const FramePoint& point, const double epsilon) const
345  {
347  return std::abs(this->x() - point.x()) < epsilon && std::abs(this->y() - point.y()) < epsilon && std::abs(this->z() - point.z()) < epsilon;
348  }
349 
355  bool isApprox(const FramePoint& point, const double epsilon) const
356  {
357  return referenceFrame == point.getReferenceFrame() && std::abs(this->x() - point.x()) < epsilon && std::abs(this->y() - point.y()) < epsilon &&
358  std::abs(this->z() - point.z()) < epsilon;
359  }
360 
366  template <typename T>
367  void operator*=(const T scale)
368  {
369  this->x() *= scale;
370  this->y() *= scale;
371  this->z() *= scale;
372  }
373 
379  template <typename T>
380  void operator/=(const T scale)
381  {
382  this->x() /= scale;
383  this->y() /= scale;
384  this->z() /= scale;
385  }
386 
387  void operator+=(const FrameVector& v)
388  {
390  this->x() += v.x();
391  this->y() += v.y();
392  this->z() += v.z();
393  }
394 
395  void operator-=(const FrameVector& v)
396  {
398  this->x() -= v.x();
399  this->y() -= v.y();
400  this->z() -= v.z();
401  }
402 };
403 
405 {
406  p += v;
407  return p;
408 }
409 
411 {
412  p -= v;
413  return p;
414 }
415 
416 template <typename T>
417 inline FrameVector operator*(const T scale, FramePoint p)
418 {
419  p *= scale;
420  return FrameVector(p.getReferenceFrame(), p.x(), p.y(), p.z());
421 }
422 
423 template <typename T>
424 inline FrameVector operator*(FramePoint p, const T scale)
425 {
426  p *= scale;
427  return FrameVector(p.getReferenceFrame(), p.x(), p.y(), p.z());
428 }
429 
437 inline bool operator==(const FramePoint& lhs, const FramePoint& rhs)
438 {
439  lhs.checkReferenceFramesMatch(&rhs);
440 
441  if (lhs.x() != rhs.x())
442  {
443  return false;
444  }
445 
446  if (lhs.y() != rhs.y())
447  {
448  return false;
449  }
450 
451  if (lhs.z() != rhs.z())
452  {
453  return false;
454  }
455 
456  return true;
457 }
458 
467 {
468  p1.getReferenceFrame()->checkReferenceFramesMatch(p2.getReferenceFrame());
469  return FrameVector(p1.getReferenceFrame(), p1.x() - p2.x(), p1.y() - p2.y(), p1.z() - p2.z());
470 }
471 
479 inline bool operator!=(const FramePoint& lhs, const FramePoint& rhs)
480 {
481  return !operator==(lhs, rhs);
482 }
483 
484 inline std::ostream& operator<<(std::ostream& output, const FramePoint& framePoint)
485 {
486  output << "ReferenceFrame = " << framePoint.getReferenceFrame()->getName() << std::endl;
487  output << "x = " << framePoint.x() << " y = " << framePoint.y() << " z = " << framePoint.z() << std::endl;
488  return output;
489 }
490 typedef std::vector<FramePoint, Eigen::aligned_allocator<FramePoint>> FramePointV;
491 } // namespace Math
492 } // namespace RobotDynamics
493 #endif // ifndef __RDL_FRAME_POINT_HPP__
An interface that objects with a ReferenceFrame extend to inherit the FrameObject::changeFrame method...
Definition: FrameObject.hpp:26
virtual void changeFrame(ReferenceFramePtr desiredFrame)
Change the ReferenceFrame this FrameObject is expressed in.
Definition: FrameObject.cpp:9
void checkReferenceFramesMatch(const FrameObject *frameObject) const
Check if two FrameObjects hold the same ReferenceFrame.
Definition: FrameObject.hpp:67
ReferenceFramePtr getReferenceFrame() const
Get a pointer to the reference frame this FrameObject is expressed in.
Definition: FrameObject.hpp:49
ReferenceFramePtr referenceFrame
Definition: FrameObject.hpp:78
A FramePoint is a 3D point that is expressed in a ReferenceFrame. To change the ReferenceFrame a Fram...
Definition: FramePoint.hpp:38
FramePoint & operator=(const FramePoint &p)
Definition: FramePoint.hpp:92
double distance2DSquared(const Point3d &point, int plane=2) const
Calculate the distance squared from this point to a Point3d.
Definition: FramePoint.hpp:230
Math::TransformableGeometricObject * getTransformableGeometricObject()
Return a pointer to this as base class type Math::TransformableGeometricObject. See FrameObject::chan...
Definition: FramePoint.hpp:122
FramePoint(ReferenceFramePtr referenceFrame, const Math::Point3d &point)
Constructor.
Definition: FramePoint.hpp:65
void operator+=(const FrameVector &v)
Definition: FramePoint.hpp:387
bool isApprox(const FramePoint &point, const double epsilon) const
Return true FramePoint argument is within epsilon of this and they have the same reference frame,...
Definition: FramePoint.hpp:355
FramePoint(ReferenceFramePtr referenceFrame)
Constructor that initializes to (x,y,z) = (0,0,0)
Definition: FramePoint.hpp:81
double distanceSquared(const FramePoint &point) const
Calculate the distance squared between two FramePoints. .
Definition: FramePoint.hpp:191
FramePoint(ReferenceFramePtr referenceFrame, const double x, const double y, const double z)
Constructor.
Definition: FramePoint.hpp:47
double distanceL1(const FramePoint &point) const
Calculate the L1 distance between two FramePoints by .
Definition: FramePoint.hpp:310
void setIncludingFrame(const Math::Point3d &point, ReferenceFramePtr referenceFrame)
Set both the ReferenceFrame the point is expressed in as well as the (x,y,z) coordinates.
Definition: FramePoint.hpp:172
double distance(const Point3d &point) const
Calculate the distance to a Point3d.
Definition: FramePoint.hpp:299
~FramePoint()
Destructor.
Definition: FramePoint.hpp:104
FramePoint changeFrameAndCopy(ReferenceFramePtr referenceFrame) const
copy into new frame point and change the frame of that
Definition: FramePoint.hpp:132
EIGEN_STRONG_INLINE void setIncludingFrame(const Math::Vector3d &v, ReferenceFramePtr referenceFrame)
Set both the ReferenceFrame this object is expressed in as well as the (x,y,z) coordinates of the poi...
Definition: FramePoint.hpp:144
double distance2DSquared(const FramePoint &point, int plane=2) const
Calculate the 2D distance squared between two FramePoints.
Definition: FramePoint.hpp:209
FramePoint()
Empty constructor that creates a point with ReferencFrame=nullptr and (x,y,z)=(0,0,...
Definition: FramePoint.hpp:88
void operator/=(const T scale)
Overloaded /= operator, performs this = this*scale.
Definition: FramePoint.hpp:380
bool epsilonEquals(const FramePoint &point, const double epsilon) const
Return true FramePoint argument is within epsilon of this, false otherwise.
Definition: FramePoint.hpp:344
void setIncludingFrame(const double x, const double y, const double z, ReferenceFramePtr referenceFrame)
Set both the ReferenceFrame the point is expressed in as well as the (x,y,z) coordinates.
Definition: FramePoint.hpp:156
double distance2D(const FramePoint &point, int plane=2) const
Calculate the distance between two FramePoints. .
Definition: FramePoint.hpp:262
FramePoint(const FramePoint &framePoint)
Copy constructor.
Definition: FramePoint.hpp:73
double distance(const FramePoint &point) const
Calculate the distance between two FramePoints. .
Definition: FramePoint.hpp:276
double distanceLinf(const FramePoint &point) const
Calculate the LInfinity distance between two FramePoints by .
Definition: FramePoint.hpp:323
void operator-=(const FrameVector &v)
Definition: FramePoint.hpp:395
FramePoint(ReferenceFramePtr referenceFrame, Math::Vector3d v)
Constructor.
Definition: FramePoint.hpp:56
void operator*=(const T scale)
Overloaded *= operator, performs this = this*scala.
Definition: FramePoint.hpp:367
Math::Point3d point() const
Get as point3d.
Definition: FramePoint.hpp:112
double distanceSquared(const Point3d &point) const
Calculate the distance squared from this point to a Point3d.
Definition: FramePoint.hpp:248
double distance2D(const Point3d &point, int plane=2) const
Calculate the distance to a Point3d.
Definition: FramePoint.hpp:289
A FrameVector is a 3D vector with a ReferenceFrame, and all operations between FrameVectors and other...
Definition: FrameVector.hpp:31
Definition: Point3.hpp:26
EIGEN_STRONG_INLINE double & z()
Definition: Point3.hpp:276
EIGEN_STRONG_INLINE double & y()
Definition: Point3.hpp:266
EIGEN_STRONG_INLINE double & x()
Definition: Point3.hpp:256
EIGEN_STRONG_INLINE void set(const std::vector< double > &vector)
Definition: Point3.hpp:68
The TransformableGeometricObject class is an essential interface because it forces all geometric obje...
Definition: rdl_eigenmath.hpp:43
Definition: rdl_eigenmath.hpp:54
A custom exception for frame operations.
Definition: FrameExceptions.hpp:28
std::shared_ptr< ReferenceFrame > ReferenceFramePtr
Definition: ReferenceFrame.hpp:68
FramePoint operator-(FramePoint p, const FrameVector &v)
Definition: FramePoint.hpp:410
bool operator!=(const FramePoint &lhs, const FramePoint &rhs)
Check if two FramePoints are not equal.
Definition: FramePoint.hpp:479
std::vector< FramePoint, Eigen::aligned_allocator< FramePoint > > FramePointV
Definition: FramePoint.hpp:490
FramePoint operator+(FramePoint p, const FrameVector &v)
Definition: FramePoint.hpp:404
std::ostream & operator<<(std::ostream &output, const FramePoint &framePoint)
Definition: FramePoint.hpp:484
ForceVector operator*(const SpatialTransform &X, ForceVector f)
Operator for transforming a ForceVector. Calls the ForceVector::transform method.
Definition: ForceVector.hpp:248
bool operator==(const FramePoint &lhs, const FramePoint &rhs)
Check if two FramePoints are equal.
Definition: FramePoint.hpp:437
Namespace for all structures of the RobotDynamics library.
Definition: examples.hpp:19