KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_box2.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) 2022 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
30
31// Code under test
32#include <math/box2.h>
33
34
38BOOST_AUTO_TEST_SUITE( BOX2TESTS )
39
40BOOST_AUTO_TEST_CASE( DefaultConstructor )
41{
42 const BOX2I box;
43 BOOST_TEST( box.GetPosition() == VECTOR2I( 0, 0 ) );
44 BOOST_TEST( box.GetSize() == VECTOR2L( 0, 0 ) );
45}
46
48{
49 const BOX2I box( VECTOR2I( 1, 2 ), VECTOR2I( 3, 4 ) );
50
51 BOOST_TEST( box.GetPosition() == VECTOR2I( 1, 2 ) );
52 BOOST_TEST( box.GetSize() == VECTOR2L( 3, 4 ) );
53
54 // Check the equality operator
55 BOOST_TEST( box == BOX2I( VECTOR2I( 1, 2 ), VECTOR2I( 3, 4 ) ) );
56
57 // Inflate in-place
58 BOX2I inflated = box;
59 inflated.Inflate( 1 );
60 BOOST_TEST( inflated.GetPosition() == VECTOR2I( 0, 1 ) );
61 BOOST_TEST( inflated.GetSize() == VECTOR2L( 5, 6 ) );
62
63 // GetInflated
64 const BOX2I inflated2 = box.GetInflated( 1 );
65 BOOST_TEST( inflated2 == inflated );
66}
67
69{
70 constexpr BOX2I box_1_2__3_4( VECTOR2I( 1, 2 ), VECTOR2I( 3, 4 ) );
71 static_assert( box_1_2__3_4.GetPosition() == VECTOR2I( 1, 2 ) );
72 static_assert( box_1_2__3_4.GetSize() == VECTOR2L( 3, 4 ) );
73
74 constexpr BOX2I box0_1__5_6 = box_1_2__3_4.GetInflated( 1 );
75 static_assert( box0_1__5_6.GetPosition() == VECTOR2I( 0, 1 ) );
76 static_assert( box0_1__5_6.GetSize() == VECTOR2L( 5, 6 ) );
77
78 static_assert( box_1_2__3_4.SquaredDiagonal() < box0_1__5_6.SquaredDiagonal() );
79
80 constexpr BOX2I box1_2__100_4 =
81 box_1_2__3_4.GetWithOffset( VECTOR2I( 100, 0 ) ).Merge( box_1_2__3_4 );
82 static_assert( box1_2__100_4.GetPosition() == VECTOR2I( 1, 2 ) );
83 static_assert( box1_2__100_4.GetSize() == VECTOR2L( 103, 4 ) );
84}
85
87{
88 const double tol = 0.000001;
89 const BOX2D box( VECTOR2D( 1.0, 2.0 ), VECTOR2D( 3.0, 4.0 ) );
90
91 // Inflate by non-integer amount
92 const BOX2D inflated = BOX2D( box ).Inflate( 1.5 );
93 BOOST_CHECK_PREDICATE( KI_TEST::IsVecWithinTol<VECTOR2I>,
94 ( inflated.GetPosition() )( VECTOR2D( -0.5, 0.5 ) )( tol ) );
95 BOOST_CHECK_PREDICATE( KI_TEST::IsVecWithinTol<VECTOR2I>,
96 ( inflated.GetSize() )( VECTOR2D( 6.0, 7.0 ) )( tol ) );
97}
98
100{
101 const BOX2I boxByCorners = BOX2I::ByCorners( VECTOR2I( 1, 2 ), VECTOR2I( 3, 4 ) );
102 const BOX2I boxByPosSize = BOX2I( VECTOR2I( 1, 2 ), VECTOR2I( 2, 2 ) );
103
104 BOOST_TEST( boxByCorners == boxByPosSize );
105}
106
108{
109 const BOX2I boxByCenter = BOX2I::ByCenter( VECTOR2I( 100, 100 ), VECTOR2I( 20, 20 ) );
110 const BOX2I boxByPosSize = BOX2I( VECTOR2I( 90, 90 ), VECTOR2I( 20, 20 ) );
111
112 BOOST_TEST( boxByCenter == boxByPosSize );
113}
114
115BOOST_AUTO_TEST_CASE( test_closest_point_to, *boost::unit_test::tolerance( 0.000001 ) )
116{
117 BOX2D box( VECTOR2D( 1, 2 ), VECTOR2D( 3, 4 ) );
118
119 // check all quadrants
120
121 // top left
122 BOOST_TEST( box.ClosestPointTo( VECTOR2D( 0, 0 ) ) == VECTOR2D( 1, 2 ) );
123
124 // top
125 BOOST_TEST( box.ClosestPointTo( VECTOR2D( 2, 0 ) ) == VECTOR2D( 2, 2 ) );
126
127 // top right
128 BOOST_TEST( box.ClosestPointTo( VECTOR2D( 6, 0 ) ) == VECTOR2D( 4, 2 ) );
129
130 // right
131 BOOST_TEST( box.ClosestPointTo( VECTOR2D( 6, 5 ) ) == VECTOR2D( 4, 5 ) );
132
133 // bottom right
134 BOOST_TEST( box.ClosestPointTo( VECTOR2D( 6, 7 ) ) == VECTOR2D( 4, 6 ) );
135
136 // bottom
137 BOOST_TEST( box.ClosestPointTo( VECTOR2D( 3, 7 ) ) == VECTOR2D( 3, 6 ) );
138
139 // bottom left
140 BOOST_TEST( box.ClosestPointTo( VECTOR2D( 0, 7 ) ) == VECTOR2D( 1, 6 ) );
141
142 // left
143 BOOST_TEST( box.ClosestPointTo( VECTOR2D( 0, 3 ) ) == VECTOR2D( 1, 3 ) );
144
145 // inside
146 BOOST_TEST( box.ClosestPointTo( VECTOR2D( 2, 4 ) ) == VECTOR2D( 2, 4 ) );
147}
148
149BOOST_AUTO_TEST_CASE( test_farthest_point_to, *boost::unit_test::tolerance( 0.000001 ) )
150{
151 BOX2D box( VECTOR2D( 1, 2 ), VECTOR2D( 3, 4 ) );
152
153 // note: the farthest point always is on a corner of the box
154
155 // outside:
156
157 // top left
158 BOOST_TEST( box.FarthestPointTo( VECTOR2D( 0, 0 ) ) == VECTOR2D( 4, 6 ) );
159
160 // top right
161 BOOST_TEST( box.FarthestPointTo( VECTOR2D( 6, 0 ) ) == VECTOR2D( 1, 6 ) );
162
163 // bottom right
164 BOOST_TEST( box.FarthestPointTo( VECTOR2D( 6, 7 ) ) == VECTOR2D( 1, 2 ) );
165
166 // bottom left
167 BOOST_TEST( box.FarthestPointTo( VECTOR2D( 0, 7 ) ) == VECTOR2D( 4, 2 ) );
168
169 // inside:
170
171 // top left
172 BOOST_TEST( box.FarthestPointTo( VECTOR2D( 2, 3 ) ) == VECTOR2D( 4, 6 ) );
173
174 // top right
175 BOOST_TEST( box.FarthestPointTo( VECTOR2D( 3, 3 ) ) == VECTOR2D( 1, 6 ) );
176
177 // bottom right
178 BOOST_TEST( box.FarthestPointTo( VECTOR2D( 3, 5 ) ) == VECTOR2D( 1, 2 ) );
179
180 // bottom left
181 BOOST_TEST( box.FarthestPointTo( VECTOR2D( 2, 5 ) ) == VECTOR2D( 4, 2 ) );
182}
183
184BOOST_AUTO_TEST_CASE( test_intersects_circle, *boost::unit_test::tolerance( 0.000001 ) )
185{
186 BOX2D box( VECTOR2D( 1, 2 ), VECTOR2D( 6, 8 ) );
187
188 // box inside circle (touching corners)
189 BOOST_TEST( box.IntersectsCircle( VECTOR2D( 4, 6 ), 5 ) == true );
190
191 // box completely inside circle
192 BOOST_TEST( box.IntersectsCircle( VECTOR2D( 4, 6 ), 6 ) == true );
193
194 // circle completely inside box
195 BOOST_TEST( box.IntersectsCircle( VECTOR2D( 4, 6 ), 2 ) == true );
196
197 // circle outside box
198 BOOST_TEST( box.IntersectsCircle( VECTOR2D( 14, 6 ), 5 ) == false );
199}
200
201BOOST_AUTO_TEST_CASE( test_intersects_circle_edge, *boost::unit_test::tolerance( 0.000001 ) )
202{
203 BOX2D box( VECTOR2D( 1, 2 ), VECTOR2D( 6, 8 ) );
204
205 // box touching edge
206 BOOST_TEST( box.IntersectsCircleEdge( VECTOR2D( 4, 6 ), 5, 1 ) == true );
207
208 // box completely inside circle
209 BOOST_TEST( box.IntersectsCircleEdge( VECTOR2D( 4, 6 ), 6, 1 ) == false );
210
211 // circle completely inside box
212 BOOST_TEST( box.IntersectsCircleEdge( VECTOR2D( 4, 6 ), 2, 1 ) == true );
213
214 // circle outside box
215 BOOST_TEST( box.IntersectsCircleEdge( VECTOR2D( 14, 6 ), 5, 1 ) == false );
216}
217
BOX2< VECTOR2I > BOX2I
Definition: box2.h:922
BOX2< VECTOR2D > BOX2D
Definition: box2.h:923
constexpr const Vec & GetPosition() const
Definition: box2.h:211
constexpr BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:558
static constexpr BOX2< VECTOR2I > ByCorners(const VECTOR2I &aCorner1, const VECTOR2I &aCorner2)
Definition: box2.h:70
constexpr ecoord_type SquaredDiagonal() const
Return the square of the length of the diagonal of the rectangle.
Definition: box2.h:781
static constexpr BOX2< VECTOR2I > ByCenter(const VECTOR2I &aCenter, const SizeVec &aSize)
Definition: box2.h:75
constexpr BOX2< Vec > GetWithOffset(const Vec &aMoveVector) const
Definition: box2.h:270
constexpr BOX2< Vec > GetInflated(coord_type aDx, coord_type aDy) const
Get a new rectangle that is this one, inflated by aDx and aDy.
Definition: box2.h:638
constexpr const SizeVec & GetSize() const
Definition: box2.h:206
BOOST_TEST(box.ClosestPointTo(VECTOR2D(0, 0))==VECTOR2D(1, 2))
BOOST_AUTO_TEST_CASE(DefaultConstructor)
Test suite for KiCad math code.
Definition: test_box2.cpp:40
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691
VECTOR2< double > VECTOR2D
Definition: vector2d.h:690
VECTOR2< int64_t > VECTOR2L
Definition: vector2d.h:692