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 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/unit_test.hpp>
21
23
24#include "geom_test_utils.h"
25
27{
28};
29
33BOOST_FIXTURE_TEST_SUITE( Chamfer, ChamferFixture )
34
42
43static void DoChamferTestChecks( const TWO_LINE_CHAMFER_TEST_CASE& aTestCase )
44{
45 // Actally do the chamfer
46 const std::optional<CHAMFER_RESULT> chamfer_result = ComputeChamferPoints( aTestCase.m_seg_a,
47 aTestCase.m_seg_b,
48 aTestCase.m_params );
49
50 BOOST_REQUIRE_EQUAL( chamfer_result.has_value(), aTestCase.m_expected_result.has_value() );
51
52 if( chamfer_result.has_value() )
53 {
54 const CHAMFER_RESULT& expected_result = aTestCase.m_expected_result.value();
55 const CHAMFER_RESULT& actual_result = chamfer_result.value();
56
58 ( actual_result.m_chamfer )( expected_result.m_chamfer ) );
59
60 const auto check_updated_seg =
61 [&]( const std::optional<SEG>& updated_seg, const std::optional<SEG>& expected_seg )
62 {
63 BOOST_REQUIRE_EQUAL( updated_seg.has_value(), expected_seg.has_value() );
64
65 if( updated_seg.has_value() )
66 {
68 ( *updated_seg )( *expected_seg ) );
69 }
70 };
71
72 check_updated_seg( actual_result.m_updated_seg_a, expected_result.m_updated_seg_a );
73 check_updated_seg( actual_result.m_updated_seg_b, expected_result.m_updated_seg_b );
74 }
75}
76
77BOOST_AUTO_TEST_CASE( SimpleChamferAtOrigin )
78{
79 /* 10
80 * 0,0 +----+-------------> 1000
81 * | /
82 * | /
83 * 10 +
84 * |
85 * v 1000
86 * */
87
88 const TWO_LINE_CHAMFER_TEST_CASE testcase
89 {
90 { VECTOR2I( 0, 0 ), VECTOR2I( 1000, 0 ) },
91 { VECTOR2I( 0, 0 ), VECTOR2I( 0, 1000 ) },
92 { 10, 10 },
93 { {
94 SEG( VECTOR2I( 10, 0 ), VECTOR2I( 0, 10 ) ), // chamfer
95 { SEG( VECTOR2I( 10, 0 ), VECTOR2I( 1000, 0 ) ) }, // rest of the line A
96 { SEG( VECTOR2I( 0, 10 ), VECTOR2I( 0, 1000 ) ) }, // rest of the line B
97 } },
98 };
99
100 DoChamferTestChecks( testcase );
101}
102
103BOOST_AUTO_TEST_CASE( SimpleChamferNotAtOrigin )
104{
105 // Same as above but the intersection is not at the origin
106 const TWO_LINE_CHAMFER_TEST_CASE testcase
107 {
108 { VECTOR2I( 1000, 1000 ), VECTOR2I( 2000, 1000 ) },
109 { VECTOR2I( 1000, 1000 ), VECTOR2I( 1000, 2000 ) },
110 { 10, 10 },
111 { {
112 SEG( VECTOR2I( 1010, 1000 ), VECTOR2I( 1000, 1010 ) ),
113 { SEG( VECTOR2I( 1010, 1000 ), VECTOR2I( 2000, 1000 ) ) },
114 { SEG( VECTOR2I( 1000, 1010 ), VECTOR2I( 1000, 2000 ) ) },
115 } },
116 };
117
118 DoChamferTestChecks( testcase );
119}
120
121BOOST_AUTO_TEST_CASE( AsymmetricChamfer )
122{
123 // Same as above but the intersection is not at the origin
124 const TWO_LINE_CHAMFER_TEST_CASE testcase
125 {
126 { VECTOR2I( 0, 0 ), VECTOR2I( 1000, 0 ) },
127 { VECTOR2I( 0, 0 ), VECTOR2I( 0, 1000 ) },
128 { 10, 100 },
129 { {
130 SEG( VECTOR2I( 10, 0 ), VECTOR2I( 0, 100 ) ), // chamfer
131 { SEG( VECTOR2I( 10, 0 ), VECTOR2I( 1000, 0 ) ) }, // rest of the line A
132 { SEG( VECTOR2I( 0, 100 ), VECTOR2I( 0, 1000 ) ) }, // rest of the line B
133 } },
134 };
135
136 DoChamferTestChecks( testcase );
137}
138
139BOOST_AUTO_TEST_CASE( ChamferFullLength )
140{
141 // Chamfer consumes the entire length of a line
142 const TWO_LINE_CHAMFER_TEST_CASE testcase
143 {
144 { VECTOR2I( 0, 0 ), VECTOR2I( 1000, 0 ) },
145 { VECTOR2I( 0, 0 ), VECTOR2I( 0, 100 ) },
146 { 100, 100 },
147 { {
148 SEG( VECTOR2I( 100, 0 ), VECTOR2I( 0, 100 ) ), // chamfer
149 { SEG( VECTOR2I( 100, 0 ), VECTOR2I( 1000, 0 ) ) }, // rest of the line A
150 std::nullopt, // line b no longer exists
151 } },
152 };
153
154 DoChamferTestChecks( testcase );
155}
156
157BOOST_AUTO_TEST_CASE( ChamferOverFullLength )
158{
159 // Chamfer consumes the entire length of a line
160 const TWO_LINE_CHAMFER_TEST_CASE testcase
161 {
162 { VECTOR2I( 0, 0 ), VECTOR2I( 1000, 0 ) },
163 { VECTOR2I( 0, 0 ), VECTOR2I( 0, 100 ) },
164 { 150, 150 }, // > 100
165 std::nullopt,
166 };
167
168 DoChamferTestChecks( testcase );
169}
170
Definition seg.h:38
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()
BOOST_CHECK_PREDICATE(ArePolylineEndPointsNearCircle,(chain)(c.m_geom.m_center_point)(radius)(accuracy+epsilon))
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683