Robot Dynamics Library
FrameVector.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 
5 #ifndef __RDL_FRAME_VECTOR_HPP__
6 #define __RDL_FRAME_VECTOR_HPP__
7 
16 
17 namespace RobotDynamics
18 {
19 namespace Math
20 {
30 class FrameVector : public FrameObject, public Math::Vector3d
31 {
32  public:
36  FrameVector() : FrameObject(nullptr), Math::Vector3d(0., 0., 0.)
37  {
38  }
39 
45  {
46  }
47 
55  FrameVector(ReferenceFramePtr referenceFrame, const double& x, const double& y, const double& z) : FrameObject(referenceFrame), Math::Vector3d(x, y, z)
56  {
57  }
58 
64  FrameVector(ReferenceFramePtr referenceFrame, const Eigen::Vector3d& vector) : FrameObject(referenceFrame), Math::Vector3d(vector[0], vector[1], vector[2])
65  {
66  }
67 
71  virtual ~FrameVector()
72  {
73  }
74 
81  {
82  return this;
83  }
84 
91  {
92  FrameVector p = *this;
94  return p;
95  }
96 
100  inline void setToZero()
101  {
102  set(0., 0., 0.);
103  }
104 
113  inline void setIncludingFrame(const double x, const double y, const double z, ReferenceFramePtr referenceFrame)
114  {
115  if (!referenceFrame)
116  {
117  throw ReferenceFrameException("Reference frame cannot be nullptr!");
118  }
119 
120  set(x, y, z);
121  this->referenceFrame = referenceFrame;
122  }
123 
130  inline void setIncludingFrame(const Eigen::Vector3d& vector, ReferenceFramePtr referenceFrame)
131  {
132  if (!referenceFrame)
133  {
134  throw ReferenceFrameException("Reference frame cannot be nullptr!");
135  }
136 
137  set(vector[0], vector[1], vector[2]);
138  this->referenceFrame = referenceFrame;
139  }
140 
147  inline double dot(const FrameVector& frameVector) const
148  {
149  checkReferenceFramesMatch(&frameVector);
150 
151  return this->x() * frameVector.x() + this->y() * frameVector.y() + this->z() * frameVector.z();
152  }
153 
160  inline FrameVector cross(const FrameVector& vector) const
161  {
162  checkReferenceFramesMatch(&vector);
163  return FrameVector(this->referenceFrame, this->y() * vector.z() - this->z() * vector.y(), vector.x() * this->z() - vector.z() * this->x(),
164  this->x() * vector.y() - this->y() * vector.x());
165  }
166 
172  inline Vector3d cross(const Vector3d& vector) const
173  {
174  return RobotDynamics::Math::Vector3d::cross(vector);
175  }
176 
182  inline double getAngleBetweenVectors(const FrameVector& frameVector) const
183  {
184  checkReferenceFramesMatch(&frameVector);
185 
186  return acos(std::max(-1., std::min(this->dot(frameVector) / (this->norm() * frameVector.norm()), 1.)));
187  }
188 
189  inline Vector3d vec() const
190  {
191  return Math::Vector3d(x(), y(), z());
192  }
193 
194  void operator+=(const Vector3d& v)
195  {
196  this->x() += v.x();
197  this->y() += v.y();
198  this->z() += v.z();
199  }
200 
201  void operator-=(const Vector3d& v)
202  {
203  this->x() -= v.x();
204  this->y() -= v.y();
205  this->z() -= v.z();
206  }
207 
212  void operator+=(const FrameVector& v)
213  {
214  this->checkReferenceFramesMatch(&v);
215  this->x() += v.x();
216  this->y() += v.y();
217  this->z() += v.z();
218  }
219 
224  void operator-=(const FrameVector& v)
225  {
226  this->checkReferenceFramesMatch(&v);
227  this->x() -= v.x();
228  this->y() -= v.y();
229  this->z() -= v.z();
230  }
231 
237  template <typename T>
238  void operator*=(const T scale)
239  {
240  this->x() *= scale;
241  this->y() *= scale;
242  this->z() *= scale;
243  }
244 };
245 
246 template <typename T>
247 inline FrameVector operator*(FrameVector v1, const T scale)
248 {
249  v1 *= scale;
250  return v1;
251 }
252 
253 template <typename T>
254 inline FrameVector operator*(const T scale, FrameVector v1)
255 {
256  v1 *= scale;
257  return v1;
258 }
259 
261 {
262  v1 += v2;
263  return v1;
264 }
265 
267 {
268  v1 -= v2;
269  return v1;
270 }
271 
278 // cppcheck-suppress passedByValue
280 {
281  v1 += v2;
282  return v1;
283 }
284 
291 // cppcheck-suppress passedByValue
293 {
294  v1 -= v2;
295  return v1;
296 }
297 typedef std::vector<FrameVector, Eigen::aligned_allocator<FrameVector>> FrameVectorV;
298 } // namespace Math
299 } // namespace RobotDynamics
300 
301 #endif // ifndef __RDL_FRAME_VECTOR_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 referenceFrame
Definition: FrameObject.hpp:78
A FrameVector is a 3D vector with a ReferenceFrame, and all operations between FrameVectors and other...
Definition: FrameVector.hpp:31
void operator+=(const FrameVector &v)
Plus euals operator that performs runtime frame checks, .
Definition: FrameVector.hpp:212
void setIncludingFrame(const double x, const double y, const double z, ReferenceFramePtr referenceFrame)
Set the x, y, and z components and the ReferenceFrame these components are expressed in.
Definition: FrameVector.hpp:113
double getAngleBetweenVectors(const FrameVector &frameVector) const
Computer the angle between two FrameVectors, .
Definition: FrameVector.hpp:182
Math::TransformableGeometricObject * getTransformableGeometricObject()
Return pointer to this object as type TransformableGeometricObject. See FrameObject::changeFrame for ...
Definition: FrameVector.hpp:80
Vector3d cross(const Vector3d &vector) const
Cross product, i.e. .
Definition: FrameVector.hpp:172
FrameVector(ReferenceFramePtr referenceFrame)
Constructor.
Definition: FrameVector.hpp:44
void operator+=(const Vector3d &v)
Definition: FrameVector.hpp:194
Vector3d vec() const
Definition: FrameVector.hpp:189
FrameVector cross(const FrameVector &vector) const
Cross product between two FrameVectors, i.e. .
Definition: FrameVector.hpp:160
FrameVector()
Default constructor. Initializes its ReferenceFrame to nullptr.
Definition: FrameVector.hpp:36
void setIncludingFrame(const Eigen::Vector3d &vector, ReferenceFramePtr referenceFrame)
Set the x, y, and z components and the ReferenceFrame these components are expressed in.
Definition: FrameVector.hpp:130
void setToZero()
Set x, y, and z components to 0.
Definition: FrameVector.hpp:100
void operator*=(const T scale)
Times euals operator that performs runtime frame checks, .
Definition: FrameVector.hpp:238
FrameVector(ReferenceFramePtr referenceFrame, const Eigen::Vector3d &vector)
Constructor.
Definition: FrameVector.hpp:64
FrameVector changeFrameAndCopy(ReferenceFramePtr referenceFrame) const
copy into new frame vector and change the frame of that
Definition: FrameVector.hpp:90
void operator-=(const Vector3d &v)
Definition: FrameVector.hpp:201
void operator-=(const FrameVector &v)
Minus euals operator that performs runtime frame checks, .
Definition: FrameVector.hpp:224
virtual ~FrameVector()
Destructor.
Definition: FrameVector.hpp:71
FrameVector(ReferenceFramePtr referenceFrame, const double &x, const double &y, const double &z)
Constructor.
Definition: FrameVector.hpp:55
double dot(const FrameVector &frameVector) const
Dot product between two FrameVectors, i.e. .
Definition: FrameVector.hpp:147
The TransformableGeometricObject class is an essential interface because it forces all geometric obje...
Definition: rdl_eigenmath.hpp:43
Definition: rdl_eigenmath.hpp:54
void set(const Eigen::Vector3d &v)
Definition: rdl_eigenmath.hpp:82
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
FramePoint operator+(FramePoint p, const FrameVector &v)
Definition: FramePoint.hpp:404
std::vector< FrameVector, Eigen::aligned_allocator< FrameVector > > FrameVectorV
Definition: FrameVector.hpp:297
ForceVector operator*(const SpatialTransform &X, ForceVector f)
Operator for transforming a ForceVector. Calls the ForceVector::transform method.
Definition: ForceVector.hpp:248
Namespace for all structures of the RobotDynamics library.
Definition: examples.hpp:19