Debian Codegraph @

tests/test_edge_node_overlap_utilities.cpp

raw
1 #include <format>
2 #include <string>
3 #include <string_view>
4 #include <unordered_set>
5
6 #include <catch2/catch_all.hpp>
7 #include <cmath>
8 #include <unordered_map>
9
10 #include "svg_analyzer.h"
11 #include "test_edge_node_overlap_utilities.h"
12 #include "test_utilities.h"
13 #include <util/unreachable.h>
14
15 /// return union of unordered sets of string views
16 std::unordered_set<std::string_view>
17 union_(const std::unordered_set<std::string_view> &a,
18 const std::unordered_set<std::string_view> &b,
19 const std::unordered_set<std::string_view> &c = {}) {
20 std::unordered_set<std::string_view> ret = a;
21 ret.insert(b.begin(), b.end());
22 ret.insert(c.begin(), c.end());
23
24 return ret;
25 }
26
27 static const std::unordered_set<std::string_view>
28 shapes_not_meeting_edge_vertically = {
29 "plaintext", //
30 "none", //
31 "promoter", //
32 "cds", //
33 "terminator", //
34 "utr", //
35 "primersite", //
36 "restrictionsite", //
37 "fivepoverhang", //
38 "threepoverhang", //
39 "noverhang", //
40 "assembly", //
41 "signature", //
42 "insulator", //
43 "ribosite", //
44 "rnastab", //
45 "proteasesite", //
46 "proteinstab", //
47 };
48
49 static const std::unordered_set<std::string_view>
50 shapes_not_meeting_edge_horizontally = {
51 "plaintext", // has space around the label as if it was a box shape
52 "none", // has space around the label as if it was a box shape
53 };
54
55 static const std::unordered_set<std::string_view> &
56 shapes_not_meeting_edge(const std::string_view rankdir) {
57 if (rankdir == "TB" || rankdir == "BT") {
58 return shapes_not_meeting_edge_vertically;
59 } else if (rankdir == "LR" || rankdir == "RL") {
60 return shapes_not_meeting_edge_horizontally;
61 }
62 UNREACHABLE();
63 }
64
65 static const std::unordered_set<std::string_view> shapes_with_concave_top = {
66 "folder", "tab", "promoter", "rpromoter", "rarrow", "larrow", "lpromoter",
67 };
68
69 static const std::unordered_set<std::string_view> shapes_with_concave_bottom = {
70 "star", "rpromoter", "rarrow", "larrow", "lpromoter"};
71
72 static const std::unordered_set<std::string_view> shapes_with_concave_left = {
73 "component"};
74
75 static const std::unordered_set<std::string_view>
76 shapes_with_left_extreme_not_centered = {
77 "egg", "triangle", "invtriangle", "trapezium", "invtrapezium",
78 "parallelogram", "pentagon", "septagon", "star"};
79
80 static const std::unordered_set<std::string_view>
81 shapes_with_right_extreme_not_centered = {
82 "egg", "triangle", "invtriangle", "trapezium", "invtrapezium",
83 "parallelogram", "pentagon", "septagon", "star"};
84
85 static const std::unordered_set<std::string_view>
86 shapes_with_invisible_descent = {"plain"};
87
88 static const std::unordered_set<std::string_view>
89 shapes_with_invisible_left_extension = {"plain"};
90
91 static const std::unordered_set<std::string_view>
92 shapes_with_invisible_right_extension = {"plain"};
93
94 static const std::unordered_set<std::string_view>
95 shapes_not_to_check_for_overlap_at_top = shapes_with_concave_top;
96
97 static const std::unordered_set<std::string_view>
98 shapes_not_to_check_for_overlap_at_bottom =
99 union_(shapes_with_concave_bottom, shapes_with_invisible_descent);
100
101 static const std::unordered_set<std::string_view>
102 shapes_not_to_check_for_overlap_at_left_side =
103 union_(shapes_with_left_extreme_not_centered, shapes_with_concave_left,
104 shapes_with_invisible_left_extension);
105
106 static const std::unordered_set<std::string_view>
107 shapes_not_to_check_for_overlap_at_right_side =
108 union_(shapes_with_right_extreme_not_centered,
109 shapes_with_invisible_right_extension);
110
111 static const std::unordered_set<std::string_view> &
112 shapes_not_to_check_for_max_overlap_at_edge_head(
113 const std::string_view rankdir) {
114 if (rankdir == "TB") {
115 return shapes_not_to_check_for_overlap_at_top;
116 } else if (rankdir == "BT") {
117 return shapes_not_to_check_for_overlap_at_bottom;
118 } else if (rankdir == "LR") {
119 return shapes_not_to_check_for_overlap_at_left_side;
120 } else if (rankdir == "RL") {
121 return shapes_not_to_check_for_overlap_at_right_side;
122 }
123 UNREACHABLE();
124 }
125
126 const std::unordered_set<std::string_view> &
127 shapes_not_to_check_for_max_overlap_at_edge_tail(
128 const std::string_view rankdir) {
129 if (rankdir == "TB") {
130 return shapes_not_to_check_for_overlap_at_bottom;
131 } else if (rankdir == "BT") {
132 return shapes_not_to_check_for_overlap_at_top;
133 } else if (rankdir == "LR") {
134 return shapes_not_to_check_for_overlap_at_right_side;
135 } else if (rankdir == "RL") {
136 return shapes_not_to_check_for_overlap_at_left_side;
137 }
138 UNREACHABLE();
139 }
140
141 /// return the overlap in the rank direction from an intersection rectangle
142 static double overlap_in_rank_direction(SVG::SVGRect intersection,
143 const std::string_view rankdir) {
144 if (rankdir == "LR" || rankdir == "RL") {
145 return intersection.width;
146 }
147 if (rankdir == "TB" || rankdir == "BT") {
148 return intersection.height;
149 }
150 UNREACHABLE();
151 }
152
153 static bool skip_max_check_at_head_node(std::string_view rankdir,
154 std::string_view node_shape) {
155 return shapes_not_to_check_for_max_overlap_at_edge_head(rankdir).contains(
156 node_shape);
157 }
158
159 static bool skip_max_check_at_tail_node(std::string_view rankdir,
160 std::string_view node_shape) {
161 return shapes_not_to_check_for_max_overlap_at_edge_tail(rankdir).contains(
162 node_shape);
163 }
164
165 static bool skip_min_check_at_head_node(std::string_view rankdir,
166 std::string_view node_shape) {
167 return shapes_not_meeting_edge(rankdir).contains(node_shape);
168 }
169
170 static bool skip_min_check_at_tail_node(std::string_view rankdir,
171 std::string_view node_shape) {
172 return shapes_not_meeting_edge(rankdir).contains(node_shape);
173 }
174
175 /// check overlap between the edge and the nodes and between the edge stem and
176 /// the edge arrows
177 static bool check_analyzed_svg(SVGAnalyzer &svg_analyzer,
178 const graph_options &graph_options,
179 const check_options &check_options) {
180
181 const auto rankdir = graph_options.rankdir;
182 const auto node_shape = graph_options.node_shape;
183 const auto dir = graph_options.dir;
184 const auto primitive_arrowhead_shape =
185 graph_options.primitive_arrowhead_shape;
186 const auto primitive_arrowtail_shape =
187 graph_options.primitive_arrowtail_shape;
188
189 REQUIRE(svg_analyzer.graphs().size() == 1);
190 auto &recreated_graph = svg_analyzer.graphs().back();
191
192 auto &tail_node = recreated_graph.node("a");
193 auto &head_node = recreated_graph.node("b");
194 auto &edge = recreated_graph.edge("a->b");
195
196 auto success = true;
197
198 // macro for doing the actual check and continue the execution in the same test
199 // case even if the assertion fails, while still capturing the result to be
200 // used to decide whether to write SVG files at the end of the test case
201 #define DO_CHECK(condition) \
202 do { \
203 CHECK(condition); \
204 success = success && (condition); \
205 } while (0)
206
207 const auto edge_bbox = edge.outline_bbox();
208
209 // check head node and edge overlap
210 {
211 const auto head_node_bbox = head_node.outline_bbox();
212 const auto overlap_bbox = edge_bbox.intersection(head_node_bbox);
213 INFO("Head node overlap:");
214 INFO(std::format(" width: {:.3f}", overlap_bbox.width));
215 INFO(std::format(" height: {:.3f}", overlap_bbox.height));
216 const auto head_node_edge_overlap =
217 overlap_in_rank_direction(overlap_bbox, rankdir);
218
219 // check maximum head node and edge overlap
220 if (check_options.check_max_edge_node_overlap) {
221 if (!skip_max_check_at_head_node(rankdir, node_shape)) {
222 DO_CHECK(head_node_edge_overlap <=
223 check_options.max_node_edge_overlap +
224 check_options.svg_rounding_error * 2);
225 }
226 }
227
228 // check minimum head node and edge overlap
229 if (check_options.check_min_edge_node_overlap) {
230 if (!skip_min_check_at_head_node(rankdir, node_shape)) {
231 DO_CHECK(head_node_edge_overlap >=
232 check_options.min_node_edge_overlap -
233 check_options.svg_rounding_error * 2);
234 }
235 }
236 }
237
238 // check tail node and edge overlap
239 {
240 const auto tail_node_bbox = tail_node.outline_bbox();
241 const auto overlap_bbox = edge_bbox.intersection(tail_node_bbox);
242 INFO("Tail node overlap:");
243 INFO(std::format(" width: {:.6f}", overlap_bbox.width));
244 INFO(std::format(" height: {:.6f}", overlap_bbox.height));
245 const auto tail_node_edge_overlap =
246 overlap_in_rank_direction(overlap_bbox, rankdir);
247
248 // check maximum tail node and edge overlap
249 if (check_options.check_max_edge_node_overlap) {
250 if (!skip_max_check_at_tail_node(rankdir, node_shape)) {
251 DO_CHECK(tail_node_edge_overlap <=
252 check_options.max_node_edge_overlap +
253 check_options.svg_rounding_error * 2);
254 }
255 }
256
257 // check minimum overlap at edge tail
258 if (check_options.check_min_edge_node_overlap) {
259 if (!skip_min_check_at_tail_node(rankdir, node_shape)) {
260 DO_CHECK(tail_node_edge_overlap >=
261 check_options.min_node_edge_overlap -
262 check_options.svg_rounding_error * 2);
263 }
264 }
265 }
266
267 auto &edge_stem = edge.stem();
268 const auto edge_stem_bbox = edge_stem.outline_bbox();
269
270 // check overlap of edge stem and arrowhead
271 if ((dir == "forward" || dir == "both") &&
272 primitive_arrowhead_shape != "none") {
273 const auto edge_arrowhead_bbox =
274 edge.arrowhead_outline_bbox(dir, primitive_arrowhead_shape);
275 const auto overlap_bbox = edge_stem_bbox.intersection(edge_arrowhead_bbox);
276 INFO("Edge stem and arrowhead overlap:");
277 INFO(std::format(" width: {:.3f}", overlap_bbox.width));
278 INFO(std::format(" height: {:.3f}", overlap_bbox.height));
279 const auto edge_stem_arrowhead_overlap =
280 overlap_in_rank_direction(overlap_bbox, rankdir);
281
282 // check maximum overlap of edge stem and arrowhead
283 if (check_options.check_max_edge_stem_arrow_overlap) {
284 const auto max_edge_stem_arrowhead_overlap =
285 check_options.max_edge_stem_arrow_overlap.at(
286 graph_options.primitive_arrowhead_shape);
287 DO_CHECK(edge_stem_arrowhead_overlap <=
288 max_edge_stem_arrowhead_overlap +
289 check_options.svg_rounding_error * 2);
290 }
291
292 // check minimum overlap of edge stem and arrowhead
293 if (check_options.check_min_edge_stem_arrow_overlap) {
294 const auto min_edge_stem_arrowhead_overlap =
295 check_options.min_edge_stem_arrow_overlap;
296 DO_CHECK(edge_stem_arrowhead_overlap >=
297 min_edge_stem_arrowhead_overlap -
298 check_options.svg_rounding_error * 2);
299 }
300 }
301
302 // check overlap of edge stem and arrowtail
303 if ((dir == "back" || dir == "both") && primitive_arrowtail_shape != "none") {
304 const auto edge_arrowtail_bbox =
305 edge.arrowtail_outline_bbox(dir, primitive_arrowtail_shape);
306 const auto overlap_bbox = edge_stem_bbox.intersection(edge_arrowtail_bbox);
307 INFO("Edge stem and arrowtail overlap:");
308 INFO(std::format(" width: {:.3f}", overlap_bbox.width));
309 INFO(std::format(" height: {:.3f}", overlap_bbox.height));
310 const auto edge_stem_arrowtail_overlap =
311 overlap_in_rank_direction(overlap_bbox, rankdir);
312
313 // check maximum overlap of edge stem and arrowtail
314 if (check_options.check_max_edge_stem_arrow_overlap) {
315 const auto max_edge_stem_arrowtail_overlap =
316 check_options.max_edge_stem_arrow_overlap.at(
317 graph_options.primitive_arrowtail_shape);
318 DO_CHECK(edge_stem_arrowtail_overlap <=
319 max_edge_stem_arrowtail_overlap +
320 check_options.svg_rounding_error * 2);
321 }
322
323 // check minimum overlap of edge stem and arrowtail
324 if (check_options.check_min_edge_stem_arrow_overlap) {
325 const auto min_edge_stem_arrowtail_overlap =
326 check_options.min_edge_stem_arrow_overlap;
327 DO_CHECK(edge_stem_arrowtail_overlap >=
328 min_edge_stem_arrowtail_overlap -
329 check_options.svg_rounding_error * 2);
330 }
331 }
332
333 return success;
334 }
335
336 /// write SVG files for manual analysis if any of the above checks failed or if
337 /// we explicitly have requested it
338 static void write_svg_files(SVGAnalyzer &svg_analyzer,
339 const check_options &check_options,
340 const write_options &write_options) {
341 const std::filesystem::path test_artifacts_directory = "test_artifacts";
342
343 if (write_options.write_original_svg) {
344 // write the original SVG generated by Graphviz to a file
345 const std::filesystem::path filename =
346 write_options.filename_base + "_original.svg";
347 write_to_file(test_artifacts_directory, filename,
348 svg_analyzer.original_svg());
349 }
350
351 if (write_options.write_recreated_svg) {
352 // write the SVG recreated by the SVG analyzer to a file
353 const std::filesystem::path filename =
354 write_options.filename_base + "_recreated.svg";
355 const auto recreated_svg = svg_analyzer.svg_string();
356 write_to_file(test_artifacts_directory, filename, recreated_svg);
357 }
358
359 if (write_options.write_annotated_svg) {
360 // annotate the SVG recreated by the SVG analyzer with bounding boxes
361 // and write to file
362 svg_analyzer.add_bboxes();
363 svg_analyzer.add_outline_bboxes();
364 svg_analyzer.add_node_edge_outline_bbox_overlaps(
365 check_options.max_node_edge_overlap);
366 const std::filesystem::path filename =
367 write_options.filename_base + "_annotated.svg";
368 write_to_file(test_artifacts_directory, filename,
369 svg_analyzer.svg_string());
370 }
371 }
372
373 const std::unordered_set<std::string_view>
374 polygon_shapes_with_left_or_right_corner_unknown_during_layout = {
375 // FIXME: These shapes are represented as a box during layout and do not
376 // get their final shape until the graph is rendered. They have a corner
377 // on their left or right side which the `poly_inside` function is
378 // unaware of.
379 // This causes a lager overlap since the outline of the box is half the
380 // penwidth outside the nominal box, whereas the miter point of the
381 // corner is further away.
382 "cds", // corner on right side, unknown during layout
383 "rpromoter", // corner on right side, unknown during layout
384 "rarrow", // corner on right side, unknown during layout
385 "lpromoter", // corner on left side, unknown during layout
386 "larrow" // corner on left side, unknown during layout
387
388 };
389
390 static bool has_corner_in_rank_direction_unknown_during_layout(
391 const std::string_view shape, const std::string_view rankdir) {
392 if (rankdir == "LR" || rankdir == "RL") {
393 return polygon_shapes_with_left_or_right_corner_unknown_during_layout
394 .contains(shape);
395 }
396 return false;
397 }
398
399 /// generate DOT source based on given options
400 static std::string generate_dot(const graph_options &graph_options) {
401 // use a semi-transparent color to easily see overlaps
402 const auto color = "\"#00000060\"";
403 const auto arrowhead = std::format("{}{}", graph_options.arrowhead_modifier,
404 graph_options.primitive_arrowhead_shape);
405 const auto arrowtail = std::format("{}{}", graph_options.arrowtail_modifier,
406 graph_options.primitive_arrowtail_shape);
407 return std::format(
408 "digraph g1 {{"
409 " graph [rankdir={}]"
410 " node [penwidth={} shape={} color={} fontname=Courier fontsize={}]"
411 " edge [penwidth={} color={} dir={} arrowhead={} "
412 "arrowtail={} arrowsize={}]"
413 " a -> b"
414 "}}",
415 graph_options.rankdir, graph_options.node_penwidth,
416 graph_options.node_shape, color, graph_options.node_fontsize,
417 graph_options.edge_penwidth, color, graph_options.dir, arrowhead,
418 arrowtail, graph_options.edge_arrowsize);
419 }
420
421 void test_edge_node_overlap(const graph_options &graph_options,
422 const tc_check_options &tc_check_options,
423 const write_options &write_options) {
424 const auto dot = generate_dot(graph_options);
425
426 auto svg_analyzer = SVGAnalyzer::make_from_dot(dot);
427
428 // The binary search in the bezier_clip function in lib/common/splines.c has a
429 // limit for when to consider the boundary found and to be the point inside
430 // the boundary. It is the maximum distance between two points on a bezier
431 // curve that are on opposite sides of the node boundary (for shape_clip) or
432 // on the opposite sides of the boundary of a virtual circle at a specified
433 // distance from a given point (for arrow_clip). An margin is needed to
434 // account for the error that this limit introduces.
435 const double graphviz_bezier_clip_margin = 0.5;
436 const int graphviz_num_decimals_in_svg = 2;
437 const double graphviz_max_svg_rounding_error =
438 std::pow(10, -graphviz_num_decimals_in_svg) / 2;
439
440 const std::unordered_map<std::string_view, double>
441 max_edge_stem_arrow_overlap = {
442 {"box",
443 graph_options.edge_penwidth / 2 + graphviz_bezier_clip_margin},
444 {"crow",
445 [&]() { // FIXME: calculate this accurately for crow/vee arrow
446 const auto tip_scale = 2.222222; // empirical value
447 return graph_options.edge_penwidth / 2 * tip_scale +
448 graphviz_bezier_clip_margin;
449 }()},
450 {"curve",
451 graph_options.edge_penwidth / 2 + graphviz_bezier_clip_margin},
452 {"diamond",
453 [&]() { // FIXME: calculate this accurately for diamond arrow
454 const auto tip_scale = 1.8027756377319939; // empirical value
455 return graph_options.edge_penwidth / 2 * tip_scale +
456 graphviz_bezier_clip_margin;
457 }()},
458 {"dot",
459 graph_options.edge_penwidth / 2 + graphviz_bezier_clip_margin},
460 {"icurve",
461 graph_options.edge_penwidth / 2 + graphviz_bezier_clip_margin},
462 {"inv",
463 [&]() { // FIXME: calculate this accurately for normal/inv arrow
464 const auto tip_scale = 2 * 1.5135442928; // empirical value
465 return graph_options.edge_penwidth / 2 * tip_scale +
466 graphviz_bezier_clip_margin;
467 }()},
468 {"normal",
469 graph_options.edge_penwidth / 2 + graphviz_bezier_clip_margin},
470 {"tee",
471 graph_options.edge_penwidth / 2 + graphviz_bezier_clip_margin},
472 {"vee",
473 graph_options.edge_penwidth / 2 + graphviz_bezier_clip_margin},
474 };
475
476 const auto extra_max_node_edge_overlap =
477 has_corner_in_rank_direction_unknown_during_layout(
478 graph_options.node_shape, graph_options.rankdir)
479 // the corner angle is roughly 90 degrees which gives a miter point
480 // penwidth / 2 * sqrt(2) from the nominal corner, i.e., penwidth / 2
481 // * (sqrt(2) - 1) from the outline bounding box which is only what
482 // the `poly_inside` function knows about during layout
483 ? graph_options.node_penwidth / 2 * (std::sqrt(2) - 1)
484 : 0;
485
486 const auto extra_node_edge_overlap_margin =
487 graph_options.node_shape == "plain"
488 // The plain shape has no visible borders and its bounding box is
489 // solely determined by the invisible bounding box of the text and it
490 // may vary between text renderers. We therefore can, and need to,
491 // allow more margin when checking for overlap.
492 ? 0.7
493 : 0;
494
495 const check_options check_options = {
496 .check_max_edge_node_overlap =
497 tc_check_options.check_max_edge_node_overlap,
498 .check_min_edge_node_overlap =
499 tc_check_options.check_min_edge_node_overlap,
500 .check_max_edge_stem_arrow_overlap =
501 tc_check_options.check_max_edge_stem_arrow_overlap,
502 .check_min_edge_stem_arrow_overlap =
503 tc_check_options.check_min_edge_stem_arrow_overlap,
504 .max_node_edge_overlap = graphviz_bezier_clip_margin +
505 extra_max_node_edge_overlap +
506 extra_node_edge_overlap_margin,
507 .min_node_edge_overlap = 0 - extra_node_edge_overlap_margin,
508 .max_edge_stem_arrow_overlap = max_edge_stem_arrow_overlap,
509 .min_edge_stem_arrow_overlap = 0,
510 .svg_rounding_error = graphviz_max_svg_rounding_error,
511 };
512
513 const auto success =
514 check_analyzed_svg(svg_analyzer, graph_options, check_options);
515
516 if (!success || write_options.write_svg_on_success) {
517 write_svg_files(svg_analyzer, check_options, write_options);
518 }
519 }