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