Blender V5.3
dot_export.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <iomanip>
10
11#include "BLI_dot_export.hh"
12
13#include <sstream>
14
15namespace blender::dot_export {
16
17/* -------------------------------------------------------------------- */
20
22{
23 Node *node = new Node(*this);
24 nodes_.append(std::unique_ptr<Node>(node));
25 top_level_nodes_.add_new(node);
26 node->attributes.set("label", label);
27 return *node;
28}
29
31{
32 Cluster *cluster = new Cluster(*this);
33 clusters_.append(std::unique_ptr<Cluster>(cluster));
34 top_level_clusters_.add_new(cluster);
35 cluster->attributes.set("label", label);
36 return *cluster;
37}
38
40{
41 UndirectedEdge *edge = new UndirectedEdge(a, b);
42 edges_.append(std::unique_ptr<UndirectedEdge>(edge));
43 return *edge;
44}
45
47{
48 DirectedEdge *edge = new DirectedEdge(from, to);
49 edges_.append(std::unique_ptr<DirectedEdge>(edge));
50 return *edge;
51}
52
54{
55 if (parent_ == new_parent) {
56 return;
57 }
58 if (parent_ == nullptr) {
59 graph_.top_level_clusters_.remove(this);
60 new_parent->children_.add_new(this);
61 }
62 else if (new_parent == nullptr) {
63 parent_->children_.remove(this);
64 graph_.top_level_clusters_.add_new(this);
65 }
66 else {
67 parent_->children_.remove(this);
68 new_parent->children_.add_new(this);
69 }
70 parent_ = new_parent;
71}
72
74{
75 if (cluster_ == cluster) {
76 return;
77 }
78 if (cluster_ == nullptr) {
79 graph_.top_level_nodes_.remove(this);
80 cluster->nodes_.add_new(this);
81 }
82 else if (cluster == nullptr) {
83 cluster_->nodes_.remove(this);
84 graph_.top_level_nodes_.add_new(this);
85 }
86 else {
87 cluster_->nodes_.remove(this);
88 cluster->nodes_.add_new(this);
89 }
90 cluster_ = cluster;
91}
92
94
95/* -------------------------------------------------------------------- */
98
100{
101 for (Cluster *cluster : top_level_clusters_) {
102 cluster->set_random_cluster_bgcolors();
103 }
104}
105
107{
108 float hue = rand() / float(RAND_MAX);
109 float staturation = 0.3f;
110 float value = 0.8f;
111 this->attributes.set("bgcolor", color_attr_from_hsv(hue, staturation, value));
112
113 for (Cluster *cluster : children_) {
114 cluster->set_random_cluster_bgcolors();
115 }
116}
117
118bool Cluster::contains(Node &node) const
119{
120 Cluster *current = node.parent_cluster();
121 while (current != nullptr) {
122 if (current == this) {
123 return true;
124 }
125 current = current->parent_;
126 }
127 return false;
128}
129
131
132/* -------------------------------------------------------------------- */
135
137{
138 std::stringstream ss;
139 ss << "digraph {\n";
141 ss << "\n";
142
143 for (const std::unique_ptr<DirectedEdge> &edge : edges_) {
144 edge->export__as_edge_statement(ss);
145 ss << "\n";
146 }
147
148 ss << "}\n";
149 return ss.str();
150}
151
153{
154 std::stringstream ss;
155 ss << "graph {\n";
157 ss << "\n";
158
159 for (const std::unique_ptr<UndirectedEdge> &edge : edges_) {
160 edge->export__as_edge_statement(ss);
161 ss << "\n";
162 }
163
164 ss << "}\n";
165 return ss.str();
166}
167
168void Graph::export__declare_nodes_and_clusters(std::stringstream &ss) const
169{
170 ss << "graph ";
172 ss << "\n\n";
173
174 for (Node *node : top_level_nodes_) {
175 node->export__as_declaration(ss);
176 }
177
178 for (Cluster *cluster : top_level_clusters_) {
179 cluster->export__declare_nodes_and_clusters(ss);
180 }
181}
182
183void Cluster::export__declare_nodes_and_clusters(std::stringstream &ss) const
184{
185 ss << "subgraph " << this->name() << " {\n";
186
187 ss << "graph ";
189 ss << "\n\n";
190
191 for (Node *node : nodes_) {
192 node->export__as_declaration(ss);
193 }
194
195 for (Cluster *cluster : children_) {
196 cluster->export__declare_nodes_and_clusters(ss);
197 }
198
199 ss << "}\n";
200}
201
202void DirectedEdge::export__as_edge_statement(std::stringstream &ss) const
203{
204 a_.to_dot_string(ss);
205 ss << " -> ";
206 b_.to_dot_string(ss);
207 ss << " ";
209}
210
211void UndirectedEdge::export__as_edge_statement(std::stringstream &ss) const
212{
213 a_.to_dot_string(ss);
214 ss << " -- ";
215 b_.to_dot_string(ss);
216 ss << " ";
218}
219
220void Attributes::export__as_bracket_list(std::stringstream &ss) const
221{
222 ss << "[";
223 attributes_.foreach_item([&](StringRef key, StringRef value) {
224 if (StringRef(value).startswith("<")) {
225 /* Don't draw the quotes, this is an HTML-like value. */
226 ss << key << "=" << value << ", ";
227 }
228 else {
229 ss << key << "=\"";
230 for (char c : value) {
231 if (c == '\"') {
232 /* Escape double quotes. */
233 ss << '\\';
234 }
235 ss << c;
236 }
237 ss << "\", ";
238 }
239 });
240 ss << "]";
241}
242
243void Node::export__as_id(std::stringstream &ss) const
244{
245 ss << '"' << uintptr_t(this) << '"';
246}
247
248void Node::export__as_declaration(std::stringstream &ss) const
249{
250 this->export__as_id(ss);
251 ss << " ";
253 ss << "\n";
254}
255
256void NodePort::to_dot_string(std::stringstream &ss) const
257{
258 node_->export__as_id(ss);
259 if (port_name_.has_value()) {
260 ss << ":" << *port_name_;
261 }
262 if (port_position_.has_value()) {
263 ss << ":" << *port_position_;
264 }
265}
266
267std::string color_attr_from_hsv(float h, float s, float v)
268{
269 std::stringstream ss;
270 ss << std::setprecision(4) << h << ' ' << s << ' ' << v;
271 return ss.str();
272}
273
275{
276 std::stringstream ss;
277
278 ss << R"(<<table border="0" cellspacing="3">)";
279
280 /* Header */
281 ss << R"(<tr><td colspan="3" align="center"><b>)";
282 ss << (data.node_name.empty() ? "No Name" : data.node_name);
283 ss << "</b></td></tr>";
284
285 /* Sockets */
286 int socket_max_amount = std::max(data.inputs.size(), data.outputs.size());
287 for (int i = 0; i < socket_max_amount; i++) {
288 ss << "<tr>";
289 if (i < data.inputs.size()) {
290 const NodeWithSockets::Input &input = data.inputs[i];
291 ss << R"(<td align="left" port="in)" << i << "\">";
292 if (input.fontcolor) {
293 ss << R"(<font color=")" << *input.fontcolor << "\">";
294 }
295 ss << (input.name.empty() ? "No Name" : input.name);
296 if (input.fontcolor) {
297 ss << "</font>";
298 }
299 ss << "</td>";
300 }
301 else {
302 ss << "<td></td>";
303 }
304 ss << "<td></td>";
305 if (i < data.outputs.size()) {
306 const NodeWithSockets::Output &output = data.outputs[i];
307 ss << R"(<td align="right" port="out)" << i << "\">";
308 if (output.fontcolor) {
309 ss << R"(<font color=")" << *output.fontcolor << "\">";
310 }
311 ss << (output.name.empty() ? "No Name" : output.name);
312 if (output.fontcolor) {
313 ss << "</font>";
314 }
315 ss << "</td>";
316 }
317 else {
318 ss << "<td></td>";
319 }
320 ss << "</tr>";
321 }
322
323 ss << "</table>>";
324
325 node_->attributes.set("label", ss.str());
326 node_->set_shape(Attr_shape::Rectangle);
327}
328
330
331} // namespace blender::dot_export
constexpr StringRef()
void set(StringRef key, StringRef value)
void export__as_bracket_list(std::stringstream &ss) const
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
Cluster & new_cluster(StringRef label="")
Definition dot_export.cc:30
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
NodeWithSocketsRef(Node &node, const NodeWithSockets &data)
void export__as_declaration(std::stringstream &ss) const
void export__as_id(std::stringstream &ss) const
void set_parent_cluster(Cluster *cluster)
Definition dot_export.cc:73
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
std::string color_attr_from_hsv(float h, float s, float v)
ATTR_WARN_UNUSED_RESULT const BMVert * v
BMesh const char void * data
_W64 unsigned int uintptr_t
Definition BKE_text.h:120
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float