kicad/pcbnew/router/pns_shove.cpp

1713 lines
43 KiB
C++
Raw Normal View History

/*
* KiRouter - a push-and-(sometimes-)shove PCB router
*
* Copyright (C) 2013-2014 CERN
* Copyright (C) 2016-2020 KiCad Developers, see AUTHORS.txt for contributors.
* Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
2013-09-26 21:53:54 +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
*
* 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
*
* 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 <deque>
#include <cassert>
#include <math/box2.h>
#include <geometry/shape_compound.h>
#include "pns_arc.h"
#include "pns_line.h"
#include "pns_node.h"
#include "pns_debug_decorator.h"
#include "pns_walkaround.h"
#include "pns_shove.h"
#include "pns_solid.h"
#include "pns_optimizer.h"
#include "pns_via.h"
#include "pns_utils.h"
#include "pns_router.h"
#include "pns_topology.h"
#include "time_limit.h"
// fixme - move all logger calls to debug decorator
typedef VECTOR2I::extended_type ecoord;
namespace PNS {
void SHOVE::replaceItems( ITEM* aOld, std::unique_ptr< ITEM > aNew )
{
OPT_BOX2I changed_area = ChangedArea( aOld, aNew.get() );
if( changed_area )
m_affectedArea = m_affectedArea ? m_affectedArea->Merge( *changed_area ) : *changed_area;
m_currentNode->Replace( aOld, std::move( aNew ) );
}
void SHOVE::replaceLine( LINE& aOld, LINE& aNew )
{
OPT_BOX2I changed_area = ChangedArea( aOld, aNew );
2015-02-18 16:53:46 +00:00
if( changed_area )
m_affectedArea = m_affectedArea ? m_affectedArea->Merge( *changed_area ) : *changed_area;
m_currentNode->Replace( aOld, aNew );
}
int SHOVE::getClearance( const ITEM* aA, const ITEM* aB ) const
{
2015-02-18 16:53:46 +00:00
if( m_forceClearance >= 0 )
return m_forceClearance;
return m_currentNode->GetClearance( aA, aB );
}
int SHOVE::getHoleClearance( const ITEM* aA, const ITEM* aB ) const
{
if( m_forceClearance >= 0 )
return m_forceClearance;
return m_currentNode->GetHoleClearance( aA, aB );
}
2015-02-18 16:53:46 +00:00
void SHOVE::sanityCheck( LINE* aOld, LINE* aNew )
{
assert( aOld->CPoint( 0 ) == aNew->CPoint( 0 ) );
assert( aOld->CPoint( -1 ) == aNew->CPoint( -1 ) );
}
SHOVE::SHOVE( NODE* aWorld, ROUTER* aRouter ) :
ALGO_BASE( aRouter )
{
m_forceClearance = -1;
2013-09-26 21:53:54 +00:00
m_root = aWorld;
m_currentNode = aWorld;
SetDebugDecorator( aRouter->GetInterface()->GetDebugDecorator() );
// Initialize other temporary variables:
m_draggedVia = NULL;
m_iter = 0;
m_multiLineMode = false;
2020-03-30 07:24:56 +00:00
m_restrictSpringbackTagId = 0;
}
2013-09-26 21:53:54 +00:00
SHOVE::~SHOVE()
{
}
LINE SHOVE::assembleLine( const LINKED_ITEM* aSeg, int* aIndex )
2013-09-26 21:53:54 +00:00
{
return m_currentNode->AssembleLine( const_cast<LINKED_ITEM*>( aSeg ), aIndex, true );
}
2013-09-26 21:53:54 +00:00
2021-01-05 16:40:34 +00:00
// A dumb function that checks if the shoved line is shoved the right way, e.g. visually
// "outwards" of the line/via applying pressure on it. Unfortunately there's no mathematical
// concept of orientation of an open curve, so we use some primitive heuristics: if the shoved
// line wraps around the start of the "pusher", it's likely shoved in wrong direction.
2013-09-26 21:53:54 +00:00
// Update: there's no concept of an orientation of an open curve, but nonetheless Tom's dumb as.... (censored)
// Two open curves put together make a closed polygon... Tom should learn high school geometry!
2021-01-05 16:40:34 +00:00
bool SHOVE::checkShoveDirection( const LINE& aCurLine, const LINE& aObstacleLine,
const LINE& aShovedLine ) const
{
2021-01-05 16:40:34 +00:00
SHAPE_LINE_CHAIN::POINT_INSIDE_TRACKER checker( aCurLine.CPoint( 0) );
checker.AddPolyline( aObstacleLine.CLine() );
checker.AddPolyline( aShovedLine.CLine().Reverse() );
bool inside = checker.IsInside();
2014-11-14 19:19:00 +00:00
return !inside;
}
2021-01-05 16:40:34 +00:00
/*
* Push aObstacleLine away from aCurLine's via by the clearance distance, returning the result
* in aResultLine.
*
* Must be called only when aCurLine itself is on another layer (or has no segments) so that it
* can be ignored.
*/
SHOVE::SHOVE_STATUS SHOVE::shoveLineFromLoneVia( const LINE& aCurLine, const LINE& aObstacleLine,
LINE& aResultLine )
{
2021-01-05 16:40:34 +00:00
// Build a hull for aCurLine's via and re-walk aObstacleLine around it.
int obstacleLineWidth = aObstacleLine.Width();
int clearance = getClearance( &aCurLine, &aObstacleLine );
int holeClearance = getHoleClearance( &aCurLine.Via(), &aObstacleLine );
2021-01-05 16:40:34 +00:00
if( holeClearance + aCurLine.Via().Drill() / 2 > clearance + aCurLine.Via().Diameter() / 2 )
clearance = holeClearance + aCurLine.Via().Drill() / 2 - aCurLine.Via().Diameter() / 2;
2021-01-05 16:40:34 +00:00
SHAPE_LINE_CHAIN hull = aCurLine.Via().Hull( clearance, obstacleLineWidth, aCurLine.Layer() );
SHAPE_LINE_CHAIN path_cw;
SHAPE_LINE_CHAIN path_ccw;
2013-09-26 21:53:54 +00:00
2021-01-05 16:40:34 +00:00
if( ! aObstacleLine.Walkaround( hull, path_cw, true ) )
return SH_INCOMPLETE;
2021-01-05 16:40:34 +00:00
if( ! aObstacleLine.Walkaround( hull, path_ccw, false ) )
return SH_INCOMPLETE;
const SHAPE_LINE_CHAIN& shortest = path_ccw.Length() < path_cw.Length() ? path_ccw : path_cw;
if( shortest.PointCount() < 2 )
return SH_INCOMPLETE;
2021-01-05 16:40:34 +00:00
if( aObstacleLine.CPoint( -1 ) != shortest.CPoint( -1 ) )
return SH_INCOMPLETE;
2021-01-05 16:40:34 +00:00
if( aObstacleLine.CPoint( 0 ) != shortest.CPoint( 0 ) )
return SH_INCOMPLETE;
2021-01-05 16:40:34 +00:00
aResultLine.SetShape( shortest );
2013-09-26 21:53:54 +00:00
2021-01-05 16:40:34 +00:00
if( aResultLine.Collide( &aCurLine, m_currentNode ) )
return SH_INCOMPLETE;
2013-09-26 21:53:54 +00:00
return SH_OK;
}
/*
2021-01-05 16:40:34 +00:00
* Re-walk aObstacleLine around the given set of hulls, returning the result in aResultLine.
*/
2021-01-05 16:40:34 +00:00
SHOVE::SHOVE_STATUS SHOVE::shoveLineToHullSet( const LINE& aCurLine, const LINE& aObstacleLine,
LINE& aResultLine, const HULL_SET& aHulls )
{
2021-01-05 16:40:34 +00:00
const SHAPE_LINE_CHAIN& obs = aObstacleLine.CLine();
int attempt;
2013-09-26 21:53:54 +00:00
for( attempt = 0; attempt < 4; attempt++ )
{
bool invertTraversal = ( attempt >= 2 );
bool clockwise = attempt % 2;
int vFirst = -1, vLast = -1;
2013-09-26 21:53:54 +00:00
SHAPE_LINE_CHAIN path;
2021-01-05 16:40:34 +00:00
LINE l( aObstacleLine );
2013-09-26 21:53:54 +00:00
for( int i = 0; i < (int) aHulls.size(); i++ )
{
const SHAPE_LINE_CHAIN& hull = aHulls[invertTraversal ? aHulls.size() - 1 - i : i];
2013-09-26 21:53:54 +00:00
if( ! l.Walkaround( hull, path, clockwise ) )
return SH_INCOMPLETE;
path.Simplify();
l.SetShape( path );
}
2014-11-14 19:19:00 +00:00
2016-08-15 15:16:53 +00:00
for( int i = 0; i < std::min( path.PointCount(), obs.PointCount() ); i++ )
2013-09-26 21:53:54 +00:00
{
if( path.CPoint( i ) != obs.CPoint( i ) )
{
vFirst = i;
break;
}
2013-09-26 21:53:54 +00:00
}
int k = obs.PointCount() - 1;
for( int i = path.PointCount() - 1; i >= 0 && k >= 0; i--, k-- )
{
if( path.CPoint( i ) != obs.CPoint( k ) )
{
vLast = i;
break;
}
}
2013-09-26 21:53:54 +00:00
2021-01-05 16:40:34 +00:00
if( ( vFirst < 0 || vLast < 0 ) && !path.CompareGeometry( aObstacleLine.CLine() ) )
{
wxLogTrace( "PNS", "attempt %d fail vfirst-last", attempt );
continue;
}
2013-09-26 21:53:54 +00:00
if( path.CPoint( -1 ) != obs.CPoint( -1 ) || path.CPoint( 0 ) != obs.CPoint( 0 ) )
{
wxLogTrace( "PNS", "attempt %d fail vend-start\n", attempt );
continue;
}
2013-09-26 21:53:54 +00:00
2021-01-05 16:40:34 +00:00
if( !checkShoveDirection( aCurLine, aObstacleLine, l ) )
{
wxLogTrace( "PNS", "attempt %d fail direction-check", attempt );
2021-01-05 16:40:34 +00:00
aResultLine.SetShape( l.CLine() );
continue;
}
2013-09-26 21:53:54 +00:00
if( path.SelfIntersecting() )
{
wxLogTrace( "PNS", "attempt %d fail self-intersect", attempt );
continue;
}
2014-11-14 19:19:00 +00:00
2021-01-05 16:40:34 +00:00
bool colliding = l.Collide( &aCurLine, m_currentNode );
2014-11-14 19:19:00 +00:00
#ifdef DEBUG
char str[128];
sprintf( str, "att-%d-shoved", attempt );
Dbg()->AddLine( l.CLine(), 3, 20000, str );
#endif
2021-01-05 16:40:34 +00:00
if(( aCurLine.Marker() & MK_HEAD ) && !colliding )
{
2021-01-05 16:40:34 +00:00
JOINT* jtStart = m_currentNode->FindJoint( aCurLine.CPoint( 0 ), &aCurLine );
2014-11-14 19:19:00 +00:00
for( ITEM* item : jtStart->LinkList() )
{
if( item->Collide( &l, m_currentNode ) )
colliding = true;
}
}
2014-11-14 19:19:00 +00:00
if( colliding )
{
wxLogTrace( "PNS", "attempt %d fail coll-check", attempt );
continue;
}
2013-09-26 21:53:54 +00:00
2021-01-05 16:40:34 +00:00
aResultLine.SetShape( l.CLine() );
2014-11-14 19:19:00 +00:00
return SH_OK;
}
2013-09-26 21:53:54 +00:00
return SH_INCOMPLETE;
}
2013-09-26 21:53:54 +00:00
/*
2021-01-05 16:40:34 +00:00
* Push aObstacleLine line away from aCurLine by the clearance distance, and return the result in
* aResultLine.
*/
2021-01-05 16:40:34 +00:00
SHOVE::SHOVE_STATUS SHOVE::ShoveObstacleLine( const LINE& aCurLine, const LINE& aObstacleLine,
LINE& aResultLine )
{
2021-01-05 16:40:34 +00:00
aResultLine.ClearLinks();
2013-09-26 21:53:54 +00:00
bool obstacleIsHead = false;
2013-09-26 21:53:54 +00:00
2021-01-05 16:40:34 +00:00
for( LINKED_ITEM* s : aObstacleLine.Links() )
{
if( s->Marker() & MK_HEAD )
{
obstacleIsHead = true;
2013-09-26 21:53:54 +00:00
break;
}
}
SHOVE_STATUS rv;
2021-01-05 16:40:34 +00:00
bool viaOnEnd = aCurLine.EndsWithVia();
2013-09-26 21:53:54 +00:00
2021-01-05 16:40:34 +00:00
if( viaOnEnd && ( !aCurLine.LayersOverlap( &aObstacleLine ) || aCurLine.SegmentCount() == 0 ) )
{
2021-01-05 16:40:34 +00:00
// Shove aObstacleLine to the hull of aCurLine's via.
rv = shoveLineFromLoneVia( aCurLine, aObstacleLine, aResultLine );
}
else
{
2021-01-05 16:40:34 +00:00
// Build a set of hulls around the segments of aCurLine. Hulls are at the clearance
// distance + aObstacleLine's linewidth so that when re-walking aObstacleLine along the
// hull it will be at the appropriate clearance.
int obstacleLineWidth = aObstacleLine.Width();
int clearance = getClearance( &aCurLine, &aObstacleLine ) + 1;
int currentLineSegmentCount = aCurLine.SegmentCount();
HULL_SET hulls;
2021-01-05 16:40:34 +00:00
hulls.reserve( currentLineSegmentCount + 1 );
2013-09-26 21:53:54 +00:00
#ifdef DEBUG
2020-12-02 18:42:37 +00:00
Dbg()->Message( wxString::Format( "shove process-single: cur net %d obs %d cl %d",
2021-01-05 16:40:34 +00:00
aCurLine.Net(),
aObstacleLine.Net(),
2020-12-02 18:42:37 +00:00
clearance ) );
#endif
2021-01-05 16:40:34 +00:00
for( int i = 0; i < currentLineSegmentCount; i++ )
2013-09-26 21:53:54 +00:00
{
2021-01-05 16:40:34 +00:00
SEGMENT seg( aCurLine, aCurLine.CSegment( i ) );
SHAPE_LINE_CHAIN hull = seg.Hull( clearance, obstacleLineWidth, aObstacleLine.Layer() );
hulls.push_back( hull );
2013-09-26 21:53:54 +00:00
}
if( viaOnEnd )
{
2021-01-05 16:40:34 +00:00
const VIA& via = aCurLine.Via();
int viaClearance = getClearance( &via, &aObstacleLine );
int holeClearance = getHoleClearance( &via, &aObstacleLine );
if( holeClearance + via.Drill() / 2 > viaClearance + via.Diameter() / 2 )
viaClearance = holeClearance + via.Drill() / 2 - via.Diameter() / 2;
2021-01-05 16:40:34 +00:00
hulls.push_back( aCurLine.Via().Hull( viaClearance, obstacleLineWidth ) );
}
2013-09-26 21:53:54 +00:00
#ifdef DEBUG
char str[128];
sprintf( str, "current-cl-%d", clearance );
2021-01-05 16:40:34 +00:00
Dbg()->AddLine( aCurLine.CLine(), 5, 20000, str );
#endif
2021-01-05 16:40:34 +00:00
rv = shoveLineToHullSet( aCurLine, aObstacleLine, aResultLine, hulls );
}
2014-11-14 19:19:00 +00:00
if( obstacleIsHead )
2021-01-05 16:40:34 +00:00
aResultLine.Mark( aResultLine.Marker() | MK_HEAD );
2013-09-26 21:53:54 +00:00
return rv;
}
2013-09-26 21:53:54 +00:00
/*
* TODO describe....
*/
SHOVE::SHOVE_STATUS SHOVE::onCollidingSegment( LINE& aCurrent, SEGMENT* aObstacleSeg )
{
int segIndex;
LINE obstacleLine = assembleLine( aObstacleSeg, &segIndex );
LINE shovedLine( obstacleLine );
SEGMENT tmp( *aObstacleSeg );
2014-11-14 19:19:00 +00:00
2016-08-15 15:16:48 +00:00
if( obstacleLine.HasLockedSegments() )
return SH_TRY_WALK;
2021-01-05 16:40:34 +00:00
SHOVE_STATUS rv = ShoveObstacleLine( aCurrent, obstacleLine, shovedLine );
2013-09-26 21:53:54 +00:00
const double extensionWalkThreshold = 1.0;
double obsLen = obstacleLine.CLine().Length();
double shovedLen = shovedLine.CLine().Length();
double extensionFactor = 0.0;
if( obsLen != 0.0f )
extensionFactor = shovedLen / obsLen - 1.0;
if( extensionFactor > extensionWalkThreshold )
return SH_TRY_WALK;
assert( obstacleLine.LayersOverlap( &shovedLine ) );
#if 0
m_logger.NewGroup( "on-colliding-segment", m_iter );
m_logger.Log( &tmp, 0, "obstacle-segment" );
m_logger.Log( &aCurrent, 1, "current-line" );
m_logger.Log( &obstacleLine, 2, "obstacle-line" );
m_logger.Log( &shovedLine, 3, "shoved-line" );
#endif
2014-11-14 19:19:00 +00:00
if( rv == SH_OK )
{
if( shovedLine.Marker() & MK_HEAD )
{
if( m_multiLineMode )
return SH_INCOMPLETE;
2015-02-18 16:53:46 +00:00
m_newHead = shovedLine;
}
2014-11-14 19:19:00 +00:00
int rank = aCurrent.Rank();
shovedLine.SetRank( rank - 1 );
sanityCheck( &obstacleLine, &shovedLine );
replaceLine( obstacleLine, shovedLine );
if( !pushLineStack( shovedLine ) )
2015-07-02 14:09:32 +00:00
rv = SH_INCOMPLETE;
2013-09-26 21:53:54 +00:00
}
return rv;
}
/*
* TODO describe....
*/
SHOVE::SHOVE_STATUS SHOVE::onCollidingArc( LINE& aCurrent, ARC* aObstacleArc )
{
int segIndex;
LINE obstacleLine = assembleLine( aObstacleArc, &segIndex );
LINE shovedLine( obstacleLine );
ARC tmp( *aObstacleArc );
if( obstacleLine.HasLockedSegments() )
return SH_TRY_WALK;
2021-01-05 16:40:34 +00:00
SHOVE_STATUS rv = ShoveObstacleLine( aCurrent, obstacleLine, shovedLine );
const double extensionWalkThreshold = 1.0;
double obsLen = obstacleLine.CLine().Length();
double shovedLen = shovedLine.CLine().Length();
double extensionFactor = 0.0;
if( obsLen != 0.0f )
extensionFactor = shovedLen / obsLen - 1.0;
if( extensionFactor > extensionWalkThreshold )
return SH_TRY_WALK;
assert( obstacleLine.LayersOverlap( &shovedLine ) );
#if 0
m_logger.NewGroup( "on-colliding-segment", m_iter );
m_logger.Log( &tmp, 0, "obstacle-segment" );
m_logger.Log( &aCurrent, 1, "current-line" );
m_logger.Log( &obstacleLine, 2, "obstacle-line" );
m_logger.Log( &shovedLine, 3, "shoved-line" );
#endif
if( rv == SH_OK )
{
if( shovedLine.Marker() & MK_HEAD )
{
if( m_multiLineMode )
return SH_INCOMPLETE;
m_newHead = shovedLine;
}
int rank = aCurrent.Rank();
shovedLine.SetRank( rank - 1 );
sanityCheck( &obstacleLine, &shovedLine );
replaceLine( obstacleLine, shovedLine );
if( !pushLineStack( shovedLine ) )
rv = SH_INCOMPLETE;
}
return rv;
}
/*
* TODO describe....
*/
SHOVE::SHOVE_STATUS SHOVE::onCollidingLine( LINE& aCurrent, LINE& aObstacle )
{
LINE shovedLine( aObstacle );
2021-01-05 16:40:34 +00:00
SHOVE_STATUS rv = ShoveObstacleLine( aCurrent, aObstacle, shovedLine );
#if 0
m_logger.NewGroup( "on-colliding-line", m_iter );
m_logger.Log( &aObstacle, 0, "obstacle-line" );
m_logger.Log( &aCurrent, 1, "current-line" );
m_logger.Log( &shovedLine, 3, "shoved-line" );
#endif
if( rv == SH_OK )
2013-09-26 21:53:54 +00:00
{
if( shovedLine.Marker() & MK_HEAD )
{
2015-02-18 16:53:46 +00:00
if( m_multiLineMode )
return SH_INCOMPLETE;
m_newHead = shovedLine;
}
sanityCheck( &aObstacle, &shovedLine );
replaceLine( aObstacle, shovedLine );
int rank = aObstacle.Rank();
shovedLine.SetRank( rank - 1 );
2014-11-14 19:19:00 +00:00
if( !pushLineStack( shovedLine ) )
{
2015-07-02 14:09:32 +00:00
rv = SH_INCOMPLETE;
}
2013-09-26 21:53:54 +00:00
}
return rv;
}
/*
* TODO describe....
*/
SHOVE::SHOVE_STATUS SHOVE::onCollidingSolid( LINE& aCurrent, ITEM* aObstacle )
{
WALKAROUND walkaround( m_currentNode, Router() );
LINE walkaroundLine( aCurrent );
2014-11-14 19:19:00 +00:00
if( aCurrent.EndsWithVia() )
{
VIA vh = aCurrent.Via();
VIA* via = NULL;
JOINT* jtStart = m_currentNode->FindJoint( vh.Pos(), &aCurrent );
if( !jtStart )
return SH_INCOMPLETE;
for( ITEM* item : jtStart->LinkList() )
{
if( item->OfKind( ITEM::VIA_T ) )
{
via = (VIA*) item;
break;
}
}
2014-11-14 19:19:00 +00:00
if( via && via->Collide( aObstacle, m_currentNode ) )
return onCollidingVia( aObstacle, via );
}
TOPOLOGY topo( m_currentNode );
2014-11-14 19:19:00 +00:00
std::set<ITEM*> cluster = topo.AssembleCluster( aObstacle, aCurrent.Layers().Start() );
#if 0
m_logger.NewGroup( "on-colliding-solid-cluster", m_iter );
for( ITEM* item : cluster )
{
m_logger.Log( item, 0, "cluster-entry" );
}
#endif
walkaround.SetSolidsOnly( false );
walkaround.RestrictToSet( true, cluster );
walkaround.SetIterationLimit( 16 ); // fixme: make configurable
int currentRank = aCurrent.Rank();
int nextRank;
2016-08-15 15:16:48 +00:00
bool success = false;
for( int attempt = 0; attempt < 2; attempt++ )
{
if( attempt == 1 || Settings().JumpOverObstacles() )
{
2016-08-15 15:16:48 +00:00
nextRank = currentRank - 1;
walkaround.SetSingleDirection( true );
}
else
{
nextRank = currentRank + 10000;
walkaround.SetSingleDirection( false );
}
WALKAROUND::WALKAROUND_STATUS status = walkaround.Route( aCurrent, walkaroundLine, false );
2016-08-15 15:16:48 +00:00
if( status != WALKAROUND::DONE )
2016-08-15 15:16:48 +00:00
continue;
walkaroundLine.ClearLinks();
walkaroundLine.Unmark();
walkaroundLine.Line().Simplify();
if( walkaroundLine.HasLoops() )
2016-08-15 15:16:48 +00:00
continue;
if( aCurrent.Marker() & MK_HEAD )
{
walkaroundLine.Mark( MK_HEAD );
if( m_multiLineMode )
2016-08-15 15:16:48 +00:00
continue;
m_newHead = walkaroundLine;
}
sanityCheck( &aCurrent, &walkaroundLine );
if( !m_lineStack.empty() )
{
LINE lastLine = m_lineStack.front();
if( lastLine.Collide( &walkaroundLine, m_currentNode ) )
{
LINE dummy( lastLine );
2021-01-05 16:40:34 +00:00
if( ShoveObstacleLine( walkaroundLine, lastLine, dummy ) == SH_OK )
2016-08-15 15:16:48 +00:00
{
success = true;
break;
2016-08-15 15:16:48 +00:00
}
} else {
success = true;
break;
2016-08-15 15:16:48 +00:00
}
}
}
2014-11-14 19:19:00 +00:00
2016-08-15 15:16:48 +00:00
if(!success)
return SH_INCOMPLETE;
replaceLine( aCurrent, walkaroundLine );
walkaroundLine.SetRank( nextRank );
2014-11-14 19:19:00 +00:00
#if 0
m_logger.NewGroup( "on-colliding-solid", m_iter );
m_logger.Log( aObstacle, 0, "obstacle-solid" );
m_logger.Log( &aCurrent, 1, "current-line" );
m_logger.Log( &walkaroundLine, 3, "walk-line" );
#endif
popLineStack();
2015-07-02 14:11:15 +00:00
if( !pushLineStack( walkaroundLine ) )
2015-07-02 14:09:32 +00:00
return SH_INCOMPLETE;
return SH_OK;
}
2013-09-26 21:53:54 +00:00
/*
* Pops NODE stackframes which no longer collide with aHeadSet. Optionally sets aDraggedVia
* to the dragged via of the last unpopped state.
*/
NODE* SHOVE::reduceSpringback( const ITEM_SET& aHeadSet, VIA_HANDLE& aDraggedVia )
{
2013-09-26 21:53:54 +00:00
while( !m_nodeStack.empty() )
{
SPRINGBACK_TAG& spTag = m_nodeStack.back();
2013-09-26 21:53:54 +00:00
2020-12-02 18:42:37 +00:00
OPT<OBSTACLE> obs = spTag.m_node->CheckColliding( aHeadSet );
if( !obs && !spTag.m_locked )
2013-09-26 21:53:54 +00:00
{
aDraggedVia = spTag.m_draggedVia;
aDraggedVia.valid = true;
2014-11-14 19:19:00 +00:00
delete spTag.m_node;
2013-09-26 21:53:54 +00:00
m_nodeStack.pop_back();
}
else
break;
2013-09-26 21:53:54 +00:00
}
return m_nodeStack.empty() ? m_root : m_nodeStack.back().m_node;
}
2013-09-26 21:53:54 +00:00
/*
* Push the current NODE on to the stack. aDraggedVia is the dragged via *before* the push
* (which will be restored in the event the stackframe is popped).
*/
bool SHOVE::pushSpringback( NODE* aNode, const OPT_BOX2I& aAffectedArea, VIA* aDraggedVia )
{
SPRINGBACK_TAG st;
OPT_BOX2I prev_area;
2015-02-18 16:53:46 +00:00
if( !m_nodeStack.empty() )
prev_area = m_nodeStack.back().m_affectedArea;
2013-09-26 21:53:54 +00:00
if( aDraggedVia )
{
st.m_draggedVia = aDraggedVia->MakeHandle();
}
st.m_node = aNode;
if( aAffectedArea )
{
if( prev_area )
2016-08-15 15:16:53 +00:00
st.m_affectedArea = prev_area->Merge( *aAffectedArea );
else
st.m_affectedArea = aAffectedArea;
} else
st.m_affectedArea = prev_area;
st.m_seq = (m_nodeStack.empty() ? 1 : m_nodeStack.back().m_seq + 1);
st.m_locked = false;
2020-03-30 07:24:56 +00:00
2013-09-26 21:53:54 +00:00
m_nodeStack.push_back( st );
2013-09-26 21:53:54 +00:00
return true;
}
2013-09-26 21:53:54 +00:00
/*
* Push or shove a via by at least aForce. (The via might be pushed or shoved slightly further
* to keep it from landing on an existing joint.)
*/
SHOVE::SHOVE_STATUS SHOVE::pushOrShoveVia( VIA* aVia, const VECTOR2I& aForce, int aCurrentRank )
{
LINE_PAIR_VEC draggedLines;
2015-02-18 16:53:46 +00:00
VECTOR2I p0( aVia->Pos() );
JOINT* jt = m_currentNode->FindJoint( p0, aVia );
VECTOR2I p0_pushed( p0 + aForce );
// nothing to do...
if ( aForce.x == 0 && aForce.y == 0 )
return SH_OK;
2015-08-07 15:07:06 +00:00
if( !jt )
{
wxLogTrace( "PNS", "weird, can't find the center-of-via joint\n" );
2015-08-07 15:07:06 +00:00
return SH_INCOMPLETE;
}
2016-08-15 15:16:53 +00:00
if( aVia->IsLocked() )
return SH_TRY_WALK;
if( jt->IsLocked() )
return SH_INCOMPLETE;
// make sure pushed via does not overlap with any existing joint
while( true )
{
JOINT* jt_next = m_currentNode->FindJoint( p0_pushed, aVia );
2015-02-18 16:53:46 +00:00
if( !jt_next )
break;
2015-02-18 16:53:46 +00:00
p0_pushed += aForce.Resize( 2 );
}
2015-02-18 16:53:46 +00:00
std::unique_ptr<VIA> pushedVia = Clone( *aVia );
pushedVia->SetPos( p0_pushed );
pushedVia->Mark( aVia->Marker() );
2013-09-26 21:53:54 +00:00
for( ITEM* item : jt->LinkList() )
{
if( item->OfKind( ITEM::SEGMENT_T | ITEM::ARC_T ) )
{
LINKED_ITEM* li = static_cast<LINKED_ITEM*>( item );
LINE_PAIR lp;
int segIndex;
2014-11-14 19:19:00 +00:00
lp.first = assembleLine( li, &segIndex );
2014-11-14 19:19:00 +00:00
2016-08-15 15:16:48 +00:00
if( lp.first.HasLockedSegments() )
return SH_TRY_WALK;
assert( segIndex == 0 || ( segIndex == ( lp.first.SegmentCount() - 1 ) ) );
if( segIndex == 0 )
lp.first.Reverse();
lp.second = lp.first;
lp.second.ClearLinks();
lp.second.DragCorner( p0_pushed, lp.second.CLine().Find( p0 ) );
lp.second.AppendVia( *pushedVia );
draggedLines.push_back( lp );
}
}
#if 0
2015-02-21 00:41:10 +00:00
m_logger.Log( aVia, 0, "obstacle-via" );
#endif
pushedVia->SetRank( aCurrentRank - 1 );
#if 0
m_logger.Log( pushedVia.get(), 1, "pushed-via" );
#endif
2014-11-14 19:19:00 +00:00
if( aVia->Marker() & MK_HEAD ) // push
{
m_draggedVia = pushedVia.get();
}
else
{ // shove
if( jt->IsStitchingVia() )
pushLineStack( LINE( *pushedVia ) );
}
replaceItems( aVia, std::move( pushedVia ) );
for( LINE_PAIR lp : draggedLines )
2013-09-26 21:53:54 +00:00
{
if( lp.first.Marker() & MK_HEAD )
{
lp.second.Mark( MK_HEAD );
2015-02-18 16:53:46 +00:00
2015-09-30 09:18:19 +00:00
if( m_multiLineMode )
return SH_INCOMPLETE;
m_newHead = lp.second;
}
unwindLineStack( &lp.first );
if( lp.second.SegmentCount() )
{
replaceLine( lp.first, lp.second );
lp.second.SetRank( aCurrentRank - 1 );
2015-07-02 14:11:15 +00:00
if( !pushLineStack( lp.second, true ) )
2015-07-02 14:09:32 +00:00
return SH_INCOMPLETE;
}
else
{
m_currentNode->Remove( lp.first );
}
#if 0
m_logger.Log( &lp.first, 2, "fan-pre" );
m_logger.Log( &lp.second, 3, "fan-post" );
#endif
2013-09-26 21:53:54 +00:00
}
return SH_OK;
}
/*
* Calculate the minimum translation vector required to resolve a collision with a via and
* shove the via by that distance.
*/
SHOVE::SHOVE_STATUS SHOVE::onCollidingVia( ITEM* aCurrent, VIA* aObstacleVia )
{
int clearance = getClearance( aCurrent, aObstacleVia );
LINE_PAIR_VEC draggedLines;
VECTOR2I mtv;
int rank = -1;
SHAPE_COMPOUND shape;
if( aCurrent->OfKind( ITEM::LINE_T ) )
{
LINE* currentLine = (LINE*) aCurrent;
#if 0
m_logger.NewGroup( "push-via-by-line", m_iter );
m_logger.Log( currentLine, 4, "current" );
#endif
2013-09-26 21:53:54 +00:00
shape.AddShape( currentLine->Shape()->Clone() );
2013-09-26 21:53:54 +00:00
// SHAPE_LINE_CHAIN collisions don't currently pay any attention to the line-chain's
// width, so we have to add it to the clearance.
clearance += currentLine->Width() / 2;
// Add a second shape for the via (if any)
if( currentLine->EndsWithVia() )
{
const VIA& currentVia = currentLine->Via();
int viaClearance = getClearance( &currentVia, aObstacleVia );
int holeClearance = getHoleClearance( &currentVia, aObstacleVia );
int effectiveRadius = std::max( currentVia.Diameter() / 2 + viaClearance,
currentVia.Drill() / 2 + holeClearance );
// Since we have to run the collision with the line's clearance + 1/2 its width (as
// it's the only way to take the SHAPE_LINE_CHAIN's width into account), we need to
// subtract that clearance back out of the via's effective radius.
shape.AddShape( new SHAPE_CIRCLE( currentVia.Pos(), effectiveRadius - clearance ) );
}
}
else if( aCurrent->OfKind( ITEM::SOLID_T ) )
2013-09-26 21:53:54 +00:00
{
shape.AddShape( aCurrent->Shape()->Clone() );
}
aObstacleVia->Shape()->Collide( &shape, clearance + PNS_HULL_MARGIN, &mtv );
rank = aCurrent->Rank() + 10000;
return pushOrShoveVia( aObstacleVia, mtv, rank );
}
/*
* TODO describe....
*/
SHOVE::SHOVE_STATUS SHOVE::onReverseCollidingVia( LINE& aCurrent, VIA* aObstacleVia )
{
int n = 0;
LINE cur( aCurrent );
cur.ClearLinks();
JOINT* jt = m_currentNode->FindJoint( aObstacleVia->Pos(), aObstacleVia );
LINE shoved( aCurrent );
shoved.ClearLinks();
2013-09-26 21:53:54 +00:00
cur.RemoveVia();
unwindLineStack( &aCurrent );
2013-09-26 21:53:54 +00:00
for( ITEM* item : jt->LinkList() )
{
if( item->OfKind( ITEM::SEGMENT_T | ITEM::ARC_T ) && item->LayersOverlap( &aCurrent ) )
2013-09-26 21:53:54 +00:00
{
LINKED_ITEM* li = static_cast<LINKED_ITEM*>( item );
LINE head = assembleLine( li );
2014-11-14 19:19:00 +00:00
head.AppendVia( *aObstacleVia );
2021-01-05 16:40:34 +00:00
SHOVE_STATUS st = ShoveObstacleLine( head, cur, shoved );
2014-11-14 19:19:00 +00:00
if( st != SH_OK )
{
#if 0
m_logger.NewGroup( "on-reverse-via-fail-shove", m_iter );
m_logger.Log( aObstacleVia, 0, "the-via" );
m_logger.Log( &aCurrent, 1, "current-line" );
m_logger.Log( &shoved, 3, "shoved-line" );
#endif
return st;
}
2014-11-14 19:19:00 +00:00
cur.SetShape( shoved.CLine() );
n++;
2013-09-26 21:53:54 +00:00
}
}
if( !n )
{
#if 0
m_logger.NewGroup( "on-reverse-via-fail-lonevia", m_iter );
m_logger.Log( aObstacleVia, 0, "the-via" );
m_logger.Log( &aCurrent, 1, "current-line" );
#endif
2014-11-14 19:19:00 +00:00
LINE head( aCurrent );
head.Line().Clear();
head.AppendVia( *aObstacleVia );
head.ClearLinks();
2021-01-05 16:40:34 +00:00
SHOVE_STATUS st = ShoveObstacleLine( head, aCurrent, shoved );
if( st != SH_OK )
return st;
cur.SetShape( shoved.CLine() );
}
if( aCurrent.EndsWithVia() )
shoved.AppendVia( aCurrent.Via() );
#if 0
m_logger.NewGroup( "on-reverse-via", m_iter );
m_logger.Log( aObstacleVia, 0, "the-via" );
m_logger.Log( &aCurrent, 1, "current-line" );
m_logger.Log( &shoved, 3, "shoved-line" );
#endif
int currentRank = aCurrent.Rank();
replaceLine( aCurrent, shoved );
2013-09-26 21:53:54 +00:00
if( !pushLineStack( shoved ) )
2015-07-02 14:09:32 +00:00
return SH_INCOMPLETE;
shoved.SetRank( currentRank );
2014-11-14 19:19:00 +00:00
return SH_OK;
}
2013-09-26 21:53:54 +00:00
void SHOVE::unwindLineStack( LINKED_ITEM* aSeg )
{
for( std::vector<LINE>::iterator i = m_lineStack.begin(); i != m_lineStack.end() ; )
{
if( i->ContainsLink( aSeg ) )
i = m_lineStack.erase( i );
else
i++;
}
2013-09-26 21:53:54 +00:00
for( std::vector<LINE>::iterator i = m_optimizerQueue.begin(); i != m_optimizerQueue.end() ; )
{
if( i->ContainsLink( aSeg ) )
i = m_optimizerQueue.erase( i );
else
i++;
2014-11-14 19:19:00 +00:00
}
}
void SHOVE::unwindLineStack( ITEM* aItem )
{
if( aItem->OfKind( ITEM::SEGMENT_T | ITEM::ARC_T ) )
unwindLineStack( static_cast<LINKED_ITEM*>( aItem ) );
else if( aItem->OfKind( ITEM::LINE_T ) )
{
LINE* l = static_cast<LINE*>( aItem );
2020-12-02 18:42:37 +00:00
for( LINKED_ITEM* seg : l->Links() )
unwindLineStack( seg );
}
}
2014-11-14 19:19:00 +00:00
bool SHOVE::pushLineStack( const LINE& aL, bool aKeepCurrentOnTop )
{
if( !aL.IsLinkedChecked() && aL.SegmentCount() != 0 )
2015-07-02 14:09:32 +00:00
return false;
2014-11-14 19:19:00 +00:00
if( aKeepCurrentOnTop && m_lineStack.size() > 0)
{
m_lineStack.insert( m_lineStack.begin() + m_lineStack.size() - 1, aL );
}
else
{
m_lineStack.push_back( aL );
}
m_optimizerQueue.push_back( aL );
2015-07-02 14:09:32 +00:00
return true;
}
void SHOVE::popLineStack( )
{
LINE& l = m_lineStack.back();
for( std::vector<LINE>::iterator i = m_optimizerQueue.begin(); i != m_optimizerQueue.end(); )
{
bool found = false;
2020-12-02 18:42:37 +00:00
for( LINKED_ITEM* s : l.Links() )
2013-09-26 21:53:54 +00:00
{
if( i->ContainsLink( s ) )
{
i = m_optimizerQueue.erase( i );
found = true;
break;
}
}
if( !found )
i++;
}
2013-09-26 21:53:54 +00:00
m_lineStack.pop_back();
}
2013-09-26 21:53:54 +00:00
/*
* Resolve the next collision.
*/
SHOVE::SHOVE_STATUS SHOVE::shoveIteration( int aIter )
{
LINE currentLine = m_lineStack.back();
NODE::OPT_OBSTACLE nearest;
SHOVE_STATUS st = SH_NULL;
2014-11-14 19:19:00 +00:00
#ifdef DEBUG
Dbg()->SetIteration( aIter );
#endif
for( ITEM::PnsKind search_order : { ITEM::SOLID_T, ITEM::VIA_T, ITEM::SEGMENT_T } )
{
nearest = m_currentNode->NearestObstacle( &currentLine, search_order );
if( nearest )
break;
2014-11-14 19:19:00 +00:00
}
if( !nearest )
{
m_lineStack.pop_back();
return SH_OK;
}
2013-09-26 21:53:54 +00:00
ITEM* ni = nearest->m_item;
2013-09-26 21:53:54 +00:00
unwindLineStack( ni );
if( !ni->OfKind( ITEM::SOLID_T ) && ni->Rank() >= 0 && ni->Rank() > currentLine.Rank() )
{
// Collision with a higher-ranking object (ie: one that we've already shoved)
//
switch( ni->Kind() )
{
case ITEM::VIA_T:
2015-10-05 16:28:41 +00:00
{
wxLogTrace( "PNS", "iter %d: reverse-collide-via", aIter );
2013-09-26 21:53:54 +00:00
if( currentLine.EndsWithVia() )
2015-10-05 16:28:41 +00:00
{
st = SH_INCOMPLETE;
}
2015-10-05 16:28:41 +00:00
else
{
st = onReverseCollidingVia( currentLine, (VIA*) ni );
2015-10-05 16:28:41 +00:00
}
break;
}
2013-09-26 21:53:54 +00:00
case ITEM::SEGMENT_T:
2015-10-05 16:28:41 +00:00
{
wxLogTrace( "PNS", "iter %d: reverse-collide-segment ", aIter );
LINE revLine = assembleLine( static_cast<SEGMENT*>( ni ) );
popLineStack();
st = onCollidingLine( revLine, currentLine );
if( !pushLineStack( revLine ) )
return SH_INCOMPLETE;
break;
}
case ITEM::ARC_T:
{
//TODO(snh): Handle Arc shove separate from track
wxLogTrace( "PNS", "iter %d: reverse-collide-arc ", aIter );
LINE revLine = assembleLine( static_cast<ARC*>( ni ) );
2015-07-02 14:09:32 +00:00
popLineStack();
2015-10-05 16:28:41 +00:00
st = onCollidingLine( revLine, currentLine );
if( !pushLineStack( revLine ) )
2015-10-05 16:28:41 +00:00
return SH_INCOMPLETE;
break;
}
2013-09-26 21:53:54 +00:00
2015-10-05 16:28:41 +00:00
default:
assert( false );
2013-09-26 21:53:54 +00:00
}
}
else
{
// Collision with a lower-ranking object or a solid
//
switch( ni->Kind() )
2013-09-26 21:53:54 +00:00
{
case ITEM::SEGMENT_T:
wxLogTrace( "PNS", "iter %d: collide-segment ", aIter );
2016-08-15 15:16:48 +00:00
st = onCollidingSegment( currentLine, (SEGMENT*) ni );
if( st == SH_TRY_WALK )
2016-08-15 15:16:48 +00:00
st = onCollidingSolid( currentLine, ni );
break;
2013-09-26 21:53:54 +00:00
//TODO(snh): Customize Arc collide
case ITEM::ARC_T:
wxLogTrace( "PNS", "iter %d: collide-arc ", aIter );
st = onCollidingArc( currentLine, static_cast<ARC*>( ni ) );
if( st == SH_TRY_WALK )
st = onCollidingSolid( currentLine, ni );
break;
case ITEM::VIA_T:
wxLogTrace( "PNS", "iter %d: shove-via ", aIter );
st = onCollidingVia( &currentLine, (VIA*) ni );
2016-08-15 15:16:48 +00:00
if( st == SH_TRY_WALK )
st = onCollidingSolid( currentLine, ni );
break;
2013-09-26 21:53:54 +00:00
case ITEM::SOLID_T:
wxLogTrace( "PNS", "iter %d: walk-solid ", aIter );
st = onCollidingSolid( currentLine, (SOLID*) ni );
break;
2014-11-14 19:19:00 +00:00
default:
break;
}
}
2013-09-26 21:53:54 +00:00
return st;
}
2013-09-26 21:53:54 +00:00
/*
* Resolve collisions.
* Each iteration pushes the next colliding object out of the way. Iterations are continued as
* long as they propagate further collisions, or until the iteration timeout or max iteration
* count is reached.
*/
SHOVE::SHOVE_STATUS SHOVE::shoveMainLoop()
{
SHOVE_STATUS st = SH_OK;
2013-09-26 21:53:54 +00:00
m_affectedArea = OPT_BOX2I();
wxLogTrace( "PNS", "ShoveStart [root: %d jts, current: %d jts]", m_root->JointCount(),
m_currentNode->JointCount() );
2013-09-26 21:53:54 +00:00
int iterLimit = Settings().ShoveIterationLimit();
TIME_LIMIT timeLimit = Settings().ShoveTimeLimit();
2013-09-26 21:53:54 +00:00
m_iter = 0;
2013-09-26 21:53:54 +00:00
timeLimit.Restart();
2013-09-26 21:53:54 +00:00
if( m_lineStack.empty() && m_draggedVia )
{
// If we're shoving a free via then push a proxy LINE (with the via on the end) onto
// the stack.
pushLineStack( LINE( *m_draggedVia ) );
}
while( !m_lineStack.empty() )
{
st = shoveIteration( m_iter );
2013-09-26 21:53:54 +00:00
m_iter++;
2014-11-14 19:19:00 +00:00
if( st == SH_INCOMPLETE || timeLimit.Expired() || m_iter >= iterLimit )
{
st = SH_INCOMPLETE;
break;
}
}
2013-09-26 21:53:54 +00:00
return st;
}
2013-09-26 21:53:54 +00:00
OPT_BOX2I SHOVE::totalAffectedArea() const
{
OPT_BOX2I area;
2015-02-18 16:53:46 +00:00
if( !m_nodeStack.empty() )
area = m_nodeStack.back().m_affectedArea;
if( area && m_affectedArea)
area->Merge( *m_affectedArea );
else if( !area )
area = m_affectedArea;
return area;
}
2013-09-26 21:53:54 +00:00
SHOVE::SHOVE_STATUS SHOVE::ShoveLines( const LINE& aCurrentHead )
{
SHOVE_STATUS st = SH_OK;
2013-09-26 21:53:54 +00:00
m_multiLineMode = false;
2015-02-18 16:53:46 +00:00
// empty head? nothing to shove...
2013-09-26 21:53:54 +00:00
if( !aCurrentHead.SegmentCount() && !aCurrentHead.EndsWithVia() )
2015-07-07 16:37:03 +00:00
return SH_INCOMPLETE;
LINE head( aCurrentHead );
head.ClearLinks();
2014-11-14 19:19:00 +00:00
m_lineStack.clear();
m_optimizerQueue.clear();
m_newHead = OPT_LINE();
#if 0
m_logger.Clear();
#endif
2013-09-26 21:53:54 +00:00
// Pop NODEs containing previous shoves which are no longer necessary
//
ITEM_SET headSet;
headSet.Add( aCurrentHead );
2013-09-26 21:53:54 +00:00
VIA_HANDLE dummyVia;
NODE* parent = reduceSpringback( headSet, dummyVia );
2013-09-26 21:53:54 +00:00
// Create a new NODE to store this version of the world
//
m_currentNode = parent->Branch();
m_currentNode->ClearRanks();
m_currentNode->Add( head );
m_currentNode->LockJoint( head.CPoint(0), &head, true );
2014-11-14 19:19:00 +00:00
if( !head.EndsWithVia() )
m_currentNode->LockJoint( head.CPoint( -1 ), &head, true );
head.Mark( MK_HEAD );
head.SetRank( 100000 );
2013-09-26 21:53:54 +00:00
#if 0
m_logger.NewGroup( "initial", 0 );
m_logger.Log( &head, 0, "head" );
#endif
if( head.EndsWithVia() )
{
std::unique_ptr< VIA >headVia = Clone( head.Via() );
headVia->Mark( MK_HEAD );
headVia->SetRank( 100000 );
m_currentNode->Add( std::move( headVia ) );
2013-09-26 21:53:54 +00:00
}
if( !pushLineStack( head ) )
{
delete m_currentNode;
m_currentNode = parent;
2015-07-02 14:09:32 +00:00
return SH_INCOMPLETE;
}
2015-07-02 14:09:32 +00:00
st = shoveMainLoop();
if( st == SH_OK )
{
runOptimizer( m_currentNode );
2013-09-26 21:53:54 +00:00
if( m_newHead )
2016-08-15 15:16:53 +00:00
st = m_currentNode->CheckColliding( &( *m_newHead ) ) ? SH_INCOMPLETE : SH_HEAD_MODIFIED;
else
st = m_currentNode->CheckColliding( &head ) ? SH_INCOMPLETE : SH_OK;
2014-11-14 19:19:00 +00:00
}
m_currentNode->RemoveByMarker( MK_HEAD );
wxLogTrace( "PNS", "Shove status : %s after %d iterations",
( ( st == SH_OK || st == SH_HEAD_MODIFIED ) ? "OK" : "FAILURE"), m_iter );
if( st == SH_OK || st == SH_HEAD_MODIFIED )
{
pushSpringback( m_currentNode, m_affectedArea, nullptr );
}
else
{
delete m_currentNode;
2014-11-14 19:19:00 +00:00
m_currentNode = parent;
2014-11-14 19:19:00 +00:00
m_newHead = OPT_LINE();
2013-09-26 21:53:54 +00:00
}
2016-08-15 15:16:48 +00:00
if(m_newHead)
m_newHead->Unmark();
if( m_newHead && head.EndsWithVia() )
{
VIA v = head.Via();
v.SetPos( m_newHead->CPoint( -1 ) );
m_newHead->AppendVia(v);
}
return st;
}
SHOVE::SHOVE_STATUS SHOVE::ShoveMultiLines( const ITEM_SET& aHeadSet )
{
SHOVE_STATUS st = SH_OK;
m_multiLineMode = true;
ITEM_SET headSet;
for( const ITEM* item : aHeadSet.CItems() )
{
const LINE* headOrig = static_cast<const LINE*>( item );
2015-02-18 16:53:46 +00:00
// empty head? nothing to shove...
if( !headOrig->SegmentCount() )
return SH_INCOMPLETE;
headSet.Add( *headOrig );
2015-02-18 16:53:46 +00:00
}
m_lineStack.clear();
m_optimizerQueue.clear();
#if 0
m_logger.Clear();
#endif
2015-02-18 16:53:46 +00:00
VIA_HANDLE dummyVia;
NODE* parent = reduceSpringback( headSet, dummyVia );
m_currentNode = parent->Branch();
m_currentNode->ClearRanks();
int n = 0;
for( const ITEM* item : aHeadSet.CItems() )
{
const LINE* headOrig = static_cast<const LINE*>( item );
LINE head( *headOrig );
head.ClearLinks();
2015-02-18 16:53:46 +00:00
m_currentNode->Add( head );
head.Mark( MK_HEAD );
head.SetRank( 100000 );
n++;
2015-09-30 09:18:19 +00:00
if( !pushLineStack( head ) )
2015-07-02 14:09:32 +00:00
return SH_INCOMPLETE;
if( head.EndsWithVia() )
{
std::unique_ptr< VIA > headVia = Clone( head.Via() );
headVia->Mark( MK_HEAD );
headVia->SetRank( 100000 );
m_currentNode->Add( std::move( headVia ) );
}
}
st = shoveMainLoop();
2015-07-02 14:11:15 +00:00
if( st == SH_OK )
runOptimizer( m_currentNode );
m_currentNode->RemoveByMarker( MK_HEAD );
wxLogTrace( "PNS", "Shove status : %s after %d iterations",
( st == SH_OK ? "OK" : "FAILURE"), m_iter );
if( st == SH_OK )
{
pushSpringback( m_currentNode, m_affectedArea, nullptr );
}
else
{
delete m_currentNode;
m_currentNode = parent;
}
return st;
}
static VIA* findViaByHandle ( NODE *aNode, const VIA_HANDLE& handle )
{
JOINT* jt = aNode->FindJoint( handle.pos, handle.layers.Start(), handle.net );
if( !jt )
return nullptr;
for( ITEM* item : jt->LinkList() )
{
if( item->OfKind( ITEM::VIA_T ) )
{
if( item->Net() == handle.net && item->Layers().Overlaps(handle.layers) )
return static_cast<VIA*>( item );
}
}
return nullptr;
}
2015-02-18 16:53:46 +00:00
SHOVE::SHOVE_STATUS SHOVE::ShoveDraggingVia( const VIA_HANDLE aOldVia, const VECTOR2I& aWhere, VIA_HANDLE& aNewVia )
{
SHOVE_STATUS st = SH_OK;
2014-11-14 19:19:00 +00:00
m_lineStack.clear();
m_optimizerQueue.clear();
m_newHead = OPT_LINE();
m_draggedVia = NULL;
2020-12-02 18:42:37 +00:00
VIA* viaToDrag = findViaByHandle( m_currentNode, aOldVia );
if( !viaToDrag )
return SH_INCOMPLETE;
// Pop NODEs containing previous shoves which are no longer necessary
ITEM_SET headSet;
VIA headVia ( *viaToDrag );
headVia.SetPos( aWhere );
headSet.Add( headVia );
VIA_HANDLE prevViaHandle;
NODE* parent = reduceSpringback( headSet, prevViaHandle );
if( prevViaHandle.valid )
{
aNewVia = prevViaHandle;
viaToDrag = findViaByHandle( parent, prevViaHandle );
}
// Create a new NODE to store this version of the world
//
m_currentNode = parent->Branch();
m_currentNode->ClearRanks();
viaToDrag->Mark( MK_HEAD );
viaToDrag->SetRank( 100000 );
// Push the via to its new location
//
st = pushOrShoveVia( viaToDrag, ( aWhere - viaToDrag->Pos() ), 0 );
// Shove any colliding objects out of the way
//
if( st == SH_OK )
st = shoveMainLoop();
2015-07-02 14:11:15 +00:00
if( st == SH_OK )
runOptimizer( m_currentNode );
2013-09-26 21:53:54 +00:00
if( st == SH_OK || st == SH_HEAD_MODIFIED )
2013-09-26 21:53:54 +00:00
{
wxLogTrace( "PNS","setNewV %p", m_draggedVia );
if (!m_draggedVia)
m_draggedVia = viaToDrag;
aNewVia = m_draggedVia->MakeHandle();
pushSpringback( m_currentNode, m_affectedArea, viaToDrag );
2013-09-26 21:53:54 +00:00
}
else
{
delete m_currentNode;
m_currentNode = parent;
}
return st;
}
void SHOVE::runOptimizer( NODE* aNode )
{
OPTIMIZER optimizer( aNode );
int optFlags = 0;
int n_passes = 0;
PNS_OPTIMIZATION_EFFORT effort = Settings().OptimizerEffort();
OPT_BOX2I area = totalAffectedArea();
int maxWidth = 0;
for( LINE& line : m_optimizerQueue )
maxWidth = std::max( line.Width(), maxWidth );
2015-02-18 16:53:46 +00:00
if( area )
area->Inflate( 10 * maxWidth );
switch( effort )
{
case OE_LOW:
optFlags = OPTIMIZER::MERGE_OBTUSE;
n_passes = 1;
break;
case OE_MEDIUM:
optFlags = OPTIMIZER::MERGE_SEGMENTS;
2015-02-18 16:53:46 +00:00
if( area )
2015-02-18 16:53:46 +00:00
optimizer.SetRestrictArea( *area );
n_passes = 2;
break;
case OE_FULL:
optFlags = OPTIMIZER::MERGE_SEGMENTS;
n_passes = 2;
break;
default:
break;
}
if( Settings().SmartPads() )
optFlags |= OPTIMIZER::SMART_PADS;
optimizer.SetEffortLevel( optFlags );
optimizer.SetCollisionMask( ITEM::ANY_T );
for( int pass = 0; pass < n_passes; pass++ )
{
std::reverse( m_optimizerQueue.begin(), m_optimizerQueue.end() );
for( LINE& line : m_optimizerQueue)
{
if( !( line.Marker() & MK_HEAD ) )
{
LINE optimized;
if( optimizer.Optimize( &line, &optimized ) )
{
aNode->Remove( line );
line.SetShape( optimized.CLine() );
aNode->Add( line );
}
}
}
}
}
NODE* SHOVE::CurrentNode()
{
return m_nodeStack.empty() ? m_root : m_nodeStack.back().m_node;
}
const LINE SHOVE::NewHead() const
{
assert( m_newHead );
return *m_newHead;
}
2014-11-14 19:19:00 +00:00
void SHOVE::SetInitialLine( LINE& aInitial )
{
m_root = m_root->Branch();
m_root->Remove( aInitial );
}
bool SHOVE::AddLockedSpringbackNode( NODE* aNode )
{
SPRINGBACK_TAG sp;
sp.m_node = aNode;
sp.m_locked = true;
m_nodeStack.push_back(sp);
return true;
}
bool SHOVE::RewindSpringbackTo( NODE* aNode )
{
bool found = false;
auto iter = m_nodeStack.begin();
while( iter != m_nodeStack.end() )
{
if ( iter->m_node == aNode )
{
found = true;
break;
}
iter++;
}
if( !found )
return false;
auto start = iter;
aNode->KillChildren();
m_nodeStack.erase( start, m_nodeStack.end() );
return true;
}
void SHOVE::UnlockSpringbackNode( NODE* aNode )
{
auto iter = m_nodeStack.begin();
while( iter != m_nodeStack.end() )
{
if ( iter->m_node == aNode )
{
iter->m_locked = false;
break;
}
iter++;
}
}
}