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
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
61{
63 std::vector<TYPED_POINT2I> m_expected_points;
64};
65
67{
68 const auto sort_vectors_x_then_y = []( const VECTOR2I& a, const VECTOR2I& b ) {
69 return LexicographicalCompare<VECTOR2I::coord_type>( a, b ) > 0;
70 };
71
72 std::vector<TYPED_POINT2I> expected_points = testcase.m_expected_points;
73
74 std::vector<TYPED_POINT2I> actual_points =
76
77 CHECK_COLLECTIONS_SAME_UNORDERED( expected_points, actual_points );
78}
79
80BOOST_AUTO_TEST_CASE( SimpleOvalVertical )
81{
82 const OVAL_POINTS_TEST_CASE testcase
83 {
84 {
85 SEG{ { 0, -1000 }, { 0, 1000 } },
86 1000,
87 },
88 {
89 { { 0, 0 }, PT_CENTER },
90 // Main points
91 { { 0, 1500 }, PT_QUADRANT },
92 { { 0, -1500 }, PT_QUADRANT },
93 { { 500, 0 }, PT_MID },
94 { { -500, 0 }, PT_MID },
95 // Cap centres
96 { { 0, 1000 }, PT_CENTER },
97 { { 0, -1000 }, PT_CENTER },
98 // Side segment ends
99 { { 500, 1000 }, PT_END },
100 { { 500, -1000 }, PT_END },
101 { { -500, 1000 }, PT_END },
102 { { -500, -1000 }, PT_END },
103 // No quadrants
104 },
105 };
106
107 DoOvalPointTestChecks( testcase );
108}
109
110BOOST_AUTO_TEST_CASE( SimpleOvalHorizontal )
111{
112 const OVAL_POINTS_TEST_CASE testcase
113 {
114 {
115 SEG{ { -1000, 0 }, { 1000, 0 } },
116 1000,
117 },
118 {
119 { { 0, 0 }, PT_CENTER },
120 // Main points
121 { { 0, 500 }, PT_MID },
122 { { 0, -500 }, PT_MID },
123 { { 1500, 0 }, PT_QUADRANT },
124 { { -1500, 0 }, PT_QUADRANT },
125 // Cap centres
126 { { 1000, 0 }, PT_CENTER },
127 { { -1000, 0 }, PT_CENTER },
128 // Side segment ends
129 { { 1000, 500 }, PT_END },
130 { { 1000, -500 }, PT_END },
131 { { -1000, 500 }, PT_END },
132 { { -1000, -500 }, PT_END },
133 // No quadrants
134 },
135 };
136
137 DoOvalPointTestChecks( testcase );
138}
139
140BOOST_AUTO_TEST_CASE( SimpleOval45Degrees )
141{
142 // In this case, it's useful to keep in mind the hypotenuse of
143 // isoceles right-angled triangles is sqrt(2) times the length of the sides
144 // 500 / sqrt(2) = 354
145 // 1000 / sqrt(2) = 707
146 // 1500 / sqrt(2) = 1061
147 // 2000 / sqrt(2) = 1414
148
149 const OVAL_POINTS_TEST_CASE testcase
150 {
151 {
152 SEG{ GetRotated( { -1500, 0 }, ANGLE_45 ), GetRotated( { 1500, 0 }, ANGLE_45 ) },
153 1000,
154 },
155 {
156 { { 0, 0 }, PT_CENTER },
157 // Main points
158 { { 1414, -1414 }, PT_END },
159 { { -1414, 1414 }, PT_END },
160 { { 354, 354 }, PT_MID },
161 { { -354, -354 }, PT_MID },
162 // Side segment ends
163 { { -1414, 707 }, PT_END },
164 { { 1414, -707 }, PT_END },
165 { { -707, 1414 }, PT_END },
166 { { 707, -1414 }, PT_END },
167 // Cap centres
168 { { 1061, -1061 }, PT_CENTER },
169 { { -1061, 1061 }, PT_CENTER },
170 // Extremum points (always one of NSEW of a cap centre because 45 degrees)
171 { { -1061 - 500, 1061 }, PT_QUADRANT },
172 { { -1061, 1061 + 500 }, PT_QUADRANT },
173 { { 1061 + 500, -1061 }, PT_QUADRANT },
174 { { 1061, -1061 - 500 }, PT_QUADRANT },
175 },
176 };
177
178 DoOvalPointTestChecks( testcase );
179}
180
Class that represents an oval shape (rectangle with semicircular end caps)
Definition: oval.h:45
Definition: seg.h:42
static constexpr EDA_ANGLE ANGLE_45
Definition: eda_angle.h:402
std::vector< TYPED_POINT2I > GetOvalKeyPoints(const OVAL &aOval, OVAL_KEY_POINT_FLAGS aFlags)
Get a list of interesting points on an oval (rectangle with semicircular end caps)
Definition: oval.cpp:84
@ OVAL_ALL_KEY_POINTS
Definition: oval.h:118
@ PT_CENTER
The point is the center of something.
Definition: point_types.h:46
@ PT_QUADRANT
The point is on a quadrant of a circle (N, E, S, W points).
Definition: point_types.h:58
@ PT_END
The point is at the end of a segment, arc, etc.
Definition: point_types.h:50
@ PT_MID
The point is at the middle of a segment, arc, etc.
Definition: point_types.h:54
std::vector< TYPED_POINT2I > m_expected_points
Definition: test_oval.cpp:63
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
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:80
void DoOvalPointTestChecks(const OVAL_POINTS_TEST_CASE &testcase)
Definition: test_oval.cpp:66
VECTOR2I GetRotated(const VECTOR2I &aVector, const EDA_ANGLE &aAngle)
Return a new VECTOR2I that is the result of rotating aVector by aAngle.
Definition: trigo.h:77