KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_chamfer.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) 2018-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
27
28#include "geom_test_utils.h"
29
31{
32};
33
37BOOST_FIXTURE_TEST_SUITE( Chamfer, ChamferFixture )
38
40{
44 std::optional<CHAMFER_RESULT> m_expected_result;
45};
46
47static void DoChamferTestChecks( const TWO_LINE_CHAMFER_TEST_CASE& aTestCase )
48{
49 // Actally do the chamfer
50 const std::optional<CHAMFER_RESULT> chamfer_result = ComputeChamferPoints( aTestCase.m_seg_a,
51 aTestCase.m_seg_b,
52 aTestCase.m_params );
53
54 BOOST_REQUIRE_EQUAL( chamfer_result.has_value(), aTestCase.m_expected_result.has_value() );
55
56 if( chamfer_result.has_value() )
57 {
58 const CHAMFER_RESULT& expected_result = aTestCase.m_expected_result.value();
59 const CHAMFER_RESULT& actual_result = chamfer_result.value();
60
61 BOOST_CHECK_PREDICATE( GEOM_TEST::SegmentsHaveSameEndPoints,
62 ( actual_result.m_chamfer )( expected_result.m_chamfer ) );
63
64 const auto check_updated_seg =
65 [&]( const std::optional<SEG>& updated_seg, const std::optional<SEG>& expected_seg )
66 {
67 BOOST_REQUIRE_EQUAL( updated_seg.has_value(), expected_seg.has_value() );
68
69 if( updated_seg.has_value() )
70 {
71 BOOST_CHECK_PREDICATE( GEOM_TEST::SegmentsHaveSameEndPoints,
72 ( *updated_seg )( *expected_seg ) );
73 }
74 };
75
76 check_updated_seg( actual_result.m_updated_seg_a, expected_result.m_updated_seg_a );
77 check_updated_seg( actual_result.m_updated_seg_b, expected_result.m_updated_seg_b );
78 }
79}
80
81BOOST_AUTO_TEST_CASE( SimpleChamferAtOrigin )
82{
83 /* 10
84 * 0,0 +----+-------------> 1000
85 * | /
86 * | /
87 * 10 +
88 * |
89 * v 1000
90 * */
91
92 const TWO_LINE_CHAMFER_TEST_CASE testcase
93 {
94 { VECTOR2I( 0, 0 ), VECTOR2I( 1000, 0 ) },
95 { VECTOR2I( 0, 0 ), VECTOR2I( 0, 1000 ) },
96 { 10, 10 },
97 { {
98 SEG( VECTOR2I( 10, 0 ), VECTOR2I( 0, 10 ) ), // chamfer
99 { SEG( VECTOR2I( 10, 0 ), VECTOR2I( 1000, 0 ) ) }, // rest of the line A
100 { SEG( VECTOR2I( 0, 10 ), VECTOR2I( 0, 1000 ) ) }, // rest of the line B
101 } },
102 };
103
104 DoChamferTestChecks( testcase );
105}
106
107BOOST_AUTO_TEST_CASE( SimpleChamferNotAtOrigin )
108{
109 // Same as above but the intersection is not at the origin
110 const TWO_LINE_CHAMFER_TEST_CASE testcase
111 {
112 { VECTOR2I( 1000, 1000 ), VECTOR2I( 2000, 1000 ) },
113 { VECTOR2I( 1000, 1000 ), VECTOR2I( 1000, 2000 ) },
114 { 10, 10 },
115 { {
116 SEG( VECTOR2I( 1010, 1000 ), VECTOR2I( 1000, 1010 ) ),
117 { SEG( VECTOR2I( 1010, 1000 ), VECTOR2I( 2000, 1000 ) ) },
118 { SEG( VECTOR2I( 1000, 1010 ), VECTOR2I( 1000, 2000 ) ) },
119 } },
120 };
121
122 DoChamferTestChecks( testcase );
123}
124
125BOOST_AUTO_TEST_CASE( AsymmetricChamfer )
126{
127 // Same as above but the intersection is not at the origin
128 const TWO_LINE_CHAMFER_TEST_CASE testcase
129 {
130 { VECTOR2I( 0, 0 ), VECTOR2I( 1000, 0 ) },
131 { VECTOR2I( 0, 0 ), VECTOR2I( 0, 1000 ) },
132 { 10, 100 },
133 { {
134 SEG( VECTOR2I( 10, 0 ), VECTOR2I( 0, 100 ) ), // chamfer
135 { SEG( VECTOR2I( 10, 0 ), VECTOR2I( 1000, 0 ) ) }, // rest of the line A
136 { SEG( VECTOR2I( 0, 100 ), VECTOR2I( 0, 1000 ) ) }, // rest of the line B
137 } },
138 };
139
140 DoChamferTestChecks( testcase );
141}
142
143BOOST_AUTO_TEST_CASE( ChamferFullLength )
144{
145 // Chamfer consumes the entire length of a line
146 const TWO_LINE_CHAMFER_TEST_CASE testcase
147 {
148 { VECTOR2I( 0, 0 ), VECTOR2I( 1000, 0 ) },
149 { VECTOR2I( 0, 0 ), VECTOR2I( 0, 100 ) },
150 { 100, 100 },
151 { {
152 SEG( VECTOR2I( 100, 0 ), VECTOR2I( 0, 100 ) ), // chamfer
153 { SEG( VECTOR2I( 100, 0 ), VECTOR2I( 1000, 0 ) ) }, // rest of the line A
154 std::nullopt, // line b no longer exists
155 } },
156 };
157
158 DoChamferTestChecks( testcase );
159}
160
161BOOST_AUTO_TEST_CASE( ChamferOverFullLength )
162{
163 // Chamfer consumes the entire length of a line
164 const TWO_LINE_CHAMFER_TEST_CASE testcase
165 {
166 { VECTOR2I( 0, 0 ), VECTOR2I( 1000, 0 ) },
167 { VECTOR2I( 0, 0 ), VECTOR2I( 0, 100 ) },
168 { 150, 150 }, // > 100
169 std::nullopt,
170 };
171
172 DoChamferTestChecks( testcase );
173}
174
Definition: seg.h:42
std::optional< CHAMFER_RESULT > ComputeChamferPoints(const SEG &aSegA, const SEG &aSegB, const CHAMFER_PARAMS &aChamferParams)
Compute the chamfer points for a given line pair and chamfer parameters.
bool SegmentsHaveSameEndPoints(const SEG &aSeg1, const SEG &aSeg2)
Check that two SEGs have the same end points, in either order.
Parameters that define a simple chamfer operation.
std::optional< SEG > m_updated_seg_a
std::optional< SEG > m_updated_seg_b
Declares the FilletFixture struct as the boost test fixture.
std::optional< CHAMFER_RESULT > m_expected_result
static void DoChamferTestChecks(const TWO_LINE_CHAMFER_TEST_CASE &aTestCase)
BOOST_AUTO_TEST_CASE(SimpleChamferAtOrigin)
BOOST_AUTO_TEST_SUITE_END()
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691