]> git.mxchange.org Git - flightgear.git/blob - AssemTris/assemtris.c
523d88e4b7f80815c172aa518844aad2441d20c2
[flightgear.git] / AssemTris / assemtris.c
1 /* splittris.c -- reassemble the pieces produced by splittris
2  *
3  * Written by Curtis Olson, started January 1998.
4  *
5  * Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
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.h"
33
34 #include "../../Src/Include/constants.h"
35 #include "../../Src/Include/types.h"
36 #include "../../Src/Math/fg_geodesy.h"
37 #include "../../Src/Math/mat3.h"
38 #include "../../Src/Math/polar.h"
39 #include "../../Src/Scenery/tileutils.h"
40
41
42 int nodecount = 0;
43
44 float nodes[MAX_NODES][3];
45
46
47 struct bucket my_index;
48 struct bucket ne_index, nw_index, sw_index, se_index;
49 struct bucket north_index, south_index, east_index, west_index;
50
51
52 /* return the file base name ( foo/bar/file.ext = file.ext ) */
53 void extract_file(char *in, char *base) {
54     int len, i;
55
56     len = strlen(in);
57
58     i = len - 1;
59     while ( (i >= 0) && (in[i] != '/') ) {
60         i--;
61     }
62
63     in += (i + 1);
64     strcpy(base, in);
65 }
66
67
68 /* return the file path name ( foo/bar/file.ext = foo/bar ) */
69 void extract_path(char *in, char *base) {
70     int len, i;
71
72     len = strlen(in);
73     strcpy(base, in);
74
75     i = len - 1;
76     while ( (i >= 0) && (in[i] != '/') ) {
77         i--;
78     }
79
80     base[i] = '\0';
81 }
82
83
84 /* check if a file exists */
85 int file_exists(char *file) {
86     struct stat stat_buf;
87     int result;
88
89     printf("checking %s ... ", file);
90
91     result = stat(file, &stat_buf);
92
93     if ( result != 0 ) {
94         /* stat failed, no file */
95         printf("not found.\n");
96         return(0);
97     } else {
98         /* stat succeeded, file exists */
99         printf("exists.\n");
100         return(1);
101     }
102 }
103
104
105 /* check to see if a shared object exists */
106 int shared_object_exists(char *basepath, char *ext, char *file) {
107     char scene_path[256];
108     long int index;
109
110     if ( strcmp(ext, ".sw") == 0 ) {
111         gen_base_path(&my_index, scene_path);
112         index = gen_index(&my_index);
113         sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
114         if ( file_exists(file) ) {
115             return(1);
116         }
117         gen_base_path(&west_index, scene_path);
118         index = gen_index(&west_index);
119         sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
120         if ( file_exists(file) ) {
121             return(1);
122         }
123         gen_base_path(&sw_index, scene_path);
124         index = gen_index(&sw_index);
125         sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
126         if ( file_exists(file) ) {
127             return(1);
128         }
129         gen_base_path(&south_index, scene_path);
130         index = gen_index(&south_index);
131         sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
132         if ( file_exists(file) ) {
133             return(1);
134         }
135     }
136
137     if ( strcmp(ext, ".se") == 0 ) {
138         gen_base_path(&my_index, scene_path);
139         index = gen_index(&my_index);
140         sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
141         if ( file_exists(file) ) {
142             return(1);
143         }
144         gen_base_path(&east_index, scene_path);
145         index = gen_index(&east_index);
146         sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
147         if ( file_exists(file) ) {
148             return(1);
149         }
150         gen_base_path(&se_index, scene_path);
151         index = gen_index(&se_index);
152         sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
153         if ( file_exists(file) ) {
154             return(1);
155         }
156         gen_base_path(&south_index, scene_path);
157         index = gen_index(&south_index);
158         sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
159         if ( file_exists(file) ) {
160             return(1);
161         }
162     }
163
164     if ( strcmp(ext, ".ne") == 0 ) {
165         gen_base_path(&my_index, scene_path);
166         index = gen_index(&my_index);
167         sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
168         if ( file_exists(file) ) {
169             return(1);
170         }
171         gen_base_path(&east_index, scene_path);
172         index = gen_index(&east_index);
173         sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
174         if ( file_exists(file) ) {
175             return(1);
176         }
177         gen_base_path(&ne_index, scene_path);
178         index = gen_index(&ne_index);
179         sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
180         if ( file_exists(file) ) {
181             return(1);
182         }
183         gen_base_path(&north_index, scene_path);
184         index = gen_index(&north_index);
185         sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
186         if ( file_exists(file) ) {
187             return(1);
188         }
189     }
190
191     if ( strcmp(ext, ".nw") == 0 ) {
192         gen_base_path(&my_index, scene_path);
193         index = gen_index(&my_index);
194         sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
195         if ( file_exists(file) ) {
196             return(1);
197         }
198         gen_base_path(&west_index, scene_path);
199         index = gen_index(&west_index);
200         sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
201         if ( file_exists(file) ) {
202             return(1);
203         }
204         gen_base_path(&nw_index, scene_path);
205         index = gen_index(&nw_index);
206         sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
207         if ( file_exists(file) ) {
208             return(1);
209         }
210         gen_base_path(&north_index, scene_path);
211         index = gen_index(&north_index);
212         sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
213         if ( file_exists(file) ) {
214             return(1);
215         }
216     }
217
218     if ( strcmp(ext, ".south") == 0 ) {
219         gen_base_path(&my_index, scene_path);
220         index = gen_index(&my_index);
221         sprintf(file, "%s/%s/%ld.1.south", basepath, scene_path, index);
222         if ( file_exists(file) ) {
223             return(1);
224         }
225         gen_base_path(&south_index, scene_path);
226         index = gen_index(&south_index);
227         sprintf(file, "%s/%s/%ld.1.north", basepath, scene_path, index);
228         if ( file_exists(file) ) {
229             return(1);
230         }
231     }
232
233     if ( strcmp(ext, ".north") == 0 ) {
234         gen_base_path(&my_index, scene_path);
235         index = gen_index(&my_index);
236         sprintf(file, "%s/%s/%ld.1.north", basepath, scene_path, index);
237         if ( file_exists(file) ) {
238             return(1);
239         }
240         gen_base_path(&north_index, scene_path);
241         index = gen_index(&north_index);
242         sprintf(file, "%s/%s/%ld.1.south", basepath, scene_path, index);
243         if ( file_exists(file) ) {
244             return(1);
245         }
246     }
247
248     if ( strcmp(ext, ".west") == 0 ) {
249         gen_base_path(&my_index, scene_path);
250         index = gen_index(&my_index);
251         sprintf(file, "%s/%s/%ld.1.west", basepath, scene_path, index);
252         if ( file_exists(file) ) {
253             return(1);
254         }
255         gen_base_path(&west_index, scene_path);
256         index = gen_index(&west_index);
257         sprintf(file, "%s/%s/%ld.1.east", basepath, scene_path, index);
258         if ( file_exists(file) ) {
259             return(1);
260         }
261     }
262
263     if ( strcmp(ext, ".east") == 0 ) {
264         gen_base_path(&my_index, scene_path);
265         index = gen_index(&my_index);
266         sprintf(file, "%s/%s/%ld.1.east", basepath, scene_path, index);
267         if ( file_exists(file) ) {
268             return(1);
269         }
270         gen_base_path(&east_index, scene_path);
271         index = gen_index(&east_index);
272         sprintf(file, "%s/%s/%ld.1.west", basepath, scene_path, index);
273         if ( file_exists(file) ) {
274             return(1);
275         }
276     }
277
278     if ( strcmp(ext, ".body") == 0 ) {
279         gen_base_path(&my_index, scene_path);
280         index = gen_index(&my_index);
281         sprintf(file, "%s/%s/%ld.1.body", basepath, scene_path, index);
282         if ( file_exists(file) ) {
283             return(1);
284         }
285     }
286
287     return(0);
288 }
289
290
291 /* my custom file opening routine ... don't open if a shared edge or
292  * vertex alread exists */
293 FILE *my_open(char *basename, char *basepath, char *ext) {
294     FILE *fp;
295     char filename[256];
296
297     /* check if a shared object already exists */
298     if ( shared_object_exists(basepath, ext, filename) ) {
299         /* not an actual file open error, but we've already got the
300          * shared edge, so we don't want to create another one */
301         fp = fopen(filename, "r");
302         printf("Opening %s\n", filename);
303         return(fp);
304     } else {
305         /* open the file */
306         printf("not opening\n");
307         return(NULL);
308     }
309 }
310
311
312 /* given a file pointer, read all the gdn (geodetic nodes from it) */
313 void read_nodes(FILE *fp) {
314     char line[256];
315
316     while ( fgets(line, 250, fp) != NULL ) {
317         if ( strncmp(line, "gdn ", 4) == 0 ) {
318             sscanf(line, "gdn %f %f %f\n", &nodes[nodecount][0], 
319                    &nodes[nodecount][1], &nodes[nodecount][2]);
320             /*
321             printf("read_nodes(%d) %.2f %.2f %.2f %s", nodecount, 
322                    nodes[nodecount][0], nodes[nodecount][1], 
323                    nodes[nodecount][2], line);
324                    */
325             nodecount++;
326         }
327     }
328 }
329
330
331 /* load in nodes from the various split and shared pieces to
332  * reconstruct a tile */
333 void build_node_list(char *basename, char *basepath) {
334     FILE *ne, *nw, *se, *sw, *north, *south, *east, *west, *body;
335
336     ne = my_open(basename, basepath, ".ne");
337     read_nodes(ne);
338     fclose(ne);
339
340     nw = my_open(basename, basepath, ".nw");
341     read_nodes(nw);
342     fclose(nw);
343
344     se = my_open(basename, basepath, ".se");
345     read_nodes(se);
346     fclose(se);
347
348     sw = my_open(basename, basepath, ".sw");
349     read_nodes(sw);
350     fclose(sw);
351
352     north = my_open(basename, basepath, ".north");
353     read_nodes(north);
354     fclose(north);
355
356     south = my_open(basename, basepath, ".south");
357     read_nodes(south);
358     fclose(south);
359
360     east = my_open(basename, basepath, ".east");
361     read_nodes(east);
362     fclose(east);
363
364     west = my_open(basename, basepath, ".west");
365     read_nodes(west);
366     fclose(west);
367
368     body = my_open(basename, basepath, ".body");
369     read_nodes(body);
370     fclose(body);
371 }
372
373
374 /* dump in WaveFront .obj format */
375 void dump_nodes(char *basename) {
376     char file[256];
377     FILE *fp;
378     int i, len;
379
380     /* generate output file name */
381     strcpy(file, basename);
382     len = strlen(file);
383     file[len-2] = '\0';
384     strcat(file, ".node");
385     
386     /* dump vertices */
387     printf("Creating node file:  %s\n", file);
388     printf("  writing vertices in .node format.\n");
389     fp = fopen(file, "w");
390
391     fprintf(fp, "%d 2 1 0\n", nodecount);
392
393     /* now write out actual node data */
394     for ( i = 0; i < nodecount; i++ ) {
395         fprintf(fp, "%d %.2f %.2f %.2f 0\n", i + 1,
396                nodes[i][0], nodes[i][1], nodes[i][2]);
397     }
398
399     fclose(fp);
400 }
401
402
403 int main(int argc, char **argv) {
404     char basename[256], basepath[256], temp[256];
405     long int tmp_index;
406     int len;
407
408     strcpy(basename, argv[1]);
409
410     /* find the base path of the file */
411     extract_path(basename, basepath);
412     extract_path(basepath, basepath);
413     extract_path(basepath, basepath);
414     printf("%s\n", basepath);
415
416     /* find the index of the current file */
417     extract_file(basename, temp);
418     len = strlen(temp);
419     if ( len >= 2 ) {
420         temp[len-2] = '\0';
421     }
422     tmp_index = atoi(temp);
423     printf("%ld\n", tmp_index);
424     parse_index(tmp_index, &my_index);
425
426     printf("bucket = %d %d %d %d\n", 
427            my_index.lon, my_index.lat, my_index.x, my_index.y);
428     /* generate the indexes of the neighbors */
429     offset_bucket(&my_index, &ne_index,  1,  1);
430     offset_bucket(&my_index, &nw_index, -1,  1);
431     offset_bucket(&my_index, &se_index,  1, -1);
432     offset_bucket(&my_index, &sw_index, -1, -1);
433
434     offset_bucket(&my_index, &north_index,  0,  1);
435     offset_bucket(&my_index, &south_index,  0, -1);
436     offset_bucket(&my_index, &east_index,  1,  0);
437     offset_bucket(&my_index, &west_index, -1,  0);
438
439     /*
440     printf("Corner indexes = %ld %ld %ld %ld\n", 
441            ne_index, nw_index, sw_index, se_index);
442     printf("Edge indexes = %ld %ld %ld %ld\n",
443            north_index, south_index, east_index, west_index);
444            */
445
446     /* load the input data files */
447     build_node_list(basename, basepath);
448
449     /* dump in WaveFront .obj format */
450     dump_nodes(basename);
451
452     return(0);
453 }
454
455
456 /* $Log$
457 /* Revision 1.2  1998/01/15 21:33:36  curt
458 /* Assembling triangles and building a new .node file with the proper shared
459 /* vertices now works.  Now we just have to use the shared normals and we'll
460 /* be all set.
461 /*
462  * Revision 1.1  1998/01/15 02:45:26  curt
463  * Initial revision.
464  *
465  */