Roadmap.cpp
Go to the documentation of this file.
00001 // -*- mode: c++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
00002 #include "PathPlanner.h"
00003 #include "Mobility.h"
00004 #include "RoadmapNode.h"
00005 #include "Roadmap.h"
00006 
00007 using namespace PathEngine;
00008 
00009 void Roadmap::clear()
00010 {
00011     nodes_.clear();
00012     m_nEdges = 0;
00013 }
00014 
00015 Roadmap::~Roadmap()
00016 {
00017     clear();
00018 }
00019 
00020 void Roadmap::addEdge(RoadmapNodePtr from, RoadmapNodePtr to)
00021 {
00022     from->addChild(to);
00023     to->addParent(from); 
00024     m_nEdges++;
00025 }
00026 
00027 void Roadmap::integrate(RoadmapPtr rdmp)
00028 {
00029     for (unsigned int i=0; i<nodes_.size(); i++){
00030         rdmp->addNode(nodes_[i]);
00031     }
00032     nodes_.clear();
00033 }
00034 
00035 RoadmapNodePtr Roadmap::node(unsigned int index)
00036 {
00037     if (index >= nodes_.size()) return RoadmapNodePtr();
00038 
00039     return nodes_[index];
00040 }
00041 
00042 void Roadmap::findNearestNode(const Configuration& pos,
00043                               RoadmapNodePtr & node, double &distance)
00044 {
00045     if (nodes_.size() == 0){
00046         node = RoadmapNodePtr();
00047         return;
00048     }
00049 
00050     node = nodes_[0];
00051     Mobility *mobility = planner_->getMobility();
00052     distance = mobility->distance(node->position(), pos);
00053     
00054     double d;
00055     for (unsigned int i=1; i<nodes_.size(); i++) {
00056         d = mobility->distance(nodes_[i]->position(), pos);
00057         if (d < distance) {
00058             distance = d;
00059             node = nodes_[i];
00060         }
00061     }
00062 }
00063 
00064 RoadmapNodePtr Roadmap::lastAddedNode()
00065 {
00066     if (nodes_.size() == 0) return RoadmapNodePtr();
00067     return nodes_[nodes_.size()-1];
00068 }
00069 
00070 int Roadmap::indexOfNode(RoadmapNodePtr node)
00071 {
00072     for (unsigned int i=0; i<nodes_.size(); i++){
00073         if (nodes_[i] == node) return (int)i; 
00074     }
00075     return -1;
00076 }
00077 
00078 std::vector<RoadmapNodePtr > Roadmap::DFS(RoadmapNodePtr startNode, RoadmapNodePtr goalNode) 
00079 {
00080     for (unsigned int i=0; i<nodes_.size(); i++) {
00081         nodes_[i]->visited(false);
00082     }
00083 
00084     std::vector<RoadmapNodePtr> path;
00085     startNode->visited(true);
00086     path.push_back(startNode);
00087     while (path.size() > 0) {
00088         RoadmapNodePtr node = path.back();
00089 
00090         if (node == goalNode) {
00091             break;
00092         } else {
00093             RoadmapNodePtr child = RoadmapNodePtr();
00094             for (unsigned int i=0; i<node->nChildren(); i++) {
00095                 if (!node->child(i)->visited()) {
00096                     child = node->child(i);
00097                     break;
00098                 }
00099             }
00100             if (child == NULL) {
00101                 path.pop_back();
00102             } else {
00103                 child->visited(true);
00104                 path.push_back(child);
00105             }
00106         }
00107     }
00108 
00109     return path;
00110 }
00111 
00112 void Roadmap::tryConnection(RoadmapNodePtr from, RoadmapNodePtr to, bool tryReverse)
00113 {
00114     Mobility *mobility = planner_->getMobility();
00115     if (mobility->isReachable(from->position(), to->position())){
00116         addEdge(from, to);
00117         if (tryReverse && mobility->isReversible()) addEdge(to, from);
00118     }
00119     if (tryReverse && !mobility->isReversible()){
00120         if (mobility->isReachable(to->position(), from->position())){
00121             addEdge(to, from);
00122         }
00123     }
00124 }
00125 
00126 bool Roadmap::removeEdge(RoadmapNodePtr from, RoadmapNodePtr to)
00127 {
00128     return from->removeChild(to) && to->removeParent(from);
00129 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Apr 11 2019 03:30:19