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
|
|
|
//
|
2008-05-30 18:06:21 +00:00
|
|
|
// implementation for kicad and kbool polygon clipping library
|
2007-12-29 19:27:58 +00:00
|
|
|
//
|
|
|
|
#include <math.h>
|
|
|
|
#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>
|
2007-12-29 19:27:58 +00:00
|
|
|
|
2012-04-10 16:28:26 +00:00
|
|
|
|
// 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
|
|
|
#define to_int( x ) KiROUND( (x) )
|
2009-05-21 17:42:42 +00:00
|
|
|
|
2012-01-10 20:12:46 +00:00
|
|
|
#ifndef MIN
|
|
|
|
#define MIN( x1, x2 ) ( (x1) > (x2) ? (x2) : (x1) )
|
|
|
|
#endif
|
|
|
|
#ifndef MAX
|
|
|
|
#define MAX( x1, x2 ) ( (x1) > (x2) ? (x1) : (x2) )
|
|
|
|
#endif
|
2007-12-29 19:27:58 +00:00
|
|
|
|
2009-06-25 20:45:27 +00:00
|
|
|
#define pi M_PI
|
2007-12-29 19:27:58 +00:00
|
|
|
|
|
|
|
CPolyLine::CPolyLine()
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2012-04-11 09:47:57 +00:00
|
|
|
m_hatchStyle = NO_HATCH;
|
|
|
|
m_hatchPitch = 0;
|
2008-05-15 11:20:19 +00:00
|
|
|
m_Width = 0;
|
|
|
|
utility = 0;
|
2008-05-30 18:06:21 +00:00
|
|
|
m_Kbool_Poly_Engine = NULL;
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
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();
|
2008-10-12 15:29:43 +00:00
|
|
|
if( m_Kbool_Poly_Engine )
|
2008-06-02 10:23:50 +00:00
|
|
|
delete m_Kbool_Poly_Engine;
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
2008-10-12 15:29:43 +00:00
|
|
|
|
2010-11-12 16:36:43 +00:00
|
|
|
/**
|
|
|
|
* Function NormalizeWithKbool
|
2008-05-30 18:06:21 +00:00
|
|
|
* Use the Kbool Library to clip contours: if outlines are crossing, the self-crossing polygon
|
|
|
|
* is converted to non self-crossing polygon by adding extra points at the crossing locations
|
2008-06-02 10:23:50 +00:00
|
|
|
* and reordering corners
|
2008-05-30 18:06:21 +00:00
|
|
|
* if more than one outside contour are found, extra CPolyLines will be created
|
|
|
|
* because copper areas have only one outside contour
|
|
|
|
* Therefore, if this results in new CPolyLines, return them as std::vector pa
|
2011-01-01 17:28:21 +00:00
|
|
|
* @param aExtraPolyList: pointer on a std::vector<CPolyLine*> to store extra CPolyLines
|
2012-01-22 17:20:22 +00:00
|
|
|
* @param bRetainArcs == true, try to retain arcs in polys
|
2008-05-30 18:06:21 +00:00
|
|
|
* @return number of external contours, or -1 if error
|
|
|
|
*/
|
2008-06-02 10:23:50 +00:00
|
|
|
int CPolyLine::NormalizeWithKbool( std::vector<CPolyLine*> * aExtraPolyList, bool bRetainArcs )
|
2007-12-29 19:27:58 +00:00
|
|
|
{
|
2008-05-30 18:06:21 +00:00
|
|
|
std::vector<CArc> arc_array;
|
|
|
|
std::vector <void*> hole_array; // list of holes
|
|
|
|
std::vector<int> * hole; // used to store corners for a given hole
|
|
|
|
CPolyLine* polyline;
|
|
|
|
int n_ext_cont = 0; // CPolyLine count
|
|
|
|
|
|
|
|
/* Creates a bool engine from this CPolyLine.
|
|
|
|
* Normalized outlines and holes will be in m_Kbool_Poly_Engine
|
|
|
|
* If some polygons are self crossing, after running the Kbool Engine, self crossing polygons
|
|
|
|
* will be converted in non self crossing polygons by inserting extra points at the crossing locations
|
|
|
|
* True holes are combined if possible
|
|
|
|
*/
|
2008-05-15 11:20:19 +00:00
|
|
|
if( bRetainArcs )
|
2008-05-30 18:06:21 +00:00
|
|
|
MakeKboolPoly( -1, -1, &arc_array );
|
2008-05-15 11:20:19 +00:00
|
|
|
else
|
2008-05-30 18:06:21 +00:00
|
|
|
MakeKboolPoly( -1, -1, NULL );
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2012-01-13 18:35:46 +00:00
|
|
|
UnHatch();
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2008-05-30 18:06:21 +00:00
|
|
|
/* now, recreate polys
|
|
|
|
* if more than one outside contour are found, extra CPolyLines will be created
|
|
|
|
* because copper areas have only one outside contour
|
|
|
|
* the first outside contour found is the new "this" outside contour
|
|
|
|
* if others outside contours are found we create new CPolyLines
|
|
|
|
* Note: if there are holes in polygons, we must store them
|
|
|
|
* and when all outside contours are found, search the corresponding outside contour for each hole
|
|
|
|
*/
|
|
|
|
while( m_Kbool_Poly_Engine->StartPolygonGet() )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2008-05-30 18:06:21 +00:00
|
|
|
// See if the current polygon is flagged as a hole
|
|
|
|
if( m_Kbool_Poly_Engine->GetPolygonPointEdgeType() == KB_INSIDE_EDGE )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2008-05-30 18:06:21 +00:00
|
|
|
hole = new std::vector<int>;
|
|
|
|
hole_array.push_back( hole );
|
|
|
|
while( m_Kbool_Poly_Engine->PolygonHasMorePoints() ) // store hole
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2008-10-12 15:29:43 +00:00
|
|
|
int x = (int) m_Kbool_Poly_Engine->GetPolygonXPoint();
|
|
|
|
int y = (int) m_Kbool_Poly_Engine->GetPolygonYPoint();
|
2008-05-30 18:06:21 +00:00
|
|
|
hole->push_back( x );
|
|
|
|
hole->push_back( y );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Kbool_Poly_Engine->EndPolygonGet();
|
|
|
|
}
|
|
|
|
else if( n_ext_cont == 0 )
|
|
|
|
{
|
|
|
|
// first external contour, replace this poly
|
|
|
|
corner.clear();
|
|
|
|
side_style.clear();
|
|
|
|
bool first = true;
|
|
|
|
while( m_Kbool_Poly_Engine->PolygonHasMorePoints() )
|
2012-04-10 16:28:26 +00:00
|
|
|
{
|
|
|
|
// foreach point in the polygon
|
2008-10-12 15:29:43 +00:00
|
|
|
int x = (int) m_Kbool_Poly_Engine->GetPolygonXPoint();
|
|
|
|
int y = (int) m_Kbool_Poly_Engine->GetPolygonYPoint();
|
2008-05-30 18:06:21 +00:00
|
|
|
if( first )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2008-05-30 18:06:21 +00:00
|
|
|
first = false;
|
|
|
|
Start( GetLayer(), x, y, GetHatchStyle() );
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
2008-05-30 18:06:21 +00:00
|
|
|
else
|
|
|
|
AppendCorner( x, y );
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
2008-05-30 18:06:21 +00:00
|
|
|
|
|
|
|
m_Kbool_Poly_Engine->EndPolygonGet();
|
|
|
|
Close();
|
|
|
|
n_ext_cont++;
|
|
|
|
}
|
2008-06-02 10:23:50 +00:00
|
|
|
else if( aExtraPolyList ) // a new outside contour is found: create a new CPolyLine
|
2008-05-30 18:06:21 +00:00
|
|
|
{
|
2008-10-12 15:29:43 +00:00
|
|
|
polyline = new CPolyLine; // create new poly
|
2008-06-02 10:23:50 +00:00
|
|
|
aExtraPolyList->push_back( polyline ); // put it in array
|
2008-05-30 18:06:21 +00:00
|
|
|
bool first = true;
|
2008-10-12 15:29:43 +00:00
|
|
|
while( m_Kbool_Poly_Engine->PolygonHasMorePoints() ) // read next external contour
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2008-10-12 15:29:43 +00:00
|
|
|
int x = (int) m_Kbool_Poly_Engine->GetPolygonXPoint();
|
|
|
|
int y = (int) m_Kbool_Poly_Engine->GetPolygonYPoint();
|
2008-05-30 18:06:21 +00:00
|
|
|
if( first )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2008-05-30 18:06:21 +00:00
|
|
|
first = false;
|
|
|
|
polyline->Start( GetLayer(), x, y, GetHatchStyle() );
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
2008-05-30 18:06:21 +00:00
|
|
|
else
|
|
|
|
polyline->AppendCorner( x, y );
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
2008-05-30 18:06:21 +00:00
|
|
|
|
|
|
|
m_Kbool_Poly_Engine->EndPolygonGet();
|
2012-01-22 17:20:22 +00:00
|
|
|
polyline->Close( STRAIGHT, false );
|
2008-05-30 18:06:21 +00:00
|
|
|
n_ext_cont++;
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 18:06:21 +00:00
|
|
|
// now add cutouts to the corresponding CPolyLine(s)
|
|
|
|
for( unsigned ii = 0; ii < hole_array.size(); ii++ )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2008-05-30 18:06:21 +00:00
|
|
|
hole = (std::vector<int> *)hole_array[ii];
|
|
|
|
polyline = NULL;
|
|
|
|
if( n_ext_cont == 1 )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2008-05-30 18:06:21 +00:00
|
|
|
polyline = this;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// find the polygon that contains this hole
|
|
|
|
// testing one corner inside is enought because a hole is entirely inside the polygon
|
2010-11-17 18:41:20 +00:00
|
|
|
// so we test only the first corner
|
2008-05-30 18:06:21 +00:00
|
|
|
int x = (*hole)[0];
|
|
|
|
int y = (*hole)[1];
|
|
|
|
if( TestPointInside( x, y ) )
|
|
|
|
polyline = this;
|
2008-06-02 10:23:50 +00:00
|
|
|
else if( aExtraPolyList )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2008-05-30 18:06:21 +00:00
|
|
|
for( int ext_ic = 0; ext_ic<n_ext_cont - 1; ext_ic++ )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2008-06-02 10:23:50 +00:00
|
|
|
if( (*aExtraPolyList)[ext_ic]->TestPointInside( x, y ) )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2008-06-02 10:23:50 +00:00
|
|
|
polyline = (*aExtraPolyList)[ext_ic];
|
2008-05-15 11:20:19 +00:00
|
|
|
break;
|
2008-05-30 18:06:21 +00:00
|
|
|
}
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-30 18:06:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( !polyline )
|
|
|
|
wxASSERT( 0 );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( unsigned ii = 0; ii< (*hole).size(); ii++ )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2008-05-30 18:06:21 +00:00
|
|
|
int x = (*hole)[ii]; ii++;
|
|
|
|
int y = (*hole)[ii];
|
2012-01-22 17:20:22 +00:00
|
|
|
polyline->AppendCorner( x, y, STRAIGHT, false );
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
|
|
|
|
2012-01-22 17:20:22 +00:00
|
|
|
polyline->Close( STRAIGHT, false );
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( bRetainArcs )
|
2008-06-02 10:23:50 +00:00
|
|
|
RestoreArcs( &arc_array, aExtraPolyList );
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2008-05-30 18:06:21 +00:00
|
|
|
delete m_Kbool_Poly_Engine;
|
|
|
|
m_Kbool_Poly_Engine = NULL;
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2008-05-30 18:06:21 +00:00
|
|
|
// free hole list
|
|
|
|
for( unsigned ii = 0; ii < hole_array.size(); ii++ )
|
|
|
|
delete (std::vector<int> *)hole_array[ii];
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2008-05-30 18:06:21 +00:00
|
|
|
return n_ext_cont;
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2010-11-12 16:36:43 +00:00
|
|
|
/**
|
|
|
|
* Function AddPolygonsToBoolEng
|
2008-10-02 13:24:31 +00:00
|
|
|
* Add a CPolyLine to a kbool engine, preparing a boolean op between polygons
|
2008-05-30 18:06:21 +00:00
|
|
|
* @param aStart_contour: starting contour number (-1 = all, 0 is the outlines of zone, > 1 = holes in zone
|
|
|
|
* @param aEnd_contour: ending contour number (-1 = all after aStart_contour)
|
|
|
|
* @param arc_array: arc converted to poly segments (NULL if not exists)
|
|
|
|
* @param aBooleng : pointer on a bool engine (handle a set of polygons)
|
|
|
|
* @param aGroup : group to fill (aGroup = GROUP_A or GROUP_B) operations are made between GROUP_A and GROUP_B
|
|
|
|
*/
|
|
|
|
int CPolyLine::AddPolygonsToBoolEng( Bool_Engine* aBooleng,
|
|
|
|
GroupType aGroup,
|
|
|
|
int aStart_contour,
|
|
|
|
int aEnd_contour,
|
|
|
|
std::vector<CArc> * arc_array )
|
2007-12-29 19:27:58 +00:00
|
|
|
{
|
2008-05-30 18:06:21 +00:00
|
|
|
int count = 0;
|
2007-12-29 19:27:58 +00:00
|
|
|
|
2008-05-30 18:06:21 +00:00
|
|
|
if( (aGroup != GROUP_A) && (aGroup != GROUP_B ) )
|
|
|
|
return 0; //Error !
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2008-10-02 13:24:31 +00:00
|
|
|
/* Convert the current polyline contour to a kbool polygon: */
|
2008-05-30 18:06:21 +00:00
|
|
|
MakeKboolPoly( aStart_contour, aEnd_contour, arc_array );
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2008-10-02 13:24:31 +00:00
|
|
|
/* add the resulting kbool set of polygons to the current kcool engine */
|
2008-05-30 18:06:21 +00:00
|
|
|
while( m_Kbool_Poly_Engine->StartPolygonGet() )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2008-05-30 18:06:21 +00:00
|
|
|
if( aBooleng->StartPolygonAdd( GROUP_A ) )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2008-05-30 18:06:21 +00:00
|
|
|
while( m_Kbool_Poly_Engine->PolygonHasMorePoints() )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2008-10-12 15:29:43 +00:00
|
|
|
int x = (int) m_Kbool_Poly_Engine->GetPolygonXPoint();
|
|
|
|
int y = (int) m_Kbool_Poly_Engine->GetPolygonYPoint();
|
2008-05-30 18:06:21 +00:00
|
|
|
aBooleng->AddPoint( x, y );
|
|
|
|
count++;
|
|
|
|
}
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2008-05-30 18:06:21 +00:00
|
|
|
aBooleng->EndPolygonAdd();
|
|
|
|
}
|
|
|
|
m_Kbool_Poly_Engine->EndPolygonGet();
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 18:06:21 +00:00
|
|
|
delete m_Kbool_Poly_Engine;
|
|
|
|
m_Kbool_Poly_Engine = NULL;
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2008-05-30 18:06:21 +00:00
|
|
|
return count;
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-12 16:36:43 +00:00
|
|
|
/**
|
|
|
|
* Function MakeKboolPoly
|
2008-05-30 18:06:21 +00:00
|
|
|
* fill a kbool engine with a closed polyline contour
|
2008-05-15 11:20:19 +00:00
|
|
|
* approximates arcs with multiple straight-line segments
|
2008-05-30 18:06:21 +00:00
|
|
|
* @param aStart_contour: starting contour number (-1 = all, 0 is the outlines of zone, > 1 = holes in zone
|
|
|
|
* @param aEnd_contour: ending contour number (-1 = all after aStart_contour)
|
2008-05-15 11:20:19 +00:00
|
|
|
* combining intersecting contours if possible
|
2008-05-30 18:06:21 +00:00
|
|
|
* @param arc_array : return corners computed from arcs approximations in arc_array
|
2008-09-26 19:51:36 +00:00
|
|
|
* @param aConvertHoles = mode for holes when a boolean operation is made
|
|
|
|
* true: holes are linked into outer contours by double overlapping segments
|
|
|
|
* false: holes are not linked: in this mode contours are added clockwise
|
|
|
|
* and polygons added counter clockwise are holes (default)
|
2008-05-15 11:20:19 +00:00
|
|
|
* @return error: 0 if Ok, 1 if error
|
|
|
|
*/
|
2008-09-26 19:51:36 +00:00
|
|
|
int CPolyLine::MakeKboolPoly( int aStart_contour, int aEnd_contour, std::vector<CArc> * arc_array,
|
2008-10-12 15:29:43 +00:00
|
|
|
bool aConvertHoles )
|
2007-12-29 19:27:58 +00:00
|
|
|
{
|
2008-05-30 18:06:21 +00:00
|
|
|
if( m_Kbool_Poly_Engine )
|
|
|
|
{
|
|
|
|
delete m_Kbool_Poly_Engine;
|
|
|
|
m_Kbool_Poly_Engine = NULL;
|
|
|
|
}
|
|
|
|
if( !GetClosed() && (aStart_contour == (GetNumContours() - 1) || aStart_contour == -1) )
|
2008-05-15 11:20:19 +00:00
|
|
|
return 1; // error
|
|
|
|
|
|
|
|
int n_arcs = 0;
|
|
|
|
|
2008-05-30 18:06:21 +00:00
|
|
|
int first_contour = aStart_contour;
|
|
|
|
int last_contour = aEnd_contour;
|
|
|
|
if( aStart_contour == -1 )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
|
|
|
first_contour = 0;
|
|
|
|
last_contour = GetNumContours() - 1;
|
|
|
|
}
|
2008-05-30 18:06:21 +00:00
|
|
|
if( aEnd_contour == -1 )
|
|
|
|
{
|
|
|
|
last_contour = GetNumContours() - 1;
|
|
|
|
}
|
2008-05-15 11:20:19 +00:00
|
|
|
if( arc_array )
|
|
|
|
arc_array->clear();
|
|
|
|
int iarc = 0;
|
|
|
|
for( int icont = first_contour; icont<=last_contour; icont++ )
|
|
|
|
{
|
2008-05-30 18:06:21 +00:00
|
|
|
// Fill a kbool engine for this contour,
|
|
|
|
// and combine it with previous contours
|
|
|
|
Bool_Engine* booleng = new Bool_Engine();
|
2008-09-27 19:26:29 +00:00
|
|
|
ArmBoolEng( booleng, aConvertHoles );
|
2008-05-30 18:06:21 +00:00
|
|
|
|
|
|
|
if( m_Kbool_Poly_Engine ) // a previous contour exists. Put it in new engine
|
|
|
|
{
|
|
|
|
while( m_Kbool_Poly_Engine->StartPolygonGet() )
|
|
|
|
{
|
|
|
|
if( booleng->StartPolygonAdd( GROUP_A ) )
|
|
|
|
{
|
|
|
|
while( m_Kbool_Poly_Engine->PolygonHasMorePoints() )
|
|
|
|
{
|
2008-10-12 15:29:43 +00:00
|
|
|
int x = (int) m_Kbool_Poly_Engine->GetPolygonXPoint();
|
|
|
|
int y = (int) m_Kbool_Poly_Engine->GetPolygonYPoint();
|
2008-05-30 18:06:21 +00:00
|
|
|
booleng->AddPoint( x, y );
|
|
|
|
}
|
|
|
|
|
|
|
|
booleng->EndPolygonAdd();
|
|
|
|
}
|
|
|
|
m_Kbool_Poly_Engine->EndPolygonGet();
|
|
|
|
}
|
|
|
|
}
|
2008-05-15 11:20:19 +00:00
|
|
|
|
|
|
|
// first, calculate number of vertices in contour
|
|
|
|
int n_vertices = 0;
|
|
|
|
int ic_st = GetContourStart( icont );
|
|
|
|
int ic_end = GetContourEnd( icont );
|
2008-05-30 18:06:21 +00:00
|
|
|
if( !booleng->StartPolygonAdd( GROUP_B ) )
|
|
|
|
{
|
|
|
|
wxASSERT( 0 );
|
|
|
|
return 1; //error
|
|
|
|
}
|
2008-05-15 11:20:19 +00:00
|
|
|
for( int ic = ic_st; ic<=ic_end; ic++ )
|
|
|
|
{
|
|
|
|
int style = side_style[ic];
|
|
|
|
int x1 = corner[ic].x;
|
|
|
|
int y1 = corner[ic].y;
|
|
|
|
int x2, y2;
|
|
|
|
if( ic < ic_end )
|
|
|
|
{
|
|
|
|
x2 = corner[ic + 1].x;
|
|
|
|
y2 = corner[ic + 1].y;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
x2 = corner[ic_st].x;
|
|
|
|
y2 = corner[ic_st].y;
|
|
|
|
}
|
|
|
|
if( style == STRAIGHT )
|
|
|
|
n_vertices++;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// style is ARC_CW or ARC_CCW
|
2012-04-25 19:33:24 +00:00
|
|
|
int n = CArc::ARC_STEPS;
|
2008-05-15 11:20:19 +00:00
|
|
|
n_vertices += n;
|
|
|
|
n_arcs++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 18:06:21 +00:00
|
|
|
// now enter this contour to booleng
|
2008-05-15 11:20:19 +00:00
|
|
|
int ivtx = 0;
|
|
|
|
for( int ic = ic_st; ic<=ic_end; ic++ )
|
|
|
|
{
|
|
|
|
int style = side_style[ic];
|
|
|
|
int x1 = corner[ic].x;
|
|
|
|
int y1 = corner[ic].y;
|
|
|
|
int x2, y2;
|
|
|
|
if( ic < ic_end )
|
|
|
|
{
|
|
|
|
x2 = corner[ic + 1].x;
|
|
|
|
y2 = corner[ic + 1].y;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
x2 = corner[ic_st].x;
|
|
|
|
y2 = corner[ic_st].y;
|
|
|
|
}
|
|
|
|
if( style == STRAIGHT )
|
|
|
|
{
|
2008-05-30 18:06:21 +00:00
|
|
|
booleng->AddPoint( x1, y1 );
|
2008-05-15 11:20:19 +00:00
|
|
|
ivtx++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// style is arc_cw or arc_ccw
|
|
|
|
int n; // number of steps for arcs
|
2012-04-25 19:33:24 +00:00
|
|
|
n = CArc::ARC_STEPS;
|
2008-05-15 11:20:19 +00:00
|
|
|
double xo, yo, theta1, theta2, a, b;
|
|
|
|
a = fabs( (double) (x1 - x2) );
|
|
|
|
b = fabs( (double) (y1 - y2) );
|
|
|
|
if( style == CPolyLine::ARC_CW )
|
|
|
|
{
|
|
|
|
// clockwise arc (ie.quadrant of ellipse)
|
|
|
|
if( x2 > x1 && y2 > y1 )
|
|
|
|
{
|
|
|
|
// first quadrant, draw second quadrant of ellipse
|
|
|
|
xo = x2;
|
|
|
|
yo = y1;
|
|
|
|
theta1 = pi;
|
|
|
|
theta2 = pi / 2.0;
|
|
|
|
}
|
|
|
|
else if( x2 < x1 && y2 > y1 )
|
|
|
|
{
|
|
|
|
// second quadrant, draw third quadrant of ellipse
|
|
|
|
xo = x1;
|
|
|
|
yo = y2;
|
|
|
|
theta1 = 3.0 * pi / 2.0;
|
|
|
|
theta2 = pi;
|
|
|
|
}
|
|
|
|
else if( x2 < x1 && y2 < y1 )
|
|
|
|
{
|
|
|
|
// third quadrant, draw fourth quadrant of ellipse
|
|
|
|
xo = x2;
|
|
|
|
yo = y1;
|
|
|
|
theta1 = 2.0 * pi;
|
|
|
|
theta2 = 3.0 * pi / 2.0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xo = x1; // fourth quadrant, draw first quadrant of ellipse
|
|
|
|
yo = y2;
|
|
|
|
theta1 = pi / 2.0;
|
|
|
|
theta2 = 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// counter-clockwise arc
|
|
|
|
if( x2 > x1 && y2 > y1 )
|
|
|
|
{
|
|
|
|
xo = x1; // first quadrant, draw fourth quadrant of ellipse
|
|
|
|
yo = y2;
|
|
|
|
theta1 = 3.0 * pi / 2.0;
|
|
|
|
theta2 = 2.0 * pi;
|
|
|
|
}
|
|
|
|
else if( x2 < x1 && y2 > y1 )
|
|
|
|
{
|
|
|
|
xo = x2; // second quadrant
|
|
|
|
yo = y1;
|
|
|
|
theta1 = 0.0;
|
|
|
|
theta2 = pi / 2.0;
|
|
|
|
}
|
|
|
|
else if( x2 < x1 && y2 < y1 )
|
|
|
|
{
|
|
|
|
xo = x1; // third quadrant
|
|
|
|
yo = y2;
|
|
|
|
theta1 = pi / 2.0;
|
|
|
|
theta2 = pi;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xo = x2; // fourth quadrant
|
|
|
|
yo = y1;
|
|
|
|
theta1 = pi;
|
|
|
|
theta2 = 3.0 * pi / 2.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now write steps for arc
|
|
|
|
if( arc_array )
|
|
|
|
{
|
|
|
|
CArc new_arc;
|
|
|
|
new_arc.style = style;
|
|
|
|
new_arc.n_steps = n;
|
|
|
|
new_arc.xi = x1;
|
|
|
|
new_arc.yi = y1;
|
|
|
|
new_arc.xf = x2;
|
|
|
|
new_arc.yf = y2;
|
|
|
|
arc_array->push_back( new_arc );
|
|
|
|
iarc++;
|
|
|
|
}
|
|
|
|
for( int is = 0; is<n; is++ )
|
|
|
|
{
|
|
|
|
double theta = theta1 + ( (theta2 - theta1) * (double) is ) / n;
|
|
|
|
double x = xo + a* cos( theta );
|
|
|
|
double y = yo + b* sin( theta );
|
|
|
|
if( is == 0 )
|
|
|
|
{
|
|
|
|
x = x1;
|
|
|
|
y = y1;
|
|
|
|
}
|
2011-12-22 08:07:50 +00:00
|
|
|
booleng->AddPoint( x, y );
|
2008-05-15 11:20:19 +00:00
|
|
|
ivtx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( n_vertices != ivtx )
|
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
|
|
|
|
2008-05-30 18:06:21 +00:00
|
|
|
// close list added to the bool engine
|
|
|
|
booleng->EndPolygonAdd();
|
|
|
|
|
|
|
|
/* now combine polygon to the previous polygons.
|
|
|
|
* note: the first polygon is the outline contour, and others are holes inside the first polygon
|
|
|
|
* The first polygon is ORed with nothing, but is is a trick to sort corners (vertex)
|
|
|
|
* clockwise with the kbool engine.
|
|
|
|
* Others polygons are substract to the outline and corners will be ordered counter clockwise
|
|
|
|
* by the kbool engine
|
|
|
|
*/
|
|
|
|
if( aStart_contour <= 0 && icont != 0 ) // substract hole to outside ( if the outline contour is take in account)
|
|
|
|
{
|
|
|
|
booleng->Do_Operation( BOOL_A_SUB_B );
|
|
|
|
}
|
|
|
|
else // add outside or add holes if we do not use the outline contour
|
|
|
|
{
|
|
|
|
booleng->Do_Operation( BOOL_OR );
|
|
|
|
}
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2008-09-26 19:51:36 +00:00
|
|
|
// now use result as new polygon (delete the old one if exists)
|
2008-05-30 18:06:21 +00:00
|
|
|
if( m_Kbool_Poly_Engine )
|
|
|
|
delete m_Kbool_Poly_Engine;
|
|
|
|
m_Kbool_Poly_Engine = booleng;
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2010-11-12 16:36:43 +00:00
|
|
|
/**
|
|
|
|
* Function ArmBoolEng
|
2008-05-30 18:06:21 +00:00
|
|
|
* Initialise parameters used in kbool
|
|
|
|
* @param aBooleng = pointer to the Bool_Engine to initialise
|
|
|
|
* @param aConvertHoles = mode for holes when a boolean operation is made
|
2008-12-03 19:45:57 +00:00
|
|
|
* true: in resulting polygon, holes are linked into outer contours by double overlapping segments
|
|
|
|
* false: in resulting polygons, holes are not linked: they are separate polygons
|
2008-05-30 18:06:21 +00:00
|
|
|
*/
|
|
|
|
void ArmBoolEng( Bool_Engine* aBooleng, bool aConvertHoles )
|
2007-12-29 19:27:58 +00:00
|
|
|
{
|
2008-05-30 18:06:21 +00:00
|
|
|
// set some global vals to arm the boolean engine
|
2008-11-09 15:01:35 +00:00
|
|
|
|
|
|
|
// input points are scaled up with GetDGrid() * GetGrid()
|
|
|
|
|
|
|
|
// DGRID is only meant to make fractional parts of input data which
|
|
|
|
/*
|
|
|
|
The input data scaled up with DGrid is related to the accuracy the user has in his input data.
|
|
|
|
User data with a minimum accuracy of 0.001, means set the DGrid to 1000.
|
|
|
|
The input data may contain data with a minimum accuracy much smaller, but by setting the DGrid
|
|
|
|
everything smaller than 1/DGrid is rounded.
|
|
|
|
|
|
|
|
DGRID is only meant to make fractional parts of input data which can be
|
|
|
|
doubles, part of the integers used in vertexes within the boolean algorithm.
|
|
|
|
And therefore DGRID bigger than 1 is not usefull, you would only loose accuracy.
|
|
|
|
Within the algorithm all input data is multiplied with DGRID, and the result
|
|
|
|
is rounded to an integer.
|
|
|
|
*/
|
2009-11-16 12:10:37 +00:00
|
|
|
double DGRID = 1000.0; // round coordinate X or Y value in calculations to this (initial value = 1000.0 in kbool example)
|
2009-09-17 17:48:40 +00:00
|
|
|
// kbool uses DGRID to convert float user units to integer
|
|
|
|
// kbool unit = (int)(user unit * DGRID)
|
2009-09-10 13:04:04 +00:00
|
|
|
// Note: in kicad, coordinates are already integer so DGRID could be set to 1
|
2009-11-16 12:10:37 +00:00
|
|
|
// we can choose 1.0,
|
|
|
|
// but choose DGRID = 1000.0 solves some filling problems
|
|
|
|
// (perhaps because this allows a better precision in kbool internal calculations
|
2009-09-10 13:04:04 +00:00
|
|
|
|
2009-11-16 12:10:37 +00:00
|
|
|
double MARGE = 1.0/DGRID; // snap with in this range points to lines in the intersection routines
|
2009-09-17 17:48:40 +00:00
|
|
|
// should always be >= 1/DGRID a MARGE >= 10/DGRID is ok
|
2009-09-10 13:04:04 +00:00
|
|
|
// this is also used to remove small segments and to decide when
|
|
|
|
// two segments are in line. ( initial value = 0.001 )
|
2009-11-16 12:10:37 +00:00
|
|
|
// For kicad we choose MARGE = 1/DGRID
|
2008-11-09 15:01:35 +00:00
|
|
|
|
2008-11-23 17:43:53 +00:00
|
|
|
double CORRECTIONFACTOR = 0.0; // correct the polygons by this number: used in BOOL_CORRECTION operation
|
|
|
|
// this operation shrinks a polygon if CORRECTIONFACTOR < 0
|
|
|
|
// or stretch it if CORRECTIONFACTOR > 0
|
|
|
|
// the size change is CORRECTIONFACTOR (holes are correctly handled)
|
2008-05-30 18:06:21 +00:00
|
|
|
double CORRECTIONABER = 1.0; // the accuracy for the rounded shapes used in correction
|
|
|
|
double ROUNDFACTOR = 1.5; // when will we round the correction shape to a circle
|
|
|
|
double SMOOTHABER = 10.0; // accuracy when smoothing a polygon
|
|
|
|
double MAXLINEMERGE = 1000.0; // leave as is, segments of this length in smoothen
|
|
|
|
|
|
|
|
|
2008-11-09 15:01:35 +00:00
|
|
|
/*
|
2010-11-12 16:36:43 +00:00
|
|
|
Grid makes sure that the integer data used within the algorithm has room for extra intersections
|
|
|
|
smaller than the smallest number within the input data.
|
|
|
|
The input data scaled up with DGrid is related to the accuracy the user has in his input data.
|
2008-11-09 15:01:35 +00:00
|
|
|
Another scaling with Grid is applied on top of it to create space in the integer number for
|
2010-11-12 16:36:43 +00:00
|
|
|
even smaller numbers.
|
2008-11-09 15:01:35 +00:00
|
|
|
*/
|
2011-02-22 14:38:48 +00:00
|
|
|
int GRID = (int) ( 10000.0 / DGRID ); // initial value = 10000 in kbool example but we use
|
|
|
|
// 10000/DGRID because the scaling is made by DGRID
|
|
|
|
// on integer pcbnew units and the global scaling
|
|
|
|
// ( GRID*DGRID) must be < 30000 to avoid overflow
|
|
|
|
// in calculations (made in long long in kbool)
|
2009-11-16 12:10:37 +00:00
|
|
|
if ( GRID <= 1 ) // Cannot be null!
|
|
|
|
GRID = 1;
|
2008-05-30 18:06:21 +00:00
|
|
|
|
|
|
|
aBooleng->SetMarge( MARGE );
|
|
|
|
aBooleng->SetGrid( GRID );
|
|
|
|
aBooleng->SetDGrid( DGRID );
|
|
|
|
aBooleng->SetCorrectionFactor( CORRECTIONFACTOR );
|
|
|
|
aBooleng->SetCorrectionAber( CORRECTIONABER );
|
|
|
|
aBooleng->SetSmoothAber( SMOOTHABER );
|
|
|
|
aBooleng->SetMaxlinemerge( MAXLINEMERGE );
|
|
|
|
aBooleng->SetRoundfactor( ROUNDFACTOR );
|
2012-01-22 17:20:22 +00:00
|
|
|
aBooleng->SetWindingRule( true ); // This is the default kbool value
|
2008-05-30 18:06:21 +00:00
|
|
|
|
|
|
|
if( aConvertHoles )
|
|
|
|
{
|
2009-09-19 16:15:40 +00:00
|
|
|
#if 1 // Can be set to 1 for kbool version >= 2.1, must be set to 0 for previous versions
|
2009-09-17 17:48:40 +00:00
|
|
|
// SetAllowNonTopHoleLinking() exists only in kbool >= 2.1
|
|
|
|
aBooleng->SetAllowNonTopHoleLinking( false ); // Default = true, but i have problems (filling errors) when true
|
|
|
|
#endif
|
2008-05-30 18:06:21 +00:00
|
|
|
aBooleng->SetLinkHoles( true ); // holes will be connected by double overlapping segments
|
|
|
|
aBooleng->SetOrientationEntryMode( false ); // all polygons are contours, not holes
|
|
|
|
}
|
|
|
|
else
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2008-11-09 17:14:53 +00:00
|
|
|
aBooleng->SetLinkHoles( false ); // holes will not be connected by double overlapping segments
|
2008-05-30 18:06:21 +00:00
|
|
|
aBooleng->SetOrientationEntryMode( true ); // holes are entered counter clockwise
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-30 18:06:21 +00:00
|
|
|
int CPolyLine::NormalizeAreaOutlines( std::vector<CPolyLine*> * pa, bool bRetainArcs )
|
|
|
|
{
|
|
|
|
return NormalizeWithKbool( pa, bRetainArcs );
|
|
|
|
}
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2008-10-12 15:29:43 +00:00
|
|
|
|
2007-12-29 19:27:58 +00:00
|
|
|
// Restore arcs to a polygon where they were replaced with steps
|
|
|
|
// If pa != NULL, also use polygons in pa array
|
|
|
|
//
|
|
|
|
int CPolyLine::RestoreArcs( std::vector<CArc> * arc_array, std::vector<CPolyLine*> * pa )
|
|
|
|
{
|
2008-05-15 11:20:19 +00:00
|
|
|
// get poly info
|
|
|
|
int n_polys = 1;
|
|
|
|
|
|
|
|
if( pa )
|
|
|
|
n_polys += pa->size();
|
|
|
|
CPolyLine* poly;
|
|
|
|
|
|
|
|
// undraw polys and clear utility flag for all corners
|
|
|
|
for( int ip = 0; ip<n_polys; ip++ )
|
|
|
|
{
|
|
|
|
if( ip == 0 )
|
|
|
|
poly = this;
|
|
|
|
else
|
|
|
|
poly = (*pa)[ip - 1];
|
2012-01-13 18:35:46 +00:00
|
|
|
poly->UnHatch();
|
2008-05-15 11:20:19 +00:00
|
|
|
for( int ic = 0; ic<poly->GetNumCorners(); ic++ )
|
2008-05-30 18:06:21 +00:00
|
|
|
poly->SetUtility( ic, 0 );
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2008-05-30 18:06:21 +00:00
|
|
|
// clear utility flag
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// find arcs and replace them
|
|
|
|
bool bFound;
|
|
|
|
int arc_start = 0;
|
|
|
|
int arc_end = 0;
|
|
|
|
for( unsigned iarc = 0; iarc<arc_array->size(); iarc++ )
|
|
|
|
{
|
|
|
|
int arc_xi = (*arc_array)[iarc].xi;
|
|
|
|
int arc_yi = (*arc_array)[iarc].yi;
|
|
|
|
int arc_xf = (*arc_array)[iarc].xf;
|
|
|
|
int arc_yf = (*arc_array)[iarc].yf;
|
|
|
|
int n_steps = (*arc_array)[iarc].n_steps;
|
|
|
|
int style = (*arc_array)[iarc].style;
|
2012-01-22 17:20:22 +00:00
|
|
|
bFound = false;
|
2008-05-15 11:20:19 +00:00
|
|
|
|
|
|
|
// loop through polys
|
|
|
|
for( int ip = 0; ip<n_polys; ip++ )
|
|
|
|
{
|
|
|
|
if( ip == 0 )
|
|
|
|
poly = this;
|
|
|
|
else
|
|
|
|
poly = (*pa)[ip - 1];
|
|
|
|
for( int icont = 0; icont<poly->GetNumContours(); icont++ )
|
|
|
|
{
|
|
|
|
int ic_start = poly->GetContourStart( icont );
|
|
|
|
int ic_end = poly->GetContourEnd( icont );
|
|
|
|
if( (ic_end - ic_start) > n_steps )
|
|
|
|
{
|
|
|
|
for( int ic = ic_start; ic<=ic_end; ic++ )
|
|
|
|
{
|
|
|
|
int ic_next = ic + 1;
|
|
|
|
if( ic_next > ic_end )
|
|
|
|
ic_next = ic_start;
|
|
|
|
int xi = poly->GetX( ic );
|
|
|
|
int yi = poly->GetY( ic );
|
|
|
|
if( xi == arc_xi && yi == arc_yi )
|
|
|
|
{
|
|
|
|
// test for forward arc
|
|
|
|
int ic2 = ic + n_steps;
|
|
|
|
if( ic2 > ic_end )
|
|
|
|
ic2 = ic2 - ic_end + ic_start - 1;
|
|
|
|
int xf = poly->GetX( ic2 );
|
|
|
|
int yf = poly->GetY( ic2 );
|
|
|
|
if( xf == arc_xf && yf == arc_yf )
|
|
|
|
{
|
|
|
|
// arc from ic to ic2
|
2012-01-22 17:20:22 +00:00
|
|
|
bFound = true;
|
2008-05-15 11:20:19 +00:00
|
|
|
arc_start = ic;
|
|
|
|
arc_end = ic2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// try reverse arc
|
|
|
|
ic2 = ic - n_steps;
|
|
|
|
if( ic2 < ic_start )
|
|
|
|
ic2 = ic2 - ic_start + ic_end + 1;
|
|
|
|
xf = poly->GetX( ic2 );
|
|
|
|
yf = poly->GetY( ic2 );
|
|
|
|
if( xf == arc_xf && yf == arc_yf )
|
|
|
|
{
|
|
|
|
// arc from ic2 to ic
|
2012-01-22 17:20:22 +00:00
|
|
|
bFound = true;
|
2008-05-15 11:20:19 +00:00
|
|
|
arc_start = ic2;
|
|
|
|
arc_end = ic;
|
|
|
|
style = 3 - style;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( bFound )
|
|
|
|
{
|
|
|
|
poly->side_style[arc_start] = style;
|
|
|
|
|
|
|
|
// mark corners for deletion from arc_start+1 to arc_end-1
|
|
|
|
for( int i = arc_start + 1; i!=arc_end; )
|
|
|
|
{
|
|
|
|
if( i > ic_end )
|
|
|
|
i = ic_start;
|
|
|
|
poly->SetUtility( i, 1 );
|
|
|
|
if( i == ic_end )
|
|
|
|
i = ic_start;
|
|
|
|
else
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( bFound )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( bFound )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( bFound )
|
2012-01-22 17:20:22 +00:00
|
|
|
(*arc_array)[iarc].bFound = true;
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// now delete all marked corners
|
|
|
|
for( int ip = 0; ip<n_polys; ip++ )
|
|
|
|
{
|
|
|
|
if( ip == 0 )
|
|
|
|
poly = this;
|
|
|
|
else
|
|
|
|
poly = (*pa)[ip - 1];
|
|
|
|
for( int ic = poly->GetNumCorners() - 1; ic>=0; ic-- )
|
|
|
|
{
|
|
|
|
if( poly->GetUtility( ic ) )
|
2012-01-22 17:20:22 +00:00
|
|
|
poly->DeleteCorner( ic, false );
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
// initialize new polyline
|
|
|
|
// set layer, width, selection box size, starting point, id and pointer
|
|
|
|
//
|
|
|
|
// if sel_box = 0, don't create selection elements at all
|
|
|
|
//
|
|
|
|
// if polyline is board outline, enter with:
|
|
|
|
// id.type = ID_BOARD
|
|
|
|
// id.st = ID_BOARD_OUTLINE
|
|
|
|
// id.i = 0
|
|
|
|
// ptr = NULL
|
|
|
|
//
|
|
|
|
// if polyline is copper area, enter with:
|
|
|
|
// id.type = ID_NET;
|
|
|
|
// id.st = ID_AREA
|
|
|
|
// id.i = index to area
|
|
|
|
// ptr = pointer to net
|
|
|
|
//
|
2008-01-31 20:53:44 +00:00
|
|
|
void CPolyLine::Start( int layer, int x, int y, int hatch )
|
2007-12-29 19:27:58 +00:00
|
|
|
{
|
2008-05-15 11:20:19 +00:00
|
|
|
m_layer = layer;
|
2012-04-11 09:47:57 +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
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
corner.push_back( poly_pt );
|
|
|
|
side_style.push_back( 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
|
|
|
// add a corner to unclosed polyline
|
|
|
|
//
|
2008-01-05 13:37:51 +00:00
|
|
|
void CPolyLine::AppendCorner( int x, int y, int style, bool bDraw )
|
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
|
|
|
|
|
|
|
// add entries for new corner and side
|
|
|
|
corner.push_back( poly_pt );
|
|
|
|
side_style.push_back( style );
|
|
|
|
if( corner.size() > 0 && !corner[corner.size() - 1].end_contour )
|
|
|
|
side_style[corner.size() - 1] = style;
|
|
|
|
if( bDraw )
|
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
|
|
|
// close last polyline contour
|
|
|
|
//
|
2008-01-05 13:37:51 +00:00
|
|
|
void CPolyLine::Close( int style, bool bDraw )
|
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
|
|
|
}
|
2012-01-13 18:35:46 +00:00
|
|
|
UnHatch();
|
2008-05-15 11:20:19 +00:00
|
|
|
side_style[corner.size() - 1] = style;
|
2012-01-22 17:20:22 +00:00
|
|
|
corner[corner.size() - 1].end_contour = true;
|
2008-05-15 11:20:19 +00:00
|
|
|
if( bDraw )
|
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
|
|
|
// move corner of polyline
|
|
|
|
//
|
|
|
|
void CPolyLine::MoveCorner( int ic, int x, int y )
|
|
|
|
{
|
2012-01-13 18:35:46 +00:00
|
|
|
UnHatch();
|
2008-05-15 11:20:19 +00:00
|
|
|
corner[ic].x = x;
|
|
|
|
corner[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
|
|
|
|
//
|
2008-01-05 13:37:51 +00:00
|
|
|
void CPolyLine::DeleteCorner( int ic, bool bDraw )
|
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
|
|
|
int icont = GetContour( ic );
|
|
|
|
int istart = GetContourStart( icont );
|
|
|
|
int iend = GetContourEnd( icont );
|
|
|
|
bool bClosed = icont < GetNumContours() - 1 || GetClosed();
|
|
|
|
|
|
|
|
if( !bClosed )
|
|
|
|
{
|
|
|
|
// open contour, must be last contour
|
|
|
|
corner.erase( corner.begin() + ic );
|
|
|
|
|
|
|
|
if( ic != istart )
|
|
|
|
side_style.erase( side_style.begin() + ic - 1 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// closed contour
|
|
|
|
corner.erase( corner.begin() + ic );
|
|
|
|
side_style.erase( side_style.begin() + ic );
|
|
|
|
if( ic == iend )
|
2012-01-22 17:20:22 +00:00
|
|
|
corner[ic - 1].end_contour = true;
|
2008-05-15 11:20:19 +00:00
|
|
|
}
|
|
|
|
if( bClosed && GetContourSize( icont ) < 3 )
|
|
|
|
{
|
|
|
|
// delete the entire contour
|
|
|
|
RemoveContour( icont );
|
|
|
|
}
|
|
|
|
if( bDraw )
|
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
|
|
|
|
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();
|
2008-05-15 11:20:19 +00:00
|
|
|
int istart = GetContourStart( icont );
|
|
|
|
int iend = GetContourEnd( icont );
|
|
|
|
|
|
|
|
if( icont == 0 && GetNumContours() == 1 )
|
|
|
|
{
|
|
|
|
// remove the only contour
|
|
|
|
wxASSERT( 0 );
|
|
|
|
}
|
|
|
|
else if( icont == GetNumContours() - 1 )
|
|
|
|
{
|
|
|
|
// remove last contour
|
|
|
|
corner.erase( corner.begin() + istart, corner.end() );
|
|
|
|
side_style.erase( side_style.begin() + istart, side_style.end() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// remove closed contour
|
|
|
|
for( int ic = iend; ic>=istart; ic-- )
|
|
|
|
{
|
|
|
|
corner.erase( corner.begin() + ic );
|
|
|
|
side_style.erase( side_style.begin() + ic );
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
for( int contour = 0; contour < GetNumContours(); contour++ )
|
2011-02-21 19:43:59 +00:00
|
|
|
{
|
2011-02-23 21:34:28 +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++ )
|
|
|
|
{
|
|
|
|
int x1, y1, nx, ny;
|
|
|
|
long long xa, ya, xb, yb;
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
x1 = corner[index].x;
|
|
|
|
y1 = corner[index].y;
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
if( index == startIndex )
|
|
|
|
{
|
|
|
|
xa = corner[endIndex].x - x1;
|
|
|
|
ya = corner[endIndex].y - y1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xa = corner[index-1].x - x1;
|
|
|
|
ya = corner[index-1].y - y1;
|
|
|
|
}
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
if( index == endIndex )
|
|
|
|
{
|
|
|
|
xb = corner[startIndex].x - x1;
|
|
|
|
yb = corner[startIndex].y - y1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xb = corner[index+1].x - x1;
|
|
|
|
yb = corner[index+1].y - y1;
|
|
|
|
}
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
unsigned int lena = (unsigned int)sqrt( (double)(xa*xa + ya*ya) );
|
|
|
|
unsigned int lenb = (unsigned int)sqrt( (double)(xb*xb + yb*yb) );
|
|
|
|
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
|
|
|
|
if( 0.5*lena < distance )
|
2011-03-17 13:53:19 +00:00
|
|
|
distance = (unsigned int)(0.5*(double)lena);
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
if( 0.5*lenb < distance )
|
2011-03-17 13:53:19 +00:00
|
|
|
distance = (unsigned int)(0.5*(double)lenb);
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
nx = (int) ( (double) (distance*xa)/sqrt( (double) (xa*xa + ya*ya) ) );
|
|
|
|
ny = (int) ( (double) (distance*ya)/sqrt( (double) (xa*xa + ya*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
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
nx = (int) ( (double) (distance*xb)/sqrt( (double) (xb*xb + yb*yb) ) );
|
|
|
|
ny = (int) ( (double) (distance*yb)/sqrt( (double) (xb*xb + yb*yb) ) );
|
|
|
|
newPoly->AppendCorner( x1 + nx, y1 + ny );
|
|
|
|
}
|
|
|
|
newPoly->Close();
|
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
|
|
|
}
|
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
for( int contour = 0; contour < GetNumContours(); contour++ )
|
2011-02-21 19:43:59 +00:00
|
|
|
{
|
2011-02-23 21:34:28 +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++ )
|
|
|
|
{
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2011-02-23 21:34:28 +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
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
x1 = corner[index].x;
|
|
|
|
y1 = corner[index].y;
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
if( index == startIndex )
|
|
|
|
{
|
|
|
|
xa = corner[endIndex].x - x1;
|
|
|
|
ya = corner[endIndex].y - y1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xa = corner[index-1].x - x1;
|
|
|
|
ya = corner[index-1].y - y1;
|
|
|
|
}
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
if( index == endIndex )
|
|
|
|
{
|
|
|
|
xb = corner[startIndex].x - x1;
|
|
|
|
yb = corner[startIndex].y - y1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xb = corner[index+1].x - x1;
|
|
|
|
yb = corner[index+1].y - y1;
|
|
|
|
}
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
double lena = sqrt( (double) (xa*xa + ya*ya) );
|
|
|
|
double lenb = sqrt( (double) (xb*xb + yb*yb) );
|
|
|
|
double cosine = ( xa*xb + ya*yb )/( lena*lenb );
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
unsigned int radius = aRadius;
|
|
|
|
double denom = sqrt( 2.0/( 1+cosine )-1 );
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
// Limit rounding distance to one half of an edge
|
|
|
|
if( 0.5*lena*denom < radius )
|
2011-03-17 13:53:19 +00:00
|
|
|
radius = (unsigned int)(0.5*lena*denom);
|
2011-02-21 19:43:59 +00:00
|
|
|
|
2011-02-23 21:34:28 +00:00
|
|
|
if( 0.5*lenb*denom < radius )
|
2011-03-17 13:53:19 +00:00
|
|
|
radius = (unsigned int)(0.5*lenb*denom);
|
2011-02-23 21:34:28 +00:00
|
|
|
|
|
|
|
// Calculate fillet arc absolute center point (xc, yx)
|
|
|
|
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;
|
|
|
|
|
|
|
|
// Calculate arc start and end vectors
|
|
|
|
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;
|
|
|
|
|
|
|
|
// Cosine of arc angle
|
|
|
|
double argument = ( xs*xe + ys*ye ) / ( radius*radius );
|
|
|
|
|
|
|
|
if( argument < -1 ) // Just in case...
|
|
|
|
argument = -1;
|
|
|
|
else if( argument > 1 )
|
|
|
|
argument = 1;
|
|
|
|
|
|
|
|
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
|
|
|
|
double tempSegments = (double)aSegments * ( arcAngle / ( 2*M_PI ) );
|
|
|
|
|
|
|
|
if( tempSegments - (int)tempSegments > 0 )
|
|
|
|
tempSegments++;
|
|
|
|
unsigned int segments = (unsigned int) tempSegments;
|
|
|
|
|
|
|
|
double deltaAngle = arcAngle / segments;
|
|
|
|
double startAngle = atan2( -ys, xs );
|
|
|
|
|
|
|
|
// Flip arc for inner corners
|
|
|
|
if( xa*yb - ya*xb <= 0 )
|
|
|
|
deltaAngle *= -1;
|
|
|
|
|
|
|
|
nx = xc + xs + 0.5;
|
|
|
|
ny = yc + ys + 0.5;
|
|
|
|
if( index == startIndex )
|
|
|
|
newPoly->Start( GetLayer(), (int)nx, (int)ny, GetHatchStyle() );
|
|
|
|
else
|
|
|
|
newPoly->AppendCorner( (int)nx, (int)ny );
|
|
|
|
|
|
|
|
unsigned int nVertices = 0;
|
|
|
|
for( unsigned int j = 0; j < segments; j++ )
|
|
|
|
{
|
|
|
|
nx = xc + cos( startAngle + (j+1)*deltaAngle )*radius + 0.5;
|
|
|
|
ny = yc - sin( startAngle + (j+1)*deltaAngle )*radius + 0.5;
|
|
|
|
newPoly->AppendCorner( (int)nx, (int)ny );
|
|
|
|
nVertices++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newPoly->Close();
|
|
|
|
}
|
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
|
|
|
|
* removes all corners from the lists.
|
|
|
|
* Others params are not chnaged
|
|
|
|
*/
|
|
|
|
{
|
2008-05-15 11:20:19 +00:00
|
|
|
corner.clear();
|
|
|
|
side_style.clear();
|
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();
|
2008-05-15 11:20:19 +00:00
|
|
|
if( (unsigned) (ic) >= corner.size() )
|
|
|
|
{
|
|
|
|
corner.push_back( CPolyPt( x, y ) );
|
|
|
|
side_style.push_back( STRAIGHT );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
corner.insert( corner.begin() + ic + 1, CPolyPt( x, y ) );
|
|
|
|
side_style.insert( side_style.begin() + ic + 1, STRAIGHT );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( (unsigned) (ic + 1) < corner.size() )
|
|
|
|
{
|
|
|
|
if( corner[ic].end_contour )
|
|
|
|
{
|
2012-01-22 17:20:22 +00:00
|
|
|
corner[ic + 1].end_contour = true;
|
|
|
|
corner[ic].end_contour = false;
|
2008-05-15 11:20:19 +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 )
|
|
|
|
{
|
|
|
|
return corner[ic].end_contour;
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2007-12-29 19:27:58 +00:00
|
|
|
CRect CPolyLine::GetBounds()
|
|
|
|
{
|
2008-05-15 11:20:19 +00:00
|
|
|
CRect r = GetCornerBounds();
|
|
|
|
|
|
|
|
r.left -= m_Width / 2;
|
|
|
|
r.right += m_Width / 2;
|
|
|
|
r.bottom -= m_Width / 2;
|
|
|
|
r.top += m_Width / 2;
|
|
|
|
return r;
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2007-12-29 19:27:58 +00:00
|
|
|
CRect CPolyLine::GetCornerBounds()
|
|
|
|
{
|
2008-05-15 11:20:19 +00:00
|
|
|
CRect r;
|
|
|
|
|
|
|
|
r.left = r.bottom = INT_MAX;
|
|
|
|
r.right = r.top = INT_MIN;
|
|
|
|
for( unsigned i = 0; i<corner.size(); i++ )
|
|
|
|
{
|
2012-01-13 18:35:46 +00:00
|
|
|
r.left = min( r.left, corner[i].x );
|
|
|
|
r.right = max( r.right, corner[i].x );
|
|
|
|
r.bottom = min( r.bottom, corner[i].y );
|
|
|
|
r.top = max( r.top, corner[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
|
|
|
|
2007-12-29 19:27:58 +00:00
|
|
|
CRect CPolyLine::GetCornerBounds( int icont )
|
|
|
|
{
|
2008-05-15 11:20:19 +00:00
|
|
|
CRect r;
|
|
|
|
|
|
|
|
r.left = r.bottom = INT_MAX;
|
|
|
|
r.right = r.top = INT_MIN;
|
|
|
|
int istart = GetContourStart( icont );
|
|
|
|
int iend = GetContourEnd( icont );
|
|
|
|
for( int i = istart; i<=iend; i++ )
|
|
|
|
{
|
2008-10-25 10:21:46 +00:00
|
|
|
r.left = MIN( r.left, corner[i].x );
|
|
|
|
r.right = MAX( r.right, corner[i].x );
|
|
|
|
r.bottom = MIN( r.bottom, corner[i].y );
|
|
|
|
r.top = MAX( r.top, corner[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
|
|
|
|
|
|
|
int CPolyLine::GetNumCorners()
|
|
|
|
{
|
|
|
|
return corner.size();
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
|
|
|
|
int CPolyLine::GetNumSides()
|
|
|
|
{
|
|
|
|
if( GetClosed() )
|
|
|
|
return corner.size();
|
|
|
|
else
|
|
|
|
return corner.size() - 1;
|
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::GetNumContours()
|
|
|
|
{
|
2008-05-15 11:20:19 +00:00
|
|
|
int ncont = 0;
|
|
|
|
|
|
|
|
if( !corner.size() )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for( unsigned ic = 0; ic<corner.size(); ic++ )
|
|
|
|
if( corner[ic].end_contour )
|
|
|
|
ncont++;
|
|
|
|
|
|
|
|
if( !corner[corner.size() - 1].end_contour )
|
|
|
|
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::GetContour( int ic )
|
|
|
|
{
|
2008-05-15 11:20:19 +00:00
|
|
|
int ncont = 0;
|
|
|
|
|
|
|
|
for( int i = 0; i<ic; i++ )
|
|
|
|
{
|
|
|
|
if( corner[i].end_contour )
|
|
|
|
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;
|
|
|
|
for( unsigned i = 0; i<corner.size(); i++ )
|
|
|
|
{
|
|
|
|
if( corner[i].end_contour )
|
|
|
|
{
|
|
|
|
ncont++;
|
|
|
|
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;
|
|
|
|
|
|
|
|
if( icont == GetNumContours() - 1 )
|
|
|
|
return corner.size() - 1;
|
|
|
|
|
|
|
|
int ncont = 0;
|
|
|
|
for( unsigned i = 0; i<corner.size(); i++ )
|
|
|
|
{
|
|
|
|
if( corner[i].end_contour )
|
|
|
|
{
|
|
|
|
if( ncont == icont )
|
|
|
|
return i;
|
|
|
|
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
|
|
|
void CPolyLine::SetSideStyle( int is, int style )
|
|
|
|
{
|
2012-01-13 18:35:46 +00:00
|
|
|
UnHatch();
|
2008-05-15 11:20:19 +00:00
|
|
|
CPoint p1, p2;
|
|
|
|
if( is == (int) (corner.size() - 1) )
|
|
|
|
{
|
|
|
|
p1.x = corner[corner.size() - 1].x;
|
|
|
|
p1.y = corner[corner.size() - 1].y;
|
|
|
|
p2.x = corner[0].x;
|
|
|
|
p2.y = corner[0].y;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p1.x = corner[is].x;
|
|
|
|
p1.y = corner[is].y;
|
|
|
|
p2.x = corner[is + 1].x;
|
|
|
|
p2.y = corner[is + 1].y;
|
|
|
|
}
|
|
|
|
if( p1.x == p2.x || p1.y == p2.y )
|
|
|
|
side_style[is] = STRAIGHT;
|
|
|
|
else
|
|
|
|
side_style[is] = style;
|
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
|
|
|
|
|
|
|
int CPolyLine::GetSideStyle( int is )
|
|
|
|
{
|
|
|
|
return side_style[is];
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-15 11:20:19 +00:00
|
|
|
int CPolyLine::GetClosed()
|
|
|
|
{
|
|
|
|
if( corner.size() == 0 )
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return corner[corner.size() - 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-01-13 18:35:46 +00:00
|
|
|
// sort function used in ::Hatch to sort points by descending CPoint.x values
|
|
|
|
bool sort_ends_by_descending_X( const CPoint& ref, const CPoint& tst )
|
|
|
|
{
|
|
|
|
return tst.x < ref.x;
|
|
|
|
}
|
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;
|
|
|
|
|
2010-11-17 18:41:20 +00:00
|
|
|
if( !GetClosed() ) // If not closed, the poly is beeing created and not finalised. Not not hatch
|
|
|
|
return;
|
|
|
|
|
|
|
|
// define range for hatch lines
|
|
|
|
int min_x = corner[0].x;
|
|
|
|
int max_x = corner[0].x;
|
|
|
|
int min_y = corner[0].y;
|
|
|
|
int max_y = corner[0].y;
|
|
|
|
for( unsigned ic = 1; ic < corner.size(); ic++ )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2010-11-17 18:41:20 +00:00
|
|
|
if( corner[ic].x < min_x )
|
|
|
|
min_x = corner[ic].x;
|
|
|
|
if( corner[ic].x > max_x )
|
|
|
|
max_x = corner[ic].x;
|
|
|
|
if( corner[ic].y < min_y )
|
|
|
|
min_y = corner[ic].y;
|
|
|
|
if( corner[ic].y > max_y )
|
|
|
|
max_y = corner[ic].y;
|
|
|
|
}
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2012-01-10 20:12:46 +00:00
|
|
|
// Calculate spacing betwwen 2 hatch lines
|
2010-11-17 18:41:20 +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
|
|
|
|
2012-01-10 20:12:46 +00:00
|
|
|
// set the "lenght" of hatch lines (the lenght on horizontal axis)
|
2012-04-11 09:47:57 +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
|
|
|
|
int layer = GetLayer();
|
|
|
|
int slope_flag = (layer & 1) ? 1 : -1; // 1 or -1
|
|
|
|
double slope = 0.707106 * slope_flag; // 45 degrees slope
|
2010-11-17 18:41:20 +00:00
|
|
|
int max_a, min_a;
|
|
|
|
if( slope_flag == 1 )
|
|
|
|
{
|
|
|
|
max_a = (int) (max_y - slope * min_x);
|
|
|
|
min_a = (int) (min_y - slope * max_x);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
max_a = (int) (max_y - slope * max_x);
|
|
|
|
min_a = (int) (min_y - slope * min_x);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
int nc = corner.size();
|
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-01-10 20:12:46 +00:00
|
|
|
static std::vector <CPoint> pointbuffer;
|
|
|
|
pointbuffer.clear();
|
|
|
|
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;
|
|
|
|
for( int ic = 0; ic<nc; ic++ )
|
2010-11-17 18:41:20 +00:00
|
|
|
{
|
2012-01-10 20:12:46 +00:00
|
|
|
double x, y, x2, y2;
|
|
|
|
int ok;
|
|
|
|
if( corner[ic].end_contour || ( ic == (int) (corner.size() - 1) ) )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
2012-01-10 20:12:46 +00:00
|
|
|
ok = FindLineSegmentIntersection( a, slope,
|
|
|
|
corner[ic].x, corner[ic].y,
|
|
|
|
corner[i_start_contour].x,
|
|
|
|
corner[i_start_contour].y,
|
|
|
|
side_style[ic],
|
|
|
|
&x, &y, &x2, &y2 );
|
|
|
|
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,
|
|
|
|
corner[ic].x, corner[ic].y,
|
|
|
|
corner[ic + 1].x, corner[ic + 1].y,
|
|
|
|
side_style[ic],
|
|
|
|
&x, &y, &x2, &y2 );
|
|
|
|
}
|
|
|
|
if( ok )
|
|
|
|
{
|
|
|
|
CPoint point( (int) x, (int) y);
|
|
|
|
pointbuffer.push_back( point );
|
|
|
|
}
|
|
|
|
if( ok == 2 )
|
|
|
|
{
|
|
|
|
CPoint point( (int) x2, (int) y2);
|
|
|
|
pointbuffer.push_back( point );
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
// 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-01-10 20:12:46 +00:00
|
|
|
m_HatchLines.push_back( CSegment( pointbuffer[ip].x,
|
|
|
|
pointbuffer[ip].y,
|
|
|
|
pointbuffer[ip + 1].x,
|
|
|
|
pointbuffer[ip + 1].y ) );
|
2010-11-17 18:41:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-10 20:12:46 +00:00
|
|
|
double dy = pointbuffer[ip + 1].y - pointbuffer[ip].y;
|
2010-11-17 18:41:20 +00:00
|
|
|
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-01-10 20:12:46 +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,
|
|
|
|
to_int( x1 ), to_int( 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,
|
|
|
|
to_int( x2 ), to_int( 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.
|
|
|
|
// Since the first is the main outline, and other are hole,
|
|
|
|
// 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.
|
|
|
|
int polycount = GetNumContours();
|
|
|
|
bool inside = false;
|
|
|
|
for( int icont = 0; icont < polycount; icont++ )
|
2008-05-15 11:20:19 +00:00
|
|
|
{
|
|
|
|
int istart = GetContourStart( icont );
|
|
|
|
int iend = GetContourEnd( icont );
|
2012-04-10 16:28:26 +00:00
|
|
|
|
2010-10-28 13:02:07 +00:00
|
|
|
// Test this polygon:
|
|
|
|
if( TestPointInsidePolygon( corner, istart, iend, px, py) ) // test point inside the current polygon
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// copy data from another poly, but don't draw it
|
|
|
|
//
|
2008-05-15 11:20:19 +00:00
|
|
|
void CPolyLine::Copy( CPolyLine* src )
|
2007-12-29 19:27:58 +00:00
|
|
|
{
|
2012-01-13 18:35:46 +00:00
|
|
|
UnHatch();
|
2012-04-11 09:47:57 +00:00
|
|
|
m_hatchStyle = src->m_hatchStyle;
|
|
|
|
m_hatchPitch = src->m_hatchPitch;
|
2009-08-23 15:22:44 +00:00
|
|
|
// copy corners, using vector copy
|
|
|
|
corner = src->corner;
|
|
|
|
// copy side styles, using vector copy
|
|
|
|
side_style = src->side_style;
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
|
|
|
|
2008-01-05 13:37:51 +00:00
|
|
|
|
|
|
|
/*******************************************/
|
|
|
|
bool CPolyLine::IsCutoutContour( int icont )
|
|
|
|
/*******************************************/
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2008-01-05 13:37:51 +00:00
|
|
|
/*
|
|
|
|
* return true if the corner icont is inside the outline (i.e it is a hole)
|
|
|
|
*/
|
|
|
|
{
|
2008-05-15 11:20:19 +00:00
|
|
|
int ncont = GetContour( icont );
|
|
|
|
|
|
|
|
if( ncont == 0 ) // the first contour is the main outline, not an hole
|
|
|
|
return false;
|
|
|
|
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();
|
2008-05-15 11:20:19 +00:00
|
|
|
for( int ic = 0; ic < GetNumCorners(); ic++ )
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Set various parameters:
|
2012-01-13 18:35:46 +00:00
|
|
|
// the calling function should UnHatch() before calling them,
|
2007-12-29 19:27:58 +00:00
|
|
|
// and Draw() after
|
|
|
|
//
|
2008-05-15 11:20:19 +00:00
|
|
|
void CPolyLine::SetX( int ic, int x )
|
|
|
|
{
|
|
|
|
corner[ic].x = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CPolyLine::SetY( int ic, int y )
|
|
|
|
{
|
|
|
|
corner[ic].y = y;
|
|
|
|
}
|
|
|
|
|
2008-01-05 13:37:51 +00:00
|
|
|
|
|
|
|
void CPolyLine::SetEndContour( int ic, bool end_contour )
|
|
|
|
{
|
2008-05-15 11:20:19 +00:00
|
|
|
corner[ic].end_contour = end_contour;
|
2008-01-05 13:37:51 +00:00
|
|
|
}
|
2007-12-29 19:27:58 +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
|
|
|
|
double r = sqrt( (double) (xi - xc) * (xi - xc) + (double) (yi - yc) * (yi - yc) );
|
|
|
|
|
|
|
|
// get angles of start and finish
|
2008-10-29 15:26:53 +00:00
|
|
|
double th_i = atan2( (double) (yi - yc), (double) (xi - xc) );
|
|
|
|
double th_f = atan2( (double) (yf - yc), (double) (xf - xc) );
|
2008-05-15 11:20:19 +00:00
|
|
|
double th_d = (th_f - th_i) / (num - 1);
|
|
|
|
double theta = th_i;
|
|
|
|
|
|
|
|
// generate arc
|
|
|
|
for( int ic = 0; ic<num; ic++ )
|
|
|
|
{
|
|
|
|
int x = to_int( xc + r * cos( theta ) );
|
|
|
|
int y = to_int( yc + r * sin( theta ) );
|
|
|
|
AppendCorner( x, y, STRAIGHT, 0 );
|
|
|
|
theta += th_d;
|
|
|
|
}
|
|
|
|
|
|
|
|
Close( STRAIGHT );
|
2007-12-29 19:27:58 +00:00
|
|
|
}
|
2009-06-25 20:45:27 +00:00
|
|
|
|
|
|
|
// Bezier Support
|
|
|
|
void CPolyLine::AppendBezier(int x1, int y1, int x2, int y2, int x3, int y3) {
|
|
|
|
std::vector<wxPoint> bezier_points;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPolyLine::AppendBezier(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4){
|
|
|
|
std::vector<wxPoint> bezier_points;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|