graph_analysis
GVGraph.hpp
Go to the documentation of this file.
1 #ifndef GRAPH_ANALYSIS_IO_GVGRAPH_HPP
2 #define GRAPH_ANALYSIS_IO_GVGRAPH_HPP
3 
4 #include <set>
5 // As of cgraph version 2.35 the WITH_CGRAPH option is always true
6 // added here to remain compatible with older cgraph versions
7 #include <graphviz/cgraph.h>
8 #define WITH_CGRAPH
9 #include <graphviz/gvc.h>
10 #include "../BaseGraph.hpp"
11 
12 // http://graphviz.org/doc/libguide/libguide.pdf
13 // The agopen method for opening a graph
14 //
15 // AGRAPH, AGDIGRAPH, AGRAPHSTRICT, AGDIGRAPHSTRICT
16 // version >= 2.36
17 // Agundirected, Agstrictundirected, Agdirected, Agstrictdirected
18 static inline Agraph_t* _agopen(const std::string& name, Agdesc_t kind)
19 {
20  // extern Agraph_t *agopen(char *name, Agdesc_t desc, Agdisc_t * disc);
21  // first arg: graph
22  // second arg: type of graph
23  // third arg: (optional) reference to functions for reading, memory, etc. --
24  // 0 for default
25 #ifdef GRAPHVIZ_DEPRECATED
26 #warning "You are using a graphviz version < 2.36.0 -- limited support only"
27  return agopen(const_cast<char*>(name.c_str()), kind);
28 #else
29  return agopen(const_cast<char*>(name.c_str()), kind, 0);
30 #endif
31 }
32 
33 static inline Agnode_t* _agnode(Agraph_t* g, const std::string& name)
34 {
35  return agnode(g, const_cast<char*>(name.c_str()), true);
36 }
37 
38 static inline Agedge_t* _agedge(Agraph_t* g, Agnode_t* node0, Agnode_t* node1, const std::string& name, bool create)
39 {
40  return agedge(g, node0, node1, const_cast<char*>(name.c_str()), create);
41 }
42 
43 static inline Agedge_t* _agidedge(Agraph_t* g, Agnode_t* node0, Agnode_t* node1, graph_analysis::GraphElementId id, bool create)
44 {
45  return agidedge(g, node0, node1, id, create);
46 }
47 
48 namespace graph_analysis {
49 namespace io {
50 
51 // A struct containing the information for a GVGraph's node
52 // This implementation has been made based on Steve D. Lazaro's presentation on graphviz integration into qt
53 // \see http://www.mupuf.org/blog/2010/07/08/how_to_usempGraphviz_to_drawmGraphs_in_a_qtmGraphics_scene/
54 // \credits Steve D. Lazaro, 2010
55 //
56 class GVNode
57 {
58 public:
60  {}
61 
62  Vertex::Ptr getVertex() const { return mpVertex; }
63  const std::string& getName() const { return mName; }
64 
65  int x() const { return mCenterPosX; }
66  int y() const { return mCenterPosY; }
67  int height() const { return mHeight; }
68  int width() const { return mWidth; }
69 
70  GVNode(Vertex::Ptr vertex, const std::string& name, int x, int y, double height, double width)
71  : mpVertex(vertex)
72  , mName(name)
73  , mCenterPosX(x)
74  , mCenterPosY(y)
75  , mHeight(height)
76  , mWidth(width)
77  {}
78 
79 private:
81 
83  std::string mName;
84 
86  int mCenterPosX, mCenterPosY;
87 
89  double mHeight, mWidth;
90 };
91 
93 class GVEdge
94 {
95 public:
97  : mpEdge(edge)
98  {}
99 
100  Edge::Ptr getEdge() const { return mpEdge; }
101 
102 private:
103  Edge::Ptr mpEdge;
104 
106  // * \brief Path of the edge's line
107  // * \return edge's path
108  // */
109  //QPainterPath path;
110 };
111 
119 class GVGraph
120 {
121 public:
123  static const double DotDefaultDPI;
124 
125  static std::set<std::string> getSupportedLayouts() { return msSupportedLayouts; }
126 
133  GVGraph(const BaseGraph::Ptr& baseGraph, const std::string& name, double node_size = 0);
134 
138  ~GVGraph();
139 
146  int setGraphAttribute(const std::string& name, const std::string& value);
147 
154  std::string getGraphAttribute(const std::string& name, const std::string& defaultValue = std::string()) const;
155 
161  void setNodeAttribute(const std::string& name, const std::string& value);
162 
168  void setEdgeAttribute(const std::string& name, const std::string& value);
169 
173  bool setAttribute(const graph_analysis::Vertex::Ptr& vertex, const std::string& name, const std::string& value);
177  bool setAttribute(const graph_analysis::Edge::Ptr& edge, const std::string& name, const std::string& value);
178 
179  // Remove edges then nodes
180  void clear();
181 
185  void addNodes(const std::vector<Vertex::Ptr>& vertices);
186  void removeNode(const graph_analysis::Vertex::Ptr& vertex);
187  void removeNode(GraphElementId id);
188  void clearNodes();
189 
191  GraphElementId addEdge(const Edge::Ptr& edge);
192  void addEdges(const std::vector<Edge::Ptr>& edges);
193  void removeEdge(const Edge::Ptr& edge);
194  void removeEdge(GraphElementId id);
195  void clearEdges();
196 
198  void setFont(const std::string& fontname, uint16_t fontsizeInPt);
199 
205  void applyLayout(const std::string& layout = "dot");
206 
207 
215  void renderToFile(const std::string& filename, const std::string& layout = "dot", const std::string& renderer = "dot", bool forced = false);
216  boxf boundingRect() const;
217  void setRootNode(const Vertex::Ptr& vertex);
218 
219  std::vector<GVNode> nodes() const;
220  std::vector<GVEdge> edges() const;
221 
222  GraphElementId getId(Agnode_t* node) const;
223  GraphElementId getId(Agedge_t* edge) const;
224 
225  std::string getUniqueName(const Vertex::Ptr& vertex) const;
226  std::string getUniqueName(const Edge::Ptr& edge) const;
227 
228 private:
230  GVC_t* mpContext;
232  Agraph_t* mpGVGraph;
234  BaseGraph::Ptr mpBaseGraph;
235 
237  std::map<GraphElementId, Agnode_t*> mNodes;
239  std::map<GraphElementId, Agedge_t*> mEdges;
240 
242  double mDPI;
244  double mScalingFactor;
246  bool mAppliedLayout;
248  static std::set<std::string> msSupportedLayouts;
249  static GraphElementId msEdgeId;
250 };
251 
252 } // end namespace io
253 } // end namespace graph_analysis
254 #endif // GRAPH_ANALYSIS_IO_GVGRAPH_HPP
graph_analysis::io::GVNode::getVertex
Vertex::Ptr getVertex() const
Definition: GVGraph.hpp:62
graph_analysis::io::GVGraph::GVGraph
GVGraph(const BaseGraph::Ptr &baseGraph, const std::string &name, double node_size=0)
Construct a Graphviz graph object.
Definition: GVGraph.cpp:30
graph_analysis::io::GVGraph::applyLayout
void applyLayout(const std::string &layout="dot")
Definition: GVGraph.cpp:344
graph_analysis::io::GVGraph::clear
void clear()
Definition: GVGraph.cpp:168
graph_analysis::io::GVEdge::getEdge
Edge::Ptr getEdge() const
Definition: GVGraph.hpp:100
graph_analysis::io::GVGraph::~GVGraph
~GVGraph()
Definition: GVGraph.cpp:82
graph_analysis::io::GVGraph::initializeFromBaseGraph
void initializeFromBaseGraph()
Add and remove nodes.
Definition: GVGraph.cpp:156
graph_analysis::io::GVNode::GVNode
GVNode(Vertex::Ptr vertex, const std::string &name, int x, int y, double height, double width)
Definition: GVGraph.hpp:70
graph_analysis::io::GVGraph::clearNodes
void clearNodes()
Definition: GVGraph.cpp:247
graph_analysis::io::GVGraph::setRootNode
void setRootNode(const Vertex::Ptr &vertex)
Definition: GVGraph.cpp:256
graph_analysis::io::GVGraph::getGraphAttribute
std::string getGraphAttribute(const std::string &name, const std::string &defaultValue=std::string()) const
Definition: GVGraph.cpp:104
graph_analysis
Main namespace of the graph analysis library.
Definition: BFS.cpp:3
graph_analysis::BaseGraph::Ptr
shared_ptr< BaseGraph > Ptr
Definition: BaseGraph.hpp:34
graph_analysis::io::GVNode::getName
const std::string & getName() const
Definition: GVGraph.hpp:63
graph_analysis::io::GVGraph::addNodes
void addNodes(const std::vector< Vertex::Ptr > &vertices)
Definition: GVGraph.cpp:206
graph_analysis::Edge::Ptr
shared_ptr< Edge > Ptr
Definition: Edge.hpp:21
graph_analysis::io::GVGraph
An object containing a libgraph graph and its associated nodes and edges.
Definition: GVGraph.hpp:119
graph_analysis::io::GVGraph::nodes
std::vector< GVNode > nodes() const
Definition: GVGraph.cpp:399
graph_analysis::io::GVGraph::setFont
void setFont(const std::string &fontname, uint16_t fontsizeInPt)
Set the font to use in all the labels.
Definition: GVGraph.cpp:328
graph_analysis::io::GVNode::height
int height() const
Definition: GVGraph.hpp:67
graph_analysis::io::GVGraph::addEdges
void addEdges(const std::vector< Edge::Ptr > &edges)
Definition: GVGraph.cpp:265
graph_analysis::io::GVGraph::setEdgeAttribute
void setEdgeAttribute(const std::string &name, const std::string &value)
Definition: GVGraph.cpp:119
graph_analysis::io::GVNode::y
int y() const
Definition: GVGraph.hpp:66
graph_analysis::io::GVGraph::addNode
GraphElementId addNode(const graph_analysis::Vertex::Ptr &vertex)
Definition: GVGraph.cpp:180
graph_analysis::io::GVNode::GVNode
GVNode()
Definition: GVGraph.hpp:59
graph_analysis::io::GVGraph::removeNode
void removeNode(const graph_analysis::Vertex::Ptr &vertex)
Definition: GVGraph.cpp:215
graph_analysis::io::GVGraph::DotDefaultDPI
static const double DotDefaultDPI
Default DPI value used by dot (which uses points instead of pixels for coordinates)
Definition: GVGraph.hpp:123
graph_analysis::GraphElementId
uint32_t GraphElementId
Definition: GraphElement.hpp:21
graph_analysis::io::GVGraph::getId
GraphElementId getId(Agnode_t *node) const
Definition: GVGraph.cpp:495
graph_analysis::Vertex::Ptr
shared_ptr< Vertex > Ptr
Definition: Vertex.hpp:22
graph_analysis::io::GVGraph::addEdge
GraphElementId addEdge(const Edge::Ptr &edge)
Add and remove edges.
Definition: GVGraph.cpp:274
graph_analysis::io::GVGraph::getSupportedLayouts
static std::set< std::string > getSupportedLayouts()
Definition: GVGraph.hpp:125
graph_analysis::io::GVEdge
A struct containing the information for a GVGraph's edge.
Definition: GVGraph.hpp:93
graph_analysis::io::GVGraph::removeEdge
void removeEdge(const Edge::Ptr &edge)
Definition: GVGraph.cpp:301
graph_analysis::io::GVGraph::renderToFile
void renderToFile(const std::string &filename, const std::string &layout="dot", const std::string &renderer="dot", bool forced=false)
Definition: GVGraph.cpp:363
graph_analysis::io::GVGraph::edges
std::vector< GVEdge > edges() const
Definition: GVGraph.cpp:432
graph_analysis::io::GVNode::width
int width() const
Definition: GVGraph.hpp:68
graph_analysis::io::GVGraph::getUniqueName
std::string getUniqueName(const Vertex::Ptr &vertex) const
Definition: GVGraph.cpp:522
graph_analysis::io::GVNode::x
int x() const
Definition: GVGraph.hpp:65
graph_analysis::io::GVGraph::setAttribute
bool setAttribute(const graph_analysis::Vertex::Ptr &vertex, const std::string &name, const std::string &value)
Definition: GVGraph.cpp:126
graph_analysis::io::GVGraph::clearEdges
void clearEdges()
Definition: GVGraph.cpp:319
graph_analysis::io::GVGraph::setNodeAttribute
void setNodeAttribute(const std::string &name, const std::string &value)
Definition: GVGraph.cpp:114
graph_analysis::io::GVNode
Definition: GVGraph.hpp:56
graph_analysis::io::GVEdge::GVEdge
GVEdge(Edge::Ptr edge)
Definition: GVGraph.hpp:96
graph_analysis::io::GVGraph::boundingRect
boxf boundingRect() const
Definition: GVGraph.cpp:379
graph_analysis::io::GVGraph::setGraphAttribute
int setGraphAttribute(const std::string &name, const std::string &value)
Definition: GVGraph.cpp:96