GraphLibrary  0.0.1
A simple library that implements various graph algorithms.
 All Classes Namespaces Files Functions Variables Typedefs Macros
WriteTikz.hpp
Go to the documentation of this file.
1 #ifndef GL_WRITE_TIKZ_HPP
2 #define GL_WRITE_TIKZ_HPP
3 
4 #include "../structures/Graph.hpp"
5 #include <iostream>
6 
7 namespace gl {
8 namespace external {
9 
17 template <class SCALAR, class STORAGE_KIND, class DIRECTION>
18 void writeTikzNetwork(std::ostream &s, Graph<SCALAR, STORAGE_KIND, DIRECTION> &g, bool writeNodes = true, bool writeEdgeWeights = true)
19 {
20  s << "\\RequirePackage{luatex85}" << std::endl;
21  s << "\\documentclass{standalone}" << std::endl;
22  s << "\\usepackage{tikz-network}" << std::endl;
23  s << "\\begin{document}" << std::endl;
24  s << "\\begin{tikzpicture}" << std::endl;
25 
26  for (gl::index_type i = 0; i < g.numNodes(); i++)
27  {
28  auto color = g.getNodeColor(i);
29  s << " \\Vertex[x=" << g.getNodePosition(i).first << ",y=" << g.getNodePosition(i).second
30  << ",RGB,color={" << +color.r() << "," << +color.g()
31  << "," << +color.b() << "},opacity=" << +color.a()
32  << ",size=" << g.getNodeCapacity(i); // idea: set to capacity of vertex
33  if (g.getNodeLabel(i) == "")
34  {
35  s << ",IdAsLabel";
36  }
37  else
38  {
39  s << ",label=" << g.getNodeLabel(i) << ",position=above";
40  }
41  s << (writeNodes ? "" : ",NoLabel")
42  << "]{" << i << "}" << std::endl;
43  }
44  s << "\n";
45 
46  // draw all edges
47  for (auto it = g.edge_cbegin(); it != g.edge_cend(); it++)
48  {
49  if ((std::is_same_v<DIRECTION, gl::Undirected> && it->source() <= it->dest()) || std::is_same_v<DIRECTION, gl::Directed>)
50  {
51  auto color = it->color();
52  s << " \\Edge[" << (g.isDirected() ? "Direct," : "")
53  << (it->source() == it->dest() ? "loopshape=45," : "")
54  << "RGB,color={" << +color.r() << "," << +color.g()
55  << "," << +color.b() << "},opacity=" << +color.a();
56  if (writeEdgeWeights)
57  {
58  s << ",label=" << it->weight();
59  }
60  s << "](" << it->source() << ")(" << it->dest() << ")" << std::endl;
61  }
62  }
63  s << "\\node[above,align=center,font=\\bfseries] at (current bounding box.north) {" << g.getGraphLabel() << "};" << std::endl;
64  s << "\\end{tikzpicture}" << std::endl;
65  s << "\\end{document}" << std::endl;
66 }
67 
68 } // namespace external
69 } // namespace gl
70 
71 #endif // GL_WRITE_TIKZ_HPP
ConstEdgeIterator edge_cbegin() const
ConstEdgeIterator to the first edge.
Definition: Graph.hpp:967
void writeTikzNetwork(std::ostream &s, Graph< SCALAR, STORAGE_KIND, DIRECTION > &g, bool writeNodes=true, bool writeEdgeWeights=true)
Write structure to stream, given.
Definition: WriteTikz.hpp:18
val_t getNodeCapacity(const idx_t &id) const
Finds the flow capacity of the given node.
Definition: Graph.hpp:1264
Stores and implements a Graph.
Definition: Graph.hpp:39
ConstEdgeIterator edge_cend() const
ConstEdgeIterator to the last edge.
Definition: Graph.hpp:989
std::string getGraphLabel() const
Returns the label of the graph.
Definition: Graph.hpp:390
bool isDirected() const
Returns true if the graph is directed, false if not.
Definition: Graph.hpp:426
idx_t numNodes() const
Returns the number of nodes currently in the graph.
Definition: Graph.hpp:406
std::size_t index_type
Definition: gl_base.hpp:18
Color getNodeColor(const idx_t &id) const
Returns the color of a node.
Definition: Graph.hpp:1325
std::string getNodeLabel(const idx_t &id) const
Finds the label of the given node.
Definition: Graph.hpp:1255
std::pair< float, float > getNodePosition(const idx_t &id) const
Finds the position of the given node.
Definition: Graph.hpp:1273