DART  6.10.1
RRT.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2021, The DART development contributors
3  * All rights reserved.
4  *
5  * The list of contributors can be found at:
6  * https://github.com/dartsim/dart/blob/master/LICENSE
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above
13  * copyright notice, this list of conditions and the following
14  * disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials
18  * provided with the distribution.
19  * * Neither the name of the Georgia Tech Research Corporation nor
20  * the names of its contributors may be used to endorse or
21  * promote products derived from this software without specific
22  * prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY GEORGIA TECH RESEARCH CORPORATION ''AS
25  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GEORGIA
28  * TECH RESEARCH CORPORATION BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
35  * OF THE POSSIBILITY OF SUCH DAMAGE.
36  *
37  */
38 
47 #ifndef DART_PLANNING_RRT_HPP_
48 #define DART_PLANNING_RRT_HPP_
49 
50 #include <list>
51 #include <vector>
52 #include <Eigen/Core>
53 
56 
57 namespace flann {
58 template <class A>
59 class L2;
60 template <class A>
61 class Index;
62 } // namespace flann
63 
64 namespace dart {
65 
66 namespace simulation {
67 class World;
68 }
69 namespace dynamics {
70 class Skeleton;
71 }
72 
73 namespace planning {
74 
76 class RRT
77 {
78 public:
80  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
81 
83  typedef enum
84  {
85  STEP_COLLISION, // Collided with obstacle. No node added.
86  STEP_REACHED, // The target node is closer than step size (so reached). No
87  // node added.
88  STEP_PROGRESS // One node added.
90 
91 public:
92  // Initialization constants and search variables
93 
94  const int
95  ndim;
96  const double stepSize;
97 
98  int activeNode;
99  std::vector<int> parentVector;
101 
103  // NOTE We are using pointers for the VectorXd's because flann copies the
104  // pointers for the data points and we give it the copies made in the heap
105  std::vector<const Eigen::VectorXd*> configVector;
106 
107 public:
111  const std::vector<std::size_t>& dofs,
112  const Eigen::VectorXd& root,
113  double stepSize = 0.02);
114 
118  const std::vector<std::size_t>& dofs,
119  const std::vector<Eigen::VectorXd>& roots,
120  double stepSize = 0.02);
121 
123  virtual ~RRT()
124  {
125  }
126 
129  bool connect();
130 
133  bool connect(const Eigen::VectorXd& target);
134 
138 
141  StepResult tryStep(const Eigen::VectorXd& qtry);
142 
144  virtual StepResult tryStepFromNode(const Eigen::VectorXd& qtry, int NNidx);
145 
151  virtual bool newConfig(
152  std::list<Eigen::VectorXd>& intermediatePoints,
153  Eigen::VectorXd& qnew,
154  const Eigen::VectorXd& qnear,
155  const Eigen::VectorXd& qtarget);
156 
160  double getGap(const Eigen::VectorXd& target);
161 
164  void tracePath(
165  int node, std::list<Eigen::VectorXd>& path, bool reverse = false);
166 
168  std::size_t getSize();
169 
171  virtual bool checkCollisions(const Eigen::VectorXd& c);
172 
174  virtual Eigen::VectorXd getRandomConfig();
175 
176 protected:
180  std::vector<std::size_t>
182 
185 
187  double randomInRange(double min, double max);
188 
190  virtual int getNearestNeighbor(const Eigen::VectorXd& qsamp);
191 
193  virtual int addNode(const Eigen::VectorXd& qnew, int parentId);
194 };
195 
196 } // namespace planning
197 } // namespace dart
198 
199 #endif // DART_PLANNING_RRT_HPP_
The rapidly-expanding random tree implementation.
Definition: RRT.hpp:77
const double stepSize
Step size at each node creation.
Definition: RRT.hpp:96
virtual int addNode(const Eigen::VectorXd &qnew, int parentId)
Adds a new node to the tree.
Definition: RRT.cpp:197
std::vector< int > parentVector
The ith node in configVector has parent with index pV[i].
Definition: RRT.hpp:99
const int ndim
Number of dof we can manipulate (may be less than robot's)
Definition: RRT.hpp:95
StepResult tryStep()
Try a single step with the given "stepSize" to a random configuration.
Definition: RRT.cpp:138
StepResult
To get byte-aligned Eigen vectors.
Definition: RRT.hpp:84
@ STEP_PROGRESS
Definition: RRT.hpp:88
@ STEP_REACHED
Definition: RRT.hpp:86
@ STEP_COLLISION
Definition: RRT.hpp:85
flann::Index< flann::L2< double > > * index
The underlying flann data structure for fast nearest neighbor searches.
Definition: RRT.hpp:184
virtual bool newConfig(std::list< Eigen::VectorXd > &intermediatePoints, Eigen::VectorXd &qnew, const Eigen::VectorXd &qnear, const Eigen::VectorXd &qtarget)
Checks if the given new configuration is in collision with an obstacle.
Definition: RRT.cpp:186
int activeNode
Last added node or the nearest node found after a search.
Definition: RRT.hpp:98
RRT(dart::simulation::WorldPtr world, dart::dynamics::SkeletonPtr robot, const std::vector< std::size_t > &dofs, const Eigen::VectorXd &root, double stepSize=0.02)
virtual Eigen::VectorXd getRandomConfig()
Returns a random configuration with the specified node IDs.
Definition: RRT.cpp:253
RRT(simulation::WorldPtr world, dynamics::SkeletonPtr robot, const std::vector< std::size_t > &dofs, const std::vector< Eigen::VectorXd > &roots, double stepSize=0.02)
Constructor with multiple roots (so, multiple trees)
dynamics::SkeletonPtr robot
The ID of the robot for which a plan is generated.
Definition: RRT.hpp:179
double getGap(const Eigen::VectorXd &target)
Returns the distance between the current active node and the given node.
Definition: RRT.cpp:269
StepResult tryStep(const Eigen::VectorXd &qtry)
Try a single step with the given "stepSize" to the given configuration.
simulation::WorldPtr world
The world that the robot is in.
Definition: RRT.hpp:177
virtual int getNearestNeighbor(const Eigen::VectorXd &qsamp)
Returns the nearest neighbor to query point.
Definition: RRT.cpp:220
std::size_t getSize()
Returns the number of nodes in the tree.
Definition: RRT.cpp:301
virtual bool checkCollisions(const Eigen::VectorXd &c)
Implementation-specific function for checking collisions.
Definition: RRT.cpp:293
bool connect()
Reach for a random node by repeatedly extending nodes from the nearest neighbor in the tree.
Definition: RRT.cpp:112
std::vector< std::size_t > dofs
The dofs of the robot the planner can manipulate.
Definition: RRT.hpp:181
bool connect(const Eigen::VectorXd &target)
Reach for a target by repeatedly extending nodes from the nearest neighbor.
std::vector< const Eigen::VectorXd * > configVector
All visited configs.
Definition: RRT.hpp:105
void tracePath(int node, std::list< Eigen::VectorXd > &path, bool reverse=false)
Traces the path from some node to the initConfig node - useful in creating the full path after the go...
Definition: RRT.cpp:276
virtual ~RRT()
Destructor.
Definition: RRT.hpp:123
double randomInRange(double min, double max)
Returns a random value between the given minimum and maximum value.
Definition: RRT.cpp:241
virtual StepResult tryStepFromNode(const Eigen::VectorXd &qtry, int NNidx)
Tries to extend tree towards provided sample.
Definition: RRT.cpp:154
Definition: RRT.hpp:61
Definition: RRT.hpp:59
std::shared_ptr< Skeleton > SkeletonPtr
Definition: SmartPointer.hpp:60
std::shared_ptr< World > WorldPtr
Definition: SmartPointer.hpp:41
Definition: BulletCollisionDetector.cpp:65
Definition: RRT.hpp:57