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