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>
|
|
|
|
*
|
|
|
|
* 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 <geometry/shape.h>
|
|
|
|
#include <geometry/shape_line_chain.h>
|
|
|
|
#include <set>
|
|
|
|
#include <list>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
|
|
|
#include "geometry/shape_poly_set.h"
|
|
|
|
|
|
|
|
using namespace ClipperLib;
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
int SHAPE_POLY_SET::NewOutline()
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
|
|
|
Path empty_path;
|
|
|
|
Paths 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-06 13:15:48 +00:00
|
|
|
assert( false );
|
2015-06-12 15:12:02 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
|
|
|
|
int SHAPE_POLY_SET::AppendVertex( 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
|
|
|
|
|
|
|
m_polys[aOutline][idx].push_back( IntPoint( x, y ) );
|
|
|
|
|
|
|
|
return m_polys[aOutline][idx].size();
|
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +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() );
|
|
|
|
|
|
|
|
return m_polys[aOutline][idx].size();
|
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
|
|
|
|
const VECTOR2I SHAPE_POLY_SET::GetVertex( int index, 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;
|
|
|
|
|
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
|
|
|
|
|
|
|
IntPoint p = m_polys[aOutline][idx][index];
|
|
|
|
return VECTOR2I (p.X, p.Y);
|
|
|
|
}
|
|
|
|
|
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-06 13:15:48 +00:00
|
|
|
Path p = convert( aOutline );
|
2015-06-12 15:12:02 +00:00
|
|
|
Paths poly;
|
|
|
|
|
|
|
|
if( !Orientation( p ) )
|
2015-07-06 13:15:48 +00:00
|
|
|
ReversePath( p ); // outlines are always CW
|
2015-06-12 15:12:02 +00:00
|
|
|
|
|
|
|
poly.push_back( p );
|
|
|
|
|
|
|
|
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-06 13:15:48 +00:00
|
|
|
Paths& poly = m_polys[aOutline];
|
|
|
|
|
|
|
|
assert( poly.size() );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
Path p = convert( aHole );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
|
|
|
if( Orientation( p ) )
|
2015-07-06 13:15:48 +00:00
|
|
|
ReversePath( p ); // holes are always CCW
|
2015-06-12 15:12:02 +00:00
|
|
|
|
|
|
|
poly.push_back( p );
|
|
|
|
|
|
|
|
return poly.size() - 1;
|
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
|
2015-06-12 15:12:02 +00:00
|
|
|
const ClipperLib::Path SHAPE_POLY_SET::convert( const SHAPE_LINE_CHAIN& aPath )
|
|
|
|
{
|
|
|
|
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 );
|
|
|
|
c_path.push_back( ClipperLib::IntPoint( vertex.x, vertex.y ) );
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c_path;
|
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
|
2015-06-12 15:12:02 +00:00
|
|
|
void SHAPE_POLY_SET::booleanOp( ClipperLib::ClipType type, const SHAPE_POLY_SET& b )
|
|
|
|
{
|
|
|
|
Clipper c;
|
|
|
|
|
|
|
|
c.StrictlySimple( true );
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
BOOST_FOREACH( Paths& subject, m_polys )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
2015-07-06 13:15:48 +00:00
|
|
|
c.AddPaths( subject, ptSubject, true );
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
BOOST_FOREACH( const Paths& clip, b.m_polys )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
2015-07-06 13:15:48 +00:00
|
|
|
c.AddPaths( clip, 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-06-12 15:12:02 +00:00
|
|
|
void SHAPE_POLY_SET::Add( const SHAPE_POLY_SET& b )
|
|
|
|
{
|
|
|
|
booleanOp( ctUnion, b );
|
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
|
2015-06-12 15:12:02 +00:00
|
|
|
void SHAPE_POLY_SET::Subtract( const SHAPE_POLY_SET& b )
|
|
|
|
{
|
|
|
|
booleanOp( ctDifference, b );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
void SHAPE_POLY_SET::Erode( int aFactor )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
|
|
|
ClipperOffset c;
|
|
|
|
|
|
|
|
BOOST_FOREACH( Paths& p, m_polys )
|
|
|
|
c.AddPaths(p, jtRound, etClosedPolygon );
|
|
|
|
|
|
|
|
PolyTree solution;
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
c.Execute( solution, aFactor );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
|
|
|
m_polys.clear();
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
for( PolyNode* n = solution.GetFirst(); n; n = n->GetNext() )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
|
|
|
Paths ps;
|
2015-07-06 13:15:48 +00:00
|
|
|
ps.push_back( n->Contour );
|
|
|
|
m_polys.push_back( ps );
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
|
|
|
|
void SHAPE_POLY_SET::importTree( ClipperLib::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() )
|
|
|
|
{
|
|
|
|
Paths paths;
|
2015-07-06 13:15:48 +00:00
|
|
|
paths.push_back( n->Contour );
|
|
|
|
|
|
|
|
for( unsigned i = 0; i < n->Childs.size(); i++ )
|
|
|
|
paths.push_back( n->Childs[i]->Contour );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
m_polys.push_back( paths );
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
// Polygon fracturing code. Work in progress.
|
2015-06-12 15:12:02 +00:00
|
|
|
struct FractureEdge
|
|
|
|
{
|
2015-07-06 13:15:48 +00:00
|
|
|
FractureEdge( bool connected, Path* owner, int index ) :
|
|
|
|
m_connected( connected ),
|
|
|
|
m_next( NULL )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
|
|
|
m_p1 = (*owner)[index];
|
|
|
|
m_p2 = (*owner)[(index + 1) % owner->size()];
|
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
FractureEdge( int64_t y = 0 ) :
|
|
|
|
m_connected( false ),
|
|
|
|
m_next( NULL )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
|
|
|
m_p1.Y = m_p2.Y = y;
|
|
|
|
}
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
FractureEdge( bool connected, const IntPoint& p1, const IntPoint& p2 ) :
|
|
|
|
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-06 13:15:48 +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;
|
|
|
|
IntPoint 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
|
|
|
{
|
|
|
|
int64_t x = edge->m_p1.X;
|
|
|
|
int64_t y = edge->m_p1.Y;
|
2015-07-06 13:15:48 +00:00
|
|
|
int64_t min_dist = std::numeric_limits<int64_t>::max();
|
|
|
|
int64_t 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-06 13:15:48 +00:00
|
|
|
int64_t x_intersect;
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-06 13:15:48 +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 );
|
|
|
|
else
|
|
|
|
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-06 13:15:48 +00:00
|
|
|
int64_t dist = ( x - x_intersect );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
if( dist > 0 && dist < min_dist )
|
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-06 13:15:48 +00:00
|
|
|
FractureEdge* lead1 = new FractureEdge( true, IntPoint( x_nearest, y), IntPoint( x, y ) );
|
|
|
|
FractureEdge* lead2 = new FractureEdge( true, IntPoint( x, y), IntPoint( x_nearest, y ) );
|
|
|
|
FractureEdge* split_2 = new FractureEdge( true, IntPoint( 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-06 13:15:48 +00:00
|
|
|
e_nearest->m_p2 = IntPoint( x_nearest, y );
|
|
|
|
e_nearest->m_next = lead1;
|
2015-06-12 15:12:02 +00:00
|
|
|
lead1->m_next = edge;
|
2015-07-06 13:15:48 +00:00
|
|
|
FractureEdge* last;
|
2015-06-12 15:12:02 +00:00
|
|
|
|
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-06 13:15:48 +00:00
|
|
|
|
2015-06-12 15:12:02 +00:00
|
|
|
void SHAPE_POLY_SET::fractureSingle( ClipperLib::Paths& paths )
|
|
|
|
{
|
|
|
|
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-06 13:15:48 +00:00
|
|
|
BOOST_FOREACH( Path& 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
|
|
|
|
|
|
|
int64_t x_min = std::numeric_limits<int64_t>::max();
|
|
|
|
|
|
|
|
for( unsigned i = 0; i < path.size(); i++ )
|
|
|
|
{
|
|
|
|
if( path[i].X < x_min )
|
|
|
|
x_min = path[i].X;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( unsigned i = 0; i < path.size(); 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-06 13:15:48 +00:00
|
|
|
if( i == path.size() - 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 )
|
|
|
|
{
|
|
|
|
if( fe->m_p1.X == x_min )
|
|
|
|
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-06 13:15:48 +00:00
|
|
|
int64_t x_min = std::numeric_limits<int64_t>::max();
|
|
|
|
FractureEdge* smallestX;
|
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-06 13:15:48 +00:00
|
|
|
int64_t xt = (*i)->m_p1.X;
|
|
|
|
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();
|
|
|
|
Path newPath;
|
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 )
|
|
|
|
newPath.push_back( e->m_p1 );
|
2015-06-19 17:39:33 +00:00
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
newPath.push_back( 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-06 13:15:48 +00:00
|
|
|
BOOST_FOREACH( Paths& 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-06 13:15:48 +00:00
|
|
|
for( unsigned i = 0; i < m_polys.size(); i++ )
|
2015-06-12 15:12:02 +00:00
|
|
|
{
|
|
|
|
Paths out;
|
2015-07-06 13:15:48 +00:00
|
|
|
SimplifyPolygons( m_polys[i], out, pftNonZero );
|
2015-06-12 15:12:02 +00:00
|
|
|
m_polys[i] = out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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";
|
2015-07-06 13:15:48 +00:00
|
|
|
|
2015-06-12 15:12:02 +00:00
|
|
|
for( unsigned j = 0; j < m_polys[i].size(); j++)
|
|
|
|
{
|
|
|
|
ss << m_polys[i][j].size() << "\n";
|
2015-07-06 13:15:48 +00:00
|
|
|
|
2015-06-12 15:12:02 +00:00
|
|
|
for( unsigned v = 0; v < m_polys[i][j].size(); v++)
|
|
|
|
ss << m_polys[i][j][v].X << " " << m_polys[i][j][v].Y << "\n";
|
|
|
|
}
|
|
|
|
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
|
|
|
{
|
|
|
|
ClipperLib::Paths paths;
|
|
|
|
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
|
|
|
{
|
|
|
|
ClipperLib::Path outline;
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
ClipperLib::IntPoint p;
|
|
|
|
|
2015-07-06 13:15:48 +00:00
|
|
|
aStream >> tmp; p.X = atoi( tmp.c_str() );
|
|
|
|
aStream >> tmp; p.Y = atoi( tmp.c_str() );
|
|
|
|
outline.push_back( p );
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
2015-07-06 13:15:48 +00:00
|
|
|
paths.push_back( outline );
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
2015-07-06 13:15:48 +00:00
|
|
|
m_polys.push_back( paths );
|
2015-06-12 15:12:02 +00:00
|
|
|
}
|
2015-07-06 13:15:48 +00:00
|
|
|
|
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;
|
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
for( unsigned i = 0; i < m_polys.size(); i++ )
|
|
|
|
{
|
|
|
|
for( unsigned j = 0; j < m_polys[i].size(); j++)
|
|
|
|
{
|
|
|
|
for( unsigned v = 0; v < m_polys[i][j].size(); v++)
|
|
|
|
{
|
|
|
|
VECTOR2I p( m_polys[i][j][v].X, m_polys[i][j][v].Y );
|
2015-07-06 13:15:48 +00:00
|
|
|
|
|
|
|
if( first )
|
|
|
|
bb = BOX2I( p, VECTOR2I( 0, 0 ) );
|
2015-06-12 15:12:02 +00:00
|
|
|
else
|
2015-07-06 13:15:48 +00:00
|
|
|
bb.Merge( p );
|
2015-06-12 15:12:02 +00:00
|
|
|
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bb.Inflate( aClearance );
|
|
|
|
return bb;
|
|
|
|
}
|