poly2tri: throw exceptions instead of crashing on non-supported polygons

This commit is contained in:
Tomasz Włostowski 2017-12-14 00:32:09 +01:00
parent c18b638c17
commit 941ebe376c
5 changed files with 43 additions and 284 deletions

View File

@ -1,251 +0,0 @@
/**
* @file zones_convert_to_polygons_aux_functions.cpp
*/
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2013 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
* Copyright (C) 1992-2013 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <fctsys.h>
#include <PolyLine.h>
#include <wxPcbStruct.h>
#include <trigo.h>
#include <class_board.h>
#include <class_module.h>
#include <class_zone.h>
#include <pcbnew.h>
#include <zones.h>
/* Function TransformOutlinesShapeWithClearanceToPolygon
* Convert the zone filled areas polygons to polygons
* inflated (optional) by max( aClearanceValue, the zone clearance)
* and copy them in aCornerBuffer
* param aClearanceValue = the clearance around polygons
* param aAddClearance = true to add a clearance area to the polygon
* false to create the outline polygon.
*/
void ZONE_CONTAINER::TransformOutlinesShapeWithClearanceToPolygon(
SHAPE_POLY_SET& aCornerBuffer, int aMinClearanceValue, bool aUseNetClearance ) const
{
// Creates the zone outline polygon (with holes if any)
SHAPE_POLY_SET polybuffer;
BuildSmoothedPoly( polybuffer );
// add clearance to outline
int clearance = aMinClearanceValue;
if( aUseNetClearance && IsOnCopperLayer() )
{
clearance = GetClearance();
if( aMinClearanceValue > clearance )
clearance = aMinClearanceValue;
}
// Calculate the polygon with clearance
// holes are linked to the main outline, so only one polygon is created.
if( clearance )
polybuffer.Inflate( clearance, 16 );
polybuffer.Fracture( SHAPE_POLY_SET::PM_FAST );
aCornerBuffer.Append( polybuffer );
}
/**
* Function BuildUnconnectedThermalStubsPolygonList
* Creates a set of polygons corresponding to stubs created by thermal shapes on pads
* which are not connected to a zone (dangling bridges)
* @param aCornerBuffer = a SHAPE_POLY_SET where to store polygons
* @param aPcb = the board.
* @param aZone = a pointer to the ZONE_CONTAINER to examine.
* @param aArcCorrection = a pointer to the ZONE_CONTAINER to examine.
* @param aRoundPadThermalRotation = the rotation in 1.0 degree for thermal stubs in round pads
*/
void BuildUnconnectedThermalStubsPolygonList( SHAPE_POLY_SET& aCornerBuffer,
const BOARD* aPcb,
const ZONE_CONTAINER* aZone,
double aArcCorrection,
double aRoundPadThermalRotation )
{
std::vector<wxPoint> corners_buffer; // a local polygon buffer to store one stub
corners_buffer.reserve( 4 );
wxPoint ptTest[4];
int zone_clearance = aZone->GetZoneClearance();
EDA_RECT item_boundingbox;
EDA_RECT zone_boundingbox = aZone->GetBoundingBox();
int biggest_clearance = aPcb->GetDesignSettings().GetBiggestClearanceValue();
biggest_clearance = std::max( biggest_clearance, zone_clearance );
zone_boundingbox.Inflate( biggest_clearance );
// half size of the pen used to draw/plot zones outlines
int pen_radius = aZone->GetMinThickness() / 2;
for( MODULE* module = aPcb->m_Modules; module; module = module->Next() )
{
for( D_PAD* pad = module->PadsList(); pad != NULL; pad = pad->Next() )
{
// Rejects non-standard pads with tht-only thermal reliefs
if( aZone->GetPadConnection( pad ) == PAD_ZONE_CONN_THT_THERMAL
&& pad->GetAttribute() != PAD_ATTRIB_STANDARD )
continue;
if( aZone->GetPadConnection( pad ) != PAD_ZONE_CONN_THERMAL
&& aZone->GetPadConnection( pad ) != PAD_ZONE_CONN_THT_THERMAL )
continue;
// check
if( !pad->IsOnLayer( aZone->GetLayer() ) )
continue;
if( pad->GetNetCode() != aZone->GetNetCode() )
continue;
// Calculate thermal bridge half width
int thermalBridgeWidth = aZone->GetThermalReliefCopperBridge( pad )
- aZone->GetMinThickness();
if( thermalBridgeWidth <= 0 )
continue;
// we need the thermal bridge half width
// with a small extra size to be sure we create a stub
// slightly larger than the actual stub
thermalBridgeWidth = ( thermalBridgeWidth + 4 ) / 2;
int thermalReliefGap = aZone->GetThermalReliefGap( pad );
item_boundingbox = pad->GetBoundingBox();
item_boundingbox.Inflate( thermalReliefGap );
if( !( item_boundingbox.Intersects( zone_boundingbox ) ) )
continue;
// Thermal bridges are like a segment from a starting point inside the pad
// to an ending point outside the pad
// calculate the ending point of the thermal pad, outside the pad
wxPoint endpoint;
endpoint.x = ( pad->GetSize().x / 2 ) + thermalReliefGap;
endpoint.y = ( pad->GetSize().y / 2 ) + thermalReliefGap;
// Calculate the starting point of the thermal stub
// inside the pad
wxPoint startpoint;
int copperThickness = aZone->GetThermalReliefCopperBridge( pad )
- aZone->GetMinThickness();
if( copperThickness < 0 )
copperThickness = 0;
// Leave a small extra size to the copper area inside to pad
copperThickness += KiROUND( IU_PER_MM * 0.04 );
startpoint.x = std::min( pad->GetSize().x, copperThickness );
startpoint.y = std::min( pad->GetSize().y, copperThickness );
startpoint.x /= 2;
startpoint.y /= 2;
// This is a CIRCLE pad tweak
// for circle pads, the thermal stubs orientation is 45 deg
double fAngle = pad->GetOrientation();
if( pad->GetShape() == PAD_SHAPE_CIRCLE )
{
endpoint.x = KiROUND( endpoint.x * aArcCorrection );
endpoint.y = endpoint.x;
fAngle = aRoundPadThermalRotation;
}
// contour line width has to be taken into calculation to avoid "thermal stub bleed"
endpoint.x += pen_radius;
endpoint.y += pen_radius;
// compute north, south, west and east points for zone connection.
ptTest[0] = wxPoint( 0, endpoint.y ); // lower point
ptTest[1] = wxPoint( 0, -endpoint.y ); // upper point
ptTest[2] = wxPoint( endpoint.x, 0 ); // right point
ptTest[3] = wxPoint( -endpoint.x, 0 ); // left point
// Test all sides
for( int i = 0; i < 4; i++ )
{
// rotate point
RotatePoint( &ptTest[i], fAngle );
// translate point
ptTest[i] += pad->ShapePos();
if( aZone->HitTestFilledArea( ptTest[i] ) )
continue;
corners_buffer.clear();
// polygons are rectangles with width of copper bridge value
switch( i )
{
case 0: // lower stub
corners_buffer.push_back( wxPoint( -thermalBridgeWidth, endpoint.y ) );
corners_buffer.push_back( wxPoint( +thermalBridgeWidth, endpoint.y ) );
corners_buffer.push_back( wxPoint( +thermalBridgeWidth, startpoint.y ) );
corners_buffer.push_back( wxPoint( -thermalBridgeWidth, startpoint.y ) );
break;
case 1: // upper stub
corners_buffer.push_back( wxPoint( -thermalBridgeWidth, -endpoint.y ) );
corners_buffer.push_back( wxPoint( +thermalBridgeWidth, -endpoint.y ) );
corners_buffer.push_back( wxPoint( +thermalBridgeWidth, -startpoint.y ) );
corners_buffer.push_back( wxPoint( -thermalBridgeWidth, -startpoint.y ) );
break;
case 2: // right stub
corners_buffer.push_back( wxPoint( endpoint.x, -thermalBridgeWidth ) );
corners_buffer.push_back( wxPoint( endpoint.x, thermalBridgeWidth ) );
corners_buffer.push_back( wxPoint( +startpoint.x, thermalBridgeWidth ) );
corners_buffer.push_back( wxPoint( +startpoint.x, -thermalBridgeWidth ) );
break;
case 3: // left stub
corners_buffer.push_back( wxPoint( -endpoint.x, -thermalBridgeWidth ) );
corners_buffer.push_back( wxPoint( -endpoint.x, thermalBridgeWidth ) );
corners_buffer.push_back( wxPoint( -startpoint.x, thermalBridgeWidth ) );
corners_buffer.push_back( wxPoint( -startpoint.x, -thermalBridgeWidth ) );
break;
}
aCornerBuffer.NewOutline();
// add computed polygon to list
for( unsigned ic = 0; ic < corners_buffer.size(); ic++ )
{
wxPoint cpos = corners_buffer[ic];
RotatePoint( &cpos, fAngle ); // Rotate according to module orientation
cpos += pad->ShapePos(); // Shift origin to position
aCornerBuffer.Append( cpos.x, cpos.y );
}
}
}
}
}

View File

@ -30,6 +30,7 @@
*/ */
#include "shapes.h" #include "shapes.h"
#include <iostream> #include <iostream>
#include <exception>
namespace p2t { namespace p2t {
@ -52,7 +53,7 @@ void Triangle::MarkNeighbor(Point* p1, Point* p2, Triangle* t)
else if ((p1 == points_[0] && p2 == points_[1]) || (p1 == points_[1] && p2 == points_[0])) else if ((p1 == points_[0] && p2 == points_[1]) || (p1 == points_[1] && p2 == points_[0]))
neighbors_[2] = t; neighbors_[2] = t;
else else
assert(0); throw std::invalid_argument("Polygon contains overlapping hole vertices.");
} }
// Exhaustive search to update neighbor pointers // Exhaustive search to update neighbor pointers
@ -150,7 +151,7 @@ void Triangle::Legalize(Point& opoint, Point& npoint)
points_[2] = points_[1]; points_[2] = points_[1];
points_[1] = &npoint; points_[1] = &npoint;
} else { } else {
assert(0); throw std::invalid_argument("Polygon contains overlapping hole vertices.");
} }
} }
@ -163,7 +164,8 @@ int Triangle::Index(const Point* p)
} else if (p == points_[2]) { } else if (p == points_[2]) {
return 2; return 2;
} }
assert(0); throw std::invalid_argument("Polygon contains overlapping hole vertices.");
return 0;
} }
int Triangle::EdgeIndex(const Point* p1, const Point* p2) int Triangle::EdgeIndex(const Point* p1, const Point* p2)
@ -222,7 +224,8 @@ Point* Triangle::PointCW(Point& point)
} else if (&point == points_[2]) { } else if (&point == points_[2]) {
return points_[1]; return points_[1];
} }
assert(0); throw std::invalid_argument("Polygon contains overlapping hole vertices.");
return NULL;
} }
// The point counter-clockwise to given point // The point counter-clockwise to given point
@ -235,7 +238,8 @@ Point* Triangle::PointCCW(Point& point)
} else if (&point == points_[2]) { } else if (&point == points_[2]) {
return points_[0]; return points_[0];
} }
assert(0); throw std::invalid_argument("Polygon contains overlapping hole vertices.");
return NULL;
} }
// The neighbor clockwise to given point // The neighbor clockwise to given point
@ -345,14 +349,14 @@ void Triangle::SetDelunayEdgeCW(Point& p, bool e)
} }
// The neighbor across to given point // The neighbor across to given point
Triangle& Triangle::NeighborAcross(Point& opoint) Triangle* Triangle::NeighborAcross(Point& opoint)
{ {
if (&opoint == points_[0]) { if (&opoint == points_[0]) {
return *neighbors_[0]; return neighbors_[0];
} else if (&opoint == points_[1]) { } else if (&opoint == points_[1]) {
return *neighbors_[1]; return neighbors_[1];
} }
return *neighbors_[2]; return neighbors_[2];
} }
void Triangle::DebugPrint() void Triangle::DebugPrint()

View File

@ -45,19 +45,21 @@ struct Edge;
struct Point { struct Point {
double x, y; double x, y;
int id;
/// Default constructor does nothing (for performance). /// Default constructor does nothing (for performance).
Point() Point()
{ {
x = 0.0; x = 0.0;
y = 0.0; y = 0.0;
id = 0;
} }
/// The edges this point constitutes an upper ending point /// The edges this point constitutes an upper ending point
std::vector<Edge*> edge_list; std::vector<Edge*> edge_list;
/// Construct using coordinates. /// Construct using coordinates.
Point(double ax, double ay) : x(ax), y(ay) {} Point(double ax, double ay, int aid = 0) : x(ax), y(ay), id(aid) {}
/// Set this point to all zeros. /// Set this point to all zeros.
void set_zero() void set_zero()
@ -201,7 +203,7 @@ void ClearDelunayEdges();
inline bool IsInterior(); inline bool IsInterior();
inline void IsInterior(bool b); inline void IsInterior(bool b);
Triangle& NeighborAcross(Point& opoint); Triangle* NeighborAcross(Point& opoint);
void DebugPrint(); void DebugPrint();
@ -321,5 +323,3 @@ inline void Triangle::IsInterior(bool b)
} }
#endif #endif

View File

@ -118,11 +118,10 @@ void Sweep::EdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle* triangl
// We are modifying the constraint maybe it would be better to // We are modifying the constraint maybe it would be better to
// not change the given constraint and just keep a variable for the new constraint // not change the given constraint and just keep a variable for the new constraint
tcx.edge_event.constrained_edge->q = p1; tcx.edge_event.constrained_edge->q = p1;
triangle = &triangle->NeighborAcross(point); triangle = triangle->NeighborAcross(point);
EdgeEvent( tcx, ep, *p1, triangle, *p1 ); EdgeEvent( tcx, ep, *p1, triangle, *p1 );
} else { } else {
std::runtime_error("EdgeEvent - collinear points not supported"); std::runtime_error("EdgeEvent - collinear points not supported");
assert(0);
} }
return; return;
} }
@ -135,11 +134,10 @@ void Sweep::EdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle* triangl
// We are modifying the constraint maybe it would be better to // We are modifying the constraint maybe it would be better to
// not change the given constraint and just keep a variable for the new constraint // not change the given constraint and just keep a variable for the new constraint
tcx.edge_event.constrained_edge->q = p2; tcx.edge_event.constrained_edge->q = p2;
triangle = &triangle->NeighborAcross(point); triangle = triangle->NeighborAcross(point);
EdgeEvent( tcx, ep, *p2, triangle, *p2 ); EdgeEvent( tcx, ep, *p2, triangle, *p2 );
} else { } else {
std::runtime_error("EdgeEvent - collinear points not supported"); std::runtime_error("EdgeEvent - collinear points not supported");
assert(0);
} }
return; return;
} }
@ -699,32 +697,36 @@ void Sweep::FillLeftConcaveEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)
void Sweep::FlipEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle* t, Point& p) void Sweep::FlipEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle* t, Point& p)
{ {
Triangle& ot = t->NeighborAcross(p); Triangle* ot = t->NeighborAcross(p);
Point& op = *ot.OppositePoint(*t, p); Point& op = *ot->OppositePoint(*t, p);
if (ot == nullptr) {
throw std::invalid_argument("Polygon contains overlapping hole vertices.");
}
if (InScanArea(p, *t->PointCCW(p), *t->PointCW(p), op)) { if (InScanArea(p, *t->PointCCW(p), *t->PointCW(p), op)) {
// Lets rotate shared edge one vertex CW // Lets rotate shared edge one vertex CW
RotateTrianglePair(*t, p, ot, op); RotateTrianglePair(*t, p, *ot, op);
tcx.MapTriangleToNodes(*t); tcx.MapTriangleToNodes(*t);
tcx.MapTriangleToNodes(ot); tcx.MapTriangleToNodes(*ot);
if (p == eq && op == ep) { if (p == eq && op == ep) {
if (eq == *tcx.edge_event.constrained_edge->q && ep == *tcx.edge_event.constrained_edge->p) { if (eq == *tcx.edge_event.constrained_edge->q && ep == *tcx.edge_event.constrained_edge->p) {
t->MarkConstrainedEdge(&ep, &eq); t->MarkConstrainedEdge(&ep, &eq);
ot.MarkConstrainedEdge(&ep, &eq); ot->MarkConstrainedEdge(&ep, &eq);
Legalize(tcx, *t); Legalize(tcx, *t);
Legalize(tcx, ot); Legalize(tcx, *ot);
} else { } else {
// XXX: I think one of the triangles should be legalized here? // XXX: I think one of the triangles should be legalized here?
} }
} else { } else {
Orientation o = Orient2d(eq, op, ep); Orientation o = Orient2d(eq, op, ep);
t = &NextFlipTriangle(tcx, (int)o, *t, ot, p, op); t = &NextFlipTriangle(tcx, (int)o, *t, *ot, p, op);
FlipEdgeEvent(tcx, ep, eq, t, p); FlipEdgeEvent(tcx, ep, eq, t, p);
} }
} else { } else {
Point& newP = NextFlipPoint(ep, eq, ot, op); Point& newP = NextFlipPoint(ep, eq, *ot, op);
FlipScanEdgeEvent(tcx, ep, eq, *t, ot, newP); FlipScanEdgeEvent(tcx, ep, eq, *t, *ot, newP);
EdgeEvent(tcx, ep, eq, t, p); EdgeEvent(tcx, ep, eq, t, p);
} }
} }
@ -759,20 +761,24 @@ Point& Sweep::NextFlipPoint(Point& ep, Point& eq, Triangle& ot, Point& op)
// Left // Left
return *ot.PointCW(op); return *ot.PointCW(op);
} else{ } else{
//throw new RuntimeException("[Unsupported] Opposing point on constrained edge"); throw std::invalid_argument("Polygon contains overlapping hole vertices.");
assert(0); return ep; // Arbitrary return val -- fixes warning
} }
} }
void Sweep::FlipScanEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle& flip_triangle, void Sweep::FlipScanEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle& flip_triangle,
Triangle& t, Point& p) Triangle& t, Point& p)
{ {
Triangle& ot = t.NeighborAcross(p); Triangle* ot = t.NeighborAcross(p);
Point& op = *ot.OppositePoint(t, p); Point& op = *ot->OppositePoint(t, p);
if (ot == NULL) {
throw std::invalid_argument("Polygon contains overlapping hole vertices.");
}
if (InScanArea(eq, *flip_triangle.PointCCW(eq), *flip_triangle.PointCW(eq), op)) { if (InScanArea(eq, *flip_triangle.PointCCW(eq), *flip_triangle.PointCW(eq), op)) {
// flip with new edge op->eq // flip with new edge op->eq
FlipEdgeEvent(tcx, eq, op, &ot, op); FlipEdgeEvent(tcx, eq, op, ot, op);
// TODO: Actually I just figured out that it should be possible to // TODO: Actually I just figured out that it should be possible to
// improve this by getting the next ot and op before the the above // improve this by getting the next ot and op before the the above
// flip and continue the flipScanEdgeEvent here // flip and continue the flipScanEdgeEvent here
@ -781,8 +787,8 @@ void Sweep::FlipScanEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle&
// Turns out at first glance that this is somewhat complicated // Turns out at first glance that this is somewhat complicated
// so it will have to wait. // so it will have to wait.
} else{ } else{
Point& newP = NextFlipPoint(ep, eq, ot, op); Point& newP = NextFlipPoint(ep, eq, *ot, op);
FlipScanEdgeEvent(tcx, ep, eq, flip_triangle, ot, newP); FlipScanEdgeEvent(tcx, ep, eq, flip_triangle, *ot, newP);
} }
} }