Blender  V3.3
BLI_dot_export.hh
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #pragma once
4 
12 #include "BLI_map.hh"
13 #include "BLI_set.hh"
14 #include "BLI_utility_mixins.hh"
15 #include "BLI_vector.hh"
16 
18 
19 #include <optional>
20 #include <sstream>
21 
22 namespace blender::dot {
23 
24 class Graph;
25 class DirectedGraph;
26 class UndirectedGraph;
27 class Node;
28 class NodePort;
29 class DirectedEdge;
30 class UndirectedEdge;
31 class Cluster;
32 
33 class Attributes {
34  private:
36 
37  public:
38  void export__as_bracket_list(std::stringstream &ss) const;
39 
40  void set(StringRef key, StringRef value)
41  {
42  attributes_.add_overwrite(key, value);
43  }
44 
45  void set(StringRef key, float value)
46  {
47  attributes_.add_overwrite(key, std::to_string(value));
48  }
49 };
50 
51 class Graph {
52  private:
55 
56  Set<Node *> top_level_nodes_;
57  Set<Cluster *> top_level_clusters_;
58 
59  friend Cluster;
60  friend Node;
61 
62  public:
64 
65  public:
68 
69  void export__declare_nodes_and_clusters(std::stringstream &ss) const;
70 
71  void set_rankdir(Attr_rankdir rankdir)
72  {
73  attributes.set("rankdir", rankdir_to_string(rankdir));
74  }
75 
77 };
78 
79 class Cluster {
80  private:
81  Graph &graph_;
82  Cluster *parent_ = nullptr;
83  Set<Cluster *> children_;
84  Set<Node *> nodes_;
85 
86  friend Graph;
87  friend Node;
88 
89  public:
91 
92  Cluster(Graph &graph) : graph_(graph)
93  {
94  }
95 
96  public:
97  void export__declare_nodes_and_clusters(std::stringstream &ss) const;
98 
99  std::string name() const
100  {
101  return "cluster_" + std::to_string((uintptr_t)this);
102  }
103 
104  void set_parent_cluster(Cluster *new_parent);
106  {
107  this->set_parent_cluster(&cluster);
108  }
109 
111  {
112  return parent_;
113  }
114 
116 
117  bool contains(Node &node) const;
118 };
119 
120 class Node {
121  private:
122  Graph &graph_;
123  Cluster *cluster_ = nullptr;
124 
125  friend Graph;
126 
127  public:
129 
130  Node(Graph &graph) : graph_(graph)
131  {
132  }
133 
134  public:
135  void set_parent_cluster(Cluster *cluster);
137  {
138  this->set_parent_cluster(&cluster);
139  }
140 
142  {
143  return cluster_;
144  }
145 
146  void set_shape(Attr_shape shape)
147  {
148  attributes.set("shape", shape_to_string(shape));
149  }
150 
151  /* See https://www.graphviz.org/doc/info/attrs.html#k:color. */
153  {
154  attributes.set("fillcolor", name);
155  attributes.set("style", "filled");
156  }
157 
158  void export__as_id(std::stringstream &ss) const;
159 
160  void export__as_declaration(std::stringstream &ss) const;
161 };
162 
163 class UndirectedGraph final : public Graph {
164  private:
166 
167  public:
168  std::string to_dot_string() const;
169 
171 };
172 
173 class DirectedGraph final : public Graph {
174  private:
176 
177  public:
178  std::string to_dot_string() const;
179 
181 };
182 
183 class NodePort {
184  private:
185  Node *node_;
186  std::optional<std::string> port_name_;
187 
188  public:
189  NodePort(Node &node, std::optional<std::string> port_name = {})
190  : node_(&node), port_name_(std::move(port_name))
191  {
192  }
193 
194  void to_dot_string(std::stringstream &ss) const;
195 };
196 
198  protected:
201 
202  public:
204 
205  public:
206  Edge(NodePort a, NodePort b) : a_(std::move(a)), b_(std::move(b))
207  {
208  }
209 
211  {
212  attributes.set("arrowhead", arrowType_to_string(type));
213  }
214 
216  {
217  attributes.set("arrowtail", arrowType_to_string(type));
218  }
219 
221  {
223  }
224 
226  {
227  attributes.set("label", label);
228  }
229 };
230 
231 class DirectedEdge : public Edge {
232  public:
233  DirectedEdge(NodePort from, NodePort to) : Edge(std::move(from), std::move(to))
234  {
235  }
236 
237  void export__as_edge_statement(std::stringstream &ss) const;
238 };
239 
240 class UndirectedEdge : public Edge {
241  public:
242  UndirectedEdge(NodePort a, NodePort b) : Edge(std::move(a), std::move(b))
243  {
244  }
245 
246  void export__as_edge_statement(std::stringstream &ss) const;
247 };
248 
249 std::string color_attr_from_hsv(float h, float s, float v);
250 
252  private:
253  Node *node_;
254 
255  public:
257  StringRef name,
258  Span<std::string> input_names,
259  Span<std::string> output_names);
260 
262  {
263  return *node_;
264  }
265 
266  NodePort input(int index) const
267  {
268  std::string port = "\"in" + std::to_string(index) + "\"";
269  return NodePort(*node_, port);
270  }
271 
272  NodePort output(int index) const
273  {
274  std::string port = "\"out" + std::to_string(index) + "\"";
275  return NodePort(*node_, port);
276  }
277 };
278 
279 } // namespace blender::dot
#define final(a, b, c)
Definition: BLI_hash.h:21
_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:280
void export__as_bracket_list(std::stringstream &ss) const
Definition: dot_export.cc:205
void set(StringRef key, StringRef value)
void set(StringRef key, float value)
void set_parent_cluster(Cluster *new_parent)
Definition: dot_export.cc:44
bool contains(Node &node) const
Definition: dot_export.cc:106
std::string name() const
void export__declare_nodes_and_clusters(std::stringstream &ss) const
Definition: dot_export.cc:168
void set_parent_cluster(Cluster &cluster)
void set_random_cluster_bgcolors()
Definition: dot_export.cc:94
DirectedEdge(NodePort from, NodePort to)
void export__as_edge_statement(std::stringstream &ss) const
Definition: dot_export.cc:187
DirectedEdge & new_edge(NodePort from, NodePort to)
Definition: dot_export.cc:37
std::string to_dot_string() const
Definition: dot_export.cc:121
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:21
void set_random_cluster_bgcolors()
Definition: dot_export.cc:87
Node & new_node(StringRef label)
Definition: dot_export.cc:12
void export__declare_nodes_and_clusters(std::stringstream &ss) const
Definition: dot_export.cc:153
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:241
NodePort output(int index) const
NodeWithSocketsRef(Node &node, StringRef name, Span< std::string > input_names, Span< std::string > output_names)
Definition: dot_export.cc:256
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:233
void set_background_color(StringRef name)
void export__as_id(std::stringstream &ss) const
Definition: dot_export.cc:228
void set_parent_cluster(Cluster *cluster)
Definition: dot_export.cc:64
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:196
UndirectedEdge & new_edge(NodePort a, NodePort b)
Definition: dot_export.cc:30
std::string to_dot_string() const
Definition: dot_export.cc:137
OperationNode * node
Depsgraph * graph
StackEntry * from
const char * label
static unsigned a[3]
Definition: RandGen.cpp:78
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:249
std::string to_string(const T &n)
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
_W64 unsigned int uintptr_t
Definition: stdint.h:119