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
|
|
|
*/
|
2012-06-08 09:56:42 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2016-07-14 07:27:32 +00:00
|
|
|
* Copyright (C) 2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
|
|
|
|
* Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
|
2012-06-08 09:56:42 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
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>
|
2016-07-14 07:27:32 +00:00
|
|
|
#include <convert_basic_shapes_to_polygon.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>
|
2007-12-19 16:44:53 +00:00
|
|
|
|
2013-05-01 19:01:14 +00:00
|
|
|
/* Build the filled solid areas data from real outlines (stored in m_Poly)
|
|
|
|
* The solid areas can be more than one on copper layers, and do not have holes
|
|
|
|
( holes are linked by overlapping segments to the main outline)
|
|
|
|
* aPcb: the current board (can be NULL for non copper zones)
|
|
|
|
* aCornerBuffer: A reference to a buffer to store polygon corners, or NULL
|
2014-11-30 17:07:02 +00:00
|
|
|
* if aCornerBuffer == NULL:
|
2013-05-01 19:01:14 +00:00
|
|
|
* - m_FilledPolysList is used to store solid areas polygons.
|
|
|
|
* - on copper layers, tracks and other items shapes of other nets are
|
|
|
|
* removed from solid areas
|
|
|
|
* if not null:
|
|
|
|
* Only the zone outline (with holes, if any) are stored in aCornerBuffer
|
2014-11-30 17:07:02 +00:00
|
|
|
* with holes linked. Therefore only one polygon is created
|
2013-05-01 19:01:14 +00:00
|
|
|
* This function calls AddClearanceAreasPolygonsToPolysList()
|
|
|
|
* to add holes for pads and tracks and other items not in net.
|
|
|
|
*/
|
2007-12-19 16:44:53 +00:00
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
bool ZONE_CONTAINER::BuildFilledSolidAreasPolygons( BOARD* aPcb, SHAPE_POLY_SET* aOutlineBuffer )
|
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 ...
|
2016-07-14 07:27:32 +00:00
|
|
|
return false;
|
2008-11-28 20:10:05 +00:00
|
|
|
|
2011-02-21 19:43:59 +00:00
|
|
|
// Make a smoothed polygon out of the user-drawn polygon if required
|
2012-07-13 18:55:29 +00:00
|
|
|
if( m_smoothedPoly )
|
2012-01-10 20:12:46 +00:00
|
|
|
{
|
2012-07-13 18:55:29 +00:00
|
|
|
delete m_smoothedPoly;
|
|
|
|
m_smoothedPoly = NULL;
|
2011-02-21 19:43:59 +00:00
|
|
|
}
|
|
|
|
|
2012-07-13 18:55:29 +00:00
|
|
|
switch( m_cornerSmoothingType )
|
2011-02-21 19:43:59 +00:00
|
|
|
{
|
2012-02-06 05:44:19 +00:00
|
|
|
case ZONE_SETTINGS::SMOOTHING_CHAMFER:
|
2017-03-07 12:06:00 +00:00
|
|
|
m_smoothedPoly = new SHAPE_POLY_SET();
|
|
|
|
*m_smoothedPoly = m_Poly->Chamfer( m_cornerRadius );
|
2011-02-21 19:43:59 +00:00
|
|
|
break;
|
2014-03-06 10:49:08 +00:00
|
|
|
|
2012-02-06 05:44:19 +00:00
|
|
|
case ZONE_SETTINGS::SMOOTHING_FILLET:
|
2017-03-07 12:06:00 +00:00
|
|
|
m_smoothedPoly = new SHAPE_POLY_SET();
|
|
|
|
*m_smoothedPoly = m_Poly->Fillet( m_cornerRadius, m_ArcToSegmentsCount );
|
2011-02-21 19:43:59 +00:00
|
|
|
break;
|
2014-03-06 10:49:08 +00:00
|
|
|
|
2011-02-21 19:43:59 +00:00
|
|
|
default:
|
2015-05-30 12:02:55 +00:00
|
|
|
// Acute angles between adjacent edges can create issues in calculations,
|
|
|
|
// in inflate/deflate outlines transforms, especially when the angle is very small.
|
|
|
|
// We can avoid issues by creating a very small chamfer which remove acute angles,
|
|
|
|
// or left it without chamfer and use only CPOLYGONS_LIST::InflateOutline to create
|
|
|
|
// clearance areas
|
2017-03-07 12:06:00 +00:00
|
|
|
m_smoothedPoly = new SHAPE_POLY_SET();
|
|
|
|
*m_smoothedPoly = m_Poly->Chamfer( Millimeter2iu( 0.0 ) );
|
2011-02-21 19:43:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-11-30 17:07:02 +00:00
|
|
|
if( aOutlineBuffer )
|
2017-03-07 12:06:00 +00:00
|
|
|
aOutlineBuffer->Append( *m_smoothedPoly );
|
2012-08-04 16:33:04 +00:00
|
|
|
|
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
|
2016-07-14 07:27:32 +00:00
|
|
|
* For non copper layers, just recalculate the m_FilledPolysList
|
2012-08-04 16:33:04 +00:00
|
|
|
* with m_ZoneMinThickness taken in account
|
2008-12-03 10:32:53 +00:00
|
|
|
*/
|
2014-11-30 17:07:02 +00:00
|
|
|
else
|
2012-01-29 19:29:19 +00:00
|
|
|
{
|
2014-11-30 17:07:02 +00:00
|
|
|
m_FilledPolysList.RemoveAllContours();
|
|
|
|
|
2012-01-29 19:29:19 +00:00
|
|
|
if( IsOnCopperLayer() )
|
2015-06-12 15:13:18 +00:00
|
|
|
{
|
2015-07-27 19:45:57 +00:00
|
|
|
AddClearanceAreasPolygonsToPolysList_NG( aPcb );
|
2016-07-14 07:27:32 +00:00
|
|
|
|
|
|
|
if( m_FillMode ) // if fill mode uses segments, create them:
|
|
|
|
{
|
|
|
|
if( !FillZoneAreasWithSegments() )
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-12 15:13:18 +00:00
|
|
|
}
|
2012-08-04 16:33:04 +00:00
|
|
|
else
|
|
|
|
{
|
2016-07-14 07:27:32 +00:00
|
|
|
m_FillMode = 0; // Fill by segments is no more used in non copper layers
|
|
|
|
// force use solid polygons (usefull only for old boards)
|
2017-03-07 12:06:00 +00:00
|
|
|
m_FilledPolysList = *m_smoothedPoly;
|
2016-07-14 07:27:32 +00:00
|
|
|
|
|
|
|
// The filled areas are deflated by -m_ZoneMinThickness / 2, because
|
|
|
|
// the outlines are drawn with a line thickness = m_ZoneMinThickness to
|
|
|
|
// give a good shape with the minimal thickness
|
|
|
|
m_FilledPolysList.Inflate( -m_ZoneMinThickness / 2, 16 );
|
2015-12-15 20:21:25 +00:00
|
|
|
m_FilledPolysList.Fracture( SHAPE_POLY_SET::PM_FAST );
|
2012-08-04 16:33:04 +00:00
|
|
|
}
|
2014-03-06 10:49:08 +00:00
|
|
|
|
2014-11-30 17:07:02 +00:00
|
|
|
m_IsFilled = true;
|
|
|
|
}
|
2014-03-06 10:49:08 +00:00
|
|
|
|
2014-11-30 17:07:02 +00:00
|
|
|
return true;
|
2008-11-28 20:10:05 +00:00
|
|
|
}
|
|
|
|
|
2013-03-20 14:50:12 +00:00
|
|
|
|
2016-07-14 07:27:32 +00:00
|
|
|
/** Helper function fillPolygonWithHorizontalSegments
|
|
|
|
* fills a polygon with horizontal segments.
|
|
|
|
* It can be used for any angle, if the zone outline to fill is rotated by this angle
|
|
|
|
* and the result is rotated by -angle
|
|
|
|
* @param aPolygon = a SHAPE_LINE_CHAIN polygon to fill
|
|
|
|
* @param aFillSegmList = a std::vector <SEGMENT> which will be populated by filling segments
|
|
|
|
* @param aStep = the horizontal grid size
|
|
|
|
*/
|
|
|
|
bool fillPolygonWithHorizontalSegments( const SHAPE_LINE_CHAIN& aPolygon,
|
|
|
|
std::vector <SEGMENT>& aFillSegmList, int aStep );
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2016-07-14 07:27:32 +00:00
|
|
|
bool ZONE_CONTAINER::FillZoneAreasWithSegments()
|
2007-12-19 16:44:53 +00:00
|
|
|
{
|
2016-07-14 07:27:32 +00:00
|
|
|
bool success = true;
|
|
|
|
// segments are on something like a grid. Give it a minimal size
|
|
|
|
// to avoid too many segments, and use the m_ZoneMinThickness when (this is usually the case)
|
|
|
|
// the size is > mingrid_size.
|
|
|
|
// This is not perfect, but the actual purpose of this code
|
|
|
|
// is to allow filling zones on a grid, with grid size > m_ZoneMinThickness,
|
|
|
|
// in order to have really a grid.
|
|
|
|
//
|
|
|
|
// Using a user selectable grid size is for future Kicad versions.
|
|
|
|
// For now the area is fully filled.
|
|
|
|
int mingrid_size = Millimeter2iu( 0.05 );
|
2016-07-14 08:22:23 +00:00
|
|
|
int grid_size = std::max( mingrid_size, m_ZoneMinThickness );
|
2016-07-14 07:27:32 +00:00
|
|
|
// Make segments slightly overlapping to ensure a good full filling
|
|
|
|
grid_size -= grid_size/20;
|
|
|
|
|
|
|
|
// All filled areas are in m_FilledPolysList
|
|
|
|
// m_FillSegmList will contain the horizontal and vertical segments
|
|
|
|
// the segment width is m_ZoneMinThickness.
|
2009-08-06 18:30:46 +00:00
|
|
|
m_FillSegmList.clear();
|
2013-03-20 14:50:12 +00:00
|
|
|
|
2016-07-14 07:27:32 +00:00
|
|
|
// Creates the horizontal segments
|
2015-07-27 19:45:57 +00:00
|
|
|
for ( int index = 0; index < m_FilledPolysList.OutlineCount(); index++ )
|
2007-12-30 18:20:51 +00:00
|
|
|
{
|
2016-07-14 07:27:32 +00:00
|
|
|
const SHAPE_LINE_CHAIN& outline0 = m_FilledPolysList.COutline( index );
|
|
|
|
success = fillPolygonWithHorizontalSegments( outline0, m_FillSegmList, grid_size );
|
|
|
|
|
|
|
|
if( !success )
|
|
|
|
break;
|
2015-07-27 19:45:57 +00:00
|
|
|
|
2016-07-14 07:27:32 +00:00
|
|
|
// Creates the vertical segments. Because the filling algo creates horizontal segments,
|
|
|
|
// to reuse the fillPolygonWithHorizontalSegments function, we rotate the polygons to fill
|
|
|
|
// then fill them, then inverse rotate the result
|
|
|
|
SHAPE_LINE_CHAIN outline90;
|
|
|
|
outline90.Append( outline0 );
|
|
|
|
|
|
|
|
// Rotate 90 degrees the outline:
|
|
|
|
for( int ii = 0; ii < outline90.PointCount(); ii++ )
|
2007-12-30 18:20:51 +00:00
|
|
|
{
|
2016-07-14 07:27:32 +00:00
|
|
|
VECTOR2I& point = outline90.Point( ii );
|
|
|
|
std::swap( point.x, point.y );
|
|
|
|
point.y = -point.y;
|
|
|
|
}
|
2007-12-19 16:44:53 +00:00
|
|
|
|
2016-07-14 07:27:32 +00:00
|
|
|
int first_point = m_FillSegmList.size();
|
|
|
|
success = fillPolygonWithHorizontalSegments( outline90, m_FillSegmList, grid_size );
|
2015-07-27 19:45:57 +00:00
|
|
|
|
2016-07-14 07:27:32 +00:00
|
|
|
if( !success )
|
|
|
|
break;
|
2015-07-27 19:45:57 +00:00
|
|
|
|
2016-07-14 07:27:32 +00:00
|
|
|
// Rotate -90 degrees the segments:
|
|
|
|
for( unsigned ii = first_point; ii < m_FillSegmList.size(); ii++ )
|
|
|
|
{
|
|
|
|
SEGMENT& segm = m_FillSegmList[ii];
|
|
|
|
std::swap( segm.m_Start.x, segm.m_Start.y );
|
|
|
|
std::swap( segm.m_End.x, segm.m_End.y );
|
|
|
|
segm.m_Start.x = - segm.m_Start.x;
|
|
|
|
segm.m_End.x = - segm.m_End.x;
|
|
|
|
}
|
|
|
|
}
|
2015-07-27 19:45:57 +00:00
|
|
|
|
2016-07-14 07:27:32 +00:00
|
|
|
if( success )
|
|
|
|
m_IsFilled = true;
|
|
|
|
else
|
|
|
|
m_FillSegmList.clear();
|
2015-07-27 19:45:57 +00:00
|
|
|
|
2016-07-14 07:27:32 +00:00
|
|
|
return success;
|
|
|
|
}
|
2015-07-27 19:45:57 +00:00
|
|
|
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2016-07-14 07:27:32 +00:00
|
|
|
bool fillPolygonWithHorizontalSegments( const SHAPE_LINE_CHAIN& aPolygon,
|
|
|
|
std::vector <SEGMENT>& aFillSegmList, int aStep )
|
|
|
|
{
|
|
|
|
std::vector <int> x_coordinates;
|
|
|
|
bool success = true;
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2016-07-14 07:27:32 +00:00
|
|
|
// Creates the horizontal segments
|
|
|
|
const SHAPE_LINE_CHAIN& outline = aPolygon;
|
|
|
|
const BOX2I& rect = outline.BBox();
|
2015-07-27 19:45:57 +00:00
|
|
|
|
2016-07-14 07:27:32 +00:00
|
|
|
// Calculate the y limits of the zone
|
|
|
|
for( int refy = rect.GetY(), endy = rect.GetBottom(); refy < endy; refy += aStep )
|
|
|
|
{
|
|
|
|
// find all intersection points of an infinite line with polyline sides
|
|
|
|
x_coordinates.clear();
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2016-07-14 07:27:32 +00:00
|
|
|
for( int v = 0; v < outline.PointCount(); v++ )
|
|
|
|
{
|
2007-12-19 16:44:53 +00:00
|
|
|
|
2016-07-14 07:27:32 +00:00
|
|
|
int seg_startX = outline.CPoint( v ).x;
|
|
|
|
int seg_startY = outline.CPoint( v ).y;
|
|
|
|
int seg_endX = outline.CPoint( v + 1 ).x;
|
|
|
|
int seg_endY = outline.CPoint( v + 1 ).y;
|
|
|
|
|
|
|
|
/* 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 );
|
|
|
|
}
|
2014-03-06 10:49:08 +00:00
|
|
|
|
2016-07-14 07:27:32 +00:00
|
|
|
// A line scan is finished: build list of segments
|
|
|
|
|
|
|
|
// Sort intersection points by increasing x value:
|
|
|
|
// So 2 consecutive points are the ends of a segment
|
2016-07-14 08:22:23 +00:00
|
|
|
sort( x_coordinates.begin(), x_coordinates.end() );
|
2016-07-14 07:27:32 +00:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
if( ( x_coordinates.size() & 1 ) != 0 )
|
|
|
|
{
|
|
|
|
success = false;
|
|
|
|
break;
|
|
|
|
}
|
2007-12-19 16:44:53 +00:00
|
|
|
|
2016-07-14 07:27:32 +00:00
|
|
|
// Create segments having the same Y coordinate
|
|
|
|
int iimax = x_coordinates.size() - 1;
|
2007-12-19 16:44:53 +00:00
|
|
|
|
2016-07-14 07:27:32 +00:00
|
|
|
for( int ii = 0; ii < iimax; ii += 2 )
|
|
|
|
{
|
|
|
|
wxPoint seg_start, seg_end;
|
|
|
|
seg_start.x = x_coordinates[ii];
|
|
|
|
seg_start.y = refy;
|
|
|
|
seg_end.x = x_coordinates[ii + 1];
|
|
|
|
seg_end.y = refy;
|
|
|
|
SEGMENT segment( seg_start, seg_end );
|
|
|
|
aFillSegmList.push_back( segment );
|
|
|
|
}
|
|
|
|
} // End examine segments in one area
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|