KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_half_line.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, 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#include <boost/test/data/test_case.hpp>
26
27#include <geometry/half_line.h>
29
30#include "geom_test_utils.h"
31
32
33BOOST_AUTO_TEST_SUITE( HalfLine )
34
35struct HalfLineBoxClipCase : public KI_TEST::NAMED_CASE
36{
39 std::optional<SEG> ExpectedClippedSeg;
40};
41
43{
46 std::optional<VECTOR2I> ExpectedIntersection;
47};
48
50{
54};
55
56const std::vector<HalfLineContainsPointCase> Contains_cases{
57 {
58 "Point on the ray",
59 HALF_LINE( SEG( VECTOR2I( 0, 0 ), VECTOR2I( 100, 100 ) ) ),
60 VECTOR2I( 50, 50 ),
61 true,
62 },
63 {
64 "Point on the ray start",
65 HALF_LINE( SEG( VECTOR2I( 0, 0 ), VECTOR2I( 100, 100 ) ) ),
66 VECTOR2I( 0, 0 ),
67 true,
68 },
69 {
70 "Point on the ray end",
71 HALF_LINE( SEG( VECTOR2I( 0, 0 ), VECTOR2I( 100, 100 ) ) ),
72 VECTOR2I( 100, 100 ),
73 true,
74 },
75 {
76 "Point on the ray, past the end",
77 HALF_LINE( SEG( VECTOR2I( 0, 0 ), VECTOR2I( 100, 100 ) ) ),
78 VECTOR2I( 150, 150 ),
79 true,
80 },
81 {
82 "Point on the infinite line, but on the wrong side",
83 HALF_LINE( SEG( VECTOR2I( 0, 0 ), VECTOR2I( 100, 100 ) ) ),
84 VECTOR2I( -50, -50 ),
85 false,
86 },
87 {
88 "Point to one side",
89 HALF_LINE( SEG( VECTOR2I( 0, 0 ), VECTOR2I( 100, 100 ) ) ),
90 VECTOR2I( 50, 0 ),
91 false,
92 }
93};
94
95
96BOOST_DATA_TEST_CASE( Contains, boost::unit_test::data::make( Contains_cases ), c )
97{
98 const bool contains = c.Hl.Contains( c.Point );
99
100 BOOST_TEST( contains == c.ExpectedContains );
101}
102
103
104const std::vector<HalfLineHalfLineIntersectionCase> Intersect_cases{
105 {
106 "Simple cross",
107 HALF_LINE( SEG( VECTOR2I( -100, -100 ), VECTOR2I( 0, 0 ) ) ),
108 HALF_LINE( SEG( VECTOR2I( 100, -100 ), VECTOR2I( 0, 0 ) ) ),
109 VECTOR2I( 0, 0 ),
110 },
111 {
112 "Parallel, no intersection",
113 HALF_LINE( SEG( VECTOR2I( -100, 0 ), VECTOR2I( -100, 100 ) ) ),
114 HALF_LINE( SEG( VECTOR2I( 100, 0 ), VECTOR2I( 100, 100 ) ) ),
115 std::nullopt,
116 }
117};
118
119
120BOOST_DATA_TEST_CASE( Intersect, boost::unit_test::data::make( Intersect_cases ), c )
121{
122 std::optional<VECTOR2I> intersection = c.HlA.Intersect( c.HlB );
123
124 BOOST_REQUIRE( intersection.has_value() == c.ExpectedIntersection.has_value() );
125
126 if( intersection )
127 {
128 BOOST_TEST( *intersection == *c.ExpectedIntersection );
129 }
130}
131
132
133const std::vector<HalfLineBoxClipCase> ClipToBox_cases{
134 {
135 "Center to right edge",
136 HALF_LINE( SEG( VECTOR2I( 0, 0 ), VECTOR2I( 100, 0 ) ) ),
137 BOX2I{ VECTOR2{ -1000, -1000 }, VECTOR2{ 2000, 2000 } },
138 SEG( VECTOR2I( 0, 0 ), VECTOR2I( 1000, 0 ) ),
139 },
140 {
141 "Centre to corner",
142 HALF_LINE( SEG( VECTOR2I( 0, 0 ), VECTOR2I( 100, 100 ) ) ),
143 BOX2I{ VECTOR2{ -1000, -1000 }, VECTOR2{ 2000, 2000 } },
144 SEG( VECTOR2I( 0, 0 ), VECTOR2I( 1000, 1000 ) ),
145 },
146 {
147 "Ray not in the box",
148 HALF_LINE( SEG( VECTOR2I( 1500, 0 ), VECTOR2I( 1600, 0 ) ) ),
149 BOX2I{ VECTOR2{ -1000, -1000 }, VECTOR2{ 2000, 2000 } },
150 std::nullopt,
151 },
152 {
153 "Ray starts outside but crosses box",
154 HALF_LINE( SEG( VECTOR2I( -1500, 0 ), VECTOR2I( 0, 0 ) ) ),
155 BOX2I{ VECTOR2{ -1000, -1000 }, VECTOR2{ 2000, 2000 } },
156 SEG( VECTOR2I( -1000, 0 ), VECTOR2I( 1000, 0 ) ),
157 },
158};
159
160
161BOOST_DATA_TEST_CASE( ClipToBox, boost::unit_test::data::make( ClipToBox_cases ), c )
162{
163 std::optional<SEG> clipped = KIGEOM::ClipHalfLineToBox( c.Hl, c.Box );
164
165 BOOST_REQUIRE( clipped.has_value() == c.ExpectedClippedSeg.has_value() );
166
167 if( clipped )
168 {
170 ( *clipped )( *c.ExpectedClippedSeg ) );
171 }
172}
173
Definition: seg.h:42
bool SegmentsHaveSameEndPoints(const SEG &aSeg1, const SEG &aSeg2)
Check that two SEGs have the same end points, in either order.
std::optional< SEG > ClipHalfLineToBox(const HALF_LINE &aRay, const BOX2I &aBox)
Get the segment of a half-line that is inside a box, if any.
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:100
Utility functions for working with shapes.
std::optional< SEG > ExpectedClippedSeg
std::optional< VECTOR2I > ExpectedIntersection
A named data-driven test case.
BOOST_DATA_TEST_CASE(ConvertToKicadUnit, boost::unit_test::data::make(altium_to_kicad_unit), input_value, expected_result)
Test conversation from Altium internal units into KiCad internal units.
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
const std::vector< HalfLineContainsPointCase > Contains_cases
BOOST_TEST(contains==c.ExpectedContains)
const std::vector< HalfLineHalfLineIntersectionCase > Intersect_cases
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
const std::vector< HalfLineBoxClipCase > ClipToBox_cases
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:695