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 
115 /* Return the arc tangent of 0.1 degrees coord vector dx, dy
116  * between -1800 and 1800
117  * Equivalent to atan2 (but faster for calculations if
118  * the angle is 0 to -1800, or + - 900)
119  * Lorenzo: In fact usually atan2 already has to do these optimizations
120  * (due to the discontinuity in tan) but this function also returns
121  * in decidegrees instead of radians, so it's handier
122  */
123 double ArcTangente( int dy, int dx );
124 
128 inline double EuclideanNorm( const wxPoint &vector )
129 {
130  // this is working with doubles
131  return hypot( vector.x, vector.y );
132 }
133 
134 inline double EuclideanNorm( const wxSize &vector )
135 {
136  // this is working with doubles, too
137  return hypot( vector.x, vector.y );
138 }
139 
145 inline double DistanceLinePoint( const wxPoint &linePointA,
146  const wxPoint &linePointB,
147  const wxPoint &referencePoint )
148 {
149  // Some of the multiple double casts are redundant. However in the previous
150  // definition the cast was (implicitly) done too late, just before
151  // the division (EuclideanNorm gives a double so from int it would
152  // be promoted); that means that the whole expression were
153  // vulnerable to overflow during int multiplications
154  return fabs( ( double(linePointB.x - linePointA.x) *
155  double(linePointA.y - referencePoint.y) -
156  double(linePointA.x - referencePoint.x ) *
157  double(linePointB.y - linePointA.y) )
158  / EuclideanNorm( linePointB - linePointA ) );
159 }
160 
166 inline bool HitTestPoints( const wxPoint &pointA, const wxPoint &pointB,
167  double threshold )
168 {
169  wxPoint vectorAB = pointB - pointA;
170 
171  // Compare the distances squared. The double is needed to avoid
172  // overflow during int multiplication
173  double sqdistance = (double)vectorAB.x * vectorAB.x +
174  (double)vectorAB.y * vectorAB.y;
175 
176  return sqdistance < threshold * threshold;
177 }
178 
182 inline double CrossProduct( const wxPoint &vectorA, const wxPoint &vectorB )
183 {
184  // As before the cast is to avoid int overflow
185  return (double)vectorA.x * vectorB.y - (double)vectorA.y * vectorB.x;
186 }
187 
196 bool TestSegmentHit( const wxPoint &aRefPoint, wxPoint aStart,
197  wxPoint aEnd, int aDist );
198 
206 inline double GetLineLength( const wxPoint& aPointA, const wxPoint& aPointB )
207 {
208  // Implicitly casted to double
209  return hypot( aPointA.x - aPointB.x,
210  aPointA.y - aPointB.y );
211 }
212 
213 // These are the usual degrees <-> radians conversion routines
214 inline double DEG2RAD( double deg ) { return deg * M_PI / 180.0; }
215 inline double RAD2DEG( double rad ) { return rad * 180.0 / M_PI; }
216 
217 // These are the same *but* work with the internal 'decidegrees' unit
218 inline double DECIDEG2RAD( double deg ) { return deg * M_PI / 1800.0; }
219 inline double RAD2DECIDEG( double rad ) { return rad * 1800.0 / M_PI; }
220 
221 /* These are templated over T (and not simply double) because Eeschema
222  is still using int for angles in some place */
223 
226 template <class T> inline T NormalizeAngle360Max( T Angle )
227 {
228  while( Angle < -3600 )
229  Angle += 3600;
230  while( Angle > 3600 )
231  Angle -= 3600;
232  return Angle;
233 }
234 
237 template <class T> inline T NormalizeAngle360Min( T Angle )
238 {
239  while( Angle <= -3600 )
240  Angle += 3600;
241  while( Angle >= 3600 )
242  Angle -= 3600;
243  return Angle;
244 }
245 
248 template <class T> inline T NormalizeAnglePos( T Angle )
249 {
250  while( Angle < 0 )
251  Angle += 3600;
252  while( Angle >= 3600 )
253  Angle -= 3600;
254  return Angle;
255 }
256 
257 template <class T> inline void NORMALIZE_ANGLE_POS( T& Angle )
258 {
259  Angle = NormalizeAnglePos( Angle );
260 }
261 
262 
265 inline double NormalizeAngleDegreesPos( double Angle )
266 {
267  while( Angle < 0 )
268  Angle += 360.0;
269  while( Angle >= 360.0 )
270  Angle -= 360.0;
271  return Angle;
272 }
273 
274 
275 inline void NORMALIZE_ANGLE_DEGREES_POS( double& Angle )
276 {
277  Angle = NormalizeAngleDegreesPos( Angle );
278 }
279 
280 
281 inline double NormalizeAngleRadiansPos( double Angle )
282 {
283  while( Angle < 0 )
284  Angle += (2 * M_PI );
285  while( Angle >= ( 2 * M_PI ) )
286  Angle -= ( 2 * M_PI );
287  return Angle;
288 }
289 
292 inline double NormalizeAngleDegrees( double Angle, double aMin, double aMax )
293 {
294  while( Angle < aMin )
295  Angle += 360.0;
296  while( Angle >= aMax )
297  Angle -= 360.0;
298  return Angle;
299 }
300 
302 // because most of the time it's an int (and templates don't promote in
303 // that way)
304 template <class T, class T2> inline T AddAngles( T a1, T2 a2 )
305 {
306  a1 += a2;
307  NORMALIZE_ANGLE_POS( a1 );
308  return a1;
309 }
310 
311 
312 template <class T> inline T NegateAndNormalizeAnglePos( T Angle )
313 {
314  Angle = -Angle;
315  while( Angle < 0 )
316  Angle += 3600;
317  while( Angle >= 3600 )
318  Angle -= 3600;
319  return Angle;
320 }
321 
322 template <class T> inline void NEGATE_AND_NORMALIZE_ANGLE_POS( T& Angle )
323 {
324  Angle = NegateAndNormalizeAnglePos( Angle );
325 }
326 
327 
329 template <class T> inline T NormalizeAngle90( T Angle )
330 {
331  while( Angle < -900 )
332  Angle += 1800;
333  while( Angle > 900 )
334  Angle -= 1800;
335  return Angle;
336 }
337 
338 template <class T> inline void NORMALIZE_ANGLE_90( T& Angle )
339 {
340  Angle = NormalizeAngle90( Angle );
341 }
342 
343 
345 template <class T> inline T NormalizeAngle180( T Angle )
346 {
347  while( Angle <= -1800 )
348  Angle += 3600;
349  while( Angle > 1800 )
350  Angle -= 3600;
351  return Angle;
352 }
353 
354 template <class T> inline void NORMALIZE_ANGLE_180( T& Angle )
355 {
356  Angle = NormalizeAngle180( Angle );
357 }
358 
367 inline bool InterceptsPositiveX( double aStartAngle, double aEndAngle )
368 {
369  double end = aEndAngle;
370 
371  if( aStartAngle > aEndAngle )
372  end += 360.0;
373 
374  return aStartAngle < 360.0 && end > 360.0;
375 }
376 
385 inline bool InterceptsNegativeX( double aStartAngle, double aEndAngle )
386 {
387  double end = aEndAngle;
388 
389  if( aStartAngle > aEndAngle )
390  end += 360.0;
391 
392  return aStartAngle < 180.0 && end > 180.0;
393 }
394 
399 inline double sindecideg( double r, double a )
400 {
401  return r * sin( DECIDEG2RAD( a ) );
402 }
403 
408 inline double cosdecideg( double r, double a )
409 {
410  return r * cos( DECIDEG2RAD( a ) );
411 }
412 
413 #endif
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:128
double GetLineLength(const wxPoint &aPointA, const wxPoint &aPointB)
Return the length of a line segment defined by aPointA and aPointB.
Definition: trigo.h:206
T NormalizeAngle360Max(T Angle)
Normalize angle to be >=-360.0 and <= 360.0 Angle can be equal to -360 or +360.
Definition: trigo.h:226
double RAD2DEG(double rad)
Definition: trigo.h:215
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:219
T NormalizeAngle90(T Angle)
Normalize angle to be in the -90.0 .. 90.0 range.
Definition: trigo.h:329
void NORMALIZE_ANGLE_DEGREES_POS(double &Angle)
Definition: trigo.h:275
void NORMALIZE_ANGLE_180(T &Angle)
Definition: trigo.h:354
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:257
double NormalizeAngleRadiansPos(double Angle)
Definition: trigo.h:281
void NORMALIZE_ANGLE_90(T &Angle)
Definition: trigo.h:338
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:345
double NormalizeAngleDegreesPos(double Angle)
Normalize angle to be in the 0.0 .
Definition: trigo.h:265
T AddAngles(T a1, T2 a2)
Add two angles (keeping the result normalized). T2 is here.
Definition: trigo.h:304
bool InterceptsPositiveX(double aStartAngle, double aEndAngle)
Test if an arc from aStartAngle to aEndAngle crosses the positive X axis (0 degrees).
Definition: trigo.h:367
double CrossProduct(const wxPoint &vectorA, const wxPoint &vectorB)
Determine the cross product.
Definition: trigo.h:182
void NEGATE_AND_NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:322
double cosdecideg(double r, double a)
Circle generation utility: computes r * cos(a) Where a is in decidegrees, not in radians.
Definition: trigo.h:408
double sindecideg(double r, double a)
Circle generation utility: computes r * sin(a) Where a is in decidegrees, not in radians.
Definition: trigo.h:399
T NegateAndNormalizeAnglePos(T Angle)
Definition: trigo.h:312
double DEG2RAD(double deg)
Definition: trigo.h:214
bool InterceptsNegativeX(double aStartAngle, double aEndAngle)
Test if an arc from aStartAngle to aEndAngle crosses the negative X axis (180 degrees).
Definition: trigo.h:385
T NormalizeAnglePos(T Angle)
Normalize angle to be in the 0.0 .
Definition: trigo.h:248
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:166
double DECIDEG2RAD(double deg)
Definition: trigo.h:218
double NormalizeAngleDegrees(double Angle, double aMin, double aMax)
Normalize angle to be aMin < angle <= aMax angle is in degrees.
Definition: trigo.h:292
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:397
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:237
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:145