KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2021 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
27#include <cmath>
28#include <math/vector2d.h>
29#include <geometry/eda_angle.h>
30
43bool IsPointOnSegment( const VECTOR2I& aSegStart, const VECTOR2I& aSegEnd,
44 const VECTOR2I& aTestPoint );
45
57bool SegmentIntersectsSegment( const VECTOR2I& a_p1_l1, const VECTOR2I& a_p2_l1,
58 const VECTOR2I& a_p1_l2, const VECTOR2I& a_p2_l2,
59 VECTOR2I* aIntersectionPoint = nullptr );
60
61/*
62 * Calculate the new point of coord coord pX, pY, for a rotation center 0, 0
63 */
64void RotatePoint( int *pX, int *pY, const EDA_ANGLE& aAngle );
65
66inline void RotatePoint( VECTOR2I& point, const EDA_ANGLE& aAngle )
67{
68 RotatePoint( &point.x, &point.y, aAngle );
69}
70
71
72/*
73 * Calculate the new point of coord coord pX, pY, for a rotation center cx, cy
74 */
75void RotatePoint( int *pX, int *pY, int cx, int cy, const EDA_ANGLE& aAngle );
76
77inline void RotatePoint( VECTOR2I& point, const VECTOR2I& centre, const EDA_ANGLE& aAngle )
78{
79 RotatePoint( &point.x, &point.y, centre.x, centre.y, aAngle );
80}
81
82
83/*
84 * Calculate the new coord point point for a rotation center 0, 0
85 */
86
87void RotatePoint( double* pX, double* pY, const EDA_ANGLE& aAngle );
88
89inline void RotatePoint( VECTOR2D& point, const EDA_ANGLE& aAngle )
90{
91 RotatePoint( &point.x, &point.y, aAngle );
92}
93
94
95void RotatePoint( double* pX, double* pY, double cx, double cy, const EDA_ANGLE& aAngle );
96
97inline void RotatePoint( VECTOR2D& point, const VECTOR2D& aCenter, const EDA_ANGLE& aAngle )
98{
99 RotatePoint( &point.x, &point.y, aCenter.x, aCenter.y, aAngle );
100}
101
110const VECTOR2I CalcArcCenter( const VECTOR2I& aStart, const VECTOR2I& aMid, const VECTOR2I& aEnd );
111const VECTOR2D CalcArcCenter( const VECTOR2D& aStart, const VECTOR2D& aMid, const VECTOR2D& aEnd );
112const VECTOR2D CalcArcCenter( const VECTOR2D& aStart, const VECTOR2D& aEnd,
113 const EDA_ANGLE& aAngle );
114
126const VECTOR2I CalcArcMid( const VECTOR2I& aStart, const VECTOR2I& aEnd, const VECTOR2I& aCenter,
127 bool aMinArcAngle = true );
128
129inline double EuclideanNorm( const VECTOR2I& vector )
130{
131 // this is working with doubles
132 return hypot( vector.x, vector.y );
133}
134
140inline double DistanceLinePoint( const VECTOR2I& linePointA, const VECTOR2I& linePointB,
141 const VECTOR2I& referencePoint )
142{
143 // Some of the multiple double casts are redundant. However in the previous definition
144 // the cast was (implicitly) done too late, just before the division (EuclideanNorm gives
145 // a double so from int it would be promoted); that means that the whole expression were
146 // vulnerable to overflow during int multiplications
147 return fabs( ( static_cast<double>( linePointB.x - linePointA.x ) *
148 static_cast<double>( linePointA.y - referencePoint.y ) -
149 static_cast<double>( linePointA.x - referencePoint.x ) *
150 static_cast<double>( linePointB.y - linePointA.y) )
151 / EuclideanNorm( linePointB - linePointA ) );
152}
153
159inline bool HitTestPoints( const VECTOR2I& pointA, const VECTOR2I& pointB, double threshold )
160{
161 VECTOR2I vectorAB = pointB - pointA;
162
163 // Compare the distances squared. The double is needed to avoid overflow during int
164 // multiplication
165 double sqdistance = (double)vectorAB.x * vectorAB.x + (double)vectorAB.y * vectorAB.y;
166
167 return sqdistance < threshold * threshold;
168}
169
178bool TestSegmentHit( const VECTOR2I& aRefPoint, const VECTOR2I& aStart, const VECTOR2I& aEnd,
179 int aDist );
180
188inline double GetLineLength( const VECTOR2I& aPointA, const VECTOR2I& aPointB )
189{
190 // Implicitly casted to double
191 return hypot( aPointA.x - aPointB.x, aPointA.y - aPointB.y );
192}
193
194// These are the usual degrees <-> radians conversion routines
195inline double DEG2RAD( double deg ) { return deg * M_PI / 180.0; }
196inline double RAD2DEG( double rad ) { return rad * 180.0 / M_PI; }
197
198// These are the same *but* work with the internal 'decidegrees' unit
199inline double RAD2DECIDEG( double rad ) { return rad * 1800.0 / M_PI; }
200
201/* These are templated over T (and not simply double) because Eeschema
202 is still using int for angles in some place */
203
205template <class T> inline T NormalizeAnglePos( T Angle )
206{
207 while( Angle < 0 )
208 Angle += 3600;
209 while( Angle >= 3600 )
210 Angle -= 3600;
211 return Angle;
212}
213
214template <class T> inline void NORMALIZE_ANGLE_POS( T& Angle )
215{
216 Angle = NormalizeAnglePos( Angle );
217}
218
219
221template <class T> inline T NormalizeAngle180( T Angle )
222{
223 while( Angle <= -1800 )
224 Angle += 3600;
225
226 while( Angle > 1800 )
227 Angle -= 3600;
228
229 return Angle;
230}
231
240inline bool InterceptsPositiveX( double aStartAngle, double aEndAngle )
241{
242 double end = aEndAngle;
243
244 if( aStartAngle > aEndAngle )
245 end += 360.0;
246
247 return aStartAngle < 360.0 && end > 360.0;
248}
249
258inline bool InterceptsNegativeX( double aStartAngle, double aEndAngle )
259{
260 double end = aEndAngle;
261
262 if( aStartAngle > aEndAngle )
263 end += 360.0;
264
265 return aStartAngle < 180.0 && end > 180.0;
266}
267
268#endif
const VECTOR2I CalcArcMid(const VECTOR2I &aStart, const VECTOR2I &aEnd, const VECTOR2I &aCenter, bool aMinArcAngle=true)
Return the middle point of an arc, half-way between aStart and aEnd.
Definition: trigo.cpp:163
bool TestSegmentHit(const VECTOR2I &aRefPoint, const VECTOR2I &aStart, const VECTOR2I &aEnd, int aDist)
Test if aRefPoint is with aDistance on the line defined by aStart and aEnd.
Definition: trigo.cpp:129
double DistanceLinePoint(const VECTOR2I &linePointA, const VECTOR2I &linePointB, const VECTOR2I &referencePoint)
Compute the distance between a line and a reference point Reference: http://mathworld....
Definition: trigo.h:140
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Definition: trigo.cpp:183
bool InterceptsPositiveX(double aStartAngle, double aEndAngle)
Test if an arc from aStartAngle to aEndAngle crosses the positive X axis (0 degrees).
Definition: trigo.h:240
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:214
bool InterceptsNegativeX(double aStartAngle, double aEndAngle)
Test if an arc from aStartAngle to aEndAngle crosses the negative X axis (180 degrees).
Definition: trigo.h:258
T NormalizeAnglePos(T Angle)
Normalize angle to be in the 0.0 .. 360.0 range: angle is in 1/10 degrees.
Definition: trigo.h:205
bool SegmentIntersectsSegment(const VECTOR2I &a_p1_l1, const VECTOR2I &a_p2_l1, const VECTOR2I &a_p1_l2, const VECTOR2I &a_p2_l2, VECTOR2I *aIntersectionPoint=nullptr)
Test if two lines intersect.
Definition: trigo.cpp:61
bool IsPointOnSegment(const VECTOR2I &aSegStart, const VECTOR2I &aSegEnd, const VECTOR2I &aTestPoint)
Test if aTestPoint is on line defined by aSegStart and aSegEnd.
Definition: trigo.cpp:42
double RAD2DEG(double rad)
Definition: trigo.h:196
bool HitTestPoints(const VECTOR2I &pointA, const VECTOR2I &pointB, double threshold)
Test, if two points are near each other.
Definition: trigo.h:159
double DEG2RAD(double deg)
Definition: trigo.h:195
T NormalizeAngle180(T Angle)
Normalize angle to be in the -180.0 .. 180.0 range.
Definition: trigo.h:221
const VECTOR2I CalcArcCenter(const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd)
Determine the center of an arc or circle given three points on its circumference.
Definition: trigo.cpp:458
double GetLineLength(const VECTOR2I &aPointA, const VECTOR2I &aPointB)
Return the length of a line segment defined by aPointA and aPointB.
Definition: trigo.h:188
double RAD2DECIDEG(double rad)
Definition: trigo.h:199
double EuclideanNorm(const VECTOR2I &vector)
Definition: trigo.h:129