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 <alejandro.garciamontoro@gmail.com>
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 
37 {
38  // Structure to store the common data.
40 
41  // Vectors containing colliding and non-colliding points
42  std::vector<VECTOR2I> collidingPoints, nonCollidingPoints;
43 
48  {
49  // Create points colliding with the poly set.
50 
51  // Inside the polygon
52  collidingPoints.emplace_back( 10, 90 );
53 
54  // Inside the polygon, but on a re-entrant angle of a hole
55  collidingPoints.emplace_back( 15, 16 );
56 
57  // On a hole edge => inside the polygon
58  collidingPoints.emplace_back( 40, 25 );
59 
60  // On the outline edge => inside the polygon
61  collidingPoints.emplace_back( 0, 10 );
62 
63  // Create points not colliding with the poly set.
64 
65  // Completely outside of the polygon
66  nonCollidingPoints.emplace_back( 200, 200 );
67 
68  // Inside the outline and inside a hole => outside the polygon
69  nonCollidingPoints.emplace_back( 15, 12 );
70  }
71 
73  {
74  }
75 };
76 
80 BOOST_FIXTURE_TEST_SUITE( SPSCollision, CollisionFixture )
81 
82 
86 {
87  BOOST_CHECK( !common.solidPolySet.HasHoles() );
88  BOOST_CHECK( common.holeyPolySet.HasHoles() );
89 }
90 
95 BOOST_AUTO_TEST_CASE( PointOnEdge )
96 {
97  // Check points on corners
98  BOOST_CHECK( common.holeyPolySet.PointOnEdge( VECTOR2I( 0, 50 ) ) );
99 
100  // Check points on outline edges
101  BOOST_CHECK( common.holeyPolySet.PointOnEdge( VECTOR2I( 0, 10 ) ) );
102 
103  // Check points on hole edges
104  BOOST_CHECK( common.holeyPolySet.PointOnEdge( VECTOR2I( 10, 11 ) ) );
105 
106  // Check points inside a hole -> not in edge
107  BOOST_CHECK( !common.holeyPolySet.PointOnEdge( VECTOR2I( 12, 12 ) ) );
108 
109  // Check points inside the polygon and outside any hole -> not on edge
110  BOOST_CHECK( !common.holeyPolySet.PointOnEdge( VECTOR2I( 90, 90 ) ) );
111 
112  // Check points outside the polygon -> not on edge
113  BOOST_CHECK( !common.holeyPolySet.PointOnEdge( VECTOR2I( 200, 200 ) ) );
114 }
115 
120 BOOST_AUTO_TEST_CASE( pointInPolygonSet )
121 {
122  // Check that the set contains the points that collide with it
123  for( const VECTOR2I& point : collidingPoints )
124  {
125  std::stringstream ss;
126  ss << "Point {" << point.x << ", " << point.y << " }";
127  BOOST_TEST_INFO( ss.str() );
128 
129  BOOST_CHECK( common.holeyPolySet.Contains( point ) );
130  }
131 
132  // Check that the set does not contain any point outside of it
133  for( const VECTOR2I& point : nonCollidingPoints )
134  {
135  std::stringstream ss;
136  ss << "Point {" << point.x << ", " << point.y << " }";
137  BOOST_TEST_INFO( ss.str() );
138 
139  BOOST_CHECK( !common.holeyPolySet.Contains( point ) );
140  }
141 }
142 
147 {
148  // When clearance = 0, the behaviour should be the same as with Contains
149 
150  // Check that the set collides with the colliding points
151  for( const VECTOR2I& point : collidingPoints )
152  {
153  std::stringstream ss;
154  ss << "Point {" << point.x << ", " << point.y << " }";
155  BOOST_TEST_INFO( ss.str() );
156 
157  BOOST_CHECK( common.holeyPolySet.Collide( point, 0 ) );
158  }
159 
160  // Check that the set does not collide with the non colliding points
161  for( const VECTOR2I& point : nonCollidingPoints )
162  {
163  std::stringstream ss;
164  ss << "Point {" << point.x << ", " << point.y << " }";
165  BOOST_TEST_INFO( ss.str() );
166 
167  BOOST_CHECK( !common.holeyPolySet.Collide( point, 0 ) );
168  }
169 
170  // Checks with clearance > 0
171 
172  // Point at the offset zone outside of the outline => collision!
173  BOOST_CHECK( common.holeyPolySet.Collide( VECTOR2I( -1, 10 ), 5 ) );
174 
175  // Point at the offset zone outside of a hole => collision!
176  BOOST_CHECK( common.holeyPolySet.Collide( VECTOR2I( 11, 11 ), 5 ) );
177 }
178 
183 BOOST_AUTO_TEST_CASE( CollideVertex )
184 {
185  // Variable to store the index of the corner hit
187 
188  // Check that the set collides with the colliding points
189  for( const VECTOR2I& point : common.holeyPoints )
190  {
191  BOOST_CHECK_MESSAGE( common.holeyPolySet.CollideVertex( point, cornerHit, 0 ), " Point "
192  << point.x << ", " << point.y << " does not collide with holeyPolySet polygon" );
193  }
194 }
195 
200 BOOST_AUTO_TEST_CASE( CollideVertexWithClearance )
201 {
202  // Variable to store the index of the corner hit
204 
205  // Check that the set collides with the colliding points
206  for( const VECTOR2I& point : common.holeyPoints )
207  {
208  BOOST_CHECK( common.holeyPolySet.CollideVertex( point + VECTOR2I( 1, 1 ), cornerHit, 2 ) );
209  }
210 }
211 
212 BOOST_AUTO_TEST_SUITE_END()
struct KI_TEST::CommonTestData common
static bool Collide(const SHAPE_CIRCLE &aA, const SHAPE_CIRCLE &aB, int aClearance, int *aActual, VECTOR2I *aMTV)
std::vector< VECTOR2I > collidingPoints
Common data for some of the SHAPE_POLY_SET tests:
VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
Struct VERTEX_INDEX.
Fixture for the Collision test suite.
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...