28#ifndef __SHAPE_INDEX_H
29#define __SHAPE_INDEX_H
33#include <geometry/rtree.h>
34#include <geometry/shape.h>
47static const SHAPE* shapeFunctor( T aItem )
49 return aItem->Shape();
62static const SHAPE* holeFunctor( T aItem )
77BOX2I boundingBox( T aObject )
79 BOX2I bbox = shapeFunctor( aObject )->
BBox();
81 if( holeFunctor( aObject ) )
82 bbox.
Merge( holeFunctor( aObject )->BBox() );
96template <
class T,
class V>
97void acceptVisitor( T aObject, V aVisitor )
113template <
class T,
class U>
114bool collide( T aObject, U aAnotherObject,
int aMinDistance )
116 return shapeFunctor( aObject )->
Collide( aAnotherObject, aMinDistance );
119template <
class T,
class V>
120bool queryCallback( T aShape,
void* aContext )
122 V* visitor = (V*) aContext;
124 acceptVisitor<T, V>( aShape, *visitor );
129template <
class T = SHAPE*>
137 RTreeIterator iterator;
146 aTree->GetFirst( iterator );
157 Init( aIndex->m_tree );
191 return iterator.IsNull();
201 return iterator.IsNotNull();
211 T
object = *iterator;
227 void Add( T aShape );
235 void Add( T aShape,
const BOX2I& aBbox );
262 acceptVisitor( shape, aVisitor );
282 int Query(
const SHAPE *aShape,
int aMinDistance, V& aVisitor)
const
287 int min[2] = { box.GetX(), box.GetY() };
288 int max[2] = { box.GetRight(), box.GetBottom() };
290 return this->m_tree->
Search( min, max, aVisitor );
323 int min[2] = { aBbox.GetX(), aBbox.GetY() };
324 int max[2] = { aBbox.GetRight(), aBbox.GetBottom() };
326 this->m_tree->Insert( min, max, aShape );
332 BOX2I box = boundingBox( aShape );
333 int min[2] = { box.GetX(), box.GetY() };
334 int max[2] = { box.GetRight(), box.GetBottom() };
336 this->m_tree->Insert( min, max, aShape );
342 BOX2I box = boundingBox( aShape );
343 int min[2] = { box.GetX(), box.GetY() };
344 int max[2] = { box.GetRight(), box.GetBottom() };
346 this->m_tree->Remove( min, max, aShape );
352 this->m_tree->RemoveAll();
366 BOX2I box = boundingBox( shape );
367 int min[2] = { box.GetX(), box.GetY() };
368 int max[2] = { box.GetRight(), box.GetBottom() };
369 newTree->
Insert( min, max, shape );
374 this->m_tree = newTree;
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition box2.h:281
BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition box2.h:363
Iterator is not remove safe.
Definition rtree.h:248
Implementation of RTree, a multidimensional bounding rectangle tree.
Definition rtree.h:110
int Search(const ELEMTYPE a_min[NUMDIMS], const ELEMTYPE a_max[NUMDIMS], std::function< bool(const DATATYPE &)> a_callback) const
Find all within search rectangle.
void Insert(const ELEMTYPE a_min[NUMDIMS], const ELEMTYPE a_max[NUMDIMS], const DATATYPE &a_dataId)
Insert entry.
Definition shape_index.h:134
bool operator++()
Shift the iterator to the next element.
Definition shape_index.h:171
bool IsNotNull() const
Check if the iterator has not reached the end.
Definition shape_index.h:199
bool operator++(int)
Shift the iterator to the next element.
Definition shape_index.h:179
T Next()
Return the current element of the iterator and moves to the next position.
Definition shape_index.h:209
bool IsNull() const
Check if the iterator has reached the end.
Definition shape_index.h:189
T operator*()
Return the next data element.
Definition shape_index.h:163
Iterator(SHAPE_INDEX *aIndex)
Create an iterator for the index object.
Definition shape_index.h:155
Definition shape_index.h:131
void Remove(T aShape)
Remove a SHAPE from the index.
Definition shape_index.h:340
Iterator Begin()
Create an iterator for the current index object.
Definition shape_index.h:378
void RemoveAll()
Remove all the contents of the index.
Definition shape_index.h:350
void Accept(V aVisitor)
Accept a visitor for every SHAPE object contained in this INDEX.
Definition shape_index.h:255
void Reindex()
Rebuild the index.
Definition shape_index.h:356
void Add(T aShape)
Add a SHAPE to the index.
Definition shape_index.h:330
int Query(const SHAPE *aShape, int aMinDistance, V &aVisitor) const
Run a callback on every SHAPE object contained in the bounding box of (shape).
Definition shape_index.h:282
An abstract shape on 2D plane.
Definition shape.h:117
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const
Check if the boundary of shape (this) lies closer to the point aP than aClearance,...
Definition shape.h:165
virtual const BOX2I BBox(int aClearance=0) const =0
Compute a bounding box of the shape, with a margin of aClearance a collision.