2013-09-18 17:55:16 +00:00
|
|
|
/*
|
|
|
|
* KiRouter - a push-and-(sometimes-)shove PCB router
|
|
|
|
*
|
2014-05-14 13:53:54 +00:00
|
|
|
* Copyright (C) 2013-2014 CERN
|
2016-08-29 14:38:11 +00:00
|
|
|
* Copyright (C) 2016 KiCad Developers, see AUTHORS.txt for contributors.
|
2013-09-18 17:55:16 +00:00
|
|
|
* Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
2013-09-26 21:53:54 +00:00
|
|
|
*
|
2013-09-18 17:55:16 +00:00
|
|
|
* 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 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
2013-09-26 21:53:54 +00:00
|
|
|
*
|
2013-09-18 17:55:16 +00:00
|
|
|
* 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.
|
2013-09-26 21:53:54 +00:00
|
|
|
*
|
2013-09-18 17:55:16 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
2014-05-14 13:53:54 +00:00
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
2013-09-18 17:55:16 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __PNS_INDEX_H
|
|
|
|
#define __PNS_INDEX_H
|
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
#include <layers_id_colors_and_visibility.h>
|
|
|
|
#include <map>
|
2017-11-01 11:14:16 +00:00
|
|
|
#include <unordered_set>
|
2014-06-24 16:17:18 +00:00
|
|
|
|
2013-09-18 17:55:16 +00:00
|
|
|
#include <boost/range/adaptor/map.hpp>
|
|
|
|
|
|
|
|
#include <list>
|
|
|
|
#include <geometry/shape_index.h>
|
|
|
|
|
|
|
|
#include "pns_item.h"
|
|
|
|
|
2016-08-29 14:38:11 +00:00
|
|
|
namespace PNS {
|
|
|
|
|
|
|
|
|
2013-09-18 17:55:16 +00:00
|
|
|
/**
|
2016-08-29 17:31:13 +00:00
|
|
|
* Class INDEX
|
2013-09-18 17:55:16 +00:00
|
|
|
*
|
|
|
|
* Custom spatial index, holding our board items and allowing for very fast searches. Items
|
2014-05-14 13:53:54 +00:00
|
|
|
* are assigned to separate R-Tree subindices depending on their type and spanned layers, reducing
|
2013-09-18 17:55:16 +00:00
|
|
|
* overlap and improving search time.
|
|
|
|
**/
|
2016-08-29 17:31:13 +00:00
|
|
|
class INDEX
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2013-09-18 17:55:16 +00:00
|
|
|
public:
|
2016-08-29 17:31:13 +00:00
|
|
|
typedef std::list<ITEM*> NET_ITEMS_LIST;
|
|
|
|
typedef SHAPE_INDEX<ITEM*> ITEM_SHAPE_INDEX;
|
2017-11-01 11:14:16 +00:00
|
|
|
typedef std::unordered_set<ITEM*> ITEM_SET;
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
INDEX();
|
|
|
|
~INDEX();
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2014-06-24 16:17:18 +00:00
|
|
|
/**
|
2014-05-14 13:53:54 +00:00
|
|
|
* Function Add()
|
|
|
|
*
|
|
|
|
* Adds item to the spatial index.
|
|
|
|
*/
|
2016-08-29 17:31:13 +00:00
|
|
|
void Add( ITEM* aItem );
|
2014-06-24 16:17:18 +00:00
|
|
|
|
|
|
|
/**
|
2014-05-14 13:53:54 +00:00
|
|
|
* Function Remove()
|
|
|
|
*
|
|
|
|
* Removes an item from the spatial index.
|
|
|
|
*/
|
2016-08-29 17:31:13 +00:00
|
|
|
void Remove( ITEM* aItem );
|
2014-05-14 13:53:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function Add()
|
|
|
|
*
|
|
|
|
* Replaces one item with another.
|
|
|
|
*/
|
2016-08-29 17:31:13 +00:00
|
|
|
void Replace( ITEM* aOldItem, ITEM* aNewItem );
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2014-06-24 16:17:18 +00:00
|
|
|
/**
|
2014-05-14 13:53:54 +00:00
|
|
|
* Function Query()
|
|
|
|
*
|
|
|
|
* Searches items in the index that are in proximity of aItem.
|
|
|
|
* For each item, function object aVisitor is called. Only items on
|
|
|
|
* overlapping layers are considered.
|
|
|
|
*
|
|
|
|
* @param aItem item to search against
|
|
|
|
* @param aMinDistance proximity distance (wrs to the item's shape)
|
|
|
|
* @param aVisitor function object called on each found item. Return
|
|
|
|
false from the visitor to stop searching.
|
|
|
|
* @return number of items found.
|
|
|
|
*/
|
2013-09-26 21:53:54 +00:00
|
|
|
template<class Visitor>
|
2016-08-29 17:31:13 +00:00
|
|
|
int Query( const ITEM* aItem, int aMinDistance, Visitor& aVisitor );
|
2014-05-14 13:53:54 +00:00
|
|
|
|
2014-06-24 16:17:18 +00:00
|
|
|
/**
|
2014-05-14 13:53:54 +00:00
|
|
|
* Function Query()
|
|
|
|
*
|
|
|
|
* Searches items in the index that are in proximity of aShape.
|
|
|
|
* For each item, function object aVisitor is called. Treats all
|
|
|
|
* layers as colliding.
|
|
|
|
*
|
|
|
|
* @param aShape shape to search against
|
|
|
|
* @param aMinDistance proximity distance (wrs to the item's shape)
|
|
|
|
* @param aVisitor function object called on each found item. Return
|
|
|
|
false from the visitor to stop searching.
|
|
|
|
* @return number of items found.
|
|
|
|
*/
|
2013-09-26 21:53:54 +00:00
|
|
|
template<class Visitor>
|
2014-05-14 13:53:54 +00:00
|
|
|
int Query( const SHAPE* aShape, int aMinDistance, Visitor& aVisitor );
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
/**
|
|
|
|
* Function Clear()
|
|
|
|
*
|
|
|
|
* Removes all items from the index.
|
|
|
|
*/
|
2013-09-26 21:53:54 +00:00
|
|
|
void Clear();
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
/**
|
|
|
|
* Function GetItemsForNet()
|
|
|
|
*
|
|
|
|
* Returns list of all items in a given net.
|
|
|
|
*/
|
2014-05-16 11:37:31 +00:00
|
|
|
NET_ITEMS_LIST* GetItemsForNet( int aNet );
|
2014-06-24 16:17:18 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
/**
|
|
|
|
* Function Contains()
|
|
|
|
*
|
|
|
|
* Returns true if item aItem exists in the index.
|
|
|
|
*/
|
2016-08-29 17:31:13 +00:00
|
|
|
bool Contains( ITEM* aItem ) const
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
|
|
|
return m_allItems.find( aItem ) != m_allItems.end();
|
|
|
|
}
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
/**
|
|
|
|
* Function Size()
|
2014-06-24 16:17:18 +00:00
|
|
|
*
|
2014-05-14 13:53:54 +00:00
|
|
|
* Returns number of items stored in the index.
|
|
|
|
*/
|
2013-09-26 21:53:54 +00:00
|
|
|
int Size() const { return m_allItems.size(); }
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
ITEM_SET::iterator begin() { return m_allItems.begin(); }
|
|
|
|
ITEM_SET::iterator end() { return m_allItems.end(); }
|
2014-05-14 13:53:54 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
private:
|
2014-06-25 04:36:02 +00:00
|
|
|
static const int MaxSubIndices = 128;
|
2013-09-26 21:53:54 +00:00
|
|
|
static const int SI_Multilayer = 2;
|
|
|
|
static const int SI_SegDiagonal = 0;
|
|
|
|
static const int SI_SegStraight = 1;
|
2014-05-16 11:37:31 +00:00
|
|
|
static const int SI_Traces = 3;
|
|
|
|
static const int SI_PadsTop = 0;
|
|
|
|
static const int SI_PadsBottom = 1;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
template <class Visitor>
|
2014-05-14 13:53:54 +00:00
|
|
|
int querySingle( int index, const SHAPE* aShape, int aMinDistance, Visitor& aVisitor );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
ITEM_SHAPE_INDEX* getSubindex( const ITEM* aItem );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
ITEM_SHAPE_INDEX* m_subIndices[MaxSubIndices];
|
|
|
|
std::map<int, NET_ITEMS_LIST> m_netMap;
|
|
|
|
ITEM_SET m_allItems;
|
2013-09-18 17:55:16 +00:00
|
|
|
};
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
INDEX::INDEX()
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2013-09-26 21:53:54 +00:00
|
|
|
memset( m_subIndices, 0, sizeof( m_subIndices ) );
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
INDEX::ITEM_SHAPE_INDEX* INDEX::getSubindex( const ITEM* aItem )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2013-09-26 21:53:54 +00:00
|
|
|
int idx_n = -1;
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
const LAYER_RANGE l = aItem->Layers();
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
switch( aItem->Kind() )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
case ITEM::VIA_T:
|
2013-09-26 21:53:54 +00:00
|
|
|
idx_n = SI_Multilayer;
|
|
|
|
break;
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
case ITEM::SOLID_T:
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
|
|
|
if( l.IsMultilayer() )
|
|
|
|
idx_n = SI_Multilayer;
|
2014-06-24 16:17:18 +00:00
|
|
|
else if( l.Start() == B_Cu ) // fixme: use kicad layer codes
|
2013-09-26 21:53:54 +00:00
|
|
|
idx_n = SI_PadsTop;
|
2014-06-24 16:17:18 +00:00
|
|
|
else if( l.Start() == F_Cu )
|
2013-09-26 21:53:54 +00:00
|
|
|
idx_n = SI_PadsBottom;
|
2018-02-03 09:39:46 +00:00
|
|
|
else
|
|
|
|
idx_n = SI_Traces + 2 * l.Start() + SI_SegStraight;
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
2014-06-24 16:17:18 +00:00
|
|
|
break;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
case ITEM::SEGMENT_T:
|
|
|
|
case ITEM::LINE_T:
|
2013-09-26 21:53:54 +00:00
|
|
|
idx_n = SI_Traces + 2 * l.Start() + SI_SegStraight;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-11 21:18:45 +00:00
|
|
|
if( idx_n < 0 || idx_n >= MaxSubIndices )
|
|
|
|
{
|
2017-10-16 18:26:37 +00:00
|
|
|
wxASSERT( idx_n >= 0 );
|
|
|
|
wxASSERT( idx_n < MaxSubIndices );
|
2017-03-11 21:18:45 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
if( !m_subIndices[idx_n] )
|
2014-05-16 11:37:31 +00:00
|
|
|
m_subIndices[idx_n] = new ITEM_SHAPE_INDEX;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
return m_subIndices[idx_n];
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
void INDEX::Add( ITEM* aItem )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
ITEM_SHAPE_INDEX* idx = getSubindex( aItem );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2017-03-11 21:18:45 +00:00
|
|
|
if( !idx )
|
|
|
|
return;
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
idx->Add( aItem );
|
|
|
|
m_allItems.insert( aItem );
|
2014-05-14 13:53:54 +00:00
|
|
|
int net = aItem->Net();
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
if( net >= 0 )
|
|
|
|
{
|
|
|
|
m_netMap[net].push_back( aItem );
|
|
|
|
}
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
void INDEX::Remove( ITEM* aItem )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
ITEM_SHAPE_INDEX* idx = getSubindex( aItem );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2017-03-11 21:18:45 +00:00
|
|
|
if( !idx )
|
|
|
|
return;
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
idx->Remove( aItem );
|
|
|
|
m_allItems.erase( aItem );
|
2014-05-14 13:53:54 +00:00
|
|
|
int net = aItem->Net();
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
if( net >= 0 && m_netMap.find( net ) != m_netMap.end() )
|
|
|
|
m_netMap[net].remove( aItem );
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
void INDEX::Replace( ITEM* aOldItem, ITEM* aNewItem )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2013-09-26 21:53:54 +00:00
|
|
|
Remove( aOldItem );
|
|
|
|
Add( aNewItem );
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class Visitor>
|
2016-08-29 17:31:13 +00:00
|
|
|
int INDEX::querySingle( int index, const SHAPE* aShape, int aMinDistance, Visitor& aVisitor )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
|
|
|
if( !m_subIndices[index] )
|
|
|
|
return 0;
|
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
return m_subIndices[index]->Query( aShape, aMinDistance, aVisitor, false );
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
|
|
|
|
2013-09-18 17:55:16 +00:00
|
|
|
template<class Visitor>
|
2016-08-29 17:31:13 +00:00
|
|
|
int INDEX::Query( const ITEM* aItem, int aMinDistance, Visitor& aVisitor )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2014-05-14 13:53:54 +00:00
|
|
|
const SHAPE* shape = aItem->Shape();
|
2013-09-26 21:53:54 +00:00
|
|
|
int total = 0;
|
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
total += querySingle( SI_Multilayer, shape, aMinDistance, aVisitor );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
const LAYER_RANGE layers = aItem->Layers();
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
if( layers.IsMultilayer() )
|
|
|
|
{
|
2014-05-14 13:53:54 +00:00
|
|
|
total += querySingle( SI_PadsTop, shape, aMinDistance, aVisitor );
|
|
|
|
total += querySingle( SI_PadsBottom, shape, aMinDistance, aVisitor );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
for( int i = layers.Start(); i <= layers.End(); ++i )
|
2014-05-14 13:53:54 +00:00
|
|
|
total += querySingle( SI_Traces + 2 * i + SI_SegStraight, shape, aMinDistance, aVisitor );
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int l = layers.Start();
|
|
|
|
|
2014-06-24 16:17:18 +00:00
|
|
|
if( l == B_Cu )
|
2014-05-14 13:53:54 +00:00
|
|
|
total += querySingle( SI_PadsTop, shape, aMinDistance, aVisitor );
|
2014-06-24 16:17:18 +00:00
|
|
|
else if( l == F_Cu )
|
2014-05-14 13:53:54 +00:00
|
|
|
total += querySingle( SI_PadsBottom, shape, aMinDistance, aVisitor );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
total += querySingle( SI_Traces + 2 * l + SI_SegStraight, shape, aMinDistance, aVisitor );
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2013-09-18 17:55:16 +00:00
|
|
|
template<class Visitor>
|
2016-08-29 17:31:13 +00:00
|
|
|
int INDEX::Query( const SHAPE* aShape, int aMinDistance, Visitor& aVisitor )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
|
|
|
int total = 0;
|
|
|
|
|
|
|
|
for( int i = 0; i < MaxSubIndices; i++ )
|
2014-05-14 13:53:54 +00:00
|
|
|
total += querySingle( i, aShape, aMinDistance, aVisitor );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
void INDEX::Clear()
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2013-09-26 21:53:54 +00:00
|
|
|
for( int i = 0; i < MaxSubIndices; ++i )
|
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
ITEM_SHAPE_INDEX* idx = m_subIndices[i];
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
if( idx )
|
|
|
|
delete idx;
|
|
|
|
|
|
|
|
m_subIndices[i] = NULL;
|
|
|
|
}
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
INDEX::~INDEX()
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2013-09-26 21:53:54 +00:00
|
|
|
Clear();
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
INDEX::NET_ITEMS_LIST* INDEX::GetItemsForNet( int aNet )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2013-09-26 21:53:54 +00:00
|
|
|
if( m_netMap.find( aNet ) == m_netMap.end() )
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return &m_netMap[aNet];
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 14:38:11 +00:00
|
|
|
}
|
|
|
|
|
2013-09-18 17:55:16 +00:00
|
|
|
#endif
|