GraphLibrary  0.0.1
A simple library that implements various graph algorithms.
 All Classes Namespaces Files Functions Variables Typedefs Macros
WriteImage.hpp
Go to the documentation of this file.
1 #ifndef GL_WRITE_IMAGE_HPP
2 #define GL_WRITE_IMAGE_HPP
3 
4 #include "../structures/Graph.hpp"
5 #include "../algorithms/SpectralPlacing.hpp"
6 #include "../algorithms/LaplacianSTL.hpp"
7 #include <iostream>
8 #include <vector>
9 #include <mgl2/mgl.h>
10 
11 namespace gl
12 {
13 namespace external
14 {
15 
22 template <class SCALAR, class STORAGE_KIND, class DIRECTION>
23 void saveImage(mglGraph *gr, gl::Graph<SCALAR, STORAGE_KIND, DIRECTION> &g, const char *lineArgs = "")
24 {
25  // calculate spectral placing
27 
28  float minX = g.getNodePosition(0).first;
29  float maxX = g.getNodePosition(0).first;
30  float minY = g.getNodePosition(0).second;
31  float maxY = g.getNodePosition(0).second;
32 
33  for (auto it = g.node_cbegin(); it != g.node_cend(); it++)
34  {
35  if (g.getNodePosition(it->id()).first < minX)
36  minX = g.getNodePosition(it->id()).first;
37  if (g.getNodePosition(it->id()).first > maxX)
38  maxX = g.getNodePosition(it->id()).first;
39  if (g.getNodePosition(it->id()).second < minY)
40  minY = g.getNodePosition(it->id()).second;
41  if (g.getNodePosition(it->id()).second > maxY)
42  maxY = g.getNodePosition(it->id()).second;
43  }
44 
45  // set plot ranges
46  gr->SetRange('x', minX, maxX);
47  gr->SetRange('y', minY, maxY);
48 
49  // draw all edges
50  for (auto it = g.edge_cbegin(); it != g.edge_cend(); it++)
51  {
52 
53  mglPoint p1(g.getNodePosition(it->source()).first, g.getNodePosition(it->source()).second);
54  mglPoint p2(g.getNodePosition(it->dest()).first, g.getNodePosition(it->dest()).second);
55  std::string output_args = lineArgs;
56  output_args += "{x" + it->color().RGB() + "}";
57 
58  gr->Line(p1, p2, output_args.data());
59  }
60 }
61 
62 } // namespace external
63 } // namespace gl
64 
65 #endif // GL_WRITE_IMAGE_HPP
ConstEdgeIterator edge_cbegin() const
ConstEdgeIterator to the first edge.
Definition: Graph.hpp:967
Stores and implements a Graph.
Definition: Graph.hpp:39
ConstNodeIterator node_cbegin() const
ConstNodeIterator to the first node.
Definition: Graph.hpp:1367
ConstEdgeIterator edge_cend() const
ConstEdgeIterator to the last edge.
Definition: Graph.hpp:989
void SpectralPlacing(Graph &g)
Compute node 2D positions using spectral placing.
Definition: SpectralPlacing.hpp:18
ConstNodeIterator node_cend() const
ConstNodeIterator to behind the last node.
Definition: Graph.hpp:1375
void saveImage(mglGraph *gr, gl::Graph< SCALAR, STORAGE_KIND, DIRECTION > &g, const char *lineArgs="")
Plot graph using mgl2.
Definition: WriteImage.hpp:23
std::pair< float, float > getNodePosition(const idx_t &id) const
Finds the position of the given node.
Definition: Graph.hpp:1273