libmove3d-planners
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros Groups Pages
graph.hpp
1 #ifndef GRAPH_HPP
2 #define GRAPH_HPP
3 
4 #include "API/Roadmap/node.hpp"
5 #include "API/Roadmap/edge.hpp"
6 #include "API/Trajectory/trajectory.hpp"
7 
8 #ifndef COMPCO_HPP
10 #endif
11 
12 #include <map>
13 
20 #ifndef _ROADMAP_H
21 struct node;
22 struct edge;
23 struct graph;
24 struct list_node;
25 struct list_edge;
26 #endif
27 
28 class Graph
29 {
30 public:
31 
32  //contructors and destructor
33  Graph() {}
34  Graph(Robot* R, graph* ptGraph);
35  Graph(Robot* R);
36  Graph(graph* G);
37  Graph(const Graph& G);
38  ~Graph();
39 
42  bool isGraphChanged() { return m_graphChanged; }
43 
45  //graph* exportCppToGraphStruct(bool deleteGraphStruct = false);
46 
47  //Accessors
52  graph* getGraphStruct() const;
53 
58  void setGraph(graph* G);
59 
64  Robot* getRobot() const;
65 
69  void setRobot(Robot* R);
70 
75  std::map<node*, Node*> getNodesTable();
76 
81  unsigned int getNumberOfNodes();
82 
87  std::vector<Node*> getNodes() const;
88  std::vector<Node*> getSortedNodes() const;
89  std::vector<Node*> getNotTrajNodes() const;
90 
94  Node* getNode(unsigned int i) const { return m_Nodes[i]; }
95 
100  unsigned int getNumberOfEdges() const;
101 
106  std::vector<Edge*> getEdges() const;
107 
108  std::vector<Edge*> getEdges(std::tr1::shared_ptr<Configuration> q,bool from=false);
109  std::vector<Edge*> getEdges(Node* nq,bool from=false);
110 
114  Edge* getEdge(unsigned int i) const { return m_Edges[i]; }
115 
116  /*
117  * Get the number of compco in the grap
118  */
119  unsigned int getNumberOfCompco() const;
120 
125 
129  std::vector<ConnectedComponent*> getConnectedComponents() const;
130 
136  Node* getNode(node* N);
137 
142  Node* getLastNode();
143 
148  std::string getName();
149 
153  void setName();
154 
159  void setName(std::string Name);
160 
166  bool equal(Graph* G);
167 
173  bool equalName(Graph* G);
174 
180  bool isInGraph(Node* N);
181 
188  Edge* isEdgeInGraph(Node* N1,Node* N2);
189 
196 
201  void createCompco(Node* node);
202 
208  Node* insertNode(Node* node);
209 
210 
216  Node* insertExtremalNode(Node* node);
217 
219  void addNode(Node * node);
220 
229  Node* insertNode(Node* expansionNode, LocalPath& path);
230 
237  Node* insertConfigurationAsNode(std::tr1::shared_ptr<Configuration> q, Node* from, double step );
238 
242  void sortEdges();
243 
248  void updateSortedNodes(Node* node);
249  Node* getRandomNodeNearTraj();
250  void updateSepareNodes(Node* n);
251  void separateNodes();
252  std::map<double, Node*> getNodesOnTraj(Node* near,bool neighbour = false);
253  std::vector<Node*> getKNodesOnTraj(Node* near, uint k);
254  void sortNodes(double init = -1);
255 
263  Edge * addEdge(Node * source, Node * target, bool compute_length = true, double length = 0.,
264  bool compute_cost = true, double cost = 0.);
265 
271  Edge * addEdge(Node * source, Node * target, localPathPtr_t path);
272 
280  void addEdges(Node * N1, Node * N2, bool computeLength = true, double length = 0.,
281  bool computeCost = true, double cost = 0.);
282 
290  void addEdges(Node * N1, Node * N2, localPathPtr_t path);
291 
298  void addOrientedEdge(Node * N1, Node * N2, localPathPtr_t path);
299 
303  void removeEdge(Edge* E);
304 
308  void removeEdges( Node* N1, Node* N2 );
309 
314  void sortNodesByDist(Node* N);
315 
320  void sortNodesByDist(confPtr_t q);
321 
327  void addNode(Node* N, double maxDist);
328 
334  void addNodes(std::vector<Node*> N, double maxDist);
335 
337  void removeNode(Node * node);
338 
344  bool linkNode(Node* N);
345 
349  bool linkNodeCompMultisol(Node *N, ConnectedComponent* compPt);
350 
356  bool linkNodeWithoutDist(Node* N);
357 
363  bool linkNodeAtDist(Node* N);
364 
370  bool linkToAllNodes(Node* N);
371 
372 
379  bool areNodesLinked(Node* node1, Node* node2, double & dist);
380 
384  bool linkNodeAndMerge(Node* node1, Node* node2);
385 
392  void createRandConfs(int NMAX);
393 
397  Node* randomNodeFromComp(Node* comp);
398 
403  bool connectNodeToCompco(Node* node, Node* compco);
404 
405 
410  static void sortNodesByDist(std::vector<Node*>& nodes, confPtr_t config);
411 
416  std::vector<Node*> KNearestWeightNeighbour(confPtr_t q, int K, double radius, bool weighted,
417  int distConfigChoice);
418 
419  std::vector<Node *> kNearestNeighbours(unsigned k, confPtr_t conf);
420 
425  std::vector<Node *> neighborsInBall(confPtr_t conf, double radius);
426 
435  Node* nearestWeightNeighbour(Node* compco, confPtr_t C, bool weighted, int distConfigChoice);
436 
440  Node* getCompco(unsigned int ith);
441 
445  std::vector<Node*> getNodesInTheCompCo(Node* node);
446 
452  void addCycles(Node* node, double step);
453 
455  void mergeComponents(Node * N1, Node * N2);
456 
457  /*fonction mergeant les compco N1 et N2*/
465  int mergeComp(Node* CompCo1, Node* CompCo2, double DistNodes, bool compute_edge_cost);
466 
472 
476  void mergeCheck();
477 
481  void removeCompco(ConnectedComponent* CompCo);
482 
487 
491  void recomputeCost();
492 
496  bool checkAllEdgesValid();
497 
501  API::Trajectory* extractBestTrajSoFar(confPtr_t qi, confPtr_t qf);
502 
506  std::pair<bool, std::vector<Node*> > extractBestNodePathSoFar(confPtr_t qi, confPtr_t qf);
507 
510 
512  std::vector<double> computeDijkstraShortestPaths(Node * n);
513  std::vector<double> computeDijkstraShortestPaths(Node * n, std::map<double, Node *> to);
514 
516  std::vector<Node *> extractAStarShortestNodePaths(Node * initN, Node * endN);
517  std::vector<Node *> extractAStarShortestNodePaths(std::vector<Node *> passingBy);
518 
520  std::vector<Node *> extractAStarShortestNodePaths(confPtr_t initC, confPtr_t endC);
521 
524 
526  API::Trajectory * extractAStarShortestPath(confPtr_t initC, confPtr_t endC);
527 
531  double getAStarShortestPathLength(confPtr_t initC, confPtr_t endC);
532 
534  API::Trajectory * extractBestTraj(confPtr_t qi, confPtr_t qf);
535 
537  void initMotionPlanning(node* start, node* goal);
538 
539  // BGL functions
540  BGL_Vertex findVertexDescriptor(Node* N);
541  BGL_Edge findEdgeDescriptor(Edge* E);
542  void saveBGLGraphToDotFile(const std::string& filename);
543  BGL_Graph& get_BGL_Graph() { return m_BoostGraph; }
544 
545  void draw();
546 
547  // Used for directed graphs
548  void resetConnections();
549  void unflagAll();
550  unsigned int getNbScc();
551  std::vector<std::vector<Node*> > getStronglyConnectedComponents();
552  std::vector<double> sortSCC(unsigned int id,Node *N1,bool forward);
553  void addScc(Node *node);
554  bool areSccConnected(unsigned int i,unsigned int j);
555  void connectScc(unsigned int i,unsigned int j);
556  bool mergeScc(unsigned int i,unsigned int j);
557 
558 private:
559 
560  void init();
561 
562  // BGL functions
563  void initBGL();
564  void unsetDescriptors();
565  void drawEdge(BGL_Vertex v1, BGL_Vertex v2);
566  void drawNode(BGL_Vertex v);
567 
568  void deleteGraphStruct();
569  void updateCompcoFromStruct();
570  void freeResources();
571  static bool compareEdges(Edge* E1, Edge* E2);
572  static bool compareNodes(Node* N1, Node* N2);
573 
574  // Old p3d graph
575  graph* m_Graph;
576  Robot* m_Robot;
577 
578  // Boost Graph Lib
579  BGL_Graph m_BoostGraph;
580 
581  // Flag that is set to false
582  // each time the graph is exported to p3d_graph
583  bool m_graphChanged;bool m_initBGL;
584 
585  std::vector<Node*> m_Nodes;
586  std::vector<Node*> m_SortedNodes;
587  std::vector<Node*> m_tempTrajNodes;
588  std::vector<Node*> m_TrajNodes;
589  std::vector<Node*> m_NotTrajNodes;
590  std::vector<Node*> m_NeighbourNodes;
591 
592  std::vector<Edge*> m_Edges;
593  std::vector<ConnectedComponent*> m_Comp;
594 
595  // Maps between old and new nodes
596  std::map<node*, Node*> m_NodesTable;
597 
598  // Graph name
599  std::string m_Name;
600 
601  std::map<Node*,bool> m_UpdateList;
602 
603  // list of the strongly connected components
604  std::vector<std::vector<Node*> > m_StronglyConnectedComponents;
605  // adjacency matrix of the transitive closure of the graph of the strongly connected components
606  std::vector<std::vector<bool> > m_Scc_TransitiveClosure;
607 
608 };
609 
610 extern Graph* API_activeGraph;
611 
612 #endif
Node * insertConfigurationAsNode(std::tr1::shared_ptr< Configuration > q, Node *from, double step)
Link a Configuration to a Node for the RRT Planner.
Definition: graph.cpp:789
Node * getLastNode()
obtient le dernier Node ajouté au Graph
Definition: graph.cpp:649
ConnectedComponent * getConnectedComponent(int ith) const
Get Compco.
Definition: graph.cpp:2017
Definition: compco.hpp:23
Node * searchConf(Configuration &q)
Gets the node that corresponds to a configuration.
Definition: graph.cpp:685
void mergeComponents(Node *N1, Node *N2)
Merge the connected components to which N1 and N2 belong.
Definition: graph.cpp:1961
bool checkAllEdgesValid()
Recompute All Edges Valid.
Definition: graph.cpp:2277
Node * randomNodeFromComp(Node *comp)
Gets Random node in the connected component.
Definition: graph.cpp:1706
Edge * isEdgeInGraph(Node *N1, Node *N2)
Looks if two Node make an edge in the Graph.
Definition: graph.cpp:1058
Classe représentant un Node d'un Graph.
Definition: node.hpp:39
void createRandConfs(int NMAX)
crée des Node à des Configurations aléatoires
Definition: graph.cpp:1656
API::Trajectory * extractTreeShortestPath(Node *initN, Node *endN)
Return a trajectory (i.e. the shortest path) from initN to endN, using parenthood in a tree...
Definition: graph.cpp:2371
graph * getGraphStruct() const
Creates a p3d_graph from Boost Graph.
Definition: graph.cpp:519
Edge * addEdge(Node *source, Node *target, bool compute_length=true, double length=0., bool compute_cost=true, double cost=0.)
ajoute un Edge au Graph
Definition: graph.cpp:1074
void removeEdge(Edge *E)
Remove edge.
Definition: graph.cpp:1260
Classe représentant un chemin local.
Definition: localpath.hpp:15
unsigned int getNumberOfNodes()
obtient le nombre de Node dans le Graph
Definition: graph.cpp:539
std::vector< Node * > KNearestWeightNeighbour(confPtr_t q, int K, double radius, bool weighted, int distConfigChoice)
K Nearest Weighted Neighbors in graph.
Definition: graph.cpp:1826
void mergeCheck()
teste si des composantes connexes doivent être merger et le fait le cas échéant
Definition: graph.cpp:2103
void removeCompco(ConnectedComponent *CompCo)
Delete the compco.
Definition: graph.cpp:2152
bool isInGraph(Node *N)
vérifie si un node est dans le Graph
Definition: graph.cpp:1378
bool linkNodeAndMerge(Node *node1, Node *node2)
Link 2 nodes and merge them.
Definition: graph.cpp:1638
bool linkToAllNodes(Node *N)
lie un Node à tous les autres Nodes visibles
Definition: graph.cpp:1576
void addCycles(Node *node, double step)
ajoute des Edges formant des cycles dans le Graph
Definition: graph.cpp:2215
API::Trajectory * extractBestTrajSoFar(confPtr_t qi, confPtr_t qf)
Extract best traj from qi that is closest to q_f.
Definition: graph.cpp:2298
void addNodes(std::vector< Node * > N, double maxDist)
ajoute des Node s'ils sont à moins d'une distance max du Graph
Definition: graph.cpp:1366
Definition: graph.hpp:28
Node * nearestWeightNeighbour(Node *compco, confPtr_t C, bool weighted, int distConfigChoice)
obtient le plus proche voisin d'une composante connexe
Definition: graph.cpp:1915
std::vector< ConnectedComponent * > getConnectedComponents() const
Get Compcos.
Definition: graph.cpp:623
Classe représentant une Edge d'un Graph.
Definition: edge.hpp:24
std::vector< Node * > getNodes() const
obtient le vecteur des Node du Graph
Definition: graph.cpp:573
This class holds a the robot represented by a kinematic chain.
Definition: robot.hpp:42
Node * insertExtremalNode(Node *node)
insert un Node de type ISOLATE
Definition: graph.cpp:709
std::pair< bool, std::vector< Node * > > extractBestNodePathSoFar(confPtr_t qi, confPtr_t qf)
Extract best vector of nodes from qi that is closest to q_f.
Definition: graph.cpp:2324
Node * insertNode(Node *node)
insert un Node dans le Graph
Definition: graph.cpp:700
void removeNode(Node *node)
Remove the given node from the graph.
Definition: graph.cpp:1308
int rebuildCompcoFromBoostGraph()
Rebuild the connected component using the strongly connected component algo from the BGL...
Definition: graph.cpp:2046
Robot * getRobot() const
obtient le Robot pour lequel le Graph est créé
Definition: graph.cpp:529
void sortEdges()
trie les Edges en fonction de leur longueur
Definition: graph.cpp:881
void addEdges(Node *N1, Node *N2, bool computeLength=true, double length=0., bool computeCost=true, double cost=0.)
ajoute deux Edge au Graph
Definition: graph.cpp:1139
bool equal(Graph *G)
teste si deux Graph sont égaux en comparant leur listNode
Definition: graph.cpp:669
std::vector< Node * > neighborsInBall(confPtr_t conf, double radius)
Return the nodes whose configurations are within a ball (in the C-space) having the given radius and ...
Definition: graph.cpp:1892
bool checkConnectedComponents()
Check C and C++ Connected Components.
Definition: graph.cpp:2184
std::vector< Node * > extractAStarShortestNodePaths(Node *initN, Node *endN)
Return the shortest path (as a list of nodes) from initN to endN, using A*.
Definition: graph.cpp:2486
double getAStarShortestPathLength(confPtr_t initC, confPtr_t endC)
Return the lenght of the shortest path (using A*) in the graph between initC and endC Return P3D_HUDG...
Definition: graph.cpp:2627
Node * getNode(unsigned int i) const
Get Node ith node in Graph.
Definition: graph.hpp:94
bool linkNode(Node *N)
lie un Node au Graph
Definition: graph.cpp:1391
void connectScc(unsigned int i, unsigned int j)
Adds an edge in the transitive closure of the graph of the strongly connected components from strongl...
Definition: graph.cpp:3105
API::Trajectory * extractBestTraj(confPtr_t qi, confPtr_t qf)
Extract best traj.
Definition: graph.cpp:2678
unsigned int getNumberOfEdges() const
obtient le nombre de Node dans le Graph
Definition: graph.cpp:556
int mergeComp(Node *CompCo1, Node *CompCo2, double DistNodes, bool compute_edge_cost)
merge deux composantes connexes
Definition: graph.cpp:1978
Definition: trajectory.hpp:40
void setRobot(Robot *R)
sets the Robot of the Graph
Definition: graph.cpp:204
Edge * getEdge(unsigned int i) const
Returns the ith edge.
Definition: graph.hpp:114
bool linkNodeCompMultisol(Node *N, ConnectedComponent *compPt)
Links a node to a compco with multisol.
Definition: graph.cpp:1436
void updateSortedNodes(Node *node)
trie les Nodes en fonction de leur distance à la position du robot via la trajectoire courante ...
Definition: graph.cpp:890
API::Trajectory * extractAStarShortestPath(Node *initN, Node *endN)
Return a trajectory (i.e. the shortest path) from initN to endN, using A*.
Definition: graph.cpp:2601
void recomputeCost()
Recompute the Graph cost (Edges and Nodes)
Definition: graph.cpp:2261
bool isGraphChanged()
return the value of the Change flag
Definition: graph.hpp:42
void setName()
modifie le nom du Graph; un nom standard est alors mis
Definition: graph.cpp:659
std::vector< Edge * > getEdges() const
obtient le vecteur des Edge du Graph
Definition: graph.cpp:588
std::vector< double > computeDijkstraShortestPaths(Node *n)
Compute the lengths of all the shortest paths having for source the node n, using Dijkstra...
Definition: graph.cpp:2402
void sortNodesByDist(Node *N)
Sort nodes against one given node.
Definition: graph.cpp:866
bool areNodesLinked(Node *node1, Node *node2, double &dist)
Function that computes if a node is linked to another node.
Definition: graph.cpp:1585
bool equalName(Graph *G)
teste si deux Graph ont le même nom
Definition: graph.cpp:674
bool linkNodeWithoutDist(Node *N)
Lie un node au Graph sans prendre en compte la distance max.
Definition: graph.cpp:1406
void createCompco(Node *node)
New Compco.
Definition: graph.cpp:2009
Classe représentant une Configuration d'un Robot.
Definition: configuration.hpp:25
void addScc(Node *node)
Adds the singleton {node} to the list of the strongly connected components.
Definition: graph.cpp:3066
std::map< node *, Node * > getNodesTable()
obtient la table des noeuds du Graph
Definition: graph.cpp:628
std::vector< Node * > getNodesInTheCompCo(Node *node)
Vector of nodes in the same compco.
Definition: graph.cpp:2033
void addOrientedEdge(Node *N1, Node *N2, localPathPtr_t path)
Add the oriented edge N1->N2.
Definition: graph.cpp:1209
std::string getName()
obtient le nom du Graph
Definition: graph.cpp:654
bool mergeScc(unsigned int i, unsigned int j)
Merges strongly connected component (scc) of index i with scc of idex j.
Definition: graph.cpp:3154
void removeEdges(Node *N1, Node *N2)
Remove the edges between the nodes N1 and N2.
Definition: graph.cpp:1272
void addNode(Node *node)
Add the given node to the storing structure of the graph (without creating any link).
Definition: graph.cpp:719
void initMotionPlanning(node *start, node *goal)
Init Motion planning problem.
Definition: graph.cpp:2804
bool linkNodeAtDist(Node *N)
Lie un Node au Graph en prenant en compte la distance max.
Definition: graph.cpp:1531
void setGraph(graph *G)
stocke la structure p3d_graph
Definition: graph.cpp:524
bool connectNodeToCompco(Node *node, Node *compco)
Function that trys to connect a node to a given connected component.
Definition: graph.cpp:1717
bool areSccConnected(unsigned int i, unsigned int j)
Returns true if in the transitive closure of the graph of the strongly connected components, the strongly connected component (scc) of index i is connected to the scc j.
Definition: graph.cpp:3093
Node * getCompco(unsigned int ith)
Get Ith Compco.
Definition: graph.cpp:2025