Parasol Planning Library (PPL)
Cfg.h
Go to the documentation of this file.
1 #ifndef PMPL_CFG_H_
2 #define PMPL_CFG_H_
3 
4 #include <cstddef>
5 #include <iostream>
6 #include <map>
7 #include <memory>
8 #include <vector>
9 
12 #include "Utilities/MPUtils.h"
13 
14 #include "Vector.h"
15 
16 class MultiBody;
17 class Boundary;
18 class Environment;
19 class Robot;
20 
21 enum class DofType;
22 
23 namespace mathtool {
24  class EulerAngle;
25  class Transformation;
26 }
27 
28 
38 class Cfg {
39 
40  public:
41 
44 
51  explicit Cfg();
52 
55  explicit Cfg(Robot* const _robot);
56 
61  explicit Cfg(const mathtool::Vector3d& _v, Robot* const _robot = nullptr);
62 
65  Cfg(const Cfg& _other);
66 
69  Cfg(Cfg&& _other);
70 
71  virtual ~Cfg();
72 
76 
79  Cfg& operator=(const Cfg& _cfg);
80 
83  Cfg& operator=(Cfg&& _cfg);
84 
87  Cfg& operator+=(const Cfg& _cfg);
90  Cfg& operator-=(const Cfg& _cfg);
93  Cfg& operator*=(const Cfg& _cfg);
96  Cfg& operator/=(const Cfg& _cfg);
99  Cfg& operator*=(const double _d);
102  Cfg& operator/=(const double _d);
103 
107 
109  Cfg operator-() const;
110 
115  Cfg operator+(const Cfg& _cfg) const;
116 
121  Cfg operator-(const Cfg& _cfg) const;
122 
127  Cfg operator*(const Cfg& _cfg) const;
128 
133  Cfg operator/(const Cfg& _cfg) const;
134 
139  Cfg operator*(const double _d) const;
140 
145  Cfg operator/(const double _d) const;
146 
150 
154  bool operator==(const Cfg& _cfg) const;
155 
159  bool operator!=(const Cfg& _cfg) const;
160 
167  bool WithinResolution(const Cfg& _cfg, const double _posRes,
168  const double _oriRes) const;
169 
173 
179  bool operator<(const Cfg& _cfg) const;
180 
184 
187  Robot* GetRobot() const noexcept;
188 
191  void SetRobot(Robot* const _r) noexcept;
192 
195  MultiBody* GetMultiBody() const noexcept;
196 
200  size_t DOF() const noexcept;
201 
205  size_t PosDOF() const noexcept;
206 
210  size_t OriDOF() const noexcept;
211 
215  size_t JointDOF() const noexcept;
216 
218  bool IsNonholonomic() const noexcept;
219 
223 
227  double& operator[](const size_t _dof) noexcept;
228  double operator[](const size_t _dof) const noexcept;
229 
232  const std::vector<double>& GetData() const noexcept;
233 
237  double& Velocity(const size_t _dof) noexcept;
238  double Velocity(const size_t _dof) const noexcept;
239 
242  const std::vector<double>& GetVelocity() const noexcept;
243 
246  virtual void SetData(const std::vector<double>& _data);
247  virtual void SetData(std::vector<double>&& _data);
248 
251  void SetJointData(const std::vector<double>& _data);
252 
254  Point3d GetPoint() const noexcept;
255 
257  virtual std::vector<double> GetPosition() const;
259  virtual std::vector<double> GetRotation() const;
261  virtual std::vector<double> GetJoints() const;
263  virtual std::vector<double> GetNonJoints() const;
265  virtual std::vector<double> GetOrientation() const;
266 
268  virtual double Magnitude() const;
270  virtual double PositionMagnitude() const;
272  virtual double OrientationMagnitude() const;
273 
275  mathtool::Vector3d GetLinearPosition() const;
277  mathtool::Vector3d GetAngularPosition() const;
279  mathtool::EulerAngle GetEulerAngle() const;
281  mathtool::Vector3d GetLinearVelocity() const;
283  mathtool::Vector3d GetAngularVelocity() const;
285  mathtool::Transformation GetBaseTransformation() const;
286 
288  void SetLinearPosition(const mathtool::Vector3d&);
290  void SetAngularPosition(const mathtool::Vector3d&);
292  void SetEulerAngle(const mathtool::EulerAngle&);
294  void SetLinearVelocity(const mathtool::Vector3d&);
296  void SetAngularVelocity(const mathtool::Vector3d&);
298  void SetBaseTransformation(const mathtool::Transformation&);
299 
301  void TransformCfg(const mathtool::Transformation&);
302 
308 
310  bool GetLabel(const std::string& _label) const;
312  bool IsLabel(const std::string& _label) const noexcept;
314  void SetLabel(const std::string& _label, const bool _value) noexcept;
315 
317  double GetStat(const std::string& _stat) const;
319  bool IsStat(const std::string& _stat) const noexcept;
321  void SetStat(const std::string& _stat, const double _value = 0) noexcept;
323  void IncrementStat(const std::string& _stat, const double _value = 1)
324  noexcept;
325 
329 
331  void Zero() noexcept;
332 
338  bool InBounds(const Boundary* const _b) const noexcept;
340  bool InBounds(const Environment* const _env) const noexcept;
341 
347  virtual void GetRandomCfg(const Boundary* const _b);
350  virtual void GetRandomCfg(Environment* _env);
351 
353  virtual void GetRandomVelocity();
354 
359  template <typename DistanceMetricPointer>
360  void GetRandomRay(const double _length, DistanceMetricPointer _dm,
361  const bool _norm = true);
362 
364  virtual void ConfigureRobot() const;
365 
370  virtual void IncrementTowardsGoal(const Cfg& _goal, const Cfg& _increment);
371 
377  virtual void FindIncrement(const Cfg& _start, const Cfg& _goal,
378  const int _nTicks);
379 
387  virtual void FindIncrement(const Cfg& _start, const Cfg& _goal, int* _nTicks,
388  const double _positionRes, const double _orientationRes);
389 
395  virtual void WeightedSum(const Cfg& _c1, const Cfg& _c2,
396  const double _weight = .5);
397 
402  virtual void GetPositionOrientationFrom2Cfg(const Cfg& _pos, const Cfg& _ori);
403 
407 
417  std::vector<double> DirectionInLocalFrame(const Cfg& _target) const;
418 
422 
423  // Static pointer for reading roadmaps. It should be set to the relevant
424  // robot before reading in the map file, and nullptr otherwise.
429  static Robot* inputRobot;
430 
433  virtual void Read(std::istream& _is);
434 
437  virtual void Write(std::ostream& _os) const;
438 
444  std::string PrettyPrint(const size_t _precision = 4) const;
445 
446  std::string ToString() const;
447 
453 
455  std::shared_ptr<Cfg> m_witnessCfg;
456 
460 
462  void EnableNormalization() const;
463 
465  void DisableNormalization() const;
466 
470  virtual void NormalizeOrientation(const int _index = -1) noexcept;
471 
473  void EnforceVelocityLimits() noexcept;
474 
476 
477  protected:
478 
481 
482  std::vector<double> m_dofs;
483  std::vector<double> m_vel;
484  Robot* m_robot{nullptr};
485 
486  std::map<std::string, bool> m_labelMap;
487  std::map<std::string, double> m_statMap;
488 
490  mutable double (*m_normalizer)(const double&){Normalize};
491 
493 
494 };
495 
496 
497 /*--------------------------- Generation Methods -----------------------------*/
498 
499 template <class DistanceMetricPointer>
500 void
502 GetRandomRay(const double _length, DistanceMetricPointer _dm, const bool _norm) {
503  // Randomly sample DOFs.
504  for(size_t i = 0; i < DOF(); ++i)
505  m_dofs[i] = GRand();
506 
507  // Scale to appropriate length.
508  _dm->ScaleCfg(_length, *this);
509 
510  // Normalize if requested.
511  if(_norm)
513 }
514 
515 /*----------------------------------------------------------------------------*/
516 
517 std::ostream& operator<<(std::ostream& _os, const Cfg& _cfg);
518 std::istream& operator>>(std::istream& _is, Cfg& _cfg);
519 
520 #endif
521 
std::ostream & operator<<(std::ostream &_os, const Cfg &_cfg)
Definition: Cfg.cpp:1280
std::istream & operator>>(std::istream &_is, Cfg &_cfg)
Definition: Cfg.cpp:1273
double Normalize(const double &_a)
Normalize a value into the range [-1,1).
Definition: MPUtils.cpp:58
double GRand(bool _reset)
Definition: MPUtils.cpp:13
DofType
Types of movement that are supported.
Definition: MultiBody.h:24
Definition: Boundary.h:30
Definition: Cfg.h:38
MultiBody * GetMultiBody() const noexcept
Definition: Cfg.cpp:431
void EnforceVelocityLimits() noexcept
Ensure that this Cfg respects the robot's velocity limits.
Definition: Cfg.cpp:1318
virtual void ConfigureRobot() const
Configure the robot with the DOF values of this configuration.
Definition: Cfg.cpp:989
virtual void GetPositionOrientationFrom2Cfg(const Cfg &_pos, const Cfg &_ori)
Definition: Cfg.cpp:1083
double & Velocity(const size_t _dof) noexcept
Definition: Cfg.cpp:496
virtual std::vector< double > GetNonJoints() const
Get a vector of the robot's non-joint DOFs.
Definition: Cfg.cpp:616
virtual std::vector< double > GetRotation() const
Get a vector of the robot's rotational DOFs.
Definition: Cfg.cpp:582
std::string ToString() const
Definition: Cfg.cpp:1263
mathtool::Vector3d GetAngularPosition() const
Get the euler vector rotation.
Definition: Cfg.cpp:669
std::vector< double > DirectionInLocalFrame(const Cfg &_target) const
Definition: Cfg.cpp:1096
void SetJointData(const std::vector< double > &_data)
Definition: Cfg.cpp:545
size_t OriDOF() const noexcept
Definition: Cfg.cpp:452
Robot * m_robot
The robot this cfg refers to.
Definition: Cfg.h:484
Cfg operator-() const
Find the negative of the configuration.
Definition: Cfg.cpp:261
bool IsNonholonomic() const noexcept
Is the robot nonholonomic?
Definition: Cfg.cpp:466
double(* m_normalizer)(const double &)
The function to use for normalizing orientation DOFs.
Definition: Cfg.h:490
virtual std::vector< double > GetPosition() const
Get a vector of the robot's positional DOFs.
Definition: Cfg.cpp:571
virtual void Read(std::istream &_is)
Definition: Cfg.cpp:1157
void SetStat(const std::string &_stat, const double _value=0) noexcept
Set a statistic given a string identifier.
Definition: Cfg.cpp:860
bool IsStat(const std::string &_stat) const noexcept
Does the robot have a statistic given a string identifier?
Definition: Cfg.cpp:853
mathtool::Vector3d GetAngularVelocity() const
Get the rotation velocity in R^3.
Definition: Cfg.cpp:709
void SetRobot(Robot *const _r) noexcept
Definition: Cfg.cpp:424
virtual std::vector< double > GetJoints() const
Get a vector of the robot's joint DOFs.
Definition: Cfg.cpp:604
void SetLinearPosition(const mathtool::Vector3d &)
Sets the robot's linear position.
Definition: Cfg.cpp:734
virtual void WeightedSum(const Cfg &_c1, const Cfg &_c2, const double _weight=.5)
Definition: Cfg.cpp:1068
virtual void GetRandomVelocity()
Randomly sample the velocity for this configuration.
Definition: Cfg.cpp:977
mathtool::Vector3d GetLinearPosition() const
Get the position in R^3.
Definition: Cfg.cpp:659
virtual void Write(std::ostream &_os) const
Definition: Cfg.cpp:1214
virtual void FindIncrement(const Cfg &_start, const Cfg &_goal, const int _nTicks)
Definition: Cfg.cpp:1029
void TransformCfg(const mathtool::Transformation &)
Transforms the cfg about a new frame of reference.
Definition: Cfg.cpp:812
bool IsLabel(const std::string &_label) const noexcept
Does the robot have a given string label?
Definition: Cfg.cpp:830
const std::vector< double > & GetData() const noexcept
Definition: Cfg.cpp:489
virtual ~Cfg()
void SetLabel(const std::string &_label, const bool _value) noexcept
Set the robot's string label given the string identifier.
Definition: Cfg.cpp:837
Robot * GetRobot() const noexcept
Definition: Cfg.cpp:417
bool InBounds(const Boundary *const _b) const noexcept
Definition: Cfg.cpp:887
Point3d GetPoint() const noexcept
Get the robot's reference point.
Definition: Cfg.cpp:561
std::vector< double > m_vel
The velocities, if any.
Definition: Cfg.h:483
size_t PosDOF() const noexcept
Definition: Cfg.cpp:445
mathtool::Transformation GetBaseTransformation() const
Get the world transformation of the robot's base.
Definition: Cfg.cpp:727
size_t DOF() const noexcept
Definition: Cfg.cpp:438
bool WithinResolution(const Cfg &_cfg, const double _posRes, const double _oriRes) const
Definition: Cfg.cpp:368
virtual void IncrementTowardsGoal(const Cfg &_goal, const Cfg &_increment)
Definition: Cfg.cpp:996
Cfg(const mathtool::Vector3d &_v, Robot *const _robot=nullptr)
virtual double Magnitude() const
Get the magnitude of the robot's DOF vector.
Definition: Cfg.cpp:627
virtual double PositionMagnitude() const
Get the magnitude of the robot's positional DOF vector.
Definition: Cfg.cpp:637
double GetStat(const std::string &_stat) const
Get a statistic given a string identifier.
Definition: Cfg.cpp:844
mathtool::EulerAngle GetEulerAngle() const
Get the euler angle rotation.
Definition: Cfg.cpp:678
Cfg operator*(const Cfg &_cfg) const
Definition: Cfg.cpp:285
void Zero() noexcept
Set all DOFs to zero.
Definition: Cfg.cpp:875
void SetLinearVelocity(const mathtool::Vector3d &)
Sets the robot's linear velocity.
Definition: Cfg.cpp:770
virtual std::vector< double > GetOrientation() const
Get a vector of the robot's orientational DOFs.
Definition: Cfg.cpp:593
Cfg & operator=(const Cfg &_cfg)
Definition: Cfg.cpp:73
Cfg & operator-=(const Cfg &_cfg)
Definition: Cfg.cpp:132
std::map< std::string, bool > m_labelMap
A map of labels for this cfg.
Definition: Cfg.h:486
static Robot * inputRobot
Definition: Cfg.h:429
mathtool::Vector3d GetLinearVelocity() const
Get the position velocity in R^3.
Definition: Cfg.cpp:696
bool operator<(const Cfg &_cfg) const
Definition: Cfg.cpp:346
void IncrementStat(const std::string &_stat, const double _value=1) noexcept
Increment a statistic given a string identifier.
Definition: Cfg.cpp:867
void SetAngularPosition(const mathtool::Vector3d &)
Sets the robot's angular position.
Definition: Cfg.cpp:742
virtual double OrientationMagnitude() const
Get the magnitude of the robot's orientational DOF vector.
Definition: Cfg.cpp:648
size_t JointDOF() const noexcept
Definition: Cfg.cpp:459
Cfg operator+(const Cfg &_cfg) const
Definition: Cfg.cpp:269
const std::vector< double > & GetVelocity() const noexcept
Definition: Cfg.cpp:510
void SetBaseTransformation(const mathtool::Transformation &)
Sets the robot's base tranformation.
Definition: Cfg.cpp:802
std::shared_ptr< Cfg > m_witnessCfg
Definition: Cfg.h:455
bool operator==(const Cfg &_cfg) const
Definition: Cfg.cpp:318
Cfg & operator+=(const Cfg &_cfg)
Definition: Cfg.cpp:109
std::map< std::string, double > m_statMap
A map of stats for this cfg.
Definition: Cfg.h:487
virtual void GetRandomCfg(const Boundary *const _b)
Definition: Cfg.cpp:902
void SetAngularVelocity(const mathtool::Vector3d &)
Sets the robot's angular velocity.
Definition: Cfg.cpp:781
std::vector< double > m_dofs
The DOF values.
Definition: Cfg.h:482
bool GetLabel(const std::string &_label) const
Get the robot's label for the given string identifier.
Definition: Cfg.cpp:823
void EnableNormalization() const
Enable the normalization of orientation DOFs.
Definition: Cfg.cpp:1289
Cfg operator/(const Cfg &_cfg) const
Definition: Cfg.cpp:293
bool operator!=(const Cfg &_cfg) const
Definition: Cfg.cpp:338
virtual void SetData(const std::vector< double > &_data)
Definition: Cfg.cpp:517
CDInfo m_clearanceInfo
Definition: Cfg.h:454
void GetRandomRay(const double _length, DistanceMetricPointer _dm, const bool _norm=true)
Definition: Cfg.h:502
void SetEulerAngle(const mathtool::EulerAngle &)
Sets the robot's euler angle.
Definition: Cfg.cpp:752
std::string PrettyPrint(const size_t _precision=4) const
Definition: Cfg.cpp:1238
Cfg & operator*=(const Cfg &_cfg)
Definition: Cfg.cpp:168
Cfg & operator/=(const Cfg &_cfg)
Definition: Cfg.cpp:191
void DisableNormalization() const
Disable the normalization of orientation DOFs.
Definition: Cfg.cpp:1296
virtual void NormalizeOrientation(const int _index=-1) noexcept
Definition: Cfg.cpp:1303
Cfg()
Definition: Cfg.cpp:19
Definition: Environment.h:137
Definition: MultiBody.h:65
Definition: Robot.h:31
Definition: Cfg.h:23
Definition: CDInfo.h:139