diff options
author | Jacques Lucke <jacques@blender.org> | 2020-08-05 16:30:22 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2020-08-05 16:30:22 +0300 |
commit | 40ad9890599ed17bccb2be98e48322271c2c407e (patch) | |
tree | 54dcaf57c80c3f0a0e1b69e78d8b1281f002a6eb /source/blender | |
parent | 754a663f6812e504585b07831e7dbc5a7505eced (diff) |
BLI: improve dot exporter interface
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/blenlib/BLI_dot_export.hh | 75 | ||||
-rw-r--r-- | source/blender/blenlib/intern/dot_export.cc | 34 |
2 files changed, 60 insertions, 49 deletions
diff --git a/source/blender/blenlib/BLI_dot_export.hh b/source/blender/blenlib/BLI_dot_export.hh index 8155f8518e4..42fe53d67cf 100644 --- a/source/blender/blenlib/BLI_dot_export.hh +++ b/source/blender/blenlib/BLI_dot_export.hh @@ -44,9 +44,8 @@ class NodePort; class DirectedEdge; class UndirectedEdge; class Cluster; -class AttributeList; -class AttributeList { +class Attributes { private: Map<std::string, std::string> attributes_; @@ -57,11 +56,15 @@ class AttributeList { { attributes_.add_overwrite(key, value); } + + void set(StringRef key, float value) + { + attributes_.add_overwrite(key, std::to_string(value)); + } }; class Graph { private: - AttributeList attributes_; Vector<std::unique_ptr<Node>> nodes_; Vector<std::unique_ptr<Cluster>> clusters_; @@ -72,19 +75,17 @@ class Graph { friend Node; public: + Attributes attributes; + + public: Node &new_node(StringRef label); Cluster &new_cluster(StringRef label = ""); void export__declare_nodes_and_clusters(std::stringstream &ss) const; - void set_attribute(StringRef key, StringRef value) - { - attributes_.set(key, value); - } - void set_rankdir(Attr_rankdir rankdir) { - this->set_attribute("rankdir", rankdir_to_string(rankdir)); + attributes.set("rankdir", rankdir_to_string(rankdir)); } void set_random_cluster_bgcolors(); @@ -92,7 +93,6 @@ class Graph { class Cluster { private: - AttributeList attributes_; Graph &graph_; Cluster *parent_ = nullptr; Set<Cluster *> children_; @@ -101,6 +101,9 @@ class Cluster { friend Graph; friend Node; + public: + Attributes attributes; + Cluster(Graph &graph) : graph_(graph) { } @@ -108,9 +111,9 @@ class Cluster { public: void export__declare_nodes_and_clusters(std::stringstream &ss) const; - void set_attribute(StringRef key, StringRef value) + std::string name() const { - attributes_.set(key, value); + return "cluster_" + std::to_string((uintptr_t)this); } void set_parent_cluster(Cluster *cluster); @@ -119,53 +122,52 @@ class Cluster { this->set_parent_cluster(&cluster); } + Cluster *parent_cluster() + { + return parent_; + } + void set_random_cluster_bgcolors(); + + bool contains(Node &node) const; }; class Node { private: - AttributeList attributes_; Graph &graph_; Cluster *cluster_ = nullptr; friend Graph; - Node(Graph &graph) : graph_(graph) - { - } - public: - const AttributeList &attributes() const - { - return attributes_; - } + Attributes attributes; - AttributeList &attributes() + Node(Graph &graph) : graph_(graph) { - return attributes_; } + public: void set_parent_cluster(Cluster *cluster); void set_parent_cluster(Cluster &cluster) { this->set_parent_cluster(&cluster); } - void set_attribute(StringRef key, StringRef value) + Cluster *parent_cluster() { - attributes_.set(key, value); + return cluster_; } void set_shape(Attr_shape shape) { - this->set_attribute("shape", shape_to_string(shape)); + attributes.set("shape", shape_to_string(shape)); } /* See https://www.graphviz.org/doc/info/attrs.html#k:color. */ void set_background_color(StringRef name) { - this->set_attribute("fillcolor", name); - this->set_attribute("style", "filled"); + attributes.set("fillcolor", name); + attributes.set("style", "filled"); } void export__as_id(std::stringstream &ss) const; @@ -209,38 +211,35 @@ class NodePort { class Edge : blender::NonCopyable, blender::NonMovable { protected: - AttributeList attributes_; NodePort a_; NodePort b_; public: - Edge(NodePort a, NodePort b) : a_(std::move(a)), b_(std::move(b)) - { - } + Attributes attributes; - void set_attribute(StringRef key, StringRef value) + public: + Edge(NodePort a, NodePort b) : a_(std::move(a)), b_(std::move(b)) { - attributes_.set(key, value); } void set_arrowhead(Attr_arrowType type) { - this->set_attribute("arrowhead", arrowType_to_string(type)); + attributes.set("arrowhead", arrowType_to_string(type)); } void set_arrowtail(Attr_arrowType type) { - this->set_attribute("arrowtail", arrowType_to_string(type)); + attributes.set("arrowtail", arrowType_to_string(type)); } void set_dir(Attr_dirType type) { - this->set_attribute("dir", dirType_to_string(type)); + attributes.set("dir", dirType_to_string(type)); } void set_label(StringRef label) { - this->set_attribute("label", label); + attributes.set("label", label); } }; diff --git a/source/blender/blenlib/intern/dot_export.cc b/source/blender/blenlib/intern/dot_export.cc index 48b6dc826d0..8e4aafa4bb8 100644 --- a/source/blender/blenlib/intern/dot_export.cc +++ b/source/blender/blenlib/intern/dot_export.cc @@ -28,7 +28,7 @@ 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->set_attribute("label", label); + node->attributes.set("label", label); return *node; } @@ -37,7 +37,7 @@ 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->set_attribute("label", label); + cluster->attributes.set("label", label); return *cluster; } @@ -110,13 +110,25 @@ void Cluster::set_random_cluster_bgcolors() float hue = rand() / (float)RAND_MAX; float staturation = 0.3f; float value = 0.8f; - this->set_attribute("bgcolor", color_attr_from_hsv(hue, staturation, value)); + 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 **********************************************/ @@ -155,7 +167,7 @@ std::string UndirectedGraph::to_dot_string() const void Graph::export__declare_nodes_and_clusters(std::stringstream &ss) const { ss << "graph "; - attributes_.export__as_bracket_list(ss); + attributes.export__as_bracket_list(ss); ss << "\n\n"; for (Node *node : top_level_nodes_) { @@ -169,10 +181,10 @@ void Graph::export__declare_nodes_and_clusters(std::stringstream &ss) const void Cluster::export__declare_nodes_and_clusters(std::stringstream &ss) const { - ss << "subgraph cluster_" << (uintptr_t)this << " {\n"; + ss << "subgraph " << this->name() << " {\n"; ss << "graph "; - attributes_.export__as_bracket_list(ss); + attributes.export__as_bracket_list(ss); ss << "\n\n"; for (Node *node : nodes_) { @@ -192,7 +204,7 @@ void DirectedEdge::export__as_edge_statement(std::stringstream &ss) const ss << " -> "; b_.to_dot_string(ss); ss << " "; - attributes_.export__as_bracket_list(ss); + attributes.export__as_bracket_list(ss); } void UndirectedEdge::export__as_edge_statement(std::stringstream &ss) const @@ -201,10 +213,10 @@ void UndirectedEdge::export__as_edge_statement(std::stringstream &ss) const ss << " -- "; b_.to_dot_string(ss); ss << " "; - attributes_.export__as_bracket_list(ss); + attributes.export__as_bracket_list(ss); } -void AttributeList::export__as_bracket_list(std::stringstream &ss) const +void Attributes::export__as_bracket_list(std::stringstream &ss) const { ss << "["; attributes_.foreach_item([&](StringRef key, StringRef value) { @@ -228,7 +240,7 @@ void Node::export__as_declaration(std::stringstream &ss) const { this->export__as_id(ss); ss << " "; - attributes_.export__as_bracket_list(ss); + attributes.export__as_bracket_list(ss); ss << "\n"; } @@ -296,7 +308,7 @@ NodeWithSocketsRef::NodeWithSocketsRef(Node &node, ss << "</table>>"; - node_->set_attribute("label", ss.str()); + node_->attributes.set("label", ss.str()); node_->set_shape(Attr_shape::Rectangle); } |