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, see <https://www.gnu.org/licenses/>.
18 */
19
21#include <boost/test/data/test_case.hpp>
22
23#include <geometry/half_line.h>
25
26#include "geom_test_utils.h"
27
28
29BOOST_AUTO_TEST_SUITE( HalfLine )
30
31struct HalfLineBoxClipCase : public KI_TEST::NAMED_CASE
32{
35 std::optional<SEG> ExpectedClippedSeg;
36};
37
44
51
52const std::vector<HalfLineContainsPointCase> Contains_cases{
53 {
54 "Point on the ray",
55 HALF_LINE( SEG( VECTOR2I( 0, 0 ), VECTOR2I( 100, 100 ) ) ),
56 VECTOR2I( 50, 50 ),
57 true,
58 },
59 {
60 "Point on the ray start",
61 HALF_LINE( SEG( VECTOR2I( 0, 0 ), VECTOR2I( 100, 100 ) ) ),
62 VECTOR2I( 0, 0 ),
63 true,
64 },
65 {
66 "Point on the ray end",
67 HALF_LINE( SEG( VECTOR2I( 0, 0 ), VECTOR2I( 100, 100 ) ) ),
68 VECTOR2I( 100, 100 ),
69 true,
70 },
71 {
72 "Point on the ray, past the end",
73 HALF_LINE( SEG( VECTOR2I( 0, 0 ), VECTOR2I( 100, 100 ) ) ),
74 VECTOR2I( 150, 150 ),
75 true,
76 },
77 {
78 "Point on the infinite line, but on the wrong side",
79 HALF_LINE( SEG( VECTOR2I( 0, 0 ), VECTOR2I( 100, 100 ) ) ),
80 VECTOR2I( -50, -50 ),
81 false,
82 },
83 {
84 "Point to one side",
85 HALF_LINE( SEG( VECTOR2I( 0, 0 ), VECTOR2I( 100, 100 ) ) ),
86 VECTOR2I( 50, 0 ),
87 false,
88 }
89};
90
91
92BOOST_DATA_TEST_CASE( Contains, boost::unit_test::data::make( Contains_cases ), c )
93{
94 const bool contains = c.Hl.Contains( c.Point );
95
96 BOOST_TEST( contains == c.ExpectedContains );
97}
98
99
100const std::vector<HalfLineHalfLineIntersectionCase> Intersect_cases{
101 {
102 "Simple cross",
103 HALF_LINE( SEG( VECTOR2I( -100, -100 ), VECTOR2I( 0, 0 ) ) ),
104 HALF_LINE( SEG( VECTOR2I( 100, -100 ), VECTOR2I( 0, 0 ) ) ),
105 VECTOR2I( 0, 0 ),
106 },
107 {
108 "Parallel, no intersection",
109 HALF_LINE( SEG( VECTOR2I( -100, 0 ), VECTOR2I( -100, 100 ) ) ),
110 HALF_LINE( SEG( VECTOR2I( 100, 0 ), VECTOR2I( 100, 100 ) ) ),
111 std::nullopt,
112 }
113};
114
115
116BOOST_DATA_TEST_CASE( Intersect, boost::unit_test::data::make( Intersect_cases ), c )
117{
118 std::optional<VECTOR2I> intersection = c.HlA.Intersect( c.HlB );
119
120 BOOST_REQUIRE( intersection.has_value() == c.ExpectedIntersection.has_value() );
121
122 if( intersection )
123 {
124 BOOST_TEST( *intersection == *c.ExpectedIntersection );
125 }
126}
127
128
129const std::vector<HalfLineBoxClipCase> ClipToBox_cases{
130 {
131 "Center to right edge",
132 HALF_LINE( SEG( VECTOR2I( 0, 0 ), VECTOR2I( 100, 0 ) ) ),
133 BOX2I{ VECTOR2{ -1000, -1000 }, VECTOR2{ 2000, 2000 } },
134 SEG( VECTOR2I( 0, 0 ), VECTOR2I( 1000, 0 ) ),
135 },
136 {
137 "Centre to corner",
138 HALF_LINE( SEG( VECTOR2I( 0, 0 ), VECTOR2I( 100, 100 ) ) ),
139 BOX2I{ VECTOR2{ -1000, -1000 }, VECTOR2{ 2000, 2000 } },
140 SEG( VECTOR2I( 0, 0 ), VECTOR2I( 1000, 1000 ) ),
141 },
142 {
143 "Ray not in the box",
144 HALF_LINE( SEG( VECTOR2I( 1500, 0 ), VECTOR2I( 1600, 0 ) ) ),
145 BOX2I{ VECTOR2{ -1000, -1000 }, VECTOR2{ 2000, 2000 } },
146 std::nullopt,
147 },
148 {
149 "Ray starts outside but crosses box",
150 HALF_LINE( SEG( VECTOR2I( -1500, 0 ), VECTOR2I( 0, 0 ) ) ),
151 BOX2I{ VECTOR2{ -1000, -1000 }, VECTOR2{ 2000, 2000 } },
152 SEG( VECTOR2I( -1000, 0 ), VECTOR2I( 1000, 0 ) ),
153 },
154};
155
156
157BOOST_DATA_TEST_CASE( ClipToBox, boost::unit_test::data::make( ClipToBox_cases ), c )
158{
159 std::optional<SEG> clipped = KIGEOM::ClipHalfLineToBox( c.Hl, c.Box );
160
161 BOOST_REQUIRE( clipped.has_value() == c.ExpectedClippedSeg.has_value() );
162
163 if( clipped )
164 {
166 ( *clipped )( *c.ExpectedClippedSeg ) );
167 }
168}
169
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
Definition seg.h:38
Define a general 2D-vector/point.
Definition vector2d.h:67
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:96
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
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_TEST(netlist.find("R_G1 ARM_OUT1 DIE_B R='0.001 / ((SW_STATE)") !=std::string::npos)
BOOST_CHECK_PREDICATE(ArePolylineEndPointsNearCircle,(chain)(c.m_geom.m_center_point)(radius)(accuracy+epsilon))
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683