1 // assemtris.c -- reassemble the pieces produced by splittris
3 // Written by Curtis Olson, started January 1998.
5 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.h"
34 #include <Include/fg_constants.h>
35 #include <Include/fg_types.h>
36 #include <Bucket/bucketutils.h>
39 // #define OFFSET_LON 0.1
40 // #define OFFSET_LAT 0.1
42 #define OFFSET_LON 0.0
43 #define OFFSET_LAT 0.0
48 static double nodes[MAX_NODES][3];
49 static double exnodes[MAX_NODES][3];
53 fgBUCKET ne_index, nw_index, sw_index, se_index;
54 fgBUCKET north_index, south_index, east_index, west_index;
57 // return the file base name ( foo/bar/file.ext = file.ext )
58 void extract_file(char *in, char *base) {
64 while ( (i >= 0) && (in[i] != '/') ) {
73 // return the file path name ( foo/bar/file.ext = foo/bar )
74 void extract_path(char *in, char *base) {
81 while ( (i >= 0) && (in[i] != '/') ) {
89 // check to see if specified node is in the extra list
90 int is_extra_node(double *n) {
93 for ( i = 1; i <= excount; i++ ) {
94 // we only check lon/lat in case the height got fooled with
96 if ( (fabs(n[0] - exnodes[i][0]) < FG_EPSILON) &&
97 (fabs(n[1] - exnodes[i][1]) < FG_EPSILON) ) {
105 // Read all the extra nodes. These typically define inner areas to
106 // exclude from triangulations. There will be a .poly file that
107 // refers to these by position number which assumes all the extra
108 // nodes come first in the generated .node file.
109 void read_extra_nodes(char *exfile) {
111 int i, junk1, junk2, junk3;
113 // load extra nodes if they exist
115 if ( (fd = fopen(exfile, "r")) != NULL ) {
116 printf("Found and 'extra' node file = %s\n", exfile);
117 fscanf(fd, "%d %d %d %d", &excount, &junk1, &junk2, &junk3);
119 if ( excount > MAX_NODES - 1 ) {
120 printf("Error, too many 'extra' nodes, increase array size\n");
123 printf(" Expecting %d 'extra' nodes\n", excount);
126 for ( i = 1; i <= excount; i++ ) {
127 fscanf(fd, "%d %lf %lf %lf\n", &junk1,
128 &exnodes[i][0], &exnodes[i][1], &exnodes[i][2]);
129 printf("(extra) %d %.2f %.2f %.2f\n",
130 i, exnodes[i][0], exnodes[i][1], exnodes[i][2]);
137 // check if a file exists
138 int file_exists(char *file) {
139 struct stat stat_buf;
142 printf("checking %s ... ", file);
144 result = stat(file, &stat_buf);
147 // stat failed, no file
148 printf("not found.\n");
151 // stat succeeded, file exists
158 // check to see if a shared object exists
159 int shared_object_exists(char *basepath, char *ext, char *file) {
160 char scene_path[256];
163 if ( strcmp(ext, ".sw") == 0 ) {
164 fgBucketGenBasePath(&my_index, scene_path);
165 index = fgBucketGenIndex(&my_index);
166 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
167 if ( file_exists(file) ) {
170 fgBucketGenBasePath(&west_index, scene_path);
171 index = fgBucketGenIndex(&west_index);
172 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
173 if ( file_exists(file) ) {
176 fgBucketGenBasePath(&sw_index, scene_path);
177 index = fgBucketGenIndex(&sw_index);
178 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
179 if ( file_exists(file) ) {
182 fgBucketGenBasePath(&south_index, scene_path);
183 index = fgBucketGenIndex(&south_index);
184 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
185 if ( file_exists(file) ) {
190 if ( strcmp(ext, ".se") == 0 ) {
191 fgBucketGenBasePath(&my_index, scene_path);
192 index = fgBucketGenIndex(&my_index);
193 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
194 if ( file_exists(file) ) {
197 fgBucketGenBasePath(&east_index, scene_path);
198 index = fgBucketGenIndex(&east_index);
199 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
200 if ( file_exists(file) ) {
203 fgBucketGenBasePath(&se_index, scene_path);
204 index = fgBucketGenIndex(&se_index);
205 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
206 if ( file_exists(file) ) {
209 fgBucketGenBasePath(&south_index, scene_path);
210 index = fgBucketGenIndex(&south_index);
211 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
212 if ( file_exists(file) ) {
217 if ( strcmp(ext, ".ne") == 0 ) {
218 fgBucketGenBasePath(&my_index, scene_path);
219 index = fgBucketGenIndex(&my_index);
220 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
221 if ( file_exists(file) ) {
224 fgBucketGenBasePath(&east_index, scene_path);
225 index = fgBucketGenIndex(&east_index);
226 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
227 if ( file_exists(file) ) {
230 fgBucketGenBasePath(&ne_index, scene_path);
231 index = fgBucketGenIndex(&ne_index);
232 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
233 if ( file_exists(file) ) {
236 fgBucketGenBasePath(&north_index, scene_path);
237 index = fgBucketGenIndex(&north_index);
238 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
239 if ( file_exists(file) ) {
244 if ( strcmp(ext, ".nw") == 0 ) {
245 fgBucketGenBasePath(&my_index, scene_path);
246 index = fgBucketGenIndex(&my_index);
247 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
248 if ( file_exists(file) ) {
251 fgBucketGenBasePath(&west_index, scene_path);
252 index = fgBucketGenIndex(&west_index);
253 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
254 if ( file_exists(file) ) {
257 fgBucketGenBasePath(&nw_index, scene_path);
258 index = fgBucketGenIndex(&nw_index);
259 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
260 if ( file_exists(file) ) {
263 fgBucketGenBasePath(&north_index, scene_path);
264 index = fgBucketGenIndex(&north_index);
265 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
266 if ( file_exists(file) ) {
271 if ( strcmp(ext, ".south") == 0 ) {
272 fgBucketGenBasePath(&my_index, scene_path);
273 index = fgBucketGenIndex(&my_index);
274 sprintf(file, "%s/%s/%ld.1.south", basepath, scene_path, index);
275 if ( file_exists(file) ) {
278 fgBucketGenBasePath(&south_index, scene_path);
279 index = fgBucketGenIndex(&south_index);
280 sprintf(file, "%s/%s/%ld.1.north", basepath, scene_path, index);
281 if ( file_exists(file) ) {
286 if ( strcmp(ext, ".north") == 0 ) {
287 fgBucketGenBasePath(&my_index, scene_path);
288 index = fgBucketGenIndex(&my_index);
289 sprintf(file, "%s/%s/%ld.1.north", basepath, scene_path, index);
290 if ( file_exists(file) ) {
293 fgBucketGenBasePath(&north_index, scene_path);
294 index = fgBucketGenIndex(&north_index);
295 sprintf(file, "%s/%s/%ld.1.south", basepath, scene_path, index);
296 if ( file_exists(file) ) {
301 if ( strcmp(ext, ".west") == 0 ) {
302 fgBucketGenBasePath(&my_index, scene_path);
303 index = fgBucketGenIndex(&my_index);
304 sprintf(file, "%s/%s/%ld.1.west", basepath, scene_path, index);
305 if ( file_exists(file) ) {
308 fgBucketGenBasePath(&west_index, scene_path);
309 index = fgBucketGenIndex(&west_index);
310 sprintf(file, "%s/%s/%ld.1.east", basepath, scene_path, index);
311 if ( file_exists(file) ) {
316 if ( strcmp(ext, ".east") == 0 ) {
317 fgBucketGenBasePath(&my_index, scene_path);
318 index = fgBucketGenIndex(&my_index);
319 sprintf(file, "%s/%s/%ld.1.east", basepath, scene_path, index);
320 if ( file_exists(file) ) {
323 fgBucketGenBasePath(&east_index, scene_path);
324 index = fgBucketGenIndex(&east_index);
325 sprintf(file, "%s/%s/%ld.1.west", basepath, scene_path, index);
326 if ( file_exists(file) ) {
331 if ( strcmp(ext, ".body") == 0 ) {
332 fgBucketGenBasePath(&my_index, scene_path);
333 index = fgBucketGenIndex(&my_index);
334 sprintf(file, "%s/%s/%ld.1.body", basepath, scene_path, index);
335 if ( file_exists(file) ) {
344 // my custom file opening routine ... don't open if a shared edge or
345 // vertex alread exists
346 FILE *my_open(char *basename, char *basepath, char *ext) {
350 // check if a shared object already exists
351 if ( shared_object_exists(basepath, ext, filename) ) {
352 // not an actual file open error, but we've already got the
353 // shared edge, so we don't want to create another one
354 fp = fopen(filename, "r");
355 printf("Opening %s\n", filename);
359 printf("not opening\n");
365 // given a file pointer, read all the gdn (geodetic nodes from it.)
366 // The specified offset values (in arcsec) are used to overlap the
367 // edges of the tile slightly to cover gaps induced by floating point
368 // precision problems. 1 arcsec == about 100 feet so 0.01 arcsec ==
370 void read_nodes(FILE *fp, double offset_lon, double offset_lat) {
375 offset_lon = offset_lat = 0.0;
377 while ( fgets(line, 250, fp) != NULL ) {
378 if ( strncmp(line, "gdn ", 4) == 0 ) {
379 sscanf(line, "gdn %lf %lf %lf\n", &n[0], &n[1], &n[2]);
381 ex_index = is_extra_node(n);
383 if ( ex_index == 0 ) {
385 nodes[nodecount][0] = n[0] + offset_lon;
386 nodes[nodecount][1] = n[1] + offset_lat;
387 nodes[nodecount][2] = n[2];
389 // printf("read_nodes(%d) %.2f %.2f %.2f %s", nodecount,
390 // nodes[nodecount][0], nodes[nodecount][1],
391 // nodes[nodecount][2], line);
397 printf("found extra node %.2f %.2f %.2f\n", n[0], n[1], n[2]);
398 // preserve the DEM altitude for now
399 exnodes[ex_index][2] = n[2];
406 // load in nodes from the various split and shared pieces to
407 // reconstruct a tile
408 void build_node_list(char *basename, char *basepath) {
410 FILE *ne, *nw, *se, *sw, *north, *south, *east, *west, *body;
412 // load extra nodes if they exist
413 strcpy(exfile, basename);
414 strcat(exfile, ".node.ex");
415 read_extra_nodes(exfile);
417 ne = my_open(basename, basepath, ".ne");
418 read_nodes(ne, OFFSET_LON, OFFSET_LAT);
421 nw = my_open(basename, basepath, ".nw");
422 read_nodes(nw, -1.0 * OFFSET_LON, OFFSET_LAT);
425 se = my_open(basename, basepath, ".se");
426 read_nodes(se, OFFSET_LON, -1.0 * OFFSET_LAT);
429 sw = my_open(basename, basepath, ".sw");
430 read_nodes(sw, -1.0 * OFFSET_LON, -1.0 * OFFSET_LAT);
433 north = my_open(basename, basepath, ".north");
434 read_nodes(north, 0.0, OFFSET_LAT);
437 south = my_open(basename, basepath, ".south");
438 read_nodes(south, 0.0, -1.0 * OFFSET_LAT);
441 east = my_open(basename, basepath, ".east");
442 read_nodes(east, OFFSET_LON, 0.0);
445 west = my_open(basename, basepath, ".west");
446 read_nodes(west, -1.0 * OFFSET_LON, 0.0);
449 body = my_open(basename, basepath, ".body");
450 read_nodes(body, 0.0, 0.0);
455 // dump in WaveFront .obj format
456 void dump_nodes(char *basename) {
461 // generate output file name
462 strcpy(file, basename);
463 // len = strlen(file);
464 // file[len-2] = '\0';
465 strcat(file, ".node");
468 printf("Creating node file: %s\n", file);
469 printf(" writing vertices in .node format.\n");
470 fd = fopen(file, "w");
472 fprintf(fd, "%d 2 1 0\n", excount + nodecount);
474 // now write out extra node data
475 for ( i = 1; i <= excount; i++ ) {
476 fprintf(fd, "%d %.2f %.2f %.2f 0\n",
477 i, exnodes[i][0], exnodes[i][1], exnodes[i][2]);
480 // now write out actual node data
481 for ( i = 0; i < nodecount; i++ ) {
482 fprintf(fd, "%d %.2f %.2f %.2f 0\n", excount + i + 1,
483 nodes[i][0], nodes[i][1], nodes[i][2]);
490 int main(int argc, char **argv) {
491 char basename[256], basepath[256], temp[256];
496 strcpy(basename, argv[1]);
497 len = strlen(basename);
499 // find the base path of the file
500 extract_path(basename, basepath);
501 extract_path(basepath, basepath);
502 extract_path(basepath, basepath);
503 printf("%s\n", basepath);
505 // find the index of the current file
506 extract_file(basename, temp);
507 // len = strlen(temp);
509 // temp[len-2] = '\0';
511 tmp_index = atoi(temp);
512 printf("%ld\n", tmp_index);
513 fgBucketParseIndex(tmp_index, &my_index);
515 printf("bucket = %d %d %d %d\n",
516 my_index.lon, my_index.lat, my_index.x, my_index.y);
517 // generate the indexes of the neighbors
518 fgBucketOffset(&my_index, &ne_index, 1, 1);
519 fgBucketOffset(&my_index, &nw_index, -1, 1);
520 fgBucketOffset(&my_index, &se_index, 1, -1);
521 fgBucketOffset(&my_index, &sw_index, -1, -1);
523 fgBucketOffset(&my_index, &north_index, 0, 1);
524 fgBucketOffset(&my_index, &south_index, 0, -1);
525 fgBucketOffset(&my_index, &east_index, 1, 0);
526 fgBucketOffset(&my_index, &west_index, -1, 0);
528 // printf("Corner indexes = %ld %ld %ld %ld\n",
529 // ne_index, nw_index, sw_index, se_index);
530 // printf("Edge indexes = %ld %ld %ld %ld\n",
531 // north_index, south_index, east_index, west_index);
534 // load the input data files
535 build_node_list(basename, basepath);
537 // dump in WaveFront .obj format
538 dump_nodes(basename);
545 // Revision 1.1 1998/09/25 19:35:29 curt
546 // Renamed assemtris.[ch] to assemtris.[ch]xx
548 // Revision 1.13 1998/09/21 20:56:30 curt
549 // Changes to avoid setting airport area nodes back to their original
550 // elevations if they have been changed.
553 // Revision 1.12 1998/09/09 16:24:51 curt
554 // Fixed a bug in the handling of exclude files which was causing
555 // a crash by calling fclose() on an invalid file handle.
556 // Removed overlapping offsets.
558 // Revision 1.11 1998/08/06 12:47:59 curt
559 // Removed overlap in tiles as a test.
561 // Revision 1.10 1998/07/21 04:34:20 curt
562 // Mods to handle extra nodes (i.e. preserve cutouts).
564 // Revision 1.9 1998/07/04 00:55:39 curt
565 // typedef'd struct fgBUCKET.
567 // Revision 1.8 1998/06/01 17:58:19 curt
568 // Added a slight border overlap to try to minimize pixel wide gaps between
569 // tiles due to round off error. This is not a perfect solution, but helps.
571 // Revision 1.7 1998/04/14 02:26:00 curt
572 // Code reorganizations. Added a Lib/ directory for more general libraries.
574 // Revision 1.6 1998/04/08 22:54:58 curt
575 // Adopted Gnu automake/autoconf system.
577 // Revision 1.5 1998/03/03 16:00:52 curt
578 // More c++ compile tweaks.
580 // Revision 1.4 1998/01/31 00:41:23 curt
581 // Made a few changes converting floats to doubles.
583 // Revision 1.3 1998/01/27 18:37:00 curt
584 // Lots of updates to get back in sync with changes made over in .../Src/
586 // Revision 1.2 1998/01/15 21:33:36 curt
587 // Assembling triangles and building a new .node file with the proper shared
588 // vertices now works. Now we just have to use the shared normals and we'll
591 // Revision 1.1 1998/01/15 02:45:26 curt