2020-09-18 14:24:09 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2022-07-22 22:05:25 +00:00
|
|
|
* Copyright (C) 2020-2022 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_
|
|
|
|
|
2020-11-14 18:11:28 +00:00
|
|
|
#include <board_item.h>
|
2021-11-03 18:52:41 +00:00
|
|
|
#include <pad.h>
|
2023-03-30 11:49:23 +00:00
|
|
|
#include <pcb_text.h>
|
2021-06-03 18:05:43 +00:00
|
|
|
#include <memory>
|
2020-09-18 14:24:09 +00:00
|
|
|
#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>
|
2021-06-06 12:59:05 +00:00
|
|
|
#include <geometry/shape.h>
|
2021-11-03 18:52:41 +00:00
|
|
|
#include <geometry/shape_segment.h>
|
2020-09-18 14:24:09 +00:00
|
|
|
#include <math/vector2d.h>
|
2022-03-14 19:49:00 +00:00
|
|
|
#include "geometry/shape_null.h"
|
2022-08-25 14:03:44 +00:00
|
|
|
#include "board.h"
|
2020-09-18 14:24:09 +00:00
|
|
|
|
|
|
|
/**
|
2021-07-09 14:56:17 +00:00
|
|
|
* Implement an R-tree for fast spatial and layer indexing of connectable items.
|
2020-09-18 14:24:09 +00:00
|
|
|
* 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
|
|
|
|
{
|
2022-07-21 22:01:50 +00:00
|
|
|
ITEM_WITH_SHAPE( BOARD_ITEM *aParent, const SHAPE* aShape,
|
2020-10-27 17:09:27 +00:00
|
|
|
std::shared_ptr<SHAPE> aParentShape = nullptr ) :
|
2022-07-22 22:05:25 +00:00
|
|
|
parent( aParent ),
|
|
|
|
shape( aShape ),
|
|
|
|
shapeStorage( nullptr ),
|
2023-05-07 13:54:33 +00:00
|
|
|
parentShape( std::move( aParentShape ) )
|
2020-10-27 17:09:27 +00:00
|
|
|
{};
|
|
|
|
|
2023-05-07 13:54:33 +00:00
|
|
|
ITEM_WITH_SHAPE( BOARD_ITEM *aParent, const std::shared_ptr<SHAPE>& aShape,
|
2022-07-22 22:05:25 +00:00
|
|
|
std::shared_ptr<SHAPE> aParentShape = nullptr ) :
|
|
|
|
parent( aParent ),
|
|
|
|
shape( aShape.get() ),
|
|
|
|
shapeStorage( aShape ),
|
2023-05-07 13:54:33 +00:00
|
|
|
parentShape( std::move( aParentShape ) )
|
2022-07-22 22:05:25 +00:00
|
|
|
{};
|
|
|
|
|
|
|
|
BOARD_ITEM* parent;
|
|
|
|
const SHAPE* shape;
|
|
|
|
std::shared_ptr<SHAPE> shapeStorage;
|
2020-09-18 14:24:09 +00:00
|
|
|
std::shared_ptr<SHAPE> parentShape;
|
|
|
|
};
|
|
|
|
|
2020-09-23 21:47:01 +00:00
|
|
|
private:
|
2020-11-15 21:41:38 +00:00
|
|
|
|
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()
|
|
|
|
{
|
2022-07-22 22:05:25 +00:00
|
|
|
for( drc_rtree* tree : m_tree )
|
2022-01-29 01:07:11 +00:00
|
|
|
{
|
2022-07-22 22:05:25 +00:00
|
|
|
for( DRC_RTREE::ITEM_WITH_SHAPE* el : *tree )
|
2022-01-29 01:07:11 +00:00
|
|
|
delete el;
|
|
|
|
|
2020-09-18 14:24:09 +00:00
|
|
|
delete tree;
|
2022-01-29 01:07:11 +00:00
|
|
|
}
|
2020-09-18 14:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-09 14:56:17 +00:00
|
|
|
* Insert an item into the tree on a particular layer with an optional worst clearance.
|
2020-09-18 14:24:09 +00:00
|
|
|
*/
|
2021-06-02 13:00:11 +00:00
|
|
|
void Insert( BOARD_ITEM* aItem, PCB_LAYER_ID aLayer, int aWorstClearance = 0 )
|
2020-09-18 14:24:09 +00:00
|
|
|
{
|
2021-08-12 16:58:30 +00:00
|
|
|
Insert( aItem, aLayer, aLayer, aWorstClearance );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert an item into the tree on a particular layer with a worst clearance. Allows the
|
|
|
|
* source layer to be different from the tree layer.
|
|
|
|
*/
|
|
|
|
void Insert( BOARD_ITEM* aItem, PCB_LAYER_ID aRefLayer, PCB_LAYER_ID aTargetLayer,
|
|
|
|
int aWorstClearance )
|
|
|
|
{
|
|
|
|
wxCHECK( aTargetLayer != UNDEFINED_LAYER, /* void */ );
|
2020-09-18 14:24:09 +00:00
|
|
|
|
2023-06-06 15:09:34 +00:00
|
|
|
if( ( aItem->Type() == PCB_FIELD_T || aItem->Type() == PCB_TEXT_T )
|
|
|
|
&& !static_cast<PCB_TEXT*>( aItem )->IsVisible() )
|
|
|
|
{
|
2020-11-14 16:59:34 +00:00
|
|
|
return;
|
2023-06-06 15:09:34 +00:00
|
|
|
}
|
2020-11-14 16:59:34 +00:00
|
|
|
|
2022-07-21 22:01:50 +00:00
|
|
|
std::vector<const SHAPE*> subshapes;
|
2021-08-12 16:58:30 +00:00
|
|
|
std::shared_ptr<SHAPE> shape = aItem->GetEffectiveShape( aRefLayer );
|
2021-06-02 13:00:11 +00:00
|
|
|
|
|
|
|
if( shape->HasIndexableSubshapes() )
|
|
|
|
shape->GetIndexableSubshapes( subshapes );
|
2020-10-27 17:09:27 +00:00
|
|
|
else
|
2021-06-02 13:00:11 +00:00
|
|
|
subshapes.push_back( shape.get() );
|
|
|
|
|
2022-07-21 22:01:50 +00:00
|
|
|
for( const SHAPE* subshape : subshapes )
|
2020-10-27 17:09:27 +00:00
|
|
|
{
|
2022-07-21 22:01:50 +00:00
|
|
|
if( dynamic_cast<const SHAPE_NULL*>( subshape ) )
|
2022-03-14 19:49:00 +00:00
|
|
|
continue;
|
|
|
|
|
2021-06-02 13:00:11 +00:00
|
|
|
BOX2I bbox = subshape->BBox();
|
2021-01-31 00:30:40 +00:00
|
|
|
|
2021-06-02 13:00:11 +00:00
|
|
|
bbox.Inflate( aWorstClearance );
|
2021-01-31 00:30:40 +00:00
|
|
|
|
2021-06-02 13:00:11 +00:00
|
|
|
const int mmin[2] = { bbox.GetX(), bbox.GetY() };
|
|
|
|
const int mmax[2] = { bbox.GetRight(), bbox.GetBottom() };
|
|
|
|
ITEM_WITH_SHAPE* itemShape = new ITEM_WITH_SHAPE( aItem, subshape, shape );
|
2021-01-31 00:30:40 +00:00
|
|
|
|
2021-08-12 16:58:30 +00:00
|
|
|
m_tree[aTargetLayer]->Insert( mmin, mmax, itemShape );
|
2021-06-02 13:00:11 +00:00
|
|
|
m_count++;
|
2020-09-18 14:24:09 +00:00
|
|
|
}
|
2022-07-22 22:05:25 +00:00
|
|
|
|
|
|
|
if( aItem->Type() == PCB_PAD_T && aItem->HasHole() )
|
|
|
|
{
|
|
|
|
std::shared_ptr<SHAPE_SEGMENT> hole = aItem->GetEffectiveHoleShape();
|
|
|
|
BOX2I bbox = hole->BBox();
|
|
|
|
|
|
|
|
bbox.Inflate( aWorstClearance );
|
|
|
|
|
|
|
|
const int mmin[2] = { bbox.GetX(), bbox.GetY() };
|
|
|
|
const int mmax[2] = { bbox.GetRight(), bbox.GetBottom() };
|
|
|
|
ITEM_WITH_SHAPE* itemShape = new ITEM_WITH_SHAPE( aItem, hole, shape );
|
|
|
|
|
|
|
|
m_tree[aTargetLayer]->Insert( mmin, mmax, itemShape );
|
|
|
|
m_count++;
|
|
|
|
|
|
|
|
}
|
2020-09-18 14:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-09 14:56:17 +00:00
|
|
|
* Remove all items from the RTree.
|
2020-09-18 14:24:09 +00:00
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*/
|
2022-08-25 14:03:44 +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
|
|
|
{
|
2022-08-25 14:03:44 +00:00
|
|
|
// keep track of BOARD_ITEMs that have already been found to collide (some items might
|
|
|
|
// be built of COMPOUND/triangulated shapes and a single subshape collision means we have
|
|
|
|
// a hit)
|
2020-09-18 14:24:09 +00:00
|
|
|
std::unordered_set<BOARD_ITEM*> collidingCompounds;
|
|
|
|
|
2021-03-23 21:03:59 +00:00
|
|
|
// keep track of results of client filter so we don't ask more than once for compound
|
|
|
|
// shapes
|
2022-08-25 14:03:44 +00:00
|
|
|
std::unordered_map<BOARD_ITEM*, bool> filterResults;
|
2021-03-23 21:03:59 +00:00
|
|
|
|
2022-08-31 09:15:42 +00:00
|
|
|
BOX2I box = aRefItem->GetBoundingBox();
|
2020-09-18 14:24:09 +00:00
|
|
|
box.Inflate( aClearance );
|
|
|
|
|
2021-08-12 16:58:30 +00:00
|
|
|
int min[2] = { box.GetX(), box.GetY() };
|
|
|
|
int max[2] = { box.GetRight(), box.GetBottom() };
|
2020-09-18 14:24:09 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2021-03-23 21:03:59 +00:00
|
|
|
bool filtered;
|
|
|
|
auto it = filterResults.find( aItem->parent );
|
|
|
|
|
|
|
|
if( it == filterResults.end() )
|
2020-10-11 10:51:23 +00:00
|
|
|
{
|
2021-03-23 21:03:59 +00:00
|
|
|
filtered = aFilter && !aFilter( aItem->parent );
|
|
|
|
filterResults[ aItem->parent ] = filtered;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
filtered = it->second;
|
|
|
|
}
|
2020-09-18 14:24:09 +00:00
|
|
|
|
2021-03-23 21:03:59 +00:00
|
|
|
if( filtered )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if( refShape->Collide( aItem->shape, aClearance ) )
|
|
|
|
{
|
|
|
|
collidingCompounds.insert( aItem->parent );
|
|
|
|
count++;
|
|
|
|
|
|
|
|
if( aVisitor )
|
|
|
|
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.
|
|
|
|
*/
|
2022-08-31 16:17:14 +00:00
|
|
|
bool QueryColliding( const BOX2I& aBox, SHAPE* aRefShape, PCB_LAYER_ID aLayer, int aClearance,
|
2021-06-02 13:00:11 +00:00
|
|
|
int* aActual, VECTOR2I* aPos ) const
|
2020-10-28 17:55:57 +00:00
|
|
|
{
|
2022-08-31 16:17:14 +00:00
|
|
|
BOX2I bbox = aBox;
|
|
|
|
bbox.Inflate( aClearance );
|
2020-10-28 17:55:57 +00:00
|
|
|
|
2022-08-31 16:17:14 +00:00
|
|
|
int min[2] = { bbox.GetX(), bbox.GetY() };
|
|
|
|
int max[2] = { bbox.GetRight(), bbox.GetBottom() };
|
2020-10-28 17:55:57 +00:00
|
|
|
|
|
|
|
bool collision = false;
|
|
|
|
int actual = INT_MAX;
|
|
|
|
VECTOR2I pos;
|
|
|
|
|
|
|
|
auto visit =
|
|
|
|
[&]( ITEM_WITH_SHAPE* aItem ) -> bool
|
|
|
|
{
|
|
|
|
int curActual;
|
|
|
|
VECTOR2I curPos;
|
|
|
|
|
2020-11-19 23:51:48 +00:00
|
|
|
if( aRefShape->Collide( aItem->shape, aClearance, &curActual, &curPos ) )
|
2020-10-28 17:55:57 +00:00
|
|
|
{
|
|
|
|
collision = true;
|
|
|
|
|
|
|
|
if( curActual < actual )
|
|
|
|
{
|
|
|
|
actual = curActual;
|
|
|
|
pos = curPos;
|
|
|
|
}
|
2021-06-02 15:15:37 +00:00
|
|
|
|
|
|
|
// Stop looking after we have a true collision
|
|
|
|
if( actual <= 0 )
|
|
|
|
return false;
|
2020-10-28 17:55:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
this->m_tree[aLayer]->Search( min, max, visit );
|
|
|
|
|
|
|
|
if( collision )
|
|
|
|
{
|
2021-02-27 00:18:04 +00:00
|
|
|
if( aActual )
|
|
|
|
*aActual = std::max( 0, actual );
|
|
|
|
|
|
|
|
if( aPos )
|
|
|
|
*aPos = pos;
|
2020-10-28 17:55:57 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-02 13:00:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Quicker version of above that just reports a raw yes/no.
|
|
|
|
*/
|
2022-08-31 16:17:14 +00:00
|
|
|
bool QueryColliding( const BOX2I& aBox, SHAPE* aRefShape, PCB_LAYER_ID aLayer ) const
|
2021-06-02 13:00:11 +00:00
|
|
|
{
|
2022-03-08 23:54:34 +00:00
|
|
|
SHAPE_POLY_SET* poly = dynamic_cast<SHAPE_POLY_SET*>( aRefShape );
|
|
|
|
|
2021-06-02 13:00:11 +00:00
|
|
|
int min[2] = { aBox.GetX(), aBox.GetY() };
|
|
|
|
int max[2] = { aBox.GetRight(), aBox.GetBottom() };
|
|
|
|
bool collision = false;
|
|
|
|
|
2022-03-08 23:54:34 +00:00
|
|
|
// Special case the polygon case. Otherwise we'll call its Collide() method which will
|
|
|
|
// triangulate it as well and then do triangle/triangle collisions. This ends up being
|
2023-02-04 13:44:42 +00:00
|
|
|
// *much* slower than 3 segment Collide()s and a PointInside().
|
2022-03-08 23:54:34 +00:00
|
|
|
auto polyVisitor =
|
|
|
|
[&]( ITEM_WITH_SHAPE* aItem ) -> bool
|
|
|
|
{
|
2022-07-21 22:01:50 +00:00
|
|
|
const SHAPE* shape = aItem->shape;
|
|
|
|
wxASSERT( dynamic_cast<const SHAPE_POLY_SET::TRIANGULATED_POLYGON::TRI*>( shape ) );
|
|
|
|
auto tri = static_cast<const SHAPE_POLY_SET::TRIANGULATED_POLYGON::TRI*>( shape );
|
2022-03-08 23:54:34 +00:00
|
|
|
|
|
|
|
const SHAPE_LINE_CHAIN& outline = poly->Outline( 0 );
|
|
|
|
|
2023-02-04 13:44:42 +00:00
|
|
|
for( int ii = 0; ii < (int) tri->GetSegmentCount(); ++ii )
|
|
|
|
{
|
|
|
|
if( outline.Collide( tri->GetSegment( ii ) ) )
|
|
|
|
{
|
|
|
|
collision = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also must check for poly being completely inside the triangle
|
|
|
|
if( tri->PointInside( outline.CPoint( 0 ) ) )
|
2022-03-08 23:54:34 +00:00
|
|
|
{
|
|
|
|
collision = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto visitor =
|
2021-06-02 13:00:11 +00:00
|
|
|
[&]( ITEM_WITH_SHAPE* aItem ) -> bool
|
|
|
|
{
|
|
|
|
if( aRefShape->Collide( aItem->shape, 0 ) )
|
|
|
|
{
|
|
|
|
collision = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2023-02-04 13:44:42 +00:00
|
|
|
if( poly && poly->OutlineCount() == 1 && poly->HoleCount( 0 ) == 0 )
|
2022-03-08 23:54:34 +00:00
|
|
|
this->m_tree[aLayer]->Search( min, max, polyVisitor );
|
|
|
|
else
|
|
|
|
this->m_tree[aLayer]->Search( min, max, visitor );
|
2021-06-02 13:00:11 +00:00
|
|
|
|
|
|
|
return collision;
|
|
|
|
}
|
2020-10-28 17:55:57 +00:00
|
|
|
|
2022-07-11 19:26:56 +00:00
|
|
|
/**
|
|
|
|
* Gets the BOARD_ITEMs that overlap the specified point/layer
|
|
|
|
* @param aPt Position on the tree
|
|
|
|
* @param aLayer Layer to search
|
|
|
|
* @return vector of overlapping BOARD_ITEMS*
|
|
|
|
*/
|
2022-08-25 14:03:44 +00:00
|
|
|
std::unordered_set<BOARD_ITEM*> GetObjectsAt( const VECTOR2I& aPt, PCB_LAYER_ID aLayer,
|
|
|
|
int aClearance = 0 )
|
2022-07-11 19:26:56 +00:00
|
|
|
{
|
2022-08-25 14:03:44 +00:00
|
|
|
std::unordered_set<BOARD_ITEM*> retval;
|
2022-07-11 19:26:56 +00:00
|
|
|
int min[2] = { aPt.x - aClearance, aPt.y - aClearance };
|
|
|
|
int max[2] = { aPt.x + aClearance, aPt.y + aClearance };
|
|
|
|
|
|
|
|
auto visitor =
|
|
|
|
[&]( ITEM_WITH_SHAPE* aItem ) -> bool
|
|
|
|
{
|
|
|
|
retval.insert( aItem->parent );
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
m_tree[aLayer]->Search( min, max, visitor );
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2022-08-25 14:03:44 +00:00
|
|
|
int QueryCollidingPairs( DRC_RTREE* aRefTree, std::vector<LAYER_PAIR> aLayerPairs,
|
|
|
|
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-11-14 16:59:34 +00:00
|
|
|
std::vector<PAIR_INFO> pairsToVisit;
|
2020-09-23 21:47:01 +00:00
|
|
|
|
2020-11-14 16:59:34 +00:00
|
|
|
for( LAYER_PAIR& layerPair : aLayerPairs )
|
2020-09-23 21:47:01 +00:00
|
|
|
{
|
2020-11-14 16:59:34 +00:00
|
|
|
const PCB_LAYER_ID refLayer = layerPair.first;
|
|
|
|
const PCB_LAYER_ID targetLayer = layerPair.second;
|
2020-09-23 21:47:01 +00:00
|
|
|
|
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 );
|
|
|
|
|
2020-11-14 16:59:34 +00:00
|
|
|
int min[2] = { box.GetX(), box.GetY() };
|
|
|
|
int max[2] = { box.GetRight(), box.GetBottom() };
|
2020-09-23 21:47:01 +00:00
|
|
|
|
2020-10-04 11:46:35 +00:00
|
|
|
auto visit =
|
|
|
|
[&]( ITEM_WITH_SHAPE* aItemToTest ) -> bool
|
|
|
|
{
|
|
|
|
// don't collide items against themselves
|
2020-11-14 16:59:34 +00:00
|
|
|
if( aItemToTest->parent == refItem->parent )
|
2020-10-04 11:46:35 +00:00
|
|
|
return true;
|
2020-09-23 21:47:01 +00:00
|
|
|
|
2020-11-14 16:59:34 +00:00
|
|
|
pairsToVisit.emplace_back( layerPair, refItem, aItemToTest );
|
2020-10-12 22:59:01 +00:00
|
|
|
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)
|
2022-08-25 14:03:44 +00:00
|
|
|
std::unordered_map<PTR_PTR_CACHE_KEY, int> collidingCompounds;
|
2020-10-12 22:59:01 +00:00
|
|
|
|
|
|
|
int progress = 0;
|
|
|
|
int count = pairsToVisit.size();
|
|
|
|
|
2020-11-14 16:59:34 +00:00
|
|
|
for( const PAIR_INFO& pair : pairsToVisit )
|
2020-10-12 22:59:01 +00:00
|
|
|
{
|
|
|
|
if( !aProgressReporter( progress++, count ) )
|
|
|
|
break;
|
|
|
|
|
2020-11-14 16:59:34 +00:00
|
|
|
BOARD_ITEM* a = pair.refItem->parent;
|
|
|
|
BOARD_ITEM* b = pair.testItem->parent;
|
|
|
|
|
|
|
|
// store canonical order so we don't collide in both directions (a:b and b:a)
|
|
|
|
if( static_cast<void*>( a ) > static_cast<void*>( b ) )
|
|
|
|
std::swap( a, b );
|
|
|
|
|
2020-10-12 22:59:01 +00:00
|
|
|
// don't report multiple collisions for compound or triangulated shapes
|
2020-11-14 16:59:34 +00:00
|
|
|
if( collidingCompounds.count( { a, b } ) )
|
2020-10-12 22:59:01 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
bool collisionDetected = false;
|
|
|
|
|
|
|
|
if( !aVisitor( pair.layerPair, pair.refItem, pair.testItem, &collisionDetected ) )
|
|
|
|
break;
|
|
|
|
|
|
|
|
if( collisionDetected )
|
2020-11-14 16:59:34 +00:00
|
|
|
collidingCompounds[ { a, b } ] = 1;
|
2020-10-12 22:59:01 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 21:47:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-18 14:24:09 +00:00
|
|
|
/**
|
2021-07-09 14:56:17 +00:00
|
|
|
* Return the number of items in the tree.
|
|
|
|
*
|
|
|
|
* @return number of elements in the tree.
|
2020-09-18 14:24:09 +00:00
|
|
|
*/
|
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 } };
|
|
|
|
};
|
|
|
|
|
2022-08-31 16:17:14 +00:00
|
|
|
DRC_LAYER( drc_rtree* aTree, const BOX2I& aRect ) : layer_tree( aTree )
|
2020-09-18 14:24:09 +00:00
|
|
|
{
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-06 09:27:41 +00:00
|
|
|
DRC_LAYER OnLayer( PCB_LAYER_ID aLayer ) const
|
2020-09-18 14:24:09 +00:00
|
|
|
{
|
|
|
|
return DRC_LAYER( m_tree[int( aLayer )] );
|
|
|
|
}
|
|
|
|
|
2022-01-11 00:49:49 +00:00
|
|
|
DRC_LAYER Overlapping( PCB_LAYER_ID aLayer, const VECTOR2I& aPoint, int aAccuracy = 0 ) const
|
2020-09-18 14:24:09 +00:00
|
|
|
{
|
2022-08-31 16:17:14 +00:00
|
|
|
BOX2I rect( aPoint, VECTOR2I( 0, 0 ) );
|
2020-09-18 14:24:09 +00:00
|
|
|
rect.Inflate( aAccuracy );
|
|
|
|
return DRC_LAYER( m_tree[int( aLayer )], rect );
|
|
|
|
}
|
|
|
|
|
2022-08-31 16:17:14 +00:00
|
|
|
DRC_LAYER Overlapping( PCB_LAYER_ID aLayer, const BOX2I& aRect ) const
|
2020-09-18 14:24:09 +00:00
|
|
|
{
|
|
|
|
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_ */
|