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 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
23
26
27// Code under test
28#include <math/box2.h>
29
30
34BOOST_AUTO_TEST_SUITE( BOX2TESTS )
35
36BOOST_AUTO_TEST_CASE( DefaultConstructor )
37{
38 const BOX2I box;
39 BOOST_TEST( box.GetPosition() == VECTOR2I( 0, 0 ) );
40 BOOST_TEST( box.GetSize() == VECTOR2L( 0, 0 ) );
41}
42
44{
45 const BOX2I box( VECTOR2I( 1, 2 ), VECTOR2I( 3, 4 ) );
46
47 BOOST_TEST( box.GetPosition() == VECTOR2I( 1, 2 ) );
48 BOOST_TEST( box.GetSize() == VECTOR2L( 3, 4 ) );
49
50 // Check the equality operator
51 BOOST_TEST( box == BOX2I( VECTOR2I( 1, 2 ), VECTOR2I( 3, 4 ) ) );
52
53 // Inflate in-place
54 BOX2I inflated = box;
55 inflated.Inflate( 1 );
56 BOOST_TEST( inflated.GetPosition() == VECTOR2I( 0, 1 ) );
57 BOOST_TEST( inflated.GetSize() == VECTOR2L( 5, 6 ) );
58
59 // GetInflated
60 const BOX2I inflated2 = box.GetInflated( 1 );
61 BOOST_TEST( inflated2 == inflated );
62}
63
65{
66 constexpr BOX2I box_1_2__3_4( VECTOR2I( 1, 2 ), VECTOR2I( 3, 4 ) );
67 static_assert( box_1_2__3_4.GetPosition() == VECTOR2I( 1, 2 ) );
68 static_assert( box_1_2__3_4.GetSize() == VECTOR2L( 3, 4 ) );
69
70 constexpr BOX2I box0_1__5_6 = box_1_2__3_4.GetInflated( 1 );
71 static_assert( box0_1__5_6.GetPosition() == VECTOR2I( 0, 1 ) );
72 static_assert( box0_1__5_6.GetSize() == VECTOR2L( 5, 6 ) );
73
74 static_assert( box_1_2__3_4.SquaredDiagonal() < box0_1__5_6.SquaredDiagonal() );
75
76 constexpr BOX2I box1_2__100_4 =
77 box_1_2__3_4.GetWithOffset( VECTOR2I( 100, 0 ) ).Merge( box_1_2__3_4 );
78 static_assert( box1_2__100_4.GetPosition() == VECTOR2I( 1, 2 ) );
79 static_assert( box1_2__100_4.GetSize() == VECTOR2L( 103, 4 ) );
80}
81
83{
84 const double tol = 0.000001;
85 const BOX2D box( VECTOR2D( 1.0, 2.0 ), VECTOR2D( 3.0, 4.0 ) );
86
87 // Inflate by non-integer amount
88 const BOX2D inflated = BOX2D( box ).Inflate( 1.5 );
90 ( inflated.GetPosition() )( VECTOR2D( -0.5, 0.5 ) )( tol ) );
92 ( inflated.GetSize() )( VECTOR2D( 6.0, 7.0 ) )( tol ) );
93}
94
96{
97 const BOX2I boxByCorners = BOX2I::ByCorners( VECTOR2I( 1, 2 ), VECTOR2I( 3, 4 ) );
98 const BOX2I boxByPosSize = BOX2I( VECTOR2I( 1, 2 ), VECTOR2I( 2, 2 ) );
99
100 BOOST_TEST( boxByCorners == boxByPosSize );
101}
102
104{
105 const BOX2I boxByCenter = BOX2I::ByCenter( VECTOR2I( 100, 100 ), VECTOR2I( 20, 20 ) );
106 const BOX2I boxByPosSize = BOX2I( VECTOR2I( 90, 90 ), VECTOR2I( 20, 20 ) );
107
108 BOOST_TEST( boxByCenter == boxByPosSize );
109}
110
111BOOST_AUTO_TEST_CASE( test_closest_point_to, *boost::unit_test::tolerance( 0.000001 ) )
112{
113 BOX2D box( VECTOR2D( 1, 2 ), VECTOR2D( 3, 4 ) );
114
115 // check all quadrants
116
117 // top left
118 BOOST_TEST( box.NearestPoint( VECTOR2D( 0, 0 ) ) == VECTOR2D( 1, 2 ) );
119
120 // top
121 BOOST_TEST( box.NearestPoint( VECTOR2D( 2, 0 ) ) == VECTOR2D( 2, 2 ) );
122
123 // top right
124 BOOST_TEST( box.NearestPoint( VECTOR2D( 6, 0 ) ) == VECTOR2D( 4, 2 ) );
125
126 // right
127 BOOST_TEST( box.NearestPoint( VECTOR2D( 6, 5 ) ) == VECTOR2D( 4, 5 ) );
128
129 // bottom right
130 BOOST_TEST( box.NearestPoint( VECTOR2D( 6, 7 ) ) == VECTOR2D( 4, 6 ) );
131
132 // bottom
133 BOOST_TEST( box.NearestPoint( VECTOR2D( 3, 7 ) ) == VECTOR2D( 3, 6 ) );
134
135 // bottom left
136 BOOST_TEST( box.NearestPoint( VECTOR2D( 0, 7 ) ) == VECTOR2D( 1, 6 ) );
137
138 // left
139 BOOST_TEST( box.NearestPoint( VECTOR2D( 0, 3 ) ) == VECTOR2D( 1, 3 ) );
140
141 // inside
142 BOOST_TEST( box.NearestPoint( VECTOR2D( 2, 4 ) ) == VECTOR2D( 2, 4 ) );
143}
144
145BOOST_AUTO_TEST_CASE( test_farthest_point_to, *boost::unit_test::tolerance( 0.000001 ) )
146{
147 BOX2D box( VECTOR2D( 1, 2 ), VECTOR2D( 3, 4 ) );
148
149 // note: the farthest point always is on a corner of the box
150
151 // outside:
152
153 // top left
154 BOOST_TEST( box.FarthestPointTo( VECTOR2D( 0, 0 ) ) == VECTOR2D( 4, 6 ) );
155
156 // top right
157 BOOST_TEST( box.FarthestPointTo( VECTOR2D( 6, 0 ) ) == VECTOR2D( 1, 6 ) );
158
159 // bottom right
160 BOOST_TEST( box.FarthestPointTo( VECTOR2D( 6, 7 ) ) == VECTOR2D( 1, 2 ) );
161
162 // bottom left
163 BOOST_TEST( box.FarthestPointTo( VECTOR2D( 0, 7 ) ) == VECTOR2D( 4, 2 ) );
164
165 // inside:
166
167 // top left
168 BOOST_TEST( box.FarthestPointTo( VECTOR2D( 2, 3 ) ) == VECTOR2D( 4, 6 ) );
169
170 // top right
171 BOOST_TEST( box.FarthestPointTo( VECTOR2D( 3, 3 ) ) == VECTOR2D( 1, 6 ) );
172
173 // bottom right
174 BOOST_TEST( box.FarthestPointTo( VECTOR2D( 3, 5 ) ) == VECTOR2D( 1, 2 ) );
175
176 // bottom left
177 BOOST_TEST( box.FarthestPointTo( VECTOR2D( 2, 5 ) ) == VECTOR2D( 4, 2 ) );
178}
179
180BOOST_AUTO_TEST_CASE( test_intersects_circle, *boost::unit_test::tolerance( 0.000001 ) )
181{
182 BOX2D box( VECTOR2D( 1, 2 ), VECTOR2D( 6, 8 ) );
183
184 // box inside circle (touching corners)
185 BOOST_TEST( box.IntersectsCircle( VECTOR2D( 4, 6 ), 5 ) == true );
186
187 // box completely inside circle
188 BOOST_TEST( box.IntersectsCircle( VECTOR2D( 4, 6 ), 6 ) == true );
189
190 // circle completely inside box
191 BOOST_TEST( box.IntersectsCircle( VECTOR2D( 4, 6 ), 2 ) == true );
192
193 // circle outside box
194 BOOST_TEST( box.IntersectsCircle( VECTOR2D( 14, 6 ), 5 ) == false );
195}
196
197BOOST_AUTO_TEST_CASE( test_intersects_circle_edge, *boost::unit_test::tolerance( 0.000001 ) )
198{
199 BOX2D box( VECTOR2D( 1, 2 ), VECTOR2D( 6, 8 ) );
200
201 // box touching edge
202 BOOST_TEST( box.IntersectsCircleEdge( VECTOR2D( 4, 6 ), 5, 1 ) == true );
203
204 // box completely inside circle
205 BOOST_TEST( box.IntersectsCircleEdge( VECTOR2D( 4, 6 ), 6, 1 ) == false );
206
207 // circle completely inside box
208 BOOST_TEST( box.IntersectsCircleEdge( VECTOR2D( 4, 6 ), 2, 1 ) == true );
209
210 // circle outside box
211 BOOST_TEST( box.IntersectsCircleEdge( VECTOR2D( 14, 6 ), 5, 1 ) == false );
212}
213
214// Reference distance from a point to a box by clamping per axis
215static int64_t refSquaredDistance( const BOX2I& aBox, const VECTOR2I& aP )
216{
217 const int64_t x1 = std::min<int64_t>( aBox.GetLeft(), aBox.GetRight() );
218 const int64_t x2 = std::max<int64_t>( aBox.GetLeft(), aBox.GetRight() );
219 const int64_t y1 = std::min<int64_t>( aBox.GetTop(), aBox.GetBottom() );
220 const int64_t y2 = std::max<int64_t>( aBox.GetTop(), aBox.GetBottom() );
221 const int64_t dx = aP.x < x1 ? x1 - aP.x : ( aP.x > x2 ? aP.x - x2 : 0 );
222 const int64_t dy = aP.y < y1 ? y1 - aP.y : ( aP.y > y2 ? aP.y - y2 : 0 );
223
224 return dx * dx + dy * dy;
225}
226
227BOOST_AUTO_TEST_CASE( SquaredDistanceToPoint )
228{
229 const BOX2I box( VECTOR2I( 0, 0 ), VECTOR2I( 100, 100 ) );
230
231 BOOST_CHECK_EQUAL( box.SquaredDistance( VECTOR2I( 1000, 50 ) ), 810000 );
232 BOOST_CHECK_EQUAL( box.SquaredDistance( VECTOR2I( -20, 50 ) ), 400 );
233 BOOST_CHECK_EQUAL( box.SquaredDistance( VECTOR2I( 50, 1000 ) ), 810000 );
234 BOOST_CHECK_EQUAL( box.SquaredDistance( VECTOR2I( 50, -30 ) ), 900 );
235 BOOST_CHECK_EQUAL( box.SquaredDistance( VECTOR2I( 50, 50 ) ), 0 );
236
237 const int coords[] = { -300, -100, -1, 0, 1, 50, 99, 100, 101, 250, 1000 };
238
239 for( const BOX2I& b : { box, BOX2I( VECTOR2I( 10, -40 ), VECTOR2I( 60, 25 ) ),
240 BOX2I( VECTOR2I( 100, 100 ), VECTOR2L( -100, -100 ) ) } )
241 {
242 for( int x : coords )
243 {
244 for( int y : coords )
245 BOOST_CHECK_EQUAL( b.SquaredDistance( VECTOR2I( x, y ) ), refSquaredDistance( b, VECTOR2I( x, y ) ) );
246 }
247 }
248}
249
250BOOST_AUTO_TEST_CASE( InflateSentinelDoesNotWrap )
251{
252 const int delta = std::numeric_limits<int>::max() / 2 + 50000;
253
254 BOX2I box( VECTOR2I( 1000, 2000 ), VECTOR2I( 300, 400 ) );
255 box.Inflate( delta );
256
257 BOOST_CHECK_EQUAL( box.GetLeft(), 1000 - delta );
258 BOOST_CHECK_EQUAL( box.GetTop(), 2000 - delta );
259 BOOST_CHECK_EQUAL( box.GetWidth(), 300 + 2 * int64_t( delta ) );
260 BOOST_CHECK_EQUAL( box.GetHeight(), 400 + 2 * int64_t( delta ) );
261
262 // Far enough to saturate the coordinate range, the box must still cover the original
263 BOX2I huge( VECTOR2I( -1000, 500 ), VECTOR2I( 2000, 100 ) );
264 huge.Inflate( std::numeric_limits<int>::max() );
265
266 BOOST_CHECK_EQUAL( huge.GetLeft(), std::numeric_limits<int>::lowest() );
267 BOOST_CHECK_LE( huge.GetTop(), 500 );
268 BOOST_CHECK_GE( int64_t( huge.GetLeft() ) + huge.GetWidth(), 1000 );
269 BOOST_CHECK_GE( int64_t( huge.GetTop() ) + huge.GetHeight(), 600 );
270}
271
BOX2< VECTOR2I > BOX2I
Definition box2.h:914
BOX2< VECTOR2D > BOX2D
Definition box2.h:915
constexpr const Vec & GetPosition() const
Definition box2.h:208
constexpr BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition box2.h:553
static constexpr BOX2< VECTOR2I > ByCorners(const VECTOR2I &aCorner1, const VECTOR2I &aCorner2)
Definition box2.h:67
constexpr ecoord_type SquaredDiagonal() const
Return the square of the length of the diagonal of the rectangle.
Definition box2.h:717
static constexpr BOX2< VECTOR2I > ByCenter(const VECTOR2I &aCenter, const SizeVec &aSize)
Definition box2.h:72
constexpr size_type GetWidth() const
Definition box2.h:211
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition box2.h:594
constexpr size_type GetHeight() const
Definition box2.h:212
constexpr coord_type GetLeft() const
Definition box2.h:225
constexpr BOX2< Vec > GetWithOffset(const Vec &aMoveVector) const
Definition box2.h:267
constexpr ecoord_type SquaredDistance(const Vec &aP) const
Definition box2.h:722
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:574
constexpr coord_type GetRight() const
Definition box2.h:214
constexpr const SizeVec & GetSize() const
Definition box2.h:203
constexpr coord_type GetTop() const
Definition box2.h:226
constexpr coord_type GetBottom() const
Definition box2.h:219
bool IsVecWithinTol(const VEC &aVec, const VEC &aExp, typename VEC::coord_type aTol)
Check that both x and y of a vector are within expected error.
Definition geometry.h:51
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
static int64_t refSquaredDistance(const BOX2I &aBox, const VECTOR2I &aP)
BOOST_AUTO_TEST_CASE(DefaultConstructor)
Test suite for KiCad math code.
Definition test_box2.cpp:36
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
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))
BOOST_CHECK_EQUAL(result, "25.4")
int delta
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
VECTOR2< double > VECTOR2D
Definition vector2d.h:707
VECTOR2< int64_t > VECTOR2L
Definition vector2d.h:709