KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_sch_rtree.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 3
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-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 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
28
29#include <properties/property.h>
30#include <base_units.h>
31#include <core/ignore.h>
32#include <sch_junction.h>
33#include <sch_no_connect.h>
36
37// Code under test
38#include <sch_rtree.h>
39
42
44{
45public:
49
51};
52
53
57BOOST_FIXTURE_TEST_SUITE( SchRtree, TEST_SCH_RTREE_FIXTURE )
58
59
60
64{
65 BOOST_CHECK_EQUAL( m_tree.empty(), true );
66
67 int count = 0;
68 for( auto item : m_tree )
69 {
70 ignore_unused( item );
71 count++;
72 }
73
74 BOOST_CHECK_EQUAL( count, 0 );
75
76 for( int type = 0; type <= MAX_STRUCT_TYPE_ID; type++ )
77 {
78 count = 0;
79 for( auto item : m_tree.OfType( KICAD_T( type ) ) )
80 {
81 ignore_unused( item );
82 count++;
83 }
84
85 BOOST_CHECK_EQUAL( count, 0 );
86 }
87
88 BOX2I bbox;
89
90 for( int type = 0; type <= MAX_STRUCT_TYPE_ID; type++ )
91 {
92 count = 0;
93
94 for( SCH_ITEM* item : m_tree.Overlapping( SCH_JUNCTION_T, bbox ) )
95 {
96 ignore_unused( item );
97 count++;
98 }
99
100 BOOST_CHECK_EQUAL( count, 0 );
101 }
102}
103
105{
106 for( int i = 0; i < 100; i++ )
107 {
108 SCH_JUNCTION* junction = new SCH_JUNCTION(
109 VECTOR2I( schIUScale.MilsToIU( 100 ) * i, schIUScale.MilsToIU( 100 ) * i ) );
110 m_tree.insert( junction );
111 }
112
113 int count = 0;
114
115 for( auto item : m_tree.OfType( SCH_JUNCTION_T ) )
116 {
117 ignore_unused( item );
118 count++;
119 }
120
121 BOOST_CHECK_EQUAL( count, 100 );
122
123 count = 0;
124 for( auto item : m_tree.OfType( SCH_NO_CONNECT_T ) )
125 {
126 ignore_unused( item );
127 count++;
128 }
129
130 BOOST_CHECK_EQUAL( count, 0 );
131
132 BOX2I small_bbox( VECTOR2I( -1, -1 ),
133 VECTOR2I( schIUScale.MilsToIU( 2 ), schIUScale.MilsToIU( 2 ) ) );
134 BOX2I med_bbox( VECTOR2I( 0, 0 ),
135 VECTOR2I( schIUScale.MilsToIU( 100 ), schIUScale.MilsToIU( 100 ) ) );
136 BOX2I big_bbox( VECTOR2I( 0, 0 ),
137 VECTOR2I( schIUScale.MilsToIU( 5000 ), schIUScale.MilsToIU( 5000 ) ) );
138
139 count = 0;
140
141 for( SCH_ITEM* item : m_tree.Overlapping( small_bbox ) )
142 {
143 BOOST_CHECK( small_bbox.Intersects( item->GetBoundingBox() ) );
144 count++;
145 }
146
147 BOOST_CHECK_EQUAL( count, 1 );
148
149 count = 0;
150
151 for( SCH_ITEM* item : m_tree.Overlapping( SCH_JUNCTION_T, small_bbox ) )
152 {
153 BOOST_CHECK( small_bbox.Intersects( item->GetBoundingBox() ) );
154 count++;
155 }
156
157 BOOST_CHECK_EQUAL( count, 1 );
158
159 count = 0;
160
161 for( SCH_ITEM* item : m_tree.Overlapping( SCH_NO_CONNECT_T, small_bbox ) )
162 {
163 BOOST_CHECK( small_bbox.Intersects( item->GetBoundingBox() ) );
164 count++;
165 }
166
167 BOOST_CHECK_EQUAL( count, 0 );
168
169 count = 0;
170
171 for( SCH_ITEM* item : m_tree.Overlapping( med_bbox ) )
172 {
173 BOOST_CHECK( med_bbox.Intersects( item->GetBoundingBox() ) );
174 count++;
175 }
176
177 BOOST_CHECK_EQUAL( count, 2 );
178
179 count = 0;
180
181 for( SCH_ITEM* item : m_tree.Overlapping( big_bbox ) )
182 {
183 BOOST_CHECK( big_bbox.Intersects( item->GetBoundingBox() ) );
184 count++;
185 }
186
187 BOOST_CHECK_EQUAL( count, 51 );
188
189 for( SCH_ITEM* item : m_tree )
190 delete item;
191}
192
193BOOST_AUTO_TEST_CASE( MixedElements )
194{
195 for( int i = 0; i < 100; i++ )
196 {
197 int x_sign = ( i % 2 == 0 ) ? -1 : 1;
198 int y_sign = ( i % 3 == 0 ) ? -1 : 1;
199
200 SCH_JUNCTION* junction = new SCH_JUNCTION( VECTOR2I( schIUScale.MilsToIU( 100 ) * i * x_sign,
201 schIUScale.MilsToIU( 100 ) * i * y_sign ) );
202 m_tree.insert( junction );
203
204 SCH_NO_CONNECT* nc = new SCH_NO_CONNECT( VECTOR2I( schIUScale.MilsToIU( 150 ) * i * y_sign,
205 schIUScale.MilsToIU( 150 ) * i * x_sign ) );
206 m_tree.insert( nc );
207 }
208
209 int count = 0;
210
211 for( SCH_ITEM* item : m_tree.OfType( SCH_JUNCTION_T ) )
212 {
213 ignore_unused( item );
214 count++;
215 }
216
217 BOOST_CHECK_EQUAL( count, 100 );
218
219 count = 0;
220
221 for( SCH_ITEM* item : m_tree.OfType( SCH_NO_CONNECT_T ) )
222 {
223 ignore_unused( item );
224 count++;
225 }
226
227 BOOST_CHECK_EQUAL( count, 100 );
228
229 BOX2I small_bbox( VECTOR2I( -1, -1 ), VECTOR2I( schIUScale.MilsToIU( 2 ), schIUScale.MilsToIU( 2 ) ) );
230
231 count = 0;
232
233 for( SCH_ITEM* item : m_tree.Overlapping( small_bbox ) )
234 {
235 BOOST_CHECK( small_bbox.Intersects( item->GetBoundingBox() ) );
236 count++;
237 }
238
239 BOOST_CHECK_EQUAL( count, 2 );
240
241 count = 0;
242
243 for( SCH_ITEM* item : m_tree.Overlapping( SCH_JUNCTION_T, small_bbox ) )
244 {
245 BOOST_CHECK( small_bbox.Intersects( item->GetBoundingBox() ) );
246 count++;
247 }
248
249 BOOST_CHECK_EQUAL( count, 1 );
250
251 count = 0;
252
253 for( SCH_ITEM* item : m_tree.Overlapping( SCH_NO_CONNECT_T, small_bbox ) )
254 {
255 BOOST_CHECK( small_bbox.Intersects( item->GetBoundingBox() ) );
256 count++;
257 }
258
259 BOOST_CHECK_EQUAL( count, 1 );
260
261 for( SCH_ITEM* item : m_tree )
262 delete item;
263}
264
265// This tests the case where the tree has no branches but we want to iterator over a subset
266// where the first case may or may not match
267BOOST_AUTO_TEST_CASE( SingleElementTree )
268{
269 SCH_JUNCTION* junction = new SCH_JUNCTION( VECTOR2I( schIUScale.MilsToIU( 100 ), schIUScale.MilsToIU( 100 ) ) );
270 m_tree.insert( junction );
271
272 SCH_NO_CONNECT* nc = new SCH_NO_CONNECT( VECTOR2I( schIUScale.MilsToIU( 150 ), schIUScale.MilsToIU( 150 ) ) );
273 m_tree.insert( nc );
274
275 int count = 0;
276
277 for( auto item : m_tree.OfType( SCH_JUNCTION_T ) )
278 {
279 ignore_unused( item );
280 count++;
281 }
282
283 BOOST_CHECK_EQUAL( count, 1 );
284
285 count = 0;
286 for( auto item : m_tree.OfType( SCH_NO_CONNECT_T ) )
287 {
288 ignore_unused( item );
289 count++;
290 }
291
292 BOOST_CHECK_EQUAL( count, 1 );
293
294 for( SCH_ITEM* item : m_tree )
295 delete item;
296}
297
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:114
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
constexpr bool Intersects(const BOX2< Vec > &aRect) const
Definition box2.h:311
Implement an R-tree for fast spatial and type indexing of schematic items.
Definition sch_rtree.h:40
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:168
void ignore_unused(const T &)
Definition ignore.h:24
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(Default)
Declare the test suite.
BOOST_CHECK_EQUAL(result, "25.4")
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition typeinfo.h:78
@ SCH_NO_CONNECT_T
Definition typeinfo.h:164
@ MAX_STRUCT_TYPE_ID
Definition typeinfo.h:242
@ SCH_JUNCTION_T
Definition typeinfo.h:163
Test utilities for timestamps.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695