Blender V5.3
BLI_dot_export.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
13
14#pragma once
15
16#include "BLI_map.hh"
17#include "BLI_set.hh"
18#include "BLI_utility_mixins.hh"
19#include "BLI_vector.hh"
20
22#include "BLI_dot_export_fwd.hh"
23
24#include <iosfwd>
25#include <optional>
26
27namespace blender::dot_export {
28
30 private:
32
33 public:
34 void export__as_bracket_list(std::stringstream &ss) const;
35
36 void set(StringRef key, StringRef value)
37 {
38 attributes_.add_overwrite(key, value);
39 }
40
41 void set(StringRef key, float value)
42 {
43 attributes_.add_overwrite(key, std::to_string(value));
44 }
45};
46
47class Graph {
48 private:
51
52 Set<Node *> top_level_nodes_;
53 Set<Cluster *> top_level_clusters_;
54
55 friend Cluster;
56 friend Node;
57
58 public:
60
61 Node &new_node(StringRef label);
62 Cluster &new_cluster(StringRef label = "");
63
64 void export__declare_nodes_and_clusters(std::stringstream &ss) const;
65
67 {
68 attributes.set("rankdir", rankdir_to_string(rankdir));
69 }
70
72};
73
74class Cluster {
75 private:
76 Graph &graph_;
77 Cluster *parent_ = nullptr;
78 Set<Cluster *> children_;
79 Set<Node *> nodes_;
80
81 friend Graph;
82 friend Node;
83
84 public:
86
87 Cluster(Graph &graph) : graph_(graph) {}
88
89 void export__declare_nodes_and_clusters(std::stringstream &ss) const;
90
91 std::string name() const
92 {
93 return "cluster_" + std::to_string(uintptr_t(this));
94 }
95
96 void set_parent_cluster(Cluster *new_parent);
98 {
99 this->set_parent_cluster(&cluster);
100 }
101
103 {
104 return parent_;
105 }
106
108
109 bool contains(Node &node) const;
110};
111
112class Node {
113 private:
114 Graph &graph_;
115 Cluster *cluster_ = nullptr;
116
117 friend Graph;
118
119 public:
121
122 Node(Graph &graph) : graph_(graph) {}
123
124 void set_parent_cluster(Cluster *cluster);
126 {
127 this->set_parent_cluster(&cluster);
128 }
129
131 {
132 return cluster_;
133 }
134
136 {
137 attributes.set("shape", shape_to_string(shape));
138 }
139
140 /* See https://www.graphviz.org/doc/info/attrs.html#k:color. */
142 {
143 attributes.set("fillcolor", name);
144 attributes.set("style", "filled");
145 }
146
147 void export__as_id(std::stringstream &ss) const;
148
149 void export__as_declaration(std::stringstream &ss) const;
150};
151
153 private:
155
156 public:
157 std::string to_dot_string() const;
158
160};
161
162class DirectedGraph final : public Graph {
163 private:
165
166 public:
167 std::string to_dot_string() const;
168
170};
171
172class NodePort {
173 private:
174 Node *node_;
175 std::optional<std::string> port_name_;
176 std::optional<std::string> port_position_;
177
178 public:
180 std::optional<std::string> port_name = {},
181 std::optional<std::string> port_position = {})
182 : node_(&node), port_name_(std::move(port_name)), port_position_(std::move(port_position))
183 {
184 }
185
186 void to_dot_string(std::stringstream &ss) const;
187};
188
190 protected:
193
194 public:
196
197 Edge(NodePort a, NodePort b) : a_(std::move(a)), b_(std::move(b)) {}
198
200 {
201 attributes.set("arrowhead", arrowType_to_string(type));
202 }
203
205 {
206 attributes.set("arrowtail", arrowType_to_string(type));
207 }
208
210 {
211 attributes.set("dir", dirType_to_string(type));
212 }
213
215 {
216 attributes.set("label", label);
217 }
218};
219
220class DirectedEdge : public Edge {
221 public:
222 DirectedEdge(NodePort from, NodePort to) : Edge(std::move(from), std::move(to)) {}
223
224 void export__as_edge_statement(std::stringstream &ss) const;
225};
226
227class UndirectedEdge : public Edge {
228 public:
229 UndirectedEdge(NodePort a, NodePort b) : Edge(std::move(a), std::move(b)) {}
230
231 void export__as_edge_statement(std::stringstream &ss) const;
232};
233
234std::string color_attr_from_hsv(float h, float s, float v);
235
237 struct Socket {
238 std::string name;
239 std::optional<std::string> fontcolor;
240 };
241 struct Input : public Socket {};
242 struct Output : public Socket {};
243
244 std::string node_name;
247
248 Input &add_input(std::string name)
249 {
250 this->inputs.append({});
251 Input &input = this->inputs.last();
252 input.name = std::move(name);
253 return input;
254 }
255
256 Output &add_output(std::string name)
257 {
258 this->outputs.append({});
259 Output &output = this->outputs.last();
260 output.name = std::move(name);
261 return output;
262 }
263};
264
266 private:
267 Node *node_;
268
269 public:
271
273 {
274 return *node_;
275 }
276
278 {
279 std::string port = "\"in" + std::to_string(index) + "\"";
280 return NodePort(*node_, port, "w");
281 }
282
284 {
285 std::string port = "\"out" + std::to_string(index) + "\"";
286 return NodePort(*node_, port, "e");
287 }
288};
289
290} // namespace blender::dot_export
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define output
void append(const T &value)
const T & last(const int64_t n=0) const
NonCopyable(const NonCopyable &other)=delete
NonMovable(NonMovable &&other)=delete
void set(StringRef key, float value)
void set(StringRef key, StringRef value)
void export__as_bracket_list(std::stringstream &ss) const
void set_parent_cluster(Cluster &cluster)
bool contains(Node &node) const
void set_parent_cluster(Cluster *new_parent)
Definition dot_export.cc:53
void export__declare_nodes_and_clusters(std::stringstream &ss) const
DirectedEdge(NodePort from, NodePort to)
void export__as_edge_statement(std::stringstream &ss) const
std::string to_dot_string() const
DirectedEdge & new_edge(NodePort from, NodePort to)
Definition dot_export.cc:46
void set_arrowhead(Attr_arrowType type)
void set_arrowtail(Attr_arrowType type)
void set_label(StringRef label)
void set_dir(Attr_dirType type)
Edge(NodePort a, NodePort b)
Cluster & new_cluster(StringRef label="")
Definition dot_export.cc:30
void set_rankdir(Attr_rankdir rankdir)
void export__declare_nodes_and_clusters(std::stringstream &ss) const
Node & new_node(StringRef label)
Definition dot_export.cc:21
void to_dot_string(std::stringstream &ss) const
NodePort(Node &node, std::optional< std::string > port_name={}, std::optional< std::string > port_position={})
NodeWithSocketsRef(Node &node, const NodeWithSockets &data)
void export__as_declaration(std::stringstream &ss) const
void set_shape(Attr_shape shape)
void export__as_id(std::stringstream &ss) const
void set_parent_cluster(Cluster *cluster)
Definition dot_export.cc:73
void set_parent_cluster(Cluster &cluster)
void set_background_color(StringRef name)
UndirectedEdge(NodePort a, NodePort b)
void export__as_edge_statement(std::stringstream &ss) const
UndirectedEdge & new_edge(NodePort a, NodePort b)
Definition dot_export.cc:39
StringRef dirType_to_string(Attr_dirType value)
StringRef shape_to_string(Attr_shape value)
StringRef arrowType_to_string(Attr_arrowType value)
std::string color_attr_from_hsv(float h, float s, float v)
StringRef rankdir_to_string(Attr_rankdir value)
ATTR_WARN_UNUSED_RESULT const BMVert * v
BMesh const char void * data
_W64 unsigned int uintptr_t
Definition BKE_text.h:120
Input & add_input(std::string name)
Output & add_output(std::string name)