2013-09-18 17:55:16 +00:00
|
|
|
/*
|
|
|
|
* KiRouter - a push-and-(sometimes-)shove PCB router
|
|
|
|
*
|
2014-05-14 13:53:54 +00:00
|
|
|
* Copyright (C) 2013-2014 CERN
|
2023-07-17 11:51:41 +00:00
|
|
|
* Copyright (C) 2016-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2013-09-18 17:55:16 +00:00
|
|
|
* Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
2013-09-26 21:53:54 +00:00
|
|
|
*
|
2013-09-18 17:55:16 +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
|
|
|
*
|
2013-09-18 17:55:16 +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
|
|
|
*
|
2013-09-18 17:55:16 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
2014-05-14 13:53:54 +00:00
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
2013-09-18 17:55:16 +00:00
|
|
|
*/
|
|
|
|
|
2022-08-25 22:50:47 +00:00
|
|
|
#include <optional>
|
2013-09-18 17:55:16 +00:00
|
|
|
|
|
|
|
#include <geometry/shape_line_chain.h>
|
|
|
|
|
|
|
|
#include "pns_walkaround.h"
|
|
|
|
#include "pns_optimizer.h"
|
|
|
|
#include "pns_router.h"
|
2020-02-19 17:22:37 +00:00
|
|
|
#include "pns_debug_decorator.h"
|
2022-06-03 21:18:42 +00:00
|
|
|
#include "pns_solid.h"
|
|
|
|
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2016-08-29 14:38:11 +00:00
|
|
|
namespace PNS {
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
void WALKAROUND::start( const LINE& aInitialPath )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2013-09-26 21:53:54 +00:00
|
|
|
m_iteration = 0;
|
2014-05-16 11:37:31 +00:00
|
|
|
m_iterationLimit = 50;
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
NODE::OPT_OBSTACLE WALKAROUND::nearestObstacle( const LINE& aPath )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2022-08-15 13:31:43 +00:00
|
|
|
COLLISION_SEARCH_OPTIONS opts;
|
2022-08-30 12:52:34 +00:00
|
|
|
|
|
|
|
opts.m_kindMask = m_itemMask;
|
|
|
|
|
|
|
|
if( ! m_restrictedSet.empty() )
|
|
|
|
opts.m_restrictedSet = &m_restrictedSet;
|
|
|
|
else
|
|
|
|
opts.m_restrictedSet = nullptr;
|
|
|
|
|
2022-08-15 13:31:43 +00:00
|
|
|
opts.m_useClearanceEpsilon = false;
|
|
|
|
|
2022-08-30 12:52:34 +00:00
|
|
|
NODE::OPT_OBSTACLE obs = m_world->NearestObstacle( &aPath, opts );
|
2015-08-19 16:07:16 +00:00
|
|
|
|
|
|
|
if( m_restrictedSet.empty() )
|
|
|
|
return obs;
|
|
|
|
|
|
|
|
else if( obs && m_restrictedSet.find ( obs->m_item ) != m_restrictedSet.end() )
|
|
|
|
return obs;
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
return NODE::OPT_OBSTACLE();
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-06-03 21:18:42 +00:00
|
|
|
void WALKAROUND::RestrictToSet( bool aEnabled, const std::set<ITEM*>& aSet )
|
|
|
|
{
|
|
|
|
m_restrictedVertices.clear();
|
2023-06-23 12:52:34 +00:00
|
|
|
m_restrictedSet.clear();
|
2022-06-03 21:18:42 +00:00
|
|
|
|
|
|
|
if( aEnabled )
|
2023-06-23 12:52:34 +00:00
|
|
|
{
|
2023-07-17 11:51:41 +00:00
|
|
|
for( ITEM* item : aSet )
|
2023-06-23 12:52:34 +00:00
|
|
|
{
|
|
|
|
m_restrictedSet.insert( item );
|
|
|
|
|
|
|
|
if ( item->HasHole() )
|
|
|
|
m_restrictedSet.insert( item->Hole() );
|
|
|
|
}
|
|
|
|
}
|
2022-06-03 21:18:42 +00:00
|
|
|
|
2023-07-17 11:51:41 +00:00
|
|
|
for( ITEM* item : aSet )
|
2022-06-03 21:18:42 +00:00
|
|
|
{
|
2023-07-17 11:51:41 +00:00
|
|
|
if( SOLID* solid = dyn_cast<SOLID*>( item ) )
|
2022-06-03 21:18:42 +00:00
|
|
|
m_restrictedVertices.push_back( solid->Anchor( 0 ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-27 22:08:31 +00:00
|
|
|
WALKAROUND::WALKAROUND_STATUS WALKAROUND::singleStep( LINE& aPath, bool aWindingDirection )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2022-08-25 22:50:47 +00:00
|
|
|
std::optional<OBSTACLE>& current_obs =
|
2013-09-26 21:53:54 +00:00
|
|
|
aWindingDirection ? m_currentObstacle[0] : m_currentObstacle[1];
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
if( !current_obs )
|
|
|
|
return DONE;
|
|
|
|
|
2021-06-06 18:57:41 +00:00
|
|
|
VECTOR2I initialLast = aPath.CPoint( -1 );
|
|
|
|
|
2021-05-27 22:08:31 +00:00
|
|
|
SHAPE_LINE_CHAIN path_walk;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2022-08-30 12:52:34 +00:00
|
|
|
|
|
|
|
SHAPE_LINE_CHAIN hull = current_obs->m_item->Hull( current_obs->m_clearance, aPath.Width() );
|
2022-08-15 13:31:43 +00:00
|
|
|
|
|
|
|
bool s_cw = aPath.Walkaround( hull, path_walk, aWindingDirection );
|
2014-11-14 19:19:00 +00:00
|
|
|
|
2022-02-26 01:42:16 +00:00
|
|
|
PNS_DBG( Dbg(), BeginGroup, "hull/walk", 1 );
|
2022-08-30 12:52:34 +00:00
|
|
|
PNS_DBG( Dbg(), AddShape, &hull, RED, 0, wxString::Format( "hull-%s-%d-cl %d", aWindingDirection ? wxT( "cw" ) : wxT( "ccw" ), m_iteration, current_obs->m_clearance ) );
|
2022-02-26 01:42:16 +00:00
|
|
|
PNS_DBG( Dbg(), AddShape, &aPath.CLine(), GREEN, 0, wxString::Format( "path-%s-%d", aWindingDirection ? wxT( "cw" ) : wxT( "ccw" ), m_iteration ) );
|
|
|
|
PNS_DBG( Dbg(), AddShape, &path_walk, BLUE, 0, wxString::Format( "result-%s-%d", aWindingDirection ? wxT( "cw" ) : wxT( "ccw" ), m_iteration ) );
|
2022-02-05 02:06:25 +00:00
|
|
|
PNS_DBG( Dbg(), Message, wxString::Format( wxT( "Stat cw %d" ), !!s_cw ) );
|
2021-05-29 22:32:38 +00:00
|
|
|
PNS_DBGN( Dbg(), EndGroup );
|
2020-02-19 17:22:37 +00:00
|
|
|
|
2021-05-27 22:08:31 +00:00
|
|
|
path_walk.Simplify();
|
|
|
|
aPath.SetShape( path_walk );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2021-06-06 18:57:41 +00:00
|
|
|
// If the end of the line is inside an obstacle, additional walkaround iterations are not
|
2021-06-06 19:44:19 +00:00
|
|
|
// going to help. Exit now to prevent pegging the iteration limiter and causing lag.
|
2022-08-15 13:31:43 +00:00
|
|
|
if( current_obs && hull.PointInside( initialLast ) && !hull.PointOnEdge( initialLast ) )
|
2021-06-06 18:57:41 +00:00
|
|
|
{
|
|
|
|
return ALMOST_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
current_obs = nearestObstacle( LINE( aPath, path_walk ) );
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
return IN_PROGRESS;
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-19 17:22:37 +00:00
|
|
|
const WALKAROUND::RESULT WALKAROUND::Route( const LINE& aInitialPath )
|
|
|
|
{
|
|
|
|
LINE path_cw( aInitialPath ), path_ccw( aInitialPath );
|
|
|
|
WALKAROUND_STATUS s_cw = IN_PROGRESS, s_ccw = IN_PROGRESS;
|
|
|
|
SHAPE_LINE_CHAIN best_path;
|
|
|
|
RESULT result;
|
|
|
|
|
|
|
|
// special case for via-in-the-middle-of-track placement
|
|
|
|
if( aInitialPath.PointCount() <= 1 )
|
|
|
|
{
|
2021-07-19 23:56:05 +00:00
|
|
|
if( aInitialPath.EndsWithVia() && m_world->CheckColliding( &aInitialPath.Via(),
|
|
|
|
m_itemMask ) )
|
2020-02-19 17:22:37 +00:00
|
|
|
return RESULT( STUCK, STUCK );
|
|
|
|
|
|
|
|
return RESULT( DONE, DONE, aInitialPath, aInitialPath );
|
|
|
|
}
|
|
|
|
|
|
|
|
start( aInitialPath );
|
|
|
|
|
|
|
|
m_currentObstacle[0] = m_currentObstacle[1] = nearestObstacle( aInitialPath );
|
|
|
|
|
|
|
|
result.lineCw = aInitialPath;
|
|
|
|
result.lineCcw = aInitialPath;
|
|
|
|
|
|
|
|
if( m_forceWinding )
|
|
|
|
{
|
|
|
|
s_cw = m_forceCw ? IN_PROGRESS : STUCK;
|
|
|
|
s_ccw = m_forceCw ? STUCK : IN_PROGRESS;
|
|
|
|
}
|
|
|
|
|
2021-06-06 20:03:03 +00:00
|
|
|
// In some situations, there isn't a trivial path (or even a path at all). Hitting the
|
|
|
|
// iteration limit causes lag, so we can exit out early if the walkaround path gets very long
|
|
|
|
// compared with the initial path. If the length exceeds the initial length times this factor,
|
|
|
|
// fail out.
|
|
|
|
const int maxWalkDistFactor = 10;
|
|
|
|
long long lengthLimit = aInitialPath.CLine().Length() * maxWalkDistFactor;
|
|
|
|
|
2020-02-19 17:22:37 +00:00
|
|
|
while( m_iteration < m_iterationLimit )
|
|
|
|
{
|
2021-05-27 22:08:31 +00:00
|
|
|
if( s_cw != STUCK && s_cw != ALMOST_DONE )
|
2020-02-19 17:22:37 +00:00
|
|
|
s_cw = singleStep( path_cw, true );
|
|
|
|
|
2021-05-27 22:08:31 +00:00
|
|
|
if( s_ccw != STUCK && s_ccw != ALMOST_DONE )
|
2020-02-19 17:22:37 +00:00
|
|
|
s_ccw = singleStep( path_ccw, false );
|
|
|
|
|
|
|
|
if( s_cw != IN_PROGRESS )
|
|
|
|
{
|
|
|
|
result.lineCw = path_cw;
|
|
|
|
result.statusCw = s_cw;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( s_ccw != IN_PROGRESS )
|
|
|
|
{
|
|
|
|
result.lineCcw = path_ccw;
|
|
|
|
result.statusCcw = s_ccw;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( s_cw != IN_PROGRESS && s_ccw != IN_PROGRESS )
|
|
|
|
break;
|
|
|
|
|
2023-10-16 21:04:01 +00:00
|
|
|
double length = (double)aInitialPath.CLine().Length();
|
|
|
|
wxCHECK2( length != 0, length = 1.0 );
|
|
|
|
double lcw = path_cw.Line().Length() / length;
|
|
|
|
|
|
|
|
length = (double)aInitialPath.CLine().Length();
|
|
|
|
wxCHECK2( length != 0, length = 1.0 );
|
|
|
|
double lccw = path_ccw.Line().Length() / length;
|
2022-08-30 12:52:34 +00:00
|
|
|
|
|
|
|
PNS_DBG( Dbg(), Message, wxString::Format( wxT( "lcw %.1f lccw %.1f" ), lcw, lccw ) );
|
|
|
|
|
|
|
|
|
2021-06-06 20:03:03 +00:00
|
|
|
// Safety valve
|
2022-10-26 22:27:01 +00:00
|
|
|
if( m_lengthLimitOn && path_cw.Line().Length() > lengthLimit && path_ccw.Line().Length() > lengthLimit )
|
2021-06-06 20:03:03 +00:00
|
|
|
break;
|
|
|
|
|
2020-02-19 17:22:37 +00:00
|
|
|
m_iteration++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( s_cw == IN_PROGRESS )
|
|
|
|
{
|
|
|
|
result.lineCw = path_cw;
|
|
|
|
result.statusCw = ALMOST_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( s_ccw == IN_PROGRESS )
|
|
|
|
{
|
|
|
|
result.lineCcw = path_ccw;
|
|
|
|
result.statusCcw = ALMOST_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( result.lineCw.SegmentCount() < 1 || result.lineCw.CPoint( 0 ) != aInitialPath.CPoint( 0 ) )
|
|
|
|
{
|
|
|
|
result.statusCw = STUCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( result.lineCw.PointCount() > 0 && result.lineCw.CPoint( -1 ) != aInitialPath.CPoint( -1 ) )
|
|
|
|
{
|
|
|
|
result.statusCw = ALMOST_DONE;
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:56:05 +00:00
|
|
|
if( result.lineCcw.SegmentCount() < 1 ||
|
|
|
|
result.lineCcw.CPoint( 0 ) != aInitialPath.CPoint( 0 ) )
|
2020-02-19 17:22:37 +00:00
|
|
|
{
|
|
|
|
result.statusCcw = STUCK;
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:56:05 +00:00
|
|
|
if( result.lineCcw.PointCount() > 0 &&
|
|
|
|
result.lineCcw.CPoint( -1 ) != aInitialPath.CPoint( -1 ) )
|
2020-02-19 17:22:37 +00:00
|
|
|
{
|
|
|
|
result.statusCcw = ALMOST_DONE;
|
|
|
|
}
|
|
|
|
|
2022-06-03 21:18:42 +00:00
|
|
|
result.lineCw.ClearLinks();
|
|
|
|
result.lineCcw.ClearLinks();
|
|
|
|
|
2020-02-19 17:22:37 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-19 23:56:05 +00:00
|
|
|
WALKAROUND::WALKAROUND_STATUS WALKAROUND::Route( const LINE& aInitialPath, LINE& aWalkPath,
|
|
|
|
bool aOptimize )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
LINE path_cw( aInitialPath ), path_ccw( aInitialPath );
|
2015-03-03 11:59:26 +00:00
|
|
|
WALKAROUND_STATUS s_cw = IN_PROGRESS, s_ccw = IN_PROGRESS;
|
2013-09-26 21:53:54 +00:00
|
|
|
SHAPE_LINE_CHAIN best_path;
|
|
|
|
|
2015-07-02 14:09:24 +00:00
|
|
|
// special case for via-in-the-middle-of-track placement
|
|
|
|
if( aInitialPath.PointCount() <= 1 )
|
|
|
|
{
|
2023-07-17 11:51:41 +00:00
|
|
|
if( aInitialPath.EndsWithVia() && m_world->CheckColliding( &aInitialPath.Via(), m_itemMask ) )
|
2015-07-10 21:42:05 +00:00
|
|
|
return STUCK;
|
2015-07-02 14:09:24 +00:00
|
|
|
|
|
|
|
aWalkPath = aInitialPath;
|
|
|
|
return DONE;
|
|
|
|
}
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
start( aInitialPath );
|
|
|
|
|
|
|
|
m_currentObstacle[0] = m_currentObstacle[1] = nearestObstacle( aInitialPath );
|
|
|
|
|
|
|
|
aWalkPath = aInitialPath;
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
if( m_forceWinding )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
s_cw = m_forceCw ? IN_PROGRESS : STUCK;
|
|
|
|
s_ccw = m_forceCw ? STUCK : IN_PROGRESS;
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
while( m_iteration < m_iterationLimit )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2021-08-24 19:16:35 +00:00
|
|
|
if( path_cw.PointCount() == 0 )
|
|
|
|
s_cw = STUCK; // cw path is empty, can't continue
|
|
|
|
|
|
|
|
if( path_ccw.PointCount() == 0 )
|
|
|
|
s_ccw = STUCK; // ccw path is empty, can't continue
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
if( s_cw != STUCK )
|
|
|
|
s_cw = singleStep( path_cw, true );
|
|
|
|
|
|
|
|
if( s_ccw != STUCK )
|
|
|
|
s_ccw = singleStep( path_ccw, false );
|
|
|
|
|
|
|
|
if( ( s_cw == DONE && s_ccw == DONE ) || ( s_cw == STUCK && s_ccw == STUCK ) )
|
|
|
|
{
|
2014-05-14 13:53:54 +00:00
|
|
|
int len_cw = path_cw.CLine().Length();
|
|
|
|
int len_ccw = path_ccw.CLine().Length();
|
2014-11-14 19:19:00 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
if( m_forceLongerPath )
|
2015-02-18 16:53:46 +00:00
|
|
|
aWalkPath = ( len_cw > len_ccw ? path_cw : path_ccw );
|
2013-09-26 21:53:54 +00:00
|
|
|
else
|
2015-02-18 16:53:46 +00:00
|
|
|
aWalkPath = ( len_cw < len_ccw ? path_cw : path_ccw );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if( s_cw == DONE && !m_forceLongerPath )
|
|
|
|
{
|
|
|
|
aWalkPath = path_cw;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if( s_ccw == DONE && !m_forceLongerPath )
|
|
|
|
{
|
|
|
|
aWalkPath = path_ccw;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_iteration++;
|
|
|
|
}
|
2014-11-14 19:19:00 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( m_iteration == m_iterationLimit )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2014-05-14 13:53:54 +00:00
|
|
|
int len_cw = path_cw.CLine().Length();
|
|
|
|
int len_ccw = path_ccw.CLine().Length();
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
if( m_forceLongerPath )
|
2014-05-16 11:37:31 +00:00
|
|
|
aWalkPath = ( len_cw > len_ccw ? path_cw : path_ccw );
|
2013-09-26 21:53:54 +00:00
|
|
|
else
|
2014-05-16 11:37:31 +00:00
|
|
|
aWalkPath = ( len_cw < len_ccw ? path_cw : path_ccw );
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
aWalkPath.Line().Simplify();
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( aWalkPath.SegmentCount() < 1 )
|
2014-05-14 13:53:54 +00:00
|
|
|
return STUCK;
|
2021-07-19 23:56:05 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( aWalkPath.CPoint( -1 ) != aInitialPath.CPoint( -1 ) )
|
2020-02-19 17:22:37 +00:00
|
|
|
return ALMOST_DONE;
|
2021-07-19 23:56:05 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( aWalkPath.CPoint( 0 ) != aInitialPath.CPoint( 0 ) )
|
2014-05-14 13:53:54 +00:00
|
|
|
return STUCK;
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
WALKAROUND_STATUS st = s_ccw == DONE || s_cw == DONE ? DONE : STUCK;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2015-02-18 00:29:54 +00:00
|
|
|
if( st == DONE )
|
|
|
|
{
|
|
|
|
if( aOptimize )
|
2016-08-29 17:31:13 +00:00
|
|
|
OPTIMIZER::Optimize( &aWalkPath, OPTIMIZER::MERGE_OBTUSE, m_world );
|
2015-02-18 00:29:54 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
return st;
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
2016-08-29 14:38:11 +00:00
|
|
|
}
|