IndexArrayPtr PGL::determine_faces_from_edges(const Point2ArrayPtr& points, const std::vector<std::pair<uint32_t, uint32_t> >& edges) { typedef Wm5::PlanarGraph<Vector2> Graph; Graph graph; int i = 0; for (Point2Array::const_iterator it = points->begin(); it != points->end(); ++it) graph.InsertVertex(*it,i++); for (std::vector<std::pair<uint32_t, uint32_t> >::const_iterator itedge = edges.begin(); itedge != edges.end(); ++itedge) graph.InsertEdge(itedge->first,itedge->second); std::vector<Graph::Primitive*> mPrimitives; graph.ExtractPrimitives(mPrimitives); IndexArrayPtr result(new IndexArray()); for (std::vector<Graph::Primitive*>::const_iterator itPrim = mPrimitives.begin(); itPrim != mPrimitives.end(); ++itPrim){ if ((*itPrim)->Type == Graph::PT_MINIMAL_CYCLE){ Index lresult; for (std::vector<std::pair<Vector2,int> >::const_iterator itSequence = (*itPrim)->Sequence.begin(); itSequence != (*itPrim)->Sequence.end(); ++itSequence) lresult.push_back(itSequence->second); result->push_back(lresult); } } return result; }
/********************************************************************** * Purpose: Reads file and inserts info into Graph graph * Primes all the data. If success, return true. * Entry: string file, Graph<string, string> & graph * Exit: returns true if successful. ************************************************************************/ bool ReadFile(const string & file, Graph<string, string> & graph) { ifstream map(file.c_str()); bool valid = false; if (map.is_open()) { valid = true; while (!map.eof()) { string orig, dest, hwy; int weight = 0; getline(map, orig, ','); getline(map, dest, ','); getline(map, hwy, ','); map >> weight; map.get(); // for end line try { graph.InsertVertex(orig); } catch (Exception e) { //cout << e << endl; } try { graph.InsertVertex(dest); } catch (Exception e) { //cout << e << endl; } graph.InsertEdge(orig, dest, hwy, weight); } map.close(); }
Graph MapHelp::GetGraphFromGrid() { cout << "start Grid to Graph" << endl; vector<Waypoint> way; Graph gr; int NodeID = 0; for (int i = 0; i < this->GetBlownGridMap()->GetHeight(); ++i) { for (int j = 0; j < this->GetBlownGridMap()->GetWidth(); ++j) { gr.InsertVertex(NodeID, 5, j, i, false); // cout << "node " << NodeID << " x: " << i << " y: " << j // << " value: " << this->GetBlownGridMap()->GetValue(j, i) // << endl; NodeID++; } } for (int i = 0; i < this->GetBlownGridMap()->GetHeight(); ++i) { for (int j = 0; j < this->GetBlownGridMap()->GetWidth(); ++j) { // For each cell: int ij = this->GetBlownGridMap()->GetValue(j, i); if (ij == 0) { // Iterate over the closest neighbours 3x3 matrix:+s for (int k = i - 1; k <= i + 1; k++) { for (int m = j - 1; m <= j + 1; m++) { // node name: k,m // if (k < 0 || m < 0 || k >= this->GetBlownGridMap()->GetHeight() || m >= this->GetBlownGridMap()->GetWidth()) { } else { int km = this->GetBlownGridMap()->GetValue(m, k);//[k][m]; //blownMapMatrix[k][m] //if (node [k,m]=white AND [i,j]=white AND [k,m]!=[i,j]) -> add edge if (km == 0 && (k != i || m != j)) { //add edge // from ij to km //gr.InsertEdge(gr.GetNodeByPos(i, j)->GetData(), gr.GetNodeByPos(k, m)->GetData(), 1); // cout << "Parent: " << i << " " << j << " Son: " // << k << " " << m << endl; gr.AddSuccessor( gr.GetNodeByPos(j, i)->GetData(), gr.GetNodeByPos(m, k)->GetData()); // cout << gr.GetNodeByPos(j, i)->GetSuccessors().size() << endl; } } } } } } } cout << "Finished Grid to Graph" << endl; return gr; }
int main() { _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); Graph<string, string> g; g.InsertVertex("Anchorage"); g.InsertVertex("Eagle River"); g.InsertVertex("Old Glenn - AK HWY 1 - AK HWY 3 JCT"); g.InsertVertex("Wasilla"); g.InsertVertex("Palmer"); g.InsertVertex("Talkeetna"); g.InsertVertex("Fairbanks"); g.InsertEdge("Anchorage", "Eagle River", "Old Glenn Hwy", 16); g.InsertEdge("Eagle River", "Old Glenn - AK HWY 1 - AK HWY 3 JCT", "Old Glenn", 19); g.InsertEdge("Old Glenn - AK HWY 1 - AK HWY 3 JCT", "Palmer", "HWY 1", 6); g.InsertEdge("Old Glenn - AK HWY 1 - AK HWY 3 JCT", "Wasilla", "HWY 3", 8); g.InsertEdge("Wasilla", "Talkeetna", "HWY 3", 70); g.InsertEdge("Talkeetna", "Fairbanks", "HWY 3", 300); /*try { g.InsertEdge("Anchorage", "Eagle River", "Old Glenn Hwy", 16); } catch (Exception e) { cout << e << endl; }*/ /*Graph<string, string> copy(g); Graph<string, string> equal; equal = g;*/ cout << "Depth:" << endl; g.DepthFirst(&Print); cout << "\nBreadth:" << endl; g.BreadthFirst(&Print); g.DeleteVertex("Fairbanks"); try { cout << "Invalid Delete" << endl; g.DeleteVertex("Fairbanks"); } catch (Exception e) { cout << e << endl; } cout << "\nBreadth after Delete Vertex Fairbanks:" << endl; g.BreadthFirst(&Print); g.DeleteEdge("Old Glenn - AK HWY 1 - AK HWY 3 JCT", "Wasilla", "HWY 3", 8); cout << "\nDepth after Delete Edge:" << endl; g.DepthFirst(&Print); return 0; }
int main() { Graph head; std::cout << &head << std::endl; std::cout << head.InsertVertex('a')->GetName() << std::endl; std::cout << head.InsertVertex('b')->GetName() << std::endl; std::cout << head.InsertVertex('c')->GetName() << std::endl; std::cout << head.InsertVertex('d')->GetName() << std::endl; std::cout << head.GetFirstVertex()->GetName() << std::endl; std::cout << head.InsertEdge('a', 'b')->GetName() << std::endl; std::cout << head.InsertEdge('a', 'c')->GetName() << std::endl; std::cout << head.InsertEdge('a', 'd')->GetName() << std::endl; std::cout << head.InsertEdge('b', 'a')->GetName() << std::endl; std::cout << head.DeleteEdge('b', 'a') << std::endl; std::cout << head.DeleteEdge('a', 'b') << std::endl; std::cout << head.DeleteEdge('a', 'c') << std::endl; std::cout << head.DeleteEdge('a', 'd') << std::endl; std::cout << head.DeleteVertex('b') << std::endl; std::cout << head.DeleteVertex('c') << std::endl; std::cout << head.DeleteVertex('d') << std::endl; return 0; }
void InsertVertices(Graph<string, string>& g) { g.InsertVertex("Anchorage"); g.InsertVertex("Eagle River"); g.InsertVertex("Old Glenn - AK HWY 1 - AK HWY 3 JCT"); g.InsertVertex("Wasilla"); g.InsertVertex("Palmer"); g.InsertVertex("Talkeetna"); g.InsertVertex("Fairbanks"); }
void Load(Graph& mGraph, vector<Graph::Primitive*>& mPrimitives) { // std::string path = Environment::GetPathR("tri.txt"); //std::ifstream inFile(path.c_str()); ifstream inFile("./PlanarGraph.txt"); //ofstream verify; //verify.open("verify.txt",ios::out|ios::app); int numVertices; inFile >> numVertices; //verify<< numVertices<<"\n"; int i; for (i = 0; i < numVertices; ++i) { double x, y; inFile >> x; inFile >> y; // verify<< x <<"\t"<< y <<"\n"; //y = GetHeight() - 1 - y; mGraph.InsertVertex(Vec2(x, y), i); } int numEdges; inFile >> numEdges; //verify<< numEdges<<"\n"; for (i = 0; i < numEdges; ++i) { int v0, v1; inFile >> v0; inFile >> v1; // verify<< v0 <<"\t"<< v1 <<"\n"; mGraph.InsertEdge(v0, v1); } #ifdef EXTRACT_PRIMITIVES mGraph.ExtractPrimitives(mPrimitives); #endif }