KiCad PCB EDA Suite
trigo.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-2021 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 TRIGO_H
25 #define TRIGO_H
26 
31 #include <cmath>
32 #include <math/vector2d.h>
33 #include <wx/gdicmn.h> // For wxPoint
34 
47 bool IsPointOnSegment( const wxPoint& aSegStart, const wxPoint& aSegEnd,
48  const wxPoint& aTestPoint );
49 
61 bool SegmentIntersectsSegment( const wxPoint &a_p1_l1, const wxPoint &a_p2_l1,
62  const wxPoint &a_p1_l2, const wxPoint &a_p2_l2,
63  wxPoint* aIntersectionPoint = nullptr );
64 
65 /*
66  * Calculate the new point of coord coord pX, pY,
67  * for a rotation center 0, 0, and angle in (1 / 10 degree)
68  */
69 void RotatePoint( int *pX, int *pY, double angle );
70 
71 /*
72  * Calculate the new point of coord coord pX, pY,
73  * for a rotation center cx, cy, and angle in (1 / 10 degree)
74  */
75 void RotatePoint( int *pX, int *pY, int cx, int cy, double angle );
76 
77 /*
78  * Calculates the new coord point point
79  * for a rotation angle in (1 / 10 degree)
80  */
81 inline void RotatePoint( wxPoint* point, double angle )
82 {
83  RotatePoint( &point->x, &point->y, angle );
84 }
85 
86 inline void RotatePoint( VECTOR2I& point, double angle )
87 {
88  RotatePoint( &point.x, &point.y, angle );
89 }
90 
91 void RotatePoint( VECTOR2I& point, const VECTOR2I& centre, double angle );
92 
93 /*
94  * Calculates the new coord point point
95  * for a center rotation center and angle in (1 / 10 degree)
96  */
97 void RotatePoint( wxPoint *point, const wxPoint & centre, double angle );
98 
99 void RotatePoint( double *pX, double *pY, double angle );
100 
101 void RotatePoint( double *pX, double *pY, double cx, double cy, double angle );
102 
111 const VECTOR2I GetArcCenter( const VECTOR2I& aStart, const VECTOR2I& aMid, const VECTOR2I& aEnd );
112 const VECTOR2D GetArcCenter( const VECTOR2D& aStart, const VECTOR2D& aMid, const VECTOR2D& aEnd );
113 const wxPoint GetArcCenter( const wxPoint& aStart, const wxPoint& aMid, const wxPoint& aEnd );
114 const wxPoint GetArcCenter( VECTOR2I aStart, VECTOR2I aEnd, double aAngle );
115 
119 double GetArcAngle( const VECTOR2I& aStart, const VECTOR2I& aMid, const VECTOR2I& aEnd );
120 
132 const VECTOR2I GetArcMid( const VECTOR2I& aStart, const VECTOR2I& aEnd, const VECTOR2I& aCenter,
133  bool aMinArcAngle = true );
134 
135 /* Return the arc tangent of 0.1 degrees coord vector dx, dy
136  * between -1800 and 1800
137  * Equivalent to atan2 (but faster for calculations if
138  * the angle is 0 to -1800, or + - 900)
139  * Lorenzo: In fact usually atan2 already has to do these optimizations
140  * (due to the discontinuity in tan) but this function also returns
141  * in decidegrees instead of radians, so it's handier
142  */
143 double ArcTangente( int dy, int dx );
144 
148 inline double EuclideanNorm( const wxPoint &vector )
149 {
150  // this is working with doubles
151  return hypot( vector.x, vector.y );
152 }
153 
154 inline double EuclideanNorm( const wxSize &vector )
155 {
156  // this is working with doubles, too
157  return hypot( vector.x, vector.y );
158 }
159 
165 inline double DistanceLinePoint( const wxPoint &linePointA,
166  const wxPoint &linePointB,
167  const wxPoint &referencePoint )
168 {
169  // Some of the multiple double casts are redundant. However in the previous
170  // definition the cast was (implicitly) done too late, just before
171  // the division (EuclideanNorm gives a double so from int it would
172  // be promoted); that means that the whole expression were
173  // vulnerable to overflow during int multiplications
174  return fabs( ( static_cast<double>( linePointB.x - linePointA.x ) *
175  static_cast<double>( linePointA.y - referencePoint.y ) -
176  static_cast<double>( linePointA.x - referencePoint.x ) *
177  static_cast<double>( linePointB.y - linePointA.y) )
178  / EuclideanNorm( linePointB - linePointA ) );
179 }
180 
186 inline bool HitTestPoints( const wxPoint &pointA, const wxPoint &pointB, double threshold )
187 {
188  wxPoint vectorAB = pointB - pointA;
189 
190  // Compare the distances squared. The double is needed to avoid
191  // overflow during int multiplication
192  double sqdistance = (double)vectorAB.x * vectorAB.x + (double)vectorAB.y * vectorAB.y;
193 
194  return sqdistance < threshold * threshold;
195 }
196 
200 inline double CrossProduct( const wxPoint &vectorA, const wxPoint &vectorB )
201 {
202  // As before the cast is to avoid int overflow
203  return (double)vectorA.x * vectorB.y - (double)vectorA.y * vectorB.x;
204 }
205 
214 bool TestSegmentHit( const wxPoint &aRefPoint, wxPoint aStart, wxPoint aEnd, int aDist );
215 
223 inline double GetLineLength( const wxPoint& aPointA, const wxPoint& aPointB )
224 {
225  // Implicitly casted to double
226  return hypot( aPointA.x - aPointB.x,
227  aPointA.y - aPointB.y );
228 }
229 
230 // These are the usual degrees <-> radians conversion routines
231 inline double DEG2RAD( double deg ) { return deg * M_PI / 180.0; }
232 inline double RAD2DEG( double rad ) { return rad * 180.0 / M_PI; }
233 
234 // These are the same *but* work with the internal 'decidegrees' unit
235 inline double DECIDEG2RAD( double deg ) { return deg * M_PI / 1800.0; }
236 inline double RAD2DECIDEG( double rad ) { return rad * 1800.0 / M_PI; }
237 
238 /* These are templated over T (and not simply double) because Eeschema
239  is still using int for angles in some place */
240 
243 template <class T> inline T NormalizeAngle360Max( T Angle )
244 {
245  while( Angle < -3600 )
246  Angle += 3600;
247  while( Angle > 3600 )
248  Angle -= 3600;
249  return Angle;
250 }
251 
254 template <class T> inline T NormalizeAngle360Min( T Angle )
255 {
256  while( Angle <= -3600 )
257  Angle += 3600;
258  while( Angle >= 3600 )
259  Angle -= 3600;
260  return Angle;
261 }
262 
263 
266 template <class T>
267 inline T NormalizeAngleNeg( T Angle )
268 {
269  while( Angle <= -3600 )
270  Angle += 3600;
271  while( Angle > 0 )
272  Angle -= 3600;
273  return Angle;
274 }
275 
276 
279 template <class T> inline T NormalizeAnglePos( T Angle )
280 {
281  while( Angle < 0 )
282  Angle += 3600;
283  while( Angle >= 3600 )
284  Angle -= 3600;
285  return Angle;
286 }
287 
288 template <class T> inline void NORMALIZE_ANGLE_POS( T& Angle )
289 {
290  Angle = NormalizeAnglePos( Angle );
291 }
292 
293 
296 inline double NormalizeAngleDegreesPos( double Angle )
297 {
298  while( Angle < 0 )
299  Angle += 360.0;
300  while( Angle >= 360.0 )
301  Angle -= 360.0;
302  return Angle;
303 }
304 
305 
306 inline void NORMALIZE_ANGLE_DEGREES_POS( double& Angle )
307 {
308  Angle = NormalizeAngleDegreesPos( Angle );
309 }
310 
311 
312 inline double NormalizeAngleRadiansPos( double Angle )
313 {
314  while( Angle < 0 )
315  Angle += (2 * M_PI );
316  while( Angle >= ( 2 * M_PI ) )
317  Angle -= ( 2 * M_PI );
318  return Angle;
319 }
320 
323 inline double NormalizeAngleDegrees( double Angle, double aMin, double aMax )
324 {
325  while( Angle < aMin )
326  Angle += 360.0;
327  while( Angle >= aMax )
328  Angle -= 360.0;
329  return Angle;
330 }
331 
333 // because most of the time it's an int (and templates don't promote in
334 // that way)
335 template <class T, class T2> inline T AddAngles( T a1, T2 a2 )
336 {
337  a1 += a2;
338  NORMALIZE_ANGLE_POS( a1 );
339  return a1;
340 }
341 
342 
343 template <class T> inline T NegateAndNormalizeAnglePos( T Angle )
344 {
345  Angle = -Angle;
346  while( Angle < 0 )
347  Angle += 3600;
348  while( Angle >= 3600 )
349  Angle -= 3600;
350  return Angle;
351 }
352 
353 template <class T> inline void NEGATE_AND_NORMALIZE_ANGLE_POS( T& Angle )
354 {
355  Angle = NegateAndNormalizeAnglePos( Angle );
356 }
357 
358 
360 template <class T> inline T NormalizeAngle90( T Angle )
361 {
362  while( Angle < -900 )
363  Angle += 1800;
364  while( Angle > 900 )
365  Angle -= 1800;
366  return Angle;
367 }
368 
369 template <class T> inline void NORMALIZE_ANGLE_90( T& Angle )
370 {
371  Angle = NormalizeAngle90( Angle );
372 }
373 
374 
376 template <class T> inline T NormalizeAngle180( T Angle )
377 {
378  while( Angle <= -1800 )
379  Angle += 3600;
380  while( Angle > 1800 )
381  Angle -= 3600;
382  return Angle;
383 }
384 
385 template <class T> inline void NORMALIZE_ANGLE_180( T& Angle )
386 {
387  Angle = NormalizeAngle180( Angle );
388 }
389 
398 inline bool InterceptsPositiveX( double aStartAngle, double aEndAngle )
399 {
400  double end = aEndAngle;
401 
402  if( aStartAngle > aEndAngle )
403  end += 360.0;
404 
405  return aStartAngle < 360.0 && end > 360.0;
406 }
407 
416 inline bool InterceptsNegativeX( double aStartAngle, double aEndAngle )
417 {
418  double end = aEndAngle;
419 
420  if( aStartAngle > aEndAngle )
421  end += 360.0;
422 
423  return aStartAngle < 180.0 && end > 180.0;
424 }
425 
430 inline double sindecideg( double r, double a )
431 {
432  return r * sin( DECIDEG2RAD( a ) );
433 }
434 
439 inline double cosdecideg( double r, double a )
440 {
441  return r * cos( DECIDEG2RAD( a ) );
442 }
443 
444 #endif
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:148
T NormalizeAngleNeg(T Angle)
Normalize angle to be in the 0.0 .
Definition: trigo.h:267
double GetLineLength(const wxPoint &aPointA, const wxPoint &aPointB)
Return the length of a line segment defined by aPointA and aPointB.
Definition: trigo.h:223
double GetArcAngle(const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd)
Returns the subtended angle for a given arc.
Definition: trigo.cpp:492
T NormalizeAngle360Max(T Angle)
Normalize angle to be >=-360.0 and <= 360.0 Angle can be equal to -360 or +360.
Definition: trigo.h:243
double RAD2DEG(double rad)
Definition: trigo.h:232
Define a general 2D-vector/point.
Definition: vector2d.h:61
bool IsPointOnSegment(const wxPoint &aSegStart, const wxPoint &aSegEnd, const wxPoint &aTestPoint)
Test if aTestPoint is on line defined by aSegStart and aSegEnd.
Definition: trigo.cpp:42
double RAD2DECIDEG(double rad)
Definition: trigo.h:236
T NormalizeAngle90(T Angle)
Normalize angle to be in the -90.0 .. 90.0 range.
Definition: trigo.h:360
void NORMALIZE_ANGLE_DEGREES_POS(double &Angle)
Definition: trigo.h:306
void NORMALIZE_ANGLE_180(T &Angle)
Definition: trigo.h:385
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:228
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:288
double NormalizeAngleRadiansPos(double Angle)
Definition: trigo.h:312
void NORMALIZE_ANGLE_90(T &Angle)
Definition: trigo.h:369
bool TestSegmentHit(const wxPoint &aRefPoint, wxPoint aStart, wxPoint aEnd, int aDist)
Test if aRefPoint is with aDistance on the line defined by aStart and aEnd.
Definition: trigo.cpp:129
bool SegmentIntersectsSegment(const wxPoint &a_p1_l1, const wxPoint &a_p2_l1, const wxPoint &a_p1_l2, const wxPoint &a_p2_l2, wxPoint *aIntersectionPoint=nullptr)
Test if two lines intersect.
Definition: trigo.cpp:61
T NormalizeAngle180(T Angle)
Normalize angle to be in the -180.0 .. 180.0 range.
Definition: trigo.h:376
double NormalizeAngleDegreesPos(double Angle)
Normalize angle to be in the 0.0 .
Definition: trigo.h:296
const VECTOR2I GetArcMid(const VECTOR2I &aStart, const VECTOR2I &aEnd, const VECTOR2I &aCenter, bool aMinArcAngle=true)
Returns the middle point of an arc, half-way between aStart and aEnd.
Definition: trigo.cpp:162
T AddAngles(T a1, T2 a2)
Add two angles (keeping the result normalized). T2 is here.
Definition: trigo.h:335
bool InterceptsPositiveX(double aStartAngle, double aEndAngle)
Test if an arc from aStartAngle to aEndAngle crosses the positive X axis (0 degrees).
Definition: trigo.h:398
double CrossProduct(const wxPoint &vectorA, const wxPoint &vectorB)
Determine the cross product.
Definition: trigo.h:200
void NEGATE_AND_NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:353
double cosdecideg(double r, double a)
Circle generation utility: computes r * cos(a) Where a is in decidegrees, not in radians.
Definition: trigo.h:439
double sindecideg(double r, double a)
Circle generation utility: computes r * sin(a) Where a is in decidegrees, not in radians.
Definition: trigo.h:430
T NegateAndNormalizeAnglePos(T Angle)
Definition: trigo.h:343
double DEG2RAD(double deg)
Definition: trigo.h:231
bool InterceptsNegativeX(double aStartAngle, double aEndAngle)
Test if an arc from aStartAngle to aEndAngle crosses the negative X axis (180 degrees).
Definition: trigo.h:416
T NormalizeAnglePos(T Angle)
Normalize angle to be in the 0.0 .
Definition: trigo.h:279
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
bool HitTestPoints(const wxPoint &pointA, const wxPoint &pointB, double threshold)
Test, if two points are near each other.
Definition: trigo.h:186
double DECIDEG2RAD(double deg)
Definition: trigo.h:235
double NormalizeAngleDegrees(double Angle, double aMin, double aMax)
Normalize angle to be aMin < angle <= aMax angle is in degrees.
Definition: trigo.h:323
double ArcTangente(int dy, int dx)
Definition: trigo.cpp:182
const VECTOR2I GetArcCenter(const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd)
Determine the center of an arc or circle given three points on its circumference.
Definition: trigo.cpp:450
T NormalizeAngle360Min(T Angle)
Normalize angle to be > -360.0 and < 360.0 Angle equal to -360 or +360 are set to 0.
Definition: trigo.h:254
double DistanceLinePoint(const wxPoint &linePointA, const wxPoint &linePointB, const wxPoint &referencePoint)
Compute the distance between a line and a reference point Reference: http://mathworld....
Definition: trigo.h:165