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 
44 bool IsPointOnSegment( const wxPoint& aSegStart, const wxPoint& aSegEnd,
45  const wxPoint& aTestPoint );
46 
57 bool SegmentIntersectsSegment( const wxPoint &a_p1_l1, const wxPoint &a_p2_l1,
58  const wxPoint &a_p1_l2, const wxPoint &a_p2_l2 );
59 
60 /*
61  * Calculate the new point of coord coord pX, pY,
62  * for a rotation center 0, 0, and angle in (1 / 10 degree)
63  */
64 void RotatePoint( int *pX, int *pY, double angle );
65 
66 /*
67  * Calculate the new point of coord coord pX, pY,
68  * for a rotation center cx, cy, and angle in (1 / 10 degree)
69  */
70 void RotatePoint( int *pX, int *pY, int cx, int cy, double angle );
71 
72 /*
73  * Calculates the new coord point point
74  * for a rotation angle in (1 / 10 degree)
75  */
76 inline void RotatePoint( wxPoint* point, double angle )
77 {
78  RotatePoint( &point->x, &point->y, angle );
79 }
80 
81 /*
82  * Calculates the new coord point point
83  * for a center rotation center and angle in (1 / 10 degree)
84  */
85 void RotatePoint( wxPoint *point, const wxPoint & centre, double angle );
86 
87 void RotatePoint( double *pX, double *pY, double angle );
88 
89 void RotatePoint( double *pX, double *pY, double cx, double cy, double angle );
90 
91 /* Return the arc tangent of 0.1 degrees coord vector dx, dy
92  * between -1800 and 1800
93  * Equivalent to atan2 (but faster for calculations if
94  * the angle is 0 to -1800, or + - 900)
95  * Lorenzo: In fact usually atan2 already has to do these optimizations
96  * (due to the discontinuity in tan) but this function also returns
97  * in decidegrees instead of radians, so it's handier
98  */
99 double ArcTangente( int dy, int dx );
100 
104 inline double EuclideanNorm( const wxPoint &vector )
105 {
106  // this is working with doubles
107  return hypot( vector.x, vector.y );
108 }
109 
110 inline double EuclideanNorm( const wxSize &vector )
111 {
112  // this is working with doubles, too
113  return hypot( vector.x, vector.y );
114 }
115 
121 inline double DistanceLinePoint( const wxPoint &linePointA,
122  const wxPoint &linePointB,
123  const wxPoint &referencePoint )
124 {
125  // Some of the multiple double casts are redundant. However in the previous
126  // definition the cast was (implicitly) done too late, just before
127  // the division (EuclideanNorm gives a double so from int it would
128  // be promoted); that means that the whole expression were
129  // vulnerable to overflow during int multiplications
130  return fabs( ( double(linePointB.x - linePointA.x) *
131  double(linePointA.y - referencePoint.y) -
132  double(linePointA.x - referencePoint.x ) *
133  double(linePointB.y - linePointA.y) )
134  / EuclideanNorm( linePointB - linePointA ) );
135 }
136 
142 inline bool HitTestPoints( const wxPoint &pointA, const wxPoint &pointB,
143  double threshold )
144 {
145  wxPoint vectorAB = pointB - pointA;
146 
147  // Compare the distances squared. The double is needed to avoid
148  // overflow during int multiplication
149  double sqdistance = (double)vectorAB.x * vectorAB.x +
150  (double)vectorAB.y * vectorAB.y;
151 
152  return sqdistance < threshold * threshold;
153 }
154 
158 inline double CrossProduct( const wxPoint &vectorA, const wxPoint &vectorB )
159 {
160  // As before the cast is to avoid int overflow
161  return (double)vectorA.x * vectorB.y - (double)vectorA.y * vectorB.x;
162 }
163 
173 bool TestSegmentHit( const wxPoint &aRefPoint, wxPoint aStart,
174  wxPoint aEnd, int aDist );
175 
183 inline double GetLineLength( const wxPoint& aPointA, const wxPoint& aPointB )
184 {
185  // Implicitly casted to double
186  return hypot( aPointA.x - aPointB.x,
187  aPointA.y - aPointB.y );
188 }
189 
190 // These are the usual degrees <-> radians conversion routines
191 inline double DEG2RAD( double deg ) { return deg * M_PI / 180.0; }
192 inline double RAD2DEG( double rad ) { return rad * 180.0 / M_PI; }
193 
194 // These are the same *but* work with the internal 'decidegrees' unit
195 inline double DECIDEG2RAD( double deg ) { return deg * M_PI / 1800.0; }
196 inline double RAD2DECIDEG( double rad ) { return rad * 1800.0 / M_PI; }
197 
198 /* These are templated over T (and not simply double) because eeschema
199  is still using int for angles in some place */
200 
202 template <class T> inline T NormalizeAngle360( T Angle )
203 {
204  while( Angle <= -3600 )
205  Angle += 3600;
206  while( Angle >= 3600 )
207  Angle -= 3600;
208  return Angle;
209 }
210 
211 
214 template <class T> inline T NormalizeAnglePos( T Angle )
215 {
216  while( Angle < 0 )
217  Angle += 3600;
218  while( Angle >= 3600 )
219  Angle -= 3600;
220  return Angle;
221 }
222 template <class T> inline void NORMALIZE_ANGLE_POS( T& Angle )
223 {
224  Angle = NormalizeAnglePos( Angle );
225 }
226 
227 
230 inline double NormalizeAngleDegreesPos( double Angle )
231 {
232  while( Angle < 0 )
233  Angle += 360.0;
234  while( Angle >= 360.0 )
235  Angle -= 360.0;
236  return Angle;
237 }
238 inline void NORMALIZE_ANGLE_DEGREES_POS( double& Angle )
239 {
240  Angle = NormalizeAngleDegreesPos( Angle );
241 }
242 
244 // because most of the time it's an int (and templates don't promote in
245 // that way)
246 template <class T, class T2> inline T AddAngles( T a1, T2 a2 )
247 {
248  a1 += a2;
249  NORMALIZE_ANGLE_POS( a1 );
250  return a1;
251 }
252 
253 template <class T> inline T NegateAndNormalizeAnglePos( T Angle )
254 {
255  Angle = -Angle;
256  while( Angle < 0 )
257  Angle += 3600;
258  while( Angle >= 3600 )
259  Angle -= 3600;
260  return Angle;
261 }
262 template <class T> inline void NEGATE_AND_NORMALIZE_ANGLE_POS( T& Angle )
263 {
264  Angle = NegateAndNormalizeAnglePos( Angle );
265 }
266 
267 
269 template <class T> inline T NormalizeAngle90( T Angle )
270 {
271  while( Angle < -900 )
272  Angle += 1800;
273  while( Angle > 900 )
274  Angle -= 1800;
275  return Angle;
276 }
277 template <class T> inline void NORMALIZE_ANGLE_90( T& Angle )
278 {
279  Angle = NormalizeAngle90( Angle );
280 }
281 
282 
284 template <class T> inline T NormalizeAngle180( T Angle )
285 {
286  while( Angle <= -1800 )
287  Angle += 3600;
288  while( Angle > 1800 )
289  Angle -= 3600;
290  return Angle;
291 }
292 template <class T> inline void NORMALIZE_ANGLE_180( T& Angle )
293 {
294  Angle = NormalizeAngle180( Angle );
295 }
296 
297 
302 inline double sindecideg( double r, double a )
303 {
304  return r * sin( DECIDEG2RAD( a ) );
305 }
306 
311 inline double cosdecideg( double r, double a )
312 {
313  return r * cos( DECIDEG2RAD( a ) );
314 }
315 
316 #endif
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:104
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:183
T
enum T contains all this lexer's tokens.
double RAD2DEG(double rad)
Definition: trigo.h:192
bool IsPointOnSegment(const wxPoint &aSegStart, const wxPoint &aSegEnd, const wxPoint &aTestPoint)
Function IsPointOnSegment.
Definition: trigo.cpp:39
double RAD2DECIDEG(double rad)
Definition: trigo.h:196
T NormalizeAngle90(T Angle)
Normalize angle to be in the -90.0 .. 90.0 range.
Definition: trigo.h:269
void NORMALIZE_ANGLE_DEGREES_POS(double &Angle)
Definition: trigo.h:238
void NORMALIZE_ANGLE_180(T &Angle)
Definition: trigo.h:292
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:317
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:222
void NORMALIZE_ANGLE_90(T &Angle)
Definition: trigo.h:277
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:284
double NormalizeAngleDegreesPos(double Angle)
Normalize angle to be in the 0.0 .
Definition: trigo.h:230
T AddAngles(T a1, T2 a2)
Add two angles (keeping the result normalized). T2 is here.
Definition: trigo.h:246
double CrossProduct(const wxPoint &vectorA, const wxPoint &vectorB)
Determine the cross product.
Definition: trigo.h:158
void NEGATE_AND_NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:262
double cosdecideg(double r, double a)
Circle generation utility: computes r * cos(a) Where a is in decidegrees, not in radians.
Definition: trigo.h:311
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:302
T NegateAndNormalizeAnglePos(T Angle)
Definition: trigo.h:253
double DEG2RAD(double deg)
Definition: trigo.h:191
T NormalizeAnglePos(T Angle)
Normalize angle to be in the 0.0 .
Definition: trigo.h:214
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:142
double DECIDEG2RAD(double deg)
Definition: trigo.h:195
double ArcTangente(int dy, int dx)
Definition: trigo.cpp:271
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:121
T NormalizeAngle360(T Angle)
Normalize angle to be in the -360.0 .. 360.0:
Definition: trigo.h:202