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 The 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>
37
41namespace GEOM_TEST
42{
43
53enum class QUADRANT {
55};
56
57/*
58 * @brief Check value in Quadrant 1 (x and y both >= 0)
59 */
60template<typename T>
61bool IsInQuadrant( const VECTOR2<T>& aPoint, QUADRANT aQuadrant )
62{
63 bool isInQuad = false;
64
65 switch( aQuadrant )
66 {
67 case QUADRANT::Q1:
68 isInQuad = aPoint.x >= 0 && aPoint.y >= 0;
69 break;
70 case QUADRANT::Q2:
71 isInQuad = aPoint.x <= 0 && aPoint.y >= 0;
72 break;
73 case QUADRANT::Q3:
74 isInQuad = aPoint.x <= 0 && aPoint.y <= 0;
75 break;
76 case QUADRANT::Q4:
77 isInQuad = aPoint.x >= 0 && aPoint.y <= 0;
78 break;
79 }
80
81 return isInQuad;
82}
83
84/*
85 * @Brief Check if both ends of a segment are in Quadrant 1
86 */
87inline bool SegmentCompletelyInQuadrant( const SEG& aSeg, QUADRANT aQuadrant )
88{
89 return IsInQuadrant( aSeg.A, aQuadrant)
90 && IsInQuadrant( aSeg.B, aQuadrant );
91}
92
93/*
94 * @brief Check if at least one end of the segment is in Quadrant 1
95 */
96inline bool SegmentEndsInQuadrant( const SEG& aSeg, QUADRANT aQuadrant )
97{
98 return IsInQuadrant( aSeg.A, aQuadrant )
99 || IsInQuadrant( aSeg.B, aQuadrant );
100}
101
102/*
103 * @brief Check if a segment is entirely within a certain radius of a point.
104 */
105inline bool SegmentCompletelyWithinRadius( const SEG& aSeg, const VECTOR2I& aPt, const int aRadius )
106{
107 // This is true iff both ends of the segment are within the radius
108 return ( ( aSeg.A - aPt ).EuclideanNorm() < aRadius )
109 && ( ( aSeg.B - aPt ).EuclideanNorm() < aRadius );
110}
111
121template <typename T>
122bool IsPointAtDistance( const VECTOR2<T>& aPtA, const VECTOR2<T>& aPtB, T aExpDist, T aTol )
123{
124 const int dist = ( aPtB - aPtA ).EuclideanNorm();
125 const bool ok = KI_TEST::IsWithin( dist, aExpDist, aTol );
126
127 if( !ok )
128 {
129 BOOST_TEST_INFO( "Points not at expected distance: distance is " << dist << ", expected "
130 << aExpDist );
131 }
132
133 return ok;
134}
135
145template <typename T>
147 const std::vector<VECTOR2<T>>& aPoints, const VECTOR2<T>& aCentre, T aRad, T aTol )
148{
149 bool ok = true;
150
151 for( unsigned i = 0; i < aPoints.size(); ++i )
152 {
153 if( !IsPointAtDistance( aPoints[i], aCentre, aRad, aTol ) )
154 {
155 BOOST_TEST_INFO( "Point " << i << " " << aPoints[i] << " is not within tolerance ("
156 << aTol << ") of radius (" << aRad << ") from centre point "
157 << aCentre );
158 ok = false;
159 }
160 }
161
162 return ok;
163}
164
165/*
166 * @brief Check if two vectors are perpendicular
167 *
168 * @param a: vector A
169 * @param b: vector B
170 * @param aTolerance: the allowed deviation from PI/2 (e.g. when rounding)
171 */
172
173template<typename T>
174bool ArePerpendicular( const VECTOR2<T>& a, const VECTOR2<T>& b, const EDA_ANGLE& aTolerance )
175{
176 EDA_ANGLE angle = std::abs( EDA_ANGLE( a ) - EDA_ANGLE( b ) );
177
178 // Normalise: angles of 3*pi/2 are also perpendicular
179 if (angle > ANGLE_180)
180 angle -= ANGLE_180;
181
182 return KI_TEST::IsWithin( angle.AsRadians(), ANGLE_90.AsRadians(), aTolerance.AsRadians() );
183}
184
185/*
186 * @brief Fillet every polygon in a set and return a new set
187 */
188inline SHAPE_POLY_SET FilletPolySet( SHAPE_POLY_SET& aPolySet, int aRadius, int aError )
189{
190 SHAPE_POLY_SET filletedPolySet;
191
192 for ( int i = 0; i < aPolySet.OutlineCount(); ++i )
193 {
194 const auto filleted = aPolySet.FilletPolygon( aRadius, aError, i );
195
196 filletedPolySet.AddOutline( filleted[0] );
197 }
198
199 return filletedPolySet;
200}
201
210inline bool IsOutlineValid( const SHAPE_LINE_CHAIN& aChain )
211{
212 ssize_t prevArcIdx = -1;
213 std::set<size_t> testedArcs;
214
215 if( aChain.PointCount() > 0 && !aChain.IsClosed() && aChain.IsSharedPt( 0 ) )
216 return false; //can't have first point being shared on an open chain
217
218 for( int i = 0; i < aChain.PointCount(); i++ )
219 {
220 ssize_t arcIdx = aChain.ArcIndex( i );
221
222 if( arcIdx >= 0 )
223 {
224 // Point on arc, lets make sure it collides with the arc shape and we haven't
225 // previously seen the same arc index
226
227 if( prevArcIdx != arcIdx && testedArcs.count( arcIdx ) )
228 return false; // we've already seen this arc before, not contiguous
229
230 if( !aChain.Arc( arcIdx ).Collide( aChain.CPoint( i ),
232 {
233 return false;
234 }
235
236 testedArcs.insert( arcIdx );
237 }
238
239 if( prevArcIdx != arcIdx )
240 {
241 // we have changed arc shapes, run a few extra tests
242
243 if( prevArcIdx >= 0 )
244 {
245 // prev point on arc, test that the last arc point on the chain
246 // matches the end point of the arc
247 VECTOR2I pointToTest = aChain.CPoint( i );
248
249 if( !aChain.IsSharedPt( i ) )
250 pointToTest = aChain.CPoint( i - 1 );
251
252 SHAPE_ARC lastArc = aChain.Arc( prevArcIdx );
253
254 if( lastArc.GetP1() != pointToTest )
255 return false;
256 }
257
258 if( arcIdx >= 0 )
259 {
260 // new arc, test that the start point of the arc matches the point on the chain
261 VECTOR2I pointToTest = aChain.CPoint( i );
262 SHAPE_ARC currentArc = aChain.Arc( arcIdx );
263
264 if( currentArc.GetP0() != pointToTest )
265 return false;
266 }
267 }
268
269 prevArcIdx = arcIdx;
270 }
271
272 // Make sure last arc point matches the end of the arc
273 if( prevArcIdx >= 0 )
274 {
275 if( aChain.IsClosed() && aChain.IsSharedPt( 0 ) )
276 {
277 if( aChain.CShapes()[0].first != prevArcIdx )
278 return false;
279
280 if( aChain.Arc( prevArcIdx ).GetP1() != aChain.CPoint( 0 ) )
281 return false;
282 }
283 else
284 {
285 if( aChain.Arc( prevArcIdx ).GetP1() != aChain.CLastPoint() )
286 return false;
287 }
288 }
289
290 return true;
291}
292
300inline bool IsPolySetValid( const SHAPE_POLY_SET& aSet )
301{
302 for( int i = 0; i < aSet.OutlineCount(); i++ )
303 {
304 if( !IsOutlineValid( aSet.Outline( i ) ) )
305 return false;
306
307 for( int j = 0; j < aSet.HoleCount( i ); j++ )
308 {
309 if( !IsOutlineValid( aSet.CHole( i, j ) ) )
310 return false;
311 }
312 }
313
314 return true;
315}
316
322inline bool SegmentsHaveSameEndPoints( const SEG& aSeg1, const SEG& aSeg2 )
323{
324 return ( aSeg1.A == aSeg2.A && aSeg1.B == aSeg2.B )
325 || ( aSeg1.A == aSeg2.B && aSeg1.B == aSeg2.A );
326}
327
328} // namespace GEOM_TEST
329
330
331// Not clear why boost_test_print_type doesn't work on Debian specifically for this type,
332// but this works on all platforms
333std::ostream& operator<<( std::ostream& os, const TYPED_POINT2I& c );
334
335#endif // GEOM_TEST_UTILS_H
double AsRadians() const
Definition eda_angle.h:120
Definition seg.h:42
VECTOR2I A
Definition seg.h:49
VECTOR2I B
Definition seg.h:50
const VECTOR2I & GetP1() const
Definition shape_arc.h:119
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,...
static int DefaultAccuracyForPCB()
Definition shape_arc.h:283
const VECTOR2I & GetP0() const
Definition shape_arc.h:118
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
const VECTOR2I & CLastPoint() const
Return the last point in the line chain.
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:413
static constexpr EDA_ANGLE ANGLE_180
Definition eda_angle.h:415
std::ostream & operator<<(std::ostream &os, const TYPED_POINT2I &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:61
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:400
Numerical test predicates.
BOOST_TEST_INFO("Parsed: "<< path)
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695