KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_shape_poly_set.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2022 KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
19#include <trigo.h>
20
22#include <qa_utils/numeric.h>
24
25#include "geom_test_utils.h"
26
27
28BOOST_AUTO_TEST_SUITE( ShapePolySet )
29
30BOOST_AUTO_TEST_CASE( RemoveNullSegments )
31{
32 SHAPE_POLY_SET base_set;
33
34 base_set.NewOutline();
35 base_set.Append( 0, 0, -1, -1, true );
36 base_set.Append( 0, 10, -1, -1, true );
37 base_set.Append( 10, 10, -1, -1, true );
38 base_set.Append( 10, 10, -1, -1, true );
39 base_set.Append( 10, 10, -1, -1, true );
40 base_set.Append( 10, 10, -1, -1, true );
41 base_set.Append( 10, 0, -1, -1, true );
42
43 int removed = base_set.RemoveNullSegments();
44
45 BOOST_CHECK_EQUAL( removed, 3 );
46 BOOST_CHECK_EQUAL( base_set.VertexCount(), 4 );
47
48 base_set.DeletePolygon( 0 );
49
50 base_set.NewOutline();
51 base_set.Append( 0, 0, -1, -1, true );
52 base_set.Append( 0, 10, -1, -1, true );
53 base_set.Append( 0, 10, -1, -1, true );
54 base_set.Append( 10, 10, -1, -1, true );
55 base_set.Append( 10, 10, -1, -1, true );
56 base_set.Append( 10, 0, -1, -1, true );
57 base_set.Append( 10, 0, -1, -1, true );
58 base_set.Append( 0, 0, -1, -1, true );
59
60 removed = base_set.RemoveNullSegments();
61
62 BOOST_CHECK_EQUAL( removed, 4 );
63 BOOST_CHECK_EQUAL( base_set.VertexCount(), 4 );
64}
65
66BOOST_AUTO_TEST_CASE( GetNeighbourIndexes )
67{
68 SHAPE_POLY_SET base_set;
69
70 base_set.NewOutline();
71 base_set.Append( 0, 0, -1, -1, true );
72 base_set.Append( 0, 10, -1, -1, true );
73 base_set.Append( 10, 10, -1, -1, true );
74 base_set.Append( 10, 0, -1, -1, true );
75
76 // Check we're testing what we think
77 BOOST_REQUIRE( base_set.OutlineCount() == 1 );
78 BOOST_REQUIRE( base_set.FullPointCount() == 4 );
79
80 int prev = 0;
81 int next = 0;
82 bool ok = false;
83
84 ok = base_set.GetNeighbourIndexes( 0, &prev, &next );
85 BOOST_TEST( ok );
86 BOOST_TEST( prev == 3 );
87 BOOST_TEST( next == 1 );
88
89 ok = base_set.GetNeighbourIndexes( 1, &prev, &next );
90 BOOST_TEST( ok );
91 BOOST_TEST( prev == 0 );
92 BOOST_TEST( next == 2 );
93
94 ok = base_set.GetNeighbourIndexes( 2, &prev, &next );
95 BOOST_TEST( ok );
96 BOOST_TEST( prev == 1 );
97 BOOST_TEST( next == 3 );
98
99 ok = base_set.GetNeighbourIndexes( 3, &prev, &next );
100 BOOST_TEST( ok );
101 BOOST_TEST( prev == 2 );
102 BOOST_TEST( next == 0 );
103
104 ok = base_set.GetNeighbourIndexes( 4, &prev, &next );
105 BOOST_TEST( !ok );
106
107 ok = base_set.GetNeighbourIndexes( -1, &prev, &next );
108 BOOST_TEST( !ok );
109}
110
111BOOST_AUTO_TEST_CASE( GetNeighbourIndexes_MultiOutline )
112{
113 SHAPE_POLY_SET base_set;
114
115 base_set.NewOutline();
116 base_set.Append( 0, 0, -1, -1, true );
117 base_set.Append( 0, 10, -1, -1, true );
118 base_set.Append( 10, 10, -1, -1, true );
119 base_set.Append( 10, 0, -1, -1, true );
120
121 base_set.NewOutline();
122 base_set.Append( 20, 0, -1, -1, true );
123 base_set.Append( 20, 10, -1, -1, true );
124 base_set.Append( 30, 10, -1, -1, true );
125 base_set.Append( 30, 0, -1, -1, true );
126
127 // Check we're testing what we think
128 BOOST_TEST_REQUIRE( base_set.OutlineCount() == 2 );
129 BOOST_TEST_REQUIRE( base_set.FullPointCount() == 8 );
130
131 int next = 0;
132 int prev = 0;
133 bool ok = false;
134
135 // Can we still get outline 0?
136 ok = base_set.GetNeighbourIndexes( 0, &prev, &next );
137 BOOST_TEST( ok );
138 BOOST_TEST( prev == 3 );
139 BOOST_TEST( next == 1 );
140
141 // End out outline 0
142 ok = base_set.GetNeighbourIndexes( 3, &prev, &next );
143 BOOST_TEST( ok );
144 BOOST_TEST( prev == 2 );
145 BOOST_TEST( next == 0 );
146
147 // Check outline 1
148 ok = base_set.GetNeighbourIndexes( 4, &prev, &next );
149 BOOST_TEST( ok );
150 BOOST_TEST( prev == 7 );
151 BOOST_TEST( next == 5 );
152
153 // End out outline 1
154 ok = base_set.GetNeighbourIndexes( 7, &prev, &next );
155 BOOST_TEST( ok );
156 BOOST_TEST( prev == 6 );
157 BOOST_TEST( next == 4 );
158
159 // Bad indexes
160 ok = base_set.GetNeighbourIndexes( 8, &prev, &next );
161 BOOST_TEST( !ok );
162 ok = base_set.GetNeighbourIndexes( -1, &prev, &next );
163 BOOST_TEST( !ok );
164}
165
Represent a set of closed polygons.
int VertexCount(int aOutline=-1, int aHole=-1) const
Return the number of vertices in a given outline/hole.
void DeletePolygon(int aIdx)
Delete aIdx-th polygon from the set.
int FullPointCount() const
Return the number of points in the shape poly set.
int RemoveNullSegments()
Look for null segments; ie, segments whose ends are exactly the same and deletes them.
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Appends a vertex at the end of the given outline/hole (default: the last outline)
bool GetNeighbourIndexes(int aGlobalIndex, int *aPrevious, int *aNext) const
Return the global indexes of the previous and the next corner of the aGlobalIndex-th corner of a cont...
int NewOutline()
Creates a new empty polygon in the set and returns its index.
int OutlineCount() const
Return the number of outlines in the set.
Numerical test predicates.
CITER next(CITER it)
Definition: ptree.cpp:126
BOOST_TEST(box.ClosestPointTo(VECTOR2D(0, 0))==VECTOR2D(1, 2))
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(RemoveNullSegments)