1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2013 CERN
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef __SHAPE_INDEX_LIST_H
#define __SHAPE_INDEX_LIST_H
#include <boost/unordered_map.hpp>
template <class T>
const SHAPE* defaultShapeFunctor( const T aItem )
{
return aItem->Shape();
}
template <class T, const SHAPE* (ShapeFunctor) (const T) = defaultShapeFunctor<T> >
class SHAPE_INDEX_LIST
{
struct SHAPE_ENTRY
{
SHAPE_ENTRY( T aParent )
{
shape = ShapeFunctor( aParent );
bbox = shape->BBox( 0 );
parent = aParent;
}
~SHAPE_ENTRY()
{
}
T parent;
const SHAPE* shape;
BOX2I bbox;
};
typedef std::vector<SHAPE_ENTRY> SHAPE_VEC;
typedef typename std::vector<SHAPE_ENTRY>::iterator SHAPE_VEC_ITER;
public:
// "Normal" iterator interface, for STL algorithms.
class iterator
{
public:
iterator()
{}
iterator( SHAPE_VEC_ITER aCurrent ) :
m_current( aCurrent )
{}
iterator( const iterator& aB ) :
m_current( aB.m_current )
{}
T operator*() const
{
return (*m_current).parent;
}
void operator++()
{
++m_current;
}
iterator& operator++( int aDummy )
{
++m_current;
return *this;
}
bool operator==( const iterator& aRhs ) const
{
return m_current == aRhs.m_current;
}
bool operator!=( const iterator& aRhs ) const
{
return m_current != aRhs.m_current;
}
const iterator& operator=( const iterator& aRhs )
{
m_current = aRhs.m_current;
return *this;
}
private:
SHAPE_VEC_ITER m_current;
};
// "Query" iterator, for iterating over a set of spatially matching shapes.
class query_iterator
{
public:
query_iterator()
{
}
query_iterator( SHAPE_VEC_ITER aCurrent, SHAPE_VEC_ITER aEnd, SHAPE* aShape,
int aMinDistance, bool aExact ) :
m_end( aEnd ),
m_current( aCurrent ),
m_shape( aShape ),
m_minDistance( aMinDistance ),
m_exact( aExact )
{
if( aShape )
{
m_refBBox = aShape->BBox();
next();
}
}
query_iterator( const query_iterator& aB ) :
m_end( aB.m_end ),
m_current( aB.m_current ),
m_shape( aB.m_shape ),
m_minDistance( aB.m_minDistance ),
m_exact( aB.m_exact ),
m_refBBox( aB.m_refBBox )
{
}
T operator*() const
{
return (*m_current).parent;
}
query_iterator& operator++()
{
++m_current;
next();
return *this;
}
query_iterator& operator++( int aDummy )
{
++m_current;
next();
return *this;
}
bool operator==( const query_iterator& aRhs ) const
{
return m_current == aRhs.m_current;
}
bool operator!=( const query_iterator& aRhs ) const
{
return m_current != aRhs.m_current;
}
const query_iterator& operator=( const query_iterator& aRhs )
{
m_end = aRhs.m_end;
m_current = aRhs.m_current;
m_shape = aRhs.m_shape;
m_minDistance = aRhs.m_minDistance;
m_exact = aRhs.m_exact;
m_refBBox = aRhs.m_refBBox;
return *this;
}
private:
void next()
{
while( m_current != m_end )
{
if( m_refBBox.Distance( m_current->bbox ) <= m_minDistance )
{
if( !m_exact || m_current->shape->Collide( m_shape, m_minDistance ) )
return;
}
++m_current;
}
}
SHAPE_VEC_ITER m_end;
SHAPE_VEC_ITER m_current;
BOX2I m_refBBox;
bool m_exact;
SHAPE* m_shape;
int m_minDistance;
};
void Add( T aItem )
{
SHAPE_ENTRY s( aItem );
m_shapes.push_back( s );
}
void Remove( const T aItem )
{
SHAPE_VEC_ITER i;
for( i = m_shapes.begin(); i != m_shapes.end(); ++i )
{
if( i->parent == aItem )
break;
}
if( i == m_shapes.end() )
return;
m_shapes.erase( i );
}
int Size() const
{
return m_shapes.size();
}
template <class Visitor>
int Query( const SHAPE* aShape, int aMinDistance, Visitor& aV, bool aExact = true ) // const
{
SHAPE_VEC_ITER i;
int n = 0;
VECTOR2I::extended_type minDistSq = (VECTOR2I::extended_type) aMinDistance * aMinDistance;
BOX2I refBBox = aShape->BBox();
for( i = m_shapes.begin(); i != m_shapes.end(); ++i )
{
if( refBBox.SquaredDistance( i->bbox ) <= minDistSq )
{
if( !aExact || i->shape->Collide( aShape, aMinDistance ) )
{
n++;
if( !aV( i->parent ) )
return n;
}
}
}
return n;
}
void Clear()
{
m_shapes.clear();
}
query_iterator qbegin( SHAPE* aShape, int aMinDistance, bool aExact )
{
return query_iterator( m_shapes.begin(), m_shapes.end(), aShape, aMinDistance, aExact );
}
const query_iterator qend()
{
return query_iterator( m_shapes.end(), m_shapes.end(), NULL, 0, false );
}
iterator begin()
{
return iterator( m_shapes.begin() );
}
iterator end()
{
return iterator( m_shapes.end() );
}
private:
SHAPE_VEC m_shapes;
};
#endif
|