]> git.mxchange.org Git - flightgear.git/blobdiff - DEM/dem.cxx
Eliminated a possible memory overrun error.
[flightgear.git] / DEM / dem.cxx
index 52612143a3166fe9bda1422159792c1dd750a901..13bd4ea0bed1d78672ba0dea3402e11dcc29fc3d 100644 (file)
@@ -29,9 +29,9 @@
 #endif
 
 #include <ctype.h>    // isspace()
+#include <stdlib.h>   // atoi()
 #include <math.h>     // rint()
 #include <stdio.h>
-#include <stdlib.h>   // atoi()
 #include <string.h>
 #include <sys/stat.h> // stat()
 #include <unistd.h>   // stat()
@@ -44,6 +44,8 @@
 #include <Include/fg_constants.h>
 
 
+#define MAX_EX_NODES 10000
+
 #ifdef WIN32
 #  define MKDIR(a) mkdir(a,S_IRWXU)     // I am just guessing at this flag (NHV)
 #endif // WIN32
@@ -205,7 +207,7 @@ static int next_exp(gzFile fd) {
 int fgDEM::read_a_record( void ) {
     int i, inum;
     double dnum;
-    char name[144];
+    char name[256];
     char token[80];
     char *ptr;
 
@@ -681,10 +683,15 @@ void fgDEM::outputmesh_set_pt( int i, int j, double value ) {
 }
 
 
-// Write out a node file that can be used by the "triangle" program
+// Write out a node file that can be used by the "triangle" program.
+// Check for an optional "index.node.ex" file in case there is a .poly
+// file to go along with this node file.  Include these nodes first
+// since they are referenced by position from the .poly file.
 void fgDEM::outputmesh_output_nodes( char *fg_root, fgBUCKET *p ) {
+    double exnodes[MAX_EX_NODES][3];
+    double junk1, junk2, junk3;
     struct stat stat_buf;
-    char base_path[256], dir[256], file[256];
+    char base_path[256], dir[256], file[256], exfile[256];
 #ifdef WIN32
     char tmp_path[256];
 #endif
@@ -692,7 +699,7 @@ void fgDEM::outputmesh_output_nodes( char *fg_root, fgBUCKET *p ) {
     FILE *fd;
     long int index;
     int colmin, colmax, rowmin, rowmax;
-    int i, j, count, result;
+    int i, j, count, excount, result;
 
     // determine dimensions
     colmin = p->x * ( (cols - 1) / 8);
@@ -744,10 +751,36 @@ void fgDEM::outputmesh_output_nodes( char *fg_root, fgBUCKET *p ) {
     index = fgBucketGenIndex(p);
     sprintf(file, "%s/%ld.node", dir, index);
 
+    // get (optional) extra node file name (in case there is matching
+    // .poly file.
+    strcpy(exfile, file);
+    strcat(exfile, ".ex");
+
+    // load extra nodes if they exist
+    excount = 0;
+    if ( (fd = fopen(exfile, "r")) != NULL ) {
+       fscanf(fd, "%d %d %d %d", &excount, &junk1, &junk2, &junk3);
+
+       if ( excount > MAX_EX_NODES - 1 ) {
+           printf("Error, too many 'extra' nodes, increase array size\n");
+           exit(-1);
+       } else {
+           printf("    Expecting %d 'extra' nodes\n", excount);
+       }
+
+       for ( i = 1; i <= excount; i++ ) {
+           fscanf(fd, "%d %lf %lf %lf\n", &junk1, 
+                  &exnodes[i][0], &exnodes[i][1], &exnodes[i][2]);
+           printf("(extra) %d %.2f %.2f %.2f\n", 
+                   i, exnodes[i][0], exnodes[i][1], exnodes[i][2]);
+       }
+    }
+    fclose(fd);
+
     printf("Creating node file:  %s\n", file);
     fd = fopen(file, "w");
 
-    // first count nodes to generate header
+    // first count regular nodes to generate header
     count = 0;
     for ( j = rowmin; j <= rowmax; j++ ) {
        for ( i = colmin; i <= colmax; i++ ) {
@@ -757,10 +790,16 @@ void fgDEM::outputmesh_output_nodes( char *fg_root, fgBUCKET *p ) {
        }
        // printf("    count = %d\n", count);
     }
-    fprintf(fd, "%d 2 1 0\n", count);
+    fprintf(fd, "%d 2 1 0\n", count + excount);
+
+    // now write out extra node data
+    for ( i = 1; i <= excount; i++ ) {
+       fprintf(fd, "%d %.2f %.2f %.2f\n", 
+               i, exnodes[i][0], exnodes[i][1], exnodes[i][2]);
+    }
 
-    // now write out actual node data
-    count = 1;
+    // write out actual node data
+    count = excount + 1;
     for ( j = rowmin; j <= rowmax; j++ ) {
        for ( i = colmin; i <= colmax; i++ ) {
            if ( output_data[i][j] > -9000.0 ) {
@@ -780,12 +819,26 @@ void fgDEM::outputmesh_output_nodes( char *fg_root, fgBUCKET *p ) {
 
 fgDEM::~fgDEM( void ) {
     // printf("class fgDEM DEstructor called.\n");
-    free(dem_data);
-    free(output_data);
+    delete(dem_data);
+    delete(output_data);
 }
 
 
 // $Log$
+// Revision 1.12  1998/08/24 20:03:31  curt
+// Eliminated a possible memory overrun error.
+// Use the proper free() rather than the incorrect delete().
+//
+// Revision 1.11  1998/07/20 12:46:11  curt
+// When outputing to a .node file, first check for an optional
+// "index.node.ex" file in case there is a .poly file to go along with this
+// node file.  Include these nodes first since they are referenced by position
+// from the .poly file.  This is my first pass at adding an area "cutout"
+// feature to the terrain generation pipeline.
+//
+// Revision 1.10  1998/07/13 20:58:02  curt
+// .
+//
 // Revision 1.9  1998/07/13 15:29:49  curt
 // Added #ifdef HAVE_CONFIG_H
 //