KiCad PCB EDA Suite
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 CHANGELOG.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 
29 #include <geometry/seg.h>
32 
35 
39 namespace GEOM_TEST
40 {
41 
51 enum class QUADRANT {
52  Q1, Q2, Q3, Q4
53 };
54 
55 /*
56  * @brief Check value in Quadrant 1 (x and y both >= 0)
57  */
58 template<typename T>
59 bool IsInQuadrant( const VECTOR2<T>& aPoint, QUADRANT aQuadrant )
60 {
61  bool isInQuad = false;
62 
63  switch( aQuadrant )
64  {
65  case QUADRANT::Q1:
66  isInQuad = aPoint.x >= 0 && aPoint.y >= 0;
67  break;
68  case QUADRANT::Q2:
69  isInQuad = aPoint.x <= 0 && aPoint.y >= 0;
70  break;
71  case QUADRANT::Q3:
72  isInQuad = aPoint.x <= 0 && aPoint.y <= 0;
73  break;
74  case QUADRANT::Q4:
75  isInQuad = aPoint.x >= 0 && aPoint.y <= 0;
76  break;
77  }
78 
79  return isInQuad;
80 }
81 
82 /*
83  * @Brief Check if both ends of a segment are in Quadrant 1
84  */
85 inline bool SegmentCompletelyInQuadrant( const SEG& aSeg, QUADRANT aQuadrant )
86 {
87  return IsInQuadrant( aSeg.A, aQuadrant)
88  && IsInQuadrant( aSeg.B, aQuadrant );
89 }
90 
91 /*
92  * @brief Check if at least one end of the segment is in Quadrant 1
93  */
94 inline bool SegmentEndsInQuadrant( const SEG& aSeg, QUADRANT aQuadrant )
95 {
96  return IsInQuadrant( aSeg.A, aQuadrant )
97  || IsInQuadrant( aSeg.B, aQuadrant );
98 }
99 
100 /*
101  * @brief Check if a segment is entirely within a certain radius of a point.
102  */
103 inline bool SegmentCompletelyWithinRadius( const SEG& aSeg, const VECTOR2I& aPt, const int aRadius )
104 {
105  // This is true iff both ends of the segment are within the radius
106  return ( ( aSeg.A - aPt ).EuclideanNorm() < aRadius )
107  && ( ( aSeg.B - aPt ).EuclideanNorm() < aRadius );
108 }
109 
119 template <typename T>
120 bool IsPointAtDistance( const VECTOR2<T>& aPtA, const VECTOR2<T>& aPtB, T aExpDist, T aTol )
121 {
122  const int dist = ( aPtB - aPtA ).EuclideanNorm();
123  const bool ok = KI_TEST::IsWithin( dist, aExpDist, aTol );
124 
125  if( !ok )
126  {
127  BOOST_TEST_INFO( "Points not at expected distance: distance is " << dist << ", expected "
128  << aExpDist );
129  }
130 
131  return ok;
132 }
133 
143 template <typename T>
145  const std::vector<VECTOR2<T>>& aPoints, const VECTOR2<T>& aCentre, T aRad, T aTol )
146 {
147  bool ok = true;
148 
149  for( unsigned i = 0; i < aPoints.size(); ++i )
150  {
151  if( !IsPointAtDistance( aPoints[i], aCentre, aRad, aTol ) )
152  {
153  BOOST_TEST_INFO( "Point " << i << " " << aPoints[i] << " is not within tolerance ("
154  << aTol << ") of radius (" << aRad << ") from centre point "
155  << aCentre );
156  ok = false;
157  }
158  }
159 
160  return ok;
161 }
162 
163 /*
164  * @brief Check if two vectors are perpendicular
165  *
166  * @param a: vector A
167  * @param b: vector B
168  * @param aTolerance: the allowed deviation from PI/2 (e.g. when rounding)
169  */
170 
171 template<typename T>
172 bool ArePerpendicular( const VECTOR2<T>& a, const VECTOR2<T>& b, double aTolerance )
173 {
174  auto angle = std::abs( a.Angle() - b.Angle() );
175 
176  // Normalise: angles of 3*pi/2 are also perpendicular
177  if (angle > M_PI)
178  {
179  angle -= M_PI;
180  }
181 
182  return KI_TEST::IsWithin( angle, M_PI / 2.0, aTolerance );
183 }
184 
191 inline SHAPE_LINE_CHAIN MakeSquarePolyLine( int aSize, const VECTOR2I& aCentre )
192 {
193  SHAPE_LINE_CHAIN polyLine;
194 
195  const VECTOR2I corner = aCentre + aSize / 2;
196 
197  polyLine.Append( VECTOR2I( corner.x, corner.y ) );
198  polyLine.Append( VECTOR2I( -corner.x, corner.y ) ) ;
199  polyLine.Append( VECTOR2I( -corner.x, -corner.y ) );
200  polyLine.Append( VECTOR2I( corner.x, -corner.y ) );
201 
202  polyLine.SetClosed( true );
203 
204  return polyLine;
205 }
206 
207 /*
208  * @brief Fillet every polygon in a set and return a new set
209  */
210 inline SHAPE_POLY_SET FilletPolySet( SHAPE_POLY_SET& aPolySet, int aRadius, int aError )
211 {
212  SHAPE_POLY_SET filletedPolySet;
213 
214  for ( int i = 0; i < aPolySet.OutlineCount(); ++i )
215  {
216  const auto filleted = aPolySet.FilletPolygon( aRadius, aError, i );
217 
218  filletedPolySet.AddOutline( filleted[0] );
219  }
220 
221  return filletedPolySet;
222 }
223 
224 } // namespace GEOM_TEST
225 
227 {
228 template <>
229 struct print_log_value<SHAPE_LINE_CHAIN>
230 {
231  inline void operator()( std::ostream& os, const SHAPE_LINE_CHAIN& c )
232  {
233  os << "SHAPE_LINE_CHAIN: " << c.PointCount() << " points: [\n";
234 
235  for( int i = 0; i < c.PointCount(); ++i )
236  {
237  os << " " << i << ": " << c.CPoint( i ) << "\n";
238  }
239 
240  os << "]";
241  }
242 };
243 }
245 
246 
247 #endif // GEOM_TEST_UTILS_H
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:128
Before Boost 1.64, nullptr_t wasn't handled.
bool IsWithin(T aValue, T aNominal, T aError)
Check if a value is within a tolerance of a nominal value.
Definition: numeric.h:57
int OutlineCount() const
Returns the number of outlines in the set
SHAPE_POLY_SET FilletPolySet(SHAPE_POLY_SET &aPolySet, int aRadius, int aError)
VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
T
enum T contains all this lexer's tokens.
QUADRANT
Geometric quadrants, from top-right, anti-clockwise.
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
int PointCount() const
Function PointCount()
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
#define BOOST_TEST_PRINT_NAMESPACE_CLOSE
const VECTOR2I & CPoint(int aIndex) const
Function Point()
void SetClosed(bool aClosed)
Function SetClosed()
bool SegmentCompletelyWithinRadius(const SEG &aSeg, const VECTOR2I &aPt, const int aRadius)
SHAPE_POLY_SET.
SHAPE_LINE_CHAIN MakeSquarePolyLine(int aSize, const VECTOR2I &aCentre)
construct a square polygon of given size width and centre
void operator()(std::ostream &os, const SHAPE_LINE_CHAIN &c)
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 SegmentEndsInQuadrant(const SEG &aSeg, QUADRANT aQuadrant)
double Angle() const
Function Angle computes the angle of the vector.
Definition: vector2d.h:313
Utility functions for testing geometry functions.
Definition: seg.h:39
int AddOutline(const SHAPE_LINE_CHAIN &aOutline)
Adds a new outline to the set and returns its index
SHAPE_LINE_CHAIN.
POLYGON FilletPolygon(unsigned int aRadius, int aErrorMax, int aIndex, std::set< VECTOR2I > *aPreserveCorners=nullptr)
Function Fillet returns a filleted version of the aIndex-th polygon.
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
VECTOR2I A
Definition: seg.h:47
bool IsInQuadrant(const VECTOR2< T > &aPoint, QUADRANT aQuadrant)
Numerical test predicates.
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 SegmentCompletelyInQuadrant(const SEG &aSeg, QUADRANT aQuadrant)
#define BOOST_TEST_INFO(A)
If HAVE_EXPECTED_FAILURES is defined, this means that boost::unit_test::expected_failures is availabl...
bool ArePerpendicular(const VECTOR2< T > &a, const VECTOR2< T > &b, double aTolerance)
VECTOR2I B
Definition: seg.h:48