KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_eda_angle.cpp
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
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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <boost/test/tools/context.hpp>
21
22#include <cmath>
23#include <iomanip>
24
26
27#include <geometry/eda_angle.h>
28
29
30BOOST_AUTO_TEST_SUITE( EdaAngle )
31
32
44
45
46static const std::vector<EDA_ANGLE_NORMALISE_CASE> normalize_cases =
47{
48 //@todo: should we unify the ranges of Normalize180, Normalize720 to be the same
49 // as Normalize90 (i.e. inclusive of both sides of the range)?
50
51 // [0,360) (-360,0] [-90,90] (-180,180] [-360,360)
52 // Original Normalized NormNeg Norm90 Norm180 Norm720
53 { 90.0, 90.0, -270.0, 90.0, 90.0, 90.0 },
54 { -90.0, 270.0, -90.0, -90.0, -90.0, -90.0 },
55 { 135.0, 135.0, -225.0, -45.0, 135.0, 135.0 },
56 { -135.0, 225.0, -135.0, 45.0, -135.0, -135.0 },
57 { 180.0, 180.0, -180.0, 0.0, 180.0, 180.0 },
58 { -180.0, 180.0, -180.0, 0.0, 180.0, -180.0 },
59 { 360.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
60 { -360.0, 0.0, 0.0, 0.0, 0.0, -360.0 },
61 { 390.0, 30.0, -330.0, 30.0, 30.0, 30.0 },
62 { -390.0, 330.0, -30.0, -30.0, -30.0, -30.0 },
63 { 720.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
64 { -720.0, 0.0, 0.0, 0.0, 0.0, -360.0 },
65};
66
67
69{
70 for( const auto& c : normalize_cases )
71 {
72 BOOST_TEST_INFO_SCOPE( "Original angle: " << c.m_Angle << " degrees" );
73
74 EDA_ANGLE normalized( c.m_Angle, DEGREES_T );
75 normalized.Normalize();
76
77 EDA_ANGLE normalizedNegative( c.m_Angle, DEGREES_T );
78 normalizedNegative.NormalizeNegative();
79
80 EDA_ANGLE normalized90( c.m_Angle, DEGREES_T );
81 normalized90.Normalize90();
82
83 EDA_ANGLE normalized180( c.m_Angle, DEGREES_T );
84 normalized180.Normalize180();
85
86 EDA_ANGLE normalized720( c.m_Angle, DEGREES_T );
87 normalized720.Normalize720();
88
89 BOOST_CHECK_EQUAL( normalized.AsDegrees(), c.m_ExpNormalized );
90 BOOST_CHECK_EQUAL( normalizedNegative.AsDegrees(), c.m_ExpNormalizedNegative );
91 BOOST_CHECK_EQUAL( normalized90.AsDegrees(), c.m_ExpNormalized90 );
92 BOOST_CHECK_EQUAL( normalized180.AsDegrees(), c.m_ExpNormalized180 );
93 BOOST_CHECK_EQUAL( normalized720.AsDegrees(), c.m_ExpNormalized720 );
94 }
95}
96
97
98BOOST_AUTO_TEST_CASE( NormalizeHugeAndNonFinite )
99{
100 // The old loops leave NaN alone and never finish on inf or 1e19, so fail here before reaching those
101 BOOST_REQUIRE_EQUAL( EDA_ANGLE( NAN, DEGREES_T ).Normalize180().AsDegrees(), 0.0 );
102
103 // 1e19 is exact and 1e19 mod 360 = 280
104 // [0,360) (-360,0] [-90,90] (-180,180] [-360,360)
105 const std::vector<EDA_ANGLE_NORMALISE_CASE> cases = {
106 { 1e19, 280.0, -80.0, -80.0, -80.0, 280.0 },
107 { -1e19, 80.0, -280.0, 80.0, 80.0, -280.0 },
108 { INFINITY, 0.0, 0.0, 0.0, 0.0, 0.0 },
109 { -INFINITY, 0.0, 0.0, 0.0, 0.0, 0.0 },
110 { NAN, 0.0, 0.0, 0.0, 0.0, 0.0 },
111 };
112
113 for( const auto& c : cases )
114 {
115 BOOST_TEST_INFO_SCOPE( "Original angle: " << c.m_Angle << " degrees" );
116
117 BOOST_CHECK_EQUAL( EDA_ANGLE( c.m_Angle, DEGREES_T ).Normalize().AsDegrees(), c.m_ExpNormalized );
119 c.m_ExpNormalizedNegative );
120 BOOST_CHECK_EQUAL( EDA_ANGLE( c.m_Angle, DEGREES_T ).Normalize90().AsDegrees(), c.m_ExpNormalized90 );
121 BOOST_CHECK_EQUAL( EDA_ANGLE( c.m_Angle, DEGREES_T ).Normalize180().AsDegrees(), c.m_ExpNormalized180 );
122 BOOST_CHECK_EQUAL( EDA_ANGLE( c.m_Angle, DEGREES_T ).Normalize720().AsDegrees(), c.m_ExpNormalized720 );
123 }
124
125 // 9e18 is an exact multiple of 180
126 BOOST_CHECK( EDA_ANGLE( 9e18, DEGREES_T ).IsCardinal() );
127 BOOST_CHECK( !EDA_ANGLE( 9e18, DEGREES_T ).IsCardinal90() );
128
129 for( double bad : { INFINITY, -INFINITY } )
130 {
131 BOOST_CHECK( !EDA_ANGLE( bad, DEGREES_T ).IsCardinal() );
132 BOOST_CHECK( !EDA_ANGLE( bad, DEGREES_T ).IsCardinal90() );
133 }
134}
135
136
142static double refStep( double aValue, double aLow, bool aLowIncl, double aHigh, bool aHighIncl, double aPeriod )
143{
144 while( aLowIncl ? aValue < aLow : aValue <= aLow )
145 aValue += aPeriod;
146
147 while( aHighIncl ? aValue > aHigh : aValue >= aHigh )
148 aValue -= aPeriod;
149
150 return aValue;
151}
152
153
154static bool refIsCardinal( double aValue )
155{
156 while( aValue < 0.0 )
157 aValue += 90.0;
158
159 while( aValue >= 90.0 )
160 aValue -= 90.0;
161
162 return aValue == 0.0;
163}
164
165
166static bool refIsCardinal90( double aValue )
167{
168 aValue = std::abs( aValue );
169
170 while( aValue >= 180.0 )
171 aValue -= 180.0;
172
173 return aValue == 90.0;
174}
175
176
177static EDA_ANGLE deg( double d )
178{
179 return EDA_ANGLE( d, DEGREES_T );
180};
181
182
183static EDA_ORIENTATION orientation( double d )
184{
185 return EDA_ORIENTATION{ deg( d ) };
186};
187
188
189BOOST_AUTO_TEST_CASE( NormalizeMatchesSteppingLoops )
190{
191 std::vector<double> inputs;
192
193 for( int i = -40; i <= 40; ++i )
194 {
195 double base = 45.0 * i;
196 inputs.insert( inputs.end(), { base, std::nextafter( base, -INFINITY ), std::nextafter( base, INFINITY ),
197 base - 1e-20, base + 1e-20 } );
198 }
199
200 for( int i = -5000; i <= 5000; ++i )
201 inputs.push_back( i * 199.87654321 );
202
203 inputs.insert( inputs.end(), { 0.0, -0.0, 1e-300, -1e-300 } );
204
205 auto sameBits = []( double a, double b )
206 {
207 return a == b && std::signbit( a ) == std::signbit( b );
208 };
209
210 for( double x : inputs )
211 {
212 BOOST_TEST_INFO_SCOPE( "Original angle: " << std::setprecision( 17 ) << x << " degrees" );
213
214 BOOST_CHECK( sameBits( EDA_ANGLE( x, DEGREES_T ).Normalize().AsDegrees(),
215 refStep( x, 0.0, true, 360.0, false, 360.0 ) ) );
216 BOOST_CHECK( sameBits( EDA_ANGLE( x, DEGREES_T ).NormalizeNegative().AsDegrees(),
217 refStep( x, -360.0, false, 0.0, true, 360.0 ) ) );
218 BOOST_CHECK( sameBits( EDA_ANGLE( x, DEGREES_T ).Normalize90().AsDegrees(),
219 refStep( x, -90.0, true, 90.0, true, 180.0 ) ) );
220 BOOST_CHECK( sameBits( EDA_ANGLE( x, DEGREES_T ).Normalize180().AsDegrees(),
221 refStep( x, -180.0, false, 180.0, true, 360.0 ) ) );
222 BOOST_CHECK( sameBits( EDA_ANGLE( x, DEGREES_T ).Normalize720().AsDegrees(),
223 refStep( x, -360.0, true, 360.0, false, 360.0 ) ) );
224 BOOST_CHECK_EQUAL( EDA_ANGLE( x, DEGREES_T ).IsCardinal(), refIsCardinal( x ) );
225 BOOST_CHECK_EQUAL( EDA_ANGLE( x, DEGREES_T ).IsCardinal90(), refIsCardinal90( x ) );
226 }
227}
228
229
230BOOST_AUTO_TEST_CASE( ConstantAngles )
231{
232 BOOST_CHECK_EQUAL( ANGLE_0.AsDegrees(), 0.0 );
233 BOOST_CHECK_EQUAL( ANGLE_45.AsDegrees(), 45.0 );
234 BOOST_CHECK_EQUAL( ANGLE_90.AsDegrees(), 90.0 );
235 BOOST_CHECK_EQUAL( ANGLE_135.AsDegrees(), 135.0 );
236 BOOST_CHECK_EQUAL( ANGLE_180.AsDegrees(), 180.0 );
237 BOOST_CHECK_EQUAL( ANGLE_270.AsDegrees(), 270.0 );
238 BOOST_CHECK_EQUAL( ANGLE_360.AsDegrees(), 360.0 );
239
240 BOOST_CHECK_EQUAL( ANGLE_HORIZONTAL.AsDegrees(), 0.0 );
241 BOOST_CHECK_EQUAL( ANGLE_VERTICAL.AsDegrees(), 90.0 );
242 BOOST_CHECK_EQUAL( FULL_CIRCLE.AsDegrees(), 360.0 );
243}
244
245
247{
248 BOOST_CHECK_EQUAL( deg( 80 ).Snapped( ANGLE_90 ).AsDegrees(), 90.0 );
249 BOOST_CHECK_EQUAL( deg( 30 ).Snapped( ANGLE_90 ).AsDegrees(), 0.0 );
250 BOOST_CHECK_EQUAL( deg( 135 ).Snapped( ANGLE_90 ).AsDegrees(), 180.0 );
251 BOOST_CHECK_EQUAL( deg( 45 ).Snapped( ANGLE_90 ).AsDegrees(), 90.0 );
252 BOOST_CHECK_EQUAL( deg( -10 ).Snapped( ANGLE_90 ).AsDegrees(), 0.0 );
253 BOOST_CHECK_EQUAL( deg( 80 ).Snapped( ANGLE_45 ).AsDegrees(), 90.0 );
254 BOOST_CHECK_EQUAL( deg( 5 ).Snapped( ANGLE_0 ).AsDegrees(), 5.0 ); // step <= 0 guard
255
256 // Minimal equivalent rotation used by grid-aware placement: raw - raw.Snapped(period).
257 auto residual = [&]( double raw, const EDA_ANGLE& period )
258 {
259 EDA_ANGLE a = deg( raw );
260 return ( a - a.Snapped( period ) ).AsDegrees();
261 };
262
263 BOOST_CHECK_EQUAL( residual( 80, ANGLE_90 ), -10.0 );
264 BOOST_CHECK_EQUAL( residual( 30, ANGLE_90 ), 30.0 );
265 BOOST_CHECK_EQUAL( residual( 135, ANGLE_90 ), -45.0 );
266 BOOST_CHECK_EQUAL( residual( 0, ANGLE_90 ), 0.0 );
267}
268
269
270BOOST_AUTO_TEST_CASE( AngleReflect )
271{
272 const auto checkReflect = []( double aAngle, double aAxis, double aExpected )
273 {
274 BOOST_TEST_CONTEXT( "Testing reflect( " << aAngle << " about " << aAxis << " ) -> " << aExpected )
275 {
276 const EDA_ANGLE angle = deg( aAngle );
277 const EDA_ANGLE axis = deg( aAxis );
278
279 const EDA_ANGLE reflected = angle.Reflect( axis );
280 BOOST_CHECK_EQUAL( reflected.Normalized().AsDegrees(), aExpected );
281
282 // And check the reverse returns
283 const EDA_ANGLE reversed = reflected.Reflect( axis );
284
285 BOOST_CHECK_EQUAL( reversed.Normalized().AsDegrees(), angle.Normalized().AsDegrees() );
286
287 // And check that and axis reversal has no effect on the reflection result.
288 const EDA_ANGLE reversedAxis = axis + deg( 180 );
289 const EDA_ANGLE reflectedWithReversedAxis = angle.Reflect( reversedAxis );
290 BOOST_CHECK_EQUAL( reflectedWithReversedAxis.Normalized().AsDegrees(), aExpected );
291 }
292 };
293
294
295 // ^
296 // | A = 90
297 // ------
298 // | A' = -90
299 // v
300 checkReflect( 90, 0, 270.0 );
301
302 // |
303 // / | \
304 // A | A'
305 checkReflect( 45, 90, 135.0 );
306
307 // | A /
308 // v /
309 // /
310 // / <---- A'
311 //
312 // Note: reflecting in a non-cardinal axis is the only time a sign error manifests, so this
313 // one is probably the most critical test.
314 checkReflect( -90, 45, 180.0 );
315
316 // / /
317 // v / /
318 // / v
319 checkReflect( 45, 45, 45 );
320}
321
322
324{
325 // Construction normalizes into [0, 360), and equality compares canonical values.
326 BOOST_CHECK_EQUAL( orientation( 370 ).GetAngle().AsDegrees(), 10.0 );
327 BOOST_CHECK_EQUAL( orientation( -90 ).GetAngle().AsDegrees(), 270.0 );
328 BOOST_CHECK( orientation( 370 ) == orientation( 10 ) );
329
330 // Addition and subtraction wrap in every form.
331 EDA_ORIENTATION turn = orientation( 350 );
332 BOOST_CHECK_EQUAL( ( turn + deg( 30 ) ).GetAngle().AsDegrees(), 20.0 );
333 BOOST_CHECK_EQUAL( ( deg( 30 ) + turn ).GetAngle().AsDegrees(), 20.0 );
334 BOOST_CHECK_EQUAL( ( turn += deg( 30 ) ).GetAngle().AsDegrees(), 20.0 );
335 BOOST_CHECK_EQUAL( ( turn -= deg( 30 ) ).GetAngle().AsDegrees(), 350.0 );
336
337 // Negation and subtraction
338 BOOST_CHECK_EQUAL( ( -orientation( 45 ) ).GetAngle().AsDegrees(), 315.0 );
339 // Wrapping subtraction
340 BOOST_CHECK_EQUAL( ( ANGLE_180 - orientation( 270 ) ).GetAngle().AsDegrees(), 270.0 );
341 // Orientation minus angle, with wrapping
342 BOOST_CHECK_EQUAL( ( orientation( 10 ) - deg( 350 ) ).GetAngle().AsDegrees(), 20.0 );
343
344 // A difference of orientations -> signed rotation amount (EDA_ANGLE, not EDA_ORIENTATION)
345 EDA_ANGLE diff = orientation( 10 ) - orientation( 350 );
346 BOOST_CHECK_EQUAL( diff.AsDegrees(), -340.0 );
347
348 // -0 becomes 0
349 BOOST_CHECK( !std::signbit( ( -orientation( 0 ) ).GetAngle().AsDegrees() ) );
350}
351
352
353BOOST_AUTO_TEST_CASE( OrientationReflect )
354{
355 // Reflecting an orientation is just reflecting its angle over the given axis.
356 BOOST_CHECK_EQUAL( orientation( 30 ).Reflect( deg( 90 ) ).GetAngle().AsDegrees(), 150.0 );
357 BOOST_CHECK_EQUAL( orientation( 0 ).Reflect( deg( 45 ) ).GetAngle().AsDegrees(), 90.0 );
358}
359
static std::optional< VECTOR2D > normalized(const VECTOR2D &aVec)
EDA_ANGLE Normalize()
Definition eda_angle.h:228
EDA_ANGLE NormalizeNegative()
Definition eda_angle.h:247
EDA_ANGLE Normalize90()
Definition eda_angle.h:260
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 Normalize180()
Definition eda_angle.h:273
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 Normalize720()
Definition eda_angle.h:286
EDA_ANGLE Normalized() const
Definition eda_angle.h:241
An "orientation", held as an angle constrained to the range [0, 360) degrees.
Definition eda_angle.h:497
static constexpr EDA_ANGLE ANGLE_0
Definition eda_angle.h:472
static constexpr EDA_ANGLE ANGLE_90
Definition eda_angle.h:474
@ DEGREES_T
Definition eda_angle.h:30
static constexpr EDA_ANGLE ANGLE_VERTICAL
Definition eda_angle.h:469
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
static constexpr EDA_ANGLE ANGLE_135
Definition eda_angle.h:475
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:461
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_REQUIRE_EQUAL(rows.size(), 1)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
static double refStep(double aValue, double aLow, bool aLowIncl, double aHigh, bool aHighIncl, double aPeriod)
Reference copy of the stepping loops that normalization used before exact folding.
static bool refIsCardinal(double aValue)
static EDA_ORIENTATION orientation(double d)
BOOST_AUTO_TEST_CASE(Normalize)
static EDA_ANGLE deg(double d)
static const std::vector< EDA_ANGLE_NORMALISE_CASE > normalize_cases
static bool refIsCardinal90(double aValue)
BOOST_AUTO_TEST_SUITE_END()
BOOST_TEST_CONTEXT("Test Clearance")