Blender  V2.93
BLI_dot_export.hh
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #pragma once
18 
26 #include "BLI_map.hh"
27 #include "BLI_set.hh"
28 #include "BLI_utility_mixins.hh"
29 #include "BLI_vector.hh"
30 
32 
33 #include <optional>
34 #include <sstream>
35 
36 namespace blender::dot {
37 
38 class Graph;
39 class DirectedGraph;
40 class UndirectedGraph;
41 class Node;
42 class NodePort;
43 class DirectedEdge;
44 class UndirectedEdge;
45 class Cluster;
46 
47 class Attributes {
48  private:
50 
51  public:
52  void export__as_bracket_list(std::stringstream &ss) const;
53 
54  void set(StringRef key, StringRef value)
55  {
56  attributes_.add_overwrite(key, value);
57  }
58 
59  void set(StringRef key, float value)
60  {
61  attributes_.add_overwrite(key, std::to_string(value));
62  }
63 };
64 
65 class Graph {
66  private:
69 
70  Set<Node *> top_level_nodes_;
71  Set<Cluster *> top_level_clusters_;
72 
73  friend Cluster;
74  friend Node;
75 
76  public:
78 
79  public:
82 
83  void export__declare_nodes_and_clusters(std::stringstream &ss) const;
84 
85  void set_rankdir(Attr_rankdir rankdir)
86  {
87  attributes.set("rankdir", rankdir_to_string(rankdir));
88  }
89 
91 };
92 
93 class Cluster {
94  private:
95  Graph &graph_;
96  Cluster *parent_ = nullptr;
97  Set<Cluster *> children_;
98  Set<Node *> nodes_;
99 
100  friend Graph;
101  friend Node;
102 
103  public:
105 
106  Cluster(Graph &graph) : graph_(graph)
107  {
108  }
109 
110  public:
111  void export__declare_nodes_and_clusters(std::stringstream &ss) const;
112 
113  std::string name() const
114  {
115  return "cluster_" + std::to_string((uintptr_t)this);
116  }
117 
118  void set_parent_cluster(Cluster *new_parent);
120  {
121  this->set_parent_cluster(&cluster);
122  }
123 
125  {
126  return parent_;
127  }
128 
130 
131  bool contains(Node &node) const;
132 };
133 
134 class Node {
135  private:
136  Graph &graph_;
137  Cluster *cluster_ = nullptr;
138 
139  friend Graph;
140 
141  public:
143 
144  Node(Graph &graph) : graph_(graph)
145  {
146  }
147 
148  public:
149  void set_parent_cluster(Cluster *cluster);
151  {
152  this->set_parent_cluster(&cluster);
153  }
154 
156  {
157  return cluster_;
158  }
159 
160  void set_shape(Attr_shape shape)
161  {
162  attributes.set("shape", shape_to_string(shape));
163  }
164 
165  /* See https://www.graphviz.org/doc/info/attrs.html#k:color. */
167  {
168  attributes.set("fillcolor", name);
169  attributes.set("style", "filled");
170  }
171 
172  void export__as_id(std::stringstream &ss) const;
173 
174  void export__as_declaration(std::stringstream &ss) const;
175 };
176 
177 class UndirectedGraph final : public Graph {
178  private:
180 
181  public:
182  std::string to_dot_string() const;
183 
185 };
186 
187 class DirectedGraph final : public Graph {
188  private:
190 
191  public:
192  std::string to_dot_string() const;
193 
195 };
196 
197 class NodePort {
198  private:
199  Node *node_;
200  std::optional<std::string> port_name_;
201 
202  public:
203  NodePort(Node &node, std::optional<std::string> port_name = {})
204  : node_(&node), port_name_(std::move(port_name))
205  {
206  }
207 
208  void to_dot_string(std::stringstream &ss) const;
209 };
210 
212  protected:
215 
216  public:
218 
219  public:
220  Edge(NodePort a, NodePort b) : a_(std::move(a)), b_(std::move(b))
221  {
222  }
223 
225  {
226  attributes.set("arrowhead", arrowType_to_string(type));
227  }
228 
230  {
231  attributes.set("arrowtail", arrowType_to_string(type));
232  }
233 
235  {
237  }
238 
240  {
241  attributes.set("label", label);
242  }
243 };
244 
245 class DirectedEdge : public Edge {
246  public:
247  DirectedEdge(NodePort from, NodePort to) : Edge(std::move(from), std::move(to))
248  {
249  }
250 
251  void export__as_edge_statement(std::stringstream &ss) const;
252 };
253 
254 class UndirectedEdge : public Edge {
255  public:
256  UndirectedEdge(NodePort a, NodePort b) : Edge(std::move(a), std::move(b))
257  {
258  }
259 
260  void export__as_edge_statement(std::stringstream &ss) const;
261 };
262 
263 std::string color_attr_from_hsv(float h, float s, float v);
264 
266  private:
267  Node *node_;
268 
269  public:
271  StringRef name,
272  Span<std::string> input_names,
273  Span<std::string> output_names);
274 
276  {
277  return *node_;
278  }
279 
280  NodePort input(int index) const
281  {
282  std::string port = "\"in" + std::to_string(index) + "\"";
283  return NodePort(*node_, port);
284  }
285 
286  NodePort output(int index) const
287  {
288  std::string port = "\"out" + std::to_string(index) + "\"";
289  return NodePort(*node_, port);
290  }
291 };
292 
293 } // namespace blender::dot
#define final(a, b, c)
Definition: BLI_hash.h:35
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
ATTR_WARN_UNUSED_RESULT const BMVert * v
bool add_overwrite(const Key &key, const Value &value)
Definition: BLI_map.hh:294
void export__as_bracket_list(std::stringstream &ss) const
Definition: dot_export.cc:219
void set(StringRef key, StringRef value)
void set(StringRef key, float value)
void set_parent_cluster(Cluster *new_parent)
Definition: dot_export.cc:58
bool contains(Node &node) const
Definition: dot_export.cc:120
std::string name() const
void export__declare_nodes_and_clusters(std::stringstream &ss) const
Definition: dot_export.cc:182
void set_parent_cluster(Cluster &cluster)
void set_random_cluster_bgcolors()
Definition: dot_export.cc:108
DirectedEdge(NodePort from, NodePort to)
void export__as_edge_statement(std::stringstream &ss) const
Definition: dot_export.cc:201
DirectedEdge & new_edge(NodePort from, NodePort to)
Definition: dot_export.cc:51
std::string to_dot_string() const
Definition: dot_export.cc:135
void set_arrowtail(Attr_arrowType type)
Edge(NodePort a, NodePort b)
void set_arrowhead(Attr_arrowType type)
void set_dir(Attr_dirType type)
void set_label(StringRef label)
Cluster & new_cluster(StringRef label="")
Definition: dot_export.cc:35
void set_random_cluster_bgcolors()
Definition: dot_export.cc:101
Node & new_node(StringRef label)
Definition: dot_export.cc:26
void export__declare_nodes_and_clusters(std::stringstream &ss) const
Definition: dot_export.cc:167
void set_rankdir(Attr_rankdir rankdir)
NodePort(Node &node, std::optional< std::string > port_name={})
void to_dot_string(std::stringstream &ss) const
Definition: dot_export.cc:255
NodePort output(int index) const
NodeWithSocketsRef(Node &node, StringRef name, Span< std::string > input_names, Span< std::string > output_names)
Definition: dot_export.cc:270
NodePort input(int index) const
void set_shape(Attr_shape shape)
Node(Graph &graph)
void export__as_declaration(std::stringstream &ss) const
Definition: dot_export.cc:247
void set_background_color(StringRef name)
void export__as_id(std::stringstream &ss) const
Definition: dot_export.cc:242
void set_parent_cluster(Cluster *cluster)
Definition: dot_export.cc:78
Cluster * parent_cluster()
void set_parent_cluster(Cluster &cluster)
UndirectedEdge(NodePort a, NodePort b)
void export__as_edge_statement(std::stringstream &ss) const
Definition: dot_export.cc:210
UndirectedEdge & new_edge(NodePort a, NodePort b)
Definition: dot_export.cc:44
std::string to_dot_string() const
Definition: dot_export.cc:151
OperationNode * node
Depsgraph * graph
StackEntry * from
const char * label
static unsigned a[3]
Definition: RandGen.cpp:92
StringRef dirType_to_string(Attr_dirType value)
StringRef shape_to_string(Attr_shape value)
StringRef rankdir_to_string(Attr_rankdir value)
StringRef arrowType_to_string(Attr_arrowType value)
std::string color_attr_from_hsv(float h, float s, float v)
Definition: dot_export.cc:263
std::string to_string(const T &n)
_W64 unsigned int uintptr_t
Definition: stdint.h:122