/* * 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 * * 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 #include #include /** * 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 object generic T object * @return a SHAPE* object equivalent to object. */ template static const SHAPE* shapeFunctor( T aItem ) { return aItem->GetShape(); } /** * shapeFunctor template function: specialization for T = SHAPE* */ template<> const SHAPE* shapeFunctor( SHAPE* aItem ) { return aItem; } /** * 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 object generic T object * @return a BOX2I object containing the bounding box of the T object. */ template BOX2I boundingBox( T object ) { return shapeFunctor(object)->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 object generic T object * @param visitor V visitor object */ template void acceptVisitor( T object, V visitor ) { visitor(object); } /** * 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 object generic T object * @param anotherObject generic U object * @param minDistance minimum collision distance * @return if object and anotherObject collide */ template bool collide( T object, U anotherObject, int minDistance ) { return shapeFunctor(object)->Collide( anotherObject, minDistance ); } template bool queryCallback(T shape, void* context) { V* visitor = (V*) context; acceptVisitor(shape, *visitor); return true; } template class SHAPE_INDEX { public: SHAPE_INDEX(); ~SHAPE_INDEX(); /** * Function Add() * * Adds a SHAPE to the index. * @param shape the new SHAPE */ void Add( T shape ); /** * Function Remove() * * Removes a SHAPE to the index. * @param shape the new SHAPE */ void Remove( T shape ); /** * 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 visitor Visitor object to be run */ template void Accept( V visitor ) { SHAPE_INDEX::Iterator iter = this->Begin(); while(!iter.IsNull()) { T shape = *iter; acceptVisitor(shape, visitor); 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 shape shape to search against * @param minDistance distance threshold * @param visitor object to be invoked on every object contained in the search area. */ template int Query( const SHAPE *shape, int minDistance, V& visitor, bool aExact ) { BOX2I box = shape->BBox(); box.Inflate(minDistance); int min[2] = {box.GetX(), box.GetY()}; int max[2] = {box.GetRight(), box.GetBottom()}; return this->m_tree->Search(min, max, visitor); } class Iterator { private: typedef typename RTree::Iterator RTreeIterator; RTreeIterator iterator; /** * Function Init() * * Setup the internal tree iterator. * @param tree pointer to a RTREE object */ void Init(RTree* tree) { tree->GetFirst(iterator); } public: /** * Iterator constructor * * Creates an iterator for the index object * @param index SHAPE_INDEX object to iterate */ Iterator(SHAPE_INDEX* index) { Init(index->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; } }; /** * Function Begin() * * Creates an iterator for the current index object * @return iterator */ Iterator Begin(); private: RTree* m_tree; }; /* * Class members implementation */ template SHAPE_INDEX::SHAPE_INDEX() { this->m_tree = new RTree(); } template SHAPE_INDEX::~SHAPE_INDEX() { delete this->m_tree; } template void SHAPE_INDEX::Add(T shape) { BOX2I box = boundingBox(shape); int min[2]= {box.GetX(), box.GetY()}; int max[2] = {box.GetRight(), box.GetBottom()}; this->m_tree->Insert(min, max, shape); } template void SHAPE_INDEX::Remove(T shape) { BOX2I box = boundingBox(shape); int min[2]= {box.GetX(), box.GetY()}; int max[2] = {box.GetRight(), box.GetBottom()}; this->m_tree->Remove(min, max, shape); } template void SHAPE_INDEX::RemoveAll() { this->m_tree->RemoveAll(); } template void SHAPE_INDEX::Reindex() { RTree* newTree; newTree = new RTree(); SHAPE_INDEX::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 typename SHAPE_INDEX::Iterator SHAPE_INDEX::Begin() { return Iterator(this); } #endif