2013-09-18 17:55:16 +00:00
|
|
|
/*
|
|
|
|
* KiRouter - a push-and-(sometimes-)shove PCB router
|
|
|
|
*
|
2017-01-17 14:06:33 +00:00
|
|
|
* Copyright (C) 2013-2017 CERN
|
2022-09-11 21:17:45 +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
|
|
|
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <math/box2.h>
|
2013-09-18 17:55:16 +00:00
|
|
|
#include <math/vector2d.h>
|
|
|
|
|
|
|
|
#include "pns_line.h"
|
|
|
|
#include "pns_node.h"
|
|
|
|
#include "pns_via.h"
|
|
|
|
#include "pns_utils.h"
|
2022-08-30 12:52:34 +00:00
|
|
|
#include "pns_router.h"
|
|
|
|
#include "pns_debug_decorator.h"
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
#include <geometry/shape_rect.h>
|
|
|
|
|
2016-08-29 14:38:11 +00:00
|
|
|
namespace PNS {
|
|
|
|
|
2019-05-17 00:13:21 +00:00
|
|
|
LINE::LINE( const LINE& aOther )
|
2020-04-15 20:16:24 +00:00
|
|
|
: LINK_HOLDER( aOther ),
|
2019-05-17 00:13:21 +00:00
|
|
|
m_line( aOther.m_line ),
|
|
|
|
m_width( aOther.m_width ),
|
|
|
|
m_snapThreshhold( aOther.m_snapThreshhold )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2015-08-12 19:12:12 +00:00
|
|
|
m_net = aOther.m_net;
|
|
|
|
m_movable = aOther.m_movable;
|
|
|
|
m_layers = aOther.m_layers;
|
2022-09-11 21:17:45 +00:00
|
|
|
|
|
|
|
m_via = nullptr;
|
|
|
|
|
|
|
|
if( aOther.m_via )
|
|
|
|
{
|
|
|
|
m_via = aOther.m_via->Clone();
|
|
|
|
m_via->SetOwner( this );
|
|
|
|
}
|
|
|
|
|
2015-08-12 19:12:12 +00:00
|
|
|
m_marker = aOther.m_marker;
|
|
|
|
m_rank = aOther.m_rank;
|
2021-01-01 17:17:55 +00:00
|
|
|
m_blockingObstacle = aOther.m_blockingObstacle;
|
2015-08-12 19:12:12 +00:00
|
|
|
|
|
|
|
copyLinks( &aOther );
|
2014-05-14 13:53:54 +00:00
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
LINE::~LINE()
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2022-09-11 21:17:45 +00:00
|
|
|
if( m_via && m_via->BelongsTo( this ) )
|
|
|
|
delete m_via;
|
2014-05-16 11:37:31 +00:00
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
|
2018-09-21 03:15:47 +00:00
|
|
|
LINE& LINE::operator=( const LINE& aOther )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
|
|
|
m_line = aOther.m_line;
|
|
|
|
m_width = aOther.m_width;
|
2018-10-09 21:50:20 +00:00
|
|
|
m_net = aOther.m_net;
|
|
|
|
m_movable = aOther.m_movable;
|
|
|
|
m_layers = aOther.m_layers;
|
2022-09-11 21:17:45 +00:00
|
|
|
|
|
|
|
m_via = nullptr;
|
|
|
|
|
|
|
|
if( aOther.m_via )
|
|
|
|
{
|
|
|
|
m_via = aOther.m_via->Clone();
|
|
|
|
m_via->SetOwner( this );
|
|
|
|
}
|
|
|
|
|
2018-10-09 21:50:20 +00:00
|
|
|
m_marker = aOther.m_marker;
|
|
|
|
m_rank = aOther.m_rank;
|
2019-08-07 21:45:38 +00:00
|
|
|
m_owner = aOther.m_owner;
|
2019-05-17 00:13:21 +00:00
|
|
|
m_snapThreshhold = aOther.m_snapThreshhold;
|
2021-01-01 17:17:55 +00:00
|
|
|
m_blockingObstacle = aOther.m_blockingObstacle;
|
2018-10-09 21:50:20 +00:00
|
|
|
|
2015-07-07 16:36:38 +00:00
|
|
|
copyLinks( &aOther );
|
2014-09-16 12:12:59 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2014-09-16 12:12:59 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
LINE* LINE::Clone() const
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
LINE* l = new LINE( *this );
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
return l;
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2020-10-05 19:54:12 +00:00
|
|
|
void LINE::Mark( int aMarker ) const
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
|
|
|
m_marker = aMarker;
|
2014-09-16 12:12:59 +00:00
|
|
|
|
2020-10-05 19:54:12 +00:00
|
|
|
for( const LINKED_ITEM* s : m_links )
|
2016-08-29 18:36:05 +00:00
|
|
|
s->Mark( aMarker );
|
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2020-10-05 19:54:12 +00:00
|
|
|
void LINE::Unmark( int aMarker ) const
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2020-10-05 19:54:12 +00:00
|
|
|
for( const LINKED_ITEM* s : m_links )
|
2017-01-17 14:06:33 +00:00
|
|
|
s->Unmark( aMarker );
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
m_marker = 0;
|
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
int LINE::Marker() const
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
|
|
|
int marker = m_marker;
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2020-04-15 20:16:24 +00:00
|
|
|
for( auto s : m_links )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2016-08-29 18:36:05 +00:00
|
|
|
marker |= s->Marker();
|
2014-05-14 13:53:54 +00:00
|
|
|
}
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
return marker;
|
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
SEGMENT* SEGMENT::Clone() const
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
SEGMENT* s = new SEGMENT;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
s->m_seg = m_seg;
|
2013-09-26 21:53:54 +00:00
|
|
|
s->m_net = m_net;
|
|
|
|
s->m_layers = m_layers;
|
2014-05-14 13:53:54 +00:00
|
|
|
s->m_marker = m_marker;
|
|
|
|
s->m_rank = m_rank;
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
return s;
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2016-08-29 18:36:05 +00:00
|
|
|
int LINE::CountCorners( int aAngles ) const
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2014-05-14 13:53:54 +00:00
|
|
|
int count = 0;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
for( int i = 0; i < m_line.SegmentCount() - 1; i++ )
|
|
|
|
{
|
|
|
|
const SEG seg1 = m_line.CSegment( i );
|
|
|
|
const SEG seg2 = m_line.CSegment( i + 1 );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
const DIRECTION_45 dir1( seg1 );
|
|
|
|
const DIRECTION_45 dir2( seg2 );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
DIRECTION_45::AngleType a = dir1.Angle( dir2 );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
if( a & aAngles )
|
|
|
|
count++;
|
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
return count;
|
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2021-05-27 22:00:40 +00:00
|
|
|
static int areNeighbours( int x, int y, int max = 0 )
|
|
|
|
{
|
|
|
|
if( x > 0 && x - 1 == y )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if( x < max - 1 && x + 1 == y )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-26 22:13:07 +00:00
|
|
|
#ifdef TOM_EXTRA_DEBUG
|
2021-05-27 22:00:40 +00:00
|
|
|
SHAPE_LINE_CHAIN g_pnew, g_hnew;
|
2022-10-26 22:13:07 +00:00
|
|
|
#endif
|
2021-05-27 22:00:40 +00:00
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
bool LINE::Walkaround( const SHAPE_LINE_CHAIN& aObstacle, SHAPE_LINE_CHAIN& aPath, bool aCw ) const
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2015-07-10 21:42:05 +00:00
|
|
|
const SHAPE_LINE_CHAIN& line( CLine() );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
if( line.SegmentCount() < 1 )
|
2021-02-18 22:17:12 +00:00
|
|
|
{
|
2014-05-14 13:53:54 +00:00
|
|
|
return false;
|
2021-02-18 22:17:12 +00:00
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2019-02-03 10:21:48 +00:00
|
|
|
const auto pFirst = line.CPoint(0);
|
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
bool inFirst = aObstacle.PointInside( pFirst ) && !aObstacle.PointOnEdge( pFirst );
|
|
|
|
|
2021-05-27 22:00:40 +00:00
|
|
|
// We can't really walk around if the beginning of the path lies inside the obstacle hull.
|
2021-02-18 22:17:12 +00:00
|
|
|
// Double check if it's not on the hull itself as this triggers many unroutable corner cases.
|
2021-05-27 22:00:40 +00:00
|
|
|
if( inFirst )
|
2019-02-03 10:21:48 +00:00
|
|
|
{
|
2021-05-27 22:00:40 +00:00
|
|
|
return false;
|
2019-02-03 10:21:48 +00:00
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
enum VERTEX_TYPE { INSIDE = 0, OUTSIDE, ON_EDGE };
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
// Represents an entry in directed graph of hull/path vertices. Scanning this graph
|
|
|
|
// starting from the path's first point results (if possible) with a correct walkaround path
|
|
|
|
struct VERTEX
|
2020-02-04 14:48:40 +00:00
|
|
|
{
|
2021-02-18 22:17:12 +00:00
|
|
|
// vertex classification (inside/outside/exactly on the hull)
|
|
|
|
VERTEX_TYPE type;
|
|
|
|
// true = vertex coming from the hull primitive
|
|
|
|
bool isHull;
|
|
|
|
// position
|
|
|
|
VECTOR2I pos;
|
|
|
|
// list of neighboring vertices
|
|
|
|
std::vector<VERTEX*> neighbours;
|
|
|
|
// index of this vertex in path (pnew)
|
|
|
|
int indexp = -1;
|
|
|
|
// index of this vertex in the hull (hnew)
|
|
|
|
int indexh = -1;
|
|
|
|
// visited indicator (for BFS search)
|
|
|
|
bool visited = false;
|
|
|
|
};
|
2020-02-04 14:48:40 +00:00
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
SHAPE_LINE_CHAIN::INTERSECTIONS ips;
|
2020-08-16 19:36:58 +00:00
|
|
|
|
2021-05-27 22:00:40 +00:00
|
|
|
HullIntersection( aObstacle, line, ips );
|
2020-02-04 14:48:40 +00:00
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
SHAPE_LINE_CHAIN pnew( CLine() ), hnew( aObstacle );
|
2020-02-04 14:48:40 +00:00
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
std::vector<VERTEX> vts;
|
2020-02-04 14:48:40 +00:00
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
auto findVertex = [&]( VECTOR2I pos) -> VERTEX*
|
|
|
|
{
|
|
|
|
for( VERTEX& v : vts )
|
2020-02-04 14:48:40 +00:00
|
|
|
{
|
2021-02-18 22:17:12 +00:00
|
|
|
if(v.pos == pos )
|
|
|
|
return &v;
|
2020-02-04 14:48:40 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
return nullptr;
|
|
|
|
};
|
2020-02-04 14:48:40 +00:00
|
|
|
|
2021-02-23 21:50:30 +00:00
|
|
|
|
2021-05-27 22:00:40 +00:00
|
|
|
// corner case for loopy tracks: insert the end loop point back into the hull
|
|
|
|
if( auto isect = pnew.SelfIntersecting() )
|
|
|
|
{
|
2021-05-30 22:17:08 +00:00
|
|
|
if( isect->p != pnew.CPoint( -1 ) )
|
|
|
|
{
|
|
|
|
pnew.Split( isect->p );
|
|
|
|
}
|
2021-02-23 21:50:30 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
// insert all intersections found into the new hull/path SLCs
|
2021-05-01 14:27:07 +00:00
|
|
|
for( auto& ip : ips )
|
2021-02-18 22:17:12 +00:00
|
|
|
{
|
2021-05-27 22:00:40 +00:00
|
|
|
if( pnew.Find( ip.p, 1 ) < 0)
|
2021-02-18 22:17:12 +00:00
|
|
|
{
|
|
|
|
pnew.Split(ip.p);
|
|
|
|
}
|
2020-02-04 14:48:40 +00:00
|
|
|
|
2021-05-27 22:00:40 +00:00
|
|
|
if( hnew.Find( ip.p, 1 ) < 0 )
|
2021-02-18 22:17:12 +00:00
|
|
|
{
|
|
|
|
hnew.Split(ip.p);
|
|
|
|
}
|
|
|
|
}
|
2020-08-18 14:17:16 +00:00
|
|
|
|
2021-05-27 22:00:40 +00:00
|
|
|
for( int i = 0; i < pnew.PointCount(); i++ )
|
|
|
|
{
|
|
|
|
auto p = pnew.CPoint(i);
|
|
|
|
bool onEdge = hnew.PointOnEdge( p );
|
|
|
|
|
|
|
|
if ( !onEdge )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int idx = hnew.Find( p );
|
|
|
|
|
|
|
|
if(idx < 0 )
|
|
|
|
{
|
|
|
|
hnew.Split(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TOM_EXTRA_DEBUG
|
|
|
|
for( auto& ip : ips )
|
|
|
|
{
|
|
|
|
printf("Chk: %d %d\n", pnew.Find( ip.p ), hnew.Find(ip.p) );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
// we assume the default orientation of the hulls is clockwise, so just reverse the vertex
|
|
|
|
// order if the caller wants a counter-clockwise walkaround
|
|
|
|
if ( !aCw )
|
|
|
|
hnew = hnew.Reverse();
|
2019-02-03 10:21:48 +00:00
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
vts.reserve( 2 * ( hnew.PointCount() + pnew.PointCount() ) );
|
2014-11-14 18:17:01 +00:00
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
// create a graph of hull/path vertices and classify them (inside/on edge/outside the hull)
|
|
|
|
for( int i = 0; i < pnew.PointCount(); i++ )
|
|
|
|
{
|
|
|
|
auto p = pnew.CPoint(i);
|
|
|
|
bool onEdge = hnew.PointOnEdge( p );
|
|
|
|
bool inside = hnew.PointInside( p );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2021-05-27 22:00:40 +00:00
|
|
|
#ifdef TOM_EXTRA_DEBUG
|
|
|
|
printf("pnew %d inside %d onedge %d\n", i, !!inside, !!onEdge );
|
|
|
|
#endif
|
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
VERTEX v;
|
2019-02-03 10:21:48 +00:00
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
v.indexp = i;
|
|
|
|
v.isHull = false;
|
|
|
|
v.pos = p;
|
|
|
|
v.type = inside && !onEdge ? INSIDE : onEdge ? ON_EDGE : OUTSIDE;
|
|
|
|
vts.push_back( v );
|
2019-02-03 10:21:48 +00:00
|
|
|
}
|
|
|
|
|
2021-05-27 22:00:40 +00:00
|
|
|
#ifdef TOM_EXTRA_DEBUG
|
|
|
|
g_pnew = pnew;
|
|
|
|
g_hnew = hnew;
|
|
|
|
#endif
|
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
// each path vertex neighbour list points for sure to the next vertex in the path
|
|
|
|
for( int i = 0; i < pnew.PointCount() - 1; i++ )
|
2019-02-03 10:21:48 +00:00
|
|
|
{
|
2021-02-18 22:17:12 +00:00
|
|
|
vts[i].neighbours.push_back( &vts[ i+1 ] );
|
2019-02-03 10:21:48 +00:00
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2021-05-27 22:00:40 +00:00
|
|
|
// each path vertex neighbour list points for sure to the next vertex in the path
|
|
|
|
for( int i = 1; i < pnew.PointCount() ; i++ )
|
|
|
|
{
|
|
|
|
vts[i].neighbours.push_back( &vts[ i-1 ] );
|
|
|
|
}
|
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
// insert hull vertices into the graph
|
|
|
|
for( int i = 0; i < hnew.PointCount(); i++ )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2021-02-18 22:17:12 +00:00
|
|
|
auto hp = hnew.CPoint( i );
|
|
|
|
auto vn = findVertex( hp );
|
2014-09-16 12:12:59 +00:00
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
// if vertex already present (it's very likely that in recursive shoving hull and path vertices will overlap)
|
|
|
|
// just mark it as a path vertex that also belongs to the hull
|
|
|
|
if( vn )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2021-02-18 22:17:12 +00:00
|
|
|
vn->isHull = true;
|
|
|
|
vn->indexh = i;
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
2021-02-18 22:17:12 +00:00
|
|
|
else // new hull vertex
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2021-02-18 22:17:12 +00:00
|
|
|
VERTEX v;
|
|
|
|
v.pos = hp;
|
|
|
|
v.type = ON_EDGE;
|
|
|
|
v.indexh = i;
|
|
|
|
v.isHull = true;
|
|
|
|
vts.push_back( v );
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
// go around the hull and fix up the neighbour link lists
|
|
|
|
for( int i = 0; i < hnew.PointCount(); i++ )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2021-02-18 22:17:12 +00:00
|
|
|
auto vc = findVertex( hnew.CPoint(i ) );
|
|
|
|
auto vnext = findVertex( hnew.CPoint( i+1 ) );
|
|
|
|
|
|
|
|
if(vc && vnext)
|
|
|
|
vc->neighbours.push_back(vnext);
|
2014-05-14 13:53:54 +00:00
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2021-06-06 16:55:20 +00:00
|
|
|
// In the case that the initial path ends *inside* the current obstacle (i.e. the mouse cursor
|
|
|
|
// is somewhere inside the hull for the current obstacle) we want to end the walkaround at the
|
|
|
|
// point closest to the cursor
|
|
|
|
bool inLast = aObstacle.PointInside( CPoint( -1 ) ) && !aObstacle.PointOnEdge( CPoint( -1 ) );
|
|
|
|
bool appendV = true;
|
|
|
|
int lastDst = INT_MAX;
|
|
|
|
|
2021-05-27 22:00:40 +00:00
|
|
|
int i = 0;
|
|
|
|
#ifdef TOM_EXTRA_DEBUG
|
|
|
|
for(auto &v: vts)
|
|
|
|
{
|
|
|
|
if( v.indexh < 0 && v.type == ON_EDGE )
|
|
|
|
{
|
|
|
|
v.type = OUTSIDE; // hack
|
|
|
|
}
|
|
|
|
printf("V %d pos %d %d ip %d ih %d type %d\n", i++, v.pos.x, v.pos.y, v.indexp, v.indexh, v.type );
|
|
|
|
}
|
|
|
|
#endif
|
2021-02-18 22:17:12 +00:00
|
|
|
// vts[0] = start point
|
2021-05-27 22:00:40 +00:00
|
|
|
VERTEX* v = &vts[0], *v_prev = nullptr;
|
2021-02-18 22:17:12 +00:00
|
|
|
SHAPE_LINE_CHAIN out;
|
2014-09-16 12:12:59 +00:00
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
int iterLimit = 1000;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
// keep scanning the graph until we reach the end point of the path
|
2021-06-06 16:55:20 +00:00
|
|
|
while( v->indexp != ( pnew.PointCount() - 1 ) )
|
2021-02-18 22:17:12 +00:00
|
|
|
{
|
|
|
|
iterLimit--;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
// I'm not 100% sure this algorithm doesn't have bugs that may cause it to freeze,
|
|
|
|
// so here's a temporary iteration limit
|
|
|
|
if( iterLimit == 0 )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2021-06-06 16:55:20 +00:00
|
|
|
if( v->visited )
|
2021-02-18 22:17:12 +00:00
|
|
|
{
|
2021-05-27 22:00:40 +00:00
|
|
|
// loop found? stop walking
|
2021-02-18 22:17:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-05-27 22:00:40 +00:00
|
|
|
#ifdef TOM_EXTRA_DEBUG
|
|
|
|
printf("---\nvisit ip %d ih %d type %d outs %d neig %d\n", v->indexp, v->indexh, v->type, out.PointCount(), v->neighbours.size() );
|
|
|
|
#endif
|
2021-02-18 22:17:12 +00:00
|
|
|
out.Append( v->pos );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
VERTEX* v_next = nullptr;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2021-06-06 16:55:20 +00:00
|
|
|
if( v->type == OUTSIDE )
|
2021-02-18 22:17:12 +00:00
|
|
|
{
|
|
|
|
// current vertex is outside? first look for any vertex further down the path
|
|
|
|
// that is not inside the hull
|
2021-06-06 16:55:20 +00:00
|
|
|
out.Append( v->pos );
|
2021-05-27 22:00:40 +00:00
|
|
|
VERTEX* v_next_fallback = nullptr;
|
2021-02-18 22:17:12 +00:00
|
|
|
for( auto vn : v->neighbours )
|
|
|
|
{
|
2021-06-06 16:55:20 +00:00
|
|
|
if( areNeighbours( vn->indexp , v->indexp, pnew.PointCount() ) &&
|
|
|
|
vn->type != INSIDE )
|
2021-02-18 22:17:12 +00:00
|
|
|
{
|
2021-06-06 16:55:20 +00:00
|
|
|
if( !vn->visited )
|
2021-05-27 22:00:40 +00:00
|
|
|
{
|
|
|
|
v_next = vn;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if( vn != v_prev )
|
|
|
|
v_next_fallback = vn;
|
2021-02-18 22:17:12 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-02 22:05:39 +00:00
|
|
|
|
2021-06-06 16:55:20 +00:00
|
|
|
if( !v_next )
|
2021-05-27 22:00:40 +00:00
|
|
|
v_next = v_next_fallback;
|
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
// such a vertex must always be present, if not, bummer.
|
2021-06-06 16:55:20 +00:00
|
|
|
if( !v_next )
|
2021-05-27 22:00:40 +00:00
|
|
|
{
|
|
|
|
#ifdef TOM_EXTRA_DEBUG
|
|
|
|
printf("FAIL VN fallback %p\n", v_next_fallback );
|
|
|
|
#endif
|
2021-02-18 22:17:12 +00:00
|
|
|
return false;
|
2021-05-27 22:00:40 +00:00
|
|
|
}
|
2014-09-16 12:12:59 +00:00
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
}
|
2021-06-06 16:55:20 +00:00
|
|
|
else if( v->type == ON_EDGE )
|
2021-02-18 22:17:12 +00:00
|
|
|
{
|
2021-05-27 22:00:40 +00:00
|
|
|
// look first for the first vertex outside the hull
|
|
|
|
for( VERTEX* vn : v->neighbours )
|
2021-02-18 22:17:12 +00:00
|
|
|
{
|
2021-05-27 22:00:40 +00:00
|
|
|
#ifdef TOM_EXTRA_DEBUG
|
|
|
|
printf( "- OUT scan ip %d ih %d type %d\n", vn->indexp, vn->indexh, vn->type );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if( vn->type == OUTSIDE && !vn->visited )
|
2021-02-18 22:17:12 +00:00
|
|
|
{
|
|
|
|
v_next = vn;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2021-05-27 22:00:40 +00:00
|
|
|
// no outside vertices found? continue traversing the hull
|
2021-02-18 22:17:12 +00:00
|
|
|
if( !v_next )
|
|
|
|
{
|
2021-06-06 16:55:20 +00:00
|
|
|
for( VERTEX* vn : v->neighbours )
|
2021-02-18 22:17:12 +00:00
|
|
|
{
|
2021-05-27 22:00:40 +00:00
|
|
|
#ifdef TOM_EXTRA_DEBUG
|
|
|
|
printf("- scan ip %d ih %d type %d\n", vn->indexp, vn->indexh, vn->type );
|
|
|
|
#endif
|
2021-06-06 16:55:20 +00:00
|
|
|
if( vn->type == ON_EDGE && !vn->isHull &&
|
2021-05-27 22:00:40 +00:00
|
|
|
areNeighbours( vn->indexp, v->indexp, pnew.PointCount() ) &&
|
|
|
|
( vn->indexh == ( ( v->indexh + 1 ) % hnew.PointCount() ) ) )
|
2021-02-18 22:17:12 +00:00
|
|
|
{
|
|
|
|
v_next = vn;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
// still nothing found? try to find the next (index-wise) point on the hull. I guess
|
|
|
|
// we should never reach this part of the code, but who really knows?
|
|
|
|
if( !v_next )
|
|
|
|
{
|
2022-10-26 22:13:07 +00:00
|
|
|
#ifdef TOM_EXTRA_DEBUG
|
|
|
|
printf("still no v_next\n");
|
|
|
|
#endif
|
2021-06-06 16:55:20 +00:00
|
|
|
for( VERTEX* vn : v->neighbours )
|
2021-02-18 22:17:12 +00:00
|
|
|
{
|
2021-06-06 16:55:20 +00:00
|
|
|
if( vn->type == ON_EDGE )
|
2021-02-18 22:17:12 +00:00
|
|
|
{
|
2021-06-06 16:55:20 +00:00
|
|
|
if( vn->indexh == ( ( v->indexh + 1 ) % hnew.PointCount() ) )
|
2021-02-18 22:17:12 +00:00
|
|
|
{
|
|
|
|
v_next = vn;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-06 16:55:20 +00:00
|
|
|
|
2022-10-26 22:13:07 +00:00
|
|
|
if( v_next )
|
|
|
|
{
|
|
|
|
for( auto &v : vts )
|
|
|
|
{
|
|
|
|
if( v.isHull )
|
|
|
|
v.visited = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TOM_EXTRA_DEBUG
|
|
|
|
printf("v_next %p\n", v_next);
|
|
|
|
#endif
|
|
|
|
|
2021-06-06 16:55:20 +00:00
|
|
|
// Did we get the next hull point but the end of the line is inside? Instead of walking
|
|
|
|
// around the hull some more (which will just end up taking us back to the start), lets
|
|
|
|
// just project the normal of the endpoint onto this next segment and call it quits.
|
|
|
|
if( inLast && v_next )
|
|
|
|
{
|
|
|
|
int d = ( v_next->pos - CPoint( -1 ) ).SquaredEuclideanNorm();
|
|
|
|
|
|
|
|
if( d < lastDst )
|
|
|
|
{
|
|
|
|
lastDst = d;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
VECTOR2I proj = SEG( v->pos, v_next->pos ).NearestPoint( CPoint( -1 ) );
|
|
|
|
out.Append( proj );
|
|
|
|
appendV = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-02-18 22:17:12 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
v->visited = true;
|
2021-05-27 22:00:40 +00:00
|
|
|
v_prev = v;
|
2021-02-18 22:17:12 +00:00
|
|
|
v = v_next;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
if( !v )
|
2021-05-27 22:00:40 +00:00
|
|
|
{
|
2021-02-18 22:17:12 +00:00
|
|
|
return false;
|
2021-05-27 22:00:40 +00:00
|
|
|
}
|
2021-02-18 22:17:12 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 16:55:20 +00:00
|
|
|
if( appendV )
|
|
|
|
out.Append( v->pos );
|
|
|
|
|
2021-02-18 22:17:12 +00:00
|
|
|
aPath = out;
|
2018-05-02 22:05:39 +00:00
|
|
|
|
|
|
|
return true;
|
2014-05-14 13:53:54 +00:00
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
|
2020-08-16 19:36:58 +00:00
|
|
|
const SHAPE_LINE_CHAIN SEGMENT::Hull( int aClearance, int aWalkaroundThickness, int aLayer ) const
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2022-08-30 12:52:34 +00:00
|
|
|
/*DEBUG_DECORATOR* debugDecorator = ROUTER::GetInstance()->GetInterface()->GetDebugDecorator();
|
|
|
|
|
|
|
|
PNS_DBG( debugDecorator, Message, wxString::Format( wxT( "seghull %d %d" ), aWalkaroundThickness, aClearance ) );
|
|
|
|
PNS_DBG(debugDecorator, AddShape, &m_seg, RED, 0, wxT("theseg") );
|
|
|
|
*/
|
|
|
|
|
2016-08-15 15:16:53 +00:00
|
|
|
return SegmentHull( m_seg, aClearance, aWalkaroundThickness );
|
2014-05-14 13:53:54 +00:00
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
const LINE LINE::ClipToNearestObstacle( NODE* aNode ) const
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2015-07-02 14:09:16 +00:00
|
|
|
const int IterationLimit = 5;
|
|
|
|
int i;
|
2016-08-29 17:31:13 +00:00
|
|
|
LINE l( *this );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2015-07-02 14:09:16 +00:00
|
|
|
for( i = 0; i < IterationLimit; i++ )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
NODE::OPT_OBSTACLE obs = aNode->NearestObstacle( &l );
|
2015-07-02 14:09:16 +00:00
|
|
|
|
|
|
|
if( obs )
|
|
|
|
{
|
|
|
|
l.RemoveVia();
|
2021-11-21 19:39:30 +00:00
|
|
|
VECTOR2I collisionPoint = obs->m_ipFirst;
|
|
|
|
int segIdx = l.Line().NearestSegment( collisionPoint );
|
|
|
|
|
|
|
|
if( l.Line().IsArcSegment( segIdx ) )
|
|
|
|
{
|
|
|
|
// Don't clip at arcs, start again
|
|
|
|
l.Line().Clear();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SEG nearestSegment = l.Line().CSegment( segIdx );
|
|
|
|
VECTOR2I nearestPt = nearestSegment.NearestPoint( collisionPoint );
|
|
|
|
int p = l.Line().Split( nearestPt );
|
|
|
|
l.Line().Remove( p + 1, -1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-07-02 14:09:16 +00:00
|
|
|
break;
|
2021-11-21 19:39:30 +00:00
|
|
|
}
|
2014-05-14 13:53:54 +00:00
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2015-07-02 14:09:16 +00:00
|
|
|
if( i == IterationLimit )
|
|
|
|
l.Line().Clear();
|
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
return l;
|
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
|
2018-11-25 22:40:22 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
SHAPE_LINE_CHAIN dragCornerInternal( const SHAPE_LINE_CHAIN& aOrigin, const VECTOR2I& aP )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2022-08-25 22:50:47 +00:00
|
|
|
std::optional<SHAPE_LINE_CHAIN> picked;
|
2014-05-14 13:53:54 +00:00
|
|
|
int i;
|
|
|
|
int d = 2;
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2021-02-04 01:54:49 +00:00
|
|
|
wxASSERT( aOrigin.PointCount() > 0 );
|
|
|
|
|
|
|
|
if( aOrigin.PointCount() == 1 )
|
|
|
|
{
|
|
|
|
return DIRECTION_45().BuildInitialTrace( aOrigin.CPoint( 0 ), aP );
|
|
|
|
}
|
|
|
|
else if( aOrigin.SegmentCount() == 1 )
|
2015-08-19 15:27:23 +00:00
|
|
|
{
|
|
|
|
DIRECTION_45 dir( aOrigin.CPoint( 0 ) - aOrigin.CPoint( 1 ) );
|
|
|
|
|
|
|
|
return DIRECTION_45().BuildInitialTrace( aOrigin.CPoint( 0 ), aP, dir.IsDiagonal() );
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( aOrigin.CSegment( -1 ).Length() > 100000 * 30 ) // fixme: constant/parameter?
|
2014-05-14 13:53:54 +00:00
|
|
|
d = 1;
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
for( i = aOrigin.SegmentCount() - d; i >= 0; i-- )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2016-08-15 15:16:53 +00:00
|
|
|
DIRECTION_45 d_start( aOrigin.CSegment( i ) );
|
2014-05-16 11:37:31 +00:00
|
|
|
VECTOR2I p_start = aOrigin.CPoint( i );
|
|
|
|
SHAPE_LINE_CHAIN paths[2];
|
2014-05-14 13:53:54 +00:00
|
|
|
DIRECTION_45 dirs[2];
|
2016-08-29 18:36:05 +00:00
|
|
|
DIRECTION_45 d_prev = ( i > 0 ? DIRECTION_45( aOrigin.CSegment( i-1 ) ) : DIRECTION_45() );
|
2018-11-25 22:40:22 +00:00
|
|
|
int dirCount = 0;
|
2014-05-14 13:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
for( int j = 0; j < 2; j++ )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
paths[j] = d_start.BuildInitialTrace( p_start, aP, j );
|
2018-11-25 22:40:22 +00:00
|
|
|
|
|
|
|
if( paths[j].SegmentCount() < 1 )
|
|
|
|
continue;
|
|
|
|
|
2018-12-01 19:07:59 +00:00
|
|
|
assert( dirCount < int( sizeof( dirs ) / sizeof( dirs[0] ) ) );
|
|
|
|
|
2018-11-25 22:40:22 +00:00
|
|
|
dirs[dirCount] = DIRECTION_45( paths[j].CSegment( 0 ) );
|
|
|
|
++dirCount;
|
2014-05-14 13:53:54 +00:00
|
|
|
}
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2018-11-25 22:40:22 +00:00
|
|
|
for( int j = 0; j < dirCount; j++ )
|
2014-05-16 11:37:31 +00:00
|
|
|
{
|
|
|
|
if( dirs[j] == d_start )
|
|
|
|
{
|
|
|
|
picked = paths[j];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( picked )
|
2014-05-14 13:53:54 +00:00
|
|
|
break;
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2018-11-25 22:40:22 +00:00
|
|
|
for( int j = 0; j < dirCount; j++ )
|
2014-05-16 11:37:31 +00:00
|
|
|
{
|
|
|
|
if( dirs[j].IsObtuse( d_prev ) )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
|
|
|
picked = paths[j];
|
|
|
|
break;
|
|
|
|
}
|
2014-05-16 11:37:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( picked )
|
|
|
|
break;
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
2014-09-16 12:12:59 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( picked )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
SHAPE_LINE_CHAIN path = aOrigin.Slice( 0, i );
|
|
|
|
path.Append( *picked );
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
return path;
|
2014-05-14 13:53:54 +00:00
|
|
|
}
|
2014-09-16 12:12:59 +00:00
|
|
|
|
2015-08-19 15:27:23 +00:00
|
|
|
DIRECTION_45 dir( aOrigin.CPoint( -1 ) - aOrigin.CPoint( -2 ) );
|
|
|
|
|
2015-10-12 12:08:27 +00:00
|
|
|
return DIRECTION_45().BuildInitialTrace( aOrigin.CPoint( 0 ), aP, dir.IsDiagonal() );
|
2014-05-14 13:53:54 +00:00
|
|
|
}
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2021-04-22 13:04:31 +00:00
|
|
|
|
2019-05-17 00:13:21 +00:00
|
|
|
void LINE::dragCorner45( const VECTOR2I& aP, int aIndex )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
|
|
|
SHAPE_LINE_CHAIN path;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2021-04-22 13:04:31 +00:00
|
|
|
int width = m_line.Width();
|
2019-05-17 00:13:21 +00:00
|
|
|
VECTOR2I snapped = snapDraggedCorner( m_line, aP, aIndex );
|
2014-05-14 13:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( aIndex == 0 )
|
2014-05-14 13:53:54 +00:00
|
|
|
path = dragCornerInternal( m_line.Reverse(), snapped ).Reverse();
|
2014-11-14 19:19:00 +00:00
|
|
|
else if( aIndex == m_line.SegmentCount() )
|
2014-05-14 13:53:54 +00:00
|
|
|
path = dragCornerInternal( m_line, snapped );
|
2014-05-16 11:37:31 +00:00
|
|
|
else
|
|
|
|
{
|
2021-04-12 23:46:34 +00:00
|
|
|
// Are we next to an arc? Insert a new point so we slice correctly
|
2021-05-06 22:10:39 +00:00
|
|
|
if( m_line.IsPtOnArc( static_cast<size_t>( aIndex ) + 1 ) )
|
2021-04-12 23:46:34 +00:00
|
|
|
m_line.Insert( aIndex + 1, m_line.CPoint( aIndex + 1 ) );
|
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
// fixme: awkward behaviour for "outwards" drags
|
2014-05-16 11:37:31 +00:00
|
|
|
path = dragCornerInternal( m_line.Slice( 0, aIndex ), snapped );
|
2019-05-17 00:13:21 +00:00
|
|
|
SHAPE_LINE_CHAIN path_rev =
|
2022-06-01 21:11:03 +00:00
|
|
|
dragCornerInternal( m_line.Slice( aIndex, -1 ).Reverse(), snapped ).Reverse();
|
2014-05-16 11:37:31 +00:00
|
|
|
path.Append( path_rev );
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
path.Simplify();
|
2021-04-22 13:04:31 +00:00
|
|
|
path.SetWidth( width );
|
2014-09-16 12:12:59 +00:00
|
|
|
m_line = path;
|
2014-05-14 13:53:54 +00:00
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2021-04-22 13:04:31 +00:00
|
|
|
|
2019-05-17 00:13:21 +00:00
|
|
|
void LINE::dragCornerFree( const VECTOR2I& aP, int aIndex )
|
2017-08-03 15:53:07 +00:00
|
|
|
{
|
2021-05-06 22:10:39 +00:00
|
|
|
ssize_t idx = static_cast<ssize_t>( aIndex );
|
|
|
|
ssize_t numpts = static_cast<ssize_t>( m_line.PointCount() );
|
2021-04-11 19:15:13 +00:00
|
|
|
|
|
|
|
// If we're asked to drag the end of an arc, insert a new vertex to drag instead
|
2021-05-06 22:10:39 +00:00
|
|
|
if( m_line.IsPtOnArc( idx ) )
|
2021-04-11 19:15:13 +00:00
|
|
|
{
|
2021-05-06 22:10:39 +00:00
|
|
|
if( idx == 0 || ( idx > 0 && !m_line.IsPtOnArc( idx - 1 ) ) )
|
2021-04-11 19:15:13 +00:00
|
|
|
{
|
2021-05-06 22:10:39 +00:00
|
|
|
m_line.Insert( idx, m_line.GetPoint( idx ) );
|
|
|
|
}
|
|
|
|
else if( ( idx == numpts - 1 ) || ( idx < numpts - 1 && !m_line.IsArcSegment( idx ) ) )
|
|
|
|
{
|
|
|
|
idx++;
|
|
|
|
m_line.Insert( idx, m_line.GetPoint( idx ) );
|
2021-04-11 19:15:13 +00:00
|
|
|
}
|
|
|
|
else
|
2021-05-06 22:10:39 +00:00
|
|
|
{
|
2022-02-05 02:06:25 +00:00
|
|
|
wxASSERT_MSG( false, wxT( "Attempt to dragCornerFree in the middle of an arc!" ) );
|
2021-05-06 22:10:39 +00:00
|
|
|
}
|
2021-04-11 19:15:13 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 22:10:39 +00:00
|
|
|
m_line.SetPoint( idx, aP );
|
2017-08-03 15:53:07 +00:00
|
|
|
m_line.Simplify();
|
|
|
|
}
|
|
|
|
|
2019-05-17 00:13:21 +00:00
|
|
|
void LINE::DragCorner( const VECTOR2I& aP, int aIndex, bool aFreeAngle )
|
2017-08-03 15:53:07 +00:00
|
|
|
{
|
2022-02-05 02:06:25 +00:00
|
|
|
wxCHECK_RET( aIndex >= 0, wxT( "Negative index passed to LINE::DragCorner" ) );
|
2021-04-13 00:42:50 +00:00
|
|
|
|
2017-08-03 15:53:07 +00:00
|
|
|
if( aFreeAngle )
|
|
|
|
{
|
2019-05-17 00:13:21 +00:00
|
|
|
dragCornerFree( aP, aIndex );
|
2017-08-03 15:53:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-17 00:13:21 +00:00
|
|
|
dragCorner45( aP, aIndex );
|
2017-08-03 15:53:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 00:13:21 +00:00
|
|
|
void LINE::DragSegment( const VECTOR2I& aP, int aIndex, bool aFreeAngle )
|
2017-08-03 15:53:07 +00:00
|
|
|
{
|
|
|
|
if( aFreeAngle )
|
|
|
|
{
|
|
|
|
assert( false );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-17 00:13:21 +00:00
|
|
|
dragSegment45( aP, aIndex );
|
2017-08-03 15:53:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 00:13:21 +00:00
|
|
|
VECTOR2I LINE::snapDraggedCorner(
|
|
|
|
const SHAPE_LINE_CHAIN& aPath, const VECTOR2I& aP, int aIndex ) const
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
int s_start = std::max( aIndex - 2, 0 );
|
|
|
|
int s_end = std::min( aIndex + 2, aPath.SegmentCount() - 1 );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2019-05-17 00:13:21 +00:00
|
|
|
int i, j;
|
|
|
|
int best_dist = INT_MAX;
|
2014-05-14 13:53:54 +00:00
|
|
|
VECTOR2I best_snap = aP;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2019-05-17 00:13:21 +00:00
|
|
|
if( m_snapThreshhold <= 0 )
|
2014-05-14 13:53:54 +00:00
|
|
|
return aP;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
for( i = s_start; i <= s_end; i++ )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
const SEG& a = aPath.CSegment( i );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
for( j = s_start; j < i; j++ )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
const SEG& b = aPath.CSegment( j );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2019-05-17 00:13:21 +00:00
|
|
|
if( !( DIRECTION_45( a ).IsObtuse( DIRECTION_45( b ) ) ) )
|
2014-05-14 13:53:54 +00:00
|
|
|
continue;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2019-05-17 00:13:21 +00:00
|
|
|
OPT_VECTOR2I ip = a.IntersectLines( b );
|
2014-09-16 12:12:59 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( ip )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
int dist = ( *ip - aP ).EuclideanNorm();
|
|
|
|
|
2019-05-17 00:13:21 +00:00
|
|
|
if( dist < m_snapThreshhold && dist < best_dist )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2014-05-14 13:53:54 +00:00
|
|
|
best_dist = dist;
|
|
|
|
best_snap = *ip;
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
return best_snap;
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 00:13:21 +00:00
|
|
|
VECTOR2I LINE::snapToNeighbourSegments(
|
|
|
|
const SHAPE_LINE_CHAIN& aPath, const VECTOR2I& aP, int aIndex ) const
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2019-05-17 00:13:21 +00:00
|
|
|
VECTOR2I snap_p[2];
|
2014-05-16 11:37:31 +00:00
|
|
|
DIRECTION_45 dragDir( aPath.CSegment( aIndex ) );
|
2019-05-17 00:13:21 +00:00
|
|
|
int snap_d[2] = { -1, -1 };
|
2014-05-14 13:53:54 +00:00
|
|
|
|
2019-05-17 00:13:21 +00:00
|
|
|
if( m_snapThreshhold == 0 )
|
2014-05-14 13:53:54 +00:00
|
|
|
return aP;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( aIndex >= 2 )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
SEG s = aPath.CSegment( aIndex - 2 );
|
|
|
|
|
|
|
|
if( DIRECTION_45( s ) == dragDir )
|
|
|
|
snap_d[0] = s.LineDistance( aP );
|
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
snap_p[0] = s.A;
|
2014-09-16 12:12:59 +00:00
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( aIndex < aPath.SegmentCount() - 2 )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
SEG s = aPath.CSegment( aIndex + 2 );
|
|
|
|
|
|
|
|
if( DIRECTION_45( s ) == dragDir )
|
2019-05-17 00:13:21 +00:00
|
|
|
snap_d[1] = s.LineDistance( aP );
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
snap_p[1] = s.A;
|
2014-09-16 12:12:59 +00:00
|
|
|
}
|
2014-05-14 13:53:54 +00:00
|
|
|
|
|
|
|
VECTOR2I best = aP;
|
2019-05-17 00:13:21 +00:00
|
|
|
int minDist = INT_MAX;
|
2014-05-14 13:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
for( int i = 0; i < 2; i++ )
|
|
|
|
{
|
2019-05-17 00:13:21 +00:00
|
|
|
if( snap_d[i] >= 0 && snap_d[i] < minDist && snap_d[i] <= m_snapThreshhold )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2014-05-14 13:53:54 +00:00
|
|
|
minDist = snap_d[i];
|
|
|
|
best = snap_p[i];
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
2014-05-16 11:37:31 +00:00
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
return best;
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 00:13:21 +00:00
|
|
|
void LINE::dragSegment45( const VECTOR2I& aP, int aIndex )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
SHAPE_LINE_CHAIN path( m_line );
|
2019-05-17 00:13:21 +00:00
|
|
|
VECTOR2I target( aP );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2021-04-12 02:12:53 +00:00
|
|
|
wxASSERT( aIndex < m_line.PointCount() );
|
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
SEG guideA[2], guideB[2];
|
|
|
|
int index = aIndex;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2019-05-17 00:13:21 +00:00
|
|
|
target = snapToNeighbourSegments( path, aP, aIndex );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2021-04-12 23:46:34 +00:00
|
|
|
// We require a valid s_prev and s_next. If we are at the start or end of the line, we insert
|
|
|
|
// a new point at the start or end so there is a zero-length segment for prev or next (we will
|
|
|
|
// resize it as part of the drag operation). If we are next to an arc, we do this also, as we
|
|
|
|
// cannot drag away one of the arc's points.
|
|
|
|
|
2021-05-06 22:10:39 +00:00
|
|
|
if( index == 0 || path.IsPtOnArc( index ) )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2021-04-12 23:46:34 +00:00
|
|
|
path.Insert( index > 0 ? index + 1 : 0, path.CPoint( index ) );
|
2014-05-14 13:53:54 +00:00
|
|
|
index++;
|
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( index == path.SegmentCount() - 1 )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
path.Insert( path.PointCount() - 1, path.CPoint( -1 ) );
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
2021-05-06 22:10:39 +00:00
|
|
|
else if( path.IsPtOnArc( index + 1 ) )
|
2021-04-12 23:46:34 +00:00
|
|
|
{
|
|
|
|
path.Insert( index + 1, path.CPoint( index + 1 ) );
|
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2019-05-17 00:13:21 +00:00
|
|
|
SEG dragged = path.CSegment( index );
|
2014-05-16 11:37:31 +00:00
|
|
|
DIRECTION_45 drag_dir( dragged );
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
SEG s_prev = path.CSegment( index - 1 );
|
|
|
|
SEG s_next = path.CSegment( index + 1 );
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
DIRECTION_45 dir_prev( s_prev );
|
|
|
|
DIRECTION_45 dir_next( s_next );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( dir_prev == drag_dir )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
|
|
|
dir_prev = dir_prev.Left();
|
2014-05-16 11:37:31 +00:00
|
|
|
path.Insert( index, path.CPoint( index ) );
|
2014-05-14 13:53:54 +00:00
|
|
|
index++;
|
|
|
|
}
|
2021-04-12 23:46:34 +00:00
|
|
|
else if( dir_prev == DIRECTION_45::UNDEFINED )
|
|
|
|
{
|
|
|
|
dir_prev = drag_dir.Left();
|
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( dir_next == drag_dir )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
|
|
|
dir_next = dir_next.Right();
|
2014-05-16 11:37:31 +00:00
|
|
|
path.Insert( index + 1, path.CPoint( index + 1 ) );
|
2014-05-14 13:53:54 +00:00
|
|
|
}
|
2021-04-12 23:46:34 +00:00
|
|
|
else if( dir_next == DIRECTION_45::UNDEFINED )
|
|
|
|
{
|
|
|
|
dir_next = drag_dir.Right();
|
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
s_prev = path.CSegment( index - 1 );
|
|
|
|
s_next = path.CSegment( index + 1 );
|
|
|
|
dragged = path.CSegment( index );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( aIndex == 0 )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2019-05-17 00:13:21 +00:00
|
|
|
guideA[0] = SEG( dragged.A, dragged.A + drag_dir.Right().ToVector() );
|
|
|
|
guideA[1] = SEG( dragged.A, dragged.A + drag_dir.Left().ToVector() );
|
2014-05-16 11:37:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-17 00:13:21 +00:00
|
|
|
if( dir_prev.Angle( drag_dir )
|
|
|
|
& ( DIRECTION_45::ANG_OBTUSE | DIRECTION_45::ANG_HALF_FULL ) )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
guideA[0] = SEG( s_prev.A, s_prev.A + drag_dir.Left().ToVector() );
|
|
|
|
guideA[1] = SEG( s_prev.A, s_prev.A + drag_dir.Right().ToVector() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
guideA[0] = guideA[1] = SEG( dragged.A, dragged.A + dir_prev.ToVector() );
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( aIndex == m_line.SegmentCount() - 1 )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2019-05-17 00:13:21 +00:00
|
|
|
guideB[0] = SEG( dragged.B, dragged.B + drag_dir.Right().ToVector() );
|
|
|
|
guideB[1] = SEG( dragged.B, dragged.B + drag_dir.Left().ToVector() );
|
2014-05-16 11:37:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-17 00:13:21 +00:00
|
|
|
if( dir_next.Angle( drag_dir )
|
|
|
|
& ( DIRECTION_45::ANG_OBTUSE | DIRECTION_45::ANG_HALF_FULL ) )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
guideB[0] = SEG( s_next.B, s_next.B + drag_dir.Left().ToVector() );
|
|
|
|
guideB[1] = SEG( s_next.B, s_next.B + drag_dir.Right().ToVector() );
|
|
|
|
}
|
|
|
|
else
|
2019-05-17 00:13:21 +00:00
|
|
|
guideB[0] = guideB[1] = SEG( dragged.B, dragged.B + dir_next.ToVector() );
|
2014-05-14 13:53:54 +00:00
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
SEG s_current( target, target + drag_dir.ToVector() );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2019-05-17 00:13:21 +00:00
|
|
|
int best_len = INT_MAX;
|
2014-05-14 13:53:54 +00:00
|
|
|
SHAPE_LINE_CHAIN best;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
for( int i = 0; i < 2; i++ )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
for( int j = 0; j < 2; j++ )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
OPT_VECTOR2I ip1 = s_current.IntersectLines( guideA[i] );
|
|
|
|
OPT_VECTOR2I ip2 = s_current.IntersectLines( guideB[j] );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
SHAPE_LINE_CHAIN np;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( !ip1 || !ip2 )
|
|
|
|
continue;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
SEG s1( s_prev.A, *ip1 );
|
|
|
|
SEG s2( *ip1, *ip2 );
|
|
|
|
SEG s3( *ip2, s_next.B );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
OPT_VECTOR2I ip;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2019-05-17 00:13:21 +00:00
|
|
|
if( ( ip = s1.Intersect( s_next ) ) )
|
2014-05-16 11:37:31 +00:00
|
|
|
{
|
2016-08-15 15:16:53 +00:00
|
|
|
np.Append( s1.A );
|
|
|
|
np.Append( *ip );
|
|
|
|
np.Append( s_next.B );
|
2014-05-16 11:37:31 +00:00
|
|
|
}
|
2019-05-17 00:13:21 +00:00
|
|
|
else if( ( ip = s3.Intersect( s_prev ) ) )
|
2014-05-16 11:37:31 +00:00
|
|
|
{
|
2016-08-15 15:16:53 +00:00
|
|
|
np.Append( s_prev.A );
|
|
|
|
np.Append( *ip );
|
|
|
|
np.Append( s3.B );
|
2014-05-16 11:37:31 +00:00
|
|
|
}
|
2019-05-17 00:13:21 +00:00
|
|
|
else if( ( ip = s1.Intersect( s3 ) ) )
|
2014-05-16 11:37:31 +00:00
|
|
|
{
|
|
|
|
np.Append( s_prev.A );
|
|
|
|
np.Append( *ip );
|
|
|
|
np.Append( s_next.B );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
np.Append( s_prev.A );
|
|
|
|
np.Append( *ip1 );
|
|
|
|
np.Append( *ip2 );
|
|
|
|
np.Append( s_next.B );
|
|
|
|
}
|
2014-05-14 13:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( np.Length() < best_len )
|
|
|
|
{
|
|
|
|
best_len = np.Length();
|
|
|
|
best = np;
|
|
|
|
}
|
2014-05-14 13:53:54 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( m_line.PointCount() == 1 )
|
2014-05-14 13:53:54 +00:00
|
|
|
m_line = best;
|
2014-05-16 11:37:31 +00:00
|
|
|
else if( aIndex == 0 )
|
|
|
|
m_line.Replace( 0, 1, best );
|
|
|
|
else if( aIndex == m_line.SegmentCount() - 1 )
|
|
|
|
m_line.Replace( -2, -1, best );
|
2014-05-14 13:53:54 +00:00
|
|
|
else
|
2014-05-16 11:37:31 +00:00
|
|
|
m_line.Replace( aIndex, aIndex + 1, best );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
m_line.Simplify();
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
bool LINE::CompareGeometry( const LINE& aOther )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
return m_line.CompareGeometry( aOther.m_line );
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
void LINE::Reverse()
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
m_line = m_line.Reverse();
|
|
|
|
|
2020-04-15 20:16:24 +00:00
|
|
|
std::reverse( m_links.begin(), m_links.end() );
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
void LINE::AppendVia( const VIA& aVia )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2015-07-02 14:09:24 +00:00
|
|
|
if( m_line.PointCount() > 1 && aVia.Pos() == m_line.CPoint( 0 ) )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
|
|
|
Reverse();
|
|
|
|
}
|
2014-09-16 12:12:59 +00:00
|
|
|
|
2022-09-11 21:17:45 +00:00
|
|
|
m_via = aVia.Clone();
|
|
|
|
m_via->SetOwner( this );
|
|
|
|
m_via->SetNet( m_net );
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
void LINE::SetRank( int aRank )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2014-05-14 13:53:54 +00:00
|
|
|
m_rank = aRank;
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2020-04-15 20:16:24 +00:00
|
|
|
for( auto s : m_links )
|
2016-08-29 18:36:05 +00:00
|
|
|
s->SetRank( aRank );
|
|
|
|
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
int LINE::Rank() const
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2014-05-14 13:53:54 +00:00
|
|
|
int min_rank = INT_MAX;
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2022-10-10 10:15:19 +00:00
|
|
|
if( IsLinked() )
|
|
|
|
{
|
|
|
|
for( const LINKED_ITEM* item : m_links )
|
|
|
|
min_rank = std::min( min_rank, item->Rank() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-08-29 18:36:05 +00:00
|
|
|
min_rank = m_rank;
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2016-08-29 18:36:05 +00:00
|
|
|
int rank = ( min_rank == INT_MAX ) ? -1 : min_rank;
|
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
return rank;
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
void LINE::ClipVertexRange( int aStart, int aEnd )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2021-01-04 02:37:24 +00:00
|
|
|
/**
|
|
|
|
* We need to figure out which joints to keep after the clip operation, because arcs will have
|
|
|
|
* multiple vertices. It is assumed that anything calling this method will have determined the
|
|
|
|
* vertex range to clip based on joints, meaning we will never clip in the middle of an arc.
|
|
|
|
* Clipping in the middle of an arc would break this and various other things...
|
|
|
|
*/
|
|
|
|
int firstLink = 0;
|
|
|
|
int lastLink = std::max( 0, static_cast<int>( m_links.size() ) - 1 );
|
|
|
|
int linkIdx = 0;
|
|
|
|
|
2021-05-06 22:10:39 +00:00
|
|
|
int numPoints = static_cast<int>( m_line.PointCount() );
|
2021-01-04 02:37:24 +00:00
|
|
|
|
2021-11-21 16:03:22 +00:00
|
|
|
for( int i = 0; i >= 0 && i < m_line.PointCount(); i = m_line.NextShape( i ) )
|
2021-01-04 02:37:24 +00:00
|
|
|
{
|
|
|
|
if( i <= aStart )
|
|
|
|
firstLink = linkIdx;
|
|
|
|
|
2021-07-07 18:54:19 +00:00
|
|
|
if( i < 0 || i >= aEnd - 1 || linkIdx >= lastLink )
|
2021-01-04 02:37:24 +00:00
|
|
|
{
|
|
|
|
lastLink = linkIdx;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
linkIdx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxASSERT( lastLink >= firstLink );
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
m_line = m_line.Slice( aStart, aEnd );
|
2014-09-16 12:12:59 +00:00
|
|
|
|
2021-01-04 02:37:24 +00:00
|
|
|
if( IsLinked() )
|
|
|
|
{
|
|
|
|
wxASSERT( m_links.size() < INT_MAX );
|
|
|
|
wxASSERT( static_cast<int>( m_links.size() ) >= ( lastLink - firstLink ) );
|
2016-08-29 18:36:05 +00:00
|
|
|
|
|
|
|
// Note: The range includes aEnd, but we have n-1 segments.
|
|
|
|
std::rotate(
|
2020-04-15 20:16:24 +00:00
|
|
|
m_links.begin(),
|
2021-01-04 02:37:24 +00:00
|
|
|
m_links.begin() + firstLink,
|
|
|
|
m_links.begin() + lastLink
|
2016-08-29 18:36:05 +00:00
|
|
|
);
|
2014-09-16 12:12:59 +00:00
|
|
|
|
2021-01-04 02:37:24 +00:00
|
|
|
m_links.resize( lastLink - firstLink + 1 );
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
2014-05-14 13:53:54 +00:00
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
bool LINE::HasLoops() const
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
for( int i = 0; i < PointCount(); i++ )
|
|
|
|
{
|
2018-04-30 16:14:23 +00:00
|
|
|
for( int j = i + 2; j < PointCount(); j++ )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2018-04-30 16:14:23 +00:00
|
|
|
if( CPoint( i ) == CPoint( j ) )
|
2014-05-14 13:53:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
2014-05-16 11:37:31 +00:00
|
|
|
}
|
2014-09-16 12:12:59 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
return false;
|
2014-05-14 13:53:54 +00:00
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
static void extendBox( BOX2I& aBox, bool& aDefined, const VECTOR2I& aP )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
if( aDefined )
|
2016-08-15 15:16:53 +00:00
|
|
|
{
|
|
|
|
aBox.Merge( aP );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-18 16:53:46 +00:00
|
|
|
aBox = BOX2I( aP, VECTOR2I( 0, 0 ) );
|
2015-02-18 00:29:54 +00:00
|
|
|
aDefined = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
OPT_BOX2I LINE::ChangedArea( const LINE* aOther ) const
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
BOX2I area;
|
|
|
|
bool areaDefined = false;
|
|
|
|
|
|
|
|
int i_start = -1;
|
|
|
|
int i_end_self = -1, i_end_other = -1;
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
SHAPE_LINE_CHAIN self( m_line );
|
2015-02-18 00:29:54 +00:00
|
|
|
self.Simplify();
|
2015-02-18 16:53:46 +00:00
|
|
|
SHAPE_LINE_CHAIN other( aOther->m_line );
|
2015-02-18 00:29:54 +00:00
|
|
|
other.Simplify();
|
|
|
|
|
|
|
|
int np_self = self.PointCount();
|
|
|
|
int np_other = other.PointCount();
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
int n = std::min( np_self, np_other );
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
for( int i = 0; i < n; i++ )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2015-02-18 16:53:46 +00:00
|
|
|
const VECTOR2I p1 = self.CPoint( i );
|
|
|
|
const VECTOR2I p2 = other.CPoint( i );
|
|
|
|
|
|
|
|
if( p1 != p2 )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2015-02-18 16:53:46 +00:00
|
|
|
if( i != n - 1 )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2015-02-18 16:53:46 +00:00
|
|
|
SEG s = self.CSegment( i );
|
|
|
|
|
2015-02-18 00:29:54 +00:00
|
|
|
if( !s.Contains( p2 ) )
|
|
|
|
{
|
|
|
|
i_start = i;
|
|
|
|
break;
|
|
|
|
}
|
2018-04-30 16:14:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-18 00:29:54 +00:00
|
|
|
i_start = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
for( int i = 0; i < n; i++ )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
const VECTOR2I p1 = self.CPoint( np_self - 1 - i );
|
|
|
|
const VECTOR2I p2 = other.CPoint( np_other - 1 - i );
|
2015-02-18 16:53:46 +00:00
|
|
|
|
|
|
|
if( p1 != p2 )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
i_end_self = np_self - 1 - i;
|
|
|
|
i_end_other = np_other - 1 - i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( i_start < 0 )
|
|
|
|
i_start = n;
|
|
|
|
|
|
|
|
if( i_end_self < 0 )
|
|
|
|
i_end_self = np_self - 1;
|
|
|
|
|
|
|
|
if( i_end_other < 0 )
|
|
|
|
i_end_other = np_other - 1;
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
for( int i = i_start; i <= i_end_self; i++ )
|
|
|
|
extendBox( area, areaDefined, self.CPoint( i ) );
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
for( int i = i_start; i <= i_end_other; i++ )
|
|
|
|
extendBox( area, areaDefined, other.CPoint( i ) );
|
2015-02-18 00:29:54 +00:00
|
|
|
|
|
|
|
if( areaDefined )
|
|
|
|
{
|
2015-02-18 16:53:46 +00:00
|
|
|
area.Inflate( std::max( Width(), aOther->Width() ) );
|
2015-02-18 00:29:54 +00:00
|
|
|
return area;
|
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
return OPT_BOX2I();
|
2015-02-18 00:29:54 +00:00
|
|
|
}
|
2016-08-15 15:16:48 +00:00
|
|
|
|
2016-08-15 15:16:53 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
bool LINE::HasLockedSegments() const
|
2016-08-15 15:16:48 +00:00
|
|
|
{
|
2020-04-15 20:16:24 +00:00
|
|
|
for( const auto seg : m_links )
|
2016-08-15 15:16:48 +00:00
|
|
|
{
|
2016-08-15 15:16:53 +00:00
|
|
|
if( seg->Marker() & MK_LOCKED )
|
2016-08-15 15:16:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-29 14:38:11 +00:00
|
|
|
|
2022-10-23 22:34:42 +00:00
|
|
|
|
2020-02-19 17:22:09 +00:00
|
|
|
void LINE::Clear()
|
|
|
|
{
|
2022-09-11 21:17:45 +00:00
|
|
|
if( m_via && m_via->BelongsTo( this ) )
|
|
|
|
{
|
|
|
|
delete m_via;
|
|
|
|
m_via = nullptr;
|
|
|
|
}
|
2020-02-19 17:22:09 +00:00
|
|
|
m_line.Clear();
|
2016-08-29 14:38:11 +00:00
|
|
|
}
|
2020-02-19 17:22:09 +00:00
|
|
|
|
|
|
|
|
2022-09-11 21:17:45 +00:00
|
|
|
void LINE::RemoveVia()
|
|
|
|
{
|
|
|
|
if( m_via && m_via->BelongsTo( this ) )
|
|
|
|
delete m_via;
|
|
|
|
|
|
|
|
m_via = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-23 22:34:42 +00:00
|
|
|
const std::string SEGMENT::Format( ) const
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << ITEM::Format() << " ";
|
|
|
|
ss << m_seg.Format( false );
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2020-02-19 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2022-09-11 21:17:45 +00:00
|
|
|
|