1 /* tri2obj.c -- read in a .ele/.node file pair generated by the triangle
2 * program and output a simple Wavefront .obj file.
4 * Written by Curtis Olson, started October 1997.
6 * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * (Log is kept at end of this file)
28 #include <stdlib.h> /* for atoi() */
30 #include <sys/stat.h> /* for stat() */
31 #include <unistd.h> /* for stat() */
35 #include <Include/fg_constants.h>
36 #include <Include/fg_types.h>
37 #include <Math/fg_geodesy.h>
38 #include <Math/mat3.h>
39 #include <Math/polar.h>
40 #include <Scenery/bucketutils.h>
43 int nodecount, tricount;
45 struct fgCartesianPoint nodes[MAX_NODES];
46 int tris[MAX_TRIS][3];
47 int new_tris[MAX_TRIS][3];
49 double normals[MAX_NODES][3];
51 struct fgBUCKET my_index;
52 struct fgBUCKET ne_index, nw_index, sw_index, se_index;
53 struct fgBUCKET north_index, south_index, east_index, west_index;
55 /* convert a geodetic point lon(arcsec), lat(arcsec), elev(meter) to
56 * a cartesian point */
57 struct fgCartesianPoint geod_to_cart(double geod[3]) {
58 struct fgCartesianPoint p;
59 double gc_lon, gc_lat, sl_radius;
61 /* printf("A geodetic point is (%.2f, %.2f, %.2f)\n",
62 geod[0], geod[1], geod[2]); */
64 gc_lon = geod[0]*ARCSEC_TO_RAD;
65 fgGeodToGeoc(geod[1]*ARCSEC_TO_RAD, geod[2], &sl_radius, &gc_lat);
67 /* printf("A geocentric point is (%.2f, %.2f, %.2f)\n", gc_lon,
68 gc_lat, sl_radius+geod[2]); */
70 p = fgPolarToCart(gc_lon, gc_lat, sl_radius+geod[2]);
72 /* printf("A cart point is (%.8f, %.8f, %.8f)\n", p.x, p.y, p.z); */
78 /* given three points defining a triangle, calculate the normal */
79 void calc_normal(struct fgCartesianPoint p1, struct fgCartesianPoint p2,
80 struct fgCartesianPoint p3, double normal[3])
85 v1[0] = p2.x - p1.x; v1[1] = p2.y - p1.y; v1[2] = p2.z - p1.z;
86 v2[0] = p3.x - p1.x; v2[1] = p3.y - p1.y; v2[2] = p3.z - p1.z;
88 MAT3cross_product(normal, v1, v2);
89 MAT3_NORMALIZE_VEC(normal, temp);
91 /* printf(" Normal = %.2f %.2f %.2f\n", normal[0], normal[1], normal[2]); */
95 /* return the index of all triangles containing the specified node */
96 void find_tris(int n, int *t1, int *t2, int *t3, int *t4, int *t5) {
99 *t1 = *t2 = *t3 = *t4 = *t5 = 0;
102 while ( i <= tricount ) {
103 if ( (n == tris[i][0]) || (n == tris[i][1]) || (n == tris[i][2]) ) {
106 } else if ( *t2 == 0 ) {
108 } else if ( *t3 == 0 ) {
110 } else if ( *t4 == 0 ) {
121 /* return the file base name ( foo/bar/file.ext = file.ext ) */
122 void extract_file(char *in, char *base) {
128 while ( (i >= 0) && (in[i] != '/') ) {
137 /* return the file path name ( foo/bar/file.ext = foo/bar ) */
138 void extract_path(char *in, char *base) {
145 while ( (i >= 0) && (in[i] != '/') ) {
153 /* check if a file exists */
154 int file_exists(char *file) {
155 struct stat stat_buf;
158 printf("checking %s ... ", file);
160 result = stat(file, &stat_buf);
163 /* stat failed, no file */
164 printf("not found.\n");
167 /* stat succeeded, file exists */
174 /* check to see if a shared object exists */
175 int shared_object_exists(char *basepath, char *ext, char *file) {
176 char scene_path[256];
179 if ( strcmp(ext, ".sw") == 0 ) {
180 fgBucketGenBasePath(&my_index, scene_path);
181 index = fgBucketGenIndex(&my_index);
182 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
183 if ( file_exists(file) ) {
186 fgBucketGenBasePath(&west_index, scene_path);
187 index = fgBucketGenIndex(&west_index);
188 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
189 if ( file_exists(file) ) {
192 fgBucketGenBasePath(&sw_index, scene_path);
193 index = fgBucketGenIndex(&sw_index);
194 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
195 if ( file_exists(file) ) {
198 fgBucketGenBasePath(&south_index, scene_path);
199 index = fgBucketGenIndex(&south_index);
200 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
201 if ( file_exists(file) ) {
206 if ( strcmp(ext, ".se") == 0 ) {
207 fgBucketGenBasePath(&my_index, scene_path);
208 index = fgBucketGenIndex(&my_index);
209 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
210 if ( file_exists(file) ) {
213 fgBucketGenBasePath(&east_index, scene_path);
214 index = fgBucketGenIndex(&east_index);
215 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
216 if ( file_exists(file) ) {
219 fgBucketGenBasePath(&se_index, scene_path);
220 index = fgBucketGenIndex(&se_index);
221 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
222 if ( file_exists(file) ) {
225 fgBucketGenBasePath(&south_index, scene_path);
226 index = fgBucketGenIndex(&south_index);
227 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
228 if ( file_exists(file) ) {
233 if ( strcmp(ext, ".ne") == 0 ) {
234 fgBucketGenBasePath(&my_index, scene_path);
235 index = fgBucketGenIndex(&my_index);
236 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
237 if ( file_exists(file) ) {
240 fgBucketGenBasePath(&east_index, scene_path);
241 index = fgBucketGenIndex(&east_index);
242 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
243 if ( file_exists(file) ) {
246 fgBucketGenBasePath(&ne_index, scene_path);
247 index = fgBucketGenIndex(&ne_index);
248 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
249 if ( file_exists(file) ) {
252 fgBucketGenBasePath(&north_index, scene_path);
253 index = fgBucketGenIndex(&north_index);
254 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
255 if ( file_exists(file) ) {
260 if ( strcmp(ext, ".nw") == 0 ) {
261 fgBucketGenBasePath(&my_index, scene_path);
262 index = fgBucketGenIndex(&my_index);
263 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
264 if ( file_exists(file) ) {
267 fgBucketGenBasePath(&west_index, scene_path);
268 index = fgBucketGenIndex(&west_index);
269 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
270 if ( file_exists(file) ) {
273 fgBucketGenBasePath(&nw_index, scene_path);
274 index = fgBucketGenIndex(&nw_index);
275 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
276 if ( file_exists(file) ) {
279 fgBucketGenBasePath(&north_index, scene_path);
280 index = fgBucketGenIndex(&north_index);
281 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
282 if ( file_exists(file) ) {
287 if ( strcmp(ext, ".south") == 0 ) {
288 fgBucketGenBasePath(&my_index, scene_path);
289 index = fgBucketGenIndex(&my_index);
290 sprintf(file, "%s/%s/%ld.1.south", basepath, scene_path, index);
291 if ( file_exists(file) ) {
294 fgBucketGenBasePath(&south_index, scene_path);
295 index = fgBucketGenIndex(&south_index);
296 sprintf(file, "%s/%s/%ld.1.north", basepath, scene_path, index);
297 if ( file_exists(file) ) {
302 if ( strcmp(ext, ".north") == 0 ) {
303 fgBucketGenBasePath(&my_index, scene_path);
304 index = fgBucketGenIndex(&my_index);
305 sprintf(file, "%s/%s/%ld.1.north", basepath, scene_path, index);
306 if ( file_exists(file) ) {
309 fgBucketGenBasePath(&north_index, scene_path);
310 index = fgBucketGenIndex(&north_index);
311 sprintf(file, "%s/%s/%ld.1.south", basepath, scene_path, index);
312 if ( file_exists(file) ) {
317 if ( strcmp(ext, ".west") == 0 ) {
318 fgBucketGenBasePath(&my_index, scene_path);
319 index = fgBucketGenIndex(&my_index);
320 sprintf(file, "%s/%s/%ld.1.west", basepath, scene_path, index);
321 if ( file_exists(file) ) {
324 fgBucketGenBasePath(&west_index, scene_path);
325 index = fgBucketGenIndex(&west_index);
326 sprintf(file, "%s/%s/%ld.1.east", basepath, scene_path, index);
327 if ( file_exists(file) ) {
332 if ( strcmp(ext, ".east") == 0 ) {
333 fgBucketGenBasePath(&my_index, scene_path);
334 index = fgBucketGenIndex(&my_index);
335 sprintf(file, "%s/%s/%ld.1.east", basepath, scene_path, index);
336 if ( file_exists(file) ) {
339 fgBucketGenBasePath(&east_index, scene_path);
340 index = fgBucketGenIndex(&east_index);
341 sprintf(file, "%s/%s/%ld.1.west", basepath, scene_path, index);
342 if ( file_exists(file) ) {
347 if ( strcmp(ext, ".body") == 0 ) {
348 fgBucketGenBasePath(&my_index, scene_path);
349 index = fgBucketGenIndex(&my_index);
350 sprintf(file, "%s/%s/%ld.1.body", basepath, scene_path, index);
351 if ( file_exists(file) ) {
360 /* given a file pointer, read all the vn (normals from it) */
361 void read_normals(FILE *fp) {
364 while ( fgets(line, 250, fp) != NULL ) {
365 if ( strncmp(line, "vn ", 3) == 0 ) {
366 sscanf( line, "vn %lf %lf %lf\n",
367 &normals[normalcount][0],
368 &normals[normalcount][1],
369 &normals[normalcount][2] );
371 printf("read_normals(%d) %.2f %.2f %.2f %s", normalcount,
372 normals[normalcount][0], normals[normalcount][1],
373 normals[normalcount][2], line);
381 /* my custom file opening routine ... don't open if a shared edge or
382 * vertex alread exists */
383 FILE *my_open(char *basename, char *basepath, char *ext) {
387 /* check if a shared object already exists */
388 if ( shared_object_exists(basepath, ext, filename) ) {
389 /* not an actual file open error, but we've already got the
390 * shared edge, so we don't want to create another one */
391 fp = fopen(filename, "r");
392 printf("Opening %s\n", filename);
396 printf("not opening\n");
402 /* Initialize a new mesh structure */
403 void triload(char *basename, char *basepath) {
404 char nodename[256], elename[256];
406 FILE *ne, *nw, *se, *sw, *north, *south, *east, *west;
408 int dim, junk1, junk2;
411 ne = my_open(basename, basepath, ".ne");
415 nw = my_open(basename, basepath, ".nw");
419 se = my_open(basename, basepath, ".se");
423 sw = my_open(basename, basepath, ".sw");
427 north = my_open(basename, basepath, ".north");
431 south = my_open(basename, basepath, ".south");
435 east = my_open(basename, basepath, ".east");
439 west = my_open(basename, basepath, ".west");
443 strcpy(nodename, basename);
444 strcat(nodename, ".node");
445 strcpy(elename, basename);
446 strcat(elename, ".ele");
448 printf("Loading node file: %s ...\n", nodename);
449 if ( (node = fopen(nodename, "r")) == NULL ) {
450 printf("Cannot open file '%s'\n", nodename);
454 fscanf(node, "%d %d %d %d", &nodecount, &dim, &junk1, &junk2);
456 if ( nodecount > MAX_NODES - 1 ) {
457 printf("Error, too many nodes, need to increase array size\n");
460 printf(" Expecting %d nodes\n", nodecount);
463 for ( i = 1; i <= nodecount; i++ ) {
464 fscanf(node, "%d %lf %lf %lf %d\n", &junk1,
465 &n[0], &n[1], &n[2], &junk2);
466 /* printf("%d %.2f %.2f %.2f\n", junk1, n[0], n[1], n[2]); */
467 nodes[i] = geod_to_cart(n);
468 /* printf("%d %.2f %.2f %.2f\n",
469 junk1, nodes[i].x, nodes[i].y, nodes[i].z); */
474 printf("Loading element file: %s ...\n", elename);
475 if ( (ele = fopen(elename, "r")) == NULL ) {
476 printf("Cannot open file '%s'\n", elename);
480 fscanf(ele, "%d %d %d", &tricount, &junk1, &junk2);
482 if ( tricount > MAX_TRIS - 1 ) {
483 printf("Error, too many elements, need to increase array size\n");
486 printf(" Expecting %d elements\n", tricount);
489 for ( i = 1; i <= tricount; i++ ) {
490 fscanf(ele, "%d %d %d %d\n", &junk1,
491 &tris[i][0], &tris[i][1], &tris[i][2]);
492 /* printf("%d %d %d %d\n", junk1, tris[i][0], tris[i][1], tris[i][2]);*/
499 /* dump in WaveFront .obj format */
500 void dump_obj(char *basename) {
502 double n1[3], n2[3], n3[3], n4[3], n5[3], norm[3], temp;
504 int i, t1, t2, t3, t4, t5, count;
506 strcpy(objname, basename);
507 strcat(objname, ".obj");
509 printf("Dumping to file: %s ...\n", objname);
511 obj = fopen(objname, "w");
514 printf(" writing vertices\n");
515 for ( i = 1; i <= nodecount; i++ ) {
516 fprintf(obj, "v %.2f %.2f %.2f\n",
517 nodes[i].x, nodes[i].y, nodes[i].z);
520 printf(" calculating and writing normals\n");
521 printf(" First %d normals taken from shared files.\n", normalcount);
523 /* calculate and generate normals */
524 for ( i = 1; i <= nodecount; i++ ) {
526 if ( i <= normalcount ) {
527 /* use precalculated (shared) normal */
528 norm[0] = normals[i-1][0];
529 norm[1] = normals[i-1][1];
530 norm[2] = normals[i-1][2];
532 /* printf("Finding normal\n"); */
534 find_tris(i, &t1, &t2, &t3, &t4, &t5);
536 n1[0] = n1[1] = n1[2] = 0.0;
537 n2[0] = n2[1] = n2[2] = 0.0;
538 n3[0] = n3[1] = n3[2] = 0.0;
539 n4[0] = n4[1] = n4[2] = 0.0;
540 n5[0] = n5[1] = n5[2] = 0.0;
543 calc_normal(nodes[tris[t1][0]], nodes[tris[t1][1]],
544 nodes[tris[t1][2]], n1);
547 calc_normal(nodes[tris[t2][0]], nodes[tris[t2][1]],
548 nodes[tris[t2][2]], n2);
553 calc_normal(nodes[tris[t3][0]], nodes[tris[t3][1]],
554 nodes[tris[t3][2]], n3);
559 calc_normal(nodes[tris[t4][0]], nodes[tris[t4][1]],
560 nodes[tris[t4][2]], n4);
565 calc_normal(nodes[tris[t5][0]], nodes[tris[t5][1]],
566 nodes[tris[t5][2]], n5);
570 /* printf(" norm[2] = %.2f %.2f %.2f\n", n1[2], n2[2], n3[2]); */
572 norm[0] = ( n1[0] + n2[0] + n3[0] + n4[0] + n5[0] ) / (double)count;
573 norm[1] = ( n1[1] + n2[1] + n3[1] + n4[1] + n5[1] ) / (double)count;
574 norm[2] = ( n1[2] + n2[2] + n3[2] + n4[2] + n5[2] ) / (double)count;
576 /* printf(" count = %d\n", count); */
577 /* printf(" Ave. normal = %.4f %.4f %.4f\n",
578 norm[0], norm[1], norm[2]);*/
579 MAT3_NORMALIZE_VEC(norm, temp);
580 /* printf(" Normalized ave. normal = %.4f %.4f %.4f\n", */
581 /* norm[0], norm[1], norm[2]); */
583 /* printf("%d vn %.4f %.4f %.4f\n", i, norm[0], norm[1], norm[2]); */
584 fprintf(obj, "vn %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);
588 printf(" writing faces\n");
589 for ( i = 1; i <= tricount; i++ ) {
590 fprintf(obj, "f %d %d %d\n", tris[i][0], tris[i][1], tris[i][2]);
596 int main(int argc, char **argv) {
597 char basename[256], basepath[256], temp[256];
601 strcpy(basename, argv[1]);
603 /* find the base path of the file */
604 extract_path(basename, basepath);
605 extract_path(basepath, basepath);
606 extract_path(basepath, basepath);
607 printf("%s\n", basepath);
609 /* find the index of the current file */
610 extract_file(basename, temp);
615 tmp_index = atoi(temp);
616 printf("%ld\n", tmp_index);
617 fgBucketParseIndex(tmp_index, &my_index);
619 printf("bucket = %d %d %d %d\n",
620 my_index.lon, my_index.lat, my_index.x, my_index.y);
621 /* generate the indexes of the neighbors */
622 fgBucketOffset(&my_index, &ne_index, 1, 1);
623 fgBucketOffset(&my_index, &nw_index, -1, 1);
624 fgBucketOffset(&my_index, &se_index, 1, -1);
625 fgBucketOffset(&my_index, &sw_index, -1, -1);
627 fgBucketOffset(&my_index, &north_index, 0, 1);
628 fgBucketOffset(&my_index, &south_index, 0, -1);
629 fgBucketOffset(&my_index, &east_index, 1, 0);
630 fgBucketOffset(&my_index, &west_index, -1, 0);
632 /* load the input data files */
633 triload(basename, basepath);
635 /* dump in WaveFront .obj format */
643 /* Revision 1.10 1998/01/31 00:41:27 curt
644 /* Made a few changes converting floats to doubles.
646 * Revision 1.9 1998/01/27 18:37:04 curt
647 * Lots of updates to get back in sync with changes made over in .../Src/
649 * Revision 1.8 1998/01/17 01:25:39 curt
650 * Added support for shared normals.
652 * Revision 1.7 1998/01/12 02:42:00 curt
653 * Average up to five triangles per vertex instead of three.
655 * Revision 1.6 1998/01/09 23:03:15 curt
656 * Restructured to split 1deg x 1deg dem's into 64 subsections.
658 * Revision 1.5 1997/12/08 19:17:50 curt
659 * Fixed a type in the normal generation code.
661 * Revision 1.4 1997/12/02 13:13:32 curt
662 * Fixed problem with averaged vertex normals.
664 * Revision 1.3 1997/11/15 18:05:05 curt
667 * Revision 1.2 1997/11/14 00:29:13 curt
668 * Transform scenery coordinates at this point in pipeline when scenery is
669 * being translated to .obj format, not when it is being loaded into the end
670 * renderer. Precalculate normals for each node as average of the normals
671 * of each containing polygon so Garoude shading is now supportable.
673 * Revision 1.1 1997/10/29 23:05:15 curt