kicad/pcbnew/router/pns_shove.cpp

1281 lines
32 KiB
C++
Raw Normal View History

/*
* KiRouter - a push-and-(sometimes-)shove PCB router
*
* Copyright (C) 2013-2014 CERN
* 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/>.
*/
2015-02-26 15:34:10 +00:00
#define PNS_DEBUG
#include <deque>
#include <cassert>
#include <boost/foreach.hpp>
#include "trace.h"
#include "range.h"
#include "pns_line.h"
#include "pns_node.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_shove.h"
#include "pns_utils.h"
#include "time_limit.h"
#include <profile.h>
2015-02-18 16:53:46 +00:00
void PNS_SHOVE::replaceItems( PNS_ITEM* aOld, PNS_ITEM* aNew )
{
OPT_BOX2I changed_area = ChangedArea( aOld, aNew );
2015-02-18 16:53:46 +00:00
if( changed_area )
{
2015-02-18 16:53:46 +00:00
assert( !changed_area->Contains( VECTOR2I( 0, 0 ) ) );
m_affectedAreaSum = m_affectedAreaSum ? m_affectedAreaSum->Merge ( *changed_area ) : *changed_area;
}
m_currentNode->Replace( aOld, aNew );
}
int PNS_SHOVE::getClearance( PNS_ITEM *aA, PNS_ITEM *aB ) const
{
2015-02-18 16:53:46 +00:00
if( m_forceClearance >= 0 )
return m_forceClearance;
return m_currentNode->GetClearance( aA, aB );
}
2015-02-18 16:53:46 +00:00
2014-11-14 19:19:00 +00:00
static void sanityCheck( PNS_LINE* aOld, PNS_LINE* aNew )
{
assert( aOld->CPoint( 0 ) == aNew->CPoint( 0 ) );
assert( aOld->CPoint( -1 ) == aNew->CPoint( -1 ) );
}
PNS_SHOVE::PNS_SHOVE( PNS_NODE* aWorld, PNS_ROUTER* aRouter ) :
PNS_ALGO_BASE ( aRouter )
{
m_forceClearance = -1;
2013-09-26 21:53:54 +00:00
m_root = aWorld;
// Initialize other temporary variables:
m_currentNode = NULL;
m_draggedVia = NULL;
m_iter = 0;
m_multiLineMode = false;
}
2013-09-26 21:53:54 +00:00
PNS_SHOVE::~PNS_SHOVE()
{
// free all the stuff we've created during routing/dragging operation.
2014-11-14 19:19:00 +00:00
BOOST_FOREACH( PNS_ITEM* item, m_gcItems )
delete item;
}
// garbage-collected line assembling
2014-11-14 19:19:00 +00:00
PNS_LINE* PNS_SHOVE::assembleLine( const PNS_SEGMENT* aSeg, int* aIndex )
2013-09-26 21:53:54 +00:00
{
PNS_LINE* l = m_currentNode->AssembleLine( const_cast<PNS_SEGMENT*>( aSeg ), aIndex );
2013-09-26 21:53:54 +00:00
2014-11-14 19:19:00 +00:00
m_gcItems.push_back( l );
return l;
}
2013-09-26 21:53:54 +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.
2014-11-14 19:19:00 +00:00
bool PNS_SHOVE::checkBumpDirection( PNS_LINE* aCurrent, PNS_LINE* aShoved ) const
{
const SEG ss = aCurrent->CSegment( 0 );
2013-09-26 21:53:54 +00:00
int dist = getClearance( aCurrent, aShoved ) + PNS_HULL_MARGIN;
2013-09-26 21:53:54 +00:00
dist += aCurrent->Width() / 2;
dist += aShoved->Width() / 2;
const VECTOR2I ps = ss.A - ( ss.B - ss.A ).Resize( dist );
2014-11-14 19:19:00 +00:00
return !aShoved->CLine().PointOnEdge( ps );
}
PNS_SHOVE::SHOVE_STATUS PNS_SHOVE::walkaroundLoneVia( PNS_LINE* aCurrent, PNS_LINE* aObstacle,
PNS_LINE* aShoved )
{
int clearance = getClearance( aCurrent, aObstacle );
const SHAPE_LINE_CHAIN hull = aCurrent->Via().Hull( clearance, aObstacle->Width() );
SHAPE_LINE_CHAIN path_cw, path_ccw;
2013-09-26 21:53:54 +00:00
aObstacle->Walkaround( hull, path_cw, true );
aObstacle->Walkaround( hull, path_ccw, false );
const SHAPE_LINE_CHAIN& shortest = path_ccw.Length() < path_cw.Length() ? path_ccw : path_cw;
if( shortest.PointCount() < 2 )
return SH_INCOMPLETE;
if( aObstacle->CPoint( -1 ) != shortest.CPoint( -1 ) )
return SH_INCOMPLETE;
if( aObstacle->CPoint( 0 ) != shortest.CPoint( 0 ) )
return SH_INCOMPLETE;
aShoved->SetShape( shortest );
2013-09-26 21:53:54 +00:00
if( m_currentNode->CheckColliding( aShoved, aCurrent ) )
return SH_INCOMPLETE;
2013-09-26 21:53:54 +00:00
return SH_OK;
}
PNS_SHOVE::SHOVE_STATUS PNS_SHOVE::processHullSet( PNS_LINE* aCurrent, PNS_LINE* aObstacle,
PNS_LINE* aShoved, const HULL_SET& aHulls )
{
const SHAPE_LINE_CHAIN& obs = aObstacle->CLine();
bool failingDirCheck = false;
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;
PNS_LINE l( *aObstacle );
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
l.Walkaround( hull, path, clockwise );
path.Simplify();
l.SetShape( path );
}
2014-11-14 19:19:00 +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
if( ( vFirst < 0 || vLast < 0 ) && !path.CompareGeometry( aObstacle->CLine() ) )
{
TRACE( 100, "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 ) )
{
TRACE( 100, "attempt %d fail vend-start\n", attempt );
continue;
}
2013-09-26 21:53:54 +00:00
if( !checkBumpDirection( aCurrent, &l ) )
{
TRACE( 100, "attempt %d fail direction-check", attempt );
failingDirCheck = true;
aShoved->SetShape( l.CLine() );
continue;
}
2013-09-26 21:53:54 +00:00
if( path.SelfIntersecting() )
{
TRACE( 100, "attempt %d fail self-intersect", attempt );
continue;
}
2014-11-14 19:19:00 +00:00
bool colliding = m_currentNode->CheckColliding( &l, aCurrent, PNS_ITEM::ANY, m_forceClearance );
2014-11-14 19:19:00 +00:00
if( ( aCurrent->Marker() & MK_HEAD ) && !colliding )
{
PNS_JOINT* jtStart = m_currentNode->FindJoint( aCurrent->CPoint( 0 ), aCurrent );
2014-11-14 19:19:00 +00:00
BOOST_FOREACH( PNS_ITEM* item, jtStart->LinkList() )
{
if( m_currentNode->CheckColliding( item, &l ) )
colliding = true;
}
}
2014-11-14 19:19:00 +00:00
if( colliding )
{
TRACE( 100, "attempt %d fail coll-check", attempt );
continue;
}
2013-09-26 21:53:54 +00:00
2014-11-14 19:19:00 +00:00
aShoved->SetShape( l.CLine() );
return SH_OK;
}
2013-09-26 21:53:54 +00:00
return failingDirCheck ? SH_OK : SH_INCOMPLETE;
}
2013-09-26 21:53:54 +00:00
PNS_SHOVE::SHOVE_STATUS PNS_SHOVE::ProcessSingleLine( PNS_LINE* aCurrent, PNS_LINE* aObstacle,
PNS_LINE* aShoved )
{
aShoved->ClearSegmentLinks();
2013-09-26 21:53:54 +00:00
bool obstacleIsHead = false;
2013-09-26 21:53:54 +00:00
if( aObstacle->LinkedSegments() )
{
BOOST_FOREACH( PNS_SEGMENT* s, *aObstacle->LinkedSegments() )
if( s->Marker() & MK_HEAD )
{
obstacleIsHead = true;
2013-09-26 21:53:54 +00:00
break;
}
}
SHOVE_STATUS rv;
2013-09-26 21:53:54 +00:00
bool viaOnEnd = aCurrent->EndsWithVia();
2013-09-26 21:53:54 +00:00
2014-11-14 19:19:00 +00:00
if( viaOnEnd && ( !aCurrent->LayersOverlap( aObstacle ) || aCurrent->SegmentCount() == 0 ) )
{
rv = walkaroundLoneVia( aCurrent, aObstacle, aShoved );
}
else
{
int w = aObstacle->Width();
int n_segs = aCurrent->SegmentCount();
int clearance = getClearance( aCurrent, aObstacle );
2013-09-26 21:53:54 +00:00
HULL_SET hulls;
2013-09-26 21:53:54 +00:00
hulls.reserve( n_segs + 1 );
2013-09-26 21:53:54 +00:00
for( int i = 0; i < n_segs; i++ )
2013-09-26 21:53:54 +00:00
{
PNS_SEGMENT seg( *aCurrent, aCurrent->CSegment( i ) );
hulls.push_back( seg.Hull( clearance, w ) );
2013-09-26 21:53:54 +00:00
}
if( viaOnEnd )
hulls.push_back ( aCurrent->Via().Hull( clearance, w ) );
2013-09-26 21:53:54 +00:00
rv = processHullSet ( aCurrent, aObstacle, aShoved, hulls );
}
2014-11-14 19:19:00 +00:00
if( obstacleIsHead )
aShoved->Mark( aShoved->Marker() | MK_HEAD );
2013-09-26 21:53:54 +00:00
return rv;
}
2013-09-26 21:53:54 +00:00
PNS_SHOVE::SHOVE_STATUS PNS_SHOVE::onCollidingSegment( PNS_LINE* aCurrent, PNS_SEGMENT* aObstacleSeg )
{
int segIndex;
PNS_LINE* obstacleLine = assembleLine( aObstacleSeg, &segIndex );
PNS_LINE* shovedLine = clone( obstacleLine );
2014-11-14 19:19:00 +00:00
SHOVE_STATUS rv = ProcessSingleLine( aCurrent, obstacleLine, shovedLine );
2013-09-26 21:53:54 +00:00
2015-02-18 16:53:46 +00:00
assert( obstacleLine->LayersOverlap( shovedLine ) );
2014-11-14 19:19:00 +00:00
if( rv == SH_OK )
{
2014-11-14 19:19:00 +00:00
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
sanityCheck( obstacleLine, shovedLine );
replaceItems ( obstacleLine, shovedLine );
sanityCheck( obstacleLine, shovedLine );
int rank = aCurrent->Rank();
shovedLine->SetRank( rank - 1 );
pushLine( shovedLine );
2013-09-26 21:53:54 +00:00
}
#ifdef DEBUG
m_logger.NewGroup( "on-colliding-segment", m_iter );
m_logger.Log( aObstacleSeg, 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
2013-09-26 21:53:54 +00:00
return rv;
}
2014-11-14 19:19:00 +00:00
PNS_SHOVE::SHOVE_STATUS PNS_SHOVE::onCollidingLine( PNS_LINE* aCurrent, PNS_LINE* aObstacle )
{
PNS_LINE* shovedLine = clone( aObstacle );
SHOVE_STATUS rv = ProcessSingleLine( aCurrent, aObstacle, shovedLine );
if( rv == SH_OK )
2013-09-26 21:53:54 +00:00
{
2014-11-14 19:19:00 +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 );
replaceItems( aObstacle, shovedLine );
sanityCheck( aObstacle, shovedLine );
2014-11-14 19:19:00 +00:00
int rank = aObstacle->Rank();
shovedLine->SetRank ( rank );
pushLine( shovedLine );
#ifdef DEBUG
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
2013-09-26 21:53:54 +00:00
}
return rv;
}
PNS_SHOVE::SHOVE_STATUS PNS_SHOVE::onCollidingSolid( PNS_LINE* aCurrent, PNS_SOLID* aObstacleSolid )
{
PNS_WALKAROUND walkaround( m_currentNode, Router() );
PNS_LINE* walkaroundLine = clone( aCurrent );
2014-11-14 19:19:00 +00:00
if( aCurrent->EndsWithVia() )
{
2014-11-14 19:19:00 +00:00
PNS_VIA vh = aCurrent->Via();
PNS_VIA* via = NULL;
PNS_JOINT* jtStart = m_currentNode->FindJoint( vh.Pos(), aCurrent );
if( !jtStart )
return SH_INCOMPLETE;
BOOST_FOREACH( PNS_ITEM* item, jtStart->LinkList() )
{
if( item->OfKind( PNS_ITEM::VIA ) )
{
via = (PNS_VIA*) item;
break;
}
}
2014-11-14 19:19:00 +00:00
if( via && m_currentNode->CheckColliding( via, aObstacleSolid ) )
return onCollidingVia( aObstacleSolid, via );
}
walkaround.SetSolidsOnly( true );
walkaround.SetIterationLimit ( 8 ); // fixme: make configurable
2014-11-14 19:19:00 +00:00
int currentRank = aCurrent->Rank();
int nextRank;
if( !Settings().JumpOverObstacles() )
{
nextRank = currentRank + 10000;
walkaround.SetSingleDirection( false );
}
else
{
nextRank = currentRank - 1;
walkaround.SetSingleDirection( true );
}
2014-11-14 19:19:00 +00:00
if( walkaround.Route( *aCurrent, *walkaroundLine, false ) != PNS_WALKAROUND::DONE )
2013-09-26 21:53:54 +00:00
return SH_INCOMPLETE;
walkaroundLine->ClearSegmentLinks();
walkaroundLine->Unmark();
walkaroundLine->Line().Simplify();
if( walkaroundLine->HasLoops() )
return SH_INCOMPLETE;
if( aCurrent->Marker() & MK_HEAD )
{
walkaroundLine->Mark( MK_HEAD );
2015-02-18 16:53:46 +00:00
if(m_multiLineMode)
return SH_INCOMPLETE;
m_newHead = *walkaroundLine;
}
2014-11-14 19:19:00 +00:00
replaceItems ( aCurrent, walkaroundLine );
walkaroundLine->SetRank( nextRank );
2014-11-14 19:19:00 +00:00
#ifdef DEBUG
m_logger.NewGroup( "on-colliding-solid", m_iter );
m_logger.Log( aObstacleSolid, 0, "obstacle-solid" );
m_logger.Log( aCurrent, 1, "current-line" );
m_logger.Log( walkaroundLine, 3, "walk-line" );
#endif
popLine();
pushLine( walkaroundLine );
return SH_OK;
}
2013-09-26 21:53:54 +00:00
bool PNS_SHOVE::reduceSpringback( const PNS_ITEMSET& aHeadSet )
{
2013-09-26 21:53:54 +00:00
bool rv = false;
while( !m_nodeStack.empty() )
{
SPRINGBACK_TAG spTag = m_nodeStack.back();
2013-09-26 21:53:54 +00:00
if( !spTag.m_node->CheckColliding( aHeadSet ) )
2013-09-26 21:53:54 +00:00
{
rv = 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 rv;
}
2013-09-26 21:53:54 +00:00
bool PNS_SHOVE::pushSpringback( PNS_NODE* aNode, const PNS_ITEMSET& aHeadItems,
const PNS_COST_ESTIMATOR& aCost, const OPT_BOX2I& aAffectedArea )
{
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
st.m_node = aNode;
st.m_cost = aCost;
st.m_headItems = aHeadItems;
if( aAffectedArea )
{
if( prev_area )
st.m_affectedArea = prev_area->Merge ( *aAffectedArea );
else
st.m_affectedArea = aAffectedArea;
} else
st.m_affectedArea = prev_area;
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
PNS_SHOVE::SHOVE_STATUS PNS_SHOVE::pushVia( PNS_VIA* aVia, const VECTOR2I& aForce, int aCurrentRank, bool aDryRun )
{
LINE_PAIR_VEC draggedLines;
2015-02-18 16:53:46 +00:00
VECTOR2I p0( aVia->Pos() );
PNS_JOINT* jt = m_currentNode->FindJoint( p0, aVia );
VECTOR2I p0_pushed( p0 + aForce );
2015-02-18 16:53:46 +00:00
while( aForce.x != 0 || aForce.y != 0 )
{
2015-02-18 16:53:46 +00:00
PNS_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 ); // make sure pushed via does not overlap with any existing joint
}
2015-02-18 16:53:46 +00:00
PNS_VIA* pushedVia = aVia->Clone();
pushedVia->SetPos( p0_pushed );
pushedVia->Mark( aVia->Marker() );
2013-09-26 21:53:54 +00:00
if( aVia->Marker() & MK_HEAD )
{
m_draggedVia = pushedVia;
m_draggedViaHeadSet.Clear();
}
if( !jt )
{
TRACEn( 1, "weird, can't find the center-of-via joint\n" );
return SH_INCOMPLETE;
}
BOOST_FOREACH( PNS_ITEM* item, jt->LinkList() )
{
2015-02-18 16:53:46 +00:00
if( PNS_SEGMENT* seg = dyn_cast<PNS_SEGMENT*>( item ) )
{
LINE_PAIR lp;
int segIndex;
2014-11-14 19:19:00 +00:00
lp.first = assembleLine( seg, &segIndex );
2014-11-14 19:19:00 +00:00
assert( segIndex == 0 || ( segIndex == ( lp.first->SegmentCount() - 1 ) ) );
if( segIndex == 0 )
lp.first->Reverse();
lp.second = clone( lp.first );
lp.second->ClearSegmentLinks();
lp.second->DragCorner( p0_pushed, lp.second->CLine().Find( p0 ) );
2015-02-18 16:53:46 +00:00
lp.second->AppendVia( *pushedVia );
draggedLines.push_back( lp );
2015-02-18 16:53:46 +00:00
if( aVia->Marker() & MK_HEAD )
m_draggedViaHeadSet.Add( clone ( lp.second ) );
}
}
m_draggedViaHeadSet.Add ( pushedVia );
if ( aDryRun )
return SH_OK;
replaceItems ( aVia, pushedVia );
2015-02-21 00:41:10 +00:00
#ifdef DEBUG
m_logger.Log( aVia, 0, "obstacle-via" );
#endif
if( aVia->BelongsTo( m_currentNode ) )
delete aVia;
pushedVia->SetRank( aCurrentRank - 1 );
#ifdef DEBUG
m_logger.Log( pushedVia, 1, "pushed-via" );
#endif
2014-11-14 19:19:00 +00:00
BOOST_FOREACH( 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
if ( m_multiLineMode )
return SH_INCOMPLETE;
m_newHead = *lp.second;
}
2014-11-14 19:19:00 +00:00
unwindStack( lp.first );
if( lp.second->SegmentCount() )
{
replaceItems( lp.first, lp.second );
lp.second->SetRank( aCurrentRank - 1 );
2014-11-14 19:19:00 +00:00
pushLine( lp.second );
}
else
m_currentNode->Remove( lp.first );
#ifdef DEBUG
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;
}
2014-11-14 19:19:00 +00:00
PNS_SHOVE::SHOVE_STATUS PNS_SHOVE::onCollidingVia( PNS_ITEM* aCurrent, PNS_VIA* aObstacleVia )
{
int clearance = getClearance( aCurrent, aObstacleVia ) ;
LINE_PAIR_VEC draggedLines;
bool colLine = false, colVia = false;
2014-11-14 19:19:00 +00:00
PNS_LINE* currentLine = NULL;
VECTOR2I mtvLine, mtvVia, mtv, mtvSolid;
int rank = -1;
if( aCurrent->OfKind( PNS_ITEM::LINE ) )
{
#ifdef DEBUG
m_logger.NewGroup( "push-via-by-line", m_iter );
m_logger.Log( aCurrent, 4, "current" );
#endif
2013-09-26 21:53:54 +00:00
currentLine = (PNS_LINE*) aCurrent;
2014-11-14 19:19:00 +00:00
colLine = CollideShapes( aObstacleVia->Shape(), currentLine->Shape(),
clearance + currentLine->Width() / 2 + PNS_HULL_MARGIN,
true, mtvLine );
2013-09-26 21:53:54 +00:00
if( currentLine->EndsWithVia() )
2014-11-14 19:19:00 +00:00
colVia = CollideShapes( currentLine->Via().Shape(), aObstacleVia->Shape(),
clearance + PNS_HULL_MARGIN, true, mtvVia );
2013-09-26 21:53:54 +00:00
if( !colLine && !colVia )
return SH_OK;
2013-09-26 21:53:54 +00:00
if( colLine && colVia )
mtv = mtvVia.EuclideanNorm() > mtvLine.EuclideanNorm() ? mtvVia : mtvLine;
else if( colLine )
mtv = mtvLine;
else
mtv = mtvVia;
2014-11-14 19:19:00 +00:00
rank = currentLine->Rank();
}
2014-11-14 19:19:00 +00:00
else if( aCurrent->OfKind( PNS_ITEM::SOLID ) )
2013-09-26 21:53:54 +00:00
{
2014-11-14 19:19:00 +00:00
CollideShapes( aObstacleVia->Shape(), aCurrent->Shape(),
clearance + PNS_HULL_MARGIN, true, mtvSolid );
mtv = -mtvSolid;
rank = aCurrent->Rank() + 10000;
}
2014-11-14 19:19:00 +00:00
return pushVia( aObstacleVia, mtv, rank );
}
PNS_SHOVE::SHOVE_STATUS PNS_SHOVE::onReverseCollidingVia( PNS_LINE* aCurrent, PNS_VIA* aObstacleVia )
{
std::vector<PNS_LINE*> steps;
int n = 0;
PNS_LINE* cur = clone( aCurrent );
cur->ClearSegmentLinks();
PNS_JOINT* jt = m_currentNode->FindJoint( aObstacleVia->Pos(), aObstacleVia );
PNS_LINE* shoved = clone( aCurrent );
shoved->ClearSegmentLinks();
2013-09-26 21:53:54 +00:00
cur->RemoveVia();
2014-11-14 19:19:00 +00:00
unwindStack( aCurrent );
2013-09-26 21:53:54 +00:00
BOOST_FOREACH( PNS_ITEM* item, jt->LinkList() )
{
2014-11-14 19:19:00 +00:00
if( item->OfKind( PNS_ITEM::SEGMENT ) && item->LayersOverlap( aCurrent ) )
2013-09-26 21:53:54 +00:00
{
PNS_SEGMENT* seg = (PNS_SEGMENT*) item;
PNS_LINE* head = assembleLine( seg );
2014-11-14 19:19:00 +00:00
head->AppendVia( *aObstacleVia );
SHOVE_STATUS st = ProcessSingleLine( head, cur, shoved );
2014-11-14 19:19:00 +00:00
if( st != SH_OK )
{
#ifdef DEBUG
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 )
{
#ifdef DEBUG
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
PNS_LINE head( *aCurrent );
head.Line().Clear();
head.AppendVia( *aObstacleVia );
head.ClearSegmentLinks();
SHOVE_STATUS st = ProcessSingleLine( &head, aCurrent, shoved );
if( st != SH_OK )
return st;
cur->SetShape( shoved->CLine() );
}
if( aCurrent->EndsWithVia() )
shoved->AppendVia( aCurrent->Via() );
#ifdef DEBUG
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();
replaceItems( aCurrent, shoved );
2013-09-26 21:53:54 +00:00
pushLine( shoved );
shoved->SetRank( currentRank );
2014-11-14 19:19:00 +00:00
return SH_OK;
}
2013-09-26 21:53:54 +00:00
void PNS_SHOVE::unwindStack( PNS_SEGMENT *aSeg )
{
for( std::vector<PNS_LINE*>::iterator i = m_lineStack.begin(); i != m_lineStack.end(); )
{
if( (*i)->ContainsSegment( aSeg ) )
i = m_lineStack.erase( i );
else
i++;
}
2013-09-26 21:53:54 +00:00
for( std::vector<PNS_LINE*>::iterator i = m_optimizerQueue.begin(); i != m_optimizerQueue.end(); )
{
if( (*i)->ContainsSegment( aSeg ) )
i = m_optimizerQueue.erase( i );
else
i++;
2014-11-14 19:19:00 +00:00
}
}
void PNS_SHOVE::unwindStack( PNS_ITEM* aItem )
{
if( aItem->OfKind( PNS_ITEM::SEGMENT ) )
unwindStack( static_cast<PNS_SEGMENT*>( aItem ) );
else if( aItem->OfKind( PNS_ITEM::LINE ) )
{
PNS_LINE* l = static_cast<PNS_LINE*>( aItem );
2014-11-14 19:19:00 +00:00
if( !l->LinkedSegments() )
return;
BOOST_FOREACH( PNS_SEGMENT* seg, *l->LinkedSegments() )
unwindStack( seg );
}
}
2014-11-14 19:19:00 +00:00
void PNS_SHOVE::pushLine( PNS_LINE* aL )
{
if( aL->LinkCount() >= 0 && ( aL->LinkCount() != aL->SegmentCount() ) )
{
printf("LC: %d SC %d\n", aL->LinkCount(), aL->SegmentCount() );
for(int i=0;i<aL->SegmentCount();i++)
{
SEG s = aL->CLine().CSegment(i);
printf("s %d: %d %d %d %d\n", i, s.A.x, s.A.y, s.B.x, s.B.y );
}
assert( false );
}
2014-11-14 19:19:00 +00:00
m_lineStack.push_back( aL );
m_optimizerQueue.push_back( aL );
}
void PNS_SHOVE::popLine( )
{
PNS_LINE* l = m_lineStack.back();
for( std::vector<PNS_LINE*>::iterator i = m_optimizerQueue.begin(); i != m_optimizerQueue.end(); )
{
if( ( *i ) == l )
2013-09-26 21:53:54 +00:00
{
i = m_optimizerQueue.erase( i );
}
else
i++;
}
2013-09-26 21:53:54 +00:00
m_lineStack.pop_back();
}
2013-09-26 21:53:54 +00:00
PNS_SHOVE::SHOVE_STATUS PNS_SHOVE::shoveIteration( int aIter )
{
PNS_LINE* currentLine = m_lineStack.back();
PNS_NODE::OPT_OBSTACLE nearest;
SHOVE_STATUS st = SH_NULL;
2014-11-14 19:19:00 +00:00
PNS_ITEM::PnsKind search_order[] = { PNS_ITEM::SOLID, PNS_ITEM::VIA, PNS_ITEM::SEGMENT };
2014-11-14 19:19:00 +00:00
for( int i = 0; i < 3; i++ )
{
nearest = m_currentNode->NearestObstacle( currentLine, search_order[i] );
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
PNS_ITEM* ni = nearest->m_item;
2013-09-26 21:53:54 +00:00
unwindStack( ni );
if( !ni->OfKind( PNS_ITEM::SOLID ) && ni->Rank() >= 0 && ni->Rank() > currentLine->Rank() )
{
switch( ni->Kind() )
{
case PNS_ITEM::VIA:
{
PNS_VIA* revVia = (PNS_VIA*) ni;
TRACE( 2, "iter %d: reverse-collide-via", aIter );
2014-11-14 19:19:00 +00:00
if( currentLine->EndsWithVia() && m_currentNode->CheckColliding( &currentLine->Via(), revVia ) )
{
st = SH_INCOMPLETE;
}
else
{
st = onReverseCollidingVia ( currentLine, revVia );
2013-09-26 21:53:54 +00:00
}
break;
}
2014-11-14 19:19:00 +00:00
case PNS_ITEM::SEGMENT:
{
PNS_SEGMENT* seg = (PNS_SEGMENT*) ni;
TRACE( 2, "iter %d: reverse-collide-segment ", aIter );
PNS_LINE* revLine = assembleLine( seg );
2013-09-26 21:53:54 +00:00
popLine();
st = onCollidingLine( revLine, currentLine );
pushLine( revLine );
break;
2013-09-26 21:53:54 +00:00
}
default:
assert( false );
2013-09-26 21:53:54 +00:00
}
}
else
{ // "forward" collisoins
switch( ni->Kind() )
2013-09-26 21:53:54 +00:00
{
case PNS_ITEM::SEGMENT:
TRACE( 2, "iter %d: collide-segment ", aIter );
st = onCollidingSegment( currentLine, (PNS_SEGMENT*) ni );
break;
2013-09-26 21:53:54 +00:00
case PNS_ITEM::VIA:
TRACE( 2, "iter %d: shove-via ", aIter );
st = onCollidingVia( currentLine, (PNS_VIA*) ni );
break;
2013-09-26 21:53:54 +00:00
case PNS_ITEM::SOLID:
TRACE( 2, "iter %d: walk-solid ", aIter );
st = onCollidingSolid( currentLine, (PNS_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
PNS_SHOVE::SHOVE_STATUS PNS_SHOVE::shoveMainLoop()
{
SHOVE_STATUS st = SH_OK;
2013-09-26 21:53:54 +00:00
m_affectedAreaSum = OPT_BOX2I();
TRACE( 1, "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
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
2015-02-18 16:53:46 +00:00
OPT_BOX2I PNS_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 )
{
if ( m_affectedAreaSum )
area->Merge ( *m_affectedAreaSum );
} else
area = m_affectedAreaSum;
return area;
}
2013-09-26 21:53:54 +00:00
PNS_SHOVE::SHOVE_STATUS PNS_SHOVE::ShoveLines( const PNS_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...
if( !aCurrentHead.SegmentCount() )
return SH_INCOMPLETE;
2013-09-26 21:53:54 +00:00
PNS_LINE* head = clone( &aCurrentHead );
head->ClearSegmentLinks();
2014-11-14 19:19:00 +00:00
m_lineStack.clear();
m_optimizerQueue.clear();
m_newHead = OPT_LINE();
m_logger.Clear();
2013-09-26 21:53:54 +00:00
PNS_ITEMSET headSet( clone( &aCurrentHead ) );
2013-09-26 21:53:54 +00:00
reduceSpringback( headSet );
2013-09-26 21:53:54 +00:00
PNS_NODE* parent = m_nodeStack.empty() ? m_root : m_nodeStack.back().m_node;
2013-09-26 21:53:54 +00:00
m_currentNode = parent->Branch();
m_currentNode->ClearRanks();
m_currentNode->Add( head );
2014-11-14 19:19:00 +00:00
head->Mark( MK_HEAD );
head->SetRank( 100000 );
2013-09-26 21:53:54 +00:00
m_logger.NewGroup( "initial", 0 );
m_logger.Log( head, 0, "head" );
PNS_VIA* headVia = NULL;
if( head->EndsWithVia() )
{
headVia = head->Via().Clone();
m_currentNode->Add( headVia );
headVia->Mark( MK_HEAD );
headVia->SetRank( 100000 );
m_logger.Log( headVia, 0, "head-via" );
2013-09-26 21:53:54 +00:00
}
pushLine( head );
st = shoveMainLoop();
runOptimizer( m_currentNode, head );
2013-09-26 21:53:54 +00:00
2014-11-14 19:19:00 +00:00
if( m_newHead && st == SH_OK )
2013-09-26 21:53:54 +00:00
{
st = SH_HEAD_MODIFIED;
2014-11-14 19:19:00 +00:00
}
m_currentNode->RemoveByMarker( MK_HEAD );
TRACE( 1, "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, headSet, PNS_COST_ESTIMATOR(), m_affectedAreaSum);
}
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
}
return st;
}
PNS_SHOVE::SHOVE_STATUS PNS_SHOVE::ShoveMultiLines( const PNS_ITEMSET& aHeadSet )
{
SHOVE_STATUS st = SH_OK;
m_multiLineMode = true;
PNS_ITEMSET headSet;
2015-02-18 16:53:46 +00:00
BOOST_FOREACH ( const PNS_ITEM* item, aHeadSet.CItems() )
{
2015-02-18 16:53:46 +00:00
const PNS_LINE* headOrig = static_cast<const PNS_LINE*>( item );
// empty head? nothing to shove...
if( !headOrig->SegmentCount() )
return SH_INCOMPLETE;
2015-02-18 16:53:46 +00:00
headSet.Add( clone( headOrig ) );
}
m_lineStack.clear();
m_optimizerQueue.clear();
m_logger.Clear();
2015-02-18 16:53:46 +00:00
reduceSpringback( headSet );
PNS_NODE* parent = m_nodeStack.empty() ? m_root : m_nodeStack.back().m_node;
m_currentNode = parent->Branch();
m_currentNode->ClearRanks();
int n = 0;
2015-02-18 16:53:46 +00:00
BOOST_FOREACH ( const PNS_ITEM* item, aHeadSet.CItems() )
{
2015-02-18 16:53:46 +00:00
const PNS_LINE* headOrig = static_cast<const PNS_LINE*>( item );
PNS_LINE* head = clone( headOrig );
head->ClearSegmentLinks();
2015-02-18 16:53:46 +00:00
m_currentNode->Add( head );
head->Mark( MK_HEAD );
head->SetRank( 100000 );
n++;
pushLine( head );
PNS_VIA* headVia = NULL;
if( head->EndsWithVia() )
{
headVia = head->Via().Clone(); // fixme: leak
m_currentNode->Add( headVia );
headVia->Mark( MK_HEAD );
headVia->SetRank( 100000 );
m_logger.Log( headVia, 0, "head-via" );
}
}
m_logger.NewGroup( "initial", 0 );
//m_logger.Log( head, 0, "head" );
st = shoveMainLoop();
runOptimizer( m_currentNode, NULL );
m_currentNode->RemoveByMarker( MK_HEAD );
TRACE( 1, "Shove status : %s after %d iterations",
( st == SH_OK ? "OK" : "FAILURE") % m_iter );
if( st == SH_OK )
{
pushSpringback( m_currentNode, PNS_ITEMSET(), PNS_COST_ESTIMATOR(), m_affectedAreaSum );
}
else
{
delete m_currentNode;
m_currentNode = parent;
}
return st;
}
2015-02-18 16:53:46 +00:00
PNS_SHOVE::SHOVE_STATUS PNS_SHOVE::ShoveDraggingVia( PNS_VIA* aVia, const VECTOR2I& aWhere,
PNS_VIA** 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;
m_draggedViaHeadSet.Clear();
PNS_NODE* parent = m_nodeStack.empty() ? m_root : m_nodeStack.back().m_node;
m_currentNode = parent;
//st = pushVia( aVia, ( aWhere - aVia->Pos() ), 0, true );
//reduceSpringback( m_draggedViaHeadSet );
parent = m_nodeStack.empty() ? m_root : m_nodeStack.back().m_node;
m_currentNode = parent->Branch();
m_currentNode->ClearRanks();
aVia->Mark( MK_HEAD );
st = pushVia( aVia, ( aWhere - aVia->Pos() ), 0 );
st = shoveMainLoop();
runOptimizer( m_currentNode, NULL );
//m_currentNode->RemoveByMarker( MK_HEAD );
2013-09-26 21:53:54 +00:00
if( st == SH_OK || st == SH_HEAD_MODIFIED )
2013-09-26 21:53:54 +00:00
{
2015-02-18 16:53:46 +00:00
pushSpringback( m_currentNode, m_draggedViaHeadSet, PNS_COST_ESTIMATOR(), m_affectedAreaSum );
2013-09-26 21:53:54 +00:00
}
else
{
delete m_currentNode;
m_currentNode = parent;
}
if( aNewVia )
{
*aNewVia = m_draggedVia;
}
return st;
}
void PNS_SHOVE::runOptimizer( PNS_NODE* aNode, PNS_LINE* aHead )
{
PNS_OPTIMIZER optimizer( aNode );
int optFlags = 0, n_passes = 0;
PNS_OPTIMIZATION_EFFORT effort = Settings().OptimizerEffort();
OPT_BOX2I area = totalAffectedArea();
int maxWidth = 0;
for( std::vector<PNS_LINE*>::iterator i = m_optimizerQueue.begin();
i != m_optimizerQueue.end(); ++i )
{
maxWidth = std::max ( (*i)->Width(), maxWidth );
}
2015-02-18 16:53:46 +00:00
if( area )
{
2015-02-18 16:53:46 +00:00
area->Inflate( 10 * maxWidth );
}
switch( effort )
{
case OE_LOW:
optFlags = PNS_OPTIMIZER::MERGE_OBTUSE;
n_passes = 1;
break;
case OE_MEDIUM:
optFlags = PNS_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 = PNS_OPTIMIZER::MERGE_SEGMENTS;
n_passes = 2;
break;
default:
break;
}
if( Settings().SmartPads() )
optFlags |= PNS_OPTIMIZER::SMART_PADS ;
optimizer.SetEffortLevel( optFlags );
optimizer.SetCollisionMask( PNS_ITEM::ANY );
for( int pass = 0; pass < n_passes; pass++ )
{
std::reverse( m_optimizerQueue.begin(), m_optimizerQueue.end() );
for( std::vector<PNS_LINE*>::iterator i = m_optimizerQueue.begin();
i != m_optimizerQueue.end(); ++i )
{
PNS_LINE* line = *i;
2014-11-14 19:19:00 +00:00
if( !( line -> Marker() & MK_HEAD ) )
{
PNS_LINE optimized;
if( optimizer.Optimize( line, &optimized ) )
{
aNode->Remove( line );
line->SetShape( optimized.CLine() );
aNode->Add( line );
}
}
}
}
}
PNS_NODE* PNS_SHOVE::CurrentNode()
{
return m_nodeStack.empty() ? m_root : m_nodeStack.back().m_node;
}
const PNS_LINE PNS_SHOVE::NewHead() const
{
assert( m_newHead );
return *m_newHead;
}
2014-11-14 19:19:00 +00:00
void PNS_SHOVE::SetInitialLine( PNS_LINE* aInitial )
{
m_root = m_root->Branch();
m_root->Remove( aInitial );
}