From: Magnus Jacobsson Date: Sat, 27 Aug 2022 11:13:59 +0000 (+0200) Subject: tests: SVGAnalyzer: add support for adding outline overlap bounding boxes to the... X-Git-Tag: 6.0.2~43^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f531d5c062e12e661f51bc3b88b2ac44292bc031;p=graphviz tests: SVGAnalyzer: add support for adding outline overlap bounding boxes to the re-created SVG This will be used in upcoming commits to highlight Graphviz component overlaps. Towards https://gitlab.com/graphviz/graphviz/-/issues/372. --- diff --git a/tests/graphviz_edge.cpp b/tests/graphviz_edge.cpp index 40ffd8e9e..4af31b658 100644 --- a/tests/graphviz_edge.cpp +++ b/tests/graphviz_edge.cpp @@ -1,6 +1,7 @@ #include #include "graphviz_edge.h" +#include "graphviz_node.h" #include "svg_element.h" GraphvizEdge::GraphvizEdge(SVG::SVGElement &svg_g_element) @@ -10,6 +11,11 @@ void GraphvizEdge::add_bbox() { m_svg_g_element.add_bbox(); } void GraphvizEdge::add_outline_bbox() { m_svg_g_element.add_outline_bbox(); } +void GraphvizEdge::add_outline_overlap_bbox(const GraphvizNode &node, + const double tolerance) { + m_svg_g_element.add_outline_overlap_bbox(node.svg_g_element(), tolerance); +} + std::string_view GraphvizEdge::edgeop() const { return m_edgeop; } std::string GraphvizEdge::fillcolor() const { diff --git a/tests/graphviz_edge.h b/tests/graphviz_edge.h index aec812438..7e4e23d15 100644 --- a/tests/graphviz_edge.h +++ b/tests/graphviz_edge.h @@ -3,6 +3,7 @@ #include #include +#include "graphviz_node.h" #include "svg_element.h" /** @@ -22,6 +23,11 @@ public: /// `rect` represents the outline bounding box of the edge. The outline /// bounding box is the bounding box with penwidth taken into account. void add_outline_bbox(); + /// Add an SVG `rect` element representing the overlap between the outline + /// bounding box of the edge and the specified node, to the corresponding `g` + /// element. The outline bounding box is the bounding box with penwidth taken + /// into account. + void add_outline_overlap_bbox(const GraphvizNode &node, double tolerance = 0); /// Return the bounding box of the edge SVG::SVGRect bbox() const; /// Return the center of the edge's bounding box diff --git a/tests/graphviz_graph.cpp b/tests/graphviz_graph.cpp index 9b68e1bed..1cfb1b29a 100644 --- a/tests/graphviz_graph.cpp +++ b/tests/graphviz_graph.cpp @@ -56,6 +56,15 @@ void GraphvizGraph::add_bboxes() { } } +void GraphvizGraph::add_node_edge_outline_bbox_overlaps( + const double tolerance) { + for (auto &node : m_nodes) { + for (auto &edge : m_edges) { + edge.add_outline_overlap_bbox(node, tolerance); + } + } +} + void GraphvizGraph::add_outline_bboxes() { for (auto &node : m_nodes) { node.add_outline_bbox(); diff --git a/tests/graphviz_graph.h b/tests/graphviz_graph.h index d5d771511..8bb73e07e 100644 --- a/tests/graphviz_graph.h +++ b/tests/graphviz_graph.h @@ -24,6 +24,10 @@ public: void add_edge(SVG::SVGElement &svg_g_element); /// Add a Graphviz node to the graph void add_node(SVG::SVGElement &svg_g_element); + /// Add SVG `rect` elements representing the overlap between the outline + /// bounding box of nodes and edges. The outline bounding box is the bounding + /// box with penwidth taken into account. + void add_node_edge_outline_bbox_overlaps(double tolerance = 0); /// Add SVG `rect` elements representing the outline bounding boxes of nodes /// and edges to the corresponding `g` elements. The outline bounding box is /// the bounding box with penwidth taken into account. diff --git a/tests/svg_analyzer.cpp b/tests/svg_analyzer.cpp index ab3182584..42bd4e6e9 100644 --- a/tests/svg_analyzer.cpp +++ b/tests/svg_analyzer.cpp @@ -289,6 +289,12 @@ void SVGAnalyzer::add_bboxes() { } } +void SVGAnalyzer::add_node_edge_outline_bbox_overlaps(double tolerance) { + for (auto &graph : m_graphs) { + graph.add_node_edge_outline_bbox_overlaps(tolerance); + } +} + void SVGAnalyzer::add_outline_bboxes() { for (auto &graph : m_graphs) { graph.add_outline_bboxes(); diff --git a/tests/svg_analyzer.h b/tests/svg_analyzer.h index 72c745734..dab591a41 100644 --- a/tests/svg_analyzer.h +++ b/tests/svg_analyzer.h @@ -61,6 +61,9 @@ public: /// Add SVG `rect` elements representing the bounding boxes of nodes and edges /// to the corresponding `g` elements void add_bboxes(); + /// Add SVG rects showing the overlap between the outline bounding boxes of + /// each node and edge (with penwidth taken into account) + void add_node_edge_outline_bbox_overlaps(double tolerance = 0); /// Add SVG `rect` elements representing the outline bounding boxes of nodes /// and edges to the corresponding `g` elements. The outline bounding box is /// the bounding box with penwidth taken into account. diff --git a/tests/svg_element.cpp b/tests/svg_element.cpp index eb7d3d31f..ab1018b7b 100644 --- a/tests/svg_element.cpp +++ b/tests/svg_element.cpp @@ -131,6 +131,20 @@ void SVG::SVGElement::add_outline_bbox() { add_rect(bbox, "blue"); } +void SVG::SVGElement::add_outline_overlap_bbox(SVG::SVGElement other, + const double tolerance) { + const auto bbox = outline_bbox(); + const auto other_bbox = other.outline_bbox(); + const auto overlap_bbox = bbox.intersection(other_bbox); + if (overlap_bbox.width <= 0 || overlap_bbox.height <= 0) { + return; + } + const auto within_tolerance = + overlap_bbox.width <= tolerance && overlap_bbox.height <= tolerance; + const auto color = within_tolerance ? "yellow" : "red"; + add_rect(overlap_bbox, color); +} + SVG::SVGRect SVG::SVGElement::bbox(bool throw_if_bbox_not_defined) { if (!m_bbox.has_value()) { // negative width and height bbox that will be immediately replaced by the diff --git a/tests/svg_element.h b/tests/svg_element.h index 1640995a8..a2371064f 100644 --- a/tests/svg_element.h +++ b/tests/svg_element.h @@ -105,6 +105,10 @@ public: /// edge to the element. The outline bounding box is the bounding box with /// stroke width taken into account. void add_outline_bbox(); + /// Add an SVG `rect` element representing the overlap between the outline + /// bounding box of the element and another element. The outline bounding box + /// is the bounding box with penwidth taken into account. + void add_outline_overlap_bbox(SVG::SVGElement other, double tolerance = 0); /// Add an SVG `rect` element as a child to the element void add_rect(SVG::SVGRect rect, std::string color); /// \brief Return the value of an attribute retrieved from the element and its