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 
99 /* Return the arc tangent of 0.1 degrees coord vector dx, dy
100  * between -1800 and 1800
101  * Equivalent to atan2 (but faster for calculations if
102  * the angle is 0 to -1800, or + - 900)
103  * Lorenzo: In fact usually atan2 already has to do these optimizations
104  * (due to the discontinuity in tan) but this function also returns
105  * in decidegrees instead of radians, so it's handier
106  */
107 double ArcTangente( int dy, int dx );
108 
112 inline double EuclideanNorm( const wxPoint &vector )
113 {
114  // this is working with doubles
115  return hypot( vector.x, vector.y );
116 }
117 
118 inline double EuclideanNorm( const wxSize &vector )
119 {
120  // this is working with doubles, too
121  return hypot( vector.x, vector.y );
122 }
123 
129 inline double DistanceLinePoint( const wxPoint &linePointA,
130  const wxPoint &linePointB,
131  const wxPoint &referencePoint )
132 {
133  // Some of the multiple double casts are redundant. However in the previous
134  // definition the cast was (implicitly) done too late, just before
135  // the division (EuclideanNorm gives a double so from int it would
136  // be promoted); that means that the whole expression were
137  // vulnerable to overflow during int multiplications
138  return fabs( ( double(linePointB.x - linePointA.x) *
139  double(linePointA.y - referencePoint.y) -
140  double(linePointA.x - referencePoint.x ) *
141  double(linePointB.y - linePointA.y) )
142  / EuclideanNorm( linePointB - linePointA ) );
143 }
144 
150 inline bool HitTestPoints( const wxPoint &pointA, const wxPoint &pointB,
151  double threshold )
152 {
153  wxPoint vectorAB = pointB - pointA;
154 
155  // Compare the distances squared. The double is needed to avoid
156  // overflow during int multiplication
157  double sqdistance = (double)vectorAB.x * vectorAB.x +
158  (double)vectorAB.y * vectorAB.y;
159 
160  return sqdistance < threshold * threshold;
161 }
162 
166 inline double CrossProduct( const wxPoint &vectorA, const wxPoint &vectorB )
167 {
168  // As before the cast is to avoid int overflow
169  return (double)vectorA.x * vectorB.y - (double)vectorA.y * vectorB.x;
170 }
171 
181 bool TestSegmentHit( const wxPoint &aRefPoint, wxPoint aStart,
182  wxPoint aEnd, int aDist );
183 
191 inline double GetLineLength( const wxPoint& aPointA, const wxPoint& aPointB )
192 {
193  // Implicitly casted to double
194  return hypot( aPointA.x - aPointB.x,
195  aPointA.y - aPointB.y );
196 }
197 
198 // These are the usual degrees <-> radians conversion routines
199 inline double DEG2RAD( double deg ) { return deg * M_PI / 180.0; }
200 inline double RAD2DEG( double rad ) { return rad * 180.0 / M_PI; }
201 
202 // These are the same *but* work with the internal 'decidegrees' unit
203 inline double DECIDEG2RAD( double deg ) { return deg * M_PI / 1800.0; }
204 inline double RAD2DECIDEG( double rad ) { return rad * 1800.0 / M_PI; }
205 
206 /* These are templated over T (and not simply double) because eeschema
207  is still using int for angles in some place */
208 
211 template <class T> inline T NormalizeAngle360Max( T Angle )
212 {
213  while( Angle < -3600 )
214  Angle += 3600;
215  while( Angle > 3600 )
216  Angle -= 3600;
217  return Angle;
218 }
219 
222 template <class T> inline T NormalizeAngle360Min( 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 NormalizeAnglePos( T Angle )
234 {
235  while( Angle < 0 )
236  Angle += 3600;
237  while( Angle >= 3600 )
238  Angle -= 3600;
239  return Angle;
240 }
241 template <class T> inline void NORMALIZE_ANGLE_POS( T& Angle )
242 {
243  Angle = NormalizeAnglePos( Angle );
244 }
245 
246 
249 inline double NormalizeAngleDegreesPos( double Angle )
250 {
251  while( Angle < 0 )
252  Angle += 360.0;
253  while( Angle >= 360.0 )
254  Angle -= 360.0;
255  return Angle;
256 }
257 
258 
259 inline void NORMALIZE_ANGLE_DEGREES_POS( double& Angle )
260 {
261  Angle = NormalizeAngleDegreesPos( Angle );
262 }
263 
264 
265 inline double NormalizeAngleRadiansPos( double Angle )
266 {
267  while( Angle < 0 )
268  Angle += (2 * M_PI );
269  while( Angle >= ( 2 * M_PI ) )
270  Angle -= ( 2 * M_PI );
271  return Angle;
272 }
273 
276 inline double NormalizeAngleDegrees( double Angle, double aMin, double aMax )
277 {
278  while( Angle < aMin )
279  Angle += 360.0;
280  while( Angle >= aMax )
281  Angle -= 360.0;
282  return Angle;
283 }
284 
286 // because most of the time it's an int (and templates don't promote in
287 // that way)
288 template <class T, class T2> inline T AddAngles( T a1, T2 a2 )
289 {
290  a1 += a2;
291  NORMALIZE_ANGLE_POS( a1 );
292  return a1;
293 }
294 
295 
296 template <class T> inline T NegateAndNormalizeAnglePos( T Angle )
297 {
298  Angle = -Angle;
299  while( Angle < 0 )
300  Angle += 3600;
301  while( Angle >= 3600 )
302  Angle -= 3600;
303  return Angle;
304 }
305 template <class T> inline void NEGATE_AND_NORMALIZE_ANGLE_POS( T& Angle )
306 {
307  Angle = NegateAndNormalizeAnglePos( Angle );
308 }
309 
310 
312 template <class T> inline T NormalizeAngle90( T Angle )
313 {
314  while( Angle < -900 )
315  Angle += 1800;
316  while( Angle > 900 )
317  Angle -= 1800;
318  return Angle;
319 }
320 template <class T> inline void NORMALIZE_ANGLE_90( T& Angle )
321 {
322  Angle = NormalizeAngle90( Angle );
323 }
324 
325 
327 template <class T> inline T NormalizeAngle180( T Angle )
328 {
329  while( Angle <= -1800 )
330  Angle += 3600;
331  while( Angle > 1800 )
332  Angle -= 3600;
333  return Angle;
334 }
335 template <class T> inline void NORMALIZE_ANGLE_180( T& Angle )
336 {
337  Angle = NormalizeAngle180( Angle );
338 }
339 
340 
345 inline double sindecideg( double r, double a )
346 {
347  return r * sin( DECIDEG2RAD( a ) );
348 }
349 
354 inline double cosdecideg( double r, double a )
355 {
356  return r * cos( DECIDEG2RAD( a ) );
357 }
358 
359 #endif
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:112
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:191
T NormalizeAngle360Max(T Angle)
Normalize angle to be >=-360.0 and <= 360.0 Angle can be equal to -360 or +360.
Definition: trigo.h:211
double RAD2DEG(double rad)
Definition: trigo.h:200
Class VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
bool IsPointOnSegment(const wxPoint &aSegStart, const wxPoint &aSegEnd, const wxPoint &aTestPoint)
Function IsPointOnSegment.
Definition: trigo.cpp:39
double RAD2DECIDEG(double rad)
Definition: trigo.h:204
T NormalizeAngle90(T Angle)
Normalize angle to be in the -90.0 .. 90.0 range.
Definition: trigo.h:312
void NORMALIZE_ANGLE_DEGREES_POS(double &Angle)
Definition: trigo.h:259
void NORMALIZE_ANGLE_180(T &Angle)
Definition: trigo.h:335
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:216
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:241
double NormalizeAngleRadiansPos(double Angle)
Definition: trigo.h:265
void NORMALIZE_ANGLE_90(T &Angle)
Definition: trigo.h:320
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:327
double NormalizeAngleDegreesPos(double Angle)
Normalize angle to be in the 0.0 .
Definition: trigo.h:249
T AddAngles(T a1, T2 a2)
Add two angles (keeping the result normalized). T2 is here.
Definition: trigo.h:288
double CrossProduct(const wxPoint &vectorA, const wxPoint &vectorB)
Determine the cross product.
Definition: trigo.h:166
void NEGATE_AND_NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:305
double cosdecideg(double r, double a)
Circle generation utility: computes r * cos(a) Where a is in decidegrees, not in radians.
Definition: trigo.h:354
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:345
T NegateAndNormalizeAnglePos(T Angle)
Definition: trigo.h:296
double DEG2RAD(double deg)
Definition: trigo.h:199
T NormalizeAnglePos(T Angle)
Normalize angle to be in the 0.0 .
Definition: trigo.h:233
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:150
double DECIDEG2RAD(double deg)
Definition: trigo.h:203
double NormalizeAngleDegrees(double Angle, double aMin, double aMax)
Normalize angle to be aMin < angle <= aMax angle is in degrees.
Definition: trigo.h:276
double ArcTangente(int dy, int dx)
Definition: trigo.cpp:170
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:222
double DistanceLinePoint(const wxPoint &linePointA, const wxPoint &linePointB, const wxPoint &referencePoint)
Compute the distance between a line and a reference point Reference: http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html.
Definition: trigo.h:129