KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_ellipse_to_bezier.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 (C) 2022 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 along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
21#include <geometry/ellipse.h>
22#include <bezier_curves.h>
23
24#include <fmt.h>
25
26BOOST_AUTO_TEST_SUITE( EllipseToBezier )
27
28
29
30const double MAX_ERROR = 0.01;
31
32
34{
35 std::string name;
37 std::vector<BEZIER<double>> expected;
38};
39
40
41BOOST_AUTO_TEST_CASE( EllipseToBezier )
42{
43 // This data mut be created "on the fly" to avoid a CTOR odres issue, because it uses
44 // static data like ANGLE_0 and it must be created **after** ANGLE_0 CTOR is called
45 // clang-format off
46 static const std::vector<ELLIPSE_TO_BEZIER_CASE> cases = {
47 {
48 "full circle",
49 { { 0, 0 }, { 100, 0 }, 1.0, ANGLE_0, FULL_CIRCLE },
50 { { { 100, 0 }, { 100, -55.22847498307934 }, { 55.228474983079344, -100 }, { 0, -100 } },
51 { { 0, -100 }, { -55.22847498307934, -100 }, { -100, -55.228474983079344 }, { -100, 0 } },
52 { { -100, 0 }, { -100, 55.22847498307934 }, { -55.228474983079344, 100 }, { 0, 100 } },
53 { { 0, 100 }, { 55.22847498307934, 100 }, { 100, 55.228474983079344 }, { 100, 0 } }
54 }
55 },
56 {
57 "ellipse",
58 { { 0, 0 }, { -100, 0 }, 0.5, ANGLE_0, FULL_CIRCLE },
59 { { { -100, 0 }, { -100, 27.61423749153967 }, { -55.228474983079344, 50 }, { 0, 50 } },
60 { { 0, 50 }, { 55.22847498307934, 50 }, { 100, 27.614237491539672 }, { 100, 0 } },
61 { { 100, 0 }, { 100, -27.61423749153967 }, { 55.228474983079344, -50 }, { 0, -50 } },
62 { { 0, -50 }, { -55.22847498307934, -50 }, { -100, -27.614237491539672 }, { -100, 0 } }
63 }
64 },
65 {
66 "arc1",
67 { { 0, 0 }, { 100, 0 }, 0.5, ANGLE_180, FULL_CIRCLE },
68 { { { -100, 0 }, { -100, 27.61423749153967 }, { -55.228474983079344, 50 }, { 0, 50 } },
69 { { 0, 50 }, { 55.22847498307934, 50 }, { 100, 27.614237491539672 }, { 100, 0 } }
70 }
71 },
72 {
73 "arc2",
74 { { 223, 165 }, { 372, 634 }, 0.96, EDA_ANGLE( 4.437, RADIANS_T ), EDA_ANGLE( 0.401, RADIANS_T ) },
75 { { { -463.86, 336.27 }, { -389.81, 608.33 }, { -170.75, 818.49 }, { 99.52, 876.73 } },
76 { { 99.52, 876.73 }, { 369.80, 934.98 }, { 643.36, 831.0 }, { 803.07, 609.31 } }
77 }
78 },
79 {
80 "arc3",
81 { { 112.75, 490.24 }, { 304.54, 129.16 }, 7.14, EDA_ANGLE( 1.90, RADIANS_T ), EDA_ANGLE( 1.09, RADIANS_T ) },
82 { { { 886.98, -1609.17 }, { 608.61, -1333.45 }, { 110.16, -394.92 }, { -305.88, 636.89 } },
83 { { -305.88, 636.89 }, { -721.93, 1668.69 }, { -940.88, 2509.32 }, { -829.87, 2648.63 } },
84 { { -829.87, 2648.63 }, { -718.85, 2787.95 }, { -308.47, 2187.55 }, { 152.23, 1211.78 } },
85 { { 152.23, 1211.78 }, { 612.93, 236.01 }, { 996.95, -846.11 }, { 1071.24, -1377.92 } }
86 }
87 },
88 };
89 // clang-format on
90
91 for( const ELLIPSE_TO_BEZIER_CASE& c : cases )
92 {
93 BOOST_TEST_CONTEXT( c.name )
94 {
95 std::vector<BEZIER<double>> out;
96 TransformEllipseToBeziers<double>( c.input, out );
97
98 BOOST_CHECK_EQUAL( c.expected.size(), out.size() );
99
100#if 0
101 for( BEZIER<double>& b : out )
102 {
103 BOOST_TEST_MESSAGE( fmt::format( "{{ {{ {}, {} }}, {{ {}, {} }}, {{ {}, {} }}, {{ {}, {} }} }}",
104 b.Start.x, b.Start.y, b.C1.x, b.C1.y,
105 b.C2.x, b.C2.y, b.End.x, b.End.y ) );
106 }
107#endif
108
109 for( size_t i = 0; i < out.size(); i++ )
110 {
111 BOOST_CHECK_LE( ( c.expected[i].Start - out[i].Start ).EuclideanNorm(),
112 MAX_ERROR );
113 BOOST_CHECK_LE( ( c.expected[i].C1 - out[i].C1 ).EuclideanNorm(),
114 MAX_ERROR );
115 BOOST_CHECK_LE( ( c.expected[i].C2 - out[i].C2 ).EuclideanNorm(),
116 MAX_ERROR );
117 BOOST_CHECK_LE( ( c.expected[i].End - out[i].End ).EuclideanNorm(),
118 MAX_ERROR );
119 }
120 }
121 }
122}
123
124BOOST_AUTO_TEST_SUITE_END()
Generic cubic Bezier representation.
Definition: bezier_curves.h:78
This class was created to handle importing ellipses from other file formats that support them nativel...
Definition: ellipse.h:34
static constexpr EDA_ANGLE ANGLE_0
Definition: eda_angle.h:435
@ RADIANS_T
Definition: eda_angle.h:32
static constexpr EDA_ANGLE FULL_CIRCLE
Definition: eda_angle.h:433
static constexpr EDA_ANGLE ANGLE_180
Definition: eda_angle.h:439
std::vector< BEZIER< double > > expected
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
const double MAX_ERROR
Allows for rounding in the testcases.
BOOST_AUTO_TEST_CASE(EllipseToBezier)
#define BOOST_TEST_CONTEXT(A)