From 026b9301c5a47b3660bde45bdd78498e2cb4a93b Mon Sep 17 00:00:00 2001
From: curt <curt>
Date: Thu, 29 Apr 1999 04:30:24 +0000
Subject: [PATCH] Fixed several problems with "hole" support, but it appears
 that things are working much better.  Crater lake now has an island which is
 always a good thing. :-)

---
 Tools/Construct/Clipper/clipper.cxx      |  6 ++--
 Tools/Construct/Makefile.am              |  1 +
 Tools/Construct/Triangulate/polygon.cxx  | 19 ++++++++-----
 Tools/Construct/Triangulate/triangle.cxx | 36 +++++++++++++++++-------
 4 files changed, 43 insertions(+), 19 deletions(-)

diff --git a/Tools/Construct/Clipper/clipper.cxx b/Tools/Construct/Clipper/clipper.cxx
index 501abcdf1..27b54bddd 100644
--- a/Tools/Construct/Clipper/clipper.cxx
+++ b/Tools/Construct/Clipper/clipper.cxx
@@ -56,6 +56,7 @@ bool FGClipper::load_polys(const string& path) {
     string poly_name;
     AreaType poly_type = DefaultArea;
     int contours, count, i, j;
+    int hole_flag;
     double startx, starty, x, y, lastx, lasty;
 
     FG_LOG( FG_CLIPPER, FG_INFO, "Loading " << path << " ..." );
@@ -89,6 +90,8 @@ bool FGClipper::load_polys(const string& path) {
 		exit(-1);
 	    }
 
+	    in >> hole_flag;
+
 	    in >> startx;
 	    in >> starty;
 	    v_list.vertex[0].x = startx;
@@ -119,8 +122,7 @@ bool FGClipper::load_polys(const string& path) {
 			<< lastx << ", " << lasty );
 	    }
 
-	    gpc_add_contour( poly, &v_list, 0 );
-
+	    gpc_add_contour( poly, &v_list, hole_flag );
 	}
 
 	in >> skipcomment;
diff --git a/Tools/Construct/Makefile.am b/Tools/Construct/Makefile.am
index f19226325..79fc6b321 100644
--- a/Tools/Construct/Makefile.am
+++ b/Tools/Construct/Makefile.am
@@ -3,5 +3,6 @@ SUBDIRS = \
 	Clipper \
 	Combine \
 	GenOutput \
+	Match \
 	Triangulate \
 	Main
diff --git a/Tools/Construct/Triangulate/polygon.cxx b/Tools/Construct/Triangulate/polygon.cxx
index 5e8d6a6b8..6020d9595 100644
--- a/Tools/Construct/Triangulate/polygon.cxx
+++ b/Tools/Construct/Triangulate/polygon.cxx
@@ -124,10 +124,10 @@ void FGPolygon::calc_point_inside( const int contour,
 
     if ( min_node_index == 0 ) {
 	p1 = trinodes.get_node( poly[contour][1] );
-	p2 = trinodes.get_node( poly[contour][poly.size() - 1] );
+	p2 = trinodes.get_node( poly[contour][poly[contour].size() - 1] );
     } else if ( min_node_index == (int)(poly[contour].size()) - 1 ) {
 	p1 = trinodes.get_node( poly[contour][0] );
-	p2 = trinodes.get_node( poly[contour][poly.size() - 1] );
+	p2 = trinodes.get_node( poly[contour][poly[contour].size() - 2] );
     } else {
 	p1 = trinodes.get_node( poly[contour][min_node_index - 1] );
 	p2 = trinodes.get_node( poly[contour][min_node_index + 1] );
@@ -151,9 +151,12 @@ void FGPolygon::calc_point_inside( const int contour,
     p3.sety(100);
     
     for ( int i = 0; i < (int)poly.size(); ++i ) {
-	for ( int j = 0; j < (int)(poly[i].size()) - 1; ++j ) {
-	    p1 = trinodes.get_node( poly[contour][j] );
-	    p2 = trinodes.get_node( poly[contour][j+1] );
+	cout << "contour = " << i << " size = " << poly[i].size() << endl;
+	for ( int j = 0; j < (int)(poly[i].size() - 1); ++j ) {
+	    cout << "  p1 = " << poly[i][j] << " p2 = " 
+		 << poly[i][j+1] << endl;
+	    p1 = trinodes.get_node( poly[i][j] );
+	    p2 = trinodes.get_node( poly[i][j+1] );
 
 	    if ( intersects(p1, p2, m.x(), &result) ) {
 		// cout << "intersection = " << result << endl;
@@ -163,8 +166,10 @@ void FGPolygon::calc_point_inside( const int contour,
 		}
 	    }
 	}
-	p1 = trinodes.get_node( poly[contour][0] );
-	p2 = trinodes.get_node( poly[contour][poly.size() - 1] );
+	cout << "  p1 = " << poly[i][0] << " p2 = " 
+	     << poly[i][poly[i].size() - 1] << endl;
+	p1 = trinodes.get_node( poly[i][0] );
+	p2 = trinodes.get_node( poly[i][poly[i].size() - 1] );
 	if ( intersects(p1, p2, m.x(), &result) ) {
 	    // cout << "intersection = " << result << endl;
 	    if ( ( result.y() < p3.y() ) &&
diff --git a/Tools/Construct/Triangulate/triangle.cxx b/Tools/Construct/Triangulate/triangle.cxx
index 2db6bc6b4..80a8d5d38 100644
--- a/Tools/Construct/Triangulate/triangle.cxx
+++ b/Tools/Construct/Triangulate/triangle.cxx
@@ -98,8 +98,9 @@ FGTriangle::build( const point_list& corner_list,
 	    int j;
 
 	    for ( j = 0; j < gpc_poly->num_contours; j++ ) {
-		cout << " processing contour = " << j << ", nodes = " 
-		     << gpc_poly->contour[j].num_vertices << endl;
+		cout << "  processing contour = " << j << ", nodes = " 
+		     << gpc_poly->contour[j].num_vertices << ", hole = "
+		     << gpc_poly->hole[j] << endl;
 
 		// sprintf(junkn, "g.%d", junkc++);
 		// junkfp = fopen(junkn, "w");
@@ -112,7 +113,7 @@ FGTriangle::build( const point_list& corner_list,
 		    // junkp = in_nodes.get_node( index );
 		    // fprintf(junkfp, "%.4f %.4f\n", junkp.x(), junkp.y());
 		    poly.add_node(j, index);
-		    // cout << index << endl;
+		    cout << "  - " << index << endl;
 		}
 		// fprintf(junkfp, "%.4f %.4f\n", 
 		//    gpc_poly->contour[j].vertex[0].x, 
@@ -120,12 +121,13 @@ FGTriangle::build( const point_list& corner_list,
 		// fclose(junkfp);
 
 		poly.set_hole_flag( j, gpc_poly->hole[j] );
-		polylist[i].push_back( poly );
 	    }
 
 	    for ( j = 0; j < gpc_poly->num_contours; j++ ) {
 		poly.calc_point_inside( j, in_nodes );
 	    }
+
+	    polylist[i].push_back( poly );
 	}
     }
 
@@ -299,7 +301,17 @@ int FGTriangle::run_triangulate() {
     // region list
     in.numberofregions = 0;
     for ( int i = 0; i < FG_MAX_AREA_TYPES; ++i ) {
-	in.numberofregions += polylist[i].size();
+	poly_list_iterator h_current, h_last;
+	h_current = polylist[i].begin();
+	h_last = polylist[i].end();
+	for ( ; h_current != h_last; ++h_current ) {
+	    poly = *h_current;
+	    for ( int j = 0; j < poly.contours(); j++ ) {
+		if ( ! poly.get_hole_flag( j ) ) {
+		    ++in.numberofregions;
+		}
+	    }
+	}
     }
 
     in.regionlist = (REAL *) malloc(in.numberofregions * 4 * sizeof(REAL));
@@ -311,11 +323,15 @@ int FGTriangle::run_triangulate() {
 	for ( ; h_current != h_last; ++h_current ) {
 	    poly = *h_current;
 	    for ( int j = 0; j < poly.contours(); j++ ) {
-		p = poly.get_point_inside( j );
-		in.regionlist[counter++] = p.x();  // x coord
-		in.regionlist[counter++] = p.y();  // y coord
-		in.regionlist[counter++] = i;      // region attribute
-		in.regionlist[counter++] = -1.0;   // area constraint (unused)
+		if ( ! poly.get_hole_flag( j ) ) {
+		    p = poly.get_point_inside( j );
+		    cout << "Region point = " << p << endl;
+		    in.regionlist[counter++] = p.x();  // x coord
+		    in.regionlist[counter++] = p.y();  // y coord
+		    in.regionlist[counter++] = i;      // region attribute
+		    in.regionlist[counter++] = -1.0;   // area constraint
+		                                       // (unused)
+		}
 	    }
 	}
     }