2020-09-18 14:24:09 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2020-10-29 13:18:15 +00:00
|
|
|
* Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
|
2020-09-18 14:24:09 +00:00
|
|
|
* Copyright (C) 2020 CERN
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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-3.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 3 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef DRC_RTREE_H_
|
|
|
|
#define DRC_RTREE_H_
|
|
|
|
|
|
|
|
#include <eda_rect.h>
|
|
|
|
#include <class_board_item.h>
|
|
|
|
#include <class_track.h>
|
|
|
|
#include <class_zone.h>
|
|
|
|
#include <unordered_set>
|
2020-09-23 21:47:01 +00:00
|
|
|
#include <set>
|
2020-09-18 14:24:09 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <geometry/rtree.h>
|
|
|
|
#include <math/vector2d.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DRC_RTREE -
|
|
|
|
* Implements an R-tree for fast spatial and layer indexing of connectable items.
|
|
|
|
* Non-owning.
|
|
|
|
*/
|
|
|
|
class DRC_RTREE
|
|
|
|
{
|
2020-09-23 21:47:01 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2020-09-18 14:24:09 +00:00
|
|
|
struct ITEM_WITH_SHAPE
|
|
|
|
{
|
2020-10-27 17:09:27 +00:00
|
|
|
ITEM_WITH_SHAPE( BOARD_ITEM *aParent, SHAPE* aShape,
|
|
|
|
std::shared_ptr<SHAPE> aParentShape = nullptr ) :
|
2020-09-18 14:24:09 +00:00
|
|
|
parent ( aParent ),
|
|
|
|
shape ( aShape ),
|
2020-10-27 17:09:27 +00:00
|
|
|
parentShape( aParentShape )
|
|
|
|
{};
|
|
|
|
|
2020-09-18 14:24:09 +00:00
|
|
|
BOARD_ITEM* parent;
|
|
|
|
SHAPE* shape;
|
|
|
|
std::shared_ptr<SHAPE> parentShape;
|
|
|
|
};
|
|
|
|
|
2020-09-23 21:47:01 +00:00
|
|
|
private:
|
|
|
|
|
2020-09-18 14:24:09 +00:00
|
|
|
using drc_rtree = RTree<ITEM_WITH_SHAPE*, int, 2, double>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
DRC_RTREE()
|
|
|
|
{
|
|
|
|
for( int layer : LSET::AllLayersMask().Seq() )
|
|
|
|
m_tree[layer] = new drc_rtree();
|
|
|
|
|
|
|
|
m_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
~DRC_RTREE()
|
|
|
|
{
|
|
|
|
for( auto tree : m_tree )
|
|
|
|
delete tree;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function Insert()
|
2020-10-29 13:18:15 +00:00
|
|
|
* Inserts an item into the tree.
|
2020-09-18 14:24:09 +00:00
|
|
|
*/
|
2020-11-01 14:03:13 +00:00
|
|
|
void Insert( BOARD_ITEM* aItem, int aWorstClearance = 0, int aLayer = UNDEFINED_LAYER )
|
2020-09-18 14:24:09 +00:00
|
|
|
{
|
|
|
|
std::vector<SHAPE*> subshapes;
|
|
|
|
|
2020-10-27 17:09:27 +00:00
|
|
|
auto addLayer =
|
|
|
|
[&]( PCB_LAYER_ID layer )
|
|
|
|
{
|
|
|
|
std::shared_ptr<SHAPE> shape = aItem->GetEffectiveShape( layer );
|
2020-11-01 12:13:53 +00:00
|
|
|
subshapes.clear();
|
2020-09-18 14:24:09 +00:00
|
|
|
|
2020-10-27 17:09:27 +00:00
|
|
|
if( shape->HasIndexableSubshapes() )
|
|
|
|
shape->GetIndexableSubshapes( subshapes );
|
|
|
|
else
|
|
|
|
subshapes.push_back( shape.get() );
|
2020-09-18 14:24:09 +00:00
|
|
|
|
2020-10-27 17:09:27 +00:00
|
|
|
for( SHAPE* subshape : subshapes )
|
|
|
|
{
|
|
|
|
BOX2I bbox = subshape->BBox();
|
2020-09-18 14:24:09 +00:00
|
|
|
|
2020-10-27 17:09:27 +00:00
|
|
|
bbox.Inflate( aWorstClearance );
|
2020-09-18 14:24:09 +00:00
|
|
|
|
2020-10-27 17:09:27 +00:00
|
|
|
const int mmin[2] = { bbox.GetX(), bbox.GetY() };
|
|
|
|
const int mmax[2] = { bbox.GetRight(), bbox.GetBottom() };
|
2020-09-18 14:24:09 +00:00
|
|
|
|
2020-10-27 17:09:27 +00:00
|
|
|
m_tree[layer]->Insert( mmin, mmax,
|
|
|
|
new ITEM_WITH_SHAPE( aItem, subshape, shape ) );
|
|
|
|
m_count++;
|
|
|
|
}
|
|
|
|
};
|
2020-09-18 14:24:09 +00:00
|
|
|
|
2020-10-27 17:09:27 +00:00
|
|
|
if( aLayer != UNDEFINED_LAYER )
|
|
|
|
{
|
|
|
|
addLayer( (PCB_LAYER_ID) aLayer );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( int layer : aItem->GetLayerSet().Seq() )
|
|
|
|
addLayer( (PCB_LAYER_ID) layer );
|
2020-09-18 14:24:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function RemoveAll()
|
|
|
|
* Removes all items from the RTree
|
|
|
|
*/
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
for( auto tree : m_tree )
|
|
|
|
tree->RemoveAll();
|
|
|
|
|
|
|
|
m_count = 0;
|
|
|
|
}
|
|
|
|
|
2020-10-27 17:09:27 +00:00
|
|
|
bool CheckColliding( SHAPE* aRefShape, PCB_LAYER_ID aTargetLayer, int aClearance = 0,
|
2020-10-27 11:03:35 +00:00
|
|
|
std::function<bool( BOARD_ITEM*)> aFilter = nullptr ) const
|
2020-10-02 18:57:52 +00:00
|
|
|
{
|
|
|
|
BOX2I box = aRefShape->BBox();
|
|
|
|
box.Inflate( aClearance );
|
|
|
|
|
|
|
|
int min[2] = { box.GetX(), box.GetY() };
|
|
|
|
int max[2] = { box.GetRight(), box.GetBottom() };
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
|
2020-10-11 10:51:23 +00:00
|
|
|
auto visit =
|
|
|
|
[&] ( ITEM_WITH_SHAPE* aItem ) -> bool
|
|
|
|
{
|
|
|
|
if( !aFilter || aFilter( aItem->parent ) )
|
|
|
|
{
|
|
|
|
int actual;
|
2020-10-02 18:57:52 +00:00
|
|
|
|
2020-10-11 10:51:23 +00:00
|
|
|
if( aRefShape->Collide( aItem->shape, aClearance, &actual ) )
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-10-02 18:57:52 +00:00
|
|
|
|
2020-10-11 10:51:23 +00:00
|
|
|
return true;
|
|
|
|
};
|
2020-10-02 18:57:52 +00:00
|
|
|
|
|
|
|
this->m_tree[aTargetLayer]->Search( min, max, visit );
|
|
|
|
return count > 0;
|
|
|
|
}
|
|
|
|
|
2020-10-28 17:55:57 +00:00
|
|
|
/**
|
|
|
|
* This is a fast test which essentially does bounding-box overlap given a worst-case
|
|
|
|
* clearance. It's used when looking up the specific item-to-item clearance might be
|
|
|
|
* expensive and should be deferred till we know we have a possible hit.
|
|
|
|
*/
|
2020-09-18 14:24:09 +00:00
|
|
|
int QueryColliding( BOARD_ITEM* aRefItem,
|
|
|
|
PCB_LAYER_ID aRefLayer,
|
|
|
|
PCB_LAYER_ID aTargetLayer,
|
2020-10-28 13:52:30 +00:00
|
|
|
std::function<bool( BOARD_ITEM* )> aFilter = nullptr,
|
|
|
|
std::function<bool( BOARD_ITEM* )> aVisitor = nullptr,
|
2020-10-27 11:03:35 +00:00
|
|
|
int aClearance = 0 ) const
|
2020-09-18 14:24:09 +00:00
|
|
|
{
|
|
|
|
// keep track of BOARD_ITEMs that have been already found to collide (some items
|
|
|
|
// might be build of COMPOUND/triangulated shapes and a single subshape collision
|
|
|
|
// means we have a hit)
|
|
|
|
std::unordered_set<BOARD_ITEM*> collidingCompounds;
|
|
|
|
|
|
|
|
EDA_RECT box = aRefItem->GetBoundingBox();
|
|
|
|
box.Inflate( aClearance );
|
|
|
|
|
|
|
|
int min[2] = { box.GetX(), box.GetY() };
|
|
|
|
int max[2] = { box.GetRight(), box.GetBottom() };
|
|
|
|
|
|
|
|
std::shared_ptr<SHAPE> refShape = aRefItem->GetEffectiveShape( aRefLayer );
|
|
|
|
|
2020-10-02 18:57:52 +00:00
|
|
|
int count = 0;
|
|
|
|
|
2020-10-11 10:51:23 +00:00
|
|
|
auto visit =
|
|
|
|
[&]( ITEM_WITH_SHAPE* aItem ) -> bool
|
|
|
|
{
|
2020-10-26 18:36:25 +00:00
|
|
|
if( aItem->parent == aRefItem )
|
|
|
|
return true;
|
|
|
|
|
2020-10-11 10:51:23 +00:00
|
|
|
if( collidingCompounds.find( aItem->parent ) != collidingCompounds.end() )
|
|
|
|
return true;
|
2020-09-18 14:24:09 +00:00
|
|
|
|
2020-10-11 10:51:23 +00:00
|
|
|
if( !aFilter || aFilter( aItem->parent ) )
|
|
|
|
{
|
2020-10-28 13:52:30 +00:00
|
|
|
if( refShape->Collide( aItem->shape, aClearance ) )
|
2020-10-11 10:51:23 +00:00
|
|
|
{
|
|
|
|
collidingCompounds.insert( aItem->parent );
|
|
|
|
count++;
|
2020-09-18 14:24:09 +00:00
|
|
|
|
2020-10-11 10:51:23 +00:00
|
|
|
if( aVisitor )
|
2020-10-28 13:52:30 +00:00
|
|
|
return aVisitor( aItem->parent );
|
2020-10-11 10:51:23 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-18 14:24:09 +00:00
|
|
|
|
2020-10-02 18:57:52 +00:00
|
|
|
return true;
|
2020-10-11 10:51:23 +00:00
|
|
|
};
|
2020-09-18 14:24:09 +00:00
|
|
|
|
|
|
|
this->m_tree[aTargetLayer]->Search( min, max, visit );
|
2020-10-02 18:57:52 +00:00
|
|
|
return count;
|
2020-09-18 14:24:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 17:55:57 +00:00
|
|
|
/**
|
|
|
|
* This one is for tessellated items. (All shapes in the tree will be from a single
|
|
|
|
* BOARD_ITEM.)
|
|
|
|
* It checks all items in the bbox overlap to find the minimal actual distance and
|
|
|
|
* position.
|
|
|
|
*/
|
|
|
|
bool QueryColliding( BOARD_ITEM* aRefItem, PCB_LAYER_ID aLayer,
|
|
|
|
int aClearance, int* aActual, VECTOR2I* aPos ) const
|
|
|
|
{
|
|
|
|
EDA_RECT box = aRefItem->GetBoundingBox();
|
|
|
|
box.Inflate( aClearance );
|
|
|
|
|
|
|
|
int min[2] = { box.GetX(), box.GetY() };
|
|
|
|
int max[2] = { box.GetRight(), box.GetBottom() };
|
|
|
|
|
|
|
|
std::shared_ptr<SHAPE> refShape = aRefItem->GetEffectiveShape( aLayer );
|
|
|
|
|
|
|
|
bool collision = false;
|
|
|
|
int actual = INT_MAX;
|
|
|
|
VECTOR2I pos;
|
|
|
|
|
|
|
|
auto visit =
|
|
|
|
[&]( ITEM_WITH_SHAPE* aItem ) -> bool
|
|
|
|
{
|
|
|
|
int curActual;
|
|
|
|
VECTOR2I curPos;
|
|
|
|
|
|
|
|
if( refShape->Collide( aItem->shape, aClearance, &curActual, &curPos ) )
|
|
|
|
{
|
|
|
|
collision = true;
|
|
|
|
|
|
|
|
if( curActual < actual )
|
|
|
|
{
|
|
|
|
actual = curActual;
|
|
|
|
pos = curPos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
this->m_tree[aLayer]->Search( min, max, visit );
|
|
|
|
|
|
|
|
if( collision )
|
|
|
|
{
|
|
|
|
*aActual = std::max( 0, actual );
|
|
|
|
*aPos = pos;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-23 21:47:01 +00:00
|
|
|
typedef std::pair<PCB_LAYER_ID, PCB_LAYER_ID> LAYER_PAIR;
|
|
|
|
|
2020-10-12 22:59:01 +00:00
|
|
|
struct PAIR_INFO
|
|
|
|
{
|
|
|
|
PAIR_INFO( LAYER_PAIR aPair, ITEM_WITH_SHAPE* aRef, ITEM_WITH_SHAPE* aTest ) :
|
|
|
|
layerPair( aPair ),
|
|
|
|
refItem( aRef ),
|
|
|
|
testItem( aTest )
|
|
|
|
{ };
|
|
|
|
|
|
|
|
LAYER_PAIR layerPair;
|
|
|
|
ITEM_WITH_SHAPE* refItem;
|
|
|
|
ITEM_WITH_SHAPE* testItem;
|
|
|
|
};
|
|
|
|
|
2020-09-23 21:47:01 +00:00
|
|
|
int QueryCollidingPairs( DRC_RTREE* aRefTree,
|
2020-10-04 11:46:35 +00:00
|
|
|
std::vector<LAYER_PAIR> aLayers,
|
|
|
|
std::function<bool( const LAYER_PAIR&,
|
|
|
|
ITEM_WITH_SHAPE*, ITEM_WITH_SHAPE*,
|
|
|
|
bool* aCollision )> aVisitor,
|
2020-10-12 22:59:01 +00:00
|
|
|
int aMaxClearance,
|
2020-10-27 11:03:35 +00:00
|
|
|
std::function<bool(int, int )> aProgressReporter ) const
|
2020-09-23 21:47:01 +00:00
|
|
|
{
|
2020-10-12 22:59:01 +00:00
|
|
|
std::vector< PAIR_INFO > pairsToVisit;
|
2020-09-23 21:47:01 +00:00
|
|
|
|
2020-10-12 22:59:01 +00:00
|
|
|
for( LAYER_PAIR& refLayerIter : aLayers )
|
2020-09-23 21:47:01 +00:00
|
|
|
{
|
|
|
|
const PCB_LAYER_ID refLayer = refLayerIter.first;
|
|
|
|
const PCB_LAYER_ID targetLayer = refLayerIter.second;
|
|
|
|
|
2020-10-12 22:59:01 +00:00
|
|
|
for( ITEM_WITH_SHAPE* refItem : aRefTree->OnLayer( refLayer ) )
|
2020-09-23 21:47:01 +00:00
|
|
|
{
|
|
|
|
BOX2I box = refItem->shape->BBox();
|
|
|
|
box.Inflate( aMaxClearance );
|
|
|
|
|
|
|
|
int min[2] = { box.GetX(), box.GetY() };
|
|
|
|
int max[2] = { box.GetRight(), box.GetBottom() };
|
|
|
|
|
2020-10-04 11:46:35 +00:00
|
|
|
auto visit =
|
|
|
|
[&]( ITEM_WITH_SHAPE* aItemToTest ) -> bool
|
|
|
|
{
|
|
|
|
// don't collide items against themselves
|
|
|
|
if( refLayer == targetLayer && aItemToTest->parent == refItem->parent )
|
|
|
|
return true;
|
2020-09-23 21:47:01 +00:00
|
|
|
|
2020-10-12 22:59:01 +00:00
|
|
|
pairsToVisit.emplace_back( refLayerIter, refItem, aItemToTest );
|
|
|
|
return true;
|
2020-10-04 11:46:35 +00:00
|
|
|
};
|
2020-09-23 21:47:01 +00:00
|
|
|
|
|
|
|
this->m_tree[targetLayer]->Search( min, max, visit );
|
|
|
|
};
|
|
|
|
}
|
2020-10-12 22:59:01 +00:00
|
|
|
|
|
|
|
// keep track of BOARD_ITEMs pairs that have been already found to collide (some items
|
|
|
|
// might be build of COMPOUND/triangulated shapes and a single subshape collision
|
|
|
|
// means we have a hit)
|
|
|
|
std::map< std::pair<BOARD_ITEM*, BOARD_ITEM*>, int> collidingCompounds;
|
|
|
|
|
|
|
|
int progress = 0;
|
|
|
|
int count = pairsToVisit.size();
|
|
|
|
|
|
|
|
for( PAIR_INFO& pair : pairsToVisit )
|
|
|
|
{
|
|
|
|
if( !aProgressReporter( progress++, count ) )
|
|
|
|
break;
|
|
|
|
|
|
|
|
// don't report multiple collisions for compound or triangulated shapes
|
|
|
|
if( collidingCompounds.count( { pair.refItem->parent, pair.testItem->parent } ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
bool collisionDetected = false;
|
|
|
|
|
|
|
|
if( !aVisitor( pair.layerPair, pair.refItem, pair.testItem, &collisionDetected ) )
|
|
|
|
break;
|
|
|
|
|
|
|
|
if( collisionDetected )
|
|
|
|
collidingCompounds[ { pair.refItem->parent, pair.testItem->parent } ] = 1;
|
|
|
|
}
|
|
|
|
|
2020-09-23 21:47:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-18 14:24:09 +00:00
|
|
|
/**
|
|
|
|
* Returns the number of items in the tree
|
|
|
|
* @return number of elements in the tree;
|
|
|
|
*/
|
2020-10-27 11:03:35 +00:00
|
|
|
size_t size() const
|
2020-09-18 14:24:09 +00:00
|
|
|
{
|
|
|
|
return m_count;
|
|
|
|
}
|
|
|
|
|
2020-10-27 11:03:35 +00:00
|
|
|
bool empty() const
|
2020-09-18 14:24:09 +00:00
|
|
|
{
|
|
|
|
return m_count == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
using iterator = typename drc_rtree::Iterator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The DRC_LAYER struct provides a layer-specific auto-range iterator to the RTree. Using
|
|
|
|
* this struct, one can write lines like:
|
|
|
|
*
|
|
|
|
* for( auto item : rtree.OnLayer( In1_Cu ) )
|
|
|
|
*
|
|
|
|
* and iterate over only the RTree items that are on In1
|
|
|
|
*/
|
|
|
|
struct DRC_LAYER
|
|
|
|
{
|
|
|
|
DRC_LAYER( drc_rtree* aTree ) : layer_tree( aTree )
|
|
|
|
{
|
|
|
|
m_rect = { { INT_MIN, INT_MIN }, { INT_MAX, INT_MAX } };
|
|
|
|
};
|
|
|
|
|
|
|
|
DRC_LAYER( drc_rtree* aTree, const EDA_RECT aRect ) : layer_tree( aTree )
|
|
|
|
{
|
|
|
|
m_rect = { { aRect.GetX(), aRect.GetY() },
|
|
|
|
{ aRect.GetRight(), aRect.GetBottom() } };
|
|
|
|
};
|
|
|
|
|
|
|
|
drc_rtree::Rect m_rect;
|
|
|
|
drc_rtree* layer_tree;
|
|
|
|
|
|
|
|
iterator begin()
|
|
|
|
{
|
|
|
|
return layer_tree->begin( m_rect );
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator end()
|
|
|
|
{
|
|
|
|
return layer_tree->end( m_rect );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DRC_LAYER OnLayer( PCB_LAYER_ID aLayer )
|
|
|
|
{
|
|
|
|
return DRC_LAYER( m_tree[int( aLayer )] );
|
|
|
|
}
|
|
|
|
|
|
|
|
DRC_LAYER Overlapping( PCB_LAYER_ID aLayer, const wxPoint& aPoint, int aAccuracy = 0 )
|
|
|
|
{
|
|
|
|
EDA_RECT rect( aPoint, wxSize( 0, 0 ) );
|
|
|
|
rect.Inflate( aAccuracy );
|
|
|
|
return DRC_LAYER( m_tree[int( aLayer )], rect );
|
|
|
|
}
|
|
|
|
|
|
|
|
DRC_LAYER Overlapping( PCB_LAYER_ID aLayer, const EDA_RECT& aRect )
|
|
|
|
{
|
|
|
|
return DRC_LAYER( m_tree[int( aLayer )], aRect );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
drc_rtree* m_tree[PCB_LAYER_ID_COUNT];
|
|
|
|
size_t m_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* DRC_RTREE_H_ */
|