summaryrefslogtreecommitdiff
path: root/include/geometry/shape_index.h
blob: 94703ce9037d62e0a4d1f1b00ca0b10630b85e9a (plain)
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2013 CERN
 * @author Jacobo Aragunde Pérez
 * @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_H
#define __SHAPE_INDEX_H

#include <vector>
#include <geometry/shape.h>
#include <geometry/rtree.h>


/**
 * shapeFunctor template function
 *
 * It is used by SHAPE_INDEX to get a SHAPE* from another type.
 * By default relies on T::GetShape() method, should be specialized if the T object
 * doesn't allow that method.
 * @param aItem generic T object
 * @return a SHAPE* object equivalent to object.
 */
template <class T>
static const SHAPE* shapeFunctor( T aItem )
{
    return aItem->Shape();
}

/**
 * boundingBox template method
 *
 * It is used by SHAPE_INDEX to get the bounding box of a generic T object.
 * By default relies on T::BBox() method, should be specialized if the T object
 * doesn't allow that method.
 * @param aObject generic T object
 * @return a BOX2I object containing the bounding box of the T object.
 */
template <class T>
BOX2I boundingBox( T aObject )
{
    return shapeFunctor( aObject )->BBox();
}

/**
 * acceptVisitor template method
 *
 * It is used by SHAPE_INDEX to implement Accept().
 * By default relies on V::operation() redefinition, should be specialized if V class
 * doesn't have its () operation defined to accept T objects.
 * @param aObject generic T object
 * @param aVisitor V visitor object
 */
template <class T, class V>
void acceptVisitor( T aObject, V aVisitor )
{
    aVisitor( aObject );
}

/**
 * collide template method
 *
 * It is used by SHAPE_INDEX to implement Query().
 * By default relies on T::Collide(U) method, should be specialized if the T object
 * doesn't allow that method.
 * @param aObject generic T object
 * @param aAnotherObject generic U object
 * @param aMinDistance minimum collision distance
 * @return if object and anotherObject collide
 */
template <class T, class U>
bool collide( T aObject, U aAnotherObject, int aMinDistance )
{
    return shapeFunctor( aObject )->Collide( aAnotherObject, aMinDistance );
}

template <class T, class V>
bool queryCallback( T aShape, void* aContext )
{
    V* visitor = (V*) aContext;

    acceptVisitor<T, V>( aShape, *visitor );

    return true;
}

template <class T = SHAPE*>
class SHAPE_INDEX
{
    public:
        class Iterator
        {
        private:
            typedef typename RTree<T, int, 2, float>::Iterator RTreeIterator;
            RTreeIterator iterator;

            /**
             * Function Init()
             *
             * Setup the internal tree iterator.
             * @param aTree pointer to a RTREE object
             */
            void Init( RTree<T, int, 2, float>* aTree )
            {
                aTree->GetFirst( iterator );
            }

        public:
            /**
             * Iterator constructor
             *
             * Creates an iterator for the index object
             * @param aIndex SHAPE_INDEX object to iterate
             */
            Iterator( SHAPE_INDEX* aIndex )
            {
                Init( aIndex->m_tree );
            }

            /**
             * Operator * (prefix)
             *
             * Returns the next data element.
             */
            T operator*()
            {
                return *iterator;
            }

            /**
             * Operator ++ (prefix)
             *
             * Shifts the iterator to the next element.
             */
            bool operator++()
            {
                return ++iterator;
            }

            /**
             * Operator ++ (postfix)
             *
             * Shifts the iterator to the next element.
             */
            bool operator++( int )
            {
                return ++iterator;
            }

            /**
             * Function IsNull()
             *
             * Checks if the iterator has reached the end.
             * @return true if it is in an invalid position (data finished)
             */
            bool IsNull()
            {
                return iterator.IsNull();
            }

            /**
             * Function IsNotNull()
             *
             * Checks if the iterator has not reached the end.
             * @return true if it is in an valid position (data not finished)
             */
            bool IsNotNull()
            {
                return iterator.IsNotNull();
            }

            /**
             * Function Next()
             *
             * Returns the current element of the iterator and moves to the next
             * position.
             * @return SHAPE object pointed by the iterator before moving to the next position.
             */
            T Next()
            {
                T object = *iterator;
                ++iterator;

                return object;
            }
        };

        SHAPE_INDEX();

        ~SHAPE_INDEX();

        /**
         * Function Add()
         *
         * Adds a SHAPE to the index.
         * @param aShape is the new SHAPE.
         */
        void Add( T aShape );

        /**
         * Function Remove()
         *
         * Removes a SHAPE to the index.
         * @param aShape is the new SHAPE.
         */
        void Remove( T aShape );

        /**
         * Function RemoveAll()
         *
         * Removes all the contents of the index.
         */
        void RemoveAll();

        /**
         * Function Accept()
         *
         * Accepts a visitor for every SHAPE object contained in this INDEX.
         * @param aVisitor Visitor object to be run
         */
        template <class V>
        void Accept( V aVisitor )
        {
            Iterator iter = this->Begin();

            while( !iter.IsNull() )
            {
                T shape = *iter;
                acceptVisitor( shape, aVisitor );
                iter++;
            }
        }

        /**
         * Function Reindex()
         *
         * Rebuilds the index. This should be used if the geometry of the objects
         * contained by the index has changed.
         */
        void Reindex();

        /**
         * Function Query()
         *
         * Runs a callback on every SHAPE object contained in the bounding box of (shape).
         * @param aShape shape to search against
         * @param aMinDistance distance threshold
         * @param aVisitor object to be invoked on every object contained in the search area.
         */
        template <class V>
        int Query( const SHAPE *aShape, int aMinDistance, V& aVisitor, bool aExact )
        {
            BOX2I box = aShape->BBox();
            box.Inflate( aMinDistance );

            int min[2] = { box.GetX(),         box.GetY() };
            int max[2] = { box.GetRight(),     box.GetBottom() };

            return this->m_tree->Search( min, max, aVisitor );
        }

        /**
         * Function Begin()
         *
         * Creates an iterator for the current index object
         * @return iterator
         */
        Iterator Begin();

    private:
        RTree<T, int, 2, float>* m_tree;
};

/*
 * Class members implementation
 */

template <class T>
SHAPE_INDEX<T>::SHAPE_INDEX()
{
    this->m_tree = new RTree<T, int, 2, float>();
}

template <class T>
SHAPE_INDEX<T>::~SHAPE_INDEX()
{
    delete this->m_tree;
}

template <class T>
void SHAPE_INDEX<T>::Add( T aShape )
{
    BOX2I box = boundingBox( aShape );
    int min[2] = { box.GetX(), box.GetY() };
    int max[2] = { box.GetRight(), box.GetBottom() };

    this->m_tree->Insert( min, max, aShape );
}

template <class T>
void SHAPE_INDEX<T>::Remove( T aShape )
{
    BOX2I box = boundingBox( aShape );
    int min[2] = { box.GetX(), box.GetY() };
    int max[2] = { box.GetRight(), box.GetBottom() };

    this->m_tree->Remove( min, max, aShape );
}

template <class T>
void SHAPE_INDEX<T>::RemoveAll()
{
    this->m_tree->RemoveAll();
}

template <class T>
void SHAPE_INDEX<T>::Reindex()
{
    RTree<T, int, 2, float>* newTree;
    newTree = new RTree<T, int, 2, float>();

    Iterator iter = this->Begin();

    while( !iter.IsNull() )
    {
        T shape = *iter;
        BOX2I box = boundingBox( shape );
        int min[2] = { box.GetX(), box.GetY() };
        int max[2] = { box.GetRight(), box.GetBottom() };
        newTree->Insert( min, max, shape );
        iter++;
    }

    delete this->m_tree;
    this->m_tree = newTree;
}

template <class T>
typename SHAPE_INDEX<T>::Iterator SHAPE_INDEX<T>::Begin()
{
    return Iterator( this );
}

#endif