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