2013-09-10 11:43:09 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2017-01-19 12:55:22 +00:00
|
|
|
* Copyright (C) 2013-2017 CERN
|
2013-09-10 11:43:09 +00:00
|
|
|
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2017-11-01 11:14:16 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2013-09-10 11:43:09 +00:00
|
|
|
#include <geometry/shape_line_chain.h>
|
|
|
|
#include <geometry/shape_circle.h>
|
2018-09-12 10:19:11 +00:00
|
|
|
#include "clipper.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
ClipperLib::Path SHAPE_LINE_CHAIN::convertToClipper( bool aRequiredOrientation ) const
|
|
|
|
{
|
|
|
|
ClipperLib::Path c_path;
|
|
|
|
|
|
|
|
for( int i = 0; i < PointCount(); i++ )
|
|
|
|
{
|
|
|
|
const VECTOR2I& vertex = CPoint( i );
|
|
|
|
c_path.push_back( ClipperLib::IntPoint( vertex.x, vertex.y ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( Orientation( c_path ) != aRequiredOrientation )
|
|
|
|
ReversePath( c_path );
|
|
|
|
|
|
|
|
return c_path;
|
|
|
|
}
|
|
|
|
|
2013-09-10 11:43:09 +00:00
|
|
|
|
2013-09-27 14:23:43 +00:00
|
|
|
bool SHAPE_LINE_CHAIN::Collide( const VECTOR2I& aP, int aClearance ) const
|
2013-09-10 11:43:09 +00:00
|
|
|
{
|
2014-11-14 19:19:00 +00:00
|
|
|
// fixme: ugly!
|
|
|
|
SEG s( aP, aP );
|
|
|
|
return this->Collide( s, aClearance );
|
2013-09-10 11:43:09 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 14:23:43 +00:00
|
|
|
|
2017-12-22 09:54:47 +00:00
|
|
|
void SHAPE_LINE_CHAIN::Rotate( double aAngle, const VECTOR2I& aCenter )
|
|
|
|
{
|
|
|
|
for( std::vector<VECTOR2I>::iterator i = m_points.begin(); i != m_points.end(); ++i )
|
|
|
|
{
|
|
|
|
(*i) -= aCenter;
|
|
|
|
(*i) = (*i).Rotate( aAngle );
|
|
|
|
(*i) += aCenter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-27 14:23:43 +00:00
|
|
|
bool SHAPE_LINE_CHAIN::Collide( const SEG& aSeg, int aClearance ) const
|
2013-09-10 11:43:09 +00:00
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
BOX2I box_a( aSeg.A, aSeg.B - aSeg.A );
|
2013-10-14 11:43:57 +00:00
|
|
|
BOX2I::ecoord_type dist_sq = (BOX2I::ecoord_type) aClearance * aClearance;
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
for( int i = 0; i < SegmentCount(); i++ )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
|
|
|
const SEG& s = CSegment( i );
|
2013-10-14 18:40:36 +00:00
|
|
|
BOX2I box_b( s.A, s.B - s.A );
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
BOX2I::ecoord_type d = box_a.SquaredDistance( box_b );
|
2013-10-14 11:43:57 +00:00
|
|
|
|
|
|
|
if( d < dist_sq )
|
|
|
|
{
|
|
|
|
if( s.Collide( aSeg, aClearance ) )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2013-09-10 11:43:09 +00:00
|
|
|
}
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
const SHAPE_LINE_CHAIN SHAPE_LINE_CHAIN::Reverse() const
|
2013-09-10 11:43:09 +00:00
|
|
|
{
|
2013-10-14 11:43:57 +00:00
|
|
|
SHAPE_LINE_CHAIN a( *this );
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
reverse( a.m_points.begin(), a.m_points.end() );
|
|
|
|
a.m_closed = m_closed;
|
2013-09-10 11:43:09 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
return a;
|
2013-09-10 11:43:09 +00:00
|
|
|
}
|
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-09-10 11:43:09 +00:00
|
|
|
int SHAPE_LINE_CHAIN::Length() const
|
|
|
|
{
|
2013-10-14 11:43:57 +00:00
|
|
|
int l = 0;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
for( int i = 0; i < SegmentCount(); i++ )
|
|
|
|
l += CSegment( i ).Length();
|
2013-09-27 14:23:43 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
return l;
|
2013-09-10 11:43:09 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 14:23:43 +00:00
|
|
|
|
|
|
|
void SHAPE_LINE_CHAIN::Replace( int aStartIndex, int aEndIndex, const VECTOR2I& aP )
|
2013-09-10 11:43:09 +00:00
|
|
|
{
|
2013-10-14 11:43:57 +00:00
|
|
|
if( aEndIndex < 0 )
|
|
|
|
aEndIndex += PointCount();
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
if( aStartIndex < 0 )
|
|
|
|
aStartIndex += PointCount();
|
|
|
|
|
|
|
|
if( aStartIndex == aEndIndex )
|
2013-10-14 14:13:35 +00:00
|
|
|
m_points[aStartIndex] = aP;
|
2013-10-14 11:43:57 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
m_points.erase( m_points.begin() + aStartIndex + 1, m_points.begin() + aEndIndex + 1 );
|
|
|
|
m_points[aStartIndex] = aP;
|
|
|
|
}
|
2013-09-10 11:43:09 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 14:23:43 +00:00
|
|
|
|
|
|
|
void SHAPE_LINE_CHAIN::Replace( int aStartIndex, int aEndIndex, const SHAPE_LINE_CHAIN& aLine )
|
2013-09-10 11:43:09 +00:00
|
|
|
{
|
2013-10-14 11:43:57 +00:00
|
|
|
if( aEndIndex < 0 )
|
|
|
|
aEndIndex += PointCount();
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
if( aStartIndex < 0 )
|
|
|
|
aStartIndex += PointCount();
|
2013-09-10 11:43:09 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
m_points.erase( m_points.begin() + aStartIndex, m_points.begin() + aEndIndex + 1 );
|
|
|
|
m_points.insert( m_points.begin() + aStartIndex, aLine.m_points.begin(), aLine.m_points.end() );
|
2013-09-10 11:43:09 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 14:23:43 +00:00
|
|
|
|
|
|
|
void SHAPE_LINE_CHAIN::Remove( int aStartIndex, int aEndIndex )
|
2013-09-10 11:43:09 +00:00
|
|
|
{
|
2013-10-14 14:13:35 +00:00
|
|
|
if( aEndIndex < 0 )
|
2013-10-14 11:43:57 +00:00
|
|
|
aEndIndex += PointCount();
|
2013-10-14 14:13:35 +00:00
|
|
|
|
|
|
|
if( aStartIndex < 0 )
|
2013-10-14 11:43:57 +00:00
|
|
|
aStartIndex += PointCount();
|
2013-09-10 11:43:09 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
m_points.erase( m_points.begin() + aStartIndex, m_points.begin() + aEndIndex + 1 );
|
2013-09-10 11:43:09 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 14:23:43 +00:00
|
|
|
|
2017-10-19 21:14:36 +00:00
|
|
|
int SHAPE_LINE_CHAIN::Distance( const VECTOR2I& aP, bool aOutlineOnly ) const
|
2013-09-10 11:43:09 +00:00
|
|
|
{
|
2013-10-14 11:43:57 +00:00
|
|
|
int d = INT_MAX;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2017-10-19 21:14:36 +00:00
|
|
|
if( IsClosed() && PointInside( aP ) && !aOutlineOnly )
|
2014-05-14 09:45:01 +00:00
|
|
|
return 0;
|
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
for( int s = 0; s < SegmentCount(); s++ )
|
2013-12-13 16:27:30 +00:00
|
|
|
d = std::min( d, CSegment( s ).Distance( aP ) );
|
2013-09-27 14:23:43 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
return d;
|
2013-09-10 11:43:09 +00:00
|
|
|
}
|
2013-09-27 14:23:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
int SHAPE_LINE_CHAIN::Split( const VECTOR2I& aP )
|
2013-09-10 11:43:09 +00:00
|
|
|
{
|
2013-10-14 11:43:57 +00:00
|
|
|
int ii = -1;
|
|
|
|
int min_dist = 2;
|
|
|
|
|
2014-11-14 18:17:01 +00:00
|
|
|
int found_index = Find( aP );
|
2013-10-14 11:43:57 +00:00
|
|
|
|
|
|
|
for( int s = 0; s < SegmentCount(); s++ )
|
|
|
|
{
|
|
|
|
const SEG seg = CSegment( s );
|
|
|
|
int dist = seg.Distance( aP );
|
|
|
|
|
|
|
|
// make sure we are not producing a 'slightly concave' primitive. This might happen
|
|
|
|
// if aP lies very close to one of already existing points.
|
2013-10-14 18:40:36 +00:00
|
|
|
if( dist < min_dist && seg.A != aP && seg.B != aP )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
|
|
|
min_dist = dist;
|
2014-11-14 19:19:00 +00:00
|
|
|
if( found_index < 0 )
|
2014-11-14 18:17:01 +00:00
|
|
|
ii = s;
|
2014-11-14 19:19:00 +00:00
|
|
|
else if( s < found_index )
|
2014-11-14 18:17:01 +00:00
|
|
|
ii = s;
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-10 11:43:09 +00:00
|
|
|
|
2014-11-14 18:17:01 +00:00
|
|
|
if( ii < 0 )
|
|
|
|
ii = found_index;
|
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
if( ii >= 0 )
|
|
|
|
{
|
|
|
|
m_points.insert( m_points.begin() + ii + 1, aP );
|
2013-09-27 14:23:43 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
return ii + 1;
|
|
|
|
}
|
2013-09-10 11:43:09 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
return -1;
|
2013-09-10 11:43:09 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 14:23:43 +00:00
|
|
|
|
|
|
|
int SHAPE_LINE_CHAIN::Find( const VECTOR2I& aP ) const
|
2013-09-10 11:43:09 +00:00
|
|
|
{
|
2014-05-14 09:45:01 +00:00
|
|
|
for( int s = 0; s < PointCount(); s++ )
|
2013-10-14 11:43:57 +00:00
|
|
|
if( CPoint( s ) == aP )
|
|
|
|
return s;
|
2013-09-27 14:23:43 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
return -1;
|
2013-09-10 11:43:09 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 14:23:43 +00:00
|
|
|
|
2014-05-14 09:45:01 +00:00
|
|
|
int SHAPE_LINE_CHAIN::FindSegment( const VECTOR2I& aP ) const
|
|
|
|
{
|
|
|
|
for( int s = 0; s < SegmentCount(); s++ )
|
|
|
|
if( CSegment( s ).Distance( aP ) <= 1 )
|
|
|
|
return s;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-27 14:23:43 +00:00
|
|
|
const SHAPE_LINE_CHAIN SHAPE_LINE_CHAIN::Slice( int aStartIndex, int aEndIndex ) const
|
2013-09-10 11:43:09 +00:00
|
|
|
{
|
2013-10-14 11:43:57 +00:00
|
|
|
SHAPE_LINE_CHAIN rv;
|
|
|
|
|
|
|
|
if( aEndIndex < 0 )
|
|
|
|
aEndIndex += PointCount();
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
if( aStartIndex < 0 )
|
|
|
|
aStartIndex += PointCount();
|
2013-09-27 14:23:43 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
for( int i = aStartIndex; i <= aEndIndex; i++ )
|
|
|
|
rv.Append( m_points[i] );
|
2013-09-10 11:43:09 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
return rv;
|
2013-09-10 11:43:09 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 14:23:43 +00:00
|
|
|
|
|
|
|
struct compareOriginDistance
|
|
|
|
{
|
2013-10-14 14:13:35 +00:00
|
|
|
compareOriginDistance( VECTOR2I& aOrigin ) :
|
2013-10-14 11:43:57 +00:00
|
|
|
m_origin( aOrigin ) {};
|
2013-09-10 11:43:09 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
bool operator()( const SHAPE_LINE_CHAIN::INTERSECTION& aA,
|
|
|
|
const SHAPE_LINE_CHAIN::INTERSECTION& aB )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
|
|
|
return ( m_origin - aA.p ).EuclideanNorm() < ( m_origin - aB.p ).EuclideanNorm();
|
|
|
|
}
|
2013-09-10 11:43:09 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
VECTOR2I m_origin;
|
2013-09-10 11:43:09 +00:00
|
|
|
};
|
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
int SHAPE_LINE_CHAIN::Intersect( const SEG& aSeg, INTERSECTIONS& aIp ) const
|
2013-09-10 11:43:09 +00:00
|
|
|
{
|
2013-10-14 11:43:57 +00:00
|
|
|
for( int s = 0; s < SegmentCount(); s++ )
|
|
|
|
{
|
|
|
|
OPT_VECTOR2I p = CSegment( s ).Intersect( aSeg );
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
if( p )
|
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
INTERSECTION is;
|
2013-10-14 11:43:57 +00:00
|
|
|
is.our = CSegment( s );
|
|
|
|
is.their = aSeg;
|
|
|
|
is.p = *p;
|
|
|
|
aIp.push_back( is );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
compareOriginDistance comp( aSeg.A );
|
2013-10-14 11:43:57 +00:00
|
|
|
sort( aIp.begin(), aIp.end(), comp );
|
|
|
|
|
|
|
|
return aIp.size();
|
2013-09-27 14:23:43 +00:00
|
|
|
}
|
2013-09-10 11:43:09 +00:00
|
|
|
|
2013-09-27 14:23:43 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
int SHAPE_LINE_CHAIN::Intersect( const SHAPE_LINE_CHAIN& aChain, INTERSECTIONS& aIp ) const
|
2013-09-10 11:43:09 +00:00
|
|
|
{
|
2013-09-27 14:23:43 +00:00
|
|
|
BOX2I bb_other = aChain.BBox();
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
for( int s1 = 0; s1 < SegmentCount(); s1++ )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
|
|
|
const SEG& a = CSegment( s1 );
|
2013-10-14 18:40:36 +00:00
|
|
|
const BOX2I bb_cur( a.A, a.B - a.A );
|
2013-10-14 11:43:57 +00:00
|
|
|
|
|
|
|
if( !bb_other.Intersects( bb_cur ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for( int s2 = 0; s2 < aChain.SegmentCount(); s2++ )
|
|
|
|
{
|
|
|
|
const SEG& b = aChain.CSegment( s2 );
|
2013-10-14 18:40:36 +00:00
|
|
|
INTERSECTION is;
|
2013-10-14 11:43:57 +00:00
|
|
|
|
|
|
|
if( a.Collinear( b ) )
|
|
|
|
{
|
2014-05-14 09:45:01 +00:00
|
|
|
is.our = a;
|
|
|
|
is.their = b;
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
if( a.Contains( b.A ) ) { is.p = b.A; aIp.push_back( is ); }
|
|
|
|
if( a.Contains( b.B ) ) { is.p = b.B; aIp.push_back( is ); }
|
|
|
|
if( b.Contains( a.A ) ) { is.p = a.A; aIp.push_back( is ); }
|
|
|
|
if( b.Contains( a.B ) ) { is.p = a.B; aIp.push_back( is ); }
|
2013-10-14 11:43:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
OPT_VECTOR2I p = a.Intersect( b );
|
|
|
|
|
|
|
|
if( p )
|
|
|
|
{
|
|
|
|
is.p = *p;
|
|
|
|
is.our = a;
|
|
|
|
is.their = b;
|
|
|
|
aIp.push_back( is );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return aIp.size();
|
2013-09-10 11:43:09 +00:00
|
|
|
}
|
2013-09-27 14:23:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
int SHAPE_LINE_CHAIN::PathLength( const VECTOR2I& aP ) const
|
2013-09-10 11:43:09 +00:00
|
|
|
{
|
2013-10-14 11:43:57 +00:00
|
|
|
int sum = 0;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
for( int i = 0; i < SegmentCount(); i++ )
|
|
|
|
{
|
|
|
|
const SEG seg = CSegment( i );
|
|
|
|
int d = seg.Distance( aP );
|
|
|
|
|
|
|
|
if( d <= 1 )
|
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
sum += ( aP - seg.A ).EuclideanNorm();
|
2013-10-14 11:43:57 +00:00
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sum += seg.Length();
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2013-09-10 11:43:09 +00:00
|
|
|
}
|
2013-09-27 14:23:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
bool SHAPE_LINE_CHAIN::PointInside( const VECTOR2I& aP ) const
|
2013-09-10 11:43:09 +00:00
|
|
|
{
|
2018-05-10 15:26:40 +00:00
|
|
|
if( !m_closed || PointCount() < 3 || !BBox().Contains( aP ) )
|
2013-10-14 11:43:57 +00:00
|
|
|
return false;
|
2013-09-10 11:43:09 +00:00
|
|
|
|
2018-05-03 17:52:42 +00:00
|
|
|
bool inside = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To check for interior points, we draw a line in the positive x direction from
|
|
|
|
* the point. If it intersects an even number of segments, the point is outside the
|
|
|
|
* line chain (it had to first enter and then exit). Otherwise, it is inside the chain.
|
|
|
|
*
|
|
|
|
* Note: slope might be denormal here in the case of a horizontal line but we require our
|
|
|
|
* y to move from above to below the point (or vice versa)
|
|
|
|
*/
|
2018-05-10 15:26:40 +00:00
|
|
|
for( int i = 0; i < PointCount(); i++ )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
2018-05-10 15:26:40 +00:00
|
|
|
const VECTOR2D p1 = CPoint( i );
|
|
|
|
const VECTOR2D p2 = CPoint( i + 1 ); // CPoint wraps, so ignore counts
|
|
|
|
const VECTOR2D diff = p2 - p1;
|
|
|
|
|
|
|
|
if( ( ( p1.y > aP.y ) != ( p2.y > aP.y ) ) &&
|
|
|
|
( aP.x - p1.x < ( diff.x / diff.y ) * ( aP.y - p1.y ) ) )
|
2018-05-03 17:52:42 +00:00
|
|
|
inside = !inside;
|
2013-09-27 14:23:43 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 17:52:42 +00:00
|
|
|
return inside;
|
2013-09-10 11:43:09 +00:00
|
|
|
}
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-09-27 14:23:43 +00:00
|
|
|
|
|
|
|
bool SHAPE_LINE_CHAIN::PointOnEdge( const VECTOR2I& aP ) const
|
2018-05-08 20:03:52 +00:00
|
|
|
{
|
2018-05-10 15:26:40 +00:00
|
|
|
if( !PointCount() )
|
|
|
|
return false;
|
|
|
|
else if( PointCount() == 1 )
|
|
|
|
return m_points[0] == aP;
|
|
|
|
|
|
|
|
for( int i = 0; i < PointCount(); i++ )
|
|
|
|
{
|
|
|
|
const VECTOR2I& p1 = CPoint( i );
|
|
|
|
const VECTOR2I& p2 = CPoint( i + 1 );
|
|
|
|
|
|
|
|
if( aP == p1 )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if( p1.x == p2.x && p1.x == aP.x && ( p1.y > aP.y ) != ( p2.y > aP.y ) )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const VECTOR2D diff = p2 - p1;
|
|
|
|
if( aP.x >= p1.x && aP.x <= p2.x )
|
|
|
|
{
|
2018-08-27 13:24:08 +00:00
|
|
|
if( round_nearest( p1.y + ( diff.y / diff.x ) * ( aP.x - p1.x ) ) == aP.y )
|
2018-05-10 15:26:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2018-05-08 20:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SHAPE_LINE_CHAIN::CheckClearance( const VECTOR2I& aP, const int aDist) const
|
2013-09-10 11:43:09 +00:00
|
|
|
{
|
2018-05-12 08:44:31 +00:00
|
|
|
if( !PointCount() )
|
|
|
|
return false;
|
2014-05-14 09:45:01 +00:00
|
|
|
|
2018-05-12 08:44:31 +00:00
|
|
|
else if( PointCount() == 1 )
|
2013-10-14 11:43:57 +00:00
|
|
|
return m_points[0] == aP;
|
2013-09-27 14:23:43 +00:00
|
|
|
|
2014-05-14 09:45:01 +00:00
|
|
|
for( int i = 0; i < SegmentCount(); i++ )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
|
|
|
const SEG s = CSegment( i );
|
2013-09-10 11:43:09 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
if( s.A == aP || s.B == aP )
|
2013-10-14 11:43:57 +00:00
|
|
|
return true;
|
2013-09-27 14:23:43 +00:00
|
|
|
|
2018-05-08 20:03:52 +00:00
|
|
|
if( s.Distance( aP ) <= aDist )
|
2013-10-14 11:43:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2013-09-10 11:43:09 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 14:23:43 +00:00
|
|
|
|
2017-11-01 11:14:16 +00:00
|
|
|
const OPT<SHAPE_LINE_CHAIN::INTERSECTION> SHAPE_LINE_CHAIN::SelfIntersecting() const
|
2013-09-10 11:43:09 +00:00
|
|
|
{
|
2013-10-14 11:43:57 +00:00
|
|
|
for( int s1 = 0; s1 < SegmentCount(); s1++ )
|
|
|
|
{
|
|
|
|
for( int s2 = s1 + 1; s2 < SegmentCount(); s2++ )
|
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
const VECTOR2I s2a = CSegment( s2 ).A, s2b = CSegment( s2 ).B;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
if( s1 + 1 != s2 && CSegment( s1 ).Contains( s2a ) )
|
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
INTERSECTION is;
|
2013-10-14 11:43:57 +00:00
|
|
|
is.our = CSegment( s1 );
|
|
|
|
is.their = CSegment( s2 );
|
|
|
|
is.p = s2a;
|
|
|
|
return is;
|
|
|
|
}
|
2016-04-06 18:15:49 +00:00
|
|
|
else if( CSegment( s1 ).Contains( s2b ) &&
|
|
|
|
// for closed polylines, the ending point of the
|
|
|
|
// last segment == starting point of the first segment
|
|
|
|
// this is a normal case, not self intersecting case
|
|
|
|
!( IsClosed() && s1 == 0 && s2 == SegmentCount()-1 ) )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
INTERSECTION is;
|
2013-10-14 11:43:57 +00:00
|
|
|
is.our = CSegment( s1 );
|
|
|
|
is.their = CSegment( s2 );
|
|
|
|
is.p = s2b;
|
|
|
|
return is;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
OPT_VECTOR2I p = CSegment( s1 ).Intersect( CSegment( s2 ), true );
|
|
|
|
|
|
|
|
if( p )
|
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
INTERSECTION is;
|
2013-10-14 11:43:57 +00:00
|
|
|
is.our = CSegment( s1 );
|
|
|
|
is.their = CSegment( s2 );
|
|
|
|
is.p = *p;
|
|
|
|
return is;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-01 11:14:16 +00:00
|
|
|
return OPT<SHAPE_LINE_CHAIN::INTERSECTION>();
|
2013-09-10 11:43:09 +00:00
|
|
|
}
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-09-10 11:43:09 +00:00
|
|
|
|
|
|
|
SHAPE_LINE_CHAIN& SHAPE_LINE_CHAIN::Simplify()
|
|
|
|
{
|
2013-12-13 16:27:30 +00:00
|
|
|
std::vector<VECTOR2I> pts_unique;
|
2013-10-14 11:43:57 +00:00
|
|
|
|
|
|
|
if( PointCount() < 2 )
|
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
else if( PointCount() == 2 )
|
|
|
|
{
|
|
|
|
if( m_points[0] == m_points[1] )
|
2015-02-20 09:37:25 +00:00
|
|
|
m_points.pop_back();
|
2013-10-14 11:43:57 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
int np = PointCount();
|
|
|
|
|
|
|
|
// stage 1: eliminate duplicate vertices
|
2013-10-14 14:13:35 +00:00
|
|
|
while( i < np )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
|
|
|
int j = i + 1;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
|
|
|
while( j < np && CPoint( i ) == CPoint( j ) )
|
2013-10-14 11:43:57 +00:00
|
|
|
j++;
|
|
|
|
|
|
|
|
pts_unique.push_back( CPoint( i ) );
|
|
|
|
i = j;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_points.clear();
|
|
|
|
np = pts_unique.size();
|
|
|
|
|
|
|
|
i = 0;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
// stage 1: eliminate collinear segments
|
|
|
|
while( i < np - 2 )
|
|
|
|
{
|
|
|
|
const VECTOR2I p0 = pts_unique[i];
|
2013-10-14 14:13:35 +00:00
|
|
|
const VECTOR2I p1 = pts_unique[i + 1];
|
2013-10-14 11:43:57 +00:00
|
|
|
int n = i;
|
|
|
|
|
|
|
|
while( n < np - 2 && SEG( p0, p1 ).LineDistance( pts_unique[n + 2] ) <= 1 )
|
|
|
|
n++;
|
|
|
|
|
|
|
|
m_points.push_back( p0 );
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
if( n > i )
|
|
|
|
i = n;
|
|
|
|
|
|
|
|
if( n == np )
|
|
|
|
{
|
|
|
|
m_points.push_back( pts_unique[n - 1] );
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( np > 1 )
|
|
|
|
m_points.push_back( pts_unique[np - 2] );
|
|
|
|
|
|
|
|
m_points.push_back( pts_unique[np - 1] );
|
|
|
|
|
|
|
|
return *this;
|
2013-09-10 11:43:09 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 14:23:43 +00:00
|
|
|
|
|
|
|
const VECTOR2I SHAPE_LINE_CHAIN::NearestPoint( const VECTOR2I& aP ) const
|
2013-09-10 11:43:09 +00:00
|
|
|
{
|
2013-10-14 11:43:57 +00:00
|
|
|
int min_d = INT_MAX;
|
|
|
|
int nearest = 0;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
|
|
|
for( int i = 0; i < SegmentCount(); i++ )
|
2013-10-14 11:43:57 +00:00
|
|
|
{
|
|
|
|
int d = CSegment( i ).Distance( aP );
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
if( d < min_d )
|
|
|
|
{
|
|
|
|
min_d = d;
|
|
|
|
nearest = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CSegment( nearest ).NearestPoint( aP );
|
2013-09-10 11:43:09 +00:00
|
|
|
}
|
2013-09-27 14:23:43 +00:00
|
|
|
|
|
|
|
|
2015-07-02 14:09:43 +00:00
|
|
|
const VECTOR2I SHAPE_LINE_CHAIN::NearestPoint( const SEG& aSeg, int& dist ) const
|
|
|
|
{
|
|
|
|
int nearest = 0;
|
|
|
|
|
|
|
|
dist = INT_MAX;
|
|
|
|
for( int i = 0; i < PointCount(); i++ )
|
|
|
|
{
|
|
|
|
int d = aSeg.LineDistance( CPoint( i ) );
|
|
|
|
|
|
|
|
if( d < dist )
|
|
|
|
{
|
|
|
|
dist = d;
|
|
|
|
nearest = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CPoint( nearest );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-13 16:27:30 +00:00
|
|
|
const std::string SHAPE_LINE_CHAIN::Format() const
|
2013-09-10 11:43:09 +00:00
|
|
|
{
|
2013-12-13 16:27:30 +00:00
|
|
|
std::stringstream ss;
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
ss << m_points.size() << " " << ( m_closed ? 1 : 0 ) << " ";
|
2013-09-10 11:43:09 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
for( int i = 0; i < PointCount(); i++ )
|
2013-10-14 14:13:35 +00:00
|
|
|
ss << m_points[i].x << " " << m_points[i].y << " "; // Format() << " ";
|
2013-09-10 11:43:09 +00:00
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
return ss.str();
|
2013-09-10 11:43:09 +00:00
|
|
|
}
|
2014-05-14 09:45:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
bool SHAPE_LINE_CHAIN::CompareGeometry ( const SHAPE_LINE_CHAIN & aOther ) const
|
|
|
|
{
|
2014-11-14 19:19:00 +00:00
|
|
|
SHAPE_LINE_CHAIN a(*this), b( aOther );
|
2014-05-14 09:45:01 +00:00
|
|
|
a.Simplify();
|
|
|
|
b.Simplify();
|
|
|
|
|
2014-11-14 19:19:00 +00:00
|
|
|
if( a.m_points.size() != b.m_points.size() )
|
2014-05-14 09:45:01 +00:00
|
|
|
return false;
|
|
|
|
|
2014-11-14 19:19:00 +00:00
|
|
|
for( int i = 0; i < a.PointCount(); i++)
|
|
|
|
if( a.CPoint( i ) != b.CPoint( i ) )
|
2014-05-14 09:45:01 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-14 19:19:00 +00:00
|
|
|
|
2014-05-14 09:45:01 +00:00
|
|
|
bool SHAPE_LINE_CHAIN::Intersects( const SHAPE_LINE_CHAIN& aChain ) const
|
|
|
|
{
|
|
|
|
INTERSECTIONS dummy;
|
2014-11-14 19:19:00 +00:00
|
|
|
return Intersect( aChain, dummy ) != 0;
|
2014-05-14 09:45:01 +00:00
|
|
|
}
|
|
|
|
|
2014-11-14 19:19:00 +00:00
|
|
|
|
2014-05-14 09:45:01 +00:00
|
|
|
SHAPE* SHAPE_LINE_CHAIN::Clone() const
|
|
|
|
{
|
|
|
|
return new SHAPE_LINE_CHAIN( *this );
|
|
|
|
}
|
2015-08-03 18:29:44 +00:00
|
|
|
|
|
|
|
bool SHAPE_LINE_CHAIN::Parse( std::stringstream& aStream )
|
|
|
|
{
|
|
|
|
int n_pts;
|
|
|
|
|
|
|
|
m_points.clear();
|
|
|
|
aStream >> n_pts;
|
2016-05-12 03:28:08 +00:00
|
|
|
|
|
|
|
// Rough sanity check, just make sure the loop bounds aren't absolutely outlandish
|
2016-05-22 17:39:20 +00:00
|
|
|
if( n_pts < 0 || n_pts > int( aStream.str().size() ) )
|
2016-05-12 03:28:08 +00:00
|
|
|
return false;
|
|
|
|
|
2015-08-03 18:29:44 +00:00
|
|
|
aStream >> m_closed;
|
|
|
|
|
|
|
|
for( int i = 0; i < n_pts; i++ )
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
aStream >> x;
|
|
|
|
aStream >> y;
|
|
|
|
m_points.push_back( VECTOR2I( x, y ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2016-05-12 03:28:08 +00:00
|
|
|
}
|
2017-01-19 12:55:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
const VECTOR2I SHAPE_LINE_CHAIN::PointAlong( int aPathLength ) const
|
|
|
|
{
|
|
|
|
int total = 0;
|
|
|
|
|
|
|
|
if( aPathLength == 0 )
|
|
|
|
return CPoint( 0 );
|
|
|
|
|
|
|
|
for( int i = 0; i < SegmentCount(); i++ )
|
|
|
|
{
|
|
|
|
const SEG& s = CSegment( i );
|
|
|
|
int l = s.Length();
|
|
|
|
|
|
|
|
if( total + l >= aPathLength )
|
|
|
|
{
|
|
|
|
VECTOR2I d( s.B - s.A );
|
|
|
|
return s.A + d.Resize( aPathLength - total );
|
|
|
|
}
|
|
|
|
|
|
|
|
total += l;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CPoint( -1 );
|
|
|
|
}
|
2017-10-19 21:14:36 +00:00
|
|
|
|
|
|
|
double SHAPE_LINE_CHAIN::Area() const
|
|
|
|
{
|
2017-12-31 16:46:38 +00:00
|
|
|
// see https://www.mathopenref.com/coordpolygonarea2.html
|
|
|
|
|
2017-10-19 21:14:36 +00:00
|
|
|
if( !m_closed )
|
|
|
|
return 0.0;
|
|
|
|
|
|
|
|
double area = 0.0;
|
|
|
|
int size = m_points.size();
|
|
|
|
|
|
|
|
for( int i = 0, j = size - 1; i < size; ++i )
|
|
|
|
{
|
|
|
|
area += ( (double) m_points[j].x + m_points[i].x ) * ( (double) m_points[j].y - m_points[i].y );
|
|
|
|
j = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -area * 0.5;
|
|
|
|
}
|