KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_shape_poly_set_collision.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
25#include <tuple>
27
30
31#include "fixtures_geometry.h"
32
38{
39 // Structure to store the common data.
41
42 // Vectors containing colliding and non-colliding points
43 std::vector<VECTOR2I> collidingPoints, nonCollidingPoints;
44
45 // tuple of segment under test, collision result, and intersection point
46 typedef std::tuple<SEG, bool, VECTOR2I> SEG_CASE;
47
48 std::vector<SEG_CASE> segs;
49
54 {
55 // Create points colliding with the poly set.
56
57 // Inside the polygon
58 collidingPoints.emplace_back( 10, 90 );
59
60 // Inside the polygon, but on a re-entrant angle of a hole
61 collidingPoints.emplace_back( 15, 16 );
62
63 // On a hole edge => inside the polygon
64 collidingPoints.emplace_back( 40, 25 );
65
66 // On the outline edge => inside the polygon
67 collidingPoints.emplace_back( 0, 10 );
68
69 // Create points not colliding with the poly set.
70
71 // Completely outside of the polygon
72 nonCollidingPoints.emplace_back( 200, 200 );
73
74 // Inside the outline and inside a hole => outside the polygon
75 nonCollidingPoints.emplace_back( 15, 12 );
76
77 // Seg crossing the edge
78 segs.emplace_back( std::make_tuple( SEG( VECTOR2I( 90, 90 ), VECTOR2I( 110, 110 ) ),
79 true, VECTOR2I( 100, 100 ) ) );
80 segs.emplace_back( std::make_tuple( SEG( VECTOR2I( 110, 110 ), VECTOR2I( 90, 90 ) ),
81 true, VECTOR2I( 100, 100 ) ) );
82 segs.emplace_back( std::make_tuple( SEG( VECTOR2I( 50, -10 ), VECTOR2I( 50, 50 ) ),
83 true, VECTOR2I( 50, 0 ) ) );
84 segs.emplace_back( std::make_tuple( SEG( VECTOR2I( 50, 50 ), VECTOR2I( 50, -10 ) ),
85 true, VECTOR2I( 50, 0 ) ) );
86
87 // Seg fully inside
88 segs.emplace_back( std::make_tuple( SEG( VECTOR2I( 80, 80 ), VECTOR2I( 90, 90 ) ),
89 true, VECTOR2I( 85, 85 ) ) );
90 segs.emplace_back( std::make_tuple( SEG( VECTOR2I( 90, 90 ), VECTOR2I( 80, 80 ) ),
91 true, VECTOR2I( 85, 85 ) ) );
92
93 // Seg fully outside
94 segs.emplace_back( std::make_tuple( SEG( VECTOR2I( 110, 110 ), VECTOR2I( 120, 120 ) ),
95 false, VECTOR2I() ) );
96
97 // Seg touching
98 segs.emplace_back( std::make_tuple( SEG( VECTOR2I( 100, 100 ), VECTOR2I( 110, 110 ) ),
99 true, VECTOR2I( 100, 100 ) ) );
100 segs.emplace_back( std::make_tuple( SEG( VECTOR2I( 110, 110 ), VECTOR2I( 100, 100 ) ),
101 true, VECTOR2I( 100, 100 ) ) );
102 }
103
105 {
106 }
107};
108
112BOOST_FIXTURE_TEST_SUITE( SPSCollision, CollisionFixture )
113
114
118{
119 BOOST_CHECK( !common.solidPolySet.HasHoles() );
120 BOOST_CHECK( common.holeyPolySet.HasHoles() );
121}
122
128{
129 // Check points on corners
130 BOOST_CHECK( common.holeyPolySet.PointOnEdge( VECTOR2I( 0, 50 ) ) );
131
132 // Check points on outline edges
133 BOOST_CHECK( common.holeyPolySet.PointOnEdge( VECTOR2I( 0, 10 ) ) );
134
135 // Check points on hole edges
136 BOOST_CHECK( common.holeyPolySet.PointOnEdge( VECTOR2I( 10, 11 ) ) );
137
138 // Check points inside a hole -> not in edge
139 BOOST_CHECK( !common.holeyPolySet.PointOnEdge( VECTOR2I( 12, 12 ) ) );
140
141 // Check points inside the polygon and outside any hole -> not on edge
142 BOOST_CHECK( !common.holeyPolySet.PointOnEdge( VECTOR2I( 90, 90 ) ) );
143
144 // Check points outside the polygon -> not on edge
145 BOOST_CHECK( !common.holeyPolySet.PointOnEdge( VECTOR2I( 200, 200 ) ) );
146}
147
152BOOST_AUTO_TEST_CASE( pointInPolygonSet )
153{
154 // Check that the set contains the points that collide with it
155 for( const VECTOR2I& point : collidingPoints )
156 {
157 std::stringstream ss;
158 ss << "Point {" << point.x << ", " << point.y << " }";
159 BOOST_TEST_INFO( ss.str() );
160
161 BOOST_CHECK( common.holeyPolySet.Contains( point ) );
162 }
163
164 // Check that the set does not contain any point outside of it
165 for( const VECTOR2I& point : nonCollidingPoints )
166 {
167 std::stringstream ss;
168 ss << "Point {" << point.x << ", " << point.y << " }";
169 BOOST_TEST_INFO( ss.str() );
170
171 BOOST_CHECK( !common.holeyPolySet.Contains( point ) );
172 }
173}
174
179{
180 // When clearance = 0, the behaviour should be the same as with Contains
181
182 // Check that the set collides with the colliding points
183 for( const VECTOR2I& point : collidingPoints )
184 {
185 std::stringstream ss;
186 ss << "Point {" << point.x << ", " << point.y << " }";
187 BOOST_TEST_INFO( ss.str() );
188
189 BOOST_CHECK( common.holeyPolySet.Collide( point, 0 ) );
190 }
191
192 // Check that the set does not collide with the non colliding points
193 for( const VECTOR2I& point : nonCollidingPoints )
194 {
195 std::stringstream ss;
196 ss << "Point {" << point.x << ", " << point.y << " }";
197 BOOST_TEST_INFO( ss.str() );
198
199 BOOST_CHECK( !common.holeyPolySet.Collide( point, 0 ) );
200 }
201
202 // Checks with clearance > 0
203
204 // Point at the offset zone outside of the outline => collision!
205 BOOST_CHECK( common.holeyPolySet.Collide( VECTOR2I( -1, 10 ), 5 ) );
206
207 // Point at the offset zone outside of a hole => collision!
208 BOOST_CHECK( common.holeyPolySet.Collide( VECTOR2I( 11, 11 ), 5 ) );
209}
210
215BOOST_AUTO_TEST_CASE( CollideVertex )
216{
217 // Check that the set collides with the colliding points
218 for( const VECTOR2I& point : common.holeyPoints )
219 {
220 BOOST_CHECK_MESSAGE( common.holeyPolySet.CollideVertex( point, nullptr, 0 ),
221 " Point " << point.x << ", " << point.y <<
222 " does not collide with holeyPolySet polygon" );
223 }
224}
225
230BOOST_AUTO_TEST_CASE( CollideVertexWithClearance )
231{
232 // Check that the set collides with the colliding points
233 for( const VECTOR2I& point : common.holeyPoints )
234 BOOST_CHECK( common.holeyPolySet.CollideVertex( point + VECTOR2I( 1, 1 ), nullptr, 2 ) );
235}
236
237
241BOOST_AUTO_TEST_CASE( CollideSegments )
242{
243 for( const SEG_CASE& testCase : segs )
244 {
245 SEG seg;
246 bool expectedResult;
247 VECTOR2I expectedLocation;
248
249 std::tie( seg, expectedResult, expectedLocation ) = testCase;
250
251 VECTOR2I location;
252
253 BOOST_CHECK( common.holeyPolySet.Collide( seg, 0, nullptr, &location ) == expectedResult );
254
255 if( expectedResult )
256 BOOST_REQUIRE_EQUAL( location, expectedLocation );
257 }
258}
259
Definition: seg.h:42
static bool Collide(const SHAPE_CIRCLE &aA, const SHAPE_CIRCLE &aB, int aClearance, int *aActual, VECTOR2I *aLocation, VECTOR2I *aMTV)
Fixture for the Collision test suite.
struct KI_TEST::CommonTestData common
std::tuple< SEG, bool, VECTOR2I > SEG_CASE
std::vector< VECTOR2I > collidingPoints
std::vector< VECTOR2I > nonCollidingPoints
std::vector< SEG_CASE > segs
Common data for some of the SHAPE_POLY_SET tests:
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(HasHoles)
Declares the CollisionFixture as the boost test suite fixture.
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691