-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgv-cpp.h
More file actions
136 lines (121 loc) · 4.2 KB
/
gv-cpp.h
File metadata and controls
136 lines (121 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <fstream>
#include <string>
#include <vector>
#include <map>
class graph{
public:
class node{
friend class graph;
private:
int id;
public:
std::map<std::string, std::string> attr, attrq;
};
class edge{
public:
node *a, *b;
std::map<std::string, std::string> attr, attrq;
};
private:
std::vector<edge*> edges;
std::vector<node*> nodes;
template<typename type>
std::string gen_attr(type* entity){
std::string out = "[";
for(auto [a, b]: entity->attr){
out+=a+"="+b+",";
}
for(auto [a, b]: entity->attrq){
out+=a+"=\""+b+"\",";
}
if(!(entity->attr.empty()) || !(entity->attrq.empty())) out.pop_back();
out+="]";
if(out.size() == 2) return "";
return out;
}
public:
std::map<std::string, std::string> attr, attrq;
edge* edgeDefaults = new edge;
node* nodeDefaults = new node;
graph(){
}
graph(std::map<std::string, std::string> _attr, std::map<std::string, std::string> _attrq){
attr = _attr;
attrq = _attrq;
}
node* new_node(){
node* nd = new node;
nd->id = nodes.size();
nodes.push_back(nd);
return nd;
}
node* new_node(std::map<std::string, std::string> _attr, std::map<std::string, std::string> _attrq){
node* nd = new node;
nd->attr = _attr;
nd->attrq = _attrq;
nd->id = nodes.size();
nodes.push_back(nd);
return nd;
}
edge* new_edge(node *a, node *b){
edge* e = new edge;
e->a = a;
e->b = b;
edges.push_back(e);
return e;
}
edge* new_edge(node *a, node *b, std::map<std::string, std::string> _attr, std::map<std::string, std::string> _attrq){
edge* e = new edge;
e->a = a;
e->b = b;
e->attr = _attr;
e->attrq = _attrq;
edges.push_back(e);
return e;
}
std::string generate_graphviz(bool readable = false, std::string indent = " "){
std::string begin = "";
std::string end = "";
if(readable) begin = indent, end = "\n";
std::string out = "digraph G{"+end;
std::string attrs = gen_attr(this);
if(!attrs.empty())out+=begin+"graph"+attrs+";"+end;
attrs = gen_attr(nodeDefaults);
if(!attr.empty()) out+=begin+"node"+attrs+";"+end;
attrs = gen_attr(edgeDefaults);
if(!attr.empty()) out+=begin+"edge"+attrs+";"+end;
for(int i = 0; i < nodes.size(); i++){
out+=begin+std::to_string(nodes[i]->id)+gen_attr(nodes[i])+";"+end;
}
for(int i = 0; i < edges.size(); i++){
out+=begin+std::to_string(edges[i]->a->id)+"->"+std::to_string(edges[i]->b->id)+gen_attr(edges[i])+";"+end;
}
out+="}";
return out;
}
};
class Gif{
private:
std::vector<std::string> imgs;
public:
void add(std::string img){
imgs.push_back(img);
}
void generate(std::string output, int delay=20, std::string format="jpg", std::string graphviz="dot"){
if(imgs.empty()) return;
std::string cmd = "convert -delay " + std::to_string(delay) + " -loop 0 ";
for(int i = 0; i < imgs.size(); i++){
std::ofstream F(".tmp.gv");
F << imgs[i];
F.close();
system((graphviz + " -T" + format + " .tmp.gv -o .tmp-" + std::to_string(i) + "." + format).c_str());
system("rm .tmp.gv");
cmd+=" .tmp-" + std::to_string(i) + "." + format + " ";
}
cmd+=output;
system(cmd.c_str());
for(int i = 0; i < imgs.size(); i++){
system(("rm .tmp-" + std::to_string(i) + "." + format).c_str());
}
}
};