KiCad PCB EDA Suite
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 
112 BOOST_FIXTURE_TEST_SUITE( SPSCollision, CollisionFixture )
113 
114 
118 {
119  BOOST_CHECK( !common.solidPolySet.HasHoles() );
120  BOOST_CHECK( common.holeyPolySet.HasHoles() );
121 }
122 
127 BOOST_AUTO_TEST_CASE( PointOnEdge )
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 
152 BOOST_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 
215 BOOST_AUTO_TEST_CASE( CollideVertex )
216 {
217  // Variable to store the index of the corner hit
219 
220  // Check that the set collides with the colliding points
221  for( const VECTOR2I& point : common.holeyPoints )
222  {
223  BOOST_CHECK_MESSAGE( common.holeyPolySet.CollideVertex( point, cornerHit, 0 ), " Point "
224  << point.x << ", " << point.y << " does not collide with holeyPolySet polygon" );
225  }
226 }
227 
232 BOOST_AUTO_TEST_CASE( CollideVertexWithClearance )
233 {
234  // Variable to store the index of the corner hit
236 
237  // Check that the set collides with the colliding points
238  for( const VECTOR2I& point : common.holeyPoints )
239  {
240  BOOST_CHECK( common.holeyPolySet.CollideVertex( point + VECTOR2I( 1, 1 ), cornerHit, 2 ) );
241  }
242 }
243 
244 
248 BOOST_AUTO_TEST_CASE( CollideSegments )
249 {
250  for( const SEG_CASE& testCase : segs )
251  {
252  SEG seg;
253  bool expectedResult;
254  VECTOR2I expectedLocation;
255 
256  std::tie( seg, expectedResult, expectedLocation ) = testCase;
257 
258  VECTOR2I location;
259 
260  BOOST_CHECK( common.holeyPolySet.Collide( seg, 0, nullptr, &location ) == expectedResult );
261 
262  if( expectedResult )
263  BOOST_REQUIRE_EQUAL( location, expectedLocation );
264  }
265 }
266 
267 BOOST_AUTO_TEST_SUITE_END()
struct KI_TEST::CommonTestData common
std::vector< VECTOR2I > collidingPoints
Common data for some of the SHAPE_POLY_SET tests:
std::vector< SEG_CASE > segs
Define a general 2D-vector/point.
Definition: vector2d.h:61
Structure to hold the necessary information in order to index a vertex on a SHAPE_POLY_SET object: th...
VECTOR2< int > VECTOR2I
Definition: vector2d.h:623
Fixture for the Collision test suite.
BOOST_CHECK(v2.Cross(v1)==1)
std::tuple< SEG, bool, VECTOR2I > SEG_CASE
static bool Collide(const SHAPE_CIRCLE &aA, const SHAPE_CIRCLE &aB, int aClearance, int *aActual, VECTOR2I *aLocation, VECTOR2I *aMTV)
Definition: seg.h:40
std::vector< VECTOR2I > nonCollidingPoints
BOOST_AUTO_TEST_CASE(HasHoles)
Declares the CollisionFixture as the boost test suite fixture.
#define BOOST_TEST_INFO(A)
If HAVE_EXPECTED_FAILURES is defined, this means that boost::unit_test::expected_failures is availabl...