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-2020 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 
118 double GetArcAngle( const VECTOR2I& aStart, const VECTOR2I& aMid, const VECTOR2I& aEnd );
119 
120 /* Return the arc tangent of 0.1 degrees coord vector dx, dy
121  * between -1800 and 1800
122  * Equivalent to atan2 (but faster for calculations if
123  * the angle is 0 to -1800, or + - 900)
124  * Lorenzo: In fact usually atan2 already has to do these optimizations
125  * (due to the discontinuity in tan) but this function also returns
126  * in decidegrees instead of radians, so it's handier
127  */
128 double ArcTangente( int dy, int dx );
129 
133 inline double EuclideanNorm( const wxPoint &vector )
134 {
135  // this is working with doubles
136  return hypot( vector.x, vector.y );
137 }
138 
139 inline double EuclideanNorm( const wxSize &vector )
140 {
141  // this is working with doubles, too
142  return hypot( vector.x, vector.y );
143 }
144 
150 inline double DistanceLinePoint( const wxPoint &linePointA,
151  const wxPoint &linePointB,
152  const wxPoint &referencePoint )
153 {
154  // Some of the multiple double casts are redundant. However in the previous
155  // definition the cast was (implicitly) done too late, just before
156  // the division (EuclideanNorm gives a double so from int it would
157  // be promoted); that means that the whole expression were
158  // vulnerable to overflow during int multiplications
159  return fabs( ( double(linePointB.x - linePointA.x) *
160  double(linePointA.y - referencePoint.y) -
161  double(linePointA.x - referencePoint.x ) *
162  double(linePointB.y - linePointA.y) )
163  / EuclideanNorm( linePointB - linePointA ) );
164 }
165 
171 inline bool HitTestPoints( const wxPoint &pointA, const wxPoint &pointB,
172  double threshold )
173 {
174  wxPoint vectorAB = pointB - pointA;
175 
176  // Compare the distances squared. The double is needed to avoid
177  // overflow during int multiplication
178  double sqdistance = (double)vectorAB.x * vectorAB.x +
179  (double)vectorAB.y * vectorAB.y;
180 
181  return sqdistance < threshold * threshold;
182 }
183 
187 inline double CrossProduct( const wxPoint &vectorA, const wxPoint &vectorB )
188 {
189  // As before the cast is to avoid int overflow
190  return (double)vectorA.x * vectorB.y - (double)vectorA.y * vectorB.x;
191 }
192 
201 bool TestSegmentHit( const wxPoint &aRefPoint, wxPoint aStart,
202  wxPoint aEnd, int aDist );
203 
211 inline double GetLineLength( const wxPoint& aPointA, const wxPoint& aPointB )
212 {
213  // Implicitly casted to double
214  return hypot( aPointA.x - aPointB.x,
215  aPointA.y - aPointB.y );
216 }
217 
218 // These are the usual degrees <-> radians conversion routines
219 inline double DEG2RAD( double deg ) { return deg * M_PI / 180.0; }
220 inline double RAD2DEG( double rad ) { return rad * 180.0 / M_PI; }
221 
222 // These are the same *but* work with the internal 'decidegrees' unit
223 inline double DECIDEG2RAD( double deg ) { return deg * M_PI / 1800.0; }
224 inline double RAD2DECIDEG( double rad ) { return rad * 1800.0 / M_PI; }
225 
226 /* These are templated over T (and not simply double) because Eeschema
227  is still using int for angles in some place */
228 
231 template <class T> inline T NormalizeAngle360Max( T Angle )
232 {
233  while( Angle < -3600 )
234  Angle += 3600;
235  while( Angle > 3600 )
236  Angle -= 3600;
237  return Angle;
238 }
239 
242 template <class T> inline T NormalizeAngle360Min( T Angle )
243 {
244  while( Angle <= -3600 )
245  Angle += 3600;
246  while( Angle >= 3600 )
247  Angle -= 3600;
248  return Angle;
249 }
250 
251 
254 template <class T>
255 inline T NormalizeAngleNeg( T Angle )
256 {
257  while( Angle <= -3600 )
258  Angle += 3600;
259  while( Angle > 0 )
260  Angle -= 3600;
261  return Angle;
262 }
263 
264 
267 template <class T> inline T NormalizeAnglePos( T Angle )
268 {
269  while( Angle < 0 )
270  Angle += 3600;
271  while( Angle >= 3600 )
272  Angle -= 3600;
273  return Angle;
274 }
275 
276 template <class T> inline void NORMALIZE_ANGLE_POS( T& Angle )
277 {
278  Angle = NormalizeAnglePos( Angle );
279 }
280 
281 
284 inline double NormalizeAngleDegreesPos( double Angle )
285 {
286  while( Angle < 0 )
287  Angle += 360.0;
288  while( Angle >= 360.0 )
289  Angle -= 360.0;
290  return Angle;
291 }
292 
293 
294 inline void NORMALIZE_ANGLE_DEGREES_POS( double& Angle )
295 {
296  Angle = NormalizeAngleDegreesPos( Angle );
297 }
298 
299 
300 inline double NormalizeAngleRadiansPos( double Angle )
301 {
302  while( Angle < 0 )
303  Angle += (2 * M_PI );
304  while( Angle >= ( 2 * M_PI ) )
305  Angle -= ( 2 * M_PI );
306  return Angle;
307 }
308 
311 inline double NormalizeAngleDegrees( double Angle, double aMin, double aMax )
312 {
313  while( Angle < aMin )
314  Angle += 360.0;
315  while( Angle >= aMax )
316  Angle -= 360.0;
317  return Angle;
318 }
319 
321 // because most of the time it's an int (and templates don't promote in
322 // that way)
323 template <class T, class T2> inline T AddAngles( T a1, T2 a2 )
324 {
325  a1 += a2;
326  NORMALIZE_ANGLE_POS( a1 );
327  return a1;
328 }
329 
330 
331 template <class T> inline T NegateAndNormalizeAnglePos( T Angle )
332 {
333  Angle = -Angle;
334  while( Angle < 0 )
335  Angle += 3600;
336  while( Angle >= 3600 )
337  Angle -= 3600;
338  return Angle;
339 }
340 
341 template <class T> inline void NEGATE_AND_NORMALIZE_ANGLE_POS( T& Angle )
342 {
343  Angle = NegateAndNormalizeAnglePos( Angle );
344 }
345 
346 
348 template <class T> inline T NormalizeAngle90( T Angle )
349 {
350  while( Angle < -900 )
351  Angle += 1800;
352  while( Angle > 900 )
353  Angle -= 1800;
354  return Angle;
355 }
356 
357 template <class T> inline void NORMALIZE_ANGLE_90( T& Angle )
358 {
359  Angle = NormalizeAngle90( Angle );
360 }
361 
362 
364 template <class T> inline T NormalizeAngle180( T Angle )
365 {
366  while( Angle <= -1800 )
367  Angle += 3600;
368  while( Angle > 1800 )
369  Angle -= 3600;
370  return Angle;
371 }
372 
373 template <class T> inline void NORMALIZE_ANGLE_180( T& Angle )
374 {
375  Angle = NormalizeAngle180( Angle );
376 }
377 
386 inline bool InterceptsPositiveX( double aStartAngle, double aEndAngle )
387 {
388  double end = aEndAngle;
389 
390  if( aStartAngle > aEndAngle )
391  end += 360.0;
392 
393  return aStartAngle < 360.0 && end > 360.0;
394 }
395 
404 inline bool InterceptsNegativeX( double aStartAngle, double aEndAngle )
405 {
406  double end = aEndAngle;
407 
408  if( aStartAngle > aEndAngle )
409  end += 360.0;
410 
411  return aStartAngle < 180.0 && end > 180.0;
412 }
413 
418 inline double sindecideg( double r, double a )
419 {
420  return r * sin( DECIDEG2RAD( a ) );
421 }
422 
427 inline double cosdecideg( double r, double a )
428 {
429  return r * cos( DECIDEG2RAD( a ) );
430 }
431 
432 #endif
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:133
T NormalizeAngleNeg(T Angle)
Normalize angle to be in the 0.0 .
Definition: trigo.h:255
double GetLineLength(const wxPoint &aPointA, const wxPoint &aPointB)
Return the length of a line segment defined by aPointA and aPointB.
Definition: trigo.h:211
double GetArcAngle(const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd)
Returns the subtended angle for a given arc.
Definition: trigo.cpp:447
T NormalizeAngle360Max(T Angle)
Normalize angle to be >=-360.0 and <= 360.0 Angle can be equal to -360 or +360.
Definition: trigo.h:231
double RAD2DEG(double rad)
Definition: trigo.h:220
VECTOR2 defines 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:224
T NormalizeAngle90(T Angle)
Normalize angle to be in the -90.0 .. 90.0 range.
Definition: trigo.h:348
void NORMALIZE_ANGLE_DEGREES_POS(double &Angle)
Definition: trigo.h:294
void NORMALIZE_ANGLE_180(T &Angle)
Definition: trigo.h:373
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:276
double NormalizeAngleRadiansPos(double Angle)
Definition: trigo.h:300
void NORMALIZE_ANGLE_90(T &Angle)
Definition: trigo.h:357
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:364
double NormalizeAngleDegreesPos(double Angle)
Normalize angle to be in the 0.0 .
Definition: trigo.h:284
T AddAngles(T a1, T2 a2)
Add two angles (keeping the result normalized). T2 is here.
Definition: trigo.h:323
bool InterceptsPositiveX(double aStartAngle, double aEndAngle)
Test if an arc from aStartAngle to aEndAngle crosses the positive X axis (0 degrees).
Definition: trigo.h:386
double CrossProduct(const wxPoint &vectorA, const wxPoint &vectorB)
Determine the cross product.
Definition: trigo.h:187
void NEGATE_AND_NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:341
double cosdecideg(double r, double a)
Circle generation utility: computes r * cos(a) Where a is in decidegrees, not in radians.
Definition: trigo.h:427
double sindecideg(double r, double a)
Circle generation utility: computes r * sin(a) Where a is in decidegrees, not in radians.
Definition: trigo.h:418
T NegateAndNormalizeAnglePos(T Angle)
Definition: trigo.h:331
double DEG2RAD(double deg)
Definition: trigo.h:219
bool InterceptsNegativeX(double aStartAngle, double aEndAngle)
Test if an arc from aStartAngle to aEndAngle crosses the negative X axis (180 degrees).
Definition: trigo.h:404
T NormalizeAnglePos(T Angle)
Normalize angle to be in the 0.0 .
Definition: trigo.h:267
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:171
double DECIDEG2RAD(double deg)
Definition: trigo.h:223
double NormalizeAngleDegrees(double Angle, double aMin, double aMax)
Normalize angle to be aMin < angle <= aMax angle is in degrees.
Definition: trigo.h:311
double ArcTangente(int dy, int dx)
Definition: trigo.cpp:162
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:405
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:242
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:150