2015-02-18 00:29:54 +00:00
|
|
|
/*
|
|
|
|
* KiRouter - a push-and-(sometimes-)shove PCB router
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013-2015 CERN
|
2021-07-19 23:56:05 +00:00
|
|
|
* Copyright (C) 2016-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2015-02-18 00:29:54 +00:00
|
|
|
* 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 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pns_line.h"
|
|
|
|
#include "pns_segment.h"
|
|
|
|
#include "pns_node.h"
|
|
|
|
#include "pns_joint.h"
|
|
|
|
#include "pns_solid.h"
|
|
|
|
#include "pns_router.h"
|
2015-07-02 14:10:49 +00:00
|
|
|
#include "pns_utils.h"
|
2015-02-18 00:29:54 +00:00
|
|
|
|
|
|
|
#include "pns_diff_pair.h"
|
|
|
|
#include "pns_topology.h"
|
|
|
|
|
2020-11-12 20:19:22 +00:00
|
|
|
#include <board.h>
|
2021-06-06 19:03:10 +00:00
|
|
|
#include <pad.h>
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2016-08-29 14:38:11 +00:00
|
|
|
namespace PNS {
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
bool TOPOLOGY::SimplifyLine( LINE* aLine )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2016-08-29 18:36:05 +00:00
|
|
|
if( !aLine->IsLinked() || !aLine->SegmentCount() )
|
2015-02-18 16:53:46 +00:00
|
|
|
return false;
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2019-05-17 00:13:21 +00:00
|
|
|
LINKED_ITEM* root = aLine->GetLink( 0 );
|
2016-08-29 17:31:13 +00:00
|
|
|
LINE l = m_world->AssembleLine( root );
|
2015-08-03 19:11:51 +00:00
|
|
|
SHAPE_LINE_CHAIN simplified( l.CLine() );
|
2015-02-18 00:29:54 +00:00
|
|
|
|
|
|
|
simplified.Simplify();
|
|
|
|
|
2015-08-03 19:11:51 +00:00
|
|
|
if( simplified.PointCount() != l.PointCount() )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2016-08-30 17:01:30 +00:00
|
|
|
m_world->Remove( l );
|
2017-01-18 14:52:35 +00:00
|
|
|
LINE lnew( l );
|
2015-08-03 19:11:51 +00:00
|
|
|
lnew.SetShape( simplified );
|
2016-08-30 15:28:35 +00:00
|
|
|
m_world->Add( lnew );
|
2015-02-18 00:29:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
const TOPOLOGY::JOINT_SET TOPOLOGY::ConnectedJoints( JOINT* aStart )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
std::deque<JOINT*> searchQueue;
|
2015-02-18 00:29:54 +00:00
|
|
|
JOINT_SET processed;
|
|
|
|
|
|
|
|
searchQueue.push_back( aStart );
|
|
|
|
processed.insert( aStart );
|
|
|
|
|
|
|
|
while( !searchQueue.empty() )
|
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
JOINT* current = searchQueue.front();
|
2015-02-18 00:29:54 +00:00
|
|
|
searchQueue.pop_front();
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
for( ITEM* item : current->LinkList() )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2022-08-13 03:05:45 +00:00
|
|
|
if( item->OfKind( ITEM::SEGMENT_T | ITEM::ARC_T ) )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2022-08-13 03:05:45 +00:00
|
|
|
JOINT* a = m_world->FindJoint( item->Anchor( 0 ), item );;
|
|
|
|
JOINT* b = m_world->FindJoint( item->Anchor( 1 ), item );;
|
2016-08-29 17:31:13 +00:00
|
|
|
JOINT* next = ( *a == *current ) ? b : a;
|
2015-02-18 00:29:54 +00:00
|
|
|
|
|
|
|
if( processed.find( next ) == processed.end() )
|
|
|
|
{
|
|
|
|
processed.insert( next );
|
|
|
|
searchQueue.push_back( next );
|
|
|
|
}
|
2015-02-18 16:53:46 +00:00
|
|
|
}
|
2015-02-18 00:29:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return processed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-14 16:38:39 +00:00
|
|
|
bool TOPOLOGY::NearestUnconnectedAnchorPoint( const LINE* aTrack, VECTOR2I& aPoint,
|
|
|
|
LAYER_RANGE& aLayers )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
LINE track( *aTrack );
|
2015-02-18 00:29:54 +00:00
|
|
|
VECTOR2I end;
|
|
|
|
|
|
|
|
if( !track.PointCount() )
|
|
|
|
return false;
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
std::unique_ptr<NODE> tmpNode( m_world->Branch() );
|
2016-08-30 15:28:35 +00:00
|
|
|
tmpNode->Add( track );
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
JOINT* jt = tmpNode->FindJoint( track.CPoint( -1 ), &track );
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2021-04-03 00:29:25 +00:00
|
|
|
if( !jt || jt->Net() <= 0 )
|
2015-02-18 00:29:54 +00:00
|
|
|
return false;
|
|
|
|
|
2020-12-02 18:42:37 +00:00
|
|
|
if( ( !track.EndsWithVia() && jt->LinkCount() >= 2 )
|
|
|
|
|| ( track.EndsWithVia() && jt->LinkCount() >= 3 ) ) // we got something connected
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
end = jt->Pos();
|
2022-07-14 16:38:39 +00:00
|
|
|
aLayers = jt->Layers();
|
2015-02-18 16:53:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-04 16:57:19 +00:00
|
|
|
int anchor;
|
2020-12-02 20:04:21 +00:00
|
|
|
|
2020-12-04 16:57:19 +00:00
|
|
|
TOPOLOGY topo( tmpNode.get() );
|
|
|
|
ITEM* it = topo.NearestUnconnectedItem( jt, &anchor );
|
2015-02-18 00:29:54 +00:00
|
|
|
|
|
|
|
if( !it )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
end = it->Anchor( anchor );
|
2022-07-14 16:38:39 +00:00
|
|
|
aLayers = it->Layers();
|
2015-02-18 00:29:54 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 16:38:39 +00:00
|
|
|
aPoint = end;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool TOPOLOGY::LeadingRatLine( const LINE* aTrack, SHAPE_LINE_CHAIN& aRatLine )
|
|
|
|
{
|
|
|
|
VECTOR2I end;
|
|
|
|
// Ratline doesn't care about the layer
|
|
|
|
LAYER_RANGE layers;
|
|
|
|
|
|
|
|
if( !NearestUnconnectedAnchorPoint( aTrack, end, layers ) )
|
|
|
|
return false;
|
|
|
|
|
2015-02-18 00:29:54 +00:00
|
|
|
aRatLine.Clear();
|
2022-07-14 16:38:39 +00:00
|
|
|
aRatLine.Append( aTrack->CPoint( -1 ) );
|
2015-02-18 16:53:46 +00:00
|
|
|
aRatLine.Append( end );
|
2015-02-18 00:29:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-02 14:11:15 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
ITEM* TOPOLOGY::NearestUnconnectedItem( JOINT* aStart, int* aAnchor, int aKindMask )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
std::set<ITEM*> disconnected;
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2015-02-18 00:29:54 +00:00
|
|
|
m_world->AllItemsInNet( aStart->Net(), disconnected );
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
for( const JOINT* jt : ConnectedJoints( aStart ) )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
for( ITEM* link : jt->LinkList() )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
if( disconnected.find( link ) != disconnected.end() )
|
|
|
|
disconnected.erase( link );
|
2015-02-18 16:53:46 +00:00
|
|
|
}
|
2015-02-18 00:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int best_dist = INT_MAX;
|
2021-07-19 23:56:05 +00:00
|
|
|
ITEM* best = nullptr;
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
for( ITEM* item : disconnected )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
if( item->OfKind( aKindMask ) )
|
|
|
|
{
|
2016-08-15 15:16:53 +00:00
|
|
|
for( int i = 0; i < item->AnchorCount(); i++ )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
VECTOR2I p = item->Anchor( i );
|
|
|
|
int d = ( p - aStart->Pos() ).EuclideanNorm();
|
|
|
|
|
|
|
|
if( d < best_dist )
|
|
|
|
{
|
|
|
|
best_dist = d;
|
|
|
|
best = item;
|
|
|
|
|
|
|
|
if( aAnchor )
|
|
|
|
*aAnchor = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return best;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-05 00:27:22 +00:00
|
|
|
bool TOPOLOGY::followTrivialPath( LINE* aLine, bool aLeft, ITEM_SET& aSet,
|
|
|
|
std::set<ITEM*>& aVisited, JOINT** aTerminalJoint )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2016-08-29 18:36:05 +00:00
|
|
|
assert( aLine->IsLinked() );
|
|
|
|
|
2021-04-05 00:27:22 +00:00
|
|
|
VECTOR2I anchor = aLeft ? aLine->CPoint( 0 ) : aLine->CPoint( -1 );
|
|
|
|
LINKED_ITEM* last = aLeft ? aLine->Links().front() : aLine->Links().back();
|
|
|
|
JOINT* jt = m_world->FindJoint( anchor, aLine );
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2021-07-19 23:56:05 +00:00
|
|
|
assert( jt != nullptr );
|
2015-02-18 00:29:54 +00:00
|
|
|
|
|
|
|
aVisited.insert( last );
|
|
|
|
|
2015-11-05 09:20:08 +00:00
|
|
|
if( jt->IsNonFanoutVia() || jt->IsTraceWidthChange() )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2021-07-19 23:56:05 +00:00
|
|
|
ITEM* via = nullptr;
|
|
|
|
SEGMENT* next_seg = nullptr;
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
for( ITEM* link : jt->Links().Items() )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
if( link->OfKind( ITEM::VIA_T ) )
|
2015-02-18 00:29:54 +00:00
|
|
|
via = link;
|
2015-02-18 16:53:46 +00:00
|
|
|
else if( aVisited.find( link ) == aVisited.end() )
|
2016-08-29 17:31:13 +00:00
|
|
|
next_seg = static_cast<SEGMENT*>( link );
|
2015-02-18 00:29:54 +00:00
|
|
|
}
|
2015-02-18 16:53:46 +00:00
|
|
|
|
|
|
|
if( !next_seg )
|
2021-04-05 00:27:22 +00:00
|
|
|
{
|
|
|
|
if( aTerminalJoint )
|
|
|
|
*aTerminalJoint = jt;
|
|
|
|
|
2015-02-18 00:29:54 +00:00
|
|
|
return false;
|
2021-04-05 00:27:22 +00:00
|
|
|
}
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
LINE l = m_world->AssembleLine( next_seg );
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2015-08-03 19:11:51 +00:00
|
|
|
VECTOR2I nextAnchor = ( aLeft ? l.CLine().CPoint( -1 ) : l.CLine().CPoint( 0 ) );
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
if( nextAnchor != anchor )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2015-08-03 19:11:51 +00:00
|
|
|
l.Reverse();
|
2015-02-18 00:29:54 +00:00
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
if( aLeft )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2015-11-05 09:20:08 +00:00
|
|
|
if( via )
|
|
|
|
aSet.Prepend( via );
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
aSet.Prepend( l );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-11-05 09:20:08 +00:00
|
|
|
if( via )
|
|
|
|
aSet.Add( via );
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
aSet.Add( l );
|
2015-02-18 00:29:54 +00:00
|
|
|
}
|
|
|
|
|
2021-04-05 00:27:22 +00:00
|
|
|
return followTrivialPath( &l, aLeft, aSet, aVisited, aTerminalJoint );
|
2015-02-18 00:29:54 +00:00
|
|
|
}
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2021-04-05 00:27:22 +00:00
|
|
|
if( aTerminalJoint )
|
|
|
|
*aTerminalJoint = jt;
|
|
|
|
|
2015-02-18 00:29:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2021-04-05 00:27:22 +00:00
|
|
|
const ITEM_SET TOPOLOGY::AssembleTrivialPath( ITEM* aStart,
|
2021-12-05 18:35:28 +00:00
|
|
|
std::pair<JOINT*, JOINT*>* aTerminalJoints,
|
|
|
|
bool aFollowLockedSegments )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2021-04-06 13:07:00 +00:00
|
|
|
ITEM_SET path;
|
2016-08-29 17:31:13 +00:00
|
|
|
std::set<ITEM*> visited;
|
2021-04-06 13:07:00 +00:00
|
|
|
LINKED_ITEM* seg = nullptr;
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2021-04-06 13:07:00 +00:00
|
|
|
if( aStart->Kind() == ITEM::VIA_T )
|
2016-08-15 15:16:47 +00:00
|
|
|
{
|
2021-04-06 13:07:00 +00:00
|
|
|
VIA* via = static_cast<VIA*>( aStart );
|
|
|
|
JOINT* jt = m_world->FindJoint( via->Pos(), via );
|
2016-08-15 15:16:47 +00:00
|
|
|
|
2016-08-15 15:16:53 +00:00
|
|
|
if( !jt->IsNonFanoutVia() )
|
2016-08-29 17:31:13 +00:00
|
|
|
return ITEM_SET();
|
2016-08-15 15:16:47 +00:00
|
|
|
|
2021-04-06 13:07:00 +00:00
|
|
|
for( const ITEM_SET::ENTRY& entry : jt->Links().Items() )
|
|
|
|
{
|
|
|
|
if( entry.item->OfKind( ITEM::SEGMENT_T | ITEM::ARC_T ) )
|
|
|
|
{
|
|
|
|
seg = static_cast<LINKED_ITEM*>( entry.item );
|
2016-08-15 15:16:47 +00:00
|
|
|
break;
|
2021-04-06 13:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( aStart->OfKind( ITEM::SEGMENT_T | ITEM::ARC_T ) )
|
|
|
|
{
|
|
|
|
seg = static_cast<LINKED_ITEM*>( aStart );
|
2016-08-15 15:16:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( !seg )
|
2016-08-29 17:31:13 +00:00
|
|
|
return ITEM_SET();
|
2016-08-15 15:16:47 +00:00
|
|
|
|
2021-12-05 18:35:28 +00:00
|
|
|
// Assemble a line following through locked segments
|
|
|
|
// TODO: consider if we want to allow tuning lines with different widths in the future
|
|
|
|
LINE l = m_world->AssembleLine( seg, nullptr, false, true );
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
path.Add( l );
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2021-04-05 00:27:22 +00:00
|
|
|
JOINT* jointA = nullptr;
|
|
|
|
JOINT* jointB = nullptr;
|
|
|
|
|
2022-11-11 21:11:12 +00:00
|
|
|
followTrivialPath( &l, false, path, visited, &jointB );
|
|
|
|
followTrivialPath( &l, true, path, visited, &jointA );
|
2021-04-05 00:27:22 +00:00
|
|
|
|
|
|
|
if( aTerminalJoints )
|
|
|
|
{
|
|
|
|
wxASSERT( jointA && jointB );
|
|
|
|
*aTerminalJoints = std::make_pair( jointA, jointB );
|
|
|
|
}
|
2015-02-18 00:29:54 +00:00
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2021-04-05 00:27:22 +00:00
|
|
|
const ITEM_SET TOPOLOGY::AssembleTuningPath( ITEM* aStart, SOLID** aStartPad, SOLID** aEndPad )
|
|
|
|
{
|
|
|
|
std::pair<JOINT*, JOINT*> joints;
|
2021-12-05 18:35:28 +00:00
|
|
|
ITEM_SET initialPath = AssembleTrivialPath( aStart, &joints, true );
|
2021-04-05 00:27:22 +00:00
|
|
|
|
|
|
|
PAD* padA = nullptr;
|
|
|
|
PAD* padB = nullptr;
|
|
|
|
|
2021-04-06 13:07:00 +00:00
|
|
|
auto getPadFromJoint =
|
|
|
|
[]( JOINT* aJoint, PAD** aTargetPad, SOLID** aTargetSolid )
|
2021-04-05 00:27:22 +00:00
|
|
|
{
|
2021-04-06 13:07:00 +00:00
|
|
|
for( ITEM* item : aJoint->LinkList() )
|
|
|
|
{
|
|
|
|
if( item->OfKind( ITEM::SOLID_T ) )
|
|
|
|
{
|
|
|
|
BOARD_ITEM* bi = static_cast<SOLID*>( item )->Parent();
|
2021-04-05 00:27:22 +00:00
|
|
|
|
2021-04-06 13:07:00 +00:00
|
|
|
if( bi->Type() == PCB_PAD_T )
|
|
|
|
{
|
|
|
|
*aTargetPad = static_cast<PAD*>( bi );
|
2021-04-05 00:27:22 +00:00
|
|
|
|
2021-04-06 13:07:00 +00:00
|
|
|
if( aTargetSolid )
|
|
|
|
*aTargetSolid = static_cast<SOLID*>( item );
|
|
|
|
}
|
2021-04-05 00:27:22 +00:00
|
|
|
|
2021-04-06 13:07:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-04-05 00:27:22 +00:00
|
|
|
|
2021-04-06 13:07:00 +00:00
|
|
|
if( joints.first )
|
|
|
|
getPadFromJoint( joints.first, &padA, aStartPad );
|
2021-04-05 00:27:22 +00:00
|
|
|
|
2021-04-06 13:07:00 +00:00
|
|
|
if( joints.second )
|
|
|
|
getPadFromJoint( joints.second, &padB, aEndPad );
|
2021-04-05 00:27:22 +00:00
|
|
|
|
2021-04-06 13:07:00 +00:00
|
|
|
if( !padA && !padB )
|
|
|
|
return initialPath;
|
2021-04-05 00:27:22 +00:00
|
|
|
|
|
|
|
auto clipLineToPad =
|
|
|
|
[]( SHAPE_LINE_CHAIN& aLine, PAD* aPad, bool aForward = true )
|
|
|
|
{
|
|
|
|
const std::shared_ptr<SHAPE_POLY_SET>& shape = aPad->GetEffectivePolygon();
|
|
|
|
|
|
|
|
int start = aForward ? 0 : aLine.PointCount() - 1;
|
|
|
|
int delta = aForward ? 1 : -1;
|
|
|
|
|
|
|
|
// Skip the "first" (or last) vertex, we already know it's contained in the pad
|
|
|
|
int clip = start;
|
|
|
|
|
|
|
|
for( int vertex = start + delta;
|
|
|
|
aForward ? vertex < aLine.PointCount() : vertex >= 0;
|
|
|
|
vertex += delta )
|
|
|
|
{
|
|
|
|
SEG seg( aLine.GetPoint( vertex ), aLine.GetPoint( vertex - delta ) );
|
|
|
|
|
|
|
|
bool containsA = shape->Contains( seg.A );
|
|
|
|
bool containsB = shape->Contains( seg.B );
|
|
|
|
|
|
|
|
if( containsA && containsB )
|
|
|
|
{
|
|
|
|
// Whole segment is inside: clip out this segment
|
|
|
|
clip = vertex;
|
|
|
|
}
|
|
|
|
else if( containsB &&
|
|
|
|
( aForward ? vertex < aLine.PointCount() - 1 : vertex > 0 ) )
|
|
|
|
{
|
|
|
|
// Only one point inside: Find the intersection
|
|
|
|
VECTOR2I loc;
|
|
|
|
|
|
|
|
if( shape->Collide( seg, 0, nullptr, &loc ) )
|
|
|
|
{
|
|
|
|
aLine.Replace( vertex - delta, vertex - delta, loc );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !aForward && clip < start )
|
|
|
|
aLine.Remove( clip + 1, start );
|
|
|
|
else if( clip > start )
|
|
|
|
aLine.Remove( start, clip - 1 );
|
|
|
|
|
|
|
|
// Now connect the dots
|
2021-09-23 00:25:10 +00:00
|
|
|
aLine.Insert( aForward ? 0 : aLine.PointCount(), aPad->GetPosition() );
|
2021-04-05 00:27:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
auto processPad =
|
2021-05-29 19:52:44 +00:00
|
|
|
[&]( JOINT* aJoint, PAD* aPad )
|
2021-04-05 00:27:22 +00:00
|
|
|
{
|
|
|
|
const std::shared_ptr<SHAPE_POLY_SET>& shape = aPad->GetEffectivePolygon();
|
|
|
|
|
|
|
|
for( int idx = 0; idx < initialPath.Size(); idx++ )
|
|
|
|
{
|
|
|
|
if( initialPath[idx]->Kind() != ITEM::LINE_T )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
LINE* line = static_cast<LINE*>( initialPath[idx] );
|
|
|
|
|
2021-05-29 19:52:44 +00:00
|
|
|
if( !aPad->FlashLayer( line->Layer() ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const std::vector<VECTOR2I>& points = line->CLine().CPoints();
|
|
|
|
|
|
|
|
if( points.front() != aJoint->Pos() && points.back() != aJoint->Pos() )
|
|
|
|
continue;
|
|
|
|
|
2021-04-05 00:27:22 +00:00
|
|
|
SHAPE_LINE_CHAIN& slc = line->Line();
|
|
|
|
|
|
|
|
if( shape->Contains( slc.CPoint( 0 ) ) )
|
|
|
|
clipLineToPad( slc, aPad, true );
|
|
|
|
else if( shape->Contains( slc.CPoint( -1 ) ) )
|
|
|
|
clipLineToPad( slc, aPad, false );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if( padA )
|
2021-05-29 19:52:44 +00:00
|
|
|
processPad( joints.first, padA );
|
2021-04-05 00:27:22 +00:00
|
|
|
|
|
|
|
if( padB )
|
2021-05-29 19:52:44 +00:00
|
|
|
processPad( joints.second, padB );
|
2021-04-05 00:27:22 +00:00
|
|
|
|
|
|
|
return initialPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
const ITEM_SET TOPOLOGY::ConnectedItems( JOINT* aStart, int aKindMask )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
return ITEM_SET();
|
2015-02-18 00:29:54 +00:00
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
const ITEM_SET TOPOLOGY::ConnectedItems( ITEM* aStart, int aKindMask )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
return ITEM_SET();
|
2015-02-18 00:29:54 +00:00
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2018-10-08 14:30:17 +00:00
|
|
|
bool commonParallelProjection( SEG p, SEG n, SEG &pClip, SEG& nClip );
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2015-07-02 14:11:15 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
bool TOPOLOGY::AssembleDiffPair( ITEM* aStart, DIFF_PAIR& aPair )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
int refNet = aStart->Net();
|
2016-08-15 15:16:47 +00:00
|
|
|
int coupledNet = m_world->GetRuleResolver()->DpCoupledNet( refNet );
|
2015-02-18 16:53:46 +00:00
|
|
|
|
|
|
|
if( coupledNet < 0 )
|
2015-02-18 00:29:54 +00:00
|
|
|
return false;
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
std::set<ITEM*> coupledItems;
|
2015-02-18 00:29:54 +00:00
|
|
|
|
|
|
|
m_world->AllItemsInNet( coupledNet, coupledItems );
|
|
|
|
|
2021-07-19 23:56:05 +00:00
|
|
|
SEGMENT* coupledSeg = nullptr, *refSeg;
|
2015-02-18 00:29:54 +00:00
|
|
|
int minDist = std::numeric_limits<int>::max();
|
|
|
|
|
2021-07-19 23:56:05 +00:00
|
|
|
if( ( refSeg = dyn_cast<SEGMENT*>( aStart ) ) != nullptr )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
for( ITEM* item : coupledItems )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
if( SEGMENT* s = dyn_cast<SEGMENT*>( item ) )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2021-07-19 23:56:05 +00:00
|
|
|
if( s->Layers().Start() == refSeg->Layers().Start() &&
|
|
|
|
s->Width() == refSeg->Width() )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
int dist = s->Seg().Distance( refSeg->Seg() );
|
2021-11-22 23:21:38 +00:00
|
|
|
bool isParallel = refSeg->Seg().ApproxParallel( s->Seg(), DP_PARALLELITY_THRESHOLD );
|
2015-07-02 14:10:49 +00:00
|
|
|
SEG p_clip, n_clip;
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2021-07-19 23:56:05 +00:00
|
|
|
bool isCoupled = commonParallelProjection( refSeg->Seg(), s->Seg(), p_clip,
|
|
|
|
n_clip );
|
2015-07-02 14:10:49 +00:00
|
|
|
|
|
|
|
if( isParallel && isCoupled && dist < minDist )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
minDist = dist;
|
|
|
|
coupledSeg = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-03 19:11:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-18 00:29:54 +00:00
|
|
|
return false;
|
2015-08-03 19:11:51 +00:00
|
|
|
}
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
if( !coupledSeg )
|
2015-02-18 00:29:54 +00:00
|
|
|
return false;
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
LINE lp = m_world->AssembleLine( refSeg );
|
|
|
|
LINE ln = m_world->AssembleLine( coupledSeg );
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2016-08-15 15:16:47 +00:00
|
|
|
if( m_world->GetRuleResolver()->DpNetPolarity( refNet ) < 0 )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2015-02-18 16:53:46 +00:00
|
|
|
std::swap( lp, ln );
|
2015-02-18 00:29:54 +00:00
|
|
|
}
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2015-08-03 19:11:51 +00:00
|
|
|
int gap = -1;
|
|
|
|
|
2021-11-22 00:33:32 +00:00
|
|
|
if( refSeg->Seg().ApproxParallel( coupledSeg->Seg(), DP_PARALLELITY_THRESHOLD ) )
|
2015-07-02 14:11:15 +00:00
|
|
|
{
|
|
|
|
// Segments are parallel -> compute pair gap
|
|
|
|
const VECTOR2I refDir = refSeg->Anchor( 1 ) - refSeg->Anchor( 0 );
|
|
|
|
const VECTOR2I displacement = refSeg->Anchor( 1 ) - coupledSeg->Anchor( 1 );
|
2015-08-03 19:11:51 +00:00
|
|
|
gap = (int) std::abs( refDir.Cross( displacement ) / refDir.EuclideanNorm() ) - lp.Width();
|
2015-07-02 14:10:02 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
aPair = DIFF_PAIR( lp, ln );
|
2015-08-03 19:11:51 +00:00
|
|
|
aPair.SetWidth( lp.Width() );
|
|
|
|
aPair.SetLayers( lp.Layers() );
|
2015-07-02 14:10:02 +00:00
|
|
|
aPair.SetGap( gap );
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2015-02-18 00:29:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
2015-08-19 16:07:16 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
const std::set<ITEM*> TOPOLOGY::AssembleCluster( ITEM* aStart, int aLayer )
|
2015-08-19 16:07:16 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
std::set<ITEM*> visited;
|
|
|
|
std::deque<ITEM*> pending;
|
2015-08-19 16:07:16 +00:00
|
|
|
|
|
|
|
pending.push_back( aStart );
|
|
|
|
|
|
|
|
while( !pending.empty() )
|
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
NODE::OBSTACLES obstacles;
|
|
|
|
ITEM* top = pending.front();
|
2015-08-19 16:07:16 +00:00
|
|
|
|
|
|
|
pending.pop_front();
|
|
|
|
|
|
|
|
visited.insert( top );
|
|
|
|
|
2022-06-03 21:03:24 +00:00
|
|
|
m_world->QueryColliding( top, obstacles, ITEM::ANY_T, -1, false, 0 ); // only query touching objects
|
2015-08-19 16:07:16 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
for( OBSTACLE& obs : obstacles )
|
2015-08-19 16:07:16 +00:00
|
|
|
{
|
2022-06-03 21:03:24 +00:00
|
|
|
bool trackOnTrack = ( obs.m_item->Net() != top->Net() ) && obs.m_item->OfKind( ITEM::SEGMENT_T ) && top->OfKind( ITEM::SEGMENT_T );
|
|
|
|
|
|
|
|
if( trackOnTrack )
|
|
|
|
continue;
|
|
|
|
|
2021-07-19 23:56:05 +00:00
|
|
|
if( visited.find( obs.m_item ) == visited.end() &&
|
|
|
|
obs.m_item->Layers().Overlaps( aLayer ) && !( obs.m_item->Marker() & MK_HEAD ) )
|
2015-08-19 16:07:16 +00:00
|
|
|
{
|
|
|
|
visited.insert( obs.m_item );
|
|
|
|
pending.push_back( obs.m_item );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return visited;
|
|
|
|
}
|
2016-08-29 14:38:11 +00:00
|
|
|
|
|
|
|
}
|