KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_oval.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) 2023 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#include <boost/test/unit_test.hpp>
25
26#include <geometry/oval.h>
27
28#include "geom_test_utils.h"
29
40template <typename T>
41void CHECK_COLLECTIONS_SAME_UNORDERED(const T& expected, const T& actual) {
42
43 for( const auto& p : expected )
44 {
45 BOOST_CHECK_MESSAGE( std::find( actual.begin(), actual.end(), p ) != actual.end(),
46 "Expected item not found: " << p );
47 }
48
49 for( const auto& p : actual )
50 {
51 BOOST_CHECK_MESSAGE( std::find( expected.begin(), expected.end(), p ) != expected.end(),
52 "Unexpected item: " << p );
53 }
54
55 BOOST_CHECK_EQUAL( expected.size(), actual.size() );
56}
57
59{
60};
61
62BOOST_FIXTURE_TEST_SUITE( Oval, OvalFixture )
63
65{
68 std::vector<VECTOR2I> m_expected_points;
69};
70
72{
73 const auto sort_vectors_x_then_y = []( const VECTOR2I& a, const VECTOR2I& b ) {
74 return LexicographicalCompare<VECTOR2I::coord_type>( a, b ) > 0;
75 };
76
77 std::vector<VECTOR2I> expected_points = testcase.m_expected_points;
78 std::vector<VECTOR2I> actual_points =
80
81 CHECK_COLLECTIONS_SAME_UNORDERED( expected_points, actual_points );
82}
83
84BOOST_AUTO_TEST_CASE( SimpleOvalVertical )
85{
86 const OVAL_POINTS_TEST_CASE testcase
87 {
88 { 1000, 3000 },
89 { 0, DEGREES_T },
90 {
91 { 0, 0 },
92 // Main points
93 { 0, 1500 },
94 { 0, -1500 },
95 { 500, 0 },
96 { -500, 0 },
97 // Cap centres
98 { 0, 1000 },
99 { 0, -1000 },
100 // Side segment ends
101 { 500, 1000 },
102 { 500, -1000 },
103 { -500, 1000 },
104 { -500, -1000 },
105 },
106 };
107
108 DoOvalPointTestChecks( testcase );
109}
110
111BOOST_AUTO_TEST_CASE( SimpleOvalHorizontal )
112{
113 const OVAL_POINTS_TEST_CASE testcase
114 {
115 { 3000, 1000 },
116 { 0, DEGREES_T },
117 {
118 { 0, 0 },
119 // Main points
120 { 0, 500 },
121 { 0, -500 },
122 { 1500, 0 },
123 { -1500, 0 },
124 // Cap centres
125 { 1000, 0 },
126 { -1000, 0 },
127 // Side segment ends
128 { 1000, 500 },
129 { 1000, -500 },
130 { -1000, 500 },
131 { -1000, -500 },
132 },
133 };
134
135 DoOvalPointTestChecks( testcase );
136}
137
138BOOST_AUTO_TEST_CASE( SimpleOval45Degrees )
139{
140 // In this case, it's useful to keep in mind the hypotenuse of
141 // isoceles right-angled triangles is sqrt(2) times the length of the sides
142 // 500 / sqrt(2) = 354
143 // 1000 / sqrt(2) = 707
144 // 1500 / sqrt(2) = 1061
145 // 2000 / sqrt(2) = 1414
146
147 const OVAL_POINTS_TEST_CASE testcase
148 {
149 { 4000, 1000 },
150 { 45, DEGREES_T },
151 {
152 { 0, 0 },
153 // Main points
154 { 1414, -1414 },
155 { -1414, 1414 },
156 { 354, 354 },
157 { -354, -354 },
158 // Side segment ends
159 { -1414, 707 },
160 { 1414, -707 },
161 { -707, 1414 },
162 { 707, -1414 },
163 // Cap centres
164 { 1061, -1061 },
165 { -1061, 1061 },
166 // Extremum points (always one of NSEW of a cap centre because 45 degrees)
167 { -1061 - 500, 1061 },
168 { -1061, 1061 + 500 },
169 { 1061 + 500, -1061 },
170 { 1061, -1061 - 500 },
171 },
172 };
173
174 DoOvalPointTestChecks( testcase );
175}
176
177BOOST_AUTO_TEST_SUITE_END()
@ DEGREES_T
Definition: eda_angle.h:31
@ OVAL_ALL_KEY_POINTS
Definition: oval.h:40
std::vector< VECTOR2I > GetOvalKeyPoints(const VECTOR2I &aOvalSize, const EDA_ANGLE &aRotation, OVAL_KEY_POINT_FLAGS aFlags)
Get a list of interesting points on an oval (rectangle with semicircular end caps)
Definition: oval.cpp:31
std::vector< VECTOR2I > m_expected_points
Definition: test_oval.cpp:68
VECTOR3I expected(15, 30, 45)
void CHECK_COLLECTIONS_SAME_UNORDERED(const T &expected, const T &actual)
Check that two collections contain the same elements, ignoring order.
Definition: test_oval.cpp:41
BOOST_AUTO_TEST_CASE(SimpleOvalVertical)
Definition: test_oval.cpp:84
void DoOvalPointTestChecks(const OVAL_POINTS_TEST_CASE &testcase)
Definition: test_oval.cpp:71