Welcome to mirror list, hosted at ThFree Co, Russian Federation.

dot_export.cc « intern « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 796fd6a934ab299ce83c1560a085045f94ffb5a6 (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* SPDX-License-Identifier: GPL-2.0-or-later */

#include <iomanip>

#include "BLI_dot_export.hh"

namespace blender::dot {

/* Graph Building
 ************************************************/

Node &Graph::new_node(StringRef label)
{
  Node *node = new Node(*this);
  nodes_.append(std::unique_ptr<Node>(node));
  top_level_nodes_.add_new(node);
  node->attributes.set("label", label);
  return *node;
}

Cluster &Graph::new_cluster(StringRef label)
{
  Cluster *cluster = new Cluster(*this);
  clusters_.append(std::unique_ptr<Cluster>(cluster));
  top_level_clusters_.add_new(cluster);
  cluster->attributes.set("label", label);
  return *cluster;
}

UndirectedEdge &UndirectedGraph::new_edge(NodePort a, NodePort b)
{
  UndirectedEdge *edge = new UndirectedEdge(a, b);
  edges_.append(std::unique_ptr<UndirectedEdge>(edge));
  return *edge;
}

DirectedEdge &DirectedGraph::new_edge(NodePort from, NodePort to)
{
  DirectedEdge *edge = new DirectedEdge(from, to);
  edges_.append(std::unique_ptr<DirectedEdge>(edge));
  return *edge;
}

void Cluster::set_parent_cluster(Cluster *new_parent)
{
  if (parent_ == new_parent) {
    return;
  }
  if (parent_ == nullptr) {
    graph_.top_level_clusters_.remove(this);
    new_parent->children_.add_new(this);
  }
  else if (new_parent == nullptr) {
    parent_->children_.remove(this);
    graph_.top_level_clusters_.add_new(this);
  }
  else {
    parent_->children_.remove(this);
    new_parent->children_.add_new(this);
  }
  parent_ = new_parent;
}

void Node::set_parent_cluster(Cluster *cluster)
{
  if (cluster_ == cluster) {
    return;
  }
  if (cluster_ == nullptr) {
    graph_.top_level_nodes_.remove(this);
    cluster->nodes_.add_new(this);
  }
  else if (cluster == nullptr) {
    cluster_->nodes_.remove(this);
    graph_.top_level_nodes_.add_new(this);
  }
  else {
    cluster_->nodes_.remove(this);
    cluster->nodes_.add_new(this);
  }
  cluster_ = cluster;
}

/* Utility methods
 **********************************************/

void Graph::set_random_cluster_bgcolors()
{
  for (Cluster *cluster : top_level_clusters_) {
    cluster->set_random_cluster_bgcolors();
  }
}

void Cluster::set_random_cluster_bgcolors()
{
  float hue = rand() / (float)RAND_MAX;
  float staturation = 0.3f;
  float value = 0.8f;
  this->attributes.set("bgcolor", color_attr_from_hsv(hue, staturation, value));

  for (Cluster *cluster : children_) {
    cluster->set_random_cluster_bgcolors();
  }
}

bool Cluster::contains(Node &node) const
{
  Cluster *current = node.parent_cluster();
  while (current != nullptr) {
    if (current == this) {
      return true;
    }
    current = current->parent_;
  }
  return false;
}

/* Dot Generation
 **********************************************/

std::string DirectedGraph::to_dot_string() const
{
  std::stringstream ss;
  ss << "digraph {\n";
  this->export__declare_nodes_and_clusters(ss);
  ss << "\n";

  for (const std::unique_ptr<DirectedEdge> &edge : edges_) {
    edge->export__as_edge_statement(ss);
    ss << "\n";
  }

  ss << "}\n";
  return ss.str();
}

std::string UndirectedGraph::to_dot_string() const
{
  std::stringstream ss;
  ss << "graph {\n";
  this->export__declare_nodes_and_clusters(ss);
  ss << "\n";

  for (const std::unique_ptr<UndirectedEdge> &edge : edges_) {
    edge->export__as_edge_statement(ss);
    ss << "\n";
  }

  ss << "}\n";
  return ss.str();
}

void Graph::export__declare_nodes_and_clusters(std::stringstream &ss) const
{
  ss << "graph ";
  attributes.export__as_bracket_list(ss);
  ss << "\n\n";

  for (Node *node : top_level_nodes_) {
    node->export__as_declaration(ss);
  }

  for (Cluster *cluster : top_level_clusters_) {
    cluster->export__declare_nodes_and_clusters(ss);
  }
}

void Cluster::export__declare_nodes_and_clusters(std::stringstream &ss) const
{
  ss << "subgraph " << this->name() << " {\n";

  ss << "graph ";
  attributes.export__as_bracket_list(ss);
  ss << "\n\n";

  for (Node *node : nodes_) {
    node->export__as_declaration(ss);
  }

  for (Cluster *cluster : children_) {
    cluster->export__declare_nodes_and_clusters(ss);
  }

  ss << "}\n";
}

void DirectedEdge::export__as_edge_statement(std::stringstream &ss) const
{
  a_.to_dot_string(ss);
  ss << " -> ";
  b_.to_dot_string(ss);
  ss << " ";
  attributes.export__as_bracket_list(ss);
}

void UndirectedEdge::export__as_edge_statement(std::stringstream &ss) const
{
  a_.to_dot_string(ss);
  ss << " -- ";
  b_.to_dot_string(ss);
  ss << " ";
  attributes.export__as_bracket_list(ss);
}

void Attributes::export__as_bracket_list(std::stringstream &ss) const
{
  ss << "[";
  attributes_.foreach_item([&](StringRef key, StringRef value) {
    if (StringRef(value).startswith("<")) {
      /* Don't draw the quotes, this is an HTML-like value. */
      ss << key << "=" << value << ", ";
    }
    else {
      ss << key << "=\"";
      for (char c : value) {
        if (c == '\"') {
          /* Escape double quotes. */
          ss << '\\';
        }
        ss << c;
      }
      ss << "\", ";
    }
  });
  ss << "]";
}

void Node::export__as_id(std::stringstream &ss) const
{
  ss << '"' << (uintptr_t)this << '"';
}

void Node::export__as_declaration(std::stringstream &ss) const
{
  this->export__as_id(ss);
  ss << " ";
  attributes.export__as_bracket_list(ss);
  ss << "\n";
}

void NodePort::to_dot_string(std::stringstream &ss) const
{
  node_->export__as_id(ss);
  if (port_name_.has_value()) {
    ss << ":" << *port_name_;
  }
}

std::string color_attr_from_hsv(float h, float s, float v)
{
  std::stringstream ss;
  ss << std::setprecision(4) << h << ' ' << s << ' ' << v;
  return ss.str();
}

NodeWithSocketsRef::NodeWithSocketsRef(Node &node,
                                       StringRef name,
                                       Span<std::string> input_names,
                                       Span<std::string> output_names)
    : node_(&node)
{
  std::stringstream ss;

  ss << R"(<<table border="0" cellspacing="3">)";

  /* Header */
  ss << R"(<tr><td colspan="3" align="center"><b>)";
  ss << ((name.size() == 0) ? "No Name" : name);
  ss << "</b></td></tr>";

  /* Sockets */
  int socket_max_amount = std::max(input_names.size(), output_names.size());
  for (int i = 0; i < socket_max_amount; i++) {
    ss << "<tr>";
    if (i < input_names.size()) {
      StringRef name = input_names[i];
      if (name.size() == 0) {
        name = "No Name";
      }
      ss << R"(<td align="left" port="in)" << i << "\">";
      ss << name;
      ss << "</td>";
    }
    else {
      ss << "<td></td>";
    }
    ss << "<td></td>";
    if (i < output_names.size()) {
      StringRef name = output_names[i];
      if (name.size() == 0) {
        name = "No Name";
      }
      ss << R"(<td align="right" port="out)" << i << "\">";
      ss << name;
      ss << "</td>";
    }
    else {
      ss << "<td></td>";
    }
    ss << "</tr>";
  }

  ss << "</table>>";

  node_->attributes.set("label", ss.str());
  node_->set_shape(Attr_shape::Rectangle);
}

}  // namespace blender::dot