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