1 // assemtris.cxx -- reassemble the pieces produced by splittris
3 // Written by Curtis Olson, started January 1998.
5 // Copyright (C) 1997 Curtis L. Olson - curt@me.umn.edu
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 // (Log is kept at end of this file)
27 #include <stdlib.h> // for atoi()
29 #include <sys/stat.h> // for stat()
30 #include <unistd.h> // for stat()
32 #include "assemtris.hxx"
34 #include <Include/fg_constants.h>
35 #include <Bucket/bucketutils.h>
38 // #define OFFSET_LON 0.1
39 // #define OFFSET_LAT 0.1
41 #define OFFSET_LON 0.0
42 #define OFFSET_LAT 0.0
47 static double nodes[MAX_NODES][3];
48 static double exnodes[MAX_NODES][3];
52 fgBUCKET ne_index, nw_index, sw_index, se_index;
53 fgBUCKET north_index, south_index, east_index, west_index;
56 // return the file base name ( foo/bar/file.ext = file.ext )
57 void extract_file(char *in, char *base) {
63 while ( (i >= 0) && (in[i] != '/') ) {
72 // return the file path name ( foo/bar/file.ext = foo/bar )
73 void extract_path(char *in, char *base) {
80 while ( (i >= 0) && (in[i] != '/') ) {
88 // check to see if specified node is in the extra list
89 int is_extra_node(double *n) {
92 for ( i = 1; i <= excount; i++ ) {
93 // we only check lon/lat in case the height got fooled with
95 if ( (fabs(n[0] - exnodes[i][0]) < FG_EPSILON) &&
96 (fabs(n[1] - exnodes[i][1]) < FG_EPSILON) ) {
104 // Read all the extra nodes. These typically define inner areas to
105 // exclude from triangulations. There will be a .poly file that
106 // refers to these by position number which assumes all the extra
107 // nodes come first in the generated .node file.
108 void read_extra_nodes(char *exfile) {
110 int i, junk1, junk2, junk3;
112 // load extra nodes if they exist
114 if ( (fd = fopen(exfile, "r")) != NULL ) {
115 printf("Found and 'extra' node file = %s\n", exfile);
116 fscanf(fd, "%d %d %d %d", &excount, &junk1, &junk2, &junk3);
118 if ( excount > MAX_NODES - 1 ) {
119 printf("Error, too many 'extra' nodes, increase array size\n");
122 printf(" Expecting %d 'extra' nodes\n", excount);
125 for ( i = 1; i <= excount; i++ ) {
126 fscanf(fd, "%d %lf %lf %lf\n", &junk1,
127 &exnodes[i][0], &exnodes[i][1], &exnodes[i][2]);
128 printf("(extra) %d %.2f %.2f %.2f\n",
129 i, exnodes[i][0], exnodes[i][1], exnodes[i][2]);
136 // check if a file exists
137 int file_exists(char *file) {
138 struct stat stat_buf;
141 printf("checking %s ... ", file);
143 result = stat(file, &stat_buf);
146 // stat failed, no file
147 printf("not found.\n");
150 // stat succeeded, file exists
157 // check to see if a shared object exists
158 int shared_object_exists(char *basepath, char *ext, char *file) {
159 char scene_path[256];
162 if ( strcmp(ext, ".sw") == 0 ) {
163 fgBucketGenBasePath(&my_index, scene_path);
164 index = fgBucketGenIndex(&my_index);
165 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
166 if ( file_exists(file) ) {
169 fgBucketGenBasePath(&west_index, scene_path);
170 index = fgBucketGenIndex(&west_index);
171 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
172 if ( file_exists(file) ) {
175 fgBucketGenBasePath(&sw_index, scene_path);
176 index = fgBucketGenIndex(&sw_index);
177 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
178 if ( file_exists(file) ) {
181 fgBucketGenBasePath(&south_index, scene_path);
182 index = fgBucketGenIndex(&south_index);
183 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
184 if ( file_exists(file) ) {
189 if ( strcmp(ext, ".se") == 0 ) {
190 fgBucketGenBasePath(&my_index, scene_path);
191 index = fgBucketGenIndex(&my_index);
192 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
193 if ( file_exists(file) ) {
196 fgBucketGenBasePath(&east_index, scene_path);
197 index = fgBucketGenIndex(&east_index);
198 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
199 if ( file_exists(file) ) {
202 fgBucketGenBasePath(&se_index, scene_path);
203 index = fgBucketGenIndex(&se_index);
204 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
205 if ( file_exists(file) ) {
208 fgBucketGenBasePath(&south_index, scene_path);
209 index = fgBucketGenIndex(&south_index);
210 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
211 if ( file_exists(file) ) {
216 if ( strcmp(ext, ".ne") == 0 ) {
217 fgBucketGenBasePath(&my_index, scene_path);
218 index = fgBucketGenIndex(&my_index);
219 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
220 if ( file_exists(file) ) {
223 fgBucketGenBasePath(&east_index, scene_path);
224 index = fgBucketGenIndex(&east_index);
225 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
226 if ( file_exists(file) ) {
229 fgBucketGenBasePath(&ne_index, scene_path);
230 index = fgBucketGenIndex(&ne_index);
231 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
232 if ( file_exists(file) ) {
235 fgBucketGenBasePath(&north_index, scene_path);
236 index = fgBucketGenIndex(&north_index);
237 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
238 if ( file_exists(file) ) {
243 if ( strcmp(ext, ".nw") == 0 ) {
244 fgBucketGenBasePath(&my_index, scene_path);
245 index = fgBucketGenIndex(&my_index);
246 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
247 if ( file_exists(file) ) {
250 fgBucketGenBasePath(&west_index, scene_path);
251 index = fgBucketGenIndex(&west_index);
252 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
253 if ( file_exists(file) ) {
256 fgBucketGenBasePath(&nw_index, scene_path);
257 index = fgBucketGenIndex(&nw_index);
258 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
259 if ( file_exists(file) ) {
262 fgBucketGenBasePath(&north_index, scene_path);
263 index = fgBucketGenIndex(&north_index);
264 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
265 if ( file_exists(file) ) {
270 if ( strcmp(ext, ".south") == 0 ) {
271 fgBucketGenBasePath(&my_index, scene_path);
272 index = fgBucketGenIndex(&my_index);
273 sprintf(file, "%s/%s/%ld.1.south", basepath, scene_path, index);
274 if ( file_exists(file) ) {
277 fgBucketGenBasePath(&south_index, scene_path);
278 index = fgBucketGenIndex(&south_index);
279 sprintf(file, "%s/%s/%ld.1.north", basepath, scene_path, index);
280 if ( file_exists(file) ) {
285 if ( strcmp(ext, ".north") == 0 ) {
286 fgBucketGenBasePath(&my_index, scene_path);
287 index = fgBucketGenIndex(&my_index);
288 sprintf(file, "%s/%s/%ld.1.north", basepath, scene_path, index);
289 if ( file_exists(file) ) {
292 fgBucketGenBasePath(&north_index, scene_path);
293 index = fgBucketGenIndex(&north_index);
294 sprintf(file, "%s/%s/%ld.1.south", basepath, scene_path, index);
295 if ( file_exists(file) ) {
300 if ( strcmp(ext, ".west") == 0 ) {
301 fgBucketGenBasePath(&my_index, scene_path);
302 index = fgBucketGenIndex(&my_index);
303 sprintf(file, "%s/%s/%ld.1.west", basepath, scene_path, index);
304 if ( file_exists(file) ) {
307 fgBucketGenBasePath(&west_index, scene_path);
308 index = fgBucketGenIndex(&west_index);
309 sprintf(file, "%s/%s/%ld.1.east", basepath, scene_path, index);
310 if ( file_exists(file) ) {
315 if ( strcmp(ext, ".east") == 0 ) {
316 fgBucketGenBasePath(&my_index, scene_path);
317 index = fgBucketGenIndex(&my_index);
318 sprintf(file, "%s/%s/%ld.1.east", basepath, scene_path, index);
319 if ( file_exists(file) ) {
322 fgBucketGenBasePath(&east_index, scene_path);
323 index = fgBucketGenIndex(&east_index);
324 sprintf(file, "%s/%s/%ld.1.west", basepath, scene_path, index);
325 if ( file_exists(file) ) {
330 if ( strcmp(ext, ".body") == 0 ) {
331 fgBucketGenBasePath(&my_index, scene_path);
332 index = fgBucketGenIndex(&my_index);
333 sprintf(file, "%s/%s/%ld.1.body", basepath, scene_path, index);
334 if ( file_exists(file) ) {
343 // my custom file opening routine ... don't open if a shared edge or
344 // vertex alread exists
345 FILE *my_open(char *basename, char *basepath, char *ext) {
349 // check if a shared object already exists
350 if ( shared_object_exists(basepath, ext, filename) ) {
351 // not an actual file open error, but we've already got the
352 // shared edge, so we don't want to create another one
353 fp = fopen(filename, "r");
354 printf("Opening %s\n", filename);
358 printf("not opening\n");
364 // given a file pointer, read all the gdn (geodetic nodes from it.)
365 // The specified offset values (in arcsec) are used to overlap the
366 // edges of the tile slightly to cover gaps induced by floating point
367 // precision problems. 1 arcsec == about 100 feet so 0.01 arcsec ==
369 void read_nodes(FILE *fp, double offset_lon, double offset_lat) {
374 offset_lon = offset_lat = 0.0;
376 while ( fgets(line, 250, fp) != NULL ) {
377 if ( strncmp(line, "gdn ", 4) == 0 ) {
378 sscanf(line, "gdn %lf %lf %lf\n", &n[0], &n[1], &n[2]);
380 ex_index = is_extra_node(n);
382 if ( ex_index == 0 ) {
384 nodes[nodecount][0] = n[0] + offset_lon;
385 nodes[nodecount][1] = n[1] + offset_lat;
386 nodes[nodecount][2] = n[2];
388 // printf("read_nodes(%d) %.2f %.2f %.2f %s", nodecount,
389 // nodes[nodecount][0], nodes[nodecount][1],
390 // nodes[nodecount][2], line);
396 printf("found extra node %.2f %.2f %.2f\n", n[0], n[1], n[2]);
397 // preserve the DEM altitude for now
398 exnodes[ex_index][2] = n[2];
405 // load in nodes from the various split and shared pieces to
406 // reconstruct a tile
407 void build_node_list(char *basename, char *basepath) {
409 FILE *ne, *nw, *se, *sw, *north, *south, *east, *west, *body;
411 // load extra nodes if they exist
412 strcpy(exfile, basename);
413 strcat(exfile, ".node.ex");
414 read_extra_nodes(exfile);
416 ne = my_open(basename, basepath, ".ne");
417 read_nodes(ne, OFFSET_LON, OFFSET_LAT);
420 nw = my_open(basename, basepath, ".nw");
421 read_nodes(nw, -1.0 * OFFSET_LON, OFFSET_LAT);
424 se = my_open(basename, basepath, ".se");
425 read_nodes(se, OFFSET_LON, -1.0 * OFFSET_LAT);
428 sw = my_open(basename, basepath, ".sw");
429 read_nodes(sw, -1.0 * OFFSET_LON, -1.0 * OFFSET_LAT);
432 north = my_open(basename, basepath, ".north");
433 read_nodes(north, 0.0, OFFSET_LAT);
436 south = my_open(basename, basepath, ".south");
437 read_nodes(south, 0.0, -1.0 * OFFSET_LAT);
440 east = my_open(basename, basepath, ".east");
441 read_nodes(east, OFFSET_LON, 0.0);
444 west = my_open(basename, basepath, ".west");
445 read_nodes(west, -1.0 * OFFSET_LON, 0.0);
448 body = my_open(basename, basepath, ".body");
449 read_nodes(body, 0.0, 0.0);
454 // dump in WaveFront .obj format
455 void dump_nodes(char *basename) {
460 // generate output file name
461 strcpy(file, basename);
462 // len = strlen(file);
463 // file[len-2] = '\0';
464 strcat(file, ".node");
467 printf("Creating node file: %s\n", file);
468 printf(" writing vertices in .node format.\n");
469 fd = fopen(file, "w");
471 fprintf(fd, "%d 2 1 0\n", excount + nodecount);
473 // now write out extra node data
474 for ( i = 1; i <= excount; i++ ) {
475 fprintf(fd, "%d %.2f %.2f %.2f 0\n",
476 i, exnodes[i][0], exnodes[i][1], exnodes[i][2]);
479 // now write out actual node data
480 for ( i = 0; i < nodecount; i++ ) {
481 fprintf(fd, "%d %.2f %.2f %.2f 0\n", excount + i + 1,
482 nodes[i][0], nodes[i][1], nodes[i][2]);
489 int main(int argc, char **argv) {
490 char basename[256], basepath[256], temp[256];
495 strcpy(basename, argv[1]);
496 len = strlen(basename);
498 // find the base path of the file
499 extract_path(basename, basepath);
500 extract_path(basepath, basepath);
501 extract_path(basepath, basepath);
502 printf("%s\n", basepath);
504 // find the index of the current file
505 extract_file(basename, temp);
506 // len = strlen(temp);
508 // temp[len-2] = '\0';
510 tmp_index = atoi(temp);
511 printf("%ld\n", tmp_index);
512 fgBucketParseIndex(tmp_index, &my_index);
514 printf("bucket = %d %d %d %d\n",
515 my_index.lon, my_index.lat, my_index.x, my_index.y);
516 // generate the indexes of the neighbors
517 fgBucketOffset(&my_index, &ne_index, 1, 1);
518 fgBucketOffset(&my_index, &nw_index, -1, 1);
519 fgBucketOffset(&my_index, &se_index, 1, -1);
520 fgBucketOffset(&my_index, &sw_index, -1, -1);
522 fgBucketOffset(&my_index, &north_index, 0, 1);
523 fgBucketOffset(&my_index, &south_index, 0, -1);
524 fgBucketOffset(&my_index, &east_index, 1, 0);
525 fgBucketOffset(&my_index, &west_index, -1, 0);
527 // printf("Corner indexes = %ld %ld %ld %ld\n",
528 // ne_index, nw_index, sw_index, se_index);
529 // printf("Edge indexes = %ld %ld %ld %ld\n",
530 // north_index, south_index, east_index, west_index);
533 // load the input data files
534 build_node_list(basename, basepath);
536 // dump in WaveFront .obj format
537 dump_nodes(basename);
544 // Revision 1.3 1998/11/02 18:25:40 curt
545 // Check for __CYGWIN__ (b20) as well as __CYGWIN32__ (pre b20 compilers)
546 // Other misc. tweaks.
548 // Revision 1.2 1998/09/25 19:38:01 curt
549 // Minor tweaks so that this actually compiles.
551 // Revision 1.1 1998/09/25 19:35:29 curt
552 // Renamed assemtris.[ch] to assemtris.[ch]xx
554 // Revision 1.13 1998/09/21 20:56:30 curt
555 // Changes to avoid setting airport area nodes back to their original
556 // elevations if they have been changed.
559 // Revision 1.12 1998/09/09 16:24:51 curt
560 // Fixed a bug in the handling of exclude files which was causing
561 // a crash by calling fclose() on an invalid file handle.
562 // Removed overlapping offsets.
564 // Revision 1.11 1998/08/06 12:47:59 curt
565 // Removed overlap in tiles as a test.
567 // Revision 1.10 1998/07/21 04:34:20 curt
568 // Mods to handle extra nodes (i.e. preserve cutouts).
570 // Revision 1.9 1998/07/04 00:55:39 curt
571 // typedef'd struct fgBUCKET.
573 // Revision 1.8 1998/06/01 17:58:19 curt
574 // Added a slight border overlap to try to minimize pixel wide gaps between
575 // tiles due to round off error. This is not a perfect solution, but helps.
577 // Revision 1.7 1998/04/14 02:26:00 curt
578 // Code reorganizations. Added a Lib/ directory for more general libraries.
580 // Revision 1.6 1998/04/08 22:54:58 curt
581 // Adopted Gnu automake/autoconf system.
583 // Revision 1.5 1998/03/03 16:00:52 curt
584 // More c++ compile tweaks.
586 // Revision 1.4 1998/01/31 00:41:23 curt
587 // Made a few changes converting floats to doubles.
589 // Revision 1.3 1998/01/27 18:37:00 curt
590 // Lots of updates to get back in sync with changes made over in .../Src/
592 // Revision 1.2 1998/01/15 21:33:36 curt
593 // Assembling triangles and building a new .node file with the proper shared
594 // vertices now works. Now we just have to use the shared normals and we'll
597 // Revision 1.1 1998/01/15 02:45:26 curt