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) 2013 KiCad Developers, see change_log.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 
265 // because most of the time it's an int (and templates don't promote in
266 // that way)
267 template <class T, class T2> inline T AddAngles( T a1, T2 a2 )
268 {
269  a1 += a2;
270  NORMALIZE_ANGLE_POS( a1 );
271  return a1;
272 }
273 
274 
275 template <class T> inline T NegateAndNormalizeAnglePos( T Angle )
276 {
277  Angle = -Angle;
278  while( Angle < 0 )
279  Angle += 3600;
280  while( Angle >= 3600 )
281  Angle -= 3600;
282  return Angle;
283 }
284 template <class T> inline void NEGATE_AND_NORMALIZE_ANGLE_POS( T& Angle )
285 {
286  Angle = NegateAndNormalizeAnglePos( Angle );
287 }
288 
289 
291 template <class T> inline T NormalizeAngle90( T Angle )
292 {
293  while( Angle < -900 )
294  Angle += 1800;
295  while( Angle > 900 )
296  Angle -= 1800;
297  return Angle;
298 }
299 template <class T> inline void NORMALIZE_ANGLE_90( T& Angle )
300 {
301  Angle = NormalizeAngle90( Angle );
302 }
303 
304 
306 template <class T> inline T NormalizeAngle180( T Angle )
307 {
308  while( Angle <= -1800 )
309  Angle += 3600;
310  while( Angle > 1800 )
311  Angle -= 3600;
312  return Angle;
313 }
314 template <class T> inline void NORMALIZE_ANGLE_180( T& Angle )
315 {
316  Angle = NormalizeAngle180( Angle );
317 }
318 
319 
324 inline double sindecideg( double r, double a )
325 {
326  return r * sin( DECIDEG2RAD( a ) );
327 }
328 
333 inline double cosdecideg( double r, double a )
334 {
335  return r * cos( DECIDEG2RAD( a ) );
336 }
337 
338 #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
T
enum T contains all this lexer's tokens.
double RAD2DEG(double rad)
Definition: trigo.h:200
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:291
void NORMALIZE_ANGLE_DEGREES_POS(double &Angle)
Definition: trigo.h:259
void NORMALIZE_ANGLE_180(T &Angle)
Definition: trigo.h:314
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:317
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:241
void NORMALIZE_ANGLE_90(T &Angle)
Definition: trigo.h:299
bool TestSegmentHit(const wxPoint &aRefPoint, wxPoint aStart, wxPoint aEnd, int aDist)
Function TestSegmentHit test for hit on line segment i.e.
Definition: trigo.cpp:142
T NormalizeAngle180(T Angle)
Normalize angle to be in the -180.0 .. 180.0 range.
Definition: trigo.h:306
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:267
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:284
double cosdecideg(double r, double a)
Circle generation utility: computes r * cos(a) Where a is in decidegrees, not in radians.
Definition: trigo.h:333
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:324
T NegateAndNormalizeAnglePos(T Angle)
Definition: trigo.h:275
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 ArcTangente(int dy, int dx)
Definition: trigo.cpp:271
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