2013-09-10 11:43:09 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 CERN
|
2021-07-01 17:46:17 +00:00
|
|
|
* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
2013-09-13 13:43:33 +00:00
|
|
|
* @author Jacobo Aragunde Pérez
|
2013-09-10 11:43:09 +00:00
|
|
|
* @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
|
|
|
|
|
2013-09-13 13:43:33 +00:00
|
|
|
#include <vector>
|
2013-09-10 11:43:09 +00:00
|
|
|
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <geometry/rtree.h>
|
|
|
|
#include <geometry/shape.h>
|
|
|
|
#include <math/box2.h>
|
2013-09-13 13:43:33 +00:00
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Used by #SHAPE_INDEX to get a SHAPE* from another type.
|
2013-09-13 13:43:33 +00:00
|
|
|
*
|
|
|
|
* By default relies on T::GetShape() method, should be specialized if the T object
|
|
|
|
* doesn't allow that method.
|
2021-07-01 17:46:17 +00:00
|
|
|
*
|
|
|
|
* @param aItem generic T object.
|
2013-09-13 13:43:33 +00:00
|
|
|
* @return a SHAPE* object equivalent to object.
|
|
|
|
*/
|
|
|
|
template <class T>
|
|
|
|
static const SHAPE* shapeFunctor( T aItem )
|
|
|
|
{
|
2014-05-14 11:53:02 +00:00
|
|
|
return aItem->Shape();
|
2013-09-13 13:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Used by #SHAPE_INDEX to get the bounding box of a generic T object.
|
2013-09-13 13:43:33 +00:00
|
|
|
*
|
|
|
|
* By default relies on T::BBox() method, should be specialized if the T object
|
|
|
|
* doesn't allow that method.
|
2021-07-01 17:46:17 +00:00
|
|
|
*
|
|
|
|
* @param aObject is a generic T object.
|
2013-09-13 13:43:33 +00:00
|
|
|
* @return a BOX2I object containing the bounding box of the T object.
|
|
|
|
*/
|
|
|
|
template <class T>
|
2013-09-27 16:51:21 +00:00
|
|
|
BOX2I boundingBox( T aObject )
|
2013-09-13 13:43:33 +00:00
|
|
|
{
|
2013-09-27 16:51:21 +00:00
|
|
|
return shapeFunctor( aObject )->BBox();
|
2013-09-13 13:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Used by #SHAPE_INDEX to implement Accept().
|
2013-09-13 13:43:33 +00:00
|
|
|
*
|
|
|
|
* By default relies on V::operation() redefinition, should be specialized if V class
|
|
|
|
* doesn't have its () operation defined to accept T objects.
|
2021-07-01 17:46:17 +00:00
|
|
|
*
|
|
|
|
* @param aObject is a generic T object.
|
|
|
|
* @param aVisitor is a visitor object.
|
2013-09-13 13:43:33 +00:00
|
|
|
*/
|
|
|
|
template <class T, class V>
|
2013-09-27 16:51:21 +00:00
|
|
|
void acceptVisitor( T aObject, V aVisitor )
|
2013-09-13 13:43:33 +00:00
|
|
|
{
|
2013-09-27 16:51:21 +00:00
|
|
|
aVisitor( aObject );
|
2013-09-13 13:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Used by #SHAPE_INDEX to implement Query().
|
2013-09-13 13:43:33 +00:00
|
|
|
*
|
|
|
|
* By default relies on T::Collide(U) method, should be specialized if the T object
|
|
|
|
* doesn't allow that method.
|
2021-07-01 17:46:17 +00:00
|
|
|
*
|
|
|
|
* @param aObject is a generic T object.
|
|
|
|
* @param aAnotherObject is a generic U object.
|
|
|
|
* @param aMinDistance is the minimum collision distance.
|
|
|
|
* @return true if object and anotherObject collide.
|
2013-09-13 13:43:33 +00:00
|
|
|
*/
|
2013-10-14 14:13:35 +00:00
|
|
|
template <class T, class U>
|
2013-09-27 16:51:21 +00:00
|
|
|
bool collide( T aObject, U aAnotherObject, int aMinDistance )
|
2013-09-10 11:43:09 +00:00
|
|
|
{
|
2013-09-27 16:51:21 +00:00
|
|
|
return shapeFunctor( aObject )->Collide( aAnotherObject, aMinDistance );
|
2013-09-13 13:43:33 +00:00
|
|
|
}
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
template <class T, class V>
|
2013-09-27 16:51:21 +00:00
|
|
|
bool queryCallback( T aShape, void* aContext )
|
2013-09-27 14:23:43 +00:00
|
|
|
{
|
2013-09-27 16:51:21 +00:00
|
|
|
V* visitor = (V*) aContext;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
|
|
|
acceptVisitor<T, V>( aShape, *visitor );
|
2013-09-27 14:23:43 +00:00
|
|
|
|
2013-09-13 13:43:33 +00:00
|
|
|
return true;
|
2013-09-10 11:43:09 +00:00
|
|
|
}
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
template <class T = SHAPE*>
|
2013-09-27 16:51:21 +00:00
|
|
|
class SHAPE_INDEX
|
|
|
|
{
|
2013-09-13 13:43:33 +00:00
|
|
|
public:
|
|
|
|
class Iterator
|
|
|
|
{
|
|
|
|
private:
|
2018-08-06 23:55:00 +00:00
|
|
|
typedef typename RTree<T, int, 2, double>::Iterator RTreeIterator;
|
2013-09-13 13:43:33 +00:00
|
|
|
RTreeIterator iterator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup the internal tree iterator.
|
2021-07-01 17:46:17 +00:00
|
|
|
*
|
|
|
|
* @param aTree is a #RTREE object/
|
2013-09-13 13:43:33 +00:00
|
|
|
*/
|
2018-08-06 23:55:00 +00:00
|
|
|
void Init( RTree<T, int, 2, double>* aTree )
|
2013-09-27 14:23:43 +00:00
|
|
|
{
|
2013-09-27 16:51:21 +00:00
|
|
|
aTree->GetFirst( iterator );
|
2013-09-13 13:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Create an iterator for the index object.
|
2013-09-13 13:43:33 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @param aIndex is a #SHAPE_INDEX object to iterate.
|
2013-09-13 13:43:33 +00:00
|
|
|
*/
|
2013-09-27 16:51:21 +00:00
|
|
|
Iterator( SHAPE_INDEX* aIndex )
|
2013-09-27 14:23:43 +00:00
|
|
|
{
|
2013-09-27 16:51:21 +00:00
|
|
|
Init( aIndex->m_tree );
|
2013-09-13 13:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Return the next data element.
|
2013-09-13 13:43:33 +00:00
|
|
|
*/
|
2013-09-27 14:23:43 +00:00
|
|
|
T operator*()
|
|
|
|
{
|
2013-09-13 13:43:33 +00:00
|
|
|
return *iterator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Shift the iterator to the next element.
|
2013-09-13 13:43:33 +00:00
|
|
|
*/
|
2013-09-27 14:23:43 +00:00
|
|
|
bool operator++()
|
|
|
|
{
|
2013-09-13 13:43:33 +00:00
|
|
|
return ++iterator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Shift the iterator to the next element.
|
2013-09-13 13:43:33 +00:00
|
|
|
*/
|
2013-09-27 14:23:43 +00:00
|
|
|
bool operator++( int )
|
|
|
|
{
|
2013-09-13 13:43:33 +00:00
|
|
|
return ++iterator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Check if the iterator has reached the end.
|
2013-09-13 13:43:33 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @return true if it is in an invalid position (data finished).
|
2013-09-13 13:43:33 +00:00
|
|
|
*/
|
2020-10-27 11:03:35 +00:00
|
|
|
bool IsNull() const
|
2013-09-27 14:23:43 +00:00
|
|
|
{
|
2013-09-13 13:43:33 +00:00
|
|
|
return iterator.IsNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Check if the iterator has not reached the end.
|
2013-09-13 13:43:33 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @return true if it is in an valid position (data not finished).
|
2013-09-13 13:43:33 +00:00
|
|
|
*/
|
2020-10-27 11:03:35 +00:00
|
|
|
bool IsNotNull() const
|
2013-09-27 14:23:43 +00:00
|
|
|
{
|
2013-09-13 13:43:33 +00:00
|
|
|
return iterator.IsNotNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Return the current element of the iterator and moves to the next position.
|
2013-09-13 13:43:33 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @return a #SHAPE object pointed by the iterator before moving to the next position.
|
2013-09-13 13:43:33 +00:00
|
|
|
*/
|
2013-09-27 14:23:43 +00:00
|
|
|
T Next()
|
|
|
|
{
|
2013-09-13 13:43:33 +00:00
|
|
|
T object = *iterator;
|
|
|
|
++iterator;
|
2013-09-27 14:23:43 +00:00
|
|
|
|
2013-09-13 13:43:33 +00:00
|
|
|
return object;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-09-23 15:02:25 +00:00
|
|
|
SHAPE_INDEX();
|
|
|
|
|
|
|
|
~SHAPE_INDEX();
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Add a #SHAPE to the index.
|
2013-09-23 15:02:25 +00:00
|
|
|
*
|
2013-09-27 14:23:43 +00:00
|
|
|
* @param aShape is the new SHAPE.
|
2013-09-23 15:02:25 +00:00
|
|
|
*/
|
2013-09-27 14:23:43 +00:00
|
|
|
void Add( T aShape );
|
2013-09-23 15:02:25 +00:00
|
|
|
|
2020-08-16 19:36:58 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Add a shape with alternate BBox.
|
|
|
|
*
|
|
|
|
* @param aShape Shape (Item) to add.
|
2020-08-16 19:36:58 +00:00
|
|
|
* @param aBbox alternate bounding box. This should be a subset of the item's bbox
|
|
|
|
*/
|
|
|
|
void Add( T aShape, const BOX2I& aBbox );
|
|
|
|
|
2013-09-23 15:02:25 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Remove a #SHAPE from the index.
|
2013-09-23 15:02:25 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @param aShape is the #SHAPE to remove.
|
2013-09-23 15:02:25 +00:00
|
|
|
*/
|
2013-09-27 14:23:43 +00:00
|
|
|
void Remove( T aShape );
|
2013-09-23 15:02:25 +00:00
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Remove all the contents of the index.
|
2013-09-23 15:02:25 +00:00
|
|
|
*/
|
|
|
|
void RemoveAll();
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Accept a visitor for every #SHAPE object contained in this INDEX.
|
2013-09-23 15:02:25 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @param aVisitor is the visitor object to be run.
|
2013-09-23 15:02:25 +00:00
|
|
|
*/
|
2013-10-14 14:13:35 +00:00
|
|
|
template <class V>
|
2013-09-27 14:23:43 +00:00
|
|
|
void Accept( V aVisitor )
|
2013-09-23 15:02:25 +00:00
|
|
|
{
|
|
|
|
Iterator iter = this->Begin();
|
2013-09-27 14:23:43 +00:00
|
|
|
|
|
|
|
while( !iter.IsNull() )
|
|
|
|
{
|
2013-09-23 15:02:25 +00:00
|
|
|
T shape = *iter;
|
2013-09-27 14:23:43 +00:00
|
|
|
acceptVisitor( shape, aVisitor );
|
2013-09-23 15:02:25 +00:00
|
|
|
iter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Rebuild the index.
|
2013-09-23 15:02:25 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* This should be used if the geometry of the objects contained by the index has changed.
|
2013-09-23 15:02:25 +00:00
|
|
|
*/
|
|
|
|
void Reindex();
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Run a callback on every #SHAPE object contained in the bounding box of (shape).
|
2013-09-23 15:02:25 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @param aShape is the shape to search against.
|
|
|
|
* @param aMinDistance is the distance threshold.
|
|
|
|
* @param aVisitor is the object to be invoked on every object contained in the search area.
|
2013-09-23 15:02:25 +00:00
|
|
|
*/
|
2013-10-14 14:13:35 +00:00
|
|
|
template <class V>
|
2020-10-27 11:03:35 +00:00
|
|
|
int Query( const SHAPE *aShape, int aMinDistance, V& aVisitor) const
|
2013-09-23 15:02:25 +00:00
|
|
|
{
|
2013-09-27 14:23:43 +00:00
|
|
|
BOX2I box = aShape->BBox();
|
|
|
|
box.Inflate( aMinDistance );
|
2013-09-23 15:02:25 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
int min[2] = { box.GetX(), box.GetY() };
|
|
|
|
int max[2] = { box.GetRight(), box.GetBottom() };
|
|
|
|
|
|
|
|
return this->m_tree->Search( min, max, aVisitor );
|
2013-09-23 15:02:25 +00:00
|
|
|
}
|
|
|
|
|
2013-09-13 13:43:33 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Create an iterator for the current index object.
|
2013-09-13 13:43:33 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @return iterator to the first object.
|
2013-09-13 13:43:33 +00:00
|
|
|
*/
|
|
|
|
Iterator Begin();
|
|
|
|
|
|
|
|
private:
|
2018-08-06 23:55:00 +00:00
|
|
|
RTree<T, int, 2, double>* m_tree;
|
2013-09-10 11:43:09 +00:00
|
|
|
};
|
|
|
|
|
2013-09-13 13:43:33 +00:00
|
|
|
/*
|
|
|
|
* Class members implementation
|
|
|
|
*/
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
template <class T>
|
2013-09-27 14:23:43 +00:00
|
|
|
SHAPE_INDEX<T>::SHAPE_INDEX()
|
|
|
|
{
|
2018-08-06 23:55:00 +00:00
|
|
|
this->m_tree = new RTree<T, int, 2, double>();
|
2013-09-13 13:43:33 +00:00
|
|
|
}
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
template <class T>
|
2013-09-27 14:23:43 +00:00
|
|
|
SHAPE_INDEX<T>::~SHAPE_INDEX()
|
|
|
|
{
|
2013-09-13 13:43:33 +00:00
|
|
|
delete this->m_tree;
|
|
|
|
}
|
|
|
|
|
2020-08-16 19:36:58 +00:00
|
|
|
template <class T>
|
|
|
|
void SHAPE_INDEX<T>::Add( T aShape, const BOX2I& aBbox )
|
|
|
|
{
|
|
|
|
int min[2] = { aBbox.GetX(), aBbox.GetY() };
|
|
|
|
int max[2] = { aBbox.GetRight(), aBbox.GetBottom() };
|
|
|
|
|
|
|
|
this->m_tree->Insert( min, max, aShape );
|
|
|
|
}
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
template <class T>
|
2013-09-27 14:23:43 +00:00
|
|
|
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 );
|
2013-09-13 13:43:33 +00:00
|
|
|
}
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
template <class T>
|
2013-09-27 16:51:21 +00:00
|
|
|
void SHAPE_INDEX<T>::Remove( T aShape )
|
2013-09-27 14:23:43 +00:00
|
|
|
{
|
|
|
|
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 );
|
2013-09-13 13:43:33 +00:00
|
|
|
}
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
template <class T>
|
2013-09-27 14:23:43 +00:00
|
|
|
void SHAPE_INDEX<T>::RemoveAll()
|
|
|
|
{
|
2013-09-13 13:43:33 +00:00
|
|
|
this->m_tree->RemoveAll();
|
|
|
|
}
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
template <class T>
|
2013-09-27 14:23:43 +00:00
|
|
|
void SHAPE_INDEX<T>::Reindex()
|
|
|
|
{
|
2018-08-06 23:55:00 +00:00
|
|
|
RTree<T, int, 2, double>* newTree;
|
|
|
|
newTree = new RTree<T, int, 2, double>();
|
2013-09-13 13:43:33 +00:00
|
|
|
|
2013-09-23 15:02:25 +00:00
|
|
|
Iterator iter = this->Begin();
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-09-27 14:23:43 +00:00
|
|
|
while( !iter.IsNull() )
|
|
|
|
{
|
2013-09-13 13:43:33 +00:00
|
|
|
T shape = *iter;
|
2013-09-27 14:23:43 +00:00
|
|
|
BOX2I box = boundingBox( shape );
|
|
|
|
int min[2] = { box.GetX(), box.GetY() };
|
|
|
|
int max[2] = { box.GetRight(), box.GetBottom() };
|
|
|
|
newTree->Insert( min, max, shape );
|
2013-09-13 13:43:33 +00:00
|
|
|
iter++;
|
|
|
|
}
|
2013-09-27 14:23:43 +00:00
|
|
|
|
2013-09-13 13:43:33 +00:00
|
|
|
delete this->m_tree;
|
|
|
|
this->m_tree = newTree;
|
|
|
|
}
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
template <class T>
|
2013-09-27 14:23:43 +00:00
|
|
|
typename SHAPE_INDEX<T>::Iterator SHAPE_INDEX<T>::Begin()
|
|
|
|
{
|
|
|
|
return Iterator( this );
|
2013-09-13 13:43:33 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 11:03:35 +00:00
|
|
|
#endif /* __SHAPE_INDEX_H */
|