简体   繁体   中英

Generate an adjacency list graph from topology in C++

As a beginner in c++, I need to make an adjacency list graph. I want to generate a weighted graph that shows topology relations between planes and their angle as weight. So in the graph, planes represent graph vertices (eg v) and intersection lines represent graph edges (eg v1v2). Each intersection line has also one attribute. MY program generates such a txt file:

edge, attribute, weight
v1v2, 1, 90
v1v3, 2, 45
v1v3, 2, 30
v2v3, 3, 90
...

And here is the pseudo code that generates the txt file:

for (planes.begin, numberOFplanes, planei++){
  for(planes.begin+1, numberOFplanes, planej++){

    if (planei intersect planej){
        cout << ViVj << attribute << angle << nedl;
    }
  }
}

I found below class from this link ( Graph implementation C++ ) and want to implement in my code, but I do not know how to do it, more precise I need to give the plane numbers and their angle as input to the graph class to generate a graph.

ps I know the graph class from the link is a directed class and I don't mind to generate an undirected class, both or fine.

Don't hesitate to correct me if I did not understood one point or another.

Let start with the basic geometric objets we will need:

//I considere here the one point + one orthogonal 
//representation of a plan
class Plan{
 public:
  //Constructor
  Node(const Point*& iPoint, const Vector*& iVector) {
    _point = iPoint;
    _vector = iVector;
  }

  float getAngleDifferencial(const Plan*& iPlan) {
    //well my math are a bit old too do the math 
    //but you should be fine here... I hope :)
  }

  //you may wanna add getter or stuff like that

 private:
  Point* _point;
  Vector* _vector;
}

Next is graph items:

class Node {
 public:
  Node(int iNumber,const Plan*& iPlan):
    _Id(iNumber) {
    _Plan = iPlan;
  }

 private:
  int _Id;
  Plan* _Plan;
}

class Edge {
 public:
  Edge(const Node*& iNode1,const Node*&iNode2) {
    if (iNode1->getId()==iNode2->getId()) {
      //throw Exception
    }
    _Node1 = iNode1;
    _Node2 = iNode2;
    _Weight = iNode1->getPlan()->getAngleDifferencial(iNode2->getPlan())
}

 private:
  Node* _Node1;
  Node* _Node2;
  float _Weight;
}

Lastly you will need to get some Array to put your Nodes and Edges and start playing with it.

Have fun :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM