]> git.mxchange.org Git - flightgear.git/blob - SplitTris/splittris.c
0ef6f9b9dad612b076e453599d56c3ef19abdb80
[flightgear.git] / SplitTris / splittris.c
1 /* splittris.c -- read in a .ele/.node file pair generated by the
2  *                triangle program and output a simple Wavefront .obj
3  *                file for the north, south, east, and west edge
4  *                verticies ... including the normals.
5  *
6  * Written by Curtis Olson, started January 1998.
7  *
8  * Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  * $Id$
25  * (Log is kept at end of this file) */
26
27
28 #include <math.h>
29 #include <stdio.h>
30 #include <stdlib.h>   /* for atoi() */
31 #include <string.h>
32 #include <sys/stat.h> /* for stat() */
33 #include <unistd.h>   /* for stat() */
34
35 #include "splittris.h"
36
37 #include "../../Src/Include/constants.h"
38 #include "../../Src/Include/types.h"
39 #include "../../Src/Math/fg_geodesy.h"
40 #include "../../Src/Math/mat3.h"
41 #include "../../Src/Math/polar.h"
42 #include "../../Src/Scenery/tileutils.h"
43
44
45 int nodecount, tricount;
46 double xmin, xmax, ymin, ymax;
47
48 double nodes_orig[MAX_NODES][3];
49 int tris[MAX_TRIS][3];
50 int new_tris[MAX_TRIS][3];
51
52 struct fgCartesianPoint nodes_cart[MAX_NODES];
53
54 struct bucket ne_index, nw_index, sw_index, se_index;
55 struct bucket north_index, south_index, east_index, west_index;
56
57 /* convert a geodetic point lon(arcsec), lat(arcsec), elev(meter) to
58  * a cartesian point */
59 struct fgCartesianPoint geod_to_cart(double geod[3]) {
60     struct fgCartesianPoint p;
61     double gc_lon, gc_lat, sl_radius;
62
63     /* printf("A geodetic point is (%.2f, %.2f, %.2f)\n", 
64            geod[0], geod[1], geod[2]); */
65
66     gc_lon = geod[0]*ARCSEC_TO_RAD;
67     fgGeodToGeoc(geod[1]*ARCSEC_TO_RAD, geod[2], &sl_radius, &gc_lat);
68
69     /* printf("A geocentric point is (%.2f, %.2f, %.2f)\n", gc_lon, 
70            gc_lat, sl_radius+geod[2]); */
71
72     p = fgPolarToCart(gc_lon, gc_lat, sl_radius+geod[2]);
73     
74     /* printf("A cart point is (%.8f, %.8f, %.8f)\n", p.x, p.y, p.z); */
75
76     return(p);
77 }
78
79
80 /* given three points defining a triangle, calculate the normal */
81 void calc_normal(struct fgCartesianPoint p1, struct fgCartesianPoint p2, 
82                  struct fgCartesianPoint p3, double normal[3])
83 {
84     double v1[3], v2[3];
85     float temp;
86
87     v1[0] = p2.x - p1.x; v1[1] = p2.y - p1.y; v1[2] = p2.z - p1.z;
88     v2[0] = p3.x - p1.x; v2[1] = p3.y - p1.y; v2[2] = p3.z - p1.z;
89
90     MAT3cross_product(normal, v1, v2);
91     MAT3_NORMALIZE_VEC(normal,temp);
92
93 /*  printf("  Normal = %.2f %.2f %.2f\n", normal[0], normal[1], normal[2]); */
94 }
95
96
97 /* return the file base name ( foo/bar/file.ext = file.ext ) */
98 void extract_file(char *in, char *base) {
99     int len, i;
100
101     len = strlen(in);
102
103     i = len - 1;
104     while ( (i >= 0) && (in[i] != '/') ) {
105         i--;
106     }
107
108     in += (i + 1);
109     strcpy(base, in);
110 }
111
112
113 /* return the file path name ( foo/bar/file.ext = foo/bar ) */
114 void extract_path(char *in, char *base) {
115     int len, i;
116
117     len = strlen(in);
118     strcpy(base, in);
119
120     i = len - 1;
121     while ( (i >= 0) && (in[i] != '/') ) {
122         i--;
123     }
124
125     base[i] = '\0';
126 }
127
128
129 /* return the index of all triangles containing the specified node */
130 void find_tris(int n, int *t1, int *t2, int *t3, int *t4, int *t5) {
131     int i;
132
133     *t1 = *t2 = *t3 = *t4 = *t5 = 0;
134
135     i = 1;
136     while ( i <= tricount ) {
137         if ( (n == tris[i][0]) || (n == tris[i][1]) || (n == tris[i][2]) ) {
138             if ( *t1 == 0 ) {
139                 *t1 = i;
140             } else if ( *t2 == 0 ) {
141                 *t2 = i;
142             } else if ( *t3 == 0 ) {
143                 *t3 = i;
144             } else if ( *t4 == 0 ) {
145                 *t4 = i;
146             } else {
147                 *t5 = i;
148             }
149         }
150         i++;
151     }
152 }
153
154
155 /* Initialize a new mesh structure */
156 void triload(char *basename) {
157     char nodename[256], elename[256];
158     FILE *node, *ele;
159     int dim, junk1, junk2;
160     int i;
161
162     strcpy(nodename, basename);
163     strcat(nodename, ".node");
164     strcpy(elename, basename);
165     strcat(elename, ".ele");
166
167     printf("Loading node file:  %s ...\n", nodename);
168     if ( (node = fopen(nodename, "r")) == NULL ) {
169         printf("Cannot open file '%s'\n", nodename);
170         exit(-1);
171     }
172
173     fscanf(node, "%d %d %d %d", &nodecount, &dim, &junk1, &junk2);
174
175     if ( nodecount > MAX_NODES - 1 ) {
176         printf("Error, too many nodes, need to increase array size\n");
177         exit(-1);
178     } else {
179         printf("    Expecting %d nodes\n", nodecount);
180     }
181
182     for ( i = 1; i <= nodecount; i++ ) {
183         fscanf(node, "%d %lf %lf %lf %d\n", &junk1, 
184                &nodes_orig[i][0], &nodes_orig[i][1], &nodes_orig[i][2], &junk2);
185         /* printf("%d %.2f %.2f %.2f\n", junk1, n[0], n[1], n[2]); */
186         nodes_cart[i] = geod_to_cart(nodes_orig[i]);
187         /* printf("%d %.2f %.2f %.2f\n", 
188                junk1, nodes_cart[i].x, nodes_cart[i].y, nodes_cart[i].z); */
189
190         if ( i == 1 ) {
191             xmin = xmax = nodes_orig[i][0];
192             ymin = ymax = nodes_orig[i][1];
193         } else {
194             if ( nodes_orig[i][0] < xmin ) {
195                 xmin = nodes_orig[i][0];
196             }
197             if ( nodes_orig[i][0] > xmax ) {
198                 xmax = nodes_orig[i][0];
199             }
200             if ( nodes_orig[i][1] < ymin ) {
201                 ymin = nodes_orig[i][1];
202             }
203             if ( nodes_orig[i][1] > ymax ) {
204                 ymax = nodes_orig[i][1];
205             }
206         }
207     }
208
209     fclose(node);
210
211     printf("Loading element file:  %s ...\n", elename);
212     if ( (ele = fopen(elename, "r")) == NULL ) {
213         printf("Cannot open file '%s'\n", elename);
214         exit(-1);
215     }
216
217     fscanf(ele, "%d %d %d", &tricount, &junk1, &junk2);
218
219     if ( tricount > MAX_TRIS - 1 ) {
220         printf("Error, too many elements, need to increase array size\n");
221         exit(-1);
222     } else {
223         printf("    Expecting %d elements\n", tricount);
224     }
225
226     for ( i = 1; i <= tricount; i++ ) {
227         fscanf(ele, "%d %d %d %d\n", &junk1, 
228                &tris[i][0], &tris[i][1], &tris[i][2]);
229         /* printf("%d %d %d %d\n", junk1, tris[i][0], tris[i][1], tris[i][2]);*/
230     }
231
232     fclose(ele);
233 }
234
235
236 /* check if a file exists */
237 int file_exists(char *file) {
238     struct stat stat_buf;
239     int result;
240
241     printf("checking %s ... ", file);
242
243     result = stat(file, &stat_buf);
244
245     if ( result != 0 ) {
246         /* stat failed, no file */
247         printf("not found.\n");
248         return(0);
249     } else {
250         /* stat succeeded, file exists */
251         printf("exists.\n");
252         return(1);
253     }
254 }
255
256
257 /* check to see if a shared object exists */
258 int shared_object_exists(char *basepath, char *ext) {
259     char file[256], scene_path[256];
260     long int index;
261
262     if ( strcmp(ext, ".sw") == 0 ) {
263         gen_base_path(&west_index, scene_path);
264         index = gen_index(&west_index);
265         sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
266         if ( file_exists(file) ) {
267             return(1);
268         }
269         gen_base_path(&sw_index, scene_path);
270         index = gen_index(&sw_index);
271         sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
272         if ( file_exists(file) ) {
273             return(1);
274         }
275         gen_base_path(&south_index, scene_path);
276         index = gen_index(&south_index);
277         sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
278         if ( file_exists(file) ) {
279             return(1);
280         }
281     }
282
283     if ( strcmp(ext, ".se") == 0 ) {
284         gen_base_path(&east_index, scene_path);
285         index = gen_index(&east_index);
286         sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
287         if ( file_exists(file) ) {
288             return(1);
289         }
290         gen_base_path(&se_index, scene_path);
291         index = gen_index(&se_index);
292         sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
293         if ( file_exists(file) ) {
294             return(1);
295         }
296         gen_base_path(&south_index, scene_path);
297         index = gen_index(&south_index);
298         sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
299         if ( file_exists(file) ) {
300             return(1);
301         }
302     }
303
304     if ( strcmp(ext, ".ne") == 0 ) {
305         gen_base_path(&east_index, scene_path);
306         index = gen_index(&east_index);
307         sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
308         if ( file_exists(file) ) {
309             return(1);
310         }
311         gen_base_path(&ne_index, scene_path);
312         index = gen_index(&ne_index);
313         sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
314         if ( file_exists(file) ) {
315             return(1);
316         }
317         gen_base_path(&north_index, scene_path);
318         index = gen_index(&north_index);
319         sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
320         if ( file_exists(file) ) {
321             return(1);
322         }
323     }
324
325     if ( strcmp(ext, ".nw") == 0 ) {
326         gen_base_path(&west_index, scene_path);
327         index = gen_index(&west_index);
328         sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
329         if ( file_exists(file) ) {
330             return(1);
331         }
332         gen_base_path(&nw_index, scene_path);
333         index = gen_index(&nw_index);
334         sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
335         if ( file_exists(file) ) {
336             return(1);
337         }
338         gen_base_path(&north_index, scene_path);
339         index = gen_index(&north_index);
340         sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
341         if ( file_exists(file) ) {
342             return(1);
343         }
344     }
345
346     if ( strcmp(ext, ".south") == 0 ) {
347         gen_base_path(&south_index, scene_path);
348         index = gen_index(&south_index);
349         sprintf(file, "%s/%s/%ld.1.north", basepath, scene_path, index);
350         if ( file_exists(file) ) {
351             return(1);
352         }
353     }
354
355     if ( strcmp(ext, ".north") == 0 ) {
356         gen_base_path(&north_index, scene_path);
357         index = gen_index(&north_index);
358         sprintf(file, "%s/%s/%ld.1.south", basepath, scene_path, index);
359         if ( file_exists(file) ) {
360             return(1);
361         }
362     }
363
364     if ( strcmp(ext, ".west") == 0 ) {
365         gen_base_path(&west_index, scene_path);
366         index = gen_index(&west_index);
367         sprintf(file, "%s/%s/%ld.1.east", basepath, scene_path, index);
368         if ( file_exists(file) ) {
369             return(1);
370         }
371     }
372
373     if ( strcmp(ext, ".east") == 0 ) {
374         gen_base_path(&east_index, scene_path);
375         index = gen_index(&east_index);
376         sprintf(file, "%s/%s/%ld.1.west", basepath, scene_path, index);
377         if ( file_exists(file) ) {
378             return(1);
379         }
380     }
381
382     return(0);
383 }
384
385
386 /* my custom file opening routine ... don't open if a shared edge or
387  * vertex alread exists */
388 FILE *my_open(char *basename, char *basepath, char *ext) {
389     FILE *fp;
390     char filename[256];
391
392     /* create the output file name */
393     strcpy(filename, basename);
394     strcat(filename, ext);
395
396     /* check if a shared object already exist from a different tile */
397
398     if ( shared_object_exists(basepath, ext) ) {
399         /* not an actual file open error, but we've already got the
400          * shared edge, so we don't want to create another one */
401         printf("not opening\n");
402         return(NULL);
403     } else {
404         /* open the file */
405         fp = fopen(filename, "w");
406         printf("Opening %s\n", filename);
407         return(fp);
408     }
409 }
410
411
412 /* dump in WaveFront .obj format */
413 void dump_obj(char *basename, char *basepath) {
414     double n1[3], n2[3], n3[3], n4[3], n5[3], norm[3], temp;
415     FILE *fp, *sw, *se, *ne, *nw, *north, *south, *east, *west, *body;
416     int i, t1, t2, t3, t4, t5, count;
417
418     sw = my_open(basename, basepath, ".sw");
419     se = my_open(basename, basepath, ".se");
420     ne = my_open(basename, basepath, ".ne");
421     nw = my_open(basename, basepath, ".nw");
422
423     north = my_open(basename, basepath, ".north");
424     south = my_open(basename, basepath, ".south");
425     east = my_open(basename, basepath, ".east");
426     west = my_open(basename, basepath, ".west");
427
428     body = my_open(basename, basepath, ".body");
429
430     printf("Dumping edges file basename:  %s ...\n", basename);
431
432     /* dump vertices */
433     printf("  writing vertices\n");
434     for ( i = 1; i <= nodecount; i++ ) {
435
436         if ( (fabs(nodes_orig[i][1] - ymin) < FG_EPSILON) && 
437              (fabs(nodes_orig[i][0] - xmin) < FG_EPSILON) ) {
438             fp = sw;
439         } else if ( (fabs(nodes_orig[i][1] - ymin) < FG_EPSILON) &&
440                     (fabs(nodes_orig[i][0] - xmax) < FG_EPSILON) ) {
441             fp = se;
442         } else if ( (fabs(nodes_orig[i][1] - ymax) < FG_EPSILON) &&
443                     (fabs(nodes_orig[i][0] - xmax) < FG_EPSILON)) {
444             fp = ne;
445         } else if ( (fabs(nodes_orig[i][1] - ymax) < FG_EPSILON) &&
446                     (fabs(nodes_orig[i][0] - xmin) < FG_EPSILON) ) {
447             fp = nw;
448         } else if ( fabs(nodes_orig[i][0] - xmin) < FG_EPSILON ) {
449             fp = west;
450         } else if ( fabs(nodes_orig[i][0] - xmax) < FG_EPSILON ) {
451             fp = east;
452         } else if ( fabs(nodes_orig[i][1] - ymin) < FG_EPSILON ) {
453             fp = south;
454         } else if ( fabs(nodes_orig[i][1] - ymax) < FG_EPSILON ) {
455             fp = north;
456         } else {
457             fp = body;
458         }
459         fprintf(fp, "gdn %.2f %.2f %.2f\n", 
460                 nodes_orig[i][0], nodes_orig[i][1], nodes_orig[i][2]);
461     }
462
463     printf("  calculating and writing normals\n");
464     /* calculate and generate normals */
465     for ( i = 1; i <= nodecount; i++ ) {
466 /*      printf("Finding normal\n"); */
467
468         find_tris(i, &t1, &t2, &t3, &t4, &t5);
469
470         n1[0] = n1[1] = n1[2] = 0.0;
471         n2[0] = n2[1] = n2[2] = 0.0;
472         n3[0] = n3[1] = n3[2] = 0.0;
473         n4[0] = n4[1] = n4[2] = 0.0;
474         n5[0] = n5[1] = n5[2] = 0.0;
475
476         count = 1;
477         calc_normal(nodes_cart[tris[t1][0]], nodes_cart[tris[t1][1]], 
478                     nodes_cart[tris[t1][2]], n1);
479
480         if ( t2 > 0 ) {
481             calc_normal(nodes_cart[tris[t2][0]], nodes_cart[tris[t2][1]], 
482                         nodes_cart[tris[t2][2]], n2);
483             count = 2;
484         }
485
486         if ( t3 > 0 ) {
487             calc_normal(nodes_cart[tris[t3][0]], nodes_cart[tris[t3][1]],
488                         nodes_cart[tris[t3][2]], n3);
489             count = 3;
490         }
491
492         if ( t4 > 0 ) {
493             calc_normal(nodes_cart[tris[t4][0]], nodes_cart[tris[t4][1]],
494                         nodes_cart[tris[t4][2]], n4);
495             count = 4;
496         }
497
498         if ( t5 > 0 ) {
499             calc_normal(nodes_cart[tris[t5][0]], nodes_cart[tris[t5][1]],
500                         nodes_cart[tris[t5][2]], n5);
501             count = 5;
502         }
503
504 /*      printf("  norm[2] = %.2f %.2f %.2f\n", n1[2], n2[2], n3[2]); */
505
506         norm[0] = ( n1[0] + n2[0] + n3[0] + n4[0] + n5[0] ) / (double)count;
507         norm[1] = ( n1[1] + n2[1] + n3[1] + n4[1] + n5[1] ) / (double)count;
508         norm[2] = ( n1[2] + n2[2] + n3[2] + n4[2] + n5[2] ) / (double)count;
509         
510 /*      printf("  count = %d\n", count); */
511 /*      printf("  Ave. normal = %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);*/
512         MAT3_NORMALIZE_VEC(norm, temp);
513 /*      printf("  Normalized ave. normal = %.4f %.4f %.4f\n",  */
514 /*             norm[0], norm[1], norm[2]); */
515         
516         fp = NULL;
517
518         if ( (fabs(nodes_orig[i][1] - ymin) < FG_EPSILON) && 
519              (fabs(nodes_orig[i][0] - xmin) < FG_EPSILON) ) {
520             fp = sw;
521         } else if ( (fabs(nodes_orig[i][1] - ymin) < FG_EPSILON) &&
522                     (fabs(nodes_orig[i][0] - xmax) < FG_EPSILON) ) {
523             fp = se;
524         } else if ( (fabs(nodes_orig[i][1] - ymax) < FG_EPSILON) &&
525                     (fabs(nodes_orig[i][0] - xmax) < FG_EPSILON)) {
526             fp = ne;
527         } else if ( (fabs(nodes_orig[i][1] - ymax) < FG_EPSILON) &&
528                     (fabs(nodes_orig[i][0] - xmin) < FG_EPSILON) ) {
529             fp = nw;
530         } else if ( fabs(nodes_orig[i][0] - xmin) < FG_EPSILON ) {
531             fp = west;
532         } else if ( fabs(nodes_orig[i][0] - xmax) < FG_EPSILON ) {
533             fp = east;
534         } else if ( fabs(nodes_orig[i][1] - ymin) < FG_EPSILON ) {
535             fp = south;
536         } else if ( fabs(nodes_orig[i][1] - ymax) < FG_EPSILON ) {
537             fp = north;
538         }
539         if ( fp != NULL ) {
540             fprintf(fp, "vn %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);
541         }
542     }
543
544     fclose(sw);
545     fclose(se);
546     fclose(ne);
547     fclose(nw);
548
549     fclose(north);
550     fclose(south);
551     fclose(east);
552     fclose(west);
553
554     fclose(body);
555 }
556
557
558 int main(int argc, char **argv) {
559     char basename[256], basepath[256], temp[256];
560     struct bucket p;
561     long int index;
562     int len;
563
564     strcpy(basename, argv[1]);
565
566     /* find the base path of the file */
567     extract_path(basename, basepath);
568     extract_path(basepath, basepath);
569     extract_path(basepath, basepath);
570     printf("%s\n", basepath);
571
572     /* find the index of the current file */
573     extract_file(basename, temp);
574     len = strlen(temp);
575     if ( len >= 2 ) {
576         temp[len-2] = '\0';
577     }
578     index = atoi(temp);
579     printf("%ld\n", index);
580     parse_index(index, &p);
581
582     printf("bucket = %d %d %d %d\n", p.lon, p.lat, p.x, p.y);
583     /* generate the indexes of the neighbors */
584     offset_bucket(&p, &ne_index,  1,  1);
585     offset_bucket(&p, &nw_index, -1,  1);
586     offset_bucket(&p, &se_index,  1, -1);
587     offset_bucket(&p, &sw_index, -1, -1);
588
589     offset_bucket(&p, &north_index,  0,  1);
590     offset_bucket(&p, &south_index,  0, -1);
591     offset_bucket(&p, &east_index,  1,  0);
592     offset_bucket(&p, &west_index, -1,  0);
593
594     /*
595     printf("Corner indexes = %ld %ld %ld %ld\n", 
596            ne_index, nw_index, sw_index, se_index);
597     printf("Edge indexes = %ld %ld %ld %ld\n",
598            north_index, south_index, east_index, west_index);
599            */
600
601     /* load the input data files */
602     triload(basename);
603
604     /* dump in WaveFront .obj format */
605     dump_obj(basename, basepath);
606
607     return(0);
608 }
609
610
611 /* $Log$
612 /* Revision 1.2  1998/01/14 15:54:43  curt
613 /* Initial revision completed.
614 /*
615  * Revision 1.1  1998/01/14 02:11:31  curt
616  * Initial revision.
617  *
618  */