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, see <https://www.gnu.org/licenses/>.
19 */
20
22
25
26#include "fixtures_geometry.h"
27
32{
33 // Structure to store the common data.
35
36 // Polygons to test whether the RemoveNullSegments method works
40
41 // Null segments points
42 std::vector<VECTOR2I> nullPoints;
43
45 {
46 nullPoints.emplace_back( 100, 100 );
47 nullPoints.emplace_back( 0, 100 );
48 nullPoints.emplace_back( 0, 0 );
49
50 // Create a polygon with its last segment null
51 SHAPE_LINE_CHAIN polyLine;
52 polyLine.Append( nullPoints[0] );
53 polyLine.Append( nullPoints[1] );
54 polyLine.Append( nullPoints[2] );
55 polyLine.Append( nullPoints[2], true );
56 polyLine.SetClosed( true );
57
58 lastNullSegmentPolySet.AddOutline( polyLine );
59
60 // Create a polygon with its first segment null
61 polyLine.Clear();
62 polyLine.Append( nullPoints[0] );
63 polyLine.Append( nullPoints[0], true );
64 polyLine.Append( nullPoints[1] );
65 polyLine.Append( nullPoints[2] );
66 polyLine.SetClosed( true );
67
68 firstNullSegmentPolySet.AddOutline( polyLine );
69
70 // Create a polygon with an inside segment null
71 polyLine.Clear();
72 polyLine.Append( nullPoints[0] );
73 polyLine.Append( nullPoints[1] );
74 polyLine.Append( nullPoints[1], true );
75 polyLine.Append( nullPoints[2] );
76 polyLine.SetClosed( true );
77
78 insideNullSegmentPolySet.AddOutline( polyLine );
79 }
80
82 {
83 }
84};
85
89BOOST_FIXTURE_TEST_SUITE( PolygonIterator, IteratorFixture )
90
91
94BOOST_AUTO_TEST_CASE( VertexIterator )
95{
97 int vertexIndex = 0;
98
99 for( iterator = common.holeyPolySet.IterateWithHoles(); iterator; iterator++ )
100 {
101 BOOST_CHECK_EQUAL( common.holeyPoints[vertexIndex], *iterator );
102 vertexIndex++;
103 }
104}
105
109BOOST_AUTO_TEST_CASE( SegmentIterator )
110{
112 int segmentIndex = 0;
113
114 for( iterator = common.holeyPolySet.IterateSegmentsWithHoles(); iterator; iterator++ )
115 {
116 SEG segment = *iterator;
117
118 BOOST_CHECK_EQUAL( common.holeySegments[segmentIndex].A, segment.A );
119 BOOST_CHECK_EQUAL( common.holeySegments[segmentIndex].B, segment.B );
120
121 segmentIndex++;
122 }
123}
124
128BOOST_AUTO_TEST_CASE( EmptyPolygon )
129{
131
132 for( iterator = common.emptyPolySet.IterateSegmentsWithHoles(); iterator; iterator++ )
133 {
134 BOOST_FAIL( "Empty set is being iterated!" );
135 }
136}
137
141BOOST_AUTO_TEST_CASE( UniqueVertex )
142{
144 iterator = common.uniqueVertexPolySet.IterateSegmentsWithHoles();
145
146 SEG segment = *iterator;
147 BOOST_CHECK_EQUAL( segment.A, common.uniquePoints[0] );
148 BOOST_CHECK_EQUAL( segment.B, common.uniquePoints[0] );
149
150 iterator++;
151
152 BOOST_CHECK( !iterator );
153}
154
158BOOST_AUTO_TEST_CASE( TotalVertices )
159{
160 BOOST_CHECK_EQUAL( common.emptyPolySet.TotalVertices(), 0 );
161 BOOST_CHECK_EQUAL( common.uniqueVertexPolySet.TotalVertices(), 1 );
162 BOOST_CHECK_EQUAL( common.solidPolySet.TotalVertices(), 0 );
163 BOOST_CHECK_EQUAL( common.holeyPolySet.TotalVertices(), 12 );
164}
165
169BOOST_AUTO_TEST_CASE( RemoveNullSegments )
170{
171 SHAPE_POLY_SET polygonSets[3] = { lastNullSegmentPolySet, firstNullSegmentPolySet,
172 insideNullSegmentPolySet };
173
174 for( SHAPE_POLY_SET polygonSet : polygonSets )
175 {
176 BOOST_CHECK_EQUAL( polygonSet.TotalVertices(), 4 );
177 BOOST_CHECK_EQUAL( polygonSet.RemoveNullSegments(), 1 );
178 BOOST_CHECK_EQUAL( polygonSet.TotalVertices(), 3 );
179
180 BOOST_CHECK_EQUAL( polygonSet.CVertex( 0 ), nullPoints[0] );
181 BOOST_CHECK_EQUAL( polygonSet.CVertex( 1 ), nullPoints[1] );
182 BOOST_CHECK_EQUAL( polygonSet.CVertex( 2 ), nullPoints[2] );
183 }
184}
185
186
Definition seg.h:38
VECTOR2I A
Definition seg.h:45
VECTOR2I B
Definition seg.h:46
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.
Represent a set of closed polygons.
ITERATOR_TEMPLATE< VECTOR2I > ITERATOR
SEGMENT_ITERATOR_TEMPLATE< SEG > SEGMENT_ITERATOR
Fixture for the Iterator test suite.
std::vector< VECTOR2I > nullPoints
struct KI_TEST::CommonTestData common
Common data for some of the SHAPE_POLY_SET tests:
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(VertexIterator)
Declares the IteratorFixture as the boost test suite fixture.
BOOST_CHECK_EQUAL(result, "25.4")