2008-01-06 12:43:57 +00:00
|
|
|
// PolyLine.cpp ... implementation of CPolyLine class from FreePCB.
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2008-01-06 12:43:57 +00:00
|
|
|
//
|
2013-02-19 09:21:55 +00:00
|
|
|
// implementation for kicad, using clipper polygon clipping library
|
|
|
|
// for transformations not handled (at least for Kicad) by boost::polygon
|
2007-12-29 19:27:58 +00:00
|
|
|
//
|
2012-09-21 17:02:54 +00:00
|
|
|
#include <cmath>
|
2007-12-29 19:27:58 +00:00
|
|
|
#include <vector>
|
2012-01-13 18:35:46 +00:00
|
|
|
#include <algorithm>
|
2007-12-29 19:27:58 +00:00
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <fctsys.h>
|
// Dick Hollenbeck's KiROUND R&D
// This provides better project control over rounding to int from double
// than wxRound() did. This scheme provides better logging in Debug builds
// and it provides for compile time calculation of constants.
#include <stdio.h>
#include <assert.h>
#include <limits.h>
//-----<KiROUND KIT>------------------------------------------------------------
/**
* KiROUND
* rounds a floating point number to an int using
* "round halfway cases away from zero".
* In Debug build an assert fires if will not fit into an int.
*/
#if defined( DEBUG )
// DEBUG: a macro to capture line and file, then calls this inline
static inline int KiRound( double v, int line, const char* filename )
{
v = v < 0 ? v - 0.5 : v + 0.5;
if( v > INT_MAX + 0.5 )
{
printf( "%s: in file %s on line %d, val: %.16g too ' > 0 ' for int\n", __FUNCTION__, filename, line, v );
}
else if( v < INT_MIN - 0.5 )
{
printf( "%s: in file %s on line %d, val: %.16g too ' < 0 ' for int\n", __FUNCTION__, filename, line, v );
}
return int( v );
}
#define KiROUND( v ) KiRound( v, __LINE__, __FILE__ )
#else
// RELEASE: a macro so compile can pre-compute constants.
#define KiROUND( v ) int( (v) < 0 ? (v) - 0.5 : (v) + 0.5 )
#endif
//-----</KiROUND KIT>-----------------------------------------------------------
// Only a macro is compile time calculated, an inline function causes a static constructor
// in a situation like this.
// Therefore the Release build is best done with a MACRO not an inline function.
int Computed = KiROUND( 14.3 * 8 );
int main( int argc, char** argv )
{
for( double d = double(INT_MAX)-1; d < double(INT_MAX)+8; d += 2.0 )
{
int i = KiROUND( d );
printf( "t: %d %.16g\n", i, d );
}
return 0;
}
2012-04-19 06:55:45 +00:00
|
|
|
#include <common.h> // KiROUND
|
2008-01-16 20:37:50 +00:00
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <PolyLine.h>
|
|
|
|
#include <bezier_curves.h>
|
|
|
|
#include <polygon_test_point_inside.h>
|
2012-07-30 07:40:25 +00:00
|
|
|
#include <math_for_graphics.h>
|
2012-07-31 17:51:58 +00:00
|
|
|
#include <polygon_test_point_inside.h>
|
|
|
|
|
2013-08-16 14:25:34 +00:00
|
|
|
|
2007-12-29 19:27:58 +00:00
|
|
|
CPolyLine::CPolyLine()
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2013-04-06 05:01:48 +00:00
|
|
|
m_hatchStyle = NO_HATCH;
|
|
|
|
m_hatchPitch = 0;
|
|
|
|
m_layer = LAYER_N_FRONT;
|
|
|
|
m_utility = 0;
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
2013-05-14 18:47:01 +00:00
|
|
|
CPolyLine::CPolyLine( const CPolyLine& aCPolyLine)
|
|
|
|
{
|
|
|
|
Copy( &aCPolyLine );
|
|
|
|
m_HatchLines = aCPolyLine.m_HatchLines; // vector <> copy
|
|
|
|
}
|
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2007-12-29 19:27:58 +00:00
|
|
|
// destructor, removes display elements
|
|
|
|
//
|
|
|
|
CPolyLine::~CPolyLine()
|
|
|
|
{
|
2012-01-13 18:35:46 +00:00
|
|
|
UnHatch();
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
2012-08-31 13:58:23 +00:00
|
|
|
/* Removes corners which create a null segment edge
|
|
|
|
* (i.e. when 2 successive corners are at the same location)
|
|
|
|
* returns the count of removed corners.
|
|
|
|
*/
|
|
|
|
int CPolyLine::RemoveNullSegments()
|
|
|
|
{
|
|
|
|
int removed = 0;
|
|
|
|
|
|
|
|
unsigned startcountour = 0;
|
2013-05-09 19:08:12 +00:00
|
|
|
for( unsigned icnt = 1; icnt < m_CornersList.GetCornersCount(); icnt ++ )
|
2012-08-31 13:58:23 +00:00
|
|
|
{
|
|
|
|
unsigned last = icnt-1;
|
|
|
|
if( m_CornersList[icnt].end_contour )
|
|
|
|
{
|
|
|
|
last = startcountour;
|
|
|
|
startcountour = icnt+1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( m_CornersList[last].x == m_CornersList[icnt].x ) &&
|
|
|
|
( m_CornersList[last].y == m_CornersList[icnt].y ) )
|
|
|
|
{
|
|
|
|
DeleteCorner( icnt );
|
|
|
|
icnt--;
|
|
|
|
removed ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( m_CornersList[icnt].end_contour )
|
|
|
|
{
|
|
|
|
startcountour = icnt+1;
|
|
|
|
icnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return removed;
|
|
|
|
}
|
|
|
|
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-07-25 07:36:56 +00:00
|
|
|
/**
|
2012-08-04 09:43:27 +00:00
|
|
|
* Function NormalizeAreaOutlines
|
|
|
|
* Convert a self-intersecting polygon to one (or more) non self-intersecting polygon(s)
|
|
|
|
* @param aNewPolygonList = a std::vector<CPolyLine*> reference where to store new CPolyLine
|
|
|
|
* needed by the normalization
|
|
|
|
* @return the polygon count (always >= 1, because there is at least one polygon)
|
|
|
|
* There are new polygons only if the polygon count is > 1
|
2008-05-30 18:06:21 +00:00
|
|
|
*/
|
2012-08-04 09:43:27 +00:00
|
|
|
#include "clipper.hpp"
|
|
|
|
int CPolyLine::NormalizeAreaOutlines( std::vector<CPolyLine*>* aNewPolygonList )
|
2007-12-29 19:27:58 +00:00
|
|
|
{
|
2012-08-04 09:43:27 +00:00
|
|
|
ClipperLib::Polygon raw_polygon;
|
|
|
|
ClipperLib::Polygons normalized_polygons;
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2013-05-09 19:08:12 +00:00
|
|
|
unsigned corners_count = m_CornersList.GetCornersCount();
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-08-04 09:43:27 +00:00
|
|
|
KI_POLYGON_SET polysholes;
|
|
|
|
KI_POLYGON_WITH_HOLES mainpoly;
|
|
|
|
std::vector<KI_POLY_POINT> cornerslist;
|
|
|
|
KI_POLYGON_WITH_HOLES_SET all_contours;
|
|
|
|
KI_POLYGON poly_tmp;
|
2008-05-30 18:06:21 +00:00
|
|
|
|
2012-08-04 09:43:27 +00:00
|
|
|
// Normalize first contour
|
|
|
|
unsigned ic = 0;
|
|
|
|
while( ic < corners_count )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2012-08-04 09:43:27 +00:00
|
|
|
const CPolyPt& corner = m_CornersList[ic++];
|
|
|
|
raw_polygon.push_back( ClipperLib::IntPoint( corner.x, corner.y ) );
|
2008-05-30 18:06:21 +00:00
|
|
|
|
2012-08-04 09:43:27 +00:00
|
|
|
if( corner.end_contour )
|
|
|
|
break;
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
2012-08-31 13:58:23 +00:00
|
|
|
|
2012-08-04 09:43:27 +00:00
|
|
|
ClipperLib::SimplifyPolygon( raw_polygon, normalized_polygons );
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2012-08-04 09:43:27 +00:00
|
|
|
// enter main outline
|
|
|
|
for( unsigned ii = 0; ii < normalized_polygons.size(); ii++ )
|
2008-05-30 18:06:21 +00:00
|
|
|
{
|
2012-08-04 09:43:27 +00:00
|
|
|
ClipperLib::Polygon& polygon = normalized_polygons[ii];
|
|
|
|
cornerslist.clear();
|
|
|
|
for( unsigned jj = 0; jj < polygon.size(); jj++ )
|
2013-05-08 18:20:58 +00:00
|
|
|
cornerslist.push_back( KI_POLY_POINT( KiROUND( polygon[jj].X ),
|
2013-05-04 11:57:09 +00:00
|
|
|
KiROUND( polygon[jj].Y ) ) );
|
2012-08-04 09:43:27 +00:00
|
|
|
mainpoly.set( cornerslist.begin(), cornerslist.end() );
|
|
|
|
all_contours.push_back( mainpoly );
|
2008-05-30 18:06:21 +00:00
|
|
|
}
|
2012-07-13 18:55:29 +00:00
|
|
|
|
2012-08-04 09:43:27 +00:00
|
|
|
// Enter holes
|
|
|
|
while( ic < corners_count )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2012-08-04 09:43:27 +00:00
|
|
|
cornerslist.clear();
|
|
|
|
raw_polygon.clear();
|
|
|
|
normalized_polygons.clear();
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2012-08-04 09:43:27 +00:00
|
|
|
// Normalize current hole and add it to hole list
|
|
|
|
while( ic < corners_count )
|
2012-07-30 07:40:25 +00:00
|
|
|
{
|
2012-08-04 09:43:27 +00:00
|
|
|
const CPolyPt& corner = m_CornersList[ic++];
|
|
|
|
raw_polygon.push_back( ClipperLib::IntPoint( corner.x, corner.y ) );
|
|
|
|
|
|
|
|
if( corner.end_contour )
|
2012-07-30 07:40:25 +00:00
|
|
|
{
|
2012-08-04 09:43:27 +00:00
|
|
|
ClipperLib::SimplifyPolygon( raw_polygon, normalized_polygons );
|
|
|
|
for( unsigned ii = 0; ii < normalized_polygons.size(); ii++ )
|
2012-07-30 07:40:25 +00:00
|
|
|
{
|
2012-08-04 09:43:27 +00:00
|
|
|
ClipperLib::Polygon& polygon = normalized_polygons[ii];
|
|
|
|
cornerslist.clear();
|
|
|
|
for( unsigned jj = 0; jj < polygon.size(); jj++ )
|
2013-05-08 18:20:58 +00:00
|
|
|
cornerslist.push_back( KI_POLY_POINT( KiROUND( polygon[jj].X ),
|
2013-05-04 11:57:09 +00:00
|
|
|
KiROUND( polygon[jj].Y ) ) );
|
2012-08-04 09:43:27 +00:00
|
|
|
bpl::set_points( poly_tmp, cornerslist.begin(), cornerslist.end() );
|
|
|
|
polysholes.push_back( poly_tmp );
|
2012-07-30 07:40:25 +00:00
|
|
|
}
|
2012-08-04 09:43:27 +00:00
|
|
|
break;
|
2012-07-30 07:40:25 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-04 09:43:27 +00:00
|
|
|
}
|
|
|
|
all_contours -= polysholes;
|
2012-07-30 07:40:25 +00:00
|
|
|
|
2012-08-04 09:43:27 +00:00
|
|
|
// copy polygon with holes to destination
|
|
|
|
RemoveAllContours();
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-08-04 09:43:27 +00:00
|
|
|
#define outlines all_contours
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-08-04 09:43:27 +00:00
|
|
|
for( unsigned ii = 0; ii < outlines.size(); ii++ )
|
|
|
|
{
|
|
|
|
CPolyLine* polyline = this;
|
|
|
|
if( ii > 0 )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2012-08-04 09:43:27 +00:00
|
|
|
polyline = new CPolyLine;
|
|
|
|
polyline->ImportSettings( this );
|
|
|
|
aNewPolygonList->push_back( polyline );
|
2009-09-19 16:15:40 +00:00
|
|
|
}
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2012-08-04 09:43:27 +00:00
|
|
|
KI_POLYGON_WITH_HOLES& curr_poly = outlines[ii];
|
|
|
|
KI_POLYGON_WITH_HOLES::iterator_type corner = curr_poly.begin();
|
|
|
|
// enter main contour
|
|
|
|
while( corner != curr_poly.end() )
|
2008-05-30 18:06:21 +00:00
|
|
|
{
|
2012-08-04 09:43:27 +00:00
|
|
|
polyline->AppendCorner( corner->x(), corner->y() );
|
|
|
|
corner++;
|
2008-05-30 18:06:21 +00:00
|
|
|
}
|
2012-08-04 09:43:27 +00:00
|
|
|
polyline->CloseLastContour();
|
|
|
|
|
|
|
|
// add holes (set of polygons)
|
|
|
|
KI_POLYGON_WITH_HOLES::iterator_holes_type hole = curr_poly.begin_holes();
|
|
|
|
while( hole != curr_poly.end_holes() )
|
2008-05-30 18:06:21 +00:00
|
|
|
{
|
2012-08-04 09:43:27 +00:00
|
|
|
KI_POLYGON::iterator_type hole_corner = hole->begin();
|
|
|
|
// create area with external contour: Recreate only area edges, NOT holes
|
|
|
|
while( hole_corner != hole->end() )
|
|
|
|
{
|
|
|
|
polyline->AppendCorner( hole_corner->x(), hole_corner->y() );
|
|
|
|
hole_corner++;
|
|
|
|
}
|
|
|
|
polyline->CloseLastContour();
|
|
|
|
hole++;
|
2008-05-30 18:06:21 +00:00
|
|
|
}
|
2012-08-31 13:58:23 +00:00
|
|
|
|
|
|
|
polyline->RemoveNullSegments();
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
|
|
|
|
2012-08-04 09:43:27 +00:00
|
|
|
return outlines.size();
|
2008-05-30 18:06:21 +00:00
|
|
|
}
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2012-08-02 17:32:42 +00:00
|
|
|
/**
|
|
|
|
* Function ImportSettings
|
|
|
|
* Copy settings (layer, hatch styles) from aPoly
|
|
|
|
*/
|
|
|
|
void CPolyLine::ImportSettings( const CPolyLine * aPoly )
|
|
|
|
{
|
|
|
|
SetLayer( aPoly->GetLayer() );
|
|
|
|
SetHatchStyle( aPoly->GetHatchStyle() );
|
|
|
|
SetHatchPitch( aPoly->GetHatchPitch() );
|
|
|
|
}
|
2008-10-12 15:29:43 +00:00
|
|
|
|
2012-07-31 13:12:51 +00:00
|
|
|
/* initialize a contour
|
|
|
|
* set layer, hatch style, and starting point
|
|
|
|
*/
|
2013-03-31 13:27:46 +00:00
|
|
|
void CPolyLine::Start( LAYER_NUM layer, int x, int y, int hatch )
|
2007-12-29 19:27:58 +00:00
|
|
|
{
|
2012-07-25 18:46:25 +00:00
|
|
|
m_layer = layer;
|
2012-07-30 07:40:25 +00:00
|
|
|
SetHatchStyle( (enum HATCH_STYLE) hatch );
|
2008-05-15 11:20:19 +00:00
|
|
|
CPolyPt poly_pt( x, y );
|
2012-01-22 17:20:22 +00:00
|
|
|
poly_pt.end_contour = false;
|
2007-12-29 19:27:58 +00:00
|
|
|
|
2013-05-09 19:08:12 +00:00
|
|
|
m_CornersList.Append( poly_pt );
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2007-12-29 19:27:58 +00:00
|
|
|
// add a corner to unclosed polyline
|
|
|
|
//
|
2012-07-31 13:12:51 +00:00
|
|
|
void CPolyLine::AppendCorner( int x, int y )
|
2007-12-29 19:27:58 +00:00
|
|
|
{
|
2012-01-13 18:35:46 +00:00
|
|
|
UnHatch();
|
2008-05-15 11:20:19 +00:00
|
|
|
CPolyPt poly_pt( x, y );
|
2012-01-22 17:20:22 +00:00
|
|
|
poly_pt.end_contour = false;
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2012-07-31 17:51:58 +00:00
|
|
|
// add entries for new corner
|
2013-05-09 19:08:12 +00:00
|
|
|
m_CornersList.Append( poly_pt );
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// move corner of polyline
|
|
|
|
//
|
|
|
|
void CPolyLine::MoveCorner( int ic, int x, int y )
|
|
|
|
{
|
2012-01-13 18:35:46 +00:00
|
|
|
UnHatch();
|
2012-07-25 07:36:56 +00:00
|
|
|
m_CornersList[ic].x = x;
|
|
|
|
m_CornersList[ic].y = y;
|
2012-01-13 18:35:46 +00:00
|
|
|
Hatch();
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2007-12-29 19:27:58 +00:00
|
|
|
// delete corner and adjust arrays
|
|
|
|
//
|
2012-07-31 13:12:51 +00:00
|
|
|
void CPolyLine::DeleteCorner( int ic )
|
2007-12-29 19:27:58 +00:00
|
|
|
{
|
2012-01-13 18:35:46 +00:00
|
|
|
UnHatch();
|
2012-07-25 18:46:25 +00:00
|
|
|
int icont = GetContour( ic );
|
|
|
|
int iend = GetContourEnd( icont );
|
2012-07-31 17:51:58 +00:00
|
|
|
bool closed = icont < GetContoursCount() - 1 || GetClosed();
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2012-07-31 17:51:58 +00:00
|
|
|
if( !closed )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
|
|
|
// open contour, must be last contour
|
2013-05-08 18:20:58 +00:00
|
|
|
m_CornersList.DeleteCorner( ic );
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// closed contour
|
2013-05-08 18:20:58 +00:00
|
|
|
m_CornersList.DeleteCorner( ic );
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
if( ic == iend )
|
2012-07-25 07:36:56 +00:00
|
|
|
m_CornersList[ic - 1].end_contour = true;
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-07-31 17:51:58 +00:00
|
|
|
if( closed && GetContourSize( icont ) < 3 )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
|
|
|
// delete the entire contour
|
|
|
|
RemoveContour( icont );
|
|
|
|
}
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2008-01-05 13:37:51 +00:00
|
|
|
/******************************************/
|
2007-12-29 19:27:58 +00:00
|
|
|
void CPolyLine::RemoveContour( int icont )
|
2008-01-05 13:37:51 +00:00
|
|
|
/******************************************/
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2008-01-05 13:37:51 +00:00
|
|
|
/**
|
|
|
|
* Function RemoveContour
|
|
|
|
* @param icont = contour number to remove
|
|
|
|
* remove a contour only if there is more than 1 contour
|
|
|
|
*/
|
2007-12-29 19:27:58 +00:00
|
|
|
{
|
2012-01-13 18:35:46 +00:00
|
|
|
UnHatch();
|
2012-07-25 18:46:25 +00:00
|
|
|
int istart = GetContourStart( icont );
|
|
|
|
int iend = GetContourEnd( icont );
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2012-07-25 07:36:56 +00:00
|
|
|
int polycount = GetContoursCount();
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-07-13 18:55:29 +00:00
|
|
|
if( icont == 0 && polycount == 1 )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
|
|
|
// remove the only contour
|
|
|
|
wxASSERT( 0 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// remove closed contour
|
|
|
|
for( int ic = iend; ic>=istart; ic-- )
|
|
|
|
{
|
2013-05-08 18:20:58 +00:00
|
|
|
m_CornersList.DeleteCorner( ic );
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-01-13 18:35:46 +00:00
|
|
|
Hatch();
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
2008-01-04 12:27:16 +00:00
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
CPolyLine* CPolyLine::Chamfer( unsigned int aDistance )
|
2011-02-21 19:43:59 +00:00
|
|
|
{
|
2011-02-23 21:34:28 +00:00
|
|
|
CPolyLine* newPoly = new CPolyLine;
|
2011-02-21 19:43:59 +00:00
|
|
|
|
|
|
|
if( !aDistance )
|
|
|
|
{
|
2011-02-23 21:34:28 +00:00
|
|
|
newPoly->Copy( this );
|
|
|
|
return newPoly;
|
2011-02-21 19:43:59 +00:00
|
|
|
}
|
|
|
|
|
2012-07-25 07:36:56 +00:00
|
|
|
int polycount = GetContoursCount();
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-07-13 18:55:29 +00:00
|
|
|
for( int contour = 0; contour < polycount; contour++ )
|
2011-02-21 19:43:59 +00:00
|
|
|
{
|
2012-07-25 18:46:25 +00:00
|
|
|
unsigned int startIndex = GetContourStart( contour );
|
|
|
|
unsigned int endIndex = GetContourEnd( contour );
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
for( unsigned int index = startIndex; index <= endIndex; index++ )
|
|
|
|
{
|
2012-07-25 18:46:25 +00:00
|
|
|
int x1, y1, nx, ny;
|
|
|
|
long long xa, ya, xb, yb;
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2012-07-25 18:46:25 +00:00
|
|
|
x1 = m_CornersList[index].x;
|
|
|
|
y1 = m_CornersList[index].y;
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
if( index == startIndex )
|
|
|
|
{
|
2012-07-25 18:46:25 +00:00
|
|
|
xa = m_CornersList[endIndex].x - x1;
|
|
|
|
ya = m_CornersList[endIndex].y - y1;
|
2011-02-23 21:34:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-25 18:46:25 +00:00
|
|
|
xa = m_CornersList[index - 1].x - x1;
|
|
|
|
ya = m_CornersList[index - 1].y - y1;
|
2011-02-23 21:34:28 +00:00
|
|
|
}
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
if( index == endIndex )
|
|
|
|
{
|
2012-07-25 18:46:25 +00:00
|
|
|
xb = m_CornersList[startIndex].x - x1;
|
|
|
|
yb = m_CornersList[startIndex].y - y1;
|
2011-02-23 21:34:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-25 18:46:25 +00:00
|
|
|
xb = m_CornersList[index + 1].x - x1;
|
|
|
|
yb = m_CornersList[index + 1].y - y1;
|
2011-02-23 21:34:28 +00:00
|
|
|
}
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2013-05-04 11:57:09 +00:00
|
|
|
unsigned int lena = KiROUND( hypot( xa, ya ) );
|
|
|
|
unsigned int lenb = KiROUND( hypot( xb, yb ) );
|
2012-07-25 18:46:25 +00:00
|
|
|
unsigned int distance = aDistance;
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
// Chamfer one half of an edge at most
|
2012-07-25 18:46:25 +00:00
|
|
|
if( 0.5 * lena < distance )
|
2013-05-04 11:57:09 +00:00
|
|
|
distance = int( 0.5 * lena );
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2012-07-25 18:46:25 +00:00
|
|
|
if( 0.5 * lenb < distance )
|
2013-05-04 11:57:09 +00:00
|
|
|
distance = int( 0.5 * lenb );
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2013-05-04 11:57:09 +00:00
|
|
|
nx = KiROUND( (distance * xa) / hypot( xa, ya ) );
|
|
|
|
ny = KiROUND( (distance * ya) / hypot( xa, ya ) );
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
if( index == startIndex )
|
|
|
|
newPoly->Start( GetLayer(), x1 + nx, y1 + ny, GetHatchStyle() );
|
|
|
|
else
|
|
|
|
newPoly->AppendCorner( x1 + nx, y1 + ny );
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2013-05-04 11:57:09 +00:00
|
|
|
nx = KiROUND( (distance * xb) / hypot( xb, yb ) );
|
|
|
|
ny = KiROUND( (distance * yb) / hypot( xb, yb ) );
|
2011-02-23 21:34:28 +00:00
|
|
|
newPoly->AppendCorner( x1 + nx, y1 + ny );
|
|
|
|
}
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-07-30 07:40:25 +00:00
|
|
|
newPoly->CloseLastContour();
|
2011-02-21 19:43:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return newPoly;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
CPolyLine* CPolyLine::Fillet( unsigned int aRadius, unsigned int aSegments )
|
2011-02-21 19:43:59 +00:00
|
|
|
{
|
2011-02-23 21:34:28 +00:00
|
|
|
CPolyLine* newPoly = new CPolyLine;
|
2011-02-21 19:43:59 +00:00
|
|
|
|
|
|
|
if( !aRadius )
|
|
|
|
{
|
2011-02-23 21:34:28 +00:00
|
|
|
newPoly->Copy( this );
|
|
|
|
return newPoly;
|
2011-02-21 19:43:59 +00:00
|
|
|
}
|
|
|
|
|
2012-07-25 07:36:56 +00:00
|
|
|
int polycount = GetContoursCount();
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-07-13 18:55:29 +00:00
|
|
|
for( int contour = 0; contour < polycount; contour++ )
|
2011-02-21 19:43:59 +00:00
|
|
|
{
|
2012-07-25 18:46:25 +00:00
|
|
|
unsigned int startIndex = GetContourStart( contour );
|
|
|
|
unsigned int endIndex = GetContourEnd( contour );
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
for( unsigned int index = startIndex; index <= endIndex; index++ )
|
|
|
|
{
|
2012-07-25 18:46:25 +00:00
|
|
|
int x1, y1; // Current vertex
|
|
|
|
long long xa, ya; // Previous vertex
|
|
|
|
long long xb, yb; // Next vertex
|
|
|
|
double nx, ny;
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2012-07-25 18:46:25 +00:00
|
|
|
x1 = m_CornersList[index].x;
|
|
|
|
y1 = m_CornersList[index].y;
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
if( index == startIndex )
|
|
|
|
{
|
2012-07-25 18:46:25 +00:00
|
|
|
xa = m_CornersList[endIndex].x - x1;
|
|
|
|
ya = m_CornersList[endIndex].y - y1;
|
2011-02-23 21:34:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-25 18:46:25 +00:00
|
|
|
xa = m_CornersList[index - 1].x - x1;
|
|
|
|
ya = m_CornersList[index - 1].y - y1;
|
2011-02-23 21:34:28 +00:00
|
|
|
}
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
if( index == endIndex )
|
|
|
|
{
|
2012-07-25 18:46:25 +00:00
|
|
|
xb = m_CornersList[startIndex].x - x1;
|
|
|
|
yb = m_CornersList[startIndex].y - y1;
|
2011-02-23 21:34:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-25 18:46:25 +00:00
|
|
|
xb = m_CornersList[index + 1].x - x1;
|
|
|
|
yb = m_CornersList[index + 1].y - y1;
|
2011-02-23 21:34:28 +00:00
|
|
|
}
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2013-05-04 11:57:09 +00:00
|
|
|
double lena = hypot( xa, ya );
|
|
|
|
double lenb = hypot( xb, yb );
|
2012-07-25 18:46:25 +00:00
|
|
|
double cosine = ( xa * xb + ya * yb ) / ( lena * lenb );
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2012-11-30 05:51:47 +00:00
|
|
|
double radius = aRadius;
|
2012-07-25 18:46:25 +00:00
|
|
|
double denom = sqrt( 2.0 / ( 1 + cosine ) - 1 );
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2013-08-16 11:51:35 +00:00
|
|
|
// Do nothing in case of parallel edges
|
2013-08-16 14:25:34 +00:00
|
|
|
if( std::isinf( denom ) )
|
2013-08-16 11:51:35 +00:00
|
|
|
continue;
|
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
// Limit rounding distance to one half of an edge
|
2012-07-25 18:46:25 +00:00
|
|
|
if( 0.5 * lena * denom < radius )
|
2012-11-30 05:51:47 +00:00
|
|
|
radius = 0.5 * lena * denom;
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2012-07-25 18:46:25 +00:00
|
|
|
if( 0.5 * lenb * denom < radius )
|
2012-11-30 05:51:47 +00:00
|
|
|
radius = 0.5 * lenb * denom;
|
2011-02-23 21:34:28 +00:00
|
|
|
|
|
|
|
// Calculate fillet arc absolute center point (xc, yx)
|
2012-07-25 18:46:25 +00:00
|
|
|
double k = radius / sqrt( .5 * ( 1 - cosine ) );
|
|
|
|
double lenab = sqrt( ( xa / lena + xb / lenb ) * ( xa / lena + xb / lenb ) +
|
|
|
|
( ya / lena + yb / lenb ) * ( ya / lena + yb / lenb ) );
|
|
|
|
double xc = x1 + k * ( xa / lena + xb / lenb ) / lenab;
|
|
|
|
double yc = y1 + k * ( ya / lena + yb / lenb ) / lenab;
|
2011-02-23 21:34:28 +00:00
|
|
|
|
|
|
|
// Calculate arc start and end vectors
|
2012-07-25 18:46:25 +00:00
|
|
|
k = radius / sqrt( 2 / ( 1 + cosine ) - 1 );
|
|
|
|
double xs = x1 + k * xa / lena - xc;
|
|
|
|
double ys = y1 + k * ya / lena - yc;
|
|
|
|
double xe = x1 + k * xb / lenb - xc;
|
|
|
|
double ye = y1 + k * yb / lenb - yc;
|
2011-02-23 21:34:28 +00:00
|
|
|
|
|
|
|
// Cosine of arc angle
|
2012-07-25 18:46:25 +00:00
|
|
|
double argument = ( xs * xe + ys * ye ) / ( radius * radius );
|
2011-02-23 21:34:28 +00:00
|
|
|
|
|
|
|
if( argument < -1 ) // Just in case...
|
|
|
|
argument = -1;
|
|
|
|
else if( argument > 1 )
|
|
|
|
argument = 1;
|
|
|
|
|
2012-07-25 18:46:25 +00:00
|
|
|
double arcAngle = acos( argument );
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
// Calculate the number of segments
|
2012-07-25 18:46:25 +00:00
|
|
|
double tempSegments = (double) aSegments * ( arcAngle / ( 2 * M_PI ) );
|
2011-02-23 21:34:28 +00:00
|
|
|
|
2012-07-25 18:46:25 +00:00
|
|
|
if( tempSegments - (int) tempSegments > 0 )
|
2011-02-23 21:34:28 +00:00
|
|
|
tempSegments++;
|
|
|
|
|
2012-07-25 18:46:25 +00:00
|
|
|
unsigned int segments = (unsigned int) tempSegments;
|
|
|
|
|
|
|
|
double deltaAngle = arcAngle / segments;
|
|
|
|
double startAngle = atan2( -ys, xs );
|
2011-02-23 21:34:28 +00:00
|
|
|
|
|
|
|
// Flip arc for inner corners
|
2012-07-25 18:46:25 +00:00
|
|
|
if( xa * yb - ya * xb <= 0 )
|
2011-02-23 21:34:28 +00:00
|
|
|
deltaAngle *= -1;
|
|
|
|
|
2013-05-04 11:57:09 +00:00
|
|
|
nx = xc + xs;
|
|
|
|
ny = yc + ys;
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
if( index == startIndex )
|
2013-05-04 11:57:09 +00:00
|
|
|
newPoly->Start( GetLayer(), KiROUND( nx ), KiROUND( ny ), GetHatchStyle() );
|
2011-02-23 21:34:28 +00:00
|
|
|
else
|
2013-05-04 11:57:09 +00:00
|
|
|
newPoly->AppendCorner( KiROUND( nx ), KiROUND( ny ) );
|
2011-02-23 21:34:28 +00:00
|
|
|
|
|
|
|
for( unsigned int j = 0; j < segments; j++ )
|
|
|
|
{
|
2013-05-04 11:57:09 +00:00
|
|
|
nx = xc + cos( startAngle + (j + 1) * deltaAngle ) * radius;
|
|
|
|
ny = yc - sin( startAngle + (j + 1) * deltaAngle ) * radius;
|
|
|
|
newPoly->AppendCorner( KiROUND( nx ), KiROUND( ny ) );
|
2011-02-23 21:34:28 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-07-30 07:40:25 +00:00
|
|
|
newPoly->CloseLastContour();
|
2011-02-23 21:34:28 +00:00
|
|
|
}
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2011-02-21 19:43:59 +00:00
|
|
|
return newPoly;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-05 13:37:51 +00:00
|
|
|
/******************************************/
|
2008-01-04 12:27:16 +00:00
|
|
|
void CPolyLine::RemoveAllContours( void )
|
2008-01-05 13:37:51 +00:00
|
|
|
/******************************************/
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2008-01-04 12:27:16 +00:00
|
|
|
/**
|
|
|
|
* function RemoveAllContours
|
2013-05-14 18:47:01 +00:00
|
|
|
* removes all corners from the list.
|
|
|
|
* Others params are not changed
|
2008-01-04 12:27:16 +00:00
|
|
|
*/
|
|
|
|
{
|
2013-05-14 18:47:01 +00:00
|
|
|
m_CornersList.RemoveAllContours();
|
2008-01-04 12:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-12 16:36:43 +00:00
|
|
|
/**
|
|
|
|
* Function InsertCorner
|
2007-12-29 19:27:58 +00:00
|
|
|
* insert a new corner between two existing corners
|
|
|
|
* @param ic = index for the insertion point: the corner is inserted AFTER ic
|
|
|
|
* @param x, y = coordinates corner to insert
|
|
|
|
*/
|
|
|
|
void CPolyLine::InsertCorner( int ic, int x, int y )
|
|
|
|
{
|
2012-01-13 18:35:46 +00:00
|
|
|
UnHatch();
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2013-05-09 19:08:12 +00:00
|
|
|
if( (unsigned) (ic) >= m_CornersList.GetCornersCount() )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2013-05-09 19:08:12 +00:00
|
|
|
m_CornersList.Append( CPolyPt( x, y ) );
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-08 18:20:58 +00:00
|
|
|
m_CornersList.InsertCorner(ic, CPolyPt( x, y ) );
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
|
|
|
|
2013-05-09 19:08:12 +00:00
|
|
|
if( (unsigned) (ic + 1) < m_CornersList.GetCornersCount() )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2012-07-25 07:36:56 +00:00
|
|
|
if( m_CornersList[ic].end_contour )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2012-07-25 18:46:25 +00:00
|
|
|
m_CornersList[ic + 1].end_contour = true;
|
|
|
|
m_CornersList[ic].end_contour = false;
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-01-13 18:35:46 +00:00
|
|
|
Hatch();
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2007-12-29 19:27:58 +00:00
|
|
|
// undraw polyline by removing all graphic elements from display list
|
|
|
|
//
|
2012-01-13 18:35:46 +00:00
|
|
|
void CPolyLine::UnHatch()
|
2007-12-29 19:27:58 +00:00
|
|
|
{
|
2008-05-15 11:20:19 +00:00
|
|
|
m_HatchLines.clear();
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
int CPolyLine::GetEndContour( int ic )
|
|
|
|
{
|
2012-07-25 07:36:56 +00:00
|
|
|
return m_CornersList[ic].end_contour;
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2013-09-21 18:09:41 +00:00
|
|
|
CRect CPolyLine::GetBoundingBox()
|
2007-12-29 19:27:58 +00:00
|
|
|
{
|
2008-05-15 11:20:19 +00:00
|
|
|
CRect r;
|
|
|
|
|
|
|
|
r.left = r.bottom = INT_MAX;
|
|
|
|
r.right = r.top = INT_MIN;
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2013-05-09 19:08:12 +00:00
|
|
|
for( unsigned i = 0; i< m_CornersList.GetCornersCount(); i++ )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2012-08-04 09:43:27 +00:00
|
|
|
r.left = std::min( r.left, m_CornersList[i].x );
|
|
|
|
r.right = std::max( r.right, m_CornersList[i].x );
|
|
|
|
r.bottom = std::min( r.bottom, m_CornersList[i].y );
|
|
|
|
r.top = std::max( r.top, m_CornersList[i].y );
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2013-09-21 18:09:41 +00:00
|
|
|
CRect CPolyLine::GetBoundingBox( int icont )
|
2007-12-29 19:27:58 +00:00
|
|
|
{
|
2008-05-15 11:20:19 +00:00
|
|
|
CRect r;
|
|
|
|
|
|
|
|
r.left = r.bottom = INT_MAX;
|
|
|
|
r.right = r.top = INT_MIN;
|
2012-07-25 18:46:25 +00:00
|
|
|
int istart = GetContourStart( icont );
|
|
|
|
int iend = GetContourEnd( icont );
|
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
for( int i = istart; i<=iend; i++ )
|
|
|
|
{
|
2012-08-04 09:43:27 +00:00
|
|
|
r.left = std::min( r.left, m_CornersList[i].x );
|
|
|
|
r.right = std::max( r.right, m_CornersList[i].x );
|
|
|
|
r.bottom = std::min( r.bottom, m_CornersList[i].y );
|
|
|
|
r.top = std::max( r.top, m_CornersList[i].y );
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2012-07-25 07:36:56 +00:00
|
|
|
int CPolyLine::GetContoursCount()
|
2007-12-29 19:27:58 +00:00
|
|
|
{
|
2008-05-15 11:20:19 +00:00
|
|
|
int ncont = 0;
|
|
|
|
|
2013-05-09 19:08:12 +00:00
|
|
|
if( !m_CornersList.GetCornersCount() )
|
2008-05-15 11:20:19 +00:00
|
|
|
return 0;
|
|
|
|
|
2013-05-09 19:08:12 +00:00
|
|
|
for( unsigned ic = 0; ic < m_CornersList.GetCornersCount(); ic++ )
|
2012-07-25 07:36:56 +00:00
|
|
|
if( m_CornersList[ic].end_contour )
|
2008-05-15 11:20:19 +00:00
|
|
|
ncont++;
|
|
|
|
|
2013-05-09 19:08:12 +00:00
|
|
|
if( !m_CornersList[m_CornersList.GetCornersCount() - 1].end_contour )
|
2008-05-15 11:20:19 +00:00
|
|
|
ncont++;
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
return ncont;
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2007-12-29 19:27:58 +00:00
|
|
|
int CPolyLine::GetContour( int ic )
|
|
|
|
{
|
2008-05-15 11:20:19 +00:00
|
|
|
int ncont = 0;
|
|
|
|
|
|
|
|
for( int i = 0; i<ic; i++ )
|
|
|
|
{
|
2012-07-25 07:36:56 +00:00
|
|
|
if( m_CornersList[i].end_contour )
|
2008-05-15 11:20:19 +00:00
|
|
|
ncont++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ncont;
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2007-12-29 19:27:58 +00:00
|
|
|
int CPolyLine::GetContourStart( int icont )
|
|
|
|
{
|
2008-05-15 11:20:19 +00:00
|
|
|
if( icont == 0 )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int ncont = 0;
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2013-05-09 19:08:12 +00:00
|
|
|
for( unsigned i = 0; i<m_CornersList.GetCornersCount(); i++ )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2012-07-25 07:36:56 +00:00
|
|
|
if( m_CornersList[i].end_contour )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
|
|
|
ncont++;
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
if( ncont == icont )
|
|
|
|
return i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wxASSERT( 0 );
|
|
|
|
return 0;
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2007-12-29 19:27:58 +00:00
|
|
|
int CPolyLine::GetContourEnd( int icont )
|
|
|
|
{
|
2008-05-15 11:20:19 +00:00
|
|
|
if( icont < 0 )
|
|
|
|
return 0;
|
|
|
|
|
2012-07-25 07:36:56 +00:00
|
|
|
if( icont == GetContoursCount() - 1 )
|
2013-05-09 19:08:12 +00:00
|
|
|
return m_CornersList.GetCornersCount() - 1;
|
2008-05-15 11:20:19 +00:00
|
|
|
|
|
|
|
int ncont = 0;
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2013-05-09 19:08:12 +00:00
|
|
|
for( unsigned i = 0; i<m_CornersList.GetCornersCount(); i++ )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2012-07-25 07:36:56 +00:00
|
|
|
if( m_CornersList[i].end_contour )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
|
|
|
if( ncont == icont )
|
|
|
|
return i;
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
ncont++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wxASSERT( 0 );
|
|
|
|
return 0;
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2007-12-29 19:27:58 +00:00
|
|
|
int CPolyLine::GetContourSize( int icont )
|
|
|
|
{
|
2008-05-15 11:20:19 +00:00
|
|
|
return GetContourEnd( icont ) - GetContourStart( icont ) + 1;
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
int CPolyLine::GetClosed()
|
|
|
|
{
|
2013-05-09 19:08:12 +00:00
|
|
|
if( m_CornersList.GetCornersCount() == 0 )
|
2008-05-15 11:20:19 +00:00
|
|
|
return 0;
|
|
|
|
else
|
2013-05-09 19:08:12 +00:00
|
|
|
return m_CornersList[m_CornersList.GetCornersCount() - 1].end_contour;
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2012-01-13 18:35:46 +00:00
|
|
|
// Creates hatch lines inside the outline of the complex polygon
|
2007-12-29 19:27:58 +00:00
|
|
|
//
|
2012-07-13 18:55:29 +00:00
|
|
|
// sort function used in ::Hatch to sort points by descending wxPoint.x values
|
2012-07-25 18:46:25 +00:00
|
|
|
bool sort_ends_by_descending_X( const wxPoint& ref, const wxPoint& tst )
|
2012-01-13 18:35:46 +00:00
|
|
|
{
|
|
|
|
return tst.x < ref.x;
|
|
|
|
}
|
2012-07-25 18:46:25 +00:00
|
|
|
|
|
|
|
|
2007-12-29 19:27:58 +00:00
|
|
|
void CPolyLine::Hatch()
|
|
|
|
{
|
2008-05-15 11:20:19 +00:00
|
|
|
m_HatchLines.clear();
|
2012-01-10 20:12:46 +00:00
|
|
|
|
2012-04-11 09:47:57 +00:00
|
|
|
if( m_hatchStyle == NO_HATCH || m_hatchPitch == 0 )
|
2008-05-15 11:20:19 +00:00
|
|
|
return;
|
|
|
|
|
2012-07-25 18:46:25 +00:00
|
|
|
if( !GetClosed() ) // If not closed, the poly is beeing created and not finalised. Not not hatch
|
2010-11-17 18:41:20 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// define range for hatch lines
|
2012-07-25 18:46:25 +00:00
|
|
|
int min_x = m_CornersList[0].x;
|
|
|
|
int max_x = m_CornersList[0].x;
|
|
|
|
int min_y = m_CornersList[0].y;
|
|
|
|
int max_y = m_CornersList[0].y;
|
|
|
|
|
2013-05-09 19:08:12 +00:00
|
|
|
for( unsigned ic = 1; ic < m_CornersList.GetCornersCount(); ic++ )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2012-07-25 07:36:56 +00:00
|
|
|
if( m_CornersList[ic].x < min_x )
|
|
|
|
min_x = m_CornersList[ic].x;
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-07-25 07:36:56 +00:00
|
|
|
if( m_CornersList[ic].x > max_x )
|
|
|
|
max_x = m_CornersList[ic].x;
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-07-25 07:36:56 +00:00
|
|
|
if( m_CornersList[ic].y < min_y )
|
|
|
|
min_y = m_CornersList[ic].y;
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-07-25 07:36:56 +00:00
|
|
|
if( m_CornersList[ic].y > max_y )
|
|
|
|
max_y = m_CornersList[ic].y;
|
2010-11-17 18:41:20 +00:00
|
|
|
}
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2012-01-10 20:12:46 +00:00
|
|
|
// Calculate spacing betwwen 2 hatch lines
|
2012-07-25 18:46:25 +00:00
|
|
|
int spacing;
|
|
|
|
|
2012-04-11 09:47:57 +00:00
|
|
|
if( m_hatchStyle == DIAGONAL_EDGE )
|
|
|
|
spacing = m_hatchPitch;
|
2010-11-17 18:41:20 +00:00
|
|
|
else
|
2012-04-11 09:47:57 +00:00
|
|
|
spacing = m_hatchPitch * 2;
|
2012-04-10 16:28:26 +00:00
|
|
|
|
2013-05-04 11:57:09 +00:00
|
|
|
// set the "length" of hatch lines (the lenght on horizontal axis)
|
2012-07-25 18:46:25 +00:00
|
|
|
double hatch_line_len = m_hatchPitch;
|
2012-01-10 20:12:46 +00:00
|
|
|
|
|
|
|
// To have a better look, give a slope depending on the layer
|
2013-03-31 13:27:46 +00:00
|
|
|
LAYER_NUM layer = GetLayer();
|
2012-07-25 18:46:25 +00:00
|
|
|
int slope_flag = (layer & 1) ? 1 : -1; // 1 or -1
|
|
|
|
double slope = 0.707106 * slope_flag; // 45 degrees slope
|
|
|
|
int max_a, min_a;
|
|
|
|
|
2010-11-17 18:41:20 +00:00
|
|
|
if( slope_flag == 1 )
|
|
|
|
{
|
2013-05-04 11:57:09 +00:00
|
|
|
max_a = KiROUND( max_y - slope * min_x );
|
|
|
|
min_a = KiROUND( min_y - slope * max_x );
|
2010-11-17 18:41:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-04 11:57:09 +00:00
|
|
|
max_a = KiROUND( max_y - slope * max_x );
|
|
|
|
min_a = KiROUND( min_y - slope * min_x );
|
2010-11-17 18:41:20 +00:00
|
|
|
}
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2010-11-17 18:41:20 +00:00
|
|
|
min_a = (min_a / spacing) * spacing;
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2012-01-10 20:12:46 +00:00
|
|
|
// calculate an offset depending on layer number,
|
|
|
|
// for a better look of hatches on a multilayer board
|
2010-11-17 18:41:20 +00:00
|
|
|
int offset = (layer * 7) / 8;
|
|
|
|
min_a += offset;
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2010-11-17 18:41:20 +00:00
|
|
|
// now calculate and draw hatch lines
|
2013-05-09 19:08:12 +00:00
|
|
|
int nc = m_CornersList.GetCornersCount();
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2010-11-17 18:41:20 +00:00
|
|
|
// loop through hatch lines
|
2012-04-11 09:47:57 +00:00
|
|
|
#define MAXPTS 200 // Usually we store only few values per one hatch line
|
2012-01-10 20:12:46 +00:00
|
|
|
// depending on the compexity of the zone outline
|
2012-04-10 16:28:26 +00:00
|
|
|
|
2012-07-13 18:55:29 +00:00
|
|
|
static std::vector <wxPoint> pointbuffer;
|
2012-01-10 20:12:46 +00:00
|
|
|
pointbuffer.clear();
|
2012-07-25 18:46:25 +00:00
|
|
|
pointbuffer.reserve( MAXPTS + 2 );
|
2012-04-10 16:28:26 +00:00
|
|
|
|
2012-01-10 20:12:46 +00:00
|
|
|
for( int a = min_a; a < max_a; a += spacing )
|
2010-11-17 18:41:20 +00:00
|
|
|
{
|
|
|
|
// get intersection points for this hatch line
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2012-01-10 20:12:46 +00:00
|
|
|
// Note: because we should have an even number of intersections with the
|
2012-01-13 18:35:46 +00:00
|
|
|
// current hatch line and the zone outline (a closed polygon,
|
|
|
|
// or a set of closed polygons), if an odd count is found
|
2012-01-10 20:12:46 +00:00
|
|
|
// we skip this line (should not occur)
|
|
|
|
pointbuffer.clear();
|
|
|
|
int i_start_contour = 0;
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-01-10 20:12:46 +00:00
|
|
|
for( int ic = 0; ic<nc; ic++ )
|
2010-11-17 18:41:20 +00:00
|
|
|
{
|
2012-07-25 18:46:25 +00:00
|
|
|
double x, y, x2, y2;
|
|
|
|
int ok;
|
|
|
|
|
2013-05-09 19:08:12 +00:00
|
|
|
if( m_CornersList[ic].end_contour ||
|
|
|
|
( ic == (int) (m_CornersList.GetCornersCount() - 1) ) )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2012-01-10 20:12:46 +00:00
|
|
|
ok = FindLineSegmentIntersection( a, slope,
|
2012-07-25 18:46:25 +00:00
|
|
|
m_CornersList[ic].x, m_CornersList[ic].y,
|
|
|
|
m_CornersList[i_start_contour].x,
|
|
|
|
m_CornersList[i_start_contour].y,
|
|
|
|
&x, &y, &x2, &y2 );
|
2012-01-10 20:12:46 +00:00
|
|
|
i_start_contour = ic + 1;
|
2010-11-17 18:41:20 +00:00
|
|
|
}
|
2012-01-10 20:12:46 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ok = FindLineSegmentIntersection( a, slope,
|
2012-07-25 18:46:25 +00:00
|
|
|
m_CornersList[ic].x, m_CornersList[ic].y,
|
|
|
|
m_CornersList[ic + 1].x, m_CornersList[ic + 1].y,
|
|
|
|
&x, &y, &x2, &y2 );
|
2012-01-10 20:12:46 +00:00
|
|
|
}
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-01-10 20:12:46 +00:00
|
|
|
if( ok )
|
|
|
|
{
|
2013-05-04 11:57:09 +00:00
|
|
|
wxPoint point( KiROUND( x ), KiROUND( y ) );
|
2012-01-10 20:12:46 +00:00
|
|
|
pointbuffer.push_back( point );
|
|
|
|
}
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-01-10 20:12:46 +00:00
|
|
|
if( ok == 2 )
|
|
|
|
{
|
2013-05-04 11:57:09 +00:00
|
|
|
wxPoint point( KiROUND( x2 ), KiROUND( y2 ) );
|
2012-01-10 20:12:46 +00:00
|
|
|
pointbuffer.push_back( point );
|
|
|
|
}
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-01-10 20:12:46 +00:00
|
|
|
if( pointbuffer.size() >= MAXPTS ) // overflow
|
|
|
|
{
|
|
|
|
wxASSERT( 0 );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2012-01-10 20:12:46 +00:00
|
|
|
// ensure we have found an even intersection points count
|
2012-01-13 18:35:46 +00:00
|
|
|
// because intersections are the ends of segments
|
|
|
|
// inside the polygon(s) and a segment has 2 ends.
|
2012-01-10 20:12:46 +00:00
|
|
|
// if not, this is a strange case (a bug ?) so skip this hatch
|
|
|
|
if( pointbuffer.size() % 2 != 0 )
|
|
|
|
continue;
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2012-01-13 18:35:46 +00:00
|
|
|
// sort points in order of descending x (if more than 2) to
|
|
|
|
// ensure the starting point and the ending point of the same segment
|
|
|
|
// are stored one just after the other.
|
2012-01-10 20:12:46 +00:00
|
|
|
if( pointbuffer.size() > 2 )
|
2012-01-13 18:35:46 +00:00
|
|
|
sort( pointbuffer.begin(), pointbuffer.end(), sort_ends_by_descending_X );
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2012-01-13 18:35:46 +00:00
|
|
|
// creates lines or short segments inside the complex polygon
|
2012-01-10 20:12:46 +00:00
|
|
|
for( unsigned ip = 0; ip < pointbuffer.size(); ip += 2 )
|
2010-11-17 18:41:20 +00:00
|
|
|
{
|
2012-01-10 20:12:46 +00:00
|
|
|
double dx = pointbuffer[ip + 1].x - pointbuffer[ip].x;
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-01-10 20:12:46 +00:00
|
|
|
// Push only one line for diagonal hatch,
|
|
|
|
// or for small lines < twice the line len
|
|
|
|
// else push 2 small lines
|
2012-04-11 09:47:57 +00:00
|
|
|
if( m_hatchStyle == DIAGONAL_FULL || fabs( dx ) < 2 * hatch_line_len )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2012-07-13 18:55:29 +00:00
|
|
|
m_HatchLines.push_back( CSegment( pointbuffer[ip], pointbuffer[ip + 1] ) );
|
2010-11-17 18:41:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-25 18:46:25 +00:00
|
|
|
double dy = pointbuffer[ip + 1].y - pointbuffer[ip].y;
|
|
|
|
double slope = dy / dx;
|
2012-04-10 16:28:26 +00:00
|
|
|
|
2010-11-17 18:41:20 +00:00
|
|
|
if( dx > 0 )
|
2012-01-10 20:12:46 +00:00
|
|
|
dx = hatch_line_len;
|
2008-05-15 11:20:19 +00:00
|
|
|
else
|
2012-01-10 20:12:46 +00:00
|
|
|
dx = -hatch_line_len;
|
2012-04-10 16:28:26 +00:00
|
|
|
|
2012-07-25 18:46:25 +00:00
|
|
|
double x1 = pointbuffer[ip].x + dx;
|
|
|
|
double x2 = pointbuffer[ip + 1].x - dx;
|
|
|
|
double y1 = pointbuffer[ip].y + dx * slope;
|
|
|
|
double y2 = pointbuffer[ip + 1].y - dx * slope;
|
2012-04-10 16:28:26 +00:00
|
|
|
|
2012-01-10 20:12:46 +00:00
|
|
|
m_HatchLines.push_back( CSegment( pointbuffer[ip].x,
|
|
|
|
pointbuffer[ip].y,
|
2012-07-13 18:55:29 +00:00
|
|
|
KiROUND( x1 ), KiROUND( y1 ) ) );
|
2012-04-10 16:28:26 +00:00
|
|
|
|
2012-01-10 20:12:46 +00:00
|
|
|
m_HatchLines.push_back( CSegment( pointbuffer[ip + 1].x,
|
|
|
|
pointbuffer[ip + 1].y,
|
2012-07-13 18:55:29 +00:00
|
|
|
KiROUND( x2 ), KiROUND( y2 ) ) );
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
2008-05-30 18:06:21 +00:00
|
|
|
}
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2007-12-29 19:27:58 +00:00
|
|
|
// test to see if a point is inside polyline
|
|
|
|
//
|
2008-10-12 15:29:43 +00:00
|
|
|
bool CPolyLine::TestPointInside( int px, int py )
|
2007-12-29 19:27:58 +00:00
|
|
|
{
|
2008-05-15 11:20:19 +00:00
|
|
|
if( !GetClosed() )
|
2009-09-19 16:15:40 +00:00
|
|
|
{
|
2008-05-15 11:20:19 +00:00
|
|
|
wxASSERT( 0 );
|
2009-09-19 16:15:40 +00:00
|
|
|
}
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2010-10-28 13:02:07 +00:00
|
|
|
// Test all polygons.
|
2012-07-13 18:55:29 +00:00
|
|
|
// Since the first is the main outline, and other are holes,
|
2010-10-28 13:02:07 +00:00
|
|
|
// if the tested point is inside only one contour, it is inside the whole polygon
|
|
|
|
// (in fact inside the main outline, and outside all holes).
|
|
|
|
// if inside 2 contours (the main outline + an hole), it is outside the poly.
|
2012-07-25 18:46:25 +00:00
|
|
|
int polycount = GetContoursCount();
|
|
|
|
bool inside = false;
|
|
|
|
|
2010-10-28 13:02:07 +00:00
|
|
|
for( int icont = 0; icont < polycount; icont++ )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2012-07-25 18:46:25 +00:00
|
|
|
int istart = GetContourStart( icont );
|
|
|
|
int iend = GetContourEnd( icont );
|
2012-04-10 16:28:26 +00:00
|
|
|
|
2013-05-08 18:20:58 +00:00
|
|
|
// test point inside the current polygon
|
|
|
|
if( TestPointInsidePolygon( m_CornersList, istart, iend, px, py ) )
|
2010-10-28 13:02:07 +00:00
|
|
|
inside = not inside;
|
|
|
|
}
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2008-10-12 15:29:43 +00:00
|
|
|
return inside;
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2013-05-14 18:47:01 +00:00
|
|
|
// copy data from another CPolyLine, but don't draw it
|
|
|
|
void CPolyLine::Copy( const CPolyLine* src )
|
2007-12-29 19:27:58 +00:00
|
|
|
{
|
2012-01-13 18:35:46 +00:00
|
|
|
UnHatch();
|
2013-05-14 18:47:01 +00:00
|
|
|
m_layer = src->m_layer;
|
2012-07-25 18:46:25 +00:00
|
|
|
m_hatchStyle = src->m_hatchStyle;
|
|
|
|
m_hatchPitch = src->m_hatchPitch;
|
2013-05-14 18:47:01 +00:00
|
|
|
m_CornersList.RemoveAllContours();
|
|
|
|
m_CornersList.Append( src->m_CornersList );
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
2008-01-05 13:37:51 +00:00
|
|
|
|
|
|
|
/*
|
2013-05-14 18:47:01 +00:00
|
|
|
* return true if the corner aCornerIdx is on a hole inside the main outline
|
|
|
|
* and false if it is on the main outline
|
2008-01-05 13:37:51 +00:00
|
|
|
*/
|
2013-05-14 18:47:01 +00:00
|
|
|
bool CPolyLine::IsCutoutContour( int aCornerIdx )
|
2008-01-05 13:37:51 +00:00
|
|
|
{
|
2013-05-14 18:47:01 +00:00
|
|
|
int ncont = GetContour( aCornerIdx );
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2012-07-25 18:46:25 +00:00
|
|
|
if( ncont == 0 ) // the first contour is the main outline, not an hole
|
2008-05-15 11:20:19 +00:00
|
|
|
return false;
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
return true;
|
2008-01-05 13:37:51 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2007-12-29 19:27:58 +00:00
|
|
|
void CPolyLine::MoveOrigin( int x_off, int y_off )
|
|
|
|
{
|
2012-01-13 18:35:46 +00:00
|
|
|
UnHatch();
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2013-05-09 19:08:12 +00:00
|
|
|
for( int ic = 0; ic < GetCornersCount(); ic++ )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
|
|
|
SetX( ic, GetX( ic ) + x_off );
|
|
|
|
SetY( ic, GetY( ic ) + y_off );
|
|
|
|
}
|
|
|
|
|
2012-01-13 18:35:46 +00:00
|
|
|
Hatch();
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:40:25 +00:00
|
|
|
/*
|
2012-07-31 13:12:51 +00:00
|
|
|
* AppendArc:
|
|
|
|
* adds segments to current contour to approximate the given arc
|
2012-07-30 07:40:25 +00:00
|
|
|
*/
|
2008-05-15 11:20:19 +00:00
|
|
|
void CPolyLine::AppendArc( int xi, int yi, int xf, int yf, int xc, int yc, int num )
|
2007-12-29 19:27:58 +00:00
|
|
|
{
|
2008-05-15 11:20:19 +00:00
|
|
|
// get radius
|
2013-05-04 11:57:09 +00:00
|
|
|
double radius = ::Distance( xi, yi, xf, yf );
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2013-05-08 18:20:58 +00:00
|
|
|
// get angles of start pint and end point
|
2012-07-25 18:46:25 +00:00
|
|
|
double th_i = atan2( (double) (yi - yc), (double) (xi - xc) );
|
|
|
|
double th_f = atan2( (double) (yf - yc), (double) (xf - xc) );
|
|
|
|
double th_d = (th_f - th_i) / (num - 1);
|
|
|
|
double theta = th_i;
|
2008-05-15 11:20:19 +00:00
|
|
|
|
|
|
|
// generate arc
|
2012-07-30 07:40:25 +00:00
|
|
|
for( int ic = 0; ic < num; ic++ )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2013-05-01 17:32:36 +00:00
|
|
|
int x = xc + KiROUND( radius * cos( theta ) );
|
|
|
|
int y = yc + KiROUND( radius * sin( theta ) );
|
2012-07-31 13:12:51 +00:00
|
|
|
AppendCorner( x, y );
|
2008-05-15 11:20:19 +00:00
|
|
|
theta += th_d;
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:40:25 +00:00
|
|
|
CloseLastContour();
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
2009-06-25 20:45:27 +00:00
|
|
|
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2009-06-25 20:45:27 +00:00
|
|
|
// Bezier Support
|
2012-07-25 18:46:25 +00:00
|
|
|
void CPolyLine::AppendBezier( int x1, int y1, int x2, int y2, int x3, int y3 )
|
2012-07-13 18:55:29 +00:00
|
|
|
{
|
2009-06-25 20:45:27 +00:00
|
|
|
std::vector<wxPoint> bezier_points;
|
|
|
|
|
2012-07-25 18:46:25 +00:00
|
|
|
bezier_points = Bezier2Poly( x1, y1, x2, y2, x3, y3 );
|
|
|
|
|
|
|
|
for( unsigned int i = 0; i < bezier_points.size(); i++ )
|
|
|
|
AppendCorner( bezier_points[i].x, bezier_points[i].y );
|
2009-06-25 20:45:27 +00:00
|
|
|
}
|
|
|
|
|
2012-07-25 18:46:25 +00:00
|
|
|
|
|
|
|
void CPolyLine::AppendBezier( int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4 )
|
2012-07-13 18:55:29 +00:00
|
|
|
{
|
2009-06-25 20:45:27 +00:00
|
|
|
std::vector<wxPoint> bezier_points;
|
|
|
|
|
2012-07-25 18:46:25 +00:00
|
|
|
bezier_points = Bezier2Poly( x1, y1, x2, y2, x3, y3, x4, y4 );
|
|
|
|
|
|
|
|
for( unsigned int i = 0; i < bezier_points.size(); i++ )
|
|
|
|
AppendCorner( bezier_points[i].x, bezier_points[i].y );
|
2009-06-25 20:45:27 +00:00
|
|
|
}
|
2012-07-13 18:55:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Function Distance
|
|
|
|
* Calculates the distance between a segment and a polygon (with holes):
|
|
|
|
* param aStart is the starting point of the segment.
|
|
|
|
* param aEnd is the ending point of the segment.
|
|
|
|
* param aWidth is the width of the segment.
|
|
|
|
* return distance between the segment and outline.
|
|
|
|
* 0 if segment intersects or is inside
|
|
|
|
*/
|
|
|
|
int CPolyLine::Distance( wxPoint aStart, wxPoint aEnd, int aWidth )
|
|
|
|
{
|
|
|
|
// We calculate the min dist between the segment and each outline segment
|
|
|
|
// However, if the segment to test is inside the outline, and does not cross
|
|
|
|
// any edge, it can be seen outside the polygon.
|
|
|
|
// Therefore test if a segment end is inside ( testing only one end is enough )
|
|
|
|
if( TestPointInside( aStart.x, aStart.y ) )
|
|
|
|
return 0;
|
|
|
|
|
2012-07-25 18:46:25 +00:00
|
|
|
int distance = INT_MAX;
|
|
|
|
int polycount = GetContoursCount();
|
2012-07-13 18:55:29 +00:00
|
|
|
|
|
|
|
for( int icont = 0; icont < polycount; icont++ )
|
|
|
|
{
|
2012-07-25 18:46:25 +00:00
|
|
|
int ic_start = GetContourStart( icont );
|
|
|
|
int ic_end = GetContourEnd( icont );
|
2012-07-13 18:55:29 +00:00
|
|
|
|
|
|
|
// now test spacing between area outline and segment
|
|
|
|
for( int ic2 = ic_start; ic2 <= ic_end; ic2++ )
|
|
|
|
{
|
|
|
|
int bx1 = GetX( ic2 );
|
|
|
|
int by1 = GetY( ic2 );
|
|
|
|
int bx2, by2;
|
|
|
|
|
|
|
|
if( ic2 == ic_end )
|
|
|
|
{
|
|
|
|
bx2 = GetX( ic_start );
|
|
|
|
by2 = GetY( ic_start );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bx2 = GetX( ic2 + 1 );
|
|
|
|
by2 = GetY( ic2 + 1 );
|
|
|
|
}
|
|
|
|
|
2012-08-01 07:07:56 +00:00
|
|
|
int d = GetClearanceBetweenSegments( bx1, by1, bx2, by2, 0,
|
2012-07-13 18:55:29 +00:00
|
|
|
aStart.x, aStart.y, aEnd.x, aEnd.y,
|
2012-08-01 07:07:56 +00:00
|
|
|
aWidth,
|
2012-07-25 18:46:25 +00:00
|
|
|
1, // min clearance, should be > 0
|
2012-07-13 18:55:29 +00:00
|
|
|
NULL, NULL );
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-07-13 18:55:29 +00:00
|
|
|
if( distance > d )
|
|
|
|
distance = d;
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-07-13 18:55:29 +00:00
|
|
|
if( distance <= 0 )
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return distance;
|
|
|
|
}
|
|
|
|
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-07-13 18:55:29 +00:00
|
|
|
/*
|
|
|
|
* Function Distance
|
|
|
|
* Calculates the distance between a point and polygon (with holes):
|
|
|
|
* param aPoint is the coordinate of the point.
|
|
|
|
* return distance between the point and outline.
|
|
|
|
* 0 if the point is inside
|
|
|
|
*/
|
|
|
|
int CPolyLine::Distance( const wxPoint& aPoint )
|
|
|
|
{
|
|
|
|
// We calculate the dist between the point and each outline segment
|
|
|
|
// If the point is inside the outline, the dist is 0.
|
|
|
|
if( TestPointInside( aPoint.x, aPoint.y ) )
|
|
|
|
return 0;
|
|
|
|
|
2012-07-25 18:46:25 +00:00
|
|
|
int distance = INT_MAX;
|
|
|
|
int polycount = GetContoursCount();
|
2012-07-13 18:55:29 +00:00
|
|
|
|
|
|
|
for( int icont = 0; icont < polycount; icont++ )
|
|
|
|
{
|
2012-07-25 18:46:25 +00:00
|
|
|
int ic_start = GetContourStart( icont );
|
|
|
|
int ic_end = GetContourEnd( icont );
|
2012-07-13 18:55:29 +00:00
|
|
|
|
|
|
|
// now test spacing between area outline and segment
|
|
|
|
for( int ic2 = ic_start; ic2 <= ic_end; ic2++ )
|
|
|
|
{
|
|
|
|
int bx1 = GetX( ic2 );
|
|
|
|
int by1 = GetY( ic2 );
|
|
|
|
int bx2, by2;
|
|
|
|
|
|
|
|
if( ic2 == ic_end )
|
|
|
|
{
|
|
|
|
bx2 = GetX( ic_start );
|
|
|
|
by2 = GetY( ic_start );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bx2 = GetX( ic2 + 1 );
|
|
|
|
by2 = GetY( ic2 + 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int d = KiROUND( GetPointToLineSegmentDistance( aPoint.x, aPoint.y,
|
2012-07-25 18:46:25 +00:00
|
|
|
bx1, by1, bx2, by2 ) );
|
2012-07-13 18:55:29 +00:00
|
|
|
|
|
|
|
if( distance > d )
|
|
|
|
distance = d;
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-07-13 18:55:29 +00:00
|
|
|
if( distance <= 0 )
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return distance;
|
|
|
|
}
|
2012-07-25 18:46:25 +00:00
|
|
|
|
|
|
|
|
2013-05-08 18:20:58 +00:00
|
|
|
/*
|
|
|
|
* Copy the contours to a KI_POLYGON_WITH_HOLES
|
|
|
|
* The first contour is the main outline, others are holes
|
2012-07-25 18:46:25 +00:00
|
|
|
*/
|
2013-05-08 18:20:58 +00:00
|
|
|
void CPOLYGONS_LIST::ExportTo( KI_POLYGON_WITH_HOLES& aPolygoneWithHole )
|
2012-07-25 18:46:25 +00:00
|
|
|
{
|
2013-05-08 18:20:58 +00:00
|
|
|
unsigned corners_count = m_cornersList.size();
|
2012-07-25 18:46:25 +00:00
|
|
|
|
|
|
|
std::vector<KI_POLY_POINT> cornerslist;
|
|
|
|
KI_POLYGON poly;
|
|
|
|
|
|
|
|
// Enter main outline: this is the first contour
|
|
|
|
unsigned ic = 0;
|
|
|
|
|
|
|
|
while( ic < corners_count )
|
|
|
|
{
|
2013-05-08 18:20:58 +00:00
|
|
|
const CPolyPt& corner = GetCorner( ic++ );
|
2012-07-25 18:46:25 +00:00
|
|
|
cornerslist.push_back( KI_POLY_POINT( corner.x, corner.y ) );
|
|
|
|
|
|
|
|
if( corner.end_contour )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
aPolygoneWithHole.set( cornerslist.begin(), cornerslist.end() );
|
|
|
|
|
|
|
|
// Enter holes: they are next contours (when exist)
|
|
|
|
if( ic < corners_count )
|
|
|
|
{
|
|
|
|
KI_POLYGON_SET holePolyList;
|
|
|
|
|
|
|
|
while( ic < corners_count )
|
|
|
|
{
|
|
|
|
cornerslist.clear();
|
|
|
|
|
|
|
|
while( ic < corners_count )
|
|
|
|
{
|
2013-05-08 18:20:58 +00:00
|
|
|
cornerslist.push_back( KI_POLY_POINT( GetX( ic ), GetY( ic ) ) );
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2013-05-08 18:20:58 +00:00
|
|
|
if( IsEndContour( ic ) )
|
2012-07-25 18:46:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bpl::set_points( poly, cornerslist.begin(), cornerslist.end() );
|
|
|
|
holePolyList.push_back( poly );
|
|
|
|
}
|
|
|
|
|
|
|
|
aPolygoneWithHole.set_holes( holePolyList.begin(), holePolyList.end() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-08 18:20:58 +00:00
|
|
|
/**
|
|
|
|
* Copy all contours to a KI_POLYGON_SET aPolygons
|
|
|
|
* Each contour is copied into a KI_POLYGON, and each KI_POLYGON
|
|
|
|
* is append to aPolygons
|
|
|
|
*/
|
|
|
|
void CPOLYGONS_LIST::ExportTo( KI_POLYGON_SET& aPolygons )
|
|
|
|
{
|
|
|
|
std::vector<KI_POLY_POINT> cornerslist;
|
|
|
|
unsigned corners_count = GetCornersCount();
|
|
|
|
|
|
|
|
// Count the number of polygons in aCornersBuffer
|
|
|
|
int polycount = 0;
|
|
|
|
|
|
|
|
for( unsigned ii = 0; ii < corners_count; ii++ )
|
|
|
|
{
|
|
|
|
if( IsEndContour( ii ) )
|
|
|
|
polycount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
aPolygons.reserve( polycount );
|
|
|
|
|
|
|
|
for( unsigned icnt = 0; icnt < corners_count; )
|
|
|
|
{
|
|
|
|
KI_POLYGON poly;
|
|
|
|
cornerslist.clear();
|
|
|
|
|
|
|
|
unsigned ii;
|
|
|
|
|
|
|
|
for( ii = icnt; ii < corners_count; ii++ )
|
|
|
|
{
|
|
|
|
cornerslist.push_back( KI_POLY_POINT( GetX( ii ), GetY( ii ) ) );
|
|
|
|
|
|
|
|
if( IsEndContour( ii ) )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bpl::set_points( poly, cornerslist.begin(), cornerslist.end() );
|
|
|
|
aPolygons.push_back( poly );
|
|
|
|
icnt = ii + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Imports all polygons found in a KI_POLYGON_SET in list
|
|
|
|
*/
|
|
|
|
void CPOLYGONS_LIST::ImportFrom( KI_POLYGON_SET& aPolygons )
|
|
|
|
{
|
|
|
|
CPolyPt corner;
|
|
|
|
|
|
|
|
for( unsigned ii = 0; ii < aPolygons.size(); ii++ )
|
|
|
|
{
|
|
|
|
KI_POLYGON& poly = aPolygons[ii];
|
|
|
|
|
|
|
|
for( unsigned jj = 0; jj < poly.size(); jj++ )
|
|
|
|
{
|
|
|
|
KI_POLY_POINT point = *(poly.begin() + jj);
|
|
|
|
corner.x = point.x();
|
|
|
|
corner.y = point.y();
|
|
|
|
corner.end_contour = false;
|
|
|
|
AddCorner( corner );
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseLastContour();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-07-25 18:46:25 +00:00
|
|
|
/**
|
|
|
|
* Function ConvertPolysListWithHolesToOnePolygon
|
|
|
|
* converts the outline contours aPolysListWithHoles with holes to one polygon
|
|
|
|
* with no holes (only one contour)
|
|
|
|
* holes are linked to main outlines by overlap segments, to give only one polygon
|
|
|
|
*
|
|
|
|
* @param aPolysListWithHoles = the list of corners of contours (haing holes
|
|
|
|
* @param aOnePolyList = a polygon with no holes
|
|
|
|
*/
|
2013-05-03 17:51:10 +00:00
|
|
|
void ConvertPolysListWithHolesToOnePolygon( const CPOLYGONS_LIST& aPolysListWithHoles,
|
|
|
|
CPOLYGONS_LIST& aOnePolyList )
|
2012-07-25 18:46:25 +00:00
|
|
|
{
|
2013-05-09 19:08:12 +00:00
|
|
|
unsigned corners_count = aPolysListWithHoles.GetCornersCount();
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-08-02 13:23:53 +00:00
|
|
|
int polycount = 0;
|
2012-07-25 18:46:25 +00:00
|
|
|
for( unsigned ii = 0; ii < corners_count; ii++ )
|
|
|
|
{
|
2013-05-08 18:20:58 +00:00
|
|
|
if( aPolysListWithHoles.IsEndContour( ii ) )
|
2012-07-25 18:46:25 +00:00
|
|
|
polycount++;
|
|
|
|
}
|
|
|
|
|
2012-08-02 13:23:53 +00:00
|
|
|
// If polycount<= 1, there is no holes found, and therefore just copy the polygon.
|
|
|
|
if( polycount <= 1 )
|
2012-07-25 18:46:25 +00:00
|
|
|
{
|
|
|
|
aOnePolyList = aPolysListWithHoles;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Holes are found: convert them to only one polygon with overlap segments
|
|
|
|
KI_POLYGON_SET polysholes;
|
|
|
|
KI_POLYGON_SET mainpoly;
|
|
|
|
KI_POLYGON poly_tmp;
|
|
|
|
std::vector<KI_POLY_POINT> cornerslist;
|
2013-05-09 19:08:12 +00:00
|
|
|
corners_count = aPolysListWithHoles.GetCornersCount();
|
2012-07-25 18:46:25 +00:00
|
|
|
|
|
|
|
unsigned ic = 0;
|
|
|
|
// enter main outline
|
|
|
|
while( ic < corners_count )
|
|
|
|
{
|
2013-05-08 18:20:58 +00:00
|
|
|
const CPolyPt& corner = aPolysListWithHoles.GetCorner( ic++ );
|
2012-07-25 18:46:25 +00:00
|
|
|
cornerslist.push_back( KI_POLY_POINT( corner.x, corner.y ) );
|
|
|
|
|
|
|
|
if( corner.end_contour )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bpl::set_points( poly_tmp, cornerslist.begin(), cornerslist.end() );
|
|
|
|
mainpoly.push_back( poly_tmp );
|
|
|
|
|
|
|
|
while( ic < corners_count )
|
|
|
|
{
|
|
|
|
cornerslist.clear();
|
|
|
|
{
|
|
|
|
while( ic < corners_count )
|
|
|
|
{
|
2013-05-08 18:20:58 +00:00
|
|
|
const CPolyPt& corner = aPolysListWithHoles.GetCorner( ic++ );
|
2012-07-25 18:46:25 +00:00
|
|
|
cornerslist.push_back( KI_POLY_POINT( corner.x, corner.y ) );
|
|
|
|
|
|
|
|
if( corner.end_contour )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bpl::set_points( poly_tmp, cornerslist.begin(), cornerslist.end() );
|
|
|
|
polysholes.push_back( poly_tmp );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mainpoly -= polysholes;
|
|
|
|
|
|
|
|
// copy polygon with no holes to destination
|
2012-11-27 20:12:39 +00:00
|
|
|
// Because all holes are now linked to the main outline
|
|
|
|
// by overlapping segments, we should have only one polygon in list
|
|
|
|
wxASSERT( mainpoly.size() == 1 );
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-08-02 13:23:53 +00:00
|
|
|
KI_POLYGON& poly_nohole = mainpoly[0];
|
|
|
|
CPolyPt corner( 0, 0, false );
|
2012-07-25 18:46:25 +00:00
|
|
|
|
2012-08-02 13:23:53 +00:00
|
|
|
for( unsigned jj = 0; jj < poly_nohole.size(); jj++ )
|
|
|
|
{
|
|
|
|
KI_POLY_POINT point = *(poly_nohole.begin() + jj);
|
|
|
|
corner.x = point.x();
|
|
|
|
corner.y = point.y();
|
|
|
|
corner.end_contour = false;
|
2013-05-08 18:20:58 +00:00
|
|
|
aOnePolyList.AddCorner( corner );
|
2012-07-25 18:46:25 +00:00
|
|
|
}
|
2012-08-02 13:23:53 +00:00
|
|
|
|
2013-05-08 18:20:58 +00:00
|
|
|
aOnePolyList.CloseLastContour();
|
2012-07-25 18:46:25 +00:00
|
|
|
}
|
2012-07-31 13:12:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function IsPolygonSelfIntersecting
|
|
|
|
* Test a CPolyLine for self-intersection of vertex (all contours).
|
|
|
|
*
|
|
|
|
* @return :
|
|
|
|
* false if no intersecting sides
|
|
|
|
* true if intersecting sides
|
|
|
|
* When a CPolyLine is self intersectic, it need to be normalized.
|
|
|
|
* (converted to non intersecting polygons)
|
|
|
|
*/
|
|
|
|
bool CPolyLine::IsPolygonSelfIntersecting()
|
|
|
|
{
|
|
|
|
// first, check for sides intersecting other sides
|
|
|
|
int n_cont = GetContoursCount();
|
|
|
|
|
|
|
|
// make bounding rect for each contour
|
|
|
|
std::vector<CRect> cr;
|
|
|
|
cr.reserve( n_cont );
|
|
|
|
|
|
|
|
for( int icont = 0; icont<n_cont; icont++ )
|
2013-09-21 18:09:41 +00:00
|
|
|
cr.push_back( GetBoundingBox( icont ) );
|
2012-07-31 13:12:51 +00:00
|
|
|
|
|
|
|
for( int icont = 0; icont<n_cont; icont++ )
|
|
|
|
{
|
|
|
|
int is_start = GetContourStart( icont );
|
|
|
|
int is_end = GetContourEnd( icont );
|
|
|
|
|
|
|
|
for( int is = is_start; is<=is_end; is++ )
|
|
|
|
{
|
|
|
|
int is_prev = is - 1;
|
|
|
|
|
|
|
|
if( is_prev < is_start )
|
|
|
|
is_prev = is_end;
|
|
|
|
|
|
|
|
int is_next = is + 1;
|
|
|
|
|
|
|
|
if( is_next > is_end )
|
|
|
|
is_next = is_start;
|
|
|
|
|
|
|
|
int x1i = GetX( is );
|
|
|
|
int y1i = GetY( is );
|
|
|
|
int x1f = GetX( is_next );
|
|
|
|
int y1f = GetY( is_next );
|
|
|
|
|
|
|
|
// check for intersection with any other sides
|
|
|
|
for( int icont2 = icont; icont2<n_cont; icont2++ )
|
|
|
|
{
|
|
|
|
if( cr[icont].left > cr[icont2].right
|
|
|
|
|| cr[icont].bottom > cr[icont2].top
|
|
|
|
|| cr[icont2].left > cr[icont].right
|
|
|
|
|| cr[icont2].bottom > cr[icont].top )
|
|
|
|
{
|
|
|
|
// rectangles don't overlap, do nothing
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int is2_start = GetContourStart( icont2 );
|
|
|
|
int is2_end = GetContourEnd( icont2 );
|
|
|
|
|
|
|
|
for( int is2 = is2_start; is2<=is2_end; is2++ )
|
|
|
|
{
|
|
|
|
int is2_prev = is2 - 1;
|
|
|
|
|
|
|
|
if( is2_prev < is2_start )
|
|
|
|
is2_prev = is2_end;
|
|
|
|
|
|
|
|
int is2_next = is2 + 1;
|
|
|
|
|
|
|
|
if( is2_next > is2_end )
|
|
|
|
is2_next = is2_start;
|
|
|
|
|
|
|
|
if( icont != icont2
|
|
|
|
|| ( is2 != is && is2 != is_prev && is2 != is_next &&
|
|
|
|
is != is2_prev && is != is2_next )
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int x2i = GetX( is2 );
|
|
|
|
int y2i = GetY( is2 );
|
|
|
|
int x2f = GetX( is2_next );
|
|
|
|
int y2f = GetY( is2_next );
|
|
|
|
int ret = FindSegmentIntersections( x1i, y1i, x1f, y1f,
|
2012-08-01 07:07:56 +00:00
|
|
|
x2i, y2i, x2f, y2f );
|
2012-07-31 13:12:51 +00:00
|
|
|
if( ret )
|
|
|
|
{
|
|
|
|
// intersection between non-adjacent sides
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|