KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_shape_poly_set_distance.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
24
27
29
31constexpr static double IU_PER_MM = 1e3;
32
33constexpr static inline int Millimeter2iu( double mm )
34{
35 return (int) ( mm < 0 ? mm * IU_PER_MM - 0.5 : mm * IU_PER_MM + 0.5 );
36}
37
38
42BOOST_AUTO_TEST_SUITE( SPSDistance )
43
58
59static std::vector<SPS_DISTANCE_TO_SEG_CASE> GetSPSSegDistCases()
60{
61 namespace KT = KI_TEST;
62 std::vector<SPS_DISTANCE_TO_SEG_CASE> cases;
63
64 // Single 10mm square at origin
65 const SHAPE_POLY_SET square_10mm_0_0 = KT::BuildPolyset( {
66 KT::BuildSquareChain( Millimeter2iu( 10 ) ),
67 } );
68
69 // Double square: 10mm each, one at (0, 0), one at (10, 0)
70 const SHAPE_POLY_SET squares_10mm_0_0_and_20_0 = KT::BuildPolyset( {
71 KT::BuildSquareChain( Millimeter2iu( 10 ) ),
72 KT::BuildSquareChain( Millimeter2iu( 10 ), //
73 { Millimeter2iu( 20 ), Millimeter2iu( 0 ) } ),
74 } );
75
76 // Hollow square: 10mm hole in 20mm square, at origin
77 const SHAPE_POLY_SET hollow_square_20_10_at_0_0 =
78 KT::BuildHollowSquare( Millimeter2iu( 20 ), Millimeter2iu( 10 ) );
79
80 cases.push_back( {
81 "Square poly -> 1D segment",
82 square_10mm_0_0,
83 KT::BuildHSeg( { Millimeter2iu( 0 ), Millimeter2iu( 15 ) }, Millimeter2iu( 10 ) ),
84 Millimeter2iu( 0 ), // 1-d segment
85 Millimeter2iu( 10 ),
86 } );
87
88 cases.push_back( {
89 "Square poly -> 2D (thick) segment",
90 square_10mm_0_0,
91 KT::BuildHSeg( { Millimeter2iu( 0 ), Millimeter2iu( 15 ) }, Millimeter2iu( 10 ) ),
92 Millimeter2iu( 2 ), // thick segment
93 Millimeter2iu( 9 ),
94 } );
95
96 cases.push_back( {
97 "Two Squares poly -> 2D segment (nearest second square)", squares_10mm_0_0_and_20_0,
98 KT::BuildHSeg( { Millimeter2iu( 15 ), Millimeter2iu( 15 ) }, Millimeter2iu( 10 ) ),
99 Millimeter2iu( 2 ), // thick segment
100 Millimeter2iu( 9 ), // from line to second square
101 } );
102
103 cases.push_back( {
104 "Square poly -> one intersect", square_10mm_0_0,
105 KT::BuildHSeg( { Millimeter2iu( -5 ), Millimeter2iu( 0 ) }, Millimeter2iu( 10 ) ),
106 Millimeter2iu( 0 ), // 1-d segment
107 Millimeter2iu( 0 ), // intersect
108 } );
109
110 cases.push_back( {
111 "Square poly -> multiple intersection", square_10mm_0_0,
112 KT::BuildHSeg( { Millimeter2iu( -5 ), Millimeter2iu( 0 ) }, Millimeter2iu( 20 ) ),
113 Millimeter2iu( 0 ), // 1-d segment
114 Millimeter2iu( 0 ), // intersect
115 } );
116
117 cases.push_back( {
118 "Square poly -> 1D seg touching", square_10mm_0_0,
119 // touch left side at (-5, 0)
120 KT::BuildHSeg( { Millimeter2iu( -10 ), Millimeter2iu( 0 ) }, Millimeter2iu( 5 ) ),
121 Millimeter2iu( 0 ), // 2D segment
122 Millimeter2iu( 0 ), // intersect
123 } );
124
125 cases.push_back( {
126 "Square poly -> 2D seg (end cap is nearest)", square_10mm_0_0,
127 KT::BuildHSeg( { Millimeter2iu( -20 ), Millimeter2iu( 0 ) }, Millimeter2iu( 10 ) ),
128 Millimeter2iu( 2 ), // 2D segment, 1mm cap radius
129 Millimeter2iu( 4 ), // 4mm short, 5mm to wire end, -1mm radius
130 } );
131
132 return cases;
133};
134
139{
140 for( const auto& c : GetSPSSegDistCases() )
141 {
142 BOOST_TEST_CONTEXT( c.m_case_name )
143 {
144 SHAPE_POLY_SET polyset = c.m_polyset;
145
146 int dist = sqrt( polyset.SquaredDistanceToSeg( c.m_seg ) ) - ( c.m_seg_width / 2 );
147
148 // right answer?
149 BOOST_CHECK_PREDICATE( KI_TEST::IsWithin<int>, ( dist )( c.m_exp_dist )( 1 ) );
150 }
151 }
152}
153
Definition seg.h:38
Represent a set of closed polygons.
SEG::ecoord SquaredDistanceToSeg(const SEG &aSegment, VECTOR2I *aNearest=nullptr) const
Compute the minimum distance squared between aSegment and all the polygons in the set.
bool IsWithin(T aValue, T aNominal, T aError)
Check if a value is within a tolerance of a nominal value.
Definition numeric.h:57
Utilities for creating useful polygon shapes that are commonly found in QA utilities and tests.
Utilities for creating useful line chain idioms commonly founds in QA utilities and tests.
Declares the Boost test suite fixture.
SEG m_seg
the segment to check distance to
SHAPE_POLY_SET m_polyset
list of lists of polygon points
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_PREDICATE(ArePolylineEndPointsNearCircle,(chain)(c.m_geom.m_center_point)(radius)(accuracy+epsilon))
BOOST_TEST_CONTEXT("Test Clearance")
static constexpr int Millimeter2iu(double mm)
static constexpr double IU_PER_MM
Mock up a conversion function.
BOOST_AUTO_TEST_CASE(SegDistance)
Check segment distances.
static std::vector< SPS_DISTANCE_TO_SEG_CASE > GetSPSSegDistCases()