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