]> git.mxchange.org Git - flightgear.git/blob - SplitTris/splittris.c
Adopted Gnu automake/autoconf system.
[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 <Include/fg_constants.h>
38 #include <Include/fg_types.h>
39 #include <Scenery/Bucket/bucketutils.h>
40
41 #include "fg_geodesy.h"
42 #include "mat3.h"
43 #include "polar.h"
44
45 int nodecount, tricount;
46 double xmin, xmax, ymin, ymax;
47
48 static double nodes_orig[MAX_NODES][3];
49 static int tris[MAX_TRIS][3];
50 /* static int new_tris[MAX_TRIS][3]; */
51
52 static struct fgCartesianPoint nodes_cart[MAX_NODES];
53
54 struct fgBUCKET ne_index, nw_index, sw_index, se_index;
55 struct fgBUCKET 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     double 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         fgBucketGenBasePath(&west_index, scene_path);
264         index = fgBucketGenIndex(&west_index);
265         sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
266         if ( file_exists(file) ) {
267             return(1);
268         }
269         fgBucketGenBasePath(&sw_index, scene_path);
270         index = fgBucketGenIndex(&sw_index);
271         sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
272         if ( file_exists(file) ) {
273             return(1);
274         }
275         fgBucketGenBasePath(&south_index, scene_path);
276         index = fgBucketGenIndex(&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         fgBucketGenBasePath(&east_index, scene_path);
285         index = fgBucketGenIndex(&east_index);
286         sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
287         if ( file_exists(file) ) {
288             return(1);
289         }
290         fgBucketGenBasePath(&se_index, scene_path);
291         index = fgBucketGenIndex(&se_index);
292         sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
293         if ( file_exists(file) ) {
294             return(1);
295         }
296         fgBucketGenBasePath(&south_index, scene_path);
297         index = fgBucketGenIndex(&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         fgBucketGenBasePath(&east_index, scene_path);
306         index = fgBucketGenIndex(&east_index);
307         sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
308         if ( file_exists(file) ) {
309             return(1);
310         }
311         fgBucketGenBasePath(&ne_index, scene_path);
312         index = fgBucketGenIndex(&ne_index);
313         sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
314         if ( file_exists(file) ) {
315             return(1);
316         }
317         fgBucketGenBasePath(&north_index, scene_path);
318         index = fgBucketGenIndex(&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         fgBucketGenBasePath(&west_index, scene_path);
327         index = fgBucketGenIndex(&west_index);
328         sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
329         if ( file_exists(file) ) {
330             return(1);
331         }
332         fgBucketGenBasePath(&nw_index, scene_path);
333         index = fgBucketGenIndex(&nw_index);
334         sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
335         if ( file_exists(file) ) {
336             return(1);
337         }
338         fgBucketGenBasePath(&north_index, scene_path);
339         index = fgBucketGenIndex(&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         fgBucketGenBasePath(&south_index, scene_path);
348         index = fgBucketGenIndex(&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         fgBucketGenBasePath(&north_index, scene_path);
357         index = fgBucketGenIndex(&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         fgBucketGenBasePath(&west_index, scene_path);
366         index = fgBucketGenIndex(&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         fgBucketGenBasePath(&east_index, scene_path);
375         index = fgBucketGenIndex(&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
460         if ( fp != NULL ) {
461             fprintf(fp, "gdn %.2f %.2f %.2f\n", 
462                     nodes_orig[i][0], nodes_orig[i][1], nodes_orig[i][2]);
463         }
464     }
465
466     printf("  calculating and writing normals\n");
467     /* calculate and generate normals */
468     for ( i = 1; i <= nodecount; i++ ) {
469 /*      printf("Finding normal\n"); */
470
471         find_tris(i, &t1, &t2, &t3, &t4, &t5);
472
473         n1[0] = n1[1] = n1[2] = 0.0;
474         n2[0] = n2[1] = n2[2] = 0.0;
475         n3[0] = n3[1] = n3[2] = 0.0;
476         n4[0] = n4[1] = n4[2] = 0.0;
477         n5[0] = n5[1] = n5[2] = 0.0;
478
479         count = 1;
480         calc_normal(nodes_cart[tris[t1][0]], nodes_cart[tris[t1][1]], 
481                     nodes_cart[tris[t1][2]], n1);
482
483         if ( t2 > 0 ) {
484             calc_normal(nodes_cart[tris[t2][0]], nodes_cart[tris[t2][1]], 
485                         nodes_cart[tris[t2][2]], n2);
486             count = 2;
487         }
488
489         if ( t3 > 0 ) {
490             calc_normal(nodes_cart[tris[t3][0]], nodes_cart[tris[t3][1]],
491                         nodes_cart[tris[t3][2]], n3);
492             count = 3;
493         }
494
495         if ( t4 > 0 ) {
496             calc_normal(nodes_cart[tris[t4][0]], nodes_cart[tris[t4][1]],
497                         nodes_cart[tris[t4][2]], n4);
498             count = 4;
499         }
500
501         if ( t5 > 0 ) {
502             calc_normal(nodes_cart[tris[t5][0]], nodes_cart[tris[t5][1]],
503                         nodes_cart[tris[t5][2]], n5);
504             count = 5;
505         }
506
507 /*      printf("  norm[2] = %.2f %.2f %.2f\n", n1[2], n2[2], n3[2]); */
508
509         norm[0] = ( n1[0] + n2[0] + n3[0] + n4[0] + n5[0] ) / (double)count;
510         norm[1] = ( n1[1] + n2[1] + n3[1] + n4[1] + n5[1] ) / (double)count;
511         norm[2] = ( n1[2] + n2[2] + n3[2] + n4[2] + n5[2] ) / (double)count;
512         
513 /*      printf("  count = %d\n", count); */
514 /*      printf("  Ave. normal = %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);*/
515         MAT3_NORMALIZE_VEC(norm, temp);
516 /*      printf("  Normalized ave. normal = %.4f %.4f %.4f\n",  */
517 /*             norm[0], norm[1], norm[2]); */
518         
519         fp = NULL;
520
521         if ( (fabs(nodes_orig[i][1] - ymin) < FG_EPSILON) && 
522              (fabs(nodes_orig[i][0] - xmin) < FG_EPSILON) ) {
523             fp = sw;
524         } else if ( (fabs(nodes_orig[i][1] - ymin) < FG_EPSILON) &&
525                     (fabs(nodes_orig[i][0] - xmax) < FG_EPSILON) ) {
526             fp = se;
527         } else if ( (fabs(nodes_orig[i][1] - ymax) < FG_EPSILON) &&
528                     (fabs(nodes_orig[i][0] - xmax) < FG_EPSILON)) {
529             fp = ne;
530         } else if ( (fabs(nodes_orig[i][1] - ymax) < FG_EPSILON) &&
531                     (fabs(nodes_orig[i][0] - xmin) < FG_EPSILON) ) {
532             fp = nw;
533         } else if ( fabs(nodes_orig[i][0] - xmin) < FG_EPSILON ) {
534             fp = west;
535         } else if ( fabs(nodes_orig[i][0] - xmax) < FG_EPSILON ) {
536             fp = east;
537         } else if ( fabs(nodes_orig[i][1] - ymin) < FG_EPSILON ) {
538             fp = south;
539         } else if ( fabs(nodes_orig[i][1] - ymax) < FG_EPSILON ) {
540             fp = north;
541         }
542         if ( fp != NULL ) {
543             fprintf(fp, "vn %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);
544         }
545     }
546
547     if ( sw ) { fclose(sw); }
548     if ( se ) { fclose(se); }
549     if ( ne ) { fclose(ne); }
550     if ( nw ) { fclose(nw); }
551
552     if ( north ) { fclose(north); }
553     if ( south ) { fclose(south); }
554     if ( east ) { fclose(east); }
555     if ( west ) { fclose(west); }
556
557     if ( body ) { fclose(body); }
558 }
559
560
561 int main(int argc, char **argv) {
562     char basename[256], basepath[256], temp[256];
563     struct fgBUCKET p;
564     long int index;
565     int len;
566
567     strcpy(basename, argv[1]);
568
569     /* find the base path of the file */
570     extract_path(basename, basepath);
571     extract_path(basepath, basepath);
572     extract_path(basepath, basepath);
573     printf("%s\n", basepath);
574
575     /* find the index of the current file */
576     extract_file(basename, temp);
577     len = strlen(temp);
578     if ( len >= 2 ) {
579         temp[len-2] = '\0';
580     }
581     index = atoi(temp);
582     printf("%ld\n", index);
583     fgBucketParseIndex(index, &p);
584
585     printf("bucket = %d %d %d %d\n", p.lon, p.lat, p.x, p.y);
586     /* generate the indexes of the neighbors */
587     fgBucketOffset(&p, &ne_index,  1,  1);
588     fgBucketOffset(&p, &nw_index, -1,  1);
589     fgBucketOffset(&p, &se_index,  1, -1);
590     fgBucketOffset(&p, &sw_index, -1, -1);
591
592     fgBucketOffset(&p, &north_index,  0,  1);
593     fgBucketOffset(&p, &south_index,  0, -1);
594     fgBucketOffset(&p, &east_index,  1,  0);
595     fgBucketOffset(&p, &west_index, -1,  0);
596
597     /*
598     printf("Corner indexes = %ld %ld %ld %ld\n", 
599            ne_index, nw_index, sw_index, se_index);
600     printf("Edge indexes = %ld %ld %ld %ld\n",
601            north_index, south_index, east_index, west_index);
602            */
603
604     /* load the input data files */
605     triload(basename);
606
607     /* dump in WaveFront .obj format */
608     dump_obj(basename, basepath);
609
610     return(0);
611 }
612
613
614 /* $Log$
615 /* Revision 1.7  1998/04/08 23:21:13  curt
616 /* Adopted Gnu automake/autoconf system.
617 /*
618  * Revision 1.6  1998/03/03 15:36:13  curt
619  * Tweaks for compiling with g++
620  *
621  * Revision 1.5  1998/03/03 03:37:04  curt
622  * Cumulative tweaks.
623  *
624  * Revision 1.4  1998/01/31 00:41:26  curt
625  * Made a few changes converting floats to doubles.
626  *
627  * Revision 1.3  1998/01/27 18:37:04  curt
628  * Lots of updates to get back in sync with changes made over in .../Src/
629  *
630  * Revision 1.2  1998/01/14 15:54:43  curt
631  * Initial revision completed.
632  *
633  * Revision 1.1  1998/01/14 02:11:31  curt
634  * Initial revision.
635  *
636  */