Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
planar-graph-test.cpp
Go to the documentation of this file.
1
4/*
5 * Authors:
6 * Rafał Siejakowski <rs@rs-math.net>
7 *
8 * Copyright 2022 the Authors
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it either under the terms of the GNU Lesser General Public
12 * License version 2.1 as published by the Free Software Foundation
13 * (the "LGPL") or, at your option, under the terms of the Mozilla
14 * Public License Version 1.1 (the "MPL"). If you do not alter this
15 * notice, a recipient may use your version of this file under either
16 * the MPL or the LGPL.
17 *
18 * You should have received a copy of the LGPL along with this library
19 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * You should have received a copy of the MPL along with this library
22 * in the file COPYING-MPL-1.1
23 *
24 * The contents of this file are subject to the Mozilla Public License
25 * Version 1.1 (the "License"); you may not use this file except in
26 * compliance with the License. You may obtain a copy of the License at
27 * http://www.mozilla.org/MPL/
28 *
29 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
30 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
31 * the specific language governing rights and limitations.
32 */
33
34#include <gtest/gtest.h>
35#include <iostream>
36
37#include <2geom/point.h>
38#include <2geom/pathvector.h>
41
42#include "planar-graph.h"
43#include "testing.h"
44
45using namespace Geom;
46
47#define PV(d) (parse_svg_path(d))
48#define PTH(d) (std::move(PV(d)[0]))
49#define REV(d) ((PV(d)[0]).reversed())
50
52struct TestLabel
53{
54 unsigned reversal_count = 0, merge_count = 0, detachment_count = 0;
55 void onReverse() { reversal_count++; }
56 void onMergeWith(TestLabel const &) { merge_count++; }
57 void onDetach() { detachment_count++; }
58};
59
61
62static std::vector<TestLabel> extract_labels(TestGraph const &graph)
63{
64 // Find labels of edges remaining in the graph.
65 std::vector<TestLabel> result;
66 for (auto &e : graph.getEdges()) {
67 if (!e.detached) {
68 result.push_back(e.label);
69 }
70 }
71 return result;
72}
73
74class PlanarGraphTest : public ::testing::Test
75{
76};
77
79TEST(PlanarGraphTest, EdgeInsertion)
80{
81 double const precision = 1e-3;
82 auto graph = TestGraph(precision);
83 graph.insertEdge(PTH("M 0, 0 L 1, 0"));
84 graph.insertEdge(PTH("M 0, 1 L 1, 1")); // } Endpoints near
85 graph.insertEdge(PTH("M 1, 0 L 1, 1.0009")); // } but not exact.
86
87 auto vertices = graph.getVertices();
88
89 // Test vertex clumping within the given precision
90 EXPECT_EQ(vertices.size(), 4);
91 EXPECT_EQ(graph.numEdges(), 3);
92
93 // Test lexicographic vertex position sorting by X and then Y
94 EXPECT_EQ(vertices.front().point(), Point(0, 0));
95 auto after = std::next(vertices.begin());
96 EXPECT_EQ(after->point(), Point(0, 1));
97 ++after;
98 EXPECT_EQ(after->point(), Point(1, 0));
99 EXPECT_TRUE(are_near(vertices.back().point(), Point(1, 1), precision));
100
101 EXPECT_FALSE(graph.isRegularized());
102}
103
105TEST(PlanarGraphTest, InsertDetached)
106{
107 TestGraph graph;
108 auto detached = graph.insertDetached(PTH("M 0,0 A 1,1 0,0,1 2,0 V -2 H 0 Z"));
109
110 auto const &edges = graph.getEdges();
111 EXPECT_EQ(edges.size(), 1);
112 EXPECT_TRUE(edges.at(detached).detached);
113 EXPECT_TRUE(edges.at(detached).inserted_as_detached);
114
115 EXPECT_EQ(graph.numVertices(), 0);
116 EXPECT_EQ(graph.numEdges(false), 0);
117 EXPECT_TRUE(graph.isRegularized());
118}
119
121TEST(PlanarGraphTest, ClosedPathArea)
122{
123 // Square with counter-clockwise oriented boundary, when imagining that the y-axis
124 // points up – expect the area to be +1.
125 auto square_positive = PTH("M 0,0 H 1 V 1 H 0 Z");
126 EXPECT_DOUBLE_EQ(TestGraph::closedPathArea(square_positive), 1.0);
127
128 // Expect negative area for a negatively oriented path.
129 auto triangle_negative = PTH("M 0,0 V 1 L 1,1 Z");
130 EXPECT_DOUBLE_EQ(TestGraph::closedPathArea(triangle_negative), -0.5);
131}
132
134TEST(PlanarGraphTest, Deviation)
135{
136 auto vertical_up = PTH("M 0,0 V 1");
137 auto arc_right1 = PTH("M 0,0 A 1,1 0,1,0 2,0");
138 auto arc_left1 = PTH("M 0,0 A 1,1 0,1,1 -2,0");
139 auto arc_right2 = PTH("M 0,0 A 2,2 0,1,0, 4,0");
140 auto arc_left2 = PTH("M 0,0 A 2,2 0,1,1 -4,0");
141 // A very "flat" Bézier curve deviating to the right but slower than the large arc
142 auto bezier_right = PTH("M 0,0 C 0,50 1,20 2,10");
143
144 EXPECT_TRUE(TestGraph::deviatesLeft(arc_left1, arc_left2));
145 EXPECT_TRUE(TestGraph::deviatesLeft(arc_left2, vertical_up));
146 EXPECT_TRUE(TestGraph::deviatesLeft(vertical_up, arc_right2));
147 EXPECT_TRUE(TestGraph::deviatesLeft(vertical_up, bezier_right));
148 EXPECT_TRUE(TestGraph::deviatesLeft(bezier_right, arc_right2));
149 EXPECT_TRUE(TestGraph::deviatesLeft(arc_right2, arc_right1));
150 EXPECT_TRUE(TestGraph::deviatesLeft(arc_left1, arc_right1));
151 EXPECT_TRUE(TestGraph::deviatesLeft(arc_left2, arc_right1));
152
153 EXPECT_FALSE(TestGraph::deviatesLeft(arc_right1, vertical_up));
154 EXPECT_FALSE(TestGraph::deviatesLeft(arc_right1, arc_right2));
155 EXPECT_FALSE(TestGraph::deviatesLeft(vertical_up, arc_left2));
156 EXPECT_FALSE(TestGraph::deviatesLeft(arc_left2, arc_left1));
157 EXPECT_FALSE(TestGraph::deviatesLeft(arc_right1, arc_left1));
158 EXPECT_FALSE(TestGraph::deviatesLeft(arc_right1, arc_left2));
159}
160
162TEST(PlanarGraphTest, BasicAzimuthalSort)
163{
164 TestGraph graph;
165
166 // Imagine the Y-axis pointing up (as in mathematics)!
167 bool const clockwise = true;
168 unsigned const num_rays = 9;
169 unsigned edges[num_rays];
170
171 // Insert the edges randomly but store them in what we know to be the
172 // clockwise order of outgoing azimuths from the vertex at the origin.
173 edges[7] = graph.insertEdge(PTH("M -0.2, -1 L 0, 0"));
174 edges[1] = graph.insertEdge(PTH("M -1, 0.2 L 0, 0"));
175 edges[4] = graph.insertEdge(PTH("M 0, 0 L 1, 0.2"));
176 edges[6] = graph.insertEdge(PTH("M 0.1, -1 L 0, 0"));
177 edges[2] = graph.insertEdge(PTH("M 0, 0 L -0.3, 1"));
178 edges[0] = graph.insertEdge(PTH("M -1, 0 H 0"));
179 edges[5] = graph.insertEdge(PTH("M 0, 0 L 1, -0.2"));
180 edges[3] = graph.insertEdge(PTH("M 0.2, 1 L 0, 0"));
181 edges[8] = graph.insertEdge(PTH("M -1, -0.1 L 0, 0"));
182
183 // We expect the incidence to edges[0] to be the last one
184 // in the sort order so it should appear first when going clockwise.
185 auto [origin, incidence] = graph.getIncidence(edges[0], TestGraph::Incidence::END);
186 ASSERT_TRUE(origin);
187 ASSERT_TRUE(incidence);
188
189 // Expect ±pi as the azimuth
190 EXPECT_DOUBLE_EQ(std::abs(incidence->azimuth), M_PI);
191
192 // Test sort order
193 for (unsigned i = 0; i < num_rays; i++) {
194 EXPECT_EQ(incidence->index, edges[i]);
195 incidence = (TestGraph::Incidence *)&graph.nextIncidence(*origin, *incidence, clockwise);
196 }
197}
198
200TEST(PlanarGraphTest, PathRetrieval)
201{
202 TestGraph graph;
203
204 Path const path = PTH("M 0,0 L 1,1 C 2,2 4,2 5,1");
205 Path const htap = path.reversed();
206
207 auto edge = graph.insertEdge(path);
208
209 ASSERT_EQ(graph.numEdges(), 1);
210
211 auto [start_point, start_incidence] = graph.getIncidence(edge, TestGraph::Incidence::START);
212 ASSERT_TRUE(start_point);
213 ASSERT_TRUE(start_incidence);
214 EXPECT_EQ(graph.getOutgoingPath(start_incidence), path);
215 EXPECT_EQ(graph.getIncomingPath(start_incidence), htap);
216
217 auto [end_point, end_incidence] = graph.getIncidence(edge, TestGraph::Incidence::END);
218 ASSERT_TRUE(end_point);
219 ASSERT_TRUE(end_incidence);
220 EXPECT_EQ(graph.getIncomingPath(end_incidence), path);
221 EXPECT_EQ(graph.getOutgoingPath(end_incidence), htap);
222}
223
225TEST(PlanarGraphTest, LabelRetrieval)
226{
227 TestGraph graph;
228 TestLabel label;
229
230 label.reversal_count = 420;
231 label.merge_count = 69;
232 label.detachment_count = 111;
233
234 auto edge = graph.insertEdge(PTH("M 0,0 L 1,1"), std::move(label));
235
236 auto retrieved = graph.getEdge(edge).label;
237 EXPECT_EQ(retrieved.reversal_count, 420);
238 EXPECT_EQ(retrieved.merge_count, 69);
239 EXPECT_EQ(retrieved.detachment_count, 111);
240}
241
243TEST(PlanarGraphTest, MergeDuplicate)
244{
245 char const *const d = "M 2, 3 H 0 C 1,4 1,5 0,6 H 10 L 8, 0";
246 char const *const near_d = "M 2.0009,3 H 0 C 1,4 1,5 0,6 H 10.0009 L 8, 0.0005";
247
248 // Test removal of perfect overlap:
249 TestGraph graph;
250 graph.insertEdge(PTH(d));
251 graph.insertEdge(PTH(d)); // exact duplicate
252 graph.regularize();
253
254 EXPECT_TRUE(graph.isRegularized());
255
256 auto remaining = extract_labels(graph);
257
258 // Expect there to be only 1 edge after regularization.
259 ASSERT_EQ(remaining.size(), 1);
260
261 EXPECT_EQ(remaining[0].merge_count, 1); // expect one merge,
262 EXPECT_EQ(remaining[0].reversal_count, 0); // no reversals,
263 EXPECT_EQ(remaining[0].detachment_count, 0); // no detachments.
264
265 // Test removal of imperfect overlaps within numerical precision
266 TestGraph fuzzy{1e-3};
267 fuzzy.insertEdge(PTH(d));
268 fuzzy.insertEdge(PTH(near_d));
269 fuzzy.regularize();
270
271 EXPECT_TRUE(fuzzy.isRegularized());
272
273 auto fuzmaining = extract_labels(fuzzy);
274 ASSERT_EQ(fuzmaining.size(), 1);
275
276 EXPECT_EQ(fuzmaining[0].merge_count, 1); // expect one merge,
277 EXPECT_EQ(fuzmaining[0].reversal_count, 0); // no reversals,
278 EXPECT_EQ(fuzmaining[0].detachment_count, 0); // no detachments.
279
280 // Test overlap of edges with oppositie orientations.
281 TestGraph twoway;
282 twoway.insertEdge(PTH(d));
283 twoway.insertEdge(REV(d));
284 twoway.regularize();
285
286 EXPECT_TRUE(twoway.isRegularized());
287
288 auto left = extract_labels(twoway);
289 ASSERT_EQ(left.size(), 1);
290
291 EXPECT_EQ(left[0].merge_count, 1); // expect one merge,
292 EXPECT_TRUE(left[0].reversal_count == 0 || left[0].reversal_count == 1); // 0 or 1 reversals
293 EXPECT_EQ(left[0].detachment_count, 0); // no detachments.
294}
295
297TEST(PlanarGraphTest, MergePartial)
298{
299 TestGraph graph;
300 auto longer = graph.insertEdge(PTH("M 0, 0 L 10, 10"));
301 auto shorter = graph.insertEdge(PTH("M 0, 0 L 6, 6"));
302
303 EXPECT_EQ(graph.numVertices(), 3);
304
305 graph.regularize();
306
307 EXPECT_EQ(graph.numVertices(), 3);
308 EXPECT_TRUE(graph.isRegularized());
309
310 auto labels = extract_labels(graph);
311 ASSERT_EQ(labels.size(), 2);
312
313 EXPECT_EQ(labels[longer].merge_count, 0);
314 EXPECT_EQ(labels[longer].reversal_count, 0);
315 EXPECT_EQ(labels[longer].detachment_count, 0);
316
317 EXPECT_EQ(labels[shorter].merge_count, 1);
318 EXPECT_EQ(labels[shorter].reversal_count, 0);
319 EXPECT_EQ(labels[shorter].detachment_count, 0);
320
321 // Now the same thing but with edges of opposite orientations.
322 TestGraph graphopp;
323 longer = graphopp.insertEdge(PTH("M 0, 0 L 10, 0"));
324 shorter = graphopp.insertEdge(PTH("M 10, 0 L 5, 0"));
325
326 EXPECT_EQ(graphopp.numVertices(), 3);
327
328 graphopp.regularize();
329
330 EXPECT_EQ(graphopp.numVertices(), 3);
331 EXPECT_TRUE(graphopp.isRegularized());
332
333 labels = extract_labels(graphopp);
334 ASSERT_EQ(labels.size(), 2);
335
336 EXPECT_EQ(labels[longer].merge_count, 0);
337 EXPECT_EQ(labels[longer].reversal_count, 0);
338 EXPECT_EQ(labels[longer].detachment_count, 0);
339
340 EXPECT_EQ(labels[shorter].merge_count, 1);
341 EXPECT_EQ(labels[shorter].reversal_count, 0);
342 EXPECT_EQ(labels[shorter].detachment_count, 0);
343}
344
346TEST(PlanarGraphTest, MergeY)
347{
348 TestGraph graph;
349 auto left = graph.insertEdge(PTH("M 1 0 V 1 L 0, 2"));
350 auto right = graph.insertEdge(PTH("M 1,0 V 1 L 2, 2"));
351
352 EXPECT_EQ(graph.numVertices(), 3);
353 graph.regularize();
354 EXPECT_EQ(graph.numVertices(), 4);
355
356 auto edges = graph.getEdges();
357 EXPECT_EQ(edges.size(), 3);
358
359 EXPECT_TRUE(are_near(edges[right].start->point(), Point(1, 1)));
360}
361
363TEST(PlanarGraphTest, Teardrop)
364{
365 TestGraph graph;
366 auto loop = graph.insertEdge(PTH("M 1,0 A 1,1, 0,0,1 0,1 L 2,2 V 1 H 1 V 0"));
367 // Insert a few unrelated edges
368 auto before = graph.insertEdge(PTH("M 1,0 H 10"));
369 auto after = graph.insertEdge(PTH("M 1,0 H -10"));
370
371 EXPECT_EQ(graph.numVertices(), 3);
372
373 graph.regularize();
374
375 EXPECT_EQ(graph.numVertices(), 3);
376 auto [start_vertex, start_incidence] = graph.getIncidence(loop, TestGraph::Incidence::START);
377 auto [end_vertex, end_incidence] = graph.getIncidence(loop, TestGraph::Incidence::END);
378
379 EXPECT_EQ(start_vertex, end_vertex);
380 ASSERT_NE(start_vertex, nullptr);
381
382 // Check that the incidences have been swapped
383 EXPECT_EQ(start_vertex->cyclicNextIncidence(end_incidence), start_incidence);
384 EXPECT_EQ(start_vertex->cyclicPrevIncidence(start_incidence), end_incidence);
385 auto [b, before_incidence] = graph.getIncidence(before, TestGraph::Incidence::START);
386 EXPECT_EQ(start_vertex->cyclicNextIncidence(before_incidence), end_incidence);
387 auto [a, after_incidence] = graph.getIncidence(after, TestGraph::Incidence::START);
388 EXPECT_EQ(start_vertex->cyclicPrevIncidence(after_incidence), start_incidence);
389}
390
392TEST(PlanarGraphTest, ReglueLasso)
393{
394 TestGraph graph;
395 // Insert a lasso-shaped path (a teardrop with initial self-overlap).
396 auto original_lasso = graph.insertEdge(PTH("M 0,0 V 1 C 0,2 1,3 1,4 "
397 "A 1,1 0,1,1 -1,4 C -1,3 0,2 0,1 V 0"));
398 EXPECT_EQ(graph.numVertices(), 1);
399
400 graph.regularize();
401 EXPECT_EQ(graph.numVertices(), 2);
402 EXPECT_EQ(graph.numEdges(false), 2);
403 EXPECT_TRUE(graph.getEdge(original_lasso).detached);
404
405 auto const &edges = graph.getEdges();
406 // Find the edge from origin and ensure it got glued.
407 auto from_origin = std::find_if(edges.begin(), edges.end(), [](auto const &edge) -> bool {
408 return !edge.detached && (edge.start->point() == Point(0, 0) ||
409 edge.end->point() == Point(0, 0));
410 });
411 ASSERT_NE(from_origin, edges.end());
412 ASSERT_EQ(from_origin->label.merge_count, 1);
413}
414
416TEST(PlanarGraphTest, RemoveCollapsed)
417{
418 TestGraph graph;
419 // Insert a collapsed loop
420 auto collapsed = graph.insertEdge(PTH("M 0,0 L 1,1 L 0,0"));
421 ASSERT_EQ(graph.numEdges(), 1);
422 graph.regularize();
423 ASSERT_EQ(graph.numEdges(false), 0);
424 ASSERT_TRUE(graph.getEdge(collapsed).detached);
425
426 TestGraph fuzzy(1e-3);
427 // Insert a nearly collapsed loop
428 auto nearly = fuzzy.insertEdge(PTH("M 0,0 H 2 V 0.001 L 1,0 H 0"));
429 ASSERT_EQ(fuzzy.numEdges(), 1);
430 fuzzy.regularize();
431 ASSERT_EQ(fuzzy.numEdges(false), 0);
432 ASSERT_TRUE(fuzzy.getEdge(nearly).detached);
433}
434
436TEST(PlanarGraphTest, RemoveWisp)
437{
438 TestGraph graph;
439 // Insert a horizontal segment at the origin towards positive X:
440 graph.insertEdge(PTH("M 0 0 H 1"));
441 // Insert a path with a collapsed Bézier curve towards negative X:
442 graph.insertEdge(PTH("M 0 0 C -1 0 -1 0 0 0"));
443 graph.regularize();
444
445 // Ensure that the folded Bézier is removed (and no segfault occurs).
446 EXPECT_EQ(graph.numEdges(false), 1);
447}
448/*
449 Local Variables:
450 mode:c++
451 c-file-style:"stroustrup"
452 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
453 indent-tabs-mode:nil
454 fill-column:99
455 End:
456*/
457// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
Cartesian point / 2D vector and related operations.
Point origin
Definition aa.cpp:227
Sequence of contiguous curves, aka spline.
Definition path.h:353
Path reversed() const
Obtain a reversed version of the current path.
Definition path.cpp:866
Planar graph - a graph geometrically embedded in the plane.
void regularize(double angle_precision=0x1p-50, bool remove_collapsed_loops=true)
Merge overlapping edges or their portions, adding vertices if necessary.
Path getIncomingPath(Incidence const *incidence) const
Get the incident path, always oriented towards the vertex.
unsigned insertDetached(Path &&path, EdgeLabel &&edge=EdgeLabel())
Move-insert a new labeled edge but do not connect it to the graph.
Edge const & getEdge(size_t index) const
size_t numVertices() const
bool isRegularized() const
Check if the graph has been regularized.
static bool deviatesLeft(Path const &first, Path const &second)
Determine whether the first path deviates to the left of the second.
Path getOutgoingPath(Incidence const *incidence) const
Get the incident path, always oriented away from the vertex.
static double closedPathArea(Path const &path)
Return the signed area enclosed by a closed path.
Incidence const & nextIncidence(VertexIterator const &vertex, IncConstIt const &incidence, bool clockwise=false) const
Go clockwise or counterclockwise around the vertex and find the next incidence.
std::pair< Vertex *, Incidence * > getIncidence(unsigned edge_index, Incidence::Sign sign) const
Find the incidence at the specified endpoint of the edge.
std::vector< Edge > const & getEdges() const
size_t numEdges(bool include_detached=true) const
unsigned insertEdge(Path &&path, EdgeLabel &&edge=EdgeLabel())
Move-insert a new labeled edge into the planar graph.
Two-dimensional point that doubles as a vector.
Definition point.h:66
Css & result
Geom::Point start
Glib::ustring label
Various utility functions.
Definition affine.h:22
TEST(AffineTest, Equality)
bool are_near(Affine const &a1, Affine const &a2, Coord eps=EPSILON)
PathVector - a sequence of subpaths.
static std::vector< TestLabel > extract_labels(TestGraph const &graph)
PlanarGraph< TestLabel > TestGraph
Edges edges(Path const &p, Crossings const &cr, unsigned ix)
Definition sanitize.cpp:36
bool detached
Whether the edge is detached from the graph.
EdgeLabel label
The user-supplied label of the edge.
Represents the joint between an edge and a vertex.
parse SVG path specifications
Path sink which writes an SVG-compatible command string.