2015-06-12 15:12:02 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 CERN
|
|
|
|
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
|
|
|
*
|
2015-07-27 19:45:57 +00:00
|
|
|
* Point in polygon algorithm adapted from Clipper Library (C) Angus Johnson,
|
|
|
|
* subject to Clipper library license.
|
|
|
|
*
|
2015-06-12 15:12:02 +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 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <set>
|
|
|
|
#include <list>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
#include <geometry/shape.h>
|
|
|
|
#include <geometry/shape_line_chain.h>
|
|
|
|
#include <geometry/shape_poly_set.h>
|
2015-06-12 15:12:02 +00:00
|
|
|
|
|
|
|
using namespace ClipperLib;
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
SHAPE_POLY_SET::SHAPE_POLY_SET() :
|
|
|
|
SHAPE( SH_POLY_SET )
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SHAPE_POLY_SET::~SHAPE_POLY_SET()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
int SHAPE_POLY_SET::NewOutline()
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
2015-07-27 19:45:57 +00:00
|
|
|
SHAPE_LINE_CHAIN empty_path;
|
|
|
|
POLYGON poly;
|
2015-07-06 13:15:48 +00:00
|
|
|
poly.push_back( empty_path );
|
|
|
|
m_polys.push_back( poly );
|
2015-06-12 15:12:02 +00:00
|
|
|
return m_polys.size() - 1;
|
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
|
2015-06-12 15:12:02 +00:00
|
|
|
int SHAPE_POLY_SET::NewHole( int aOutline )
|
|
|
|
{
|
2015-07-27 19:45:57 +00:00
|
|
|
m_polys.back().push_back( SHAPE_LINE_CHAIN() );
|
|
|
|
|
|
|
|
return m_polys.back().size() - 2;
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
int SHAPE_POLY_SET::Append( int x, int y, int aOutline, int aHole )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
2015-07-06 13:15:48 +00:00
|
|
|
if( aOutline < 0 )
|
2015-06-12 15:12:02 +00:00
|
|
|
aOutline += m_polys.size();
|
|
|
|
|
|
|
|
int idx;
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
if( aHole < 0 )
|
2015-06-12 15:12:02 +00:00
|
|
|
idx = 0;
|
|
|
|
else
|
|
|
|
idx = aHole + 1;
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
assert( aOutline < (int)m_polys.size() );
|
|
|
|
assert( idx < (int)m_polys[aOutline].size() );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
m_polys[aOutline][idx].Append( x, y );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
return m_polys[aOutline][idx].PointCount();
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
int SHAPE_POLY_SET::VertexCount( int aOutline , int aHole ) const
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
2015-07-06 13:15:48 +00:00
|
|
|
if( aOutline < 0 )
|
2015-06-12 15:12:02 +00:00
|
|
|
aOutline += m_polys.size();
|
|
|
|
|
|
|
|
int idx;
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
if( aHole < 0 )
|
2015-06-12 15:12:02 +00:00
|
|
|
idx = 0;
|
|
|
|
else
|
|
|
|
idx = aHole + 1;
|
|
|
|
|
|
|
|
assert ( aOutline < (int)m_polys.size() );
|
|
|
|
assert ( idx < (int)m_polys[aOutline].size() );
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
return m_polys[aOutline][idx].PointCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const VECTOR2I& SHAPE_POLY_SET::CVertex( int index, int aOutline , int aHole ) const
|
|
|
|
{
|
|
|
|
if( aOutline < 0 )
|
|
|
|
aOutline += m_polys.size();
|
|
|
|
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
if( aHole < 0 )
|
|
|
|
idx = 0;
|
|
|
|
else
|
|
|
|
idx = aHole + 1;
|
|
|
|
|
|
|
|
assert( aOutline < (int)m_polys.size() );
|
|
|
|
assert( idx < (int)m_polys[aOutline].size() );
|
|
|
|
|
|
|
|
return m_polys[aOutline][idx].CPoint( index );
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
VECTOR2I& SHAPE_POLY_SET::Vertex( int index, int aOutline , int aHole )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
2015-07-06 13:15:48 +00:00
|
|
|
if( aOutline < 0 )
|
2015-06-12 15:12:02 +00:00
|
|
|
aOutline += m_polys.size();
|
|
|
|
|
|
|
|
int idx;
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
if( aHole < 0 )
|
2015-06-12 15:12:02 +00:00
|
|
|
idx = 0;
|
|
|
|
else
|
|
|
|
idx = aHole + 1;
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
assert( aOutline < (int)m_polys.size() );
|
|
|
|
assert( idx < (int)m_polys[aOutline].size() );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
return m_polys[aOutline][idx].Point( index );
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
|
2015-06-12 15:12:02 +00:00
|
|
|
int SHAPE_POLY_SET::AddOutline( const SHAPE_LINE_CHAIN& aOutline )
|
|
|
|
{
|
2015-07-06 13:15:48 +00:00
|
|
|
assert( aOutline.IsClosed() );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
POLYGON poly;
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
poly.push_back( aOutline );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
|
|
|
m_polys.push_back( poly );
|
|
|
|
|
|
|
|
return m_polys.size() - 1;
|
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
|
|
|
|
int SHAPE_POLY_SET::AddHole( const SHAPE_LINE_CHAIN& aHole, int aOutline )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
|
|
|
assert ( m_polys.size() );
|
2015-07-06 13:15:48 +00:00
|
|
|
|
|
|
|
if( aOutline < 0 )
|
2015-06-12 15:12:02 +00:00
|
|
|
aOutline += m_polys.size();
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
POLYGON& poly = m_polys[aOutline];
|
2015-07-06 13:15:48 +00:00
|
|
|
|
|
|
|
assert( poly.size() );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
poly.push_back( aHole );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
|
|
|
return poly.size() - 1;
|
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
const Path SHAPE_POLY_SET::convertToClipper( const SHAPE_LINE_CHAIN& aPath, bool aRequiredOrientation )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
|
|
|
Path c_path;
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
for( int i = 0; i < aPath.PointCount(); i++ )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
2015-07-06 13:15:48 +00:00
|
|
|
const VECTOR2I& vertex = aPath.CPoint( i );
|
2015-07-27 19:45:57 +00:00
|
|
|
c_path.push_back( IntPoint( vertex.x, vertex.y ) );
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
if( Orientation( c_path ) != aRequiredOrientation )
|
|
|
|
ReversePath( c_path );
|
|
|
|
|
2015-06-12 15:12:02 +00:00
|
|
|
return c_path;
|
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
const SHAPE_LINE_CHAIN SHAPE_POLY_SET::convertFromClipper( const Path& aPath )
|
|
|
|
{
|
|
|
|
SHAPE_LINE_CHAIN lc;
|
|
|
|
|
|
|
|
for( unsigned int i = 0; i < aPath.size(); i++ )
|
|
|
|
lc.Append( aPath[i].X, aPath[i].Y );
|
|
|
|
|
|
|
|
return lc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SHAPE_POLY_SET::booleanOp( ClipType type, const SHAPE_POLY_SET& b )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
|
|
|
Clipper c;
|
|
|
|
|
|
|
|
c.StrictlySimple( true );
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
BOOST_FOREACH( const POLYGON& poly, m_polys )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
2015-07-27 19:45:57 +00:00
|
|
|
for( unsigned int i = 0; i < poly.size(); i++ )
|
|
|
|
c.AddPath( convertToClipper( poly[i], i > 0 ? false : true ), ptSubject, true );
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
BOOST_FOREACH( const POLYGON& poly, b.m_polys )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
2015-07-27 19:45:57 +00:00
|
|
|
for( unsigned int i = 0; i < poly.size(); i++ )
|
|
|
|
c.AddPath( convertToClipper( poly[i], i > 0 ? false : true ), ptClip, true );
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PolyTree solution;
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
c.Execute( type, solution, pftNonZero, pftNonZero );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
importTree( &solution );
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
void SHAPE_POLY_SET::BooleanAdd( const SHAPE_POLY_SET& b )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
|
|
|
booleanOp( ctUnion, b );
|
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
void SHAPE_POLY_SET::BooleanSubtract( const SHAPE_POLY_SET& b )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
|
|
|
booleanOp( ctDifference, b );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
void SHAPE_POLY_SET::Inflate( int aFactor, int aCircleSegmentsCount )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
|
|
|
ClipperOffset c;
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
BOOST_FOREACH( const POLYGON& poly, m_polys )
|
|
|
|
{
|
|
|
|
for( unsigned int i = 0; i < poly.size(); i++ )
|
|
|
|
c.AddPath( convertToClipper( poly[i], i > 0 ? false : true ), jtRound, etClosedPolygon );
|
|
|
|
}
|
2015-06-12 15:12:02 +00:00
|
|
|
|
|
|
|
PolyTree solution;
|
|
|
|
|
2015-07-31 12:15:42 +00:00
|
|
|
c.ArcTolerance = fabs( (double) aFactor ) / M_PI / aCircleSegmentsCount;
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
c.Execute( solution, aFactor );
|
2015-07-14 20:23:13 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
importTree( &solution );
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
void SHAPE_POLY_SET::importTree( PolyTree* tree)
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
|
|
|
m_polys.clear();
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
for( PolyNode* n = tree->GetFirst(); n; n = n->GetNext() )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
|
|
|
if( !n->IsHole() )
|
|
|
|
{
|
2015-07-27 19:45:57 +00:00
|
|
|
POLYGON paths;
|
|
|
|
paths.push_back( convertFromClipper( n->Contour ) );
|
2015-07-06 13:15:48 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
for( unsigned int i = 0; i < n->Childs.size(); i++ )
|
|
|
|
paths.push_back( convertFromClipper( n->Childs[i]->Contour ) );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
m_polys.push_back(paths);
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-14 20:23:13 +00:00
|
|
|
// Polygon fracturing code. Work in progress.
|
2015-07-27 19:45:57 +00:00
|
|
|
|
2015-06-12 15:12:02 +00:00
|
|
|
struct FractureEdge
|
|
|
|
{
|
2015-07-27 19:45:57 +00:00
|
|
|
FractureEdge( bool connected, SHAPE_LINE_CHAIN* owner, int index ) :
|
2015-07-06 13:15:48 +00:00
|
|
|
m_connected( connected ),
|
|
|
|
m_next( NULL )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
2015-07-27 19:45:57 +00:00
|
|
|
m_p1 = owner->CPoint( index );
|
|
|
|
m_p2 = owner->CPoint( index + 1 );
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
FractureEdge( int y = 0 ) :
|
2015-07-06 13:15:48 +00:00
|
|
|
m_connected( false ),
|
|
|
|
m_next( NULL )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
2015-07-27 19:45:57 +00:00
|
|
|
m_p1.x = m_p2.y = y;
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
FractureEdge( bool connected, const VECTOR2I& p1, const VECTOR2I& p2 ) :
|
2015-07-06 13:15:48 +00:00
|
|
|
m_connected( connected ),
|
|
|
|
m_p1( p1 ),
|
|
|
|
m_p2( p2 ),
|
|
|
|
m_next( NULL )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
bool matches( int y ) const
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
2015-07-27 19:45:57 +00:00
|
|
|
int y_min = std::min( m_p1.y, m_p2.y );
|
|
|
|
int y_max = std::max( m_p1.y, m_p2.y );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
return ( y >= y_min ) && ( y <= y_max );
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool m_connected;
|
2015-07-27 19:45:57 +00:00
|
|
|
VECTOR2I m_p1, m_p2;
|
2015-07-06 13:15:48 +00:00
|
|
|
FractureEdge* m_next;
|
2015-06-12 15:12:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-06-22 07:09:44 +00:00
|
|
|
typedef std::vector<FractureEdge*> FractureEdgeSet;
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
static int processEdge( FractureEdgeSet& edges, FractureEdge* edge )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
2015-07-27 19:45:57 +00:00
|
|
|
int x = edge->m_p1.x;
|
|
|
|
int y = edge->m_p1.y;
|
|
|
|
int min_dist = std::numeric_limits<int>::max();
|
|
|
|
int x_nearest = 0;
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
FractureEdge* e_nearest = NULL;
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
for( FractureEdgeSet::iterator i = edges.begin(); i != edges.end(); ++i )
|
|
|
|
{
|
|
|
|
if( !(*i)->matches( y ) )
|
|
|
|
continue;
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
int x_intersect;
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
if( (*i)->m_p1.y == (*i)->m_p2.y ) // horizontal edge
|
|
|
|
x_intersect = std::max ( (*i)->m_p1.x, (*i)->m_p2.x );
|
2015-07-06 13:15:48 +00:00
|
|
|
else
|
2015-07-27 19:45:57 +00:00
|
|
|
x_intersect = (*i)->m_p1.x + rescale((*i)->m_p2.x - (*i)->m_p1.x, y - (*i)->m_p1.y, (*i)->m_p2.y - (*i)->m_p1.y );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
int dist = ( x - x_intersect );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-30 11:49:22 +00:00
|
|
|
if( dist >= 0 && dist < min_dist && (*i)->m_connected )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
2015-07-06 13:15:48 +00:00
|
|
|
min_dist = dist;
|
|
|
|
x_nearest = x_intersect;
|
|
|
|
e_nearest = (*i);
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
if( e_nearest && e_nearest->m_connected )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
2015-07-06 13:15:48 +00:00
|
|
|
int count = 0;
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-30 11:49:22 +00:00
|
|
|
FractureEdge* lead1 = new FractureEdge( true, VECTOR2I( x_nearest, y ), VECTOR2I( x, y ) );
|
2015-07-27 19:45:57 +00:00
|
|
|
FractureEdge* lead2 = new FractureEdge( true, VECTOR2I( x, y ), VECTOR2I( x_nearest, y ) );
|
|
|
|
FractureEdge* split_2 = new FractureEdge( true, VECTOR2I( x_nearest, y ), e_nearest->m_p2 );
|
2015-06-22 07:09:44 +00:00
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
edges.push_back( split_2 );
|
|
|
|
edges.push_back( lead1 );
|
|
|
|
edges.push_back( lead2 );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
FractureEdge* link = e_nearest->m_next;
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
e_nearest->m_p2 = VECTOR2I( x_nearest, y );
|
2015-07-06 13:15:48 +00:00
|
|
|
e_nearest->m_next = lead1;
|
2015-06-12 15:12:02 +00:00
|
|
|
lead1->m_next = edge;
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
FractureEdge*last;
|
2015-07-06 13:15:48 +00:00
|
|
|
for( last = edge; last->m_next != edge; last = last->m_next )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
2015-07-06 13:15:48 +00:00
|
|
|
last->m_connected = true;
|
|
|
|
count++;
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
last->m_connected = true;
|
2015-06-12 15:12:02 +00:00
|
|
|
last->m_next = lead2;
|
|
|
|
lead2->m_next = split_2;
|
|
|
|
split_2->m_next = link;
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
return count + 1;
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
void SHAPE_POLY_SET::fractureSingle( POLYGON& paths )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
|
|
|
FractureEdgeSet edges;
|
2015-07-06 13:15:48 +00:00
|
|
|
FractureEdgeSet border_edges;
|
|
|
|
FractureEdge* root = NULL;
|
2015-06-12 15:12:02 +00:00
|
|
|
|
|
|
|
bool first = true;
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
if( paths.size() == 1 )
|
2015-06-12 15:12:02 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
int num_unconnected = 0;
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
BOOST_FOREACH( SHAPE_LINE_CHAIN& path, paths )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
FractureEdge *prev = NULL, *first_edge = NULL;
|
2015-07-06 13:15:48 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
int x_min = std::numeric_limits<int>::max();
|
2015-07-06 13:15:48 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
for( int i = 0; i < path.PointCount(); i++ )
|
2015-07-06 13:15:48 +00:00
|
|
|
{
|
2015-07-27 19:45:57 +00:00
|
|
|
const VECTOR2I& p = path.CPoint( i );
|
|
|
|
|
|
|
|
if( p.x < x_min )
|
|
|
|
x_min = p.x;
|
2015-07-06 13:15:48 +00:00
|
|
|
}
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
for( int i = 0; i < path.PointCount(); i++ )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
2015-07-06 13:15:48 +00:00
|
|
|
FractureEdge* fe = new FractureEdge( first, &path, index++ );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
if( !root )
|
2015-06-12 15:12:02 +00:00
|
|
|
root = fe;
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
if( !first_edge )
|
2015-06-12 15:12:02 +00:00
|
|
|
first_edge = fe;
|
2015-07-06 13:15:48 +00:00
|
|
|
|
|
|
|
if( prev )
|
2015-06-12 15:12:02 +00:00
|
|
|
prev->m_next = fe;
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
if( i == path.PointCount() - 1 )
|
2015-06-12 15:12:02 +00:00
|
|
|
fe->m_next = first_edge;
|
|
|
|
|
|
|
|
prev = fe;
|
2015-07-06 13:15:48 +00:00
|
|
|
edges.push_back( fe );
|
|
|
|
|
|
|
|
if( !first )
|
|
|
|
{
|
2015-07-27 19:45:57 +00:00
|
|
|
if( fe->m_p1.x == x_min )
|
2015-07-06 13:15:48 +00:00
|
|
|
border_edges.push_back( fe );
|
|
|
|
}
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
if( !fe->m_connected )
|
2015-06-12 15:12:02 +00:00
|
|
|
num_unconnected++;
|
|
|
|
}
|
|
|
|
first = false; // first path is always the outline
|
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
// keep connecting holes to the main outline, until there's no holes left...
|
|
|
|
while( num_unconnected > 0 )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
2015-07-27 19:45:57 +00:00
|
|
|
int x_min = std::numeric_limits<int>::max();
|
|
|
|
|
2015-07-07 16:37:03 +00:00
|
|
|
FractureEdge* smallestX = NULL;
|
2015-06-22 07:09:44 +00:00
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
// find the left-most hole edge and merge with the outline
|
|
|
|
for( FractureEdgeSet::iterator i = border_edges.begin(); i != border_edges.end(); ++i )
|
2015-06-22 07:09:44 +00:00
|
|
|
{
|
2015-07-27 19:45:57 +00:00
|
|
|
int xt = (*i)->m_p1.x;
|
2015-07-07 16:37:03 +00:00
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
if( ( xt < x_min ) && ! (*i)->m_connected )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
2015-07-06 13:15:48 +00:00
|
|
|
x_min = xt;
|
|
|
|
smallestX = *i;
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-06 13:15:48 +00:00
|
|
|
|
|
|
|
num_unconnected -= processEdge( edges, smallestX );
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
paths.clear();
|
2015-07-27 19:45:57 +00:00
|
|
|
SHAPE_LINE_CHAIN newPath;
|
|
|
|
|
|
|
|
newPath.SetClosed( true );
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
FractureEdge* e;
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
for( e = root; e->m_next != root; e = e->m_next )
|
2015-07-27 19:45:57 +00:00
|
|
|
newPath.Append( e->m_p1 );
|
2015-06-19 17:39:33 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
newPath.Append( e->m_p1 );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
for( FractureEdgeSet::iterator i = edges.begin(); i != edges.end(); ++i )
|
2015-06-22 07:09:44 +00:00
|
|
|
delete *i;
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
paths.push_back( newPath );
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
|
|
|
|
void SHAPE_POLY_SET::Fracture()
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
2015-07-27 19:45:57 +00:00
|
|
|
Simplify(); // remove overlallping holes/degeneracy
|
|
|
|
|
|
|
|
BOOST_FOREACH( POLYGON& paths, m_polys )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
|
|
|
fractureSingle( paths );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
|
2015-06-12 15:12:02 +00:00
|
|
|
void SHAPE_POLY_SET::Simplify()
|
|
|
|
{
|
2015-07-27 19:45:57 +00:00
|
|
|
SHAPE_POLY_SET empty;
|
|
|
|
|
|
|
|
booleanOp( ctUnion, empty );
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
|
2015-06-12 15:12:02 +00:00
|
|
|
const std::string SHAPE_POLY_SET::Format() const
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
|
|
|
|
ss << "polyset " << m_polys.size() << "\n";
|
|
|
|
|
|
|
|
for( unsigned i = 0; i < m_polys.size(); i++ )
|
|
|
|
{
|
|
|
|
ss << "poly " << m_polys[i].size() << "\n";
|
|
|
|
for( unsigned j = 0; j < m_polys[i].size(); j++)
|
|
|
|
{
|
2015-07-27 19:45:57 +00:00
|
|
|
ss << m_polys[i][j].PointCount() << "\n";
|
|
|
|
for( int v = 0; v < m_polys[i][j].PointCount(); v++)
|
|
|
|
ss << m_polys[i][j].CPoint( v ).x << " " << m_polys[i][j].CPoint( v ).y << "\n";
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
ss << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
|
2015-06-12 15:12:02 +00:00
|
|
|
bool SHAPE_POLY_SET::Parse( std::stringstream& aStream )
|
|
|
|
{
|
|
|
|
std::string tmp;
|
|
|
|
|
|
|
|
aStream >> tmp;
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
if( tmp != "polyset" )
|
2015-06-12 15:12:02 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
aStream >> tmp;
|
|
|
|
|
2015-06-15 14:01:43 +00:00
|
|
|
int n_polys = atoi( tmp.c_str() );
|
|
|
|
|
|
|
|
if( n_polys < 0 )
|
2015-06-12 15:12:02 +00:00
|
|
|
return false;
|
|
|
|
|
2015-06-15 14:01:43 +00:00
|
|
|
for( int i = 0; i < n_polys; i++ )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
2015-07-27 19:45:57 +00:00
|
|
|
POLYGON paths;
|
|
|
|
|
2015-06-12 15:12:02 +00:00
|
|
|
aStream >> tmp;
|
2015-07-06 13:15:48 +00:00
|
|
|
|
|
|
|
if( tmp != "poly" )
|
2015-06-12 15:12:02 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
aStream >> tmp;
|
2015-06-15 14:01:43 +00:00
|
|
|
int n_outlines = atoi( tmp.c_str() );
|
|
|
|
|
|
|
|
if( n_outlines < 0 )
|
2015-06-12 15:12:02 +00:00
|
|
|
return false;
|
|
|
|
|
2015-06-15 14:01:43 +00:00
|
|
|
for( int j = 0; j < n_outlines; j++ )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
2015-07-27 19:45:57 +00:00
|
|
|
SHAPE_LINE_CHAIN outline;
|
|
|
|
|
|
|
|
outline.SetClosed( true );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
|
|
|
aStream >> tmp;
|
2015-06-15 14:01:43 +00:00
|
|
|
int n_vertices = atoi( tmp.c_str() );
|
|
|
|
for( int v = 0; v < n_vertices; v++ )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
2015-07-27 19:45:57 +00:00
|
|
|
VECTOR2I p;
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
aStream >> tmp; p.x = atoi( tmp.c_str() );
|
|
|
|
aStream >> tmp; p.y = atoi( tmp.c_str() );
|
|
|
|
outline.Append( p );
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
2015-07-27 19:45:57 +00:00
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
paths.push_back( outline );
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
2015-07-27 19:45:57 +00:00
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
m_polys.push_back( paths );
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
|
2015-06-12 15:12:02 +00:00
|
|
|
const BOX2I SHAPE_POLY_SET::BBox( int aClearance ) const
|
|
|
|
{
|
|
|
|
BOX2I bb;
|
|
|
|
|
|
|
|
for( unsigned i = 0; i < m_polys.size(); i++ )
|
|
|
|
{
|
2015-07-29 12:18:53 +00:00
|
|
|
if( i == 0 )
|
2015-07-27 19:45:57 +00:00
|
|
|
bb = m_polys[i][0].BBox();
|
|
|
|
else
|
|
|
|
bb.Merge( m_polys[i][0].BBox() );
|
|
|
|
}
|
|
|
|
|
|
|
|
bb.Inflate( aClearance );
|
|
|
|
return bb;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SHAPE_POLY_SET::RemoveAllContours()
|
|
|
|
{
|
|
|
|
m_polys.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SHAPE_POLY_SET::DeletePolygon( int aIdx )
|
|
|
|
{
|
|
|
|
m_polys.erase( m_polys.begin() + aIdx );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SHAPE_POLY_SET::Append( const SHAPE_POLY_SET& aSet )
|
|
|
|
{
|
|
|
|
m_polys.insert( m_polys.end(), aSet.m_polys.begin(), aSet.m_polys.end() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SHAPE_POLY_SET::Append( const VECTOR2I& aP, int aOutline, int aHole )
|
|
|
|
{
|
|
|
|
Append( aP.x, aP.y, aOutline, aHole );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SHAPE_POLY_SET::Contains( const VECTOR2I& aP, int aSubpolyIndex ) const
|
|
|
|
{
|
|
|
|
// fixme: support holes!
|
|
|
|
|
|
|
|
if( aSubpolyIndex >= 0 )
|
|
|
|
return pointInPolygon( aP, m_polys[aSubpolyIndex][0] );
|
|
|
|
|
|
|
|
BOOST_FOREACH ( const POLYGON& polys, m_polys )
|
|
|
|
{
|
|
|
|
if( pointInPolygon( aP, polys[0] ) )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SHAPE_POLY_SET::pointInPolygon( const VECTOR2I& aP, const SHAPE_LINE_CHAIN& aPath ) const
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
int cnt = aPath.PointCount();
|
|
|
|
|
|
|
|
if ( !aPath.BBox().Contains( aP ) ) // test with bounding box first
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( cnt < 3 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
VECTOR2I ip = aPath.CPoint( 0 );
|
|
|
|
|
|
|
|
for( int i = 1; i <= cnt; ++i )
|
|
|
|
{
|
|
|
|
VECTOR2I ipNext = ( i == cnt ? aPath.CPoint( 0 ) : aPath.CPoint( i ) );
|
|
|
|
|
|
|
|
if( ipNext.y == aP.y )
|
2015-07-14 11:36:24 +00:00
|
|
|
{
|
2015-07-27 19:45:57 +00:00
|
|
|
if( ( ipNext.x == aP.x ) || ( ip.y == aP.y &&
|
|
|
|
( ( ipNext.x > aP.x ) == ( ip.x < aP.x ) ) ) )
|
|
|
|
return true;
|
|
|
|
}
|
2015-07-14 11:36:24 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
if( ( ip.y < aP.y ) != ( ipNext.y < aP.y ) )
|
|
|
|
{
|
|
|
|
if( ip.x >= aP.x )
|
|
|
|
{
|
|
|
|
if( ipNext.x > aP.x )
|
|
|
|
result = 1 - result;
|
2015-07-14 20:23:13 +00:00
|
|
|
else
|
2015-07-27 19:45:57 +00:00
|
|
|
{
|
|
|
|
int64_t d = (int64_t)( ip.x - aP.x ) * (int64_t)( ipNext.y - aP.y ) -
|
|
|
|
(int64_t)( ipNext.x - aP.x ) * (int64_t)( ip.y - aP.y );
|
|
|
|
|
|
|
|
if( !d )
|
|
|
|
return true;
|
2015-07-14 11:36:24 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
if( ( d > 0 ) == ( ipNext.y > ip.y ) )
|
|
|
|
result = 1 - result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( ipNext.x > aP.x )
|
|
|
|
{
|
|
|
|
int64_t d = (int64_t)( ip.x - aP.x ) * (int64_t)( ipNext.y - aP.y ) -
|
|
|
|
(int64_t)( ipNext.x - aP.x ) * (int64_t)( ip.y - aP.y );
|
|
|
|
|
|
|
|
if( !d )
|
2015-08-03 08:45:30 +00:00
|
|
|
return true;
|
2015-07-27 19:45:57 +00:00
|
|
|
|
|
|
|
if( ( d > 0 ) == ( ipNext.y > ip.y ) )
|
|
|
|
result = 1 - result;
|
|
|
|
}
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-27 19:45:57 +00:00
|
|
|
|
|
|
|
ip = ipNext;
|
2015-07-14 11:36:24 +00:00
|
|
|
}
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
return result ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SHAPE_POLY_SET::Move( const VECTOR2I& aVector )
|
|
|
|
{
|
|
|
|
BOOST_FOREACH( POLYGON &poly, m_polys )
|
|
|
|
{
|
|
|
|
BOOST_FOREACH( SHAPE_LINE_CHAIN &path, poly )
|
|
|
|
{
|
|
|
|
path.Move( aVector );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int SHAPE_POLY_SET::TotalVertices() const
|
|
|
|
{
|
|
|
|
int c = 0;
|
|
|
|
|
|
|
|
BOOST_FOREACH( const POLYGON& poly, m_polys )
|
|
|
|
{
|
|
|
|
BOOST_FOREACH ( const SHAPE_LINE_CHAIN& path, poly )
|
|
|
|
{
|
|
|
|
c += path.PointCount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c;
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|