KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_shape_poly_set_iterator.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) 2017 CERN
5 * @author Alejandro GarcĂ­a Montoro <[email protected]>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
26
29
30#include "fixtures_geometry.h"
31
36{
37 // Structure to store the common data.
39
40 // Polygons to test whether the RemoveNullSegments method works
44
45 // Null segments points
46 std::vector<VECTOR2I> nullPoints;
47
49 {
50 nullPoints.emplace_back( 100, 100 );
51 nullPoints.emplace_back( 0, 100 );
52 nullPoints.emplace_back( 0, 0 );
53
54 // Create a polygon with its last segment null
55 SHAPE_LINE_CHAIN polyLine;
56 polyLine.Append( nullPoints[0] );
57 polyLine.Append( nullPoints[1] );
58 polyLine.Append( nullPoints[2] );
59 polyLine.Append( nullPoints[2], true );
60 polyLine.SetClosed( true );
61
63
64 // Create a polygon with its first segment null
65 polyLine.Clear();
66 polyLine.Append( nullPoints[0] );
67 polyLine.Append( nullPoints[0], true );
68 polyLine.Append( nullPoints[1] );
69 polyLine.Append( nullPoints[2] );
70 polyLine.SetClosed( true );
71
73
74 // Create a polygon with an inside segment null
75 polyLine.Clear();
76 polyLine.Append( nullPoints[0] );
77 polyLine.Append( nullPoints[1] );
78 polyLine.Append( nullPoints[1], true );
79 polyLine.Append( nullPoints[2] );
80 polyLine.SetClosed( true );
81
83 }
84
86 {
87 }
88};
89
93BOOST_FIXTURE_TEST_SUITE( PolygonIterator, IteratorFixture )
94
95
98BOOST_AUTO_TEST_CASE( VertexIterator )
99{
101 int vertexIndex = 0;
102
103 for( iterator = common.holeyPolySet.IterateWithHoles(); iterator; iterator++ )
104 {
105 BOOST_CHECK_EQUAL( common.holeyPoints[vertexIndex], *iterator );
106 vertexIndex++;
107 }
108}
109
113BOOST_AUTO_TEST_CASE( SegmentIterator )
114{
116 int segmentIndex = 0;
117
118 for( iterator = common.holeyPolySet.IterateSegmentsWithHoles(); iterator; iterator++ )
119 {
120 SEG segment = *iterator;
121
122 BOOST_CHECK_EQUAL( common.holeySegments[segmentIndex].A, segment.A );
123 BOOST_CHECK_EQUAL( common.holeySegments[segmentIndex].B, segment.B );
124
125 segmentIndex++;
126 }
127}
128
132BOOST_AUTO_TEST_CASE( EmptyPolygon )
133{
135
136 for( iterator = common.emptyPolySet.IterateSegmentsWithHoles(); iterator; iterator++ )
137 {
138 BOOST_FAIL( "Empty set is being iterated!" );
139 }
140}
141
145BOOST_AUTO_TEST_CASE( UniqueVertex )
146{
148 iterator = common.uniqueVertexPolySet.IterateSegmentsWithHoles();
149
150 SEG segment = *iterator;
151 BOOST_CHECK_EQUAL( segment.A, common.uniquePoints[0] );
152 BOOST_CHECK_EQUAL( segment.B, common.uniquePoints[0] );
153
154 iterator++;
155
156 BOOST_CHECK( !iterator );
157}
158
162BOOST_AUTO_TEST_CASE( TotalVertices )
163{
164 BOOST_CHECK_EQUAL( common.emptyPolySet.TotalVertices(), 0 );
165 BOOST_CHECK_EQUAL( common.uniqueVertexPolySet.TotalVertices(), 1 );
166 BOOST_CHECK_EQUAL( common.solidPolySet.TotalVertices(), 0 );
167 BOOST_CHECK_EQUAL( common.holeyPolySet.TotalVertices(), 12 );
168}
169
173BOOST_AUTO_TEST_CASE( RemoveNullSegments )
174{
175 SHAPE_POLY_SET polygonSets[3] = { lastNullSegmentPolySet, firstNullSegmentPolySet,
176 insideNullSegmentPolySet };
177
178 for( SHAPE_POLY_SET polygonSet : polygonSets )
179 {
180 BOOST_CHECK_EQUAL( polygonSet.TotalVertices(), 4 );
181 BOOST_CHECK_EQUAL( polygonSet.RemoveNullSegments(), 1 );
182 BOOST_CHECK_EQUAL( polygonSet.TotalVertices(), 3 );
183
184 BOOST_CHECK_EQUAL( polygonSet.CVertex( 0 ), nullPoints[0] );
185 BOOST_CHECK_EQUAL( polygonSet.CVertex( 1 ), nullPoints[1] );
186 BOOST_CHECK_EQUAL( polygonSet.CVertex( 2 ), nullPoints[2] );
187 }
188}
189
190
Definition: seg.h:42
VECTOR2I A
Definition: seg.h:49
VECTOR2I B
Definition: seg.h:50
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
void SetClosed(bool aClosed)
Mark the line chain as closed (i.e.
void Clear()
Remove all points from the line chain.
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
Base class for iterating over all vertices in a given SHAPE_POLY_SET.
Base class for iterating over all segments in a given SHAPE_POLY_SET.
Represent a set of closed polygons.
int AddOutline(const SHAPE_LINE_CHAIN &aOutline)
Adds a new outline to the set and returns its index.
Fixture for the Iterator test suite.
SHAPE_POLY_SET insideNullSegmentPolySet
std::vector< VECTOR2I > nullPoints
SHAPE_POLY_SET firstNullSegmentPolySet
struct KI_TEST::CommonTestData common
Common data for some of the SHAPE_POLY_SET tests:
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(VertexIterator)
Declares the IteratorFixture as the boost test suite fixture.