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 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 <math.h>
32 #include <wx/gdicmn.h> // For wxPoint
33 #include <math/vector2d.h>
34 
45 bool IsPointOnSegment( const wxPoint& aSegStart, const wxPoint& aSegEnd,
46  const wxPoint& aTestPoint );
47 
58 bool SegmentIntersectsSegment( const wxPoint &a_p1_l1, const wxPoint &a_p2_l1,
59  const wxPoint &a_p1_l2, const wxPoint &a_p2_l2 );
60 
61 /*
62  * Calculate the new point of coord coord pX, pY,
63  * for a rotation center 0, 0, and angle in (1 / 10 degree)
64  */
65 void RotatePoint( int *pX, int *pY, double angle );
66 
67 /*
68  * Calculate the new point of coord coord pX, pY,
69  * for a rotation center cx, cy, and angle in (1 / 10 degree)
70  */
71 void RotatePoint( int *pX, int *pY, int cx, int cy, double angle );
72 
73 /*
74  * Calculates the new coord point point
75  * for a rotation angle in (1 / 10 degree)
76  */
77 inline void RotatePoint( wxPoint* point, double angle )
78 {
79  RotatePoint( &point->x, &point->y, angle );
80 }
81 
82 inline void RotatePoint( VECTOR2I& point, double angle )
83 {
84  RotatePoint( &point.x, &point.y, angle );
85 }
86 
87 void RotatePoint( VECTOR2I& point, const VECTOR2I& centre, double angle );
88 
89 /*
90  * Calculates the new coord point point
91  * for a center rotation center and angle in (1 / 10 degree)
92  */
93 void RotatePoint( wxPoint *point, const wxPoint & centre, double angle );
94 
95 void RotatePoint( double *pX, double *pY, double angle );
96 
97 void RotatePoint( double *pX, double *pY, double cx, double cy, double angle );
98 
106 const VECTOR2I GetArcCenter( const VECTOR2I& aStart, const VECTOR2I& aMid, const VECTOR2I& aEnd );
107 
108 /* Return the arc tangent of 0.1 degrees coord vector dx, dy
109  * between -1800 and 1800
110  * Equivalent to atan2 (but faster for calculations if
111  * the angle is 0 to -1800, or + - 900)
112  * Lorenzo: In fact usually atan2 already has to do these optimizations
113  * (due to the discontinuity in tan) but this function also returns
114  * in decidegrees instead of radians, so it's handier
115  */
116 double ArcTangente( int dy, int dx );
117 
121 inline double EuclideanNorm( const wxPoint &vector )
122 {
123  // this is working with doubles
124  return hypot( vector.x, vector.y );
125 }
126 
127 inline double EuclideanNorm( const wxSize &vector )
128 {
129  // this is working with doubles, too
130  return hypot( vector.x, vector.y );
131 }
132 
138 inline double DistanceLinePoint( const wxPoint &linePointA,
139  const wxPoint &linePointB,
140  const wxPoint &referencePoint )
141 {
142  // Some of the multiple double casts are redundant. However in the previous
143  // definition the cast was (implicitly) done too late, just before
144  // the division (EuclideanNorm gives a double so from int it would
145  // be promoted); that means that the whole expression were
146  // vulnerable to overflow during int multiplications
147  return fabs( ( double(linePointB.x - linePointA.x) *
148  double(linePointA.y - referencePoint.y) -
149  double(linePointA.x - referencePoint.x ) *
150  double(linePointB.y - linePointA.y) )
151  / EuclideanNorm( linePointB - linePointA ) );
152 }
153 
159 inline bool HitTestPoints( const wxPoint &pointA, const wxPoint &pointB,
160  double threshold )
161 {
162  wxPoint vectorAB = pointB - pointA;
163 
164  // Compare the distances squared. The double is needed to avoid
165  // overflow during int multiplication
166  double sqdistance = (double)vectorAB.x * vectorAB.x +
167  (double)vectorAB.y * vectorAB.y;
168 
169  return sqdistance < threshold * threshold;
170 }
171 
175 inline double CrossProduct( const wxPoint &vectorA, const wxPoint &vectorB )
176 {
177  // As before the cast is to avoid int overflow
178  return (double)vectorA.x * vectorB.y - (double)vectorA.y * vectorB.x;
179 }
180 
190 bool TestSegmentHit( const wxPoint &aRefPoint, wxPoint aStart,
191  wxPoint aEnd, int aDist );
192 
200 inline double GetLineLength( const wxPoint& aPointA, const wxPoint& aPointB )
201 {
202  // Implicitly casted to double
203  return hypot( aPointA.x - aPointB.x,
204  aPointA.y - aPointB.y );
205 }
206 
207 // These are the usual degrees <-> radians conversion routines
208 inline double DEG2RAD( double deg ) { return deg * M_PI / 180.0; }
209 inline double RAD2DEG( double rad ) { return rad * 180.0 / M_PI; }
210 
211 // These are the same *but* work with the internal 'decidegrees' unit
212 inline double DECIDEG2RAD( double deg ) { return deg * M_PI / 1800.0; }
213 inline double RAD2DECIDEG( double rad ) { return rad * 1800.0 / M_PI; }
214 
215 /* These are templated over T (and not simply double) because eeschema
216  is still using int for angles in some place */
217 
220 template <class T> inline T NormalizeAngle360Max( T Angle )
221 {
222  while( Angle < -3600 )
223  Angle += 3600;
224  while( Angle > 3600 )
225  Angle -= 3600;
226  return Angle;
227 }
228 
231 template <class T> inline T NormalizeAngle360Min( 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 NormalizeAnglePos( T Angle )
243 {
244  while( Angle < 0 )
245  Angle += 3600;
246  while( Angle >= 3600 )
247  Angle -= 3600;
248  return Angle;
249 }
250 template <class T> inline void NORMALIZE_ANGLE_POS( T& Angle )
251 {
252  Angle = NormalizeAnglePos( Angle );
253 }
254 
255 
258 inline double NormalizeAngleDegreesPos( double Angle )
259 {
260  while( Angle < 0 )
261  Angle += 360.0;
262  while( Angle >= 360.0 )
263  Angle -= 360.0;
264  return Angle;
265 }
266 
267 
268 inline void NORMALIZE_ANGLE_DEGREES_POS( double& Angle )
269 {
270  Angle = NormalizeAngleDegreesPos( Angle );
271 }
272 
273 
274 inline double NormalizeAngleRadiansPos( double Angle )
275 {
276  while( Angle < 0 )
277  Angle += (2 * M_PI );
278  while( Angle >= ( 2 * M_PI ) )
279  Angle -= ( 2 * M_PI );
280  return Angle;
281 }
282 
285 inline double NormalizeAngleDegrees( double Angle, double aMin, double aMax )
286 {
287  while( Angle < aMin )
288  Angle += 360.0;
289  while( Angle >= aMax )
290  Angle -= 360.0;
291  return Angle;
292 }
293 
295 // because most of the time it's an int (and templates don't promote in
296 // that way)
297 template <class T, class T2> inline T AddAngles( T a1, T2 a2 )
298 {
299  a1 += a2;
300  NORMALIZE_ANGLE_POS( a1 );
301  return a1;
302 }
303 
304 
305 template <class T> inline T NegateAndNormalizeAnglePos( T Angle )
306 {
307  Angle = -Angle;
308  while( Angle < 0 )
309  Angle += 3600;
310  while( Angle >= 3600 )
311  Angle -= 3600;
312  return Angle;
313 }
314 template <class T> inline void NEGATE_AND_NORMALIZE_ANGLE_POS( T& Angle )
315 {
316  Angle = NegateAndNormalizeAnglePos( Angle );
317 }
318 
319 
321 template <class T> inline T NormalizeAngle90( T Angle )
322 {
323  while( Angle < -900 )
324  Angle += 1800;
325  while( Angle > 900 )
326  Angle -= 1800;
327  return Angle;
328 }
329 template <class T> inline void NORMALIZE_ANGLE_90( T& Angle )
330 {
331  Angle = NormalizeAngle90( Angle );
332 }
333 
334 
336 template <class T> inline T NormalizeAngle180( T Angle )
337 {
338  while( Angle <= -1800 )
339  Angle += 3600;
340  while( Angle > 1800 )
341  Angle -= 3600;
342  return Angle;
343 }
344 template <class T> inline void NORMALIZE_ANGLE_180( T& Angle )
345 {
346  Angle = NormalizeAngle180( Angle );
347 }
348 
349 
354 inline double sindecideg( double r, double a )
355 {
356  return r * sin( DECIDEG2RAD( a ) );
357 }
358 
363 inline double cosdecideg( double r, double a )
364 {
365  return r * cos( DECIDEG2RAD( a ) );
366 }
367 
368 #endif
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:121
double GetLineLength(const wxPoint &aPointA, const wxPoint &aPointB)
Function GetLineLength returns the length of a line segment defined by aPointA and aPointB.
Definition: trigo.h:200
T NormalizeAngle360Max(T Angle)
Normalize angle to be >=-360.0 and <= 360.0 Angle can be equal to -360 or +360.
Definition: trigo.h:220
double RAD2DEG(double rad)
Definition: trigo.h:209
bool IsPointOnSegment(const wxPoint &aSegStart, const wxPoint &aSegEnd, const wxPoint &aTestPoint)
Function IsPointOnSegment.
Definition: trigo.cpp:39
double RAD2DECIDEG(double rad)
Definition: trigo.h:213
T NormalizeAngle90(T Angle)
Normalize angle to be in the -90.0 .. 90.0 range.
Definition: trigo.h:321
void NORMALIZE_ANGLE_DEGREES_POS(double &Angle)
Definition: trigo.h:268
void NORMALIZE_ANGLE_180(T &Angle)
Definition: trigo.h:344
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:216
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:250
double NormalizeAngleRadiansPos(double Angle)
Definition: trigo.h:274
void NORMALIZE_ANGLE_90(T &Angle)
Definition: trigo.h:329
bool TestSegmentHit(const wxPoint &aRefPoint, wxPoint aStart, wxPoint aEnd, int aDist)
Function TestSegmentHit test for hit on line segment i.e.
Definition: trigo.cpp:122
T NormalizeAngle180(T Angle)
Normalize angle to be in the -180.0 .. 180.0 range.
Definition: trigo.h:336
double NormalizeAngleDegreesPos(double Angle)
Normalize angle to be in the 0.0 .
Definition: trigo.h:258
T AddAngles(T a1, T2 a2)
Add two angles (keeping the result normalized). T2 is here.
Definition: trigo.h:297
double CrossProduct(const wxPoint &vectorA, const wxPoint &vectorB)
Determine the cross product.
Definition: trigo.h:175
void NEGATE_AND_NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:314
double cosdecideg(double r, double a)
Circle generation utility: computes r * cos(a) Where a is in decidegrees, not in radians.
Definition: trigo.h:363
bool SegmentIntersectsSegment(const wxPoint &a_p1_l1, const wxPoint &a_p2_l1, const wxPoint &a_p1_l2, const wxPoint &a_p2_l2)
Function SegmentIntersectsSegment.
Definition: trigo.cpp:58
double sindecideg(double r, double a)
Circle generation utility: computes r * sin(a) Where a is in decidegrees, not in radians.
Definition: trigo.h:354
T NegateAndNormalizeAnglePos(T Angle)
Definition: trigo.h:305
double DEG2RAD(double deg)
Definition: trigo.h:208
T NormalizeAnglePos(T Angle)
Normalize angle to be in the 0.0 .
Definition: trigo.h:242
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:159
double DECIDEG2RAD(double deg)
Definition: trigo.h:212
double NormalizeAngleDegrees(double Angle, double aMin, double aMax)
Normalize angle to be aMin < angle <= aMax angle is in degrees.
Definition: trigo.h:285
double ArcTangente(int dy, int dx)
Definition: trigo.cpp:170
const VECTOR2I GetArcCenter(const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd)
Determine the center of an arc/circle, given three points on its circumference.
Definition: trigo.cpp:349
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:231
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:138