kicad/libs/kimath/include/geometry/shape_index_list.h

296 lines
7.0 KiB
C
Raw Normal View History

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