2023-08-15 16:20:26 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2020-04-28 14:05:24 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Language grammar: https://www.graphviz.org/doc/info/lang.html
|
|
|
|
* Attributes: https://www.graphviz.org/doc/info/attrs.html
|
|
|
|
* Node Shapes: https://www.graphviz.org/doc/info/shapes.html
|
|
|
|
* Preview: https://dreampuf.github.io/GraphvizOnline
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "BLI_map.hh"
|
|
|
|
#include "BLI_set.hh"
|
|
|
|
#include "BLI_utility_mixins.hh"
|
|
|
|
#include "BLI_vector.hh"
|
|
|
|
|
|
|
|
#include "BLI_dot_export_attribute_enums.hh"
|
|
|
|
|
Cleanup: fewer iostreams related includes from BLI/BKE headers
Including <iostream> or similar headers is quite expensive, since it
also pulls in things like <locale> and so on. In many BLI headers,
iostreams are only used to implement some sort of "debug print",
or an operator<< for ostream.
Change some of the commonly used places to instead include <iosfwd>,
which is the standard way of forward-declaring iostreams related
classes, and move the actual debug-print / operator<< implementations
into .cc files.
This is not done for templated classes though (it would be possible
to provide explicit operator<< instantiations somewhere in the
source file, but that would lead to hard-to-figure-out linker error
whenever someone would add a different template type). There, where
possible, I changed from full <iostream> include to only the needed
<ostream> part.
For Span<T>, I just removed print_as_lines since it's not used by
anything. It could be moved into a .cc file using a similar approach
as above if needed.
Doing full blender build changes include counts this way:
- <iostream> 1986 -> 978
- <sstream> 2880 -> 925
It does not affect the total build time much though, mostly because
towards the end of it there's just several CPU cores finishing
compiling OpenVDB related source files.
Pull Request: https://projects.blender.org/blender/blender/pulls/111046
2023-08-11 11:27:56 +02:00
|
|
|
#include <iosfwd>
|
2020-06-29 14:29:05 +02:00
|
|
|
#include <optional>
|
2020-04-28 14:05:24 +02:00
|
|
|
|
2020-07-03 14:25:20 +02:00
|
|
|
namespace blender::dot {
|
2020-04-28 14:05:24 +02:00
|
|
|
|
|
|
|
class Graph;
|
|
|
|
class DirectedGraph;
|
|
|
|
class UndirectedGraph;
|
|
|
|
class Node;
|
|
|
|
class NodePort;
|
|
|
|
class DirectedEdge;
|
|
|
|
class UndirectedEdge;
|
|
|
|
class Cluster;
|
|
|
|
|
2020-08-05 15:30:22 +02:00
|
|
|
class Attributes {
|
2020-04-28 14:05:24 +02:00
|
|
|
private:
|
2020-07-03 14:15:05 +02:00
|
|
|
Map<std::string, std::string> attributes_;
|
2020-04-28 14:05:24 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
void export__as_bracket_list(std::stringstream &ss) const;
|
|
|
|
|
|
|
|
void set(StringRef key, StringRef value)
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
attributes_.add_overwrite(key, value);
|
2020-04-28 14:05:24 +02:00
|
|
|
}
|
2020-08-05 15:30:22 +02:00
|
|
|
|
|
|
|
void set(StringRef key, float value)
|
|
|
|
{
|
|
|
|
attributes_.add_overwrite(key, std::to_string(value));
|
|
|
|
}
|
2020-04-28 14:05:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class Graph {
|
|
|
|
private:
|
2020-07-03 14:15:05 +02:00
|
|
|
Vector<std::unique_ptr<Node>> nodes_;
|
|
|
|
Vector<std::unique_ptr<Cluster>> clusters_;
|
2020-04-28 14:05:24 +02:00
|
|
|
|
2020-07-03 14:15:05 +02:00
|
|
|
Set<Node *> top_level_nodes_;
|
|
|
|
Set<Cluster *> top_level_clusters_;
|
2020-04-28 14:05:24 +02:00
|
|
|
|
|
|
|
friend Cluster;
|
|
|
|
friend Node;
|
|
|
|
|
2020-08-05 15:30:22 +02:00
|
|
|
public:
|
|
|
|
Attributes attributes;
|
|
|
|
|
2020-04-28 14:05:24 +02:00
|
|
|
public:
|
|
|
|
Node &new_node(StringRef label);
|
|
|
|
Cluster &new_cluster(StringRef label = "");
|
|
|
|
|
|
|
|
void export__declare_nodes_and_clusters(std::stringstream &ss) const;
|
|
|
|
|
|
|
|
void set_rankdir(Attr_rankdir rankdir)
|
|
|
|
{
|
2020-08-05 15:30:22 +02:00
|
|
|
attributes.set("rankdir", rankdir_to_string(rankdir));
|
2020-04-28 14:05:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_random_cluster_bgcolors();
|
|
|
|
};
|
|
|
|
|
|
|
|
class Cluster {
|
|
|
|
private:
|
2020-07-03 14:15:05 +02:00
|
|
|
Graph &graph_;
|
|
|
|
Cluster *parent_ = nullptr;
|
|
|
|
Set<Cluster *> children_;
|
|
|
|
Set<Node *> nodes_;
|
2020-04-28 14:05:24 +02:00
|
|
|
|
|
|
|
friend Graph;
|
|
|
|
friend Node;
|
|
|
|
|
2020-08-05 15:30:22 +02:00
|
|
|
public:
|
|
|
|
Attributes attributes;
|
|
|
|
|
2023-03-29 16:50:54 +02:00
|
|
|
Cluster(Graph &graph) : graph_(graph) {}
|
2020-04-28 14:05:24 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
void export__declare_nodes_and_clusters(std::stringstream &ss) const;
|
|
|
|
|
2020-08-05 15:30:22 +02:00
|
|
|
std::string name() const
|
2020-04-28 14:05:24 +02:00
|
|
|
{
|
2022-09-26 09:38:25 +02:00
|
|
|
return "cluster_" + std::to_string(uintptr_t(this));
|
2020-04-28 14:05:24 +02:00
|
|
|
}
|
|
|
|
|
2020-09-04 20:59:13 +02:00
|
|
|
void set_parent_cluster(Cluster *new_parent);
|
2020-04-28 14:05:24 +02:00
|
|
|
void set_parent_cluster(Cluster &cluster)
|
|
|
|
{
|
|
|
|
this->set_parent_cluster(&cluster);
|
|
|
|
}
|
|
|
|
|
2020-08-05 15:30:22 +02:00
|
|
|
Cluster *parent_cluster()
|
|
|
|
{
|
|
|
|
return parent_;
|
|
|
|
}
|
|
|
|
|
2020-04-28 14:05:24 +02:00
|
|
|
void set_random_cluster_bgcolors();
|
2020-08-05 15:30:22 +02:00
|
|
|
|
|
|
|
bool contains(Node &node) const;
|
2020-04-28 14:05:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class Node {
|
|
|
|
private:
|
2020-07-03 14:15:05 +02:00
|
|
|
Graph &graph_;
|
|
|
|
Cluster *cluster_ = nullptr;
|
2020-04-28 14:05:24 +02:00
|
|
|
|
|
|
|
friend Graph;
|
|
|
|
|
|
|
|
public:
|
2020-08-05 15:30:22 +02:00
|
|
|
Attributes attributes;
|
2020-04-28 14:05:24 +02:00
|
|
|
|
2023-03-29 16:50:54 +02:00
|
|
|
Node(Graph &graph) : graph_(graph) {}
|
2020-04-28 14:05:24 +02:00
|
|
|
|
2020-08-05 15:30:22 +02:00
|
|
|
public:
|
2020-04-28 14:05:24 +02:00
|
|
|
void set_parent_cluster(Cluster *cluster);
|
|
|
|
void set_parent_cluster(Cluster &cluster)
|
|
|
|
{
|
|
|
|
this->set_parent_cluster(&cluster);
|
|
|
|
}
|
|
|
|
|
2020-08-05 15:30:22 +02:00
|
|
|
Cluster *parent_cluster()
|
2020-04-28 14:05:24 +02:00
|
|
|
{
|
2020-08-05 15:30:22 +02:00
|
|
|
return cluster_;
|
2020-04-28 14:05:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_shape(Attr_shape shape)
|
|
|
|
{
|
2020-08-05 15:30:22 +02:00
|
|
|
attributes.set("shape", shape_to_string(shape));
|
2020-04-28 14:05:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* See https://www.graphviz.org/doc/info/attrs.html#k:color. */
|
|
|
|
void set_background_color(StringRef name)
|
|
|
|
{
|
2020-08-05 15:30:22 +02:00
|
|
|
attributes.set("fillcolor", name);
|
|
|
|
attributes.set("style", "filled");
|
2020-04-28 14:05:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void export__as_id(std::stringstream &ss) const;
|
|
|
|
|
|
|
|
void export__as_declaration(std::stringstream &ss) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
class UndirectedGraph final : public Graph {
|
|
|
|
private:
|
2020-07-03 14:15:05 +02:00
|
|
|
Vector<std::unique_ptr<UndirectedEdge>> edges_;
|
2020-04-28 14:05:24 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
std::string to_dot_string() const;
|
|
|
|
|
|
|
|
UndirectedEdge &new_edge(NodePort a, NodePort b);
|
|
|
|
};
|
|
|
|
|
|
|
|
class DirectedGraph final : public Graph {
|
|
|
|
private:
|
2020-07-03 14:15:05 +02:00
|
|
|
Vector<std::unique_ptr<DirectedEdge>> edges_;
|
2020-04-28 14:05:24 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
std::string to_dot_string() const;
|
|
|
|
|
|
|
|
DirectedEdge &new_edge(NodePort from, NodePort to);
|
|
|
|
};
|
|
|
|
|
|
|
|
class NodePort {
|
|
|
|
private:
|
2020-07-03 14:15:05 +02:00
|
|
|
Node *node_;
|
|
|
|
std::optional<std::string> port_name_;
|
2022-12-29 15:09:52 +01:00
|
|
|
std::optional<std::string> port_position_;
|
2020-04-28 14:05:24 +02:00
|
|
|
|
|
|
|
public:
|
2022-12-29 15:09:52 +01:00
|
|
|
NodePort(Node &node,
|
|
|
|
std::optional<std::string> port_name = {},
|
|
|
|
std::optional<std::string> port_position = {})
|
|
|
|
: node_(&node), port_name_(std::move(port_name)), port_position_(std::move(port_position))
|
2020-04-28 14:05:24 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void to_dot_string(std::stringstream &ss) const;
|
|
|
|
};
|
|
|
|
|
2020-06-09 10:27:24 +02:00
|
|
|
class Edge : blender::NonCopyable, blender::NonMovable {
|
2020-04-28 14:05:24 +02:00
|
|
|
protected:
|
2020-07-03 14:15:05 +02:00
|
|
|
NodePort a_;
|
|
|
|
NodePort b_;
|
2020-04-28 14:05:24 +02:00
|
|
|
|
|
|
|
public:
|
2020-08-05 15:30:22 +02:00
|
|
|
Attributes attributes;
|
2020-04-28 14:05:24 +02:00
|
|
|
|
2020-08-05 15:30:22 +02:00
|
|
|
public:
|
2023-03-29 16:50:54 +02:00
|
|
|
Edge(NodePort a, NodePort b) : a_(std::move(a)), b_(std::move(b)) {}
|
2020-04-28 14:05:24 +02:00
|
|
|
|
|
|
|
void set_arrowhead(Attr_arrowType type)
|
|
|
|
{
|
2020-08-05 15:30:22 +02:00
|
|
|
attributes.set("arrowhead", arrowType_to_string(type));
|
2020-04-28 14:05:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_arrowtail(Attr_arrowType type)
|
|
|
|
{
|
2020-08-05 15:30:22 +02:00
|
|
|
attributes.set("arrowtail", arrowType_to_string(type));
|
2020-04-28 14:05:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_dir(Attr_dirType type)
|
|
|
|
{
|
2020-08-05 15:30:22 +02:00
|
|
|
attributes.set("dir", dirType_to_string(type));
|
2020-04-28 14:05:24 +02:00
|
|
|
}
|
2020-08-05 12:21:35 +02:00
|
|
|
|
|
|
|
void set_label(StringRef label)
|
|
|
|
{
|
2020-08-05 15:30:22 +02:00
|
|
|
attributes.set("label", label);
|
2020-08-05 12:21:35 +02:00
|
|
|
}
|
2020-04-28 14:05:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class DirectedEdge : public Edge {
|
|
|
|
public:
|
2023-03-29 16:50:54 +02:00
|
|
|
DirectedEdge(NodePort from, NodePort to) : Edge(std::move(from), std::move(to)) {}
|
2020-04-28 14:05:24 +02:00
|
|
|
|
|
|
|
void export__as_edge_statement(std::stringstream &ss) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
class UndirectedEdge : public Edge {
|
|
|
|
public:
|
2023-03-29 16:50:54 +02:00
|
|
|
UndirectedEdge(NodePort a, NodePort b) : Edge(std::move(a), std::move(b)) {}
|
2020-04-28 14:05:24 +02:00
|
|
|
|
|
|
|
void export__as_edge_statement(std::stringstream &ss) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string color_attr_from_hsv(float h, float s, float v);
|
|
|
|
|
2022-12-29 15:09:52 +01:00
|
|
|
struct NodeWithSockets {
|
|
|
|
struct Socket {
|
|
|
|
std::string name;
|
|
|
|
std::optional<std::string> fontcolor;
|
|
|
|
};
|
2024-01-02 18:12:54 +01:00
|
|
|
struct Input : public Socket {};
|
|
|
|
struct Output : public Socket {};
|
2022-12-29 15:09:52 +01:00
|
|
|
|
|
|
|
std::string node_name;
|
|
|
|
Vector<Input> inputs;
|
|
|
|
Vector<Output> outputs;
|
|
|
|
|
|
|
|
Input &add_input(std::string name)
|
|
|
|
{
|
|
|
|
this->inputs.append({});
|
|
|
|
Input &input = this->inputs.last();
|
|
|
|
input.name = std::move(name);
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
Output &add_output(std::string name)
|
|
|
|
{
|
|
|
|
this->outputs.append({});
|
|
|
|
Output &output = this->outputs.last();
|
|
|
|
output.name = std::move(name);
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-28 14:05:24 +02:00
|
|
|
class NodeWithSocketsRef {
|
|
|
|
private:
|
2020-07-03 14:15:05 +02:00
|
|
|
Node *node_;
|
2020-04-28 14:05:24 +02:00
|
|
|
|
|
|
|
public:
|
2022-12-29 15:09:52 +01:00
|
|
|
NodeWithSocketsRef(Node &node, const NodeWithSockets &data);
|
2020-04-28 14:05:24 +02:00
|
|
|
|
2020-07-07 18:40:42 +02:00
|
|
|
Node &node()
|
|
|
|
{
|
|
|
|
return *node_;
|
|
|
|
}
|
|
|
|
|
2020-07-20 12:16:20 +02:00
|
|
|
NodePort input(int index) const
|
2020-04-28 14:05:24 +02:00
|
|
|
{
|
|
|
|
std::string port = "\"in" + std::to_string(index) + "\"";
|
2022-12-29 15:09:52 +01:00
|
|
|
return NodePort(*node_, port, "w");
|
2020-04-28 14:05:24 +02:00
|
|
|
}
|
|
|
|
|
2020-07-20 12:16:20 +02:00
|
|
|
NodePort output(int index) const
|
2020-04-28 14:05:24 +02:00
|
|
|
{
|
|
|
|
std::string port = "\"out" + std::to_string(index) + "\"";
|
2022-12-29 15:09:52 +01:00
|
|
|
return NodePort(*node_, port, "e");
|
2020-04-28 14:05:24 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-03 14:25:20 +02:00
|
|
|
} // namespace blender::dot
|