2011-09-23 13:57:12 +00:00
|
|
|
/**
|
|
|
|
* @file zone_filling_algorithm.cpp:
|
|
|
|
* Algorithms used to fill a zone defined by a polygon and a filling starting point.
|
2007-12-30 18:20:51 +00:00
|
|
|
*/
|
2007-12-19 16:44:53 +00:00
|
|
|
|
2008-12-03 10:32:53 +00:00
|
|
|
|
|
|
|
#include <algorithm> // sort
|
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <fctsys.h>
|
|
|
|
#include <trigo.h>
|
|
|
|
#include <wxPcbStruct.h>
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <class_zone.h>
|
2007-12-19 16:44:53 +00:00
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <pcbnew.h>
|
|
|
|
#include <zones.h>
|
|
|
|
#include <protos.h>
|
2007-12-19 16:44:53 +00:00
|
|
|
|
|
|
|
/* Local functions */
|
|
|
|
|
|
|
|
/* Local variables */
|
2008-11-28 20:10:05 +00:00
|
|
|
|
|
|
|
|
2010-11-12 16:36:43 +00:00
|
|
|
/**
|
|
|
|
* Function BuildFilledPolysListData
|
2008-11-28 20:10:05 +00:00
|
|
|
* Build m_FilledPolysList data from real outlines (m_Poly)
|
|
|
|
* in order to have drawable (and plottable) filled polygons
|
|
|
|
* drawable filled polygons are polygons without hole
|
|
|
|
* @param aPcb: the current board (can be NULL for non copper zones)
|
2012-01-29 19:29:19 +00:00
|
|
|
* @param aCornerBuffer: A reference to a buffer to put polygon corners, or NULL
|
|
|
|
* if NULL (default), uses m_FilledPolysList and fill current zone.
|
2008-11-28 20:10:05 +00:00
|
|
|
* @return number of polygons
|
|
|
|
* This function does not add holes for pads and tracks but calls
|
|
|
|
* AddClearanceAreasPolygonsToPolysList() to do that for copper layers
|
|
|
|
*/
|
2012-01-29 19:29:19 +00:00
|
|
|
int ZONE_CONTAINER::BuildFilledPolysListData( BOARD* aPcb, std::vector <CPolyPt>* aCornerBuffer )
|
2008-11-28 20:10:05 +00:00
|
|
|
{
|
2012-01-29 19:29:19 +00:00
|
|
|
if( aCornerBuffer == NULL )
|
|
|
|
m_FilledPolysList.clear();
|
2008-12-03 10:32:53 +00:00
|
|
|
|
2008-11-28 20:10:05 +00:00
|
|
|
/* convert outlines + holes to outlines without holes (adding extra segments if necessary)
|
2011-09-23 13:57:12 +00:00
|
|
|
* m_Poly data is expected normalized, i.e. NormalizeAreaOutlines was used after building
|
|
|
|
* this zone
|
2008-12-03 10:32:53 +00:00
|
|
|
*/
|
2008-11-28 20:10:05 +00:00
|
|
|
|
2012-01-10 20:12:46 +00:00
|
|
|
if( GetNumCorners() <= 2 ) // malformed zone. polygon calculations do not like it ...
|
2008-11-28 20:10:05 +00:00
|
|
|
return 0;
|
|
|
|
|
2011-02-21 19:43:59 +00:00
|
|
|
// Make a smoothed polygon out of the user-drawn polygon if required
|
2012-01-10 20:12:46 +00:00
|
|
|
if( smoothedPoly )
|
|
|
|
{
|
2011-02-21 19:43:59 +00:00
|
|
|
delete smoothedPoly;
|
|
|
|
smoothedPoly = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch( cornerSmoothingType )
|
|
|
|
{
|
2012-02-06 05:44:19 +00:00
|
|
|
case ZONE_SETTINGS::SMOOTHING_CHAMFER:
|
2011-02-21 19:43:59 +00:00
|
|
|
smoothedPoly = m_Poly->Chamfer( cornerRadius );
|
|
|
|
break;
|
2012-02-06 05:44:19 +00:00
|
|
|
case ZONE_SETTINGS::SMOOTHING_FILLET:
|
2011-02-21 19:43:59 +00:00
|
|
|
smoothedPoly = m_Poly->Fillet( cornerRadius, m_ArcToSegmentsCount );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
smoothedPoly = new CPolyLine;
|
|
|
|
smoothedPoly->Copy( m_Poly );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
smoothedPoly->MakeKboolPoly( -1, -1, NULL, true );
|
2008-11-28 20:10:05 +00:00
|
|
|
int count = 0;
|
2011-02-21 19:43:59 +00:00
|
|
|
while( smoothedPoly->GetKboolEngine()->StartPolygonGet() )
|
2008-11-28 20:10:05 +00:00
|
|
|
{
|
2008-12-03 10:32:53 +00:00
|
|
|
CPolyPt corner( 0, 0, false );
|
2011-02-21 19:43:59 +00:00
|
|
|
while( smoothedPoly->GetKboolEngine()->PolygonHasMorePoints() )
|
2008-11-28 20:10:05 +00:00
|
|
|
{
|
2011-02-21 19:43:59 +00:00
|
|
|
corner.x = (int) smoothedPoly->GetKboolEngine()->GetPolygonXPoint();
|
|
|
|
corner.y = (int) smoothedPoly->GetKboolEngine()->GetPolygonYPoint();
|
2008-11-28 20:10:05 +00:00
|
|
|
corner.end_contour = false;
|
2012-01-29 19:29:19 +00:00
|
|
|
if( aCornerBuffer )
|
|
|
|
aCornerBuffer->push_back( corner );
|
|
|
|
else
|
|
|
|
m_FilledPolysList.push_back( corner );
|
2008-12-03 10:32:53 +00:00
|
|
|
count++;
|
2008-11-28 20:10:05 +00:00
|
|
|
}
|
2008-12-03 10:32:53 +00:00
|
|
|
|
2008-11-28 20:10:05 +00:00
|
|
|
corner.end_contour = true;
|
2012-01-29 19:29:19 +00:00
|
|
|
if( aCornerBuffer )
|
|
|
|
{
|
|
|
|
aCornerBuffer->pop_back();
|
|
|
|
aCornerBuffer->push_back( corner );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_FilledPolysList.pop_back();
|
|
|
|
m_FilledPolysList.push_back( corner );
|
|
|
|
}
|
2011-02-21 19:43:59 +00:00
|
|
|
smoothedPoly->GetKboolEngine()->EndPolygonGet();
|
2008-11-28 20:10:05 +00:00
|
|
|
}
|
|
|
|
|
2011-02-21 19:43:59 +00:00
|
|
|
smoothedPoly->FreeKboolEngine();
|
2008-11-28 20:10:05 +00:00
|
|
|
|
|
|
|
/* For copper layers, we now must add holes in the Polygon list.
|
2008-12-03 10:32:53 +00:00
|
|
|
* holes are pads and tracks with their clearance area
|
|
|
|
*/
|
2012-01-29 19:29:19 +00:00
|
|
|
if( ! aCornerBuffer )
|
|
|
|
{
|
|
|
|
if( IsOnCopperLayer() )
|
|
|
|
AddClearanceAreasPolygonsToPolysList( aPcb );
|
2008-11-28 20:10:05 +00:00
|
|
|
|
2012-01-29 19:29:19 +00:00
|
|
|
if ( m_FillMode ) // if fill mode uses segments, create them:
|
|
|
|
Fill_Zone_Areas_With_Segments( );
|
|
|
|
}
|
2008-12-03 10:32:53 +00:00
|
|
|
|
2008-11-28 20:10:05 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2008-12-03 10:32:53 +00:00
|
|
|
// Sort function to build filled zones
|
|
|
|
static bool SortByXValues( const int& a, const int &b)
|
|
|
|
{
|
|
|
|
return a < b;
|
|
|
|
}
|
2008-11-28 20:10:05 +00:00
|
|
|
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2010-11-12 16:36:43 +00:00
|
|
|
/**
|
2010-11-12 16:59:16 +00:00
|
|
|
* Function Fill_Zone_Areas_With_Segments
|
2010-12-14 15:56:30 +00:00
|
|
|
* Fill sub areas in a zone with segments with m_ZoneMinThickness width
|
2008-12-03 10:32:53 +00:00
|
|
|
* A scan is made line per line, on the whole filled areas, with a step of m_ZoneMinThickness.
|
|
|
|
* all intersecting points with the horizontal infinite line and polygons to fill are calculated
|
|
|
|
* a list of SEGZONE items is built, line per line
|
|
|
|
* @return number of segments created
|
2007-12-19 16:44:53 +00:00
|
|
|
*/
|
2010-12-14 15:56:30 +00:00
|
|
|
int ZONE_CONTAINER::Fill_Zone_Areas_With_Segments()
|
2007-12-19 16:44:53 +00:00
|
|
|
{
|
2008-12-03 10:32:53 +00:00
|
|
|
int ics, ice;
|
|
|
|
int count = 0;
|
|
|
|
std::vector <int> x_coordinates;
|
|
|
|
bool error = false;
|
2007-12-19 16:44:53 +00:00
|
|
|
|
2008-12-03 10:32:53 +00:00
|
|
|
int istart, iend; // index od the starting and the endif corner of one filled area in m_FilledPolysList
|
2007-12-19 16:44:53 +00:00
|
|
|
|
2008-12-03 10:32:53 +00:00
|
|
|
int margin = m_ZoneMinThickness * 2 / 10;
|
|
|
|
margin = max (2, margin);
|
|
|
|
int step = m_ZoneMinThickness - margin;
|
|
|
|
step = max(step, 2);
|
2007-12-19 16:44:53 +00:00
|
|
|
|
2008-12-03 10:32:53 +00:00
|
|
|
// Read all filled areas in m_FilledPolysList
|
2009-08-06 18:30:46 +00:00
|
|
|
m_FillSegmList.clear();
|
2008-12-03 10:32:53 +00:00
|
|
|
istart = 0;
|
|
|
|
int end_list = m_FilledPolysList.size()-1;
|
|
|
|
for( int ic = 0; ic <= end_list; ic++ )
|
2007-12-30 18:20:51 +00:00
|
|
|
{
|
2008-12-03 10:32:53 +00:00
|
|
|
CPolyPt* corner = &m_FilledPolysList[ic];
|
|
|
|
if ( corner->end_contour || (ic == end_list) )
|
2007-12-30 18:20:51 +00:00
|
|
|
{
|
2008-12-03 10:32:53 +00:00
|
|
|
iend = ic;
|
2011-03-29 19:33:07 +00:00
|
|
|
EDA_RECT rect = CalculateSubAreaBoundaryBox( istart, iend );
|
2007-12-19 16:44:53 +00:00
|
|
|
|
2008-12-03 10:32:53 +00:00
|
|
|
// Calculate the y limits of the zone
|
|
|
|
int refy = rect.GetY();
|
|
|
|
int endy = rect.GetBottom();
|
2007-12-19 16:44:53 +00:00
|
|
|
|
2008-12-03 10:32:53 +00:00
|
|
|
for( ; refy < endy; refy += step )
|
2007-12-19 16:44:53 +00:00
|
|
|
{
|
2008-12-03 10:32:53 +00:00
|
|
|
// find all intersection points of an infinite line with polyline sides
|
|
|
|
x_coordinates.clear();
|
|
|
|
for( ics = istart, ice = iend; ics <= iend; ice = ics, ics++ )
|
2007-12-19 16:44:53 +00:00
|
|
|
{
|
2008-12-03 10:32:53 +00:00
|
|
|
if ( m_FilledPolysList[ice].utility )
|
|
|
|
continue;
|
|
|
|
int seg_startX = m_FilledPolysList[ics].x;
|
|
|
|
int seg_startY = m_FilledPolysList[ics].y;
|
|
|
|
int seg_endX = m_FilledPolysList[ice].x;
|
|
|
|
int seg_endY = m_FilledPolysList[ice].y;
|
2009-01-05 05:21:35 +00:00
|
|
|
|
2008-12-03 10:32:53 +00:00
|
|
|
|
|
|
|
/* Trivial cases: skip if ref above or below the segment to test */
|
|
|
|
if( ( seg_startY > refy ) && (seg_endY > refy ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// segment below ref point, or its Y end pos on Y coordinate ref point: skip
|
|
|
|
if( ( seg_startY <= refy ) && (seg_endY <= refy ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* at this point refy is between seg_startY and seg_endY
|
|
|
|
* see if an horizontal line at Y = refy is intersecting this segment
|
|
|
|
*/
|
|
|
|
// calculate the x position of the intersection of this segment and the infinite line
|
|
|
|
// this is more easier if we move the X,Y axis origin to the segment start point:
|
|
|
|
seg_endX -= seg_startX;
|
|
|
|
seg_endY -= seg_startY;
|
|
|
|
double newrefy = (double) (refy - seg_startY);
|
|
|
|
double intersec_x;
|
|
|
|
if ( seg_endY == 0 ) // horizontal segment on the same line: skip
|
|
|
|
continue;
|
|
|
|
// Now calculate the x intersection coordinate of the horizontal line at y = newrefy
|
|
|
|
// and the segment from (0,0) to (seg_endX,seg_endY)
|
|
|
|
// with the horizontal line at the new refy position
|
|
|
|
// the line slope is slope = seg_endY/seg_endX; and inv_slope = seg_endX/seg_endY
|
|
|
|
// and the x pos relative to the new origin is intersec_x = refy/slope = refy * inv_slope
|
|
|
|
// Note: because horizontal segments are already tested and skipped, slope exists (seg_end_y not O)
|
|
|
|
double inv_slope = (double)seg_endX / seg_endY;
|
|
|
|
intersec_x = newrefy * inv_slope;
|
|
|
|
x_coordinates.push_back((int) intersec_x + seg_startX);
|
|
|
|
}
|
2007-12-30 18:20:51 +00:00
|
|
|
|
2008-12-03 10:32:53 +00:00
|
|
|
// A line scan is finished: build list of segments
|
2007-12-30 18:20:51 +00:00
|
|
|
|
2008-12-03 10:32:53 +00:00
|
|
|
// Sort intersection points by increasing x value:
|
|
|
|
// So 2 consecutive points are the ends of a segment
|
|
|
|
sort( x_coordinates.begin(), x_coordinates.end(), SortByXValues );
|
2007-12-30 18:20:51 +00:00
|
|
|
|
2008-12-03 10:32:53 +00:00
|
|
|
// Create segments
|
2007-12-30 18:20:51 +00:00
|
|
|
|
2008-12-03 10:32:53 +00:00
|
|
|
if ( !error && ( x_coordinates.size() & 1 ) != 0 )
|
|
|
|
{ // An even number of coordinates is expected, because a segment has 2 ends.
|
|
|
|
// An if this algorithm always works, it must always find an even count.
|
|
|
|
wxString msg = wxT("Fill Zone: odd number of points at y = ");
|
|
|
|
msg << refy;
|
|
|
|
wxMessageBox(msg );
|
|
|
|
error = true;
|
2007-12-19 16:44:53 +00:00
|
|
|
}
|
2009-01-05 05:21:35 +00:00
|
|
|
|
2011-09-23 13:57:12 +00:00
|
|
|
if ( error )
|
|
|
|
break;
|
|
|
|
|
2008-12-03 10:32:53 +00:00
|
|
|
int iimax = x_coordinates.size()-1;
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2008-12-03 10:32:53 +00:00
|
|
|
for (int ii = 0; ii < iimax; ii +=2 )
|
2007-12-19 16:44:53 +00:00
|
|
|
{
|
2008-12-03 10:32:53 +00:00
|
|
|
wxPoint seg_start, seg_end;
|
|
|
|
count++;
|
|
|
|
seg_start.x = x_coordinates[ii];
|
|
|
|
seg_start.y = refy;
|
|
|
|
seg_end.x = x_coordinates[ii+1];
|
|
|
|
seg_end.y = refy;
|
2009-08-06 18:30:46 +00:00
|
|
|
SEGMENT segment( seg_start, seg_end );
|
|
|
|
m_FillSegmList.push_back( segment );
|
2007-12-19 16:44:53 +00:00
|
|
|
}
|
2008-12-03 10:32:53 +00:00
|
|
|
} //End examine segments in one area
|
2011-09-23 13:57:12 +00:00
|
|
|
|
|
|
|
if ( error )
|
|
|
|
break;
|
|
|
|
|
2008-12-03 10:32:53 +00:00
|
|
|
istart = iend + 1; // istart points the first corner of the next area
|
|
|
|
} // End find one end of outline
|
2011-09-23 13:57:12 +00:00
|
|
|
|
|
|
|
if ( error )
|
|
|
|
break;
|
2008-12-03 10:32:53 +00:00
|
|
|
} // End examine all areas
|
2007-12-19 16:44:53 +00:00
|
|
|
|
2008-12-03 10:32:53 +00:00
|
|
|
return count;
|
2007-12-19 16:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|