]> git.mxchange.org Git - flightgear.git/blob - DEM/dem.cxx
27497f2d862065ff9d15abfeaed5f8cc9aabb43a
[flightgear.git] / DEM / dem.cxx
1 // -*- Mode: C++ -*-
2 //
3 // dem.c -- DEM management class
4 //
5 // Written by Curtis Olson, started March 1998.
6 //
7 // Copyright (C) 1998  Curtis L. Olson  - curt@me.umn.edu
8 //
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 // General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 //
23 // $Id$
24 // (Log is kept at end of this file)
25
26
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #include <ctype.h>    // isspace()
32 #include <stdlib.h>   // atoi()
33 #include <math.h>     // rint()
34 #include <stdio.h>
35 #include <string.h>
36 #include <sys/stat.h> // stat()
37 #include <errno.h>
38 #ifdef HAVE_UNISTD_H
39 # include <unistd.h>   // stat()
40 #endif
41 #include <string>
42
43 // #include <zlib/zlib.h>
44 #include <Misc/fgstream.hxx>
45 #include <Misc/strutils.hxx>
46 #include <Include/compiler.h>
47 FG_USING_NAMESPACE(std);
48
49 #include "dem.hxx"
50 #include "leastsqs.hxx"
51
52 #include <Include/fg_constants.h>
53
54
55 #define MAX_EX_NODES 10000
56
57 #ifdef WIN32
58 # ifdef __BORLANDC__
59 #  include <dir.h>
60 #  define MKDIR(a) mkdir(a)
61 # else
62 #  define MKDIR(a) mkdir(a,S_IRWXU)  // I am just guessing at this flag (NHV)
63 # endif // __BORLANDC__
64 #endif // WIN32
65
66
67 fgDEM::fgDEM( void ) {
68     // printf("class fgDEM CONstructor called.\n");
69     dem_data = new float[DEM_SIZE_1][DEM_SIZE_1];
70     output_data = new float[DEM_SIZE_1][DEM_SIZE_1];
71 }
72
73
74 #ifdef WIN32
75
76 // return the file path name ( foo/bar/file.ext = foo/bar )
77 static void extract_path ( const char *in, char *base) {
78     int len, i;
79     
80     len = strlen (in);
81     strcpy (base, in);
82
83     i = len - 1;
84     while ( (i >= 0) && (in[i] != '/') ) {
85         i--;
86     }
87
88     base[i] = '\0';
89 }
90
91
92 // Make a subdirectory
93 static int my_mkdir (const char *dir) {
94     struct stat stat_buf;
95     int result;
96
97     printf ("mk_dir() ");
98
99     result = stat (dir, &stat_buf);
100
101     if (result != 0) {
102         MKDIR (dir);
103         result = stat (dir, &stat_buf);
104         if (result != 0) {
105             printf ("problem creating %s\n", dir);
106         } else {
107             printf ("%s created\n", dir);
108         }
109     } else {
110         printf ("%s already exists\n", dir);
111     }
112
113     return (result);
114 }
115
116 #endif // WIN32
117
118
119 // open a DEM file
120 int fgDEM::open ( const string& file ) {
121     // open input file (or read from stdin)
122     if ( file ==  "-" ) {
123         printf("Loading DEM data file: stdin\n");
124         // fd = stdin;
125         // fd = gzdopen(STDIN_FILENO, "r");
126         printf("Not yet ported ...\n");
127         return 0;
128     } else {
129         in = new fg_gzifstream( file );
130         if ( !(*in) ) {
131             cout << "Cannot open " << file << endl;
132             return 0;
133         }
134         cout << "Loading DEM data file: " << file << endl;
135     }
136
137     return 1;
138 }
139
140
141 // close a DEM file
142 int fgDEM::close () {
143     // the fg_gzifstream doesn't seem to have a close()
144
145     delete in;
146
147     return 1;
148 }
149
150
151 // return next token from input stream
152 string fgDEM::next_token() {
153     string token;
154
155     in->stream() >> token;
156
157     // cout << "    returning " + token + "\n";
158
159     return token;
160 }
161
162
163 // return next integer from input stream
164 int fgDEM::next_int() {
165     int result;
166
167     in->stream() >> result;
168
169     return result;
170 }
171
172
173 // return next double from input stream
174 double fgDEM::next_double() {
175     double result;
176
177     in->stream() >> result;
178
179     return result;
180 }
181
182
183 // return next exponential num from input stream
184 double fgDEM::next_exp() {
185     string token;
186     double mantissa;
187     int exp, acc;
188     int i;
189
190     token = next_token();
191
192 #if 1
193     const char* p = token.c_str();
194     char buf[64];
195     char* bp = buf;
196     
197     for ( ; *p != 0; ++p )
198     {
199         if ( *p == 'D' )
200             *bp++ = 'E';
201         else
202             *bp++ = *p;
203     }
204     *bp = 0;
205     return ::atof( buf );
206 #else
207     sscanf(token.c_str(), "%lfD%d", &mantissa, &exp);
208
209     // cout << "    Mantissa = " << mantissa << "  Exp = " << exp << "\n";
210
211     acc = 1;
212     if ( exp > 0 ) {
213         for ( i = 1; i <= exp; i++ ) {
214             acc *= 10;
215         }
216     } else if ( exp < 0 ) {
217         for ( i = -1; i >= exp; i-- ) {
218             acc /= 10;
219         }
220     }
221
222     return( (int)rint(mantissa * (double)acc) );
223 #endif
224 }
225
226
227 // read and parse DEM "A" record
228 int fgDEM::read_a_record() {
229     int i, inum;
230     double dnum;
231     string name, token;
232     char c;
233     char *ptr;
234
235     // get the name field (144 characters)
236     for ( i = 0; i < 144; i++ ) {
237         in->get(c);
238         name += c;
239     }
240   
241     // clean off the trailing whitespace
242     name = trim(name, " ");
243     cout << "    Quad name field: " << name << endl;
244
245     // DEM level code, 3 reflects processing by DMA
246     inum = next_int();
247     cout << "    DEM level code = " << inum << "\n";
248
249     if ( inum > 3 ) {
250         return 0;
251     }
252
253     // Pattern code, 1 indicates a regular elevation pattern
254     inum = next_int();
255     cout << "    Pattern code = " << inum << "\n";
256
257     // Planimetric reference system code, 0 indicates geographic
258     // coordinate system.
259     inum = next_int();
260     cout << "    Planimetric reference code = " << inum << "\n";
261
262     // Zone code
263     inum = next_int();
264     cout << "    Zone code = " << inum << "\n";
265
266     // Map projection parameters (ignored)
267     for ( i = 0; i < 15; i++ ) {
268         dnum = next_double();
269         // printf("%d: %f\n",i,dnum);
270     }
271
272     // Units code, 3 represents arc-seconds as the unit of measure for
273     // ground planimetric coordinates throughout the file.
274     inum = next_int();
275     if ( inum != 3 ) {
276         cout << "    Unknown (X,Y) units code = " << inum << "!\n";
277         exit(-1);
278     }
279
280     // Units code; 2 represents meters as the unit of measure for
281     // elevation coordinates throughout the file.
282     inum = next_int();
283     if ( inum != 2 ) {
284         cout << "    Unknown (Z) units code = " << inum << "!\n";
285         exit(-1);
286     }
287
288     // Number (n) of sides in the polygon which defines the coverage of
289     // the DEM file (usually equal to 4).
290     inum = next_int();
291     if ( inum != 4 ) {
292         cout << "    Unknown polygon dimension = " << inum << "!\n";
293         exit(-1);
294     }
295
296     // Ground coordinates of bounding box in arc-seconds
297     dem_x1 = originx = next_exp();
298     dem_y1 = originy = next_exp();
299     cout << "    Origin = (" << originx << "," << originy << ")\n";
300
301     dem_x2 = next_exp();
302     dem_y2 = next_exp();
303
304     dem_x3 = next_exp();
305     dem_y3 = next_exp();
306
307     dem_x4 = next_exp();
308     dem_y4 = next_exp();
309
310     // Minimum/maximum elevations in meters
311     dem_z1 = next_exp();
312     dem_z2 = next_exp();
313     cout << "    Elevation range " << dem_z1 << " to " << dem_z2 << "\n";
314
315     // Counterclockwise angle from the primary axis of ground
316     // planimetric referenced to the primary axis of the DEM local
317     // reference system.
318     token = next_token();
319
320     // Accuracy code; 0 indicates that a record of accuracy does not
321     // exist and that no record type C will follow.
322
323     // DEM spacial resolution.  Usually (3,3,1) (3,6,1) or (3,9,1)
324     // depending on latitude
325
326     // I will eventually have to do something with this for data at
327     // higher latitudes */
328     token = next_token();
329     cout << "    accuracy & spacial resolution string = " << token << endl;
330     i = token.length();
331     cout << "    length = " << i << "\n";
332
333 #if 1
334     inum = atoi( token.substr( 0, 1 ) );
335     row_step = atof( token.substr( 1, 12 ) );
336     col_step = atof( token.substr( 13, 12 ) );
337     //token.substr( 25, 12 )
338 #else
339     ptr = token.c_str() + i - 12;
340     cout << "    last field = " << ptr << " = " << atof(ptr) << "\n";
341     ptr[0] = '\0';
342
343     ptr = ptr - 12;
344     col_step = atof(ptr);
345     cout << "    last field = " << ptr << " = " << col_step << "\n";
346     ptr[0] = '\0';
347
348     ptr = ptr - 12;
349     row_step = atof(ptr);
350     cout << "    last field = " << ptr << " = " << row_step << "\n";
351     ptr[0] = '\0';
352
353     // accuracy code = atod(token)
354     ptr = ptr - 12;
355     inum = atoi(ptr);
356 #endif
357     cout << "    Accuracy code = " << inum << "\n";
358
359     cout << "    column step = " << col_step << 
360         "  row step = " << row_step << "\n";
361
362     // dimension of arrays to follow (1)
363     token = next_token();
364
365     // number of profiles
366     dem_num_profiles = cols = next_int();
367     cout << "    Expecting " << dem_num_profiles << " profiles\n";
368
369     return 1;
370 }
371
372
373 // read and parse DEM "B" record
374 void fgDEM::read_b_record( ) {
375     string token;
376     int i;
377
378     // row / column id of this profile
379     prof_row = next_int();
380     prof_col = next_int();
381     // printf("col id = %d  row id = %d\n", prof_col, prof_row);
382
383     // Number of columns and rows (elevations) in this profile
384     prof_num_rows = rows = next_int();
385     prof_num_cols = next_int();
386     // printf("    profile num rows = %d\n", prof_num_rows);
387
388     // Ground planimetric coordinates (arc-seconds) of the first
389     // elevation in the profile
390     prof_x1 = next_exp();
391     prof_y1 = next_exp();
392     // printf("    Starting at %.2f %.2f\n", prof_x1, prof_y1);
393
394     // Elevation of local datum for the profile.  Always zero for
395     // 1-degree DEM, the reference is mean sea level.
396     token = next_token();
397
398     // Minimum and maximum elevations for the profile.
399     token = next_token();
400     token = next_token();
401
402     // One (usually) dimensional array (prof_num_cols,1) of elevations
403     for ( i = 0; i < prof_num_rows; i++ ) {
404         prof_data = next_int();
405         dem_data[cur_col][i] = (float)prof_data;
406     }
407 }
408
409
410 // parse dem file
411 int fgDEM::parse( ) {
412     int i;
413
414     cur_col = 0;
415
416     if ( !read_a_record() ) {
417         return(0);
418     }
419
420     for ( i = 0; i < dem_num_profiles; i++ ) {
421         // printf("Ready to read next b record\n");
422         read_b_record();
423         cur_col++;
424
425         if ( cur_col % 100 == 0 ) {
426             cout << "    loaded " << cur_col << " profiles of data\n";
427         }
428     }
429
430     cout << "    Done parsing\n";
431
432     return 1;
433 }
434
435
436 // return the current altitude based on mesh data.  We should rewrite
437 // this to interpolate exact values, but for now this is good enough
438 double fgDEM::interpolate_altitude( double lon, double lat ) {
439     // we expect incoming (lon,lat) to be in arcsec for now
440
441     double xlocal, ylocal, dx, dy, zA, zB, elev;
442     int x1, x2, x3, y1, y2, y3;
443     float z1, z2, z3;
444     int xindex, yindex;
445
446     /* determine if we are in the lower triangle or the upper triangle 
447        ______
448        |   /|
449        |  / |
450        | /  |
451        |/   |
452        ------
453
454        then calculate our end points
455      */
456
457     xlocal = (lon - originx) / col_step;
458     ylocal = (lat - originy) / row_step;
459
460     xindex = (int)(xlocal);
461     yindex = (int)(ylocal);
462
463     // printf("xindex = %d  yindex = %d\n", xindex, yindex);
464
465     if ( xindex + 1 == cols ) {
466         xindex--;
467     }
468
469     if ( yindex + 1 == rows ) {
470         yindex--;
471     }
472
473     if ( (xindex < 0) || (xindex + 1 >= cols) ||
474          (yindex < 0) || (yindex + 1 >= rows) ) {
475         return(-9999);
476     }
477
478     dx = xlocal - xindex;
479     dy = ylocal - yindex;
480
481     if ( dx > dy ) {
482         // lower triangle
483         // printf("  Lower triangle\n");
484
485         x1 = xindex; 
486         y1 = yindex; 
487         z1 = dem_data[x1][y1];
488
489         x2 = xindex + 1; 
490         y2 = yindex; 
491         z2 = dem_data[x2][y2];
492                                   
493         x3 = xindex + 1; 
494         y3 = yindex + 1; 
495         z3 = dem_data[x3][y3];
496
497         // printf("  dx = %.2f  dy = %.2f\n", dx, dy);
498         // printf("  (x1,y1,z1) = (%d,%d,%d)\n", x1, y1, z1);
499         // printf("  (x2,y2,z2) = (%d,%d,%d)\n", x2, y2, z2);
500         // printf("  (x3,y3,z3) = (%d,%d,%d)\n", x3, y3, z3);
501
502         zA = dx * (z2 - z1) + z1;
503         zB = dx * (z3 - z1) + z1;
504         
505         // printf("  zA = %.2f  zB = %.2f\n", zA, zB);
506
507         if ( dx > FG_EPSILON ) {
508             elev = dy * (zB - zA) / dx + zA;
509         } else {
510             elev = zA;
511         }
512     } else {
513         // upper triangle
514         // printf("  Upper triangle\n");
515
516         x1 = xindex; 
517         y1 = yindex; 
518         z1 = dem_data[x1][y1];
519
520         x2 = xindex; 
521         y2 = yindex + 1; 
522         z2 = dem_data[x2][y2];
523                                   
524         x3 = xindex + 1; 
525         y3 = yindex + 1; 
526         z3 = dem_data[x3][y3];
527
528         // printf("  dx = %.2f  dy = %.2f\n", dx, dy);
529         // printf("  (x1,y1,z1) = (%d,%d,%d)\n", x1, y1, z1);
530         // printf("  (x2,y2,z2) = (%d,%d,%d)\n", x2, y2, z2);
531         // printf("  (x3,y3,z3) = (%d,%d,%d)\n", x3, y3, z3);
532  
533         zA = dy * (z2 - z1) + z1;
534         zB = dy * (z3 - z1) + z1;
535         
536         // printf("  zA = %.2f  zB = %.2f\n", zA, zB );
537         // printf("  xB - xA = %.2f\n", col_step * dy / row_step);
538
539         if ( dy > FG_EPSILON ) {
540             elev = dx * (zB - zA) / dy    + zA;
541         } else {
542             elev = zA;
543         }
544     }
545
546     return(elev);
547 }
548
549
550 // Use least squares to fit a simpler data set to dem data
551 void fgDEM::fit( double error, fgBUCKET *p ) {
552     double x[DEM_SIZE_1], y[DEM_SIZE_1];
553     double m, b, ave_error, max_error;
554     double cury, lasty;
555     int n, row, start, end;
556     int colmin, colmax, rowmin, rowmax;
557     bool good_fit;
558     // FILE *dem, *fit, *fit1;
559
560     printf("Initializing output mesh structure\n");
561     outputmesh_init();
562
563     // determine dimensions
564     colmin = p->x * ( (cols - 1) / 8);
565     colmax = colmin + ( (cols - 1) / 8);
566     rowmin = p->y * ( (rows - 1) / 8);
567     rowmax = rowmin + ( (rows - 1) / 8);
568     printf("Fitting region = %d,%d to %d,%d\n", colmin, rowmin, colmax, rowmax);
569     
570     // include the corners explicitly
571     outputmesh_set_pt(colmin, rowmin, dem_data[colmin][rowmin]);
572     outputmesh_set_pt(colmin, rowmax, dem_data[colmin][rowmax]);
573     outputmesh_set_pt(colmax, rowmax, dem_data[colmax][rowmax]);
574     outputmesh_set_pt(colmax, rowmin, dem_data[colmax][rowmin]);
575
576     printf("Beginning best fit procedure\n");
577
578     for ( row = rowmin; row <= rowmax; row++ ) {
579         // fit  = fopen("fit.dat",  "w");
580         // fit1 = fopen("fit1.dat", "w");
581
582         start = colmin;
583
584         // printf("    fitting row = %d\n", row);
585
586         while ( start < colmax ) {
587             end = start + 1;
588             good_fit = true;
589
590             x[(end - start) - 1] = 0.0 + ( start * col_step );
591             y[(end - start) - 1] = dem_data[start][row];
592
593             while ( (end <= colmax) && good_fit ) {
594                 n = (end - start) + 1;
595                 // printf("Least square of first %d points\n", n);
596                 x[end - start] = 0.0 + ( end * col_step );
597                 y[end - start] = dem_data[end][row];
598                 least_squares(x, y, n, &m, &b);
599                 ave_error = least_squares_error(x, y, n, m, b);
600                 max_error = least_squares_max_error(x, y, n, m, b);
601
602                 /*
603                 printf("%d - %d  ave error = %.2f  max error = %.2f  y = %.2f*x + %.2f\n", 
604                 start, end, ave_error, max_error, m, b);
605                 
606                 f = fopen("gnuplot.dat", "w");
607                 for ( j = 0; j <= end; j++) {
608                     fprintf(f, "%.2f %.2f\n", 0.0 + ( j * col_step ), 
609                             dem_data[row][j]);
610                 }
611                 for ( j = start; j <= end; j++) {
612                     fprintf(f, "%.2f %.2f\n", 0.0 + ( j * col_step ), 
613                             dem_data[row][j]);
614                 }
615                 fclose(f);
616
617                 printf("Please hit return: "); gets(junk);
618                 */
619
620                 if ( max_error > error ) {
621                     good_fit = false;
622                 }
623                 
624                 end++;
625             }
626
627             if ( !good_fit ) {
628                 // error exceeded the threshold, back up
629                 end -= 2;  // back "end" up to the last good enough fit
630                 n--;       // back "n" up appropriately too
631             } else {
632                 // we popped out of the above loop while still within
633                 // the error threshold, so we must be at the end of
634                 // the data set
635                 end--;
636             }
637             
638             least_squares(x, y, n, &m, &b);
639             ave_error = least_squares_error(x, y, n, m, b);
640             max_error = least_squares_max_error(x, y, n, m, b);
641
642             /*
643             printf("\n");
644             printf("%d - %d  ave error = %.2f  max error = %.2f  y = %.2f*x + %.2f\n", 
645                    start, end, ave_error, max_error, m, b);
646             printf("\n");
647
648             fprintf(fit1, "%.2f %.2f\n", x[0], m * x[0] + b);
649             fprintf(fit1, "%.2f %.2f\n", x[end-start], m * x[end-start] + b);
650             */
651
652             if ( start > colmin ) {
653                 // skip this for the first line segment
654                 cury = m * x[0] + b;
655                 outputmesh_set_pt(start, row, (lasty + cury) / 2);
656                 // fprintf(fit, "%.2f %.2f\n", x[0], (lasty + cury) / 2);
657             }
658
659             lasty = m * x[end-start] + b;
660             start = end;
661         }
662
663         /*
664         fclose(fit);
665         fclose(fit1);
666
667         dem = fopen("gnuplot.dat", "w");
668         for ( j = 0; j < DEM_SIZE_1; j++) {
669             fprintf(dem, "%.2f %.2f\n", 0.0 + ( j * col_step ), 
670                     dem_data[j][row]);
671         } 
672         fclose(dem);
673         */
674
675         // NOTICE, this is for testing only.  This instance of
676         // output_nodes should be removed.  It should be called only
677         // once at the end once all the nodes have been generated.
678         // newmesh_output_nodes(&nm, "mesh.node");
679         // printf("Please hit return: "); gets(junk);
680     }
681
682     // outputmesh_output_nodes(fg_root, p);
683 }
684
685
686 // Initialize output mesh structure
687 void fgDEM::outputmesh_init( void ) {
688     int i, j;
689     
690     for ( j = 0; j < DEM_SIZE_1; j++ ) {
691         for ( i = 0; i < DEM_SIZE_1; i++ ) {
692             output_data[i][j] = -9999.0;
693         }
694     }
695 }
696
697
698 // Get the value of a mesh node
699 double fgDEM::outputmesh_get_pt( int i, int j ) {
700     return ( output_data[i][j] );
701 }
702
703
704 // Set the value of a mesh node
705 void fgDEM::outputmesh_set_pt( int i, int j, double value ) {
706     // printf("Setting data[%d][%d] = %.2f\n", i, j, value);
707    output_data[i][j] = value;
708 }
709
710
711 // Write out a node file that can be used by the "triangle" program.
712 // Check for an optional "index.node.ex" file in case there is a .poly
713 // file to go along with this node file.  Include these nodes first
714 // since they are referenced by position from the .poly file.
715 void fgDEM::outputmesh_output_nodes( const string& fg_root, fgBUCKET *p ) {
716     double exnodes[MAX_EX_NODES][3];
717     struct stat stat_buf;
718     string dir;
719     char base_path[256], file[256], exfile[256];
720 #ifdef WIN32
721     char tmp_path[256];
722 #endif
723     string command;
724     FILE *fd;
725     long int index;
726     int colmin, colmax, rowmin, rowmax;
727     int i, j, count, excount, result;
728
729     // determine dimensions
730     colmin = p->x * ( (cols - 1) / 8);
731     colmax = colmin + ( (cols - 1) / 8);
732     rowmin = p->y * ( (rows - 1) / 8);
733     rowmax = rowmin + ( (rows - 1) / 8);
734     cout << "  dumping region = " << colmin << "," << rowmin << " to " <<
735         colmax << "," << rowmax << "\n";
736
737     // generate the base directory
738     fgBucketGenBasePath(p, base_path);
739     cout << "fg_root = " << fg_root << "  Base Path = " << base_path << endl;
740     dir = fg_root + "/Scenery/" + base_path;
741     cout << "Dir = " << dir << endl;
742     
743     // stat() directory and create if needed
744     errno = 0;
745     result = stat(dir.c_str(), &stat_buf);
746     if ( result != 0 && errno == ENOENT ) {
747         cout << "Creating directory\n";
748
749 #ifndef WIN32
750
751         command = "mkdir -p " + dir + "\n";
752         system( command.c_str() );
753
754 #else // WIN32
755
756         // Cygwin crashes when trying to output to node file
757         // explicitly making directory structure seems OK on Win95
758
759         extract_path (base_path, tmp_path);
760
761         dir = fg_root + "/Scenery";
762         if (my_mkdir ( dir.c_str() )) { exit (-1); }
763
764         dir = fg_root + "/Scenery/" + tmp_path;
765         if (my_mkdir ( dir.c_str() )) { exit (-1); }
766
767         dir = fg_root + "/Scenery/" + base_path;
768         if (my_mkdir ( dir.c_str() )) { exit (-1); }
769
770 #endif // WIN32
771
772     } else {
773         // assume directory exists
774     }
775
776     // get index and generate output file name
777     index = fgBucketGenIndex(p);
778     sprintf(file, "%s/%ld.node", dir.c_str(), index);
779
780     // get (optional) extra node file name (in case there is matching
781     // .poly file.
782     strcpy(exfile, file);
783     strcat(exfile, ".ex");
784
785     // load extra nodes if they exist
786     excount = 0;
787     if ( (fd = fopen(exfile, "r")) != NULL ) {
788         int junki;
789         fscanf(fd, "%d %d %d %d", &excount, &junki, &junki, &junki);
790
791         if ( excount > MAX_EX_NODES - 1 ) {
792             printf("Error, too many 'extra' nodes, increase array size\n");
793             exit(-1);
794         } else {
795             printf("    Expecting %d 'extra' nodes\n", excount);
796         }
797
798         for ( i = 1; i <= excount; i++ ) {
799             fscanf(fd, "%d %lf %lf %lf\n", &junki, 
800                    &exnodes[i][0], &exnodes[i][1], &exnodes[i][2]);
801             printf("(extra) %d %.2f %.2f %.2f\n", 
802                     i, exnodes[i][0], exnodes[i][1], exnodes[i][2]);
803         }
804         fclose(fd);
805     }
806
807     printf("Creating node file:  %s\n", file);
808     fd = fopen(file, "w");
809
810     // first count regular nodes to generate header
811     count = 0;
812     for ( j = rowmin; j <= rowmax; j++ ) {
813         for ( i = colmin; i <= colmax; i++ ) {
814             if ( output_data[i][j] > -9000.0 ) {
815                 count++;
816             }
817         }
818         // printf("    count = %d\n", count);
819     }
820     fprintf(fd, "%d 2 1 0\n", count + excount);
821
822     // now write out extra node data
823     for ( i = 1; i <= excount; i++ ) {
824         fprintf(fd, "%d %.2f %.2f %.2f\n", 
825                 i, exnodes[i][0], exnodes[i][1], exnodes[i][2]);
826     }
827
828     // write out actual node data
829     count = excount + 1;
830     for ( j = rowmin; j <= rowmax; j++ ) {
831         for ( i = colmin; i <= colmax; i++ ) {
832             if ( output_data[i][j] > -9000.0 ) {
833                 fprintf(fd, "%d %.2f %.2f %.2f\n", 
834                         count++, 
835                         originx + (double)i * col_step, 
836                         originy + (double)j * row_step,
837                         output_data[i][j]);
838             }
839         }
840         // printf("    count = %d\n", count);
841     }
842
843     fclose(fd);
844 }
845
846
847 fgDEM::~fgDEM( void ) {
848     // printf("class fgDEM DEstructor called.\n");
849     delete [] dem_data;
850     delete [] output_data;
851 }
852
853
854 // $Log$
855 // Revision 1.17  1998/10/16 19:08:12  curt
856 // Portability updates from Bernie Bright.
857 //
858 // Revision 1.16  1998/10/02 21:41:39  curt
859 // Fixes for win32.
860 //
861 // Revision 1.15  1998/09/21 20:53:59  curt
862 // minor tweaks to clean a few additional things up after the rewrite.
863 //
864 // Revision 1.14  1998/09/19 17:59:45  curt
865 // Use c++ streams (fg_gzifstream).  Also converted many character arrays to
866 // the string class.
867 //
868 // Revision 1.13  1998/09/09 16:24:04  curt
869 // Fixed a bug in the handling of exclude files which was causing
870 // a crash by calling fclose() on an invalid file handle.
871 //
872 // Revision 1.12  1998/08/24 20:03:31  curt
873 // Eliminated a possible memory overrun error.
874 // Use the proper free() rather than the incorrect delete().
875 //
876 // Revision 1.11  1998/07/20 12:46:11  curt
877 // When outputing to a .node file, first check for an optional
878 // "index.node.ex" file in case there is a .poly file to go along with this
879 // node file.  Include these nodes first since they are referenced by position
880 // from the .poly file.  This is my first pass at adding an area "cutout"
881 // feature to the terrain generation pipeline.
882 //
883 // Revision 1.10  1998/07/13 20:58:02  curt
884 // .
885 //
886 // Revision 1.9  1998/07/13 15:29:49  curt
887 // Added #ifdef HAVE_CONFIG_H
888 //
889 // Revision 1.8  1998/07/04 00:47:18  curt
890 // typedef'd struct fgBUCKET.
891 //
892 // Revision 1.7  1998/06/05 18:14:39  curt
893 // Abort out early when reading the "A" record if it doesn't look like
894 // a proper DEM file.
895 //
896 // Revision 1.6  1998/05/02 01:49:21  curt
897 // Fixed a bug where the wrong variable was being initialized.
898 //
899 // Revision 1.5  1998/04/25 15:00:32  curt
900 // Changed "r" to "rb" in gzopen() options.  This fixes bad behavior in win32.
901 //
902 // Revision 1.4  1998/04/22 13:14:46  curt
903 // Fixed a bug in zlib usage.
904 //
905 // Revision 1.3  1998/04/18 03:53:05  curt
906 // Added zlib support.
907 //
908 // Revision 1.2  1998/04/14 02:43:27  curt
909 // Used "new" to auto-allocate large DEM parsing arrays in class constructor.
910 //
911 // Revision 1.1  1998/04/08 22:57:22  curt
912 // Adopted Gnu automake/autoconf system.
913 //
914 // Revision 1.3  1998/04/06 21:09:41  curt
915 // Additional win32 support.
916 // Fixed a bad bug in dem file parsing that was causing the output to be
917 // flipped about x = y.
918 //
919 // Revision 1.2  1998/03/23 20:35:41  curt
920 // Updated to use FG_EPSILON
921 //
922 // Revision 1.1  1998/03/19 02:54:47  curt
923 // Reorganized into a class lib called fgDEM.
924 //
925 // Revision 1.1  1998/03/19 01:46:28  curt
926 // Initial revision.
927 //