3 // dem.c -- DEM management class
5 // Written by Curtis Olson, started March 1998.
7 // Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
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.
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.
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.
24 // (Log is kept at end of this file)
31 #include <Include/compiler.h>
33 #include <ctype.h> // isspace()
34 #include <stdlib.h> // atoi()
35 #include <math.h> // rint()
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h> // stat()
41 #ifdef FG_HAVE_STD_INCLUDES
47 # include <unistd.h> // stat()
53 // #include <zlib/zlib.h>
54 #include <Misc/fgstream.hxx>
55 #include <Misc/strutils.hxx>
56 #include <Include/compiler.h>
57 FG_USING_NAMESPACE(std);
60 #include "leastsqs.hxx"
62 #include <Include/fg_constants.h>
65 #define MAX_EX_NODES 10000
71 # define MKDIR(a) mkdir(a)
73 # define MKDIR(a) mkdir(a,S_IRWXU) // I am just guessing at this flag (NHV)
74 # endif // __BORLANDC__
79 fgDEM::fgDEM( void ) {
80 // printf("class fgDEM CONstructor called.\n");
81 dem_data = new float[DEM_SIZE_1][DEM_SIZE_1];
82 output_data = new float[DEM_SIZE_1][DEM_SIZE_1];
89 // return the file path name ( foo/bar/file.ext = foo/bar )
90 static void extract_path ( const char *in, char *base) {
97 while ( (i >= 0) && (in[i] != '/') ) {
105 // Make a subdirectory
106 static int my_mkdir (const char *dir) {
107 struct stat stat_buf;
110 printf ("mk_dir() ");
112 result = stat (dir, &stat_buf);
116 result = stat (dir, &stat_buf);
118 printf ("problem creating %s\n", dir);
120 printf ("%s created\n", dir);
123 printf ("%s already exists\n", dir);
134 int fgDEM::open ( const string& file ) {
135 // open input file (or read from stdin)
137 printf("Loading DEM data file: stdin\n");
139 // fd = gzdopen(STDIN_FILENO, "r");
140 printf("Not yet ported ...\n");
143 in = new fg_gzifstream( file );
145 cout << "Cannot open " << file << endl;
148 cout << "Loading DEM data file: " << file << endl;
156 int fgDEM::close () {
157 // the fg_gzifstream doesn't seem to have a close()
165 // return next token from input stream
166 string fgDEM::next_token() {
171 // cout << " returning " + token + "\n";
177 // return next integer from input stream
178 int fgDEM::next_int() {
187 // return next double from input stream
188 double fgDEM::next_double() {
197 // return next exponential num from input stream
198 double fgDEM::next_exp() {
204 token = next_token();
207 const char* p = token.c_str();
211 for ( ; *p != 0; ++p )
219 return ::atof( buf );
221 sscanf(token.c_str(), "%lfD%d", &mantissa, &exp);
223 // cout << " Mantissa = " << mantissa << " Exp = " << exp << "\n";
227 for ( i = 1; i <= exp; i++ ) {
230 } else if ( exp < 0 ) {
231 for ( i = -1; i >= exp; i-- ) {
236 return( (int)rint(mantissa * (double)acc) );
241 // read and parse DEM "A" record
242 int fgDEM::read_a_record() {
249 // get the name field (144 characters)
250 for ( i = 0; i < 144; i++ ) {
255 // clean off the trailing whitespace
257 cout << " Quad name field: " << name << endl;
259 // DEM level code, 3 reflects processing by DMA
261 cout << " DEM level code = " << inum << "\n";
267 // Pattern code, 1 indicates a regular elevation pattern
269 cout << " Pattern code = " << inum << "\n";
271 // Planimetric reference system code, 0 indicates geographic
272 // coordinate system.
274 cout << " Planimetric reference code = " << inum << "\n";
278 cout << " Zone code = " << inum << "\n";
280 // Map projection parameters (ignored)
281 for ( i = 0; i < 15; i++ ) {
283 // printf("%d: %f\n",i,dnum);
286 // Units code, 3 represents arc-seconds as the unit of measure for
287 // ground planimetric coordinates throughout the file.
290 cout << " Unknown (X,Y) units code = " << inum << "!\n";
294 // Units code; 2 represents meters as the unit of measure for
295 // elevation coordinates throughout the file.
298 cout << " Unknown (Z) units code = " << inum << "!\n";
302 // Number (n) of sides in the polygon which defines the coverage of
303 // the DEM file (usually equal to 4).
306 cout << " Unknown polygon dimension = " << inum << "!\n";
310 // Ground coordinates of bounding box in arc-seconds
311 dem_x1 = originx = next_exp();
312 dem_y1 = originy = next_exp();
313 cout << " Origin = (" << originx << "," << originy << ")\n";
324 // Minimum/maximum elevations in meters
327 cout << " Elevation range " << dem_z1 << " to " << dem_z2 << "\n";
329 // Counterclockwise angle from the primary axis of ground
330 // planimetric referenced to the primary axis of the DEM local
332 token = next_token();
334 // Accuracy code; 0 indicates that a record of accuracy does not
335 // exist and that no record type C will follow.
337 // DEM spacial resolution. Usually (3,3,1) (3,6,1) or (3,9,1)
338 // depending on latitude
340 // I will eventually have to do something with this for data at
341 // higher latitudes */
342 token = next_token();
343 cout << " accuracy & spacial resolution string = " << token << endl;
345 cout << " length = " << i << "\n";
348 inum = atoi( token.substr( 0, i - 36 ) );
349 row_step = atof( token.substr( i - 36, 12 ) );
350 col_step = atof( token.substr( i - 24, 12 ) );
351 //token.substr( 25, 12 )
353 ptr = token.c_str() + i - 12;
354 cout << " last field = " << ptr << " = " << atof(ptr) << "\n";
358 col_step = atof(ptr);
359 cout << " last field = " << ptr << " = " << col_step << "\n";
363 row_step = atof(ptr);
364 cout << " last field = " << ptr << " = " << row_step << "\n";
367 // accuracy code = atod(token)
371 cout << " Accuracy code = " << inum << "\n";
373 cout << " column step = " << col_step <<
374 " row step = " << row_step << "\n";
376 // dimension of arrays to follow (1)
377 token = next_token();
379 // number of profiles
380 dem_num_profiles = cols = next_int();
381 cout << " Expecting " << dem_num_profiles << " profiles\n";
387 // read and parse DEM "B" record
388 void fgDEM::read_b_record( ) {
392 // row / column id of this profile
393 prof_row = next_int();
394 prof_col = next_int();
395 // printf("col id = %d row id = %d\n", prof_col, prof_row);
397 // Number of columns and rows (elevations) in this profile
398 prof_num_rows = rows = next_int();
399 prof_num_cols = next_int();
400 // printf(" profile num rows = %d\n", prof_num_rows);
402 // Ground planimetric coordinates (arc-seconds) of the first
403 // elevation in the profile
404 prof_x1 = next_exp();
405 prof_y1 = next_exp();
406 // printf(" Starting at %.2f %.2f\n", prof_x1, prof_y1);
408 // Elevation of local datum for the profile. Always zero for
409 // 1-degree DEM, the reference is mean sea level.
410 token = next_token();
412 // Minimum and maximum elevations for the profile.
413 token = next_token();
414 token = next_token();
416 // One (usually) dimensional array (prof_num_cols,1) of elevations
417 for ( i = 0; i < prof_num_rows; i++ ) {
418 prof_data = next_int();
419 dem_data[cur_col][i] = (float)prof_data;
425 int fgDEM::parse( ) {
430 if ( !read_a_record() ) {
434 for ( i = 0; i < dem_num_profiles; i++ ) {
435 // printf("Ready to read next b record\n");
439 if ( cur_col % 100 == 0 ) {
440 cout << " loaded " << cur_col << " profiles of data\n";
444 cout << " Done parsing\n";
450 // return the current altitude based on mesh data. We should rewrite
451 // this to interpolate exact values, but for now this is good enough
452 double fgDEM::interpolate_altitude( double lon, double lat ) {
453 // we expect incoming (lon,lat) to be in arcsec for now
455 double xlocal, ylocal, dx, dy, zA, zB, elev;
456 int x1, x2, x3, y1, y2, y3;
460 /* determine if we are in the lower triangle or the upper triangle
468 then calculate our end points
471 xlocal = (lon - originx) / col_step;
472 ylocal = (lat - originy) / row_step;
474 xindex = (int)(xlocal);
475 yindex = (int)(ylocal);
477 // printf("xindex = %d yindex = %d\n", xindex, yindex);
479 if ( xindex + 1 == cols ) {
483 if ( yindex + 1 == rows ) {
487 if ( (xindex < 0) || (xindex + 1 >= cols) ||
488 (yindex < 0) || (yindex + 1 >= rows) ) {
492 dx = xlocal - xindex;
493 dy = ylocal - yindex;
497 // printf(" Lower triangle\n");
501 z1 = dem_data[x1][y1];
505 z2 = dem_data[x2][y2];
509 z3 = dem_data[x3][y3];
511 // printf(" dx = %.2f dy = %.2f\n", dx, dy);
512 // printf(" (x1,y1,z1) = (%d,%d,%d)\n", x1, y1, z1);
513 // printf(" (x2,y2,z2) = (%d,%d,%d)\n", x2, y2, z2);
514 // printf(" (x3,y3,z3) = (%d,%d,%d)\n", x3, y3, z3);
516 zA = dx * (z2 - z1) + z1;
517 zB = dx * (z3 - z1) + z1;
519 // printf(" zA = %.2f zB = %.2f\n", zA, zB);
521 if ( dx > FG_EPSILON ) {
522 elev = dy * (zB - zA) / dx + zA;
528 // printf(" Upper triangle\n");
532 z1 = dem_data[x1][y1];
536 z2 = dem_data[x2][y2];
540 z3 = dem_data[x3][y3];
542 // printf(" dx = %.2f dy = %.2f\n", dx, dy);
543 // printf(" (x1,y1,z1) = (%d,%d,%d)\n", x1, y1, z1);
544 // printf(" (x2,y2,z2) = (%d,%d,%d)\n", x2, y2, z2);
545 // printf(" (x3,y3,z3) = (%d,%d,%d)\n", x3, y3, z3);
547 zA = dy * (z2 - z1) + z1;
548 zB = dy * (z3 - z1) + z1;
550 // printf(" zA = %.2f zB = %.2f\n", zA, zB );
551 // printf(" xB - xA = %.2f\n", col_step * dy / row_step);
553 if ( dy > FG_EPSILON ) {
554 elev = dx * (zB - zA) / dy + zA;
564 // Use least squares to fit a simpler data set to dem data
565 void fgDEM::fit( double error, fgBUCKET *p ) {
566 double x[DEM_SIZE_1], y[DEM_SIZE_1];
567 double m, b, ave_error, max_error;
569 int n, row, start, end;
570 int colmin, colmax, rowmin, rowmax;
572 // FILE *dem, *fit, *fit1;
574 printf("Initializing output mesh structure\n");
577 // determine dimensions
578 colmin = p->x * ( (cols - 1) / 8);
579 colmax = colmin + ( (cols - 1) / 8);
580 rowmin = p->y * ( (rows - 1) / 8);
581 rowmax = rowmin + ( (rows - 1) / 8);
582 printf("Fitting region = %d,%d to %d,%d\n", colmin, rowmin, colmax, rowmax);
584 // include the corners explicitly
585 outputmesh_set_pt(colmin, rowmin, dem_data[colmin][rowmin]);
586 outputmesh_set_pt(colmin, rowmax, dem_data[colmin][rowmax]);
587 outputmesh_set_pt(colmax, rowmax, dem_data[colmax][rowmax]);
588 outputmesh_set_pt(colmax, rowmin, dem_data[colmax][rowmin]);
590 printf("Beginning best fit procedure\n");
592 for ( row = rowmin; row <= rowmax; row++ ) {
593 // fit = fopen("fit.dat", "w");
594 // fit1 = fopen("fit1.dat", "w");
598 // printf(" fitting row = %d\n", row);
600 while ( start < colmax ) {
604 x[(end - start) - 1] = 0.0 + ( start * col_step );
605 y[(end - start) - 1] = dem_data[start][row];
607 while ( (end <= colmax) && good_fit ) {
608 n = (end - start) + 1;
609 // printf("Least square of first %d points\n", n);
610 x[end - start] = 0.0 + ( end * col_step );
611 y[end - start] = dem_data[end][row];
612 least_squares(x, y, n, &m, &b);
613 ave_error = least_squares_error(x, y, n, m, b);
614 max_error = least_squares_max_error(x, y, n, m, b);
617 printf("%d - %d ave error = %.2f max error = %.2f y = %.2f*x + %.2f\n",
618 start, end, ave_error, max_error, m, b);
620 f = fopen("gnuplot.dat", "w");
621 for ( j = 0; j <= end; j++) {
622 fprintf(f, "%.2f %.2f\n", 0.0 + ( j * col_step ),
625 for ( j = start; j <= end; j++) {
626 fprintf(f, "%.2f %.2f\n", 0.0 + ( j * col_step ),
631 printf("Please hit return: "); gets(junk);
634 if ( max_error > error ) {
642 // error exceeded the threshold, back up
643 end -= 2; // back "end" up to the last good enough fit
644 n--; // back "n" up appropriately too
646 // we popped out of the above loop while still within
647 // the error threshold, so we must be at the end of
652 least_squares(x, y, n, &m, &b);
653 ave_error = least_squares_error(x, y, n, m, b);
654 max_error = least_squares_max_error(x, y, n, m, b);
658 printf("%d - %d ave error = %.2f max error = %.2f y = %.2f*x + %.2f\n",
659 start, end, ave_error, max_error, m, b);
662 fprintf(fit1, "%.2f %.2f\n", x[0], m * x[0] + b);
663 fprintf(fit1, "%.2f %.2f\n", x[end-start], m * x[end-start] + b);
666 if ( start > colmin ) {
667 // skip this for the first line segment
669 outputmesh_set_pt(start, row, (lasty + cury) / 2);
670 // fprintf(fit, "%.2f %.2f\n", x[0], (lasty + cury) / 2);
673 lasty = m * x[end-start] + b;
681 dem = fopen("gnuplot.dat", "w");
682 for ( j = 0; j < DEM_SIZE_1; j++) {
683 fprintf(dem, "%.2f %.2f\n", 0.0 + ( j * col_step ),
689 // NOTICE, this is for testing only. This instance of
690 // output_nodes should be removed. It should be called only
691 // once at the end once all the nodes have been generated.
692 // newmesh_output_nodes(&nm, "mesh.node");
693 // printf("Please hit return: "); gets(junk);
696 // outputmesh_output_nodes(fg_root, p);
700 // Initialize output mesh structure
701 void fgDEM::outputmesh_init( void ) {
704 for ( j = 0; j < DEM_SIZE_1; j++ ) {
705 for ( i = 0; i < DEM_SIZE_1; i++ ) {
706 output_data[i][j] = -9999.0;
712 // Get the value of a mesh node
713 double fgDEM::outputmesh_get_pt( int i, int j ) {
714 return ( output_data[i][j] );
718 // Set the value of a mesh node
719 void fgDEM::outputmesh_set_pt( int i, int j, double value ) {
720 // printf("Setting data[%d][%d] = %.2f\n", i, j, value);
721 output_data[i][j] = value;
725 // Write out a node file that can be used by the "triangle" program.
726 // Check for an optional "index.node.ex" file in case there is a .poly
727 // file to go along with this node file. Include these nodes first
728 // since they are referenced by position from the .poly file.
729 void fgDEM::outputmesh_output_nodes( const string& fg_root, fgBUCKET *p ) {
730 double exnodes[MAX_EX_NODES][3];
731 struct stat stat_buf;
733 char base_path[256], file[256], exfile[256];
740 int colmin, colmax, rowmin, rowmax;
741 int i, j, count, excount, result;
743 // determine dimensions
744 colmin = p->x * ( (cols - 1) / 8);
745 colmax = colmin + ( (cols - 1) / 8);
746 rowmin = p->y * ( (rows - 1) / 8);
747 rowmax = rowmin + ( (rows - 1) / 8);
748 cout << " dumping region = " << colmin << "," << rowmin << " to " <<
749 colmax << "," << rowmax << "\n";
751 // generate the base directory
752 fgBucketGenBasePath(p, base_path);
753 cout << "fg_root = " << fg_root << " Base Path = " << base_path << endl;
754 dir = fg_root + "/Scenery/" + base_path;
755 cout << "Dir = " << dir << endl;
757 // stat() directory and create if needed
759 result = stat(dir.c_str(), &stat_buf);
760 if ( result != 0 && errno == ENOENT ) {
761 cout << "Creating directory\n";
765 command = "mkdir -p " + dir + "\n";
766 system( command.c_str() );
771 // Cygwin crashes when trying to output to node file
772 // explicitly making directory structure seems OK on Win95
774 extract_path (base_path, tmp_path);
776 dir = fg_root + "/Scenery";
777 if (my_mkdir ( dir.c_str() )) { exit (-1); }
779 dir = fg_root + "/Scenery/" + tmp_path;
780 if (my_mkdir ( dir.c_str() )) { exit (-1); }
782 dir = fg_root + "/Scenery/" + base_path;
783 if (my_mkdir ( dir.c_str() )) { exit (-1); }
789 // assume directory exists
792 // get index and generate output file name
793 index = fgBucketGenIndex(p);
794 sprintf(file, "%s/%ld.node", dir.c_str(), index);
796 // get (optional) extra node file name (in case there is matching
798 strcpy(exfile, file);
799 strcat(exfile, ".ex");
801 // load extra nodes if they exist
803 if ( (fd = fopen(exfile, "r")) != NULL ) {
805 fscanf(fd, "%d %d %d %d", &excount, &junki, &junki, &junki);
807 if ( excount > MAX_EX_NODES - 1 ) {
808 printf("Error, too many 'extra' nodes, increase array size\n");
811 printf(" Expecting %d 'extra' nodes\n", excount);
814 for ( i = 1; i <= excount; i++ ) {
815 fscanf(fd, "%d %lf %lf %lf\n", &junki,
816 &exnodes[i][0], &exnodes[i][1], &exnodes[i][2]);
817 printf("(extra) %d %.2f %.2f %.2f\n",
818 i, exnodes[i][0], exnodes[i][1], exnodes[i][2]);
823 printf("Creating node file: %s\n", file);
824 fd = fopen(file, "w");
826 // first count regular nodes to generate header
828 for ( j = rowmin; j <= rowmax; j++ ) {
829 for ( i = colmin; i <= colmax; i++ ) {
830 if ( output_data[i][j] > -9000.0 ) {
834 // printf(" count = %d\n", count);
836 fprintf(fd, "%d 2 1 0\n", count + excount);
838 // now write out extra node data
839 for ( i = 1; i <= excount; i++ ) {
840 fprintf(fd, "%d %.2f %.2f %.2f\n",
841 i, exnodes[i][0], exnodes[i][1], exnodes[i][2]);
844 // write out actual node data
846 for ( j = rowmin; j <= rowmax; j++ ) {
847 for ( i = colmin; i <= colmax; i++ ) {
848 if ( output_data[i][j] > -9000.0 ) {
849 fprintf(fd, "%d %.2f %.2f %.2f\n",
851 originx + (double)i * col_step,
852 originy + (double)j * row_step,
856 // printf(" count = %d\n", count);
863 fgDEM::~fgDEM( void ) {
864 // printf("class fgDEM DEstructor called.\n");
866 delete [] output_data;
871 // Revision 1.22 1999/01/19 20:56:56 curt
872 // MacOS portability changes contributed by "Robert Puyol" <puyol@abvent.fr>
874 // Revision 1.21 1998/11/06 14:04:32 curt
875 // Changes due to updates in fgstream.
877 // Revision 1.20 1998/10/28 19:38:20 curt
878 // Elliminate some unnecessary win32 specific stuff (by Norman Vine)
880 // Revision 1.19 1998/10/22 21:59:19 curt
881 // Fixed a couple subtle bugs that resulted from some of my c++ conversions.
882 // One bug could cause a segfault on certain input, and the other bug could
883 // cause the whole procedure to go balistic and generate huge files (also only
884 // on rare input combinations.)
886 // Revision 1.18 1998/10/18 01:17:09 curt
889 // Revision 1.17 1998/10/16 19:08:12 curt
890 // Portability updates from Bernie Bright.
892 // Revision 1.16 1998/10/02 21:41:39 curt
895 // Revision 1.15 1998/09/21 20:53:59 curt
896 // minor tweaks to clean a few additional things up after the rewrite.
898 // Revision 1.14 1998/09/19 17:59:45 curt
899 // Use c++ streams (fg_gzifstream). Also converted many character arrays to
902 // Revision 1.13 1998/09/09 16:24:04 curt
903 // Fixed a bug in the handling of exclude files which was causing
904 // a crash by calling fclose() on an invalid file handle.
906 // Revision 1.12 1998/08/24 20:03:31 curt
907 // Eliminated a possible memory overrun error.
908 // Use the proper free() rather than the incorrect delete().
910 // Revision 1.11 1998/07/20 12:46:11 curt
911 // When outputing to a .node file, first check for an optional
912 // "index.node.ex" file in case there is a .poly file to go along with this
913 // node file. Include these nodes first since they are referenced by position
914 // from the .poly file. This is my first pass at adding an area "cutout"
915 // feature to the terrain generation pipeline.
917 // Revision 1.10 1998/07/13 20:58:02 curt
920 // Revision 1.9 1998/07/13 15:29:49 curt
921 // Added #ifdef HAVE_CONFIG_H
923 // Revision 1.8 1998/07/04 00:47:18 curt
924 // typedef'd struct fgBUCKET.
926 // Revision 1.7 1998/06/05 18:14:39 curt
927 // Abort out early when reading the "A" record if it doesn't look like
928 // a proper DEM file.
930 // Revision 1.6 1998/05/02 01:49:21 curt
931 // Fixed a bug where the wrong variable was being initialized.
933 // Revision 1.5 1998/04/25 15:00:32 curt
934 // Changed "r" to "rb" in gzopen() options. This fixes bad behavior in win32.
936 // Revision 1.4 1998/04/22 13:14:46 curt
937 // Fixed a bug in zlib usage.
939 // Revision 1.3 1998/04/18 03:53:05 curt
940 // Added zlib support.
942 // Revision 1.2 1998/04/14 02:43:27 curt
943 // Used "new" to auto-allocate large DEM parsing arrays in class constructor.
945 // Revision 1.1 1998/04/08 22:57:22 curt
946 // Adopted Gnu automake/autoconf system.
948 // Revision 1.3 1998/04/06 21:09:41 curt
949 // Additional win32 support.
950 // Fixed a bad bug in dem file parsing that was causing the output to be
951 // flipped about x = y.
953 // Revision 1.2 1998/03/23 20:35:41 curt
954 // Updated to use FG_EPSILON
956 // Revision 1.1 1998/03/19 02:54:47 curt
957 // Reorganized into a class lib called fgDEM.
959 // Revision 1.1 1998/03/19 01:46:28 curt