void IConnectionLoader::addConnection( VertexMap& vertices, SystemTypeGetter* info, int from, int to, TypeSet tags) { TypeSet toType, fromType; toType.insert(info->getType(to)); toType.insert(tags.begin(), tags.end()); fromType.insert(info->getType(from)); fromType.insert(tags.begin(), tags.end()); //Add vertices if they dont exists already //this will fail if it already exists vertices.insert(std::make_pair(from, Vertex(from))); vertices.insert(std::make_pair(to, Vertex(to))); //Add edges to the vertices vertices.find(from)->second.addEdge(to, toType); vertices.find(to)->second.addEdge(from, fromType); }
bool vtkPolyData_to_polygon_mesh(vtkPolyData* poly_data, TM& tmesh, VertexMap& vertex_map, FaceMap& face_map) { typedef typename boost::property_map<TM, CGAL::vertex_point_t>::type VPMap; typedef typename boost::property_map_value<TM, CGAL::vertex_point_t>::type Point_3; typedef typename boost::graph_traits<TM>::vertex_descriptor vertex_descriptor; typedef typename boost::graph_traits<TM>::face_descriptor face_descriptor; VPMap vpmap = get(CGAL::vertex_point, tmesh); // get nb of points and cells vtkIdType nb_points = poly_data->GetNumberOfPoints(); vtkIdType nb_cells = poly_data->GetNumberOfCells(); //extract points for (vtkIdType i = 0; i<nb_points; ++i) { double coords[3]; poly_data->GetPoint(i, coords); vertex_descriptor v = add_vertex(tmesh); put(vpmap, v, Point_3(coords[0], coords[1], coords[2])); vertex_map.insert(std::make_pair(i, v)); } //extract cells for (vtkIdType i = 0; i<nb_cells; ++i) { vtkCell* cell_ptr = poly_data->GetCell(i); vtkIdType nb_vertices = cell_ptr->GetNumberOfPoints(); if (nb_vertices != 3){ std::cerr << "Error a cell with " << nb_vertices << " found\n"; return false; } std::vector<vertex_descriptor> vr; vr.push_back(vertex_map[cell_ptr->GetPointId(0)]); vr.push_back(vertex_map[cell_ptr->GetPointId(1)]); vr.push_back(vertex_map[cell_ptr->GetPointId(2)]); face_descriptor f = CGAL::Euler::add_face(vr, tmesh); face_map.insert(std::make_pair(i, f)); } return true; }
typename boost::graph_traits<Graph>::vertex_descriptor add_named_vertex(Graph& g, NameMap nm, const std::string& name, VertexMap& vm) { typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex; typedef typename VertexMap::iterator Iterator; Vertex v; Iterator iter; bool inserted; tie(iter, inserted) = vm.insert(make_pair(name, Vertex())); if(inserted) { // The name was unique so we need to add a vertex to the graph v = add_vertex(g); iter->second = v; put(nm, v, name); // store the name in the name map } else { // We had alread inserted this name so we can return the // associated vertex. v = iter->second; } return v; }
/** * This is a straight forward C++ implementation of Wikipedia's A* pseudo code. * As proof of this the pseudo code is embedded in the source, just above the * C++ statements. * * See http://en.wikipedia.org/wiki/A*#Pseudocode for the complete pseudo code * * But also see the discussion page: it explains why the algorithm is only * correct if the cost function is monotone.This algorithm does not give the * correct solution in our case. The routes and hence costs differ whether we * go from (0,0) to (10,10) or vice versa: * * 0-0 -> 3-2 -> 4-5 -> 4-8 -> 6-9 -> 10-10, cost = 15 * * 10-10 -> 10-7 -> 7-5 -> 6-3 -> 3-2 -> 0-0, cost = 14 * * Use this piece of code as inspiration only. * */ std::vector<Vertex> WikiAStar( Vertex start, Vertex goal) { ClosedSet closedSet; // The set of nodes already evaluated. OpenSet openSet; // The set of tentative nodes to be evaluated, initially containing the start node VertexMap predecessorMap; // The map of navigated nodes. start.actualCost = 0.0; // Cost from start along best known path. start.heuristicCost = start.actualCost + HeuristicCost(start, goal); // Estimated total cost from start to goal through y. openSet.insert(start); //while openset is not empty while(!openSet.empty()) { // current := the node in openset having the lowest f_score[] value Vertex current = *openSet.begin(); // if current = goal if(current == goal) { // return reconstruct_path(came_from, goal) return ConstructPath(predecessorMap,current); } // remove current from openset openSet.erase(current); // add current to closedset closedSet.insert(current); //for each neighbour in neighbour_nodes(current) std::vector< Vertex > neighbours = GetNeighbours(current); for(Vertex neighbour : neighbours) { // if neighbor in closedset continue (with next neighbour) if(closedSet.find(neighbour) != closedSet.end()) { continue; } // tentative_g_score := g_score[current] + dist_between(current,neighbour) double calculatedActualNeighbourCost = current.actualCost + ActualCost(current,neighbour); // if neighbour not in openset or tentative_g_score < g_score[neighbour] if(openSet.find(neighbour) == openSet.end() || calculatedActualNeighbourCost < neighbour.actualCost) { // Here we deviate from the Wikipedia article, because of the map semantics: // we cannot change the object's key values once it in the map so we first // set the vales and then put it into the map. // g_score[neighbor] := tentative_g_score neighbour.actualCost = calculatedActualNeighbourCost; // f_score[neighbor] := g_score[neighbor] + heuristic_cost_estimate(neighbor, goal) neighbour.heuristicCost = neighbour.actualCost + HeuristicCost(neighbour, goal); // came_from[neighbor] := current std::pair<VertexMap::iterator,bool> result = predecessorMap.insert(std::make_pair(neighbour,current)); // The following if-statement is not part of the pseudo code but a partial fix for a semantic difference // in the map of the pseudo code and the c++ std::map if(result.second==false) (*result.first).second = current; // if neighbor not in openset if(openSet.find(neighbour) == openSet.end()) { // add neighbor to openset openSet.insert(neighbour); } } } } //return failure return std::vector<Vertex>(); }
void Graph::addVertex(int v) { vertices.insert( std::make_pair(v, Vertex(v)) ); }