]> git.mxchange.org Git - flightgear.git/blob - AssemTris/assemtris.cxx
Initial revision.
[flightgear.git] / AssemTris / assemtris.cxx
1 // assemtris.cxx -- reassemble the pieces produced by splittris
2 //
3 // Written by Curtis Olson, started January 1998.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@me.umn.edu
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22 // (Log is kept at end of this file)
23
24
25 #include <math.h>
26 #include <stdio.h>
27 #include <stdlib.h>   // for atoi()
28 #include <string.h>
29 #include <sys/stat.h> // for stat()
30 #include <unistd.h>   // for stat()
31
32 #include "assemtris.hxx"
33
34 #include <Include/fg_constants.h>
35 #include <Bucket/bucketutils.h>
36
37
38 // #define OFFSET_LON 0.1
39 // #define OFFSET_LAT 0.1
40
41 #define OFFSET_LON 0.0
42 #define OFFSET_LAT 0.0
43
44 int nodecount = 0;
45 int excount = 0;
46
47 static double nodes[MAX_NODES][3];
48 static double exnodes[MAX_NODES][3];
49
50
51 fgBUCKET my_index;
52 fgBUCKET ne_index, nw_index, sw_index, se_index;
53 fgBUCKET north_index, south_index, east_index, west_index;
54
55
56 // return the file base name ( foo/bar/file.ext = file.ext )
57 void extract_file(char *in, char *base) {
58     int len, i;
59
60     len = strlen(in);
61
62     i = len - 1;
63     while ( (i >= 0) && (in[i] != '/') ) {
64         i--;
65     }
66
67     in += (i + 1);
68     strcpy(base, in);
69 }
70
71
72 // return the file path name ( foo/bar/file.ext = foo/bar )
73 void extract_path(char *in, char *base) {
74     int len, i;
75
76     len = strlen(in);
77     strcpy(base, in);
78
79     i = len - 1;
80     while ( (i >= 0) && (in[i] != '/') ) {
81         i--;
82     }
83
84     base[i] = '\0';
85 }
86
87
88 // check to see if specified node is in the extra list
89 int is_extra_node(double *n) {
90     int i;
91
92     for ( i = 1; i <= excount; i++ ) {
93         // we only check lon/lat in case the height got fooled with
94         // along the way
95         if ( (fabs(n[0] - exnodes[i][0]) < FG_EPSILON) &&
96              (fabs(n[1] - exnodes[i][1]) < FG_EPSILON) ) {
97             return(i);
98         }
99     }
100
101     return(0);
102 }
103
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) {
109     FILE *fd;
110     int i, junk1, junk2, junk3;
111
112     // load extra nodes if they exist
113     excount = 0;
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);
117
118         if ( excount > MAX_NODES - 1 ) {
119             printf("Error, too many 'extra' nodes, increase array size\n");
120             exit(-1);
121         } else {
122             printf("    Expecting %d 'extra' nodes\n", excount);
123         }
124
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]);
130         }
131         fclose(fd);
132     }
133 }
134
135
136 // check if a file exists
137 int file_exists(char *file) {
138     struct stat stat_buf;
139     int result;
140
141     printf("checking %s ... ", file);
142
143     result = stat(file, &stat_buf);
144
145     if ( result != 0 ) {
146         // stat failed, no file
147         printf("not found.\n");
148         return(0);
149     } else {
150         // stat succeeded, file exists
151         printf("exists.\n");
152         return(1);
153     }
154 }
155
156
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];
160     long int index;
161
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) ) {
167             return(1);
168         }
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) ) {
173             return(1);
174         }
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) ) {
179             return(1);
180         }
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) ) {
185             return(1);
186         }
187     }
188
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) ) {
194             return(1);
195         }
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) ) {
200             return(1);
201         }
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) ) {
206             return(1);
207         }
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) ) {
212             return(1);
213         }
214     }
215
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) ) {
221             return(1);
222         }
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) ) {
227             return(1);
228         }
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) ) {
233             return(1);
234         }
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) ) {
239             return(1);
240         }
241     }
242
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) ) {
248             return(1);
249         }
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) ) {
254             return(1);
255         }
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) ) {
260             return(1);
261         }
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) ) {
266             return(1);
267         }
268     }
269
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) ) {
275             return(1);
276         }
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) ) {
281             return(1);
282         }
283     }
284
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) ) {
290             return(1);
291         }
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) ) {
296             return(1);
297         }
298     }
299
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) ) {
305             return(1);
306         }
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) ) {
311             return(1);
312         }
313     }
314
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) ) {
320             return(1);
321         }
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) ) {
326             return(1);
327         }
328     }
329
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) ) {
335             return(1);
336         }
337     }
338
339     return(0);
340 }
341
342
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) {
346     FILE *fp;
347     char filename[256];
348
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);
355         return(fp);
356     } else {
357         // open the file
358         printf("not opening\n");
359         return(NULL);
360     }
361 }
362
363
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 ==
368 // about 1 foot
369 void read_nodes(FILE *fp, double offset_lon, double offset_lat) {
370     double n[3];
371     char line[256];
372     int ex_index;
373
374     offset_lon = offset_lat = 0.0;
375
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]);
379
380             ex_index = is_extra_node(n);
381
382             if ( ex_index == 0 ) {
383                 // not an extra node
384                 nodes[nodecount][0] = n[0] + offset_lon;
385                 nodes[nodecount][1] = n[1] + offset_lat;
386                 nodes[nodecount][2] = n[2];
387
388                 // printf("read_nodes(%d) %.2f %.2f %.2f %s", nodecount, 
389                 //        nodes[nodecount][0], nodes[nodecount][1], 
390                 //        nodes[nodecount][2], line);
391                      
392
393                 nodecount++;
394             } else {
395                 // is an extra node
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];
399             }
400         }
401     }
402 }
403
404
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) {
408     char exfile[256];
409     FILE *ne, *nw, *se, *sw, *north, *south, *east, *west, *body;
410
411     // load extra nodes if they exist
412     strcpy(exfile, basename);
413     strcat(exfile, ".node.ex");
414     read_extra_nodes(exfile);
415
416     ne = my_open(basename, basepath, ".ne");
417     read_nodes(ne, OFFSET_LON, OFFSET_LAT);
418     fclose(ne);
419
420     nw = my_open(basename, basepath, ".nw");
421     read_nodes(nw, -1.0 * OFFSET_LON, OFFSET_LAT);
422     fclose(nw);
423
424     se = my_open(basename, basepath, ".se");
425     read_nodes(se, OFFSET_LON, -1.0 * OFFSET_LAT);
426     fclose(se);
427
428     sw = my_open(basename, basepath, ".sw");
429     read_nodes(sw, -1.0 * OFFSET_LON, -1.0 * OFFSET_LAT);
430     fclose(sw);
431
432     north = my_open(basename, basepath, ".north");
433     read_nodes(north, 0.0, OFFSET_LAT);
434     fclose(north);
435
436     south = my_open(basename, basepath, ".south");
437     read_nodes(south, 0.0, -1.0 * OFFSET_LAT);
438     fclose(south);
439
440     east = my_open(basename, basepath, ".east");
441     read_nodes(east, OFFSET_LON, 0.0);
442     fclose(east);
443
444     west = my_open(basename, basepath, ".west");
445     read_nodes(west, -1.0 * OFFSET_LON, 0.0);
446     fclose(west);
447
448     body = my_open(basename, basepath, ".body");
449     read_nodes(body, 0.0, 0.0);
450     fclose(body);
451 }
452
453
454 // dump in WaveFront .obj format
455 void dump_nodes(char *basename) {
456     char file[256];
457     FILE *fd;
458     int i;
459
460     // generate output file name
461     strcpy(file, basename);
462     // len = strlen(file);
463     // file[len-2] = '\0';
464     strcat(file, ".node");
465     
466     // dump vertices
467     printf("Creating node file:  %s\n", file);
468     printf("  writing vertices in .node format.\n");
469     fd = fopen(file, "w");
470
471     fprintf(fd, "%d 2 1 0\n", excount + nodecount);
472
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]);
477     }
478
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]);
483     }
484
485     fclose(fd);
486 }
487
488
489 int main(int argc, char **argv) {
490     char basename[256], basepath[256], temp[256];
491     long int tmp_index;
492     int len;
493
494     // derive base name
495     strcpy(basename, argv[1]);
496     len = strlen(basename);
497
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);
503
504     // find the index of the current file
505     extract_file(basename, temp);
506     // len = strlen(temp);
507     // if ( len >= 2 ) {
508     //    temp[len-2] = '\0';
509     // }
510     tmp_index = atoi(temp);
511     printf("%ld\n", tmp_index);
512     fgBucketParseIndex(tmp_index, &my_index);
513
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);
521
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);
526
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);
531           
532
533     // load the input data files
534     build_node_list(basename, basepath);
535
536     // dump in WaveFront .obj format
537     dump_nodes(basename);
538
539     return(0);
540 }
541
542
543 // $Log$
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.
547 //
548 // Revision 1.2  1998/09/25 19:38:01  curt
549 // Minor tweaks so that this actually compiles.
550 //
551 // Revision 1.1  1998/09/25 19:35:29  curt
552 // Renamed assemtris.[ch] to assemtris.[ch]xx
553 //
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.
557 //
558 //
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.
563 //
564 // Revision 1.11  1998/08/06 12:47:59  curt
565 // Removed overlap in tiles as a test.
566 //
567 // Revision 1.10  1998/07/21 04:34:20  curt
568 // Mods to handle extra nodes (i.e. preserve cutouts).
569 //
570 // Revision 1.9  1998/07/04 00:55:39  curt
571 // typedef'd struct fgBUCKET.
572 //
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.
576 //
577 // Revision 1.7  1998/04/14 02:26:00  curt
578 // Code reorganizations.  Added a Lib/ directory for more general libraries.
579 //
580 // Revision 1.6  1998/04/08 22:54:58  curt
581 // Adopted Gnu automake/autoconf system.
582 //
583 // Revision 1.5  1998/03/03 16:00:52  curt
584 // More c++ compile tweaks.
585 //
586 // Revision 1.4  1998/01/31 00:41:23  curt
587 // Made a few changes converting floats to doubles.
588 //
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/
591 //
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
595 // be all set.
596 //
597 // Revision 1.1  1998/01/15 02:45:26  curt
598 // Initial revision.
599 //
600