2013-09-13 13:43:33 +00:00
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
template <class T>
|
|
|
|
const SHAPE* defaultShapeFunctor( const T aItem )
|
2013-09-13 13:43:33 +00:00
|
|
|
{
|
2013-10-14 11:43:57 +00:00
|
|
|
return aItem->GetShape();
|
2013-09-13 13:43:33 +00:00
|
|
|
}
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
template <class T, const SHAPE* (ShapeFunctor) (const T) = defaultShapeFunctor<T> >
|
|
|
|
class SHAPE_INDEX_LIST
|
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
struct SHAPE_ENTRY
|
2013-10-14 14:13:35 +00:00
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
SHAPE_ENTRY( T aParent )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
|
|
|
shape = ShapeFunctor( aParent );
|
|
|
|
bbox = shape->BBox( 0 );
|
|
|
|
parent = aParent;
|
|
|
|
}
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
~SHAPE_ENTRY()
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
T parent;
|
|
|
|
const SHAPE* shape;
|
|
|
|
BOX2I bbox;
|
|
|
|
};
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
typedef std::vector<SHAPE_ENTRY> SHAPE_VEC;
|
|
|
|
typedef typename std::vector<SHAPE_ENTRY>::iterator SHAPE_VEC_ITER;
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-09-13 13:43:33 +00:00
|
|
|
public:
|
2013-10-14 11:43:57 +00:00
|
|
|
// "Normal" iterator interface, for STL algorithms.
|
2013-10-14 14:13:35 +00:00
|
|
|
class iterator
|
|
|
|
{
|
|
|
|
public:
|
2013-10-14 18:40:36 +00:00
|
|
|
iterator()
|
|
|
|
{}
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
iterator( SHAPE_VEC_ITER aCurrent ) :
|
|
|
|
m_current( aCurrent )
|
|
|
|
{}
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
iterator( const iterator& aB ) :
|
2013-10-14 18:40:36 +00:00
|
|
|
m_current( aB.m_current )
|
|
|
|
{}
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
T operator*() const
|
|
|
|
{
|
|
|
|
return (*m_current).parent;
|
|
|
|
}
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
void operator++()
|
|
|
|
{
|
|
|
|
++m_current;
|
|
|
|
}
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
iterator& operator++( int aDummy )
|
|
|
|
{
|
|
|
|
++m_current;
|
|
|
|
return *this;
|
|
|
|
}
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
bool operator==( const iterator& aRhs ) const
|
|
|
|
{
|
|
|
|
return m_current == aRhs.m_current;
|
|
|
|
}
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
bool operator!=( const iterator& aRhs ) const
|
|
|
|
{
|
|
|
|
return m_current != aRhs.m_current;
|
|
|
|
}
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
const iterator& operator=( const iterator& aRhs )
|
|
|
|
{
|
|
|
|
m_current = aRhs.m_current;
|
|
|
|
return *this;
|
|
|
|
}
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
private:
|
2013-10-14 18:40:36 +00:00
|
|
|
SHAPE_VEC_ITER m_current;
|
2013-10-14 11:43:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// "Query" iterator, for iterating over a set of spatially matching shapes.
|
2013-10-14 14:13:35 +00:00
|
|
|
class query_iterator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
query_iterator()
|
|
|
|
{
|
|
|
|
}
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
query_iterator( SHAPE_VEC_ITER aCurrent, SHAPE_VEC_ITER aEnd, SHAPE* aShape,
|
2013-10-14 14:13:35 +00:00
|
|
|
int aMinDistance, bool aExact ) :
|
|
|
|
m_end( aEnd ),
|
|
|
|
m_current( aCurrent ),
|
|
|
|
m_shape( aShape ),
|
|
|
|
m_minDistance( aMinDistance ),
|
|
|
|
m_exact( aExact )
|
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
if( aShape )
|
|
|
|
{
|
|
|
|
m_refBBox = aShape->BBox();
|
|
|
|
next();
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
2013-10-14 18:40:36 +00:00
|
|
|
}
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
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 )
|
|
|
|
{
|
|
|
|
}
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
T operator*() const
|
|
|
|
{
|
|
|
|
return (*m_current).parent;
|
|
|
|
}
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
query_iterator& operator++()
|
|
|
|
{
|
|
|
|
++m_current;
|
|
|
|
next();
|
|
|
|
return *this;
|
|
|
|
}
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
query_iterator& operator++( int aDummy )
|
|
|
|
{
|
|
|
|
++m_current;
|
|
|
|
next();
|
|
|
|
return *this;
|
|
|
|
}
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
bool operator==( const query_iterator& aRhs ) const
|
|
|
|
{
|
|
|
|
return m_current == aRhs.m_current;
|
|
|
|
}
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
bool operator!=( const query_iterator& aRhs ) const
|
|
|
|
{
|
|
|
|
return m_current != aRhs.m_current;
|
|
|
|
}
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
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;
|
|
|
|
}
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
private:
|
|
|
|
void next()
|
|
|
|
{
|
|
|
|
while( m_current != m_end )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
if( m_refBBox.Distance( m_current->bbox ) <= m_minDistance )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
if( !m_exact || m_current->shape->Collide( m_shape, m_minDistance ) )
|
|
|
|
return;
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
2013-10-14 18:40:36 +00:00
|
|
|
|
|
|
|
++m_current;
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
2013-10-14 18:40:36 +00:00
|
|
|
}
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
SHAPE_VEC_ITER m_end;
|
|
|
|
SHAPE_VEC_ITER m_current;
|
2013-10-14 14:13:35 +00:00
|
|
|
BOX2I m_refBBox;
|
|
|
|
bool m_exact;
|
|
|
|
SHAPE* m_shape;
|
|
|
|
int m_minDistance;
|
2013-10-14 11:43:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void Add( T aItem )
|
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
SHAPE_ENTRY s( aItem );
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
m_shapes.push_back( s );
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Remove( const T aItem )
|
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
SHAPE_VEC_ITER i;
|
2013-10-14 11:43:57 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
template <class Visitor>
|
|
|
|
int Query( const SHAPE* aShape, int aMinDistance, Visitor& aV, bool aExact = true ) // const
|
2013-09-27 16:51:21 +00:00
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
SHAPE_VEC_ITER i;
|
2013-09-27 16:51:21 +00:00
|
|
|
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++;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-09-27 16:51:21 +00:00
|
|
|
if( !aV( i->parent ) )
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-09-27 16:51:21 +00:00
|
|
|
return n;
|
|
|
|
}
|
2013-09-13 13:43:33 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
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()
|
|
|
|
{
|
2013-10-14 14:13:35 +00:00
|
|
|
return query_iterator( m_shapes.end(), m_shapes.end(), NULL, 0, false );
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
iterator begin()
|
|
|
|
{
|
|
|
|
return iterator( m_shapes.begin() );
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator end()
|
|
|
|
{
|
|
|
|
return iterator( m_shapes.end() );
|
|
|
|
}
|
2013-09-13 13:43:33 +00:00
|
|
|
|
|
|
|
private:
|
2013-10-14 18:40:36 +00:00
|
|
|
SHAPE_VEC m_shapes;
|
2013-09-13 13:43:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|