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
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
BOX2< VECTOR2D > BOX2D
Definition box2.h:919
constexpr const Vec & GetPosition() const
Definition box2.h:207
constexpr BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition box2.h:554
static constexpr BOX2< VECTOR2I > ByCorners(const VECTOR2I &aCorner1, const VECTOR2I &aCorner2)
Definition box2.h:66
constexpr ecoord_type SquaredDiagonal() const
Return the square of the length of the diagonal of the rectangle.
Definition box2.h:777
static constexpr BOX2< VECTOR2I > ByCenter(const VECTOR2I &aCenter, const SizeVec &aSize)
Definition box2.h:71
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition box2.h:654
constexpr BOX2< Vec > GetWithOffset(const Vec &aMoveVector) const
Definition box2.h:266
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:634
constexpr const SizeVec & GetSize() const
Definition box2.h:202
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)
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))
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
VECTOR2< double > VECTOR2D
Definition vector2d.h:682
VECTOR2< int64_t > VECTOR2L
Definition vector2d.h:684