KiCad PCB EDA Suite
Loading...
Searching...
No Matches
eda_angle.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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, see <https://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <cassert>
23#include <cmath>
24#include <math/vector2d.h> // for VECTOR2I
25
26
33
34
36{
37public:
45 EDA_ANGLE( double aValue, EDA_ANGLE_T aAngleType )
46 {
47 switch( aAngleType )
48 {
49 case RADIANS_T:
50 m_value = aValue / DEGREES_TO_RADIANS;
51 break;
52
54 m_value = aValue / 10.0;
55 break;
56
57 default:
58 m_value = aValue;
59 }
60 }
61
67 constexpr explicit EDA_ANGLE( double aAngleInDegrees ) :
68 m_value( aAngleInDegrees )
69 {}
70
71 explicit EDA_ANGLE( const VECTOR2D& aVector )
72 {
73 if( aVector.x == 0.0 && aVector.y == 0.0 )
74 {
75 m_value = 0.0;
76 }
77 else if( aVector.y == 0.0 )
78 {
79 if( aVector.x >= 0 )
80 m_value = 0.0;
81 else
82 m_value = -180.0;
83 }
84 else if( aVector.x == 0.0 )
85 {
86 if( aVector.y >= 0.0 )
87 m_value = 90.0;
88 else
89 m_value = -90.0;
90 }
91 else if( aVector.x == aVector.y )
92 {
93 if( aVector.x >= 0.0 )
94 m_value = 45.0;
95 else
96 m_value = -180.0 + 45.0;
97 }
98 else if( aVector.x == -aVector.y )
99 {
100 if( aVector.x >= 0.0 )
101 m_value = -45.0;
102 else
103 m_value = 180.0 - 45.0;
104 }
105 else
106 {
107 *this = EDA_ANGLE( atan2( aVector.y, aVector.x ), RADIANS_T );
108 }
109 }
110
112 m_value( 0.0 )
113 {}
114
115 inline double AsDegrees() const { return m_value; }
116
117 inline int AsTenthsOfADegree() const { return KiROUND( m_value * 10.0 ); }
118
119 inline double AsRadians() const { return m_value * DEGREES_TO_RADIANS; }
120
121 static constexpr double DEGREES_TO_RADIANS = M_PI / 180.0;
122
127 bool IsCardinal() const;
128
133 bool IsCardinal90() const;
134
135 bool IsZero() const
136 {
137 return m_value == 0.0;
138 // return equals( m_value, 0.0 );
139 }
140
141 bool IsHorizontal() const
142 {
143 return m_value == 0.0 || m_value == 180.0;
144 // return equals( m_value, 0.0 ) || equals( m_value, 180.0 );
145 }
146
147 bool IsVertical() const
148 {
149 return m_value == 90.0 || m_value == 270.0;
150 //return equals( m_value, 90.0 ) || equals( m_value, 270.0 );
151 }
152
153 bool IsParallelTo( EDA_ANGLE aAngle ) const
154 {
155 EDA_ANGLE thisNormalized = *this;
156
157 // Normalize90 is inclusive on both ends [-90, +90]
158 // but we need it to be (-90, +90] for this test to work
159 thisNormalized.Normalize90();
160
161 if( equals( thisNormalized.AsDegrees(), -90.0 ) )
162 thisNormalized = EDA_ANGLE( 90.0, DEGREES_T );
163
164 aAngle.Normalize90();
165
166 if( equals( aAngle.AsDegrees(), -90.0 ) )
167 aAngle = EDA_ANGLE( 90.0, DEGREES_T );
168
169 return ( equals( thisNormalized.AsDegrees(), aAngle.AsDegrees() ) );
170 }
171
173 {
174 return EDA_ANGLE( -AsDegrees(), DEGREES_T );
175 }
176
177 double Sin() const
178 {
179 EDA_ANGLE test = *this;
180 test.Normalize();
181
182 if( test.m_value == 0.0 || test.m_value == 180.0 )
183 return 0.0;
184 else if( test.m_value == 45.0 || test.m_value == 135.0 )
185 return M_SQRT1_2; // sqrt(2)/2
186 else if( test.m_value == 225.0 || test.m_value == 315.0 )
187 return -M_SQRT1_2;
188 else if( test.m_value == 90.0 )
189 return 1.0;
190 else if( test.m_value == 270.0 )
191 return -1.0;
192 else
193 return sin( AsRadians() );
194 }
195
196 double Cos() const
197 {
198 EDA_ANGLE test = *this;
199 test.Normalize();
200
201 if( test.m_value == 0.0 )
202 return 1.0;
203 else if( test.m_value == 180.0 )
204 return -1.0;
205 else if( test.m_value == 90.0 || test.m_value == 270.0 )
206 return 0.0;
207 else if( test.m_value == 45.0 || test.m_value == 315.0 )
208 return M_SQRT1_2; // sqrt(2)/2
209 else if( test.m_value == 135.0 || test.m_value == 225.0 )
210 return -M_SQRT1_2;
211 else
212 return cos( AsRadians() );
213 }
214
215 double Tan() const { return tan( AsRadians() ); }
216
217 static EDA_ANGLE Arccos( double x ) { return EDA_ANGLE( acos( x ), RADIANS_T ); }
218
219 static EDA_ANGLE Arcsin( double x ) { return EDA_ANGLE( asin( x ), RADIANS_T ); }
220
221 static EDA_ANGLE Arctan( double x ) { return EDA_ANGLE( atan( x ), RADIANS_T ); }
222
223 static EDA_ANGLE Arctan2( double y, double x )
224 {
225 return EDA_ANGLE( atan2( y, x ), RADIANS_T );
226 }
227
229 {
230 m_value = foldToTwoPeriods( m_value, 360.0 );
231
232 while( m_value < -0.0 )
233 m_value += 360.0;
234
235 while( m_value >= 360.0 )
236 m_value -= 360.0;
237
238 return *this;
239 }
240
242 {
243 EDA_ANGLE ret( *this );
244 return ret.Normalize();
245 }
246
248 {
249 m_value = foldToTwoPeriods( m_value, 360.0 );
250
251 while( m_value <= -360.0 )
252 m_value += 360.0;
253
254 while( m_value > 0.0 )
255 m_value -= 360.0;
256
257 return *this;
258 }
259
261 {
262 m_value = foldToTwoPeriods( m_value, 180.0 );
263
264 while( m_value < -90.0 )
265 m_value += 180.0;
266
267 while( m_value > 90.0 )
268 m_value -= 180.0;
269
270 return *this;
271 }
272
274 {
275 m_value = foldToTwoPeriods( m_value, 360.0 );
276
277 while( m_value <= -180.0 )
278 m_value += 360.0;
279
280 while( m_value > 180.0 )
281 m_value -= 360.0;
282
283 return *this;
284 }
285
287 {
288 m_value = foldToTwoPeriods( m_value, 360.0 );
289
290 while( m_value < -360.0 )
291 m_value += 360.0;
292
293 while( m_value >= 360.0 )
294 m_value -= 360.0;
295
296 return *this;
297 }
298
300 EDA_ANGLE Snapped( const EDA_ANGLE& aStep ) const
301 {
302 double step = aStep.AsDegrees();
303
304 if( step <= 0.0 )
305 return *this;
306
307 return EDA_ANGLE( step * KiROUND( AsDegrees() / step ), DEGREES_T );
308 }
309
310 EDA_ANGLE KeepUpright() const;
311
331 [[nodiscard]] EDA_ANGLE Reflect( const EDA_ANGLE& aAxisAngle ) const
332 {
333 return EDA_ANGLE( 2.0 * aAxisAngle.AsDegrees() - AsDegrees(), DEGREES_T );
334 }
335
336 [[nodiscard]] EDA_ANGLE Round( int digits ) const
337 {
338 EDA_ANGLE angle( *this );
339 double rounded = KiROUND( angle.AsDegrees() * pow( 10.0, digits ) ) / pow( 10.0, digits );
340 angle = EDA_ANGLE( rounded, DEGREES_T );
341 return angle;
342 }
343
345 {
346 *this = EDA_ANGLE( AsDegrees() + aAngle.AsDegrees(), DEGREES_T );
347 return *this;
348 }
349
351 {
352 *this = EDA_ANGLE( AsDegrees() - aAngle.AsDegrees(), DEGREES_T );
353 return *this;
354 }
355
356private:
362 static double foldToTwoPeriods( double aValue, double aPeriod )
363 {
364 if( !std::isfinite( aValue ) )
365 return 0.0;
366
367 if( std::abs( aValue ) < 2.0 * aPeriod )
368 return aValue;
369
370 // Exact because |aValue| >= 2 * aPeriod keeps the remainder on a grid no finer than the sum
371 return std::fmod( aValue, aPeriod ) + std::copysign( aPeriod, aValue );
372 }
373
374 double m_value;
375
376};
377
378
379inline EDA_ANGLE operator-( const EDA_ANGLE& aAngle )
380{
381 return aAngle.Invert();
382}
383
384
385inline EDA_ANGLE operator-( const EDA_ANGLE& aAngleA, const EDA_ANGLE& aAngleB )
386{
387 return EDA_ANGLE( aAngleA.AsDegrees() - aAngleB.AsDegrees(), DEGREES_T );
388}
389
390
391inline EDA_ANGLE operator+( const EDA_ANGLE& aAngleA, const EDA_ANGLE& aAngleB )
392{
393 return EDA_ANGLE( aAngleA.AsDegrees() + aAngleB.AsDegrees(), DEGREES_T );
394}
395
396
397inline EDA_ANGLE operator*( const EDA_ANGLE& aAngleA, double aOperator )
398{
399 return EDA_ANGLE( aAngleA.AsDegrees() * aOperator, DEGREES_T );
400}
401
402
403inline EDA_ANGLE operator/( const EDA_ANGLE& aAngleA, double aOperator )
404{
405 return EDA_ANGLE( aAngleA.AsDegrees() / aOperator, DEGREES_T );
406}
407
408
409inline double operator/( const EDA_ANGLE& aAngleA, const EDA_ANGLE& aOperator )
410{
411 return aAngleA.AsDegrees() / aOperator.AsDegrees();
412}
413
414
415inline bool operator==( const EDA_ANGLE& aAngleA, const EDA_ANGLE& aAngleB )
416{
417 return aAngleA.AsDegrees() == aAngleB.AsDegrees();
418 //return equals( aAngleA.AsDegrees(), aAngleB.AsDegrees() );
419}
420
421
422inline bool operator!=( const EDA_ANGLE& aAngleA, const EDA_ANGLE& aAngleB )
423{
424 return aAngleA.AsDegrees() != aAngleB.AsDegrees();
425 // return !equals( aAngleA.AsDegrees(), aAngleB.AsDegrees() );
426}
427
428
429inline bool operator>( const EDA_ANGLE& aAngleA, const EDA_ANGLE& aAngleB )
430{
431 return aAngleA.AsDegrees() > aAngleB.AsDegrees();
432}
433
434
435inline bool operator<( const EDA_ANGLE& aAngleA, const EDA_ANGLE& aAngleB )
436{
437 return aAngleA.AsDegrees() < aAngleB.AsDegrees();
438}
439
440
441inline bool operator<=( const EDA_ANGLE& aAngleA, const EDA_ANGLE& aAngleB )
442{
443 return aAngleA.AsDegrees() <= aAngleB.AsDegrees();
444}
445
446
447inline bool operator>=( const EDA_ANGLE& aAngleA, const EDA_ANGLE& aAngleB )
448{
449 return aAngleA.AsDegrees() >= aAngleB.AsDegrees();
450}
451
452
453inline std::ostream& operator<<( std::ostream& aStream, const EDA_ANGLE& aAngle )
454{
455 return aStream << aAngle.AsDegrees();
456}
457
458
459namespace std
460{
461inline EDA_ANGLE abs( const EDA_ANGLE& aAngle )
462{
463 return EDA_ANGLE( std::abs( aAngle.AsDegrees() ), DEGREES_T );
464}
465}
466
467
468static constexpr EDA_ANGLE ANGLE_HORIZONTAL{ 0 };
469static constexpr EDA_ANGLE ANGLE_VERTICAL{ 90 };
470static constexpr EDA_ANGLE FULL_CIRCLE{ 360 };
471
472static constexpr EDA_ANGLE ANGLE_0{ 0 };
473static constexpr EDA_ANGLE ANGLE_45{ 45 };
474static constexpr EDA_ANGLE ANGLE_90{ 90 };
475static constexpr EDA_ANGLE ANGLE_135{ 135 };
476static constexpr EDA_ANGLE ANGLE_180{ 180 };
477static constexpr EDA_ANGLE ANGLE_270{ 270 };
478static constexpr EDA_ANGLE ANGLE_360{ 360 };
479
480
497{
498public:
499 EDA_ORIENTATION() = default;
500
501 explicit EDA_ORIENTATION( const EDA_ANGLE& aAngle ) { SetAngle( aAngle ); }
502
507 {
508 SetAngle( aAngle );
509 return *this;
510 }
511
512 bool operator==( const EDA_ORIENTATION& aOther ) const = default;
513
517 EDA_ORIENTATION operator+( const EDA_ANGLE& aAngle ) const
518 {
519 return EDA_ORIENTATION( m_angle + aAngle );
520 }
521
525 EDA_ORIENTATION operator-( const EDA_ANGLE& aAngle ) const
526 {
527 return EDA_ORIENTATION( m_angle - aAngle );
528 }
529
531 {
532 return EDA_ORIENTATION( -m_angle );
533 }
534
540 EDA_ANGLE operator-( const EDA_ORIENTATION& aOther ) const
541 {
542 return m_angle - aOther.m_angle;
543 }
544
546 {
547 SetAngle( m_angle + aAngle );
548 return *this;
549 }
550
552 {
553 SetAngle( m_angle - aAngle );
554 return *this;
555 }
556
557 EDA_ANGLE GetAngle() const { return m_angle; }
558
559 void SetAngle( const EDA_ANGLE& aAngle )
560 {
561 m_angle = aAngle;
562 m_angle.Normalize();
563
564 // Normalize -0 to 0 to avoid -0 in display and serialization.
565 if( m_angle == ANGLE_0 )
567 }
568
575 [[nodiscard]] EDA_ORIENTATION Reflect( const EDA_ANGLE& aAxisAngle ) const
576 {
577 return EDA_ORIENTATION( m_angle.Reflect( aAxisAngle ) );
578 }
579
580private:
582};
583
584
585// Free forms with the angle on the left, so the mirror-style formulas read as-written
586// (180 - orientation, -orientation).
587inline EDA_ORIENTATION operator+( const EDA_ANGLE& aAngle, const EDA_ORIENTATION& aOrientation )
588{
589 return aOrientation + aAngle;
590}
591
592
593inline EDA_ORIENTATION operator-( const EDA_ANGLE& aAngle, const EDA_ORIENTATION& aOrientation )
594{
595 return EDA_ORIENTATION( aAngle - aOrientation.GetAngle() );
596}
597
598
599inline std::ostream& operator<<( std::ostream& aStream, const EDA_ORIENTATION& aOrientation )
600{
601 // Follow EDA_ANGLE's convention of outputting angles in degrees.
602 return aStream << aOrientation.GetAngle().AsDegrees();
603}
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:985
EDA_ANGLE Normalize()
Definition eda_angle.h:228
EDA_ANGLE NormalizeNegative()
Definition eda_angle.h:247
double Sin() const
Definition eda_angle.h:177
EDA_ANGLE Normalize90()
Definition eda_angle.h:260
double Tan() const
Definition eda_angle.h:215
constexpr EDA_ANGLE(double aAngleInDegrees)
Construct an EDA_ANGLE in degrees.
Definition eda_angle.h:67
int AsTenthsOfADegree() const
Definition eda_angle.h:117
bool IsParallelTo(EDA_ANGLE aAngle) const
Definition eda_angle.h:153
double AsDegrees() const
Definition eda_angle.h:115
EDA_ANGLE Snapped(const EDA_ANGLE &aStep) const
Snap to the nearest integer multiple of aStep (returns *this if aStep <= 0).
Definition eda_angle.h:300
EDA_ANGLE Invert() const
Definition eda_angle.h:172
bool IsZero() const
Definition eda_angle.h:135
bool IsHorizontal() const
Definition eda_angle.h:141
bool IsCardinal() const
Definition eda_angle.cpp:40
EDA_ANGLE Normalize180()
Definition eda_angle.h:273
static EDA_ANGLE Arctan2(double y, double x)
Definition eda_angle.h:223
EDA_ANGLE Reflect(const EDA_ANGLE &aAxisAngle) const
Reflection, over a line with direction aAxisAngle, of the turn this angle stores.
Definition eda_angle.h:331
EDA_ANGLE KeepUpright() const
Definition eda_angle.cpp:23
EDA_ANGLE Normalize720()
Definition eda_angle.h:286
double AsRadians() const
Definition eda_angle.h:119
bool IsVertical() const
Definition eda_angle.h:147
EDA_ANGLE & operator-=(const EDA_ANGLE &aAngle)
Definition eda_angle.h:350
EDA_ANGLE Round(int digits) const
Definition eda_angle.h:336
EDA_ANGLE & operator+=(const EDA_ANGLE &aAngle)
Definition eda_angle.h:344
EDA_ANGLE(const VECTOR2D &aVector)
Definition eda_angle.h:71
EDA_ANGLE(double aValue, EDA_ANGLE_T aAngleType)
Angles can be created in degrees, 1/10ths of a degree, or radians, and read as any of the angle types...
Definition eda_angle.h:45
static double foldToTwoPeriods(double aValue, double aPeriod)
Exactly remove whole periods from values beyond two periods, keeping the sign, so the normalization l...
Definition eda_angle.h:362
static constexpr double DEGREES_TO_RADIANS
Definition eda_angle.h:121
bool IsCardinal90() const
Definition eda_angle.cpp:57
double Cos() const
Definition eda_angle.h:196
double m_value
value in degrees
Definition eda_angle.h:374
static EDA_ANGLE Arctan(double x)
Definition eda_angle.h:221
static EDA_ANGLE Arcsin(double x)
Definition eda_angle.h:219
EDA_ANGLE Normalized() const
Definition eda_angle.h:241
static EDA_ANGLE Arccos(double x)
Definition eda_angle.h:217
An "orientation", held as an angle constrained to the range [0, 360) degrees.
Definition eda_angle.h:497
EDA_ANGLE GetAngle() const
Definition eda_angle.h:557
EDA_ORIENTATION operator-() const
Definition eda_angle.h:530
EDA_ORIENTATION & operator+=(const EDA_ANGLE &aAngle)
Definition eda_angle.h:545
EDA_ORIENTATION(const EDA_ANGLE &aAngle)
Definition eda_angle.h:501
EDA_ORIENTATION operator-(const EDA_ANGLE &aAngle) const
Subtract an EDA_ANGLE from this orientation, wrapping around the [0, 360) range.
Definition eda_angle.h:525
void SetAngle(const EDA_ANGLE &aAngle)
Definition eda_angle.h:559
EDA_ANGLE operator-(const EDA_ORIENTATION &aOther) const
The difference of two orientations is a signed rotation amount, not an orientation.
Definition eda_angle.h:540
EDA_ORIENTATION Reflect(const EDA_ANGLE &aAxisAngle) const
This orientation reflected over a line with direction aAxisAngle.
Definition eda_angle.h:575
EDA_ANGLE m_angle
Definition eda_angle.h:581
bool operator==(const EDA_ORIENTATION &aOther) const =default
EDA_ORIENTATION()=default
EDA_ORIENTATION & operator=(const EDA_ANGLE &aAngle)
Assign a plain angle, normalising it.
Definition eda_angle.h:506
EDA_ORIENTATION operator+(const EDA_ANGLE &aAngle) const
Add an EDA_ANGLE to this orientation, wrapping around the [0, 360) range.
Definition eda_angle.h:517
EDA_ORIENTATION & operator-=(const EDA_ANGLE &aAngle)
Definition eda_angle.h:551
EDA_ANGLE operator/(const EDA_ANGLE &aAngleA, double aOperator)
Definition eda_angle.h:403
bool operator>(const EDA_ANGLE &aAngleA, const EDA_ANGLE &aAngleB)
Definition eda_angle.h:429
bool operator>=(const EDA_ANGLE &aAngleA, const EDA_ANGLE &aAngleB)
Definition eda_angle.h:447
EDA_ANGLE operator*(const EDA_ANGLE &aAngleA, double aOperator)
Definition eda_angle.h:397
static constexpr EDA_ANGLE ANGLE_0
Definition eda_angle.h:472
static constexpr EDA_ANGLE ANGLE_90
Definition eda_angle.h:474
bool operator<=(const EDA_ANGLE &aAngleA, const EDA_ANGLE &aAngleB)
Definition eda_angle.h:441
std::ostream & operator<<(std::ostream &aStream, const EDA_ANGLE &aAngle)
Definition eda_angle.h:453
EDA_ANGLE_T
Definition eda_angle.h:28
@ TENTHS_OF_A_DEGREE_T
Definition eda_angle.h:29
@ RADIANS_T
Definition eda_angle.h:31
@ DEGREES_T
Definition eda_angle.h:30
EDA_ANGLE operator-(const EDA_ANGLE &aAngle)
Definition eda_angle.h:379
bool operator<(const EDA_ANGLE &aAngleA, const EDA_ANGLE &aAngleB)
Definition eda_angle.h:435
static constexpr EDA_ANGLE ANGLE_VERTICAL
Definition eda_angle.h:469
bool operator==(const EDA_ANGLE &aAngleA, const EDA_ANGLE &aAngleB)
Definition eda_angle.h:415
static constexpr EDA_ANGLE ANGLE_HORIZONTAL
Definition eda_angle.h:468
static constexpr EDA_ANGLE ANGLE_45
Definition eda_angle.h:473
static constexpr EDA_ANGLE ANGLE_270
Definition eda_angle.h:477
static constexpr EDA_ANGLE FULL_CIRCLE
Definition eda_angle.h:470
static constexpr EDA_ANGLE ANGLE_360
Definition eda_angle.h:478
static constexpr EDA_ANGLE ANGLE_180
Definition eda_angle.h:476
EDA_ANGLE operator+(const EDA_ANGLE &aAngleA, const EDA_ANGLE &aAngleB)
Definition eda_angle.h:391
static constexpr EDA_ANGLE ANGLE_135
Definition eda_angle.h:475
bool operator!=(const EDA_ANGLE &aAngleA, const EDA_ANGLE &aAngleB)
Definition eda_angle.h:422
STL namespace.
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:461
bool equals(glm::mat< L, C, T, Q > const &aFirst, glm::mat< L, C, T, Q > const &aSecond, T aEpsilon=static_cast< T >(FLT_EPSILON *10))
Template to compare two glm::mat<T> values for equality within a required epsilon.
#define M_PI
VECTOR2< double > VECTOR2D
Definition vector2d.h:707