From 38bb74f3394d269b2713776bd84172b5d5a37d0f Mon Sep 17 00:00:00 2001 From: curt Date: Fri, 22 Aug 2003 17:34:56 +0000 Subject: [PATCH] Add a routine to catch/remove some addition degeneracy our polygon clipper can produce. --- src/Lib/Geometry/poly_support.cxx | 43 ++++++++++++++++++++++++++++++- src/Lib/Geometry/poly_support.hxx | 7 +++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/Lib/Geometry/poly_support.cxx b/src/Lib/Geometry/poly_support.cxx index f5e42960..e042e93f 100644 --- a/src/Lib/Geometry/poly_support.cxx +++ b/src/Lib/Geometry/poly_support.cxx @@ -510,7 +510,7 @@ static void contour_tesselate( TGContourNode *node, const TGPolygon &p, cout << "contour = " << contour_num << " nodes = " << total_pts << endl; #if 0 - // testing ... don't enable this otherwise + // testing ... don't enable this if not testing if ( contour_num != 0 || total_pts != 28 ) { out_pts.push_back( Point3D(0, 0, 0) ); out_pts.push_back( Point3D(0, 1, 0) ); @@ -1222,6 +1222,47 @@ TGPolygon reduce_degeneracy( const TGPolygon& poly ) { } +static point_list remove_contour_cycles( const point_list& contour ) { + point_list result; + result.clear(); + + unsigned int i = 0; + while ( i < contour.size() ) { + result.push_back( contour[i] ); + for ( unsigned int j = i + 1; j < contour.size(); ++j ) { + if ( contour[i] == contour[j] && i + 2 > j ) { + cout << "detected a small cycle: i = " + << i << " j = " << j << endl; + for ( int k = i; k <= j; ++k ) { + cout << " " << contour[k] << endl; + } + // i = j; + } + } + ++i; + } + + return result; +} + + +// Occasionally the outline of the clipped polygon can take a side +// track, then double back on return to the start of the side track +// branch and continue normally. Attempt to detect and clear this +// extraneous nonsense. +TGPolygon remove_cycles( const TGPolygon& poly ) { + TGPolygon result; + // cout << "remove cycles: " << poly << endl; + for ( int i = 0; i < poly.contours(); ++i ) { + point_list contour = poly.get_contour(i); + contour = remove_contour_cycles( contour ); + result.add_contour( contour, poly.get_hole_flag(i) ); + } + + return result; +} + + // remove any degenerate contours TGPolygon remove_bad_contours( const TGPolygon &poly ) { TGPolygon result; diff --git a/src/Lib/Geometry/poly_support.hxx b/src/Lib/Geometry/poly_support.hxx index c9db8f0d..961de4c8 100644 --- a/src/Lib/Geometry/poly_support.hxx +++ b/src/Lib/Geometry/poly_support.hxx @@ -86,6 +86,13 @@ TGPolygon remove_dups( const TGPolygon &poly ); TGPolygon reduce_degeneracy( const TGPolygon& poly ); +// Occasionally the outline of the clipped polygon can take a side +// track, then double back on return to the start of the side track +// branch and continue normally. Attempt to detect and clear this +// extraneous nonsense. +TGPolygon remove_cycles( const TGPolygon& poly ); + + // Find a point in the given node list that lies between start and // end, return true if something found, false if nothing found. bool find_intermediate_node( const Point3D& start, const Point3D& end,