From 59d30a34e8d831abf024e599cddd05fa0a3a9e91 Mon Sep 17 00:00:00 2001
From: Peter Sadrozinski <pete@petews.sadrohome>
Date: Sun, 18 Mar 2012 10:09:53 -0400
Subject: [PATCH] A fix for LFPG problem in expand poly.  If there are dups
 when expanding, the resultant poly was huge, and I could run out of memory.
 For closed polys, remove_dups() after snapping.

Also cleaned up the dump_xxx debug command line switches.
- now 1 based, not 0.
- debug_cp directory is the datasource name, so the directory need
  not exist before running genapt850.
---
 src/Airports/GenAirports850/airport.cxx    | 22 +++----
 src/Airports/GenAirports850/closedpoly.cxx | 70 +++++++++++++++++-----
 2 files changed, 67 insertions(+), 25 deletions(-)

diff --git a/src/Airports/GenAirports850/airport.cxx b/src/Airports/GenAirports850/airport.cxx
index 25f7694e..01deaa39 100644
--- a/src/Airports/GenAirports850/airport.cxx
+++ b/src/Airports/GenAirports850/airport.cxx
@@ -86,10 +86,10 @@ Airport::Airport( int c, char* def)
     altitude *= SG_FEET_TO_METER;
     boundary = NULL;
 
-    dbg_rwy_poly  = -1;
-    dbg_pvmt_poly = -1;
-    dbg_feat_poly = -1;
-    dbg_base_poly = -1;
+    dbg_rwy_poly  = 0;
+    dbg_pvmt_poly = 0;
+    dbg_feat_poly = 0;
+    dbg_base_poly = 0;
 
 
     SG_LOG( SG_GENERAL, SG_DEBUG, "Created airport with icao " << icao << ", control tower " << ct << ", and description " << description );
@@ -518,7 +518,7 @@ void Airport::BuildBtg(const string& root, const string_list& elev_src )
         {
             SG_LOG(SG_GENERAL, SG_INFO, "Build Feature Poly " << i + 1 << " of " << features.size() << " : " << features[i]->GetDescription() );
 
-            if ( (dbg_feat_poly >= 0) && (i == (unsigned int)dbg_feat_poly) ) {
+            if ( (dbg_feat_poly > 0) && (i == (unsigned int)dbg_feat_poly-1) ) {
                 SG_LOG(SG_GENERAL, SG_INFO, "Problem feat poly (" << i << ")");
 
                 make_shapefiles = true;
@@ -601,7 +601,7 @@ void Airport::BuildBtg(const string& root, const string_list& elev_src )
             SG_LOG(SG_GENERAL, SG_INFO, "Build Pavement " << i + 1 << " of " << pavements.size() << " : " << pavements[i]->GetDescription());
             slivers.clear();
 
-            if ( (dbg_pvmt_poly >= 0) && (i == (unsigned int)dbg_pvmt_poly) ) {
+            if ( (dbg_pvmt_poly > 0) && (i == (unsigned int)dbg_pvmt_poly-1) ) {
                 SG_LOG(SG_GENERAL, SG_INFO, "Problem pvmt poly (" << i << ")");
 
                 make_shapefiles = true;
@@ -810,6 +810,7 @@ void Airport::BuildBtg(const string& root, const string_list& elev_src )
     	TGPolygon poly = rwy_polys[k].get_poly();
         poly = snap(poly, gSnap);
         poly = remove_dups( poly );
+        poly = remove_bad_contours( poly );
     	rwy_polys[k].set_poly( poly );
     }
     for ( unsigned int k = 0; k < pvmt_polys.size(); ++k ) 
@@ -817,6 +818,7 @@ void Airport::BuildBtg(const string& root, const string_list& elev_src )
     	TGPolygon poly = pvmt_polys[k].get_poly();
         poly = snap(poly, gSnap);
         poly = remove_dups( poly );
+        poly = remove_bad_contours( poly );
     	pvmt_polys[k].set_poly( poly );
     }
 
@@ -832,7 +834,7 @@ void Airport::BuildBtg(const string& root, const string_list& elev_src )
 
     	TGPolygon poly = rwy_polys[i].get_poly();
 
-        if ( (dbg_rwy_poly >= 0) && (i == (unsigned int)dbg_rwy_poly) ) {
+        if ( (dbg_rwy_poly > 0) && (i == (unsigned int)dbg_rwy_poly-1) ) {
             SG_LOG(SG_GENERAL, SG_INFO, "Problem rwy poly (" << i << ") : " << poly );
 
             tgChopNormalPolygon( "/home/pete", "rwy", poly, false );
@@ -860,7 +862,7 @@ void Airport::BuildBtg(const string& root, const string_list& elev_src )
 
     	TGPolygon poly = pvmt_polys[i].get_poly();
 
-        if ( (dbg_pvmt_poly >= 0) && (i == (unsigned int)dbg_pvmt_poly) ) {
+        if ( (dbg_pvmt_poly > 0) && (i == (unsigned int)dbg_pvmt_poly-1) ) {
             SG_LOG(SG_GENERAL, SG_INFO, "Problem pvmt poly (" << i << ") : " << poly );
 
             tgChopNormalPolygon( "/home/pete", "pvmt", poly, false );
@@ -894,7 +896,7 @@ void Airport::BuildBtg(const string& root, const string_list& elev_src )
 
     	TGPolygon poly = line_polys[i].get_poly();
 
-        if ( (dbg_feat_poly >= 0) && (i == (unsigned int)dbg_feat_poly) ) {
+        if ( (dbg_feat_poly > 0) && (i == (unsigned int)dbg_feat_poly-1) ) {
             SG_LOG(SG_GENERAL, SG_INFO, "Problem feat poly (" << i << ") : " << poly );
 
             tgChopNormalPolygon( "/home/pete/", "feat", poly, false );
@@ -914,7 +916,7 @@ void Airport::BuildBtg(const string& root, const string_list& elev_src )
         line_polys[i].set_texcoords( tc );
     }
 
-    if ( dbg_base_poly >= 0 ) {
+    if ( dbg_base_poly > 0 ) {
         SG_LOG(SG_GENERAL, SG_INFO, "Problem base poly: " << base_poly );
 
         tgChopNormalPolygon( "/home/pete/", "Base", base_poly, false );
diff --git a/src/Airports/GenAirports850/closedpoly.cxx b/src/Airports/GenAirports850/closedpoly.cxx
index 956a18d8..086eeaaa 100644
--- a/src/Airports/GenAirports850/closedpoly.cxx
+++ b/src/Airports/GenAirports850/closedpoly.cxx
@@ -363,6 +363,7 @@ void ClosedPoly::Finish()
         }
 
         pre_tess = snap( pre_tess, gSnap );
+        pre_tess = remove_dups( pre_tess );
     }
 
     // save memory by deleting unneeded resources
@@ -387,13 +388,15 @@ void ClosedPoly::Finish()
 int ClosedPoly::BuildBtg( superpoly_list* rwy_polys, texparams_list* texparams, ClipPolyType* accum, poly_list& slivers, TGPolygon* apt_base, TGPolygon* apt_clearing, bool make_shapefiles )
 {
     TGPolygon base, safe_base;
+    TGPolygon pre_accum;
+
     string    material;
     void*     ds_id = NULL;        // If we are going to build shapefiles
     void*     l_id  = NULL;        // datasource and layer IDs
 
     if ( make_shapefiles ) {
         char ds_name[128];
-        sprintf(ds_name, "./cp_debug/problem");
+        sprintf(ds_name, "./cp_debug");
         ds_id = tgShapefileOpenDatasource( ds_name );
     }
 
@@ -431,8 +434,8 @@ int ClosedPoly::BuildBtg( superpoly_list* rwy_polys, texparams_list* texparams,
         // verify the poly has been generated
         if ( pre_tess.contours() )    
         {
-            SG_LOG(SG_GENERAL, SG_DEBUG, "BuildBtg: original poly has " << pre_tess.contours() << " contours");
-    
+            SG_LOG(SG_GENERAL, SG_DEBUG, "BuildBtg: original poly has " << pre_tess.contours() << " contours" << " and " << pre_tess.total_size() << " points" );
+
             // do this before clipping and generating the base
             // pre_tess = tgPolygonSimplify( pre_tess );
             // pre_tess = reduce_degeneracy( pre_tess );
@@ -440,6 +443,18 @@ int ClosedPoly::BuildBtg( superpoly_list* rwy_polys, texparams_list* texparams,
             TGSuperPoly sp;
             TGTexParams tp;
 
+            if ( make_shapefiles ) {
+                char layer_name[128];
+                char feature_name[128];
+
+                sprintf( layer_name, "original" );
+                l_id = tgShapefileOpenLayer( ds_id, layer_name );
+                sprintf( feature_name, "original" );
+                tgShapefileCreateFeature( ds_id, l_id, pre_tess, feature_name );
+
+                pre_accum = *accum;
+            }
+    
             TGPolygon clipped = tgPolygonDiffClipper( pre_tess, *accum );
             tgPolygonFindSlivers( clipped, slivers );
 
@@ -453,45 +468,70 @@ int ClosedPoly::BuildBtg( superpoly_list* rwy_polys, texparams_list* texparams,
             rwy_polys->push_back( sp );
 
             *accum = tgPolygonUnionClipper( pre_tess, *accum );
-//            *accum = tgPolygonUnionClipper( clipped, *accum );
 
             /* If debugging this poly, write the poly, and clipped poly and the accum buffer into their own layers */
-            if (ds_id) {
+            if ( make_shapefiles ) {
                 char layer_name[128];
                 char feature_name[128];
 
-                sprintf( layer_name, "original" );
-                l_id = tgShapefileOpenLayer( ds_id, layer_name );
-                sprintf( feature_name, "original" );
-                tgShapefileCreateFeature( ds_id, l_id, pre_tess, feature_name );
-
                 sprintf( layer_name, "clipped" );
                 l_id = tgShapefileOpenLayer( ds_id, layer_name );
                 sprintf( feature_name, "clipped" );
                 tgShapefileCreateFeature( ds_id, l_id, clipped, feature_name );
 
-                sprintf( layer_name, "accum" );
+                sprintf( layer_name, "pre_accum" );
                 l_id = tgShapefileOpenLayer( ds_id, layer_name );
-                sprintf( feature_name, "accum" );
-                tgShapefileCreateFeature( ds_id, l_id, *accum, feature_name );
+                sprintf( feature_name, "pre_accum" );
+                tgShapefileCreateFeature( ds_id, l_id, pre_accum, feature_name );
 
-                tgShapefileCloseDatasource( ds_id );        
+                sprintf( layer_name, "post_accum" );
+                l_id = tgShapefileOpenLayer( ds_id, layer_name );
+                sprintf( feature_name, "post_accum" );
+                tgShapefileCreateFeature( ds_id, l_id, *accum, feature_name );
             }
 
+            SG_LOG(SG_GENERAL, SG_DEBUG, "tp construct");
+
             tp = TGTexParams( pre_tess.get_pt(0,0), 5.0, 5.0, texture_heading );
             texparams->push_back( tp );
 
             if ( apt_base )
             {           
                 base = tgPolygonExpand( pre_tess, 20.0); 
-                safe_base = tgPolygonExpand( pre_tess, 50.0);        
+                if ( make_shapefiles ) {
+                    char layer_name[128];
+                    char feature_name[128];
 
+                    sprintf( layer_name, "exp_base" );
+                    l_id = tgShapefileOpenLayer( ds_id, layer_name );
+                    sprintf( feature_name, "exp_base" );
+                    tgShapefileCreateFeature( ds_id, l_id, base, feature_name );
+                }
+
+                safe_base = tgPolygonExpand( pre_tess, 50.0);        
+                if ( make_shapefiles ) {
+                    char layer_name[128];
+                    char feature_name[128];
+
+                    SG_LOG(SG_GENERAL, SG_INFO, "expanded safe poly: " << safe_base);
+
+                    sprintf( layer_name, "exp_safe_base" );
+                    l_id = tgShapefileOpenLayer( ds_id, layer_name );
+                    sprintf( feature_name, "exp_safe_base" );
+                    tgShapefileCreateFeature( ds_id, l_id, safe_base, feature_name );
+                }                
+    
                 // add this to the airport clearing
                 *apt_clearing = tgPolygonUnionClipper( safe_base, *apt_clearing);
 
                 // and add the clearing to the base
                 *apt_base = tgPolygonUnionClipper( base, *apt_base );
             }
+
+            if ( make_shapefiles )
+            {
+                tgShapefileCloseDatasource( ds_id );
+            }
         }
     }