KiCad PCB EDA Suite
Loading...
Searching...
No Matches
geom_test_utils.h
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) 2018 KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#ifndef GEOM_TEST_UTILS_H
25#define GEOM_TEST_UTILS_H
26
27#include <cmath>
28
30#include <geometry/seg.h>
33
34#include <qa_utils/numeric.h>
36
40namespace GEOM_TEST
41{
42
52enum class QUADRANT {
53 Q1, Q2, Q3, Q4
54};
55
56/*
57 * @brief Check value in Quadrant 1 (x and y both >= 0)
58 */
59template<typename T>
60bool IsInQuadrant( const VECTOR2<T>& aPoint, QUADRANT aQuadrant )
61{
62 bool isInQuad = false;
63
64 switch( aQuadrant )
65 {
66 case QUADRANT::Q1:
67 isInQuad = aPoint.x >= 0 && aPoint.y >= 0;
68 break;
69 case QUADRANT::Q2:
70 isInQuad = aPoint.x <= 0 && aPoint.y >= 0;
71 break;
72 case QUADRANT::Q3:
73 isInQuad = aPoint.x <= 0 && aPoint.y <= 0;
74 break;
75 case QUADRANT::Q4:
76 isInQuad = aPoint.x >= 0 && aPoint.y <= 0;
77 break;
78 }
79
80 return isInQuad;
81}
82
83/*
84 * @Brief Check if both ends of a segment are in Quadrant 1
85 */
86inline bool SegmentCompletelyInQuadrant( const SEG& aSeg, QUADRANT aQuadrant )
87{
88 return IsInQuadrant( aSeg.A, aQuadrant)
89 && IsInQuadrant( aSeg.B, aQuadrant );
90}
91
92/*
93 * @brief Check if at least one end of the segment is in Quadrant 1
94 */
95inline bool SegmentEndsInQuadrant( const SEG& aSeg, QUADRANT aQuadrant )
96{
97 return IsInQuadrant( aSeg.A, aQuadrant )
98 || IsInQuadrant( aSeg.B, aQuadrant );
99}
100
101/*
102 * @brief Check if a segment is entirely within a certain radius of a point.
103 */
104inline bool SegmentCompletelyWithinRadius( const SEG& aSeg, const VECTOR2I& aPt, const int aRadius )
105{
106 // This is true iff both ends of the segment are within the radius
107 return ( ( aSeg.A - aPt ).EuclideanNorm() < aRadius )
108 && ( ( aSeg.B - aPt ).EuclideanNorm() < aRadius );
109}
110
120template <typename T>
121bool IsPointAtDistance( const VECTOR2<T>& aPtA, const VECTOR2<T>& aPtB, T aExpDist, T aTol )
122{
123 const int dist = ( aPtB - aPtA ).EuclideanNorm();
124 const bool ok = KI_TEST::IsWithin( dist, aExpDist, aTol );
125
126 if( !ok )
127 {
128 BOOST_TEST_INFO( "Points not at expected distance: distance is " << dist << ", expected "
129 << aExpDist );
130 }
131
132 return ok;
133}
134
144template <typename T>
146 const std::vector<VECTOR2<T>>& aPoints, const VECTOR2<T>& aCentre, T aRad, T aTol )
147{
148 bool ok = true;
149
150 for( unsigned i = 0; i < aPoints.size(); ++i )
151 {
152 if( !IsPointAtDistance( aPoints[i], aCentre, aRad, aTol ) )
153 {
154 BOOST_TEST_INFO( "Point " << i << " " << aPoints[i] << " is not within tolerance ("
155 << aTol << ") of radius (" << aRad << ") from centre point "
156 << aCentre );
157 ok = false;
158 }
159 }
160
161 return ok;
162}
163
164/*
165 * @brief Check if two vectors are perpendicular
166 *
167 * @param a: vector A
168 * @param b: vector B
169 * @param aTolerance: the allowed deviation from PI/2 (e.g. when rounding)
170 */
171
172template<typename T>
173bool ArePerpendicular( const VECTOR2<T>& a, const VECTOR2<T>& b, const EDA_ANGLE& aTolerance )
174{
175 EDA_ANGLE angle = std::abs( EDA_ANGLE( a ) - EDA_ANGLE( b ) );
176
177 // Normalise: angles of 3*pi/2 are also perpendicular
178 if (angle > ANGLE_180)
179 angle -= ANGLE_180;
180
181 return KI_TEST::IsWithin( angle.AsRadians(), ANGLE_90.AsRadians(), aTolerance.AsRadians() );
182}
183
184/*
185 * @brief Fillet every polygon in a set and return a new set
186 */
187inline SHAPE_POLY_SET FilletPolySet( SHAPE_POLY_SET& aPolySet, int aRadius, int aError )
188{
189 SHAPE_POLY_SET filletedPolySet;
190
191 for ( int i = 0; i < aPolySet.OutlineCount(); ++i )
192 {
193 const auto filleted = aPolySet.FilletPolygon( aRadius, aError, i );
194
195 filletedPolySet.AddOutline( filleted[0] );
196 }
197
198 return filletedPolySet;
199}
200
209inline bool IsOutlineValid( const SHAPE_LINE_CHAIN& aChain )
210{
211 ssize_t prevArcIdx = -1;
212 std::set<size_t> testedArcs;
213
214 if( aChain.PointCount() > 0 && !aChain.IsClosed() && aChain.IsSharedPt( 0 ) )
215 return false; //can't have first point being shared on an open chain
216
217 for( int i = 0; i < aChain.PointCount(); i++ )
218 {
219 ssize_t arcIdx = aChain.ArcIndex( i );
220
221 if( arcIdx >= 0 )
222 {
223 // Point on arc, lets make sure it collides with the arc shape and we haven't
224 // previously seen the same arc index
225
226 if( prevArcIdx != arcIdx && testedArcs.count( arcIdx ) )
227 return false; // we've already seen this arc before, not contiguous
228
229 if( !aChain.Arc( arcIdx ).Collide( aChain.CPoint( i ),
231 {
232 return false;
233 }
234
235 testedArcs.insert( arcIdx );
236 }
237
238 if( prevArcIdx != arcIdx )
239 {
240 // we have changed arc shapes, run a few extra tests
241
242 if( prevArcIdx >= 0 )
243 {
244 // prev point on arc, test that the last arc point on the chain
245 // matches the end point of the arc
246 VECTOR2I pointToTest = aChain.CPoint( i );
247
248 if( !aChain.IsSharedPt( i ) )
249 pointToTest = aChain.CPoint( i - 1 );
250
251 SHAPE_ARC lastArc = aChain.Arc( prevArcIdx );
252
253 if( lastArc.GetP1() != pointToTest )
254 return false;
255 }
256
257 if( arcIdx >= 0 )
258 {
259 // new arc, test that the start point of the arc matches the point on the chain
260 VECTOR2I pointToTest = aChain.CPoint( i );
261 SHAPE_ARC currentArc = aChain.Arc( arcIdx );
262
263 if( currentArc.GetP0() != pointToTest )
264 return false;
265 }
266 }
267
268 prevArcIdx = arcIdx;
269 }
270
271 // Make sure last arc point matches the end of the arc
272 if( prevArcIdx >= 0 )
273 {
274 if( aChain.IsClosed() && aChain.IsSharedPt( 0 ) )
275 {
276 if( aChain.CShapes()[0].first != prevArcIdx )
277 return false;
278
279 if( aChain.Arc( prevArcIdx ).GetP1() != aChain.CPoint( 0 ) )
280 return false;
281 }
282 else
283 {
284 if( aChain.Arc( prevArcIdx ).GetP1() != aChain.CPoint( -1 ) )
285 return false;
286 }
287 }
288
289 return true;
290}
291
299inline bool IsPolySetValid( const SHAPE_POLY_SET& aSet )
300{
301 for( int i = 0; i < aSet.OutlineCount(); i++ )
302 {
303 if( !IsOutlineValid( aSet.Outline( i ) ) )
304 return false;
305
306 for( int j = 0; j < aSet.HoleCount( i ); j++ )
307 {
308 if( !IsOutlineValid( aSet.CHole( i, j ) ) )
309 return false;
310 }
311 }
312
313 return true;
314}
315
321inline bool SegmentsHaveSameEndPoints( const SEG& aSeg1, const SEG& aSeg2 )
322{
323 return ( aSeg1.A == aSeg2.A && aSeg1.B == aSeg2.B )
324 || ( aSeg1.A == aSeg2.B && aSeg1.B == aSeg2.A );
325}
326
327} // namespace GEOM_TEST
328
329
330// Stream printing for geometry types
331
332std::ostream& boost_test_print_type( std::ostream& os, const SHAPE_LINE_CHAIN& c );
333
334// Not clear why boost_test_print_type doesn't work on Debian specifically for this type,
335// but this works on all platforms
336std::ostream& operator<<( std::ostream& os, const TYPED_POINT2I& c );
337
338#endif // GEOM_TEST_UTILS_H
double AsRadians() const
Definition: eda_angle.h:117
Definition: seg.h:42
VECTOR2I A
Definition: seg.h:49
VECTOR2I B
Definition: seg.h:50
const VECTOR2I & GetP1() const
Definition: shape_arc.h:115
bool Collide(const SEG &aSeg, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if the boundary of shape (this) lies closer to the segment aSeg than aClearance,...
Definition: shape_arc.cpp:244
static double DefaultAccuracyForPCB()
Definition: shape_arc.h:232
const VECTOR2I & GetP0() const
Definition: shape_arc.h:114
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
const SHAPE_ARC & Arc(size_t aArc) const
bool IsClosed() const override
int PointCount() const
Return the number of points (vertices) in this line chain.
ssize_t ArcIndex(size_t aSegment) const
Return the arc index for the given segment index.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
const std::vector< std::pair< ssize_t, ssize_t > > & CShapes() const
bool IsSharedPt(size_t aIndex) const
Test if a point is shared between multiple shapes.
Represent a set of closed polygons.
int AddOutline(const SHAPE_LINE_CHAIN &aOutline)
Adds a new outline to the set and returns its index.
int HoleCount(int aOutline) const
Returns the number of holes in a given outline.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
const SHAPE_LINE_CHAIN & CHole(int aOutline, int aHole) const
POLYGON FilletPolygon(unsigned int aRadius, int aErrorMax, int aIndex)
Return a filleted version of the aIndex-th polygon.
int OutlineCount() const
Return the number of outlines in the set.
Define a general 2D-vector/point.
Definition: vector2d.h:71
static constexpr EDA_ANGLE ANGLE_90
Definition: eda_angle.h:403
static constexpr EDA_ANGLE ANGLE_180
Definition: eda_angle.h:405
std::ostream & operator<<(std::ostream &aStream, const EDA_TEXT &aText)
Definition: eda_text.cpp:1195
std::ostream & boost_test_print_type(std::ostream &os, const SHAPE_LINE_CHAIN &c)
Utility functions for testing geometry functions.
bool ArePointsNearCircle(const std::vector< VECTOR2< T > > &aPoints, const VECTOR2< T > &aCentre, T aRad, T aTol)
Predicate for checking a set of points is within a certain tolerance of a circle.
bool IsOutlineValid(const SHAPE_LINE_CHAIN &aChain)
Verify that a SHAPE_LINE_CHAIN has been assembled correctly by ensuring that the arc start and end po...
bool SegmentCompletelyWithinRadius(const SEG &aSeg, const VECTOR2I &aPt, const int aRadius)
bool IsPointAtDistance(const VECTOR2< T > &aPtA, const VECTOR2< T > &aPtB, T aExpDist, T aTol)
Check that two points are the given distance apart, within the given tolerance.
bool SegmentEndsInQuadrant(const SEG &aSeg, QUADRANT aQuadrant)
SHAPE_POLY_SET FilletPolySet(SHAPE_POLY_SET &aPolySet, int aRadius, int aError)
bool IsInQuadrant(const VECTOR2< T > &aPoint, QUADRANT aQuadrant)
bool SegmentCompletelyInQuadrant(const SEG &aSeg, QUADRANT aQuadrant)
bool ArePerpendicular(const VECTOR2< T > &a, const VECTOR2< T > &b, const EDA_ANGLE &aTolerance)
QUADRANT
Geometric quadrants, from top-right, anti-clockwise.
bool SegmentsHaveSameEndPoints(const SEG &aSeg1, const SEG &aSeg2)
Check that two SEGs have the same end points, in either order.
bool IsPolySetValid(const SHAPE_POLY_SET &aSet)
Verify that a SHAPE_POLY_SET has been assembled correctly by verifying each of the outlines and holes...
bool IsWithin(T aValue, T aNominal, T aError)
Check if a value is within a tolerance of a nominal value.
Definition: numeric.h:57
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:390
Numerical test predicates.