Revert "Keep PNS item RTree on layer-by-layer"

This reverts commit a34d33c460.

The commit did not work properly for shove routing.  Will revist in
later commit
This commit is contained in:
Seth Hillbrand 2020-07-28 20:13:18 -07:00
parent 0fecb5f277
commit ad94b6205a
2 changed files with 143 additions and 23 deletions

View File

@ -23,35 +23,91 @@
namespace PNS {
INDEX::INDEX()
{
memset( m_subIndices, 0, sizeof( m_subIndices ) );
}
INDEX::~INDEX()
{
Clear();
}
INDEX::ITEM_SHAPE_INDEX* INDEX::getSubindex( const ITEM* aItem )
{
int idx_n = -1;
const LAYER_RANGE& l = aItem->Layers();
switch( aItem->Kind() )
{
case ITEM::VIA_T:
idx_n = SI_Multilayer;
break;
case ITEM::SOLID_T:
{
if( l.IsMultilayer() )
idx_n = SI_Multilayer;
else if( l.Start() == B_Cu ) // fixme: use kicad layer codes
idx_n = SI_PadsTop;
else if( l.Start() == F_Cu )
idx_n = SI_PadsBottom;
else
idx_n = SI_Traces + 2 * l.Start() + SI_SegStraight;
}
break;
case ITEM::ARC_T:
case ITEM::SEGMENT_T:
case ITEM::LINE_T:
idx_n = SI_Traces + 2 * l.Start() + SI_SegStraight;
break;
default:
break;
}
if( idx_n < 0 || idx_n >= MaxSubIndices )
{
wxASSERT( idx_n >= 0 );
wxASSERT( idx_n < MaxSubIndices );
return nullptr;
}
if( !m_subIndices[idx_n] )
m_subIndices[idx_n] = new ITEM_SHAPE_INDEX;
return m_subIndices[idx_n];
}
void INDEX::Add( ITEM* aItem )
{
const LAYER_RANGE& range = aItem->Layers();
ITEM_SHAPE_INDEX* idx = getSubindex( aItem );
if( m_subIndices.size() <= static_cast<size_t>( range.End() ) )
m_subIndices.resize( 2 * range.End() );
for( int i = range.Start(); i < range.End(); ++i )
m_subIndices[i].Add( aItem );
if( !idx )
return;
idx->Add( aItem );
m_allItems.insert( aItem );
int net = aItem->Net();
if( net >= 0 )
{
m_netMap[net].push_back( aItem );
}
}
void INDEX::Remove( ITEM* aItem )
{
const LAYER_RANGE& range = aItem->Layers();
ITEM_SHAPE_INDEX* idx = getSubindex( aItem );
if( m_subIndices.size() <= static_cast<size_t>( range.End() ) )
if( !idx )
return;
for( int i = range.Start(); i < range.End(); ++i )
m_subIndices[i].Remove( aItem );
idx->Remove( aItem );
m_allItems.erase( aItem );
int net = aItem->Net();
@ -59,7 +115,6 @@ void INDEX::Remove( ITEM* aItem )
m_netMap[net].remove( aItem );
}
void INDEX::Replace( ITEM* aOldItem, ITEM* aNewItem )
{
Remove( aOldItem );
@ -67,6 +122,20 @@ void INDEX::Replace( ITEM* aOldItem, ITEM* aNewItem )
}
void INDEX::Clear()
{
for( int i = 0; i < MaxSubIndices; ++i )
{
ITEM_SHAPE_INDEX* idx = m_subIndices[i];
if( idx )
delete idx;
m_subIndices[i] = NULL;
}
}
INDEX::NET_ITEMS_LIST* INDEX::GetItemsForNet( int aNet )
{
if( m_netMap.find( aNet ) == m_netMap.end() )

View File

@ -2,7 +2,7 @@
* KiRouter - a push-and-(sometimes-)shove PCB router
*
* Copyright (C) 2013-2014 CERN
* Copyright (C) 2016-2020 KiCad Developers, see AUTHORS.txt for contributors.
* Copyright (C) 2016 KiCad Developers, see AUTHORS.txt for contributors.
* Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
*
* This program is free software: you can redistribute it and/or modify it
@ -50,24 +50,33 @@ public:
typedef SHAPE_INDEX<ITEM*> ITEM_SHAPE_INDEX;
typedef std::unordered_set<ITEM*> ITEM_SET;
INDEX(){};
INDEX();
~INDEX();
/**
* Function Add()
*
* Adds item to the spatial index.
*/
void Add( ITEM* aItem );
/**
* Function Remove()
*
* Removes an item from the spatial index.
*/
void Remove( ITEM* aItem );
/**
* Function Add()
*
* Replaces one item with another.
*/
void Replace( ITEM* aOldItem, ITEM* aNewItem );
/**
* 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.
@ -82,6 +91,8 @@ public:
int Query( const ITEM* aItem, int aMinDistance, Visitor& aVisitor );
/**
* 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.
@ -96,6 +107,15 @@ public:
int Query( const SHAPE* aShape, int aMinDistance, Visitor& aVisitor );
/**
* Function Clear()
*
* Removes all items from the index.
*/
void Clear();
/**
* Function GetItemsForNet()
*
* Returns list of all items in a given net.
*/
NET_ITEMS_LIST* GetItemsForNet( int aNet );
@ -111,6 +131,8 @@ public:
}
/**
* Function Size()
*
* Returns number of items stored in the index.
*/
int Size() const { return m_allItems.size(); }
@ -119,34 +141,63 @@ public:
ITEM_SET::iterator end() { return m_allItems.end(); }
private:
static const int MaxSubIndices = 128;
static const int SI_Multilayer = 2;
static const int SI_SegDiagonal = 0;
static const int SI_SegStraight = 1;
static const int SI_Traces = 3;
static const int SI_PadsTop = 0;
static const int SI_PadsBottom = 1;
template <class Visitor>
int querySingle( std::size_t aIndex, const SHAPE* aShape, int aMinDistance, Visitor& aVisitor );
int querySingle( int index, const SHAPE* aShape, int aMinDistance, Visitor& aVisitor );
std::vector<ITEM_SHAPE_INDEX> m_subIndices;
ITEM_SHAPE_INDEX* getSubindex( const ITEM* aItem );
ITEM_SHAPE_INDEX* m_subIndices[MaxSubIndices];
std::map<int, NET_ITEMS_LIST> m_netMap;
ITEM_SET m_allItems;
};
template<class Visitor>
int INDEX::querySingle( std::size_t aIndex, const SHAPE* aShape, int aMinDistance, Visitor& aVisitor )
int INDEX::querySingle( int index, const SHAPE* aShape, int aMinDistance, Visitor& aVisitor )
{
if( aIndex >= m_subIndices.size() )
if( !m_subIndices[index] )
return 0;
return m_subIndices[aIndex].Query( aShape, aMinDistance, aVisitor, false );
return m_subIndices[index]->Query( aShape, aMinDistance, aVisitor, false );
}
template<class Visitor>
int INDEX::Query( const ITEM* aItem, int aMinDistance, Visitor& aVisitor )
{
const SHAPE* shape = aItem->Shape();
int total = 0;
total += querySingle( SI_Multilayer, shape, aMinDistance, aVisitor );
const LAYER_RANGE& layers = aItem->Layers();
for( int i = layers.Start(); i <= layers.End(); ++i )
total += querySingle( i, aItem->Shape(), aMinDistance, aVisitor );
if( layers.IsMultilayer() )
{
total += querySingle( SI_PadsTop, shape, aMinDistance, aVisitor );
total += querySingle( SI_PadsBottom, shape, aMinDistance, aVisitor );
for( int i = layers.Start(); i <= layers.End(); ++i )
total += querySingle( SI_Traces + 2 * i + SI_SegStraight, shape, aMinDistance, aVisitor );
}
else
{
int l = layers.Start();
if( l == B_Cu )
total += querySingle( SI_PadsTop, shape, aMinDistance, aVisitor );
else if( l == F_Cu )
total += querySingle( SI_PadsBottom, shape, aMinDistance, aVisitor );
total += querySingle( SI_Traces + 2 * l + SI_SegStraight, shape, aMinDistance, aVisitor );
}
return total;
}
@ -156,7 +207,7 @@ int INDEX::Query( const SHAPE* aShape, int aMinDistance, Visitor& aVisitor )
{
int total = 0;
for( std::size_t i = 0; i < m_subIndices.size(); ++i )
for( int i = 0; i < MaxSubIndices; i++ )
total += querySingle( i, aShape, aMinDistance, aVisitor );
return total;