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