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 <Bucket/bucketutils.h>
39 #include <Math/fg_geodesy.h>
40 #include <Math/mat3.h>
41 #include <Math/polar3d.h>
44 int nodecount, tricount;
46 static fgCartesianPoint3d nodes[MAX_NODES];
47 static int tris[MAX_TRIS][3];
49 static double normals[MAX_NODES][3];
52 fgBUCKET ne_index, nw_index, sw_index, se_index;
53 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 fgCartesianPoint3d geod_to_cart(double geod[3]) {
58 fgCartesianPoint3d cp;
60 double gc_lon, gc_lat, sl_radius;
62 /* printf("A geodetic point is (%.2f, %.2f, %.2f)\n",
63 geod[0], geod[1], geod[2]); */
65 gc_lon = geod[0]*ARCSEC_TO_RAD;
66 fgGeodToGeoc(geod[1]*ARCSEC_TO_RAD, geod[2], &sl_radius, &gc_lat);
68 /* printf("A geocentric point is (%.2f, %.2f, %.2f)\n", gc_lon,
69 gc_lat, sl_radius+geod[2]); */
73 pp.radius = sl_radius+geod[2];
74 cp = fgPolarToCart3d(pp);
76 /* printf("A cart point is (%.8f, %.8f, %.8f)\n", cp.x, cp.y, cp.z); */
82 /* given three points defining a triangle, calculate the normal */
83 void calc_normal(fgCartesianPoint3d p1, fgCartesianPoint3d p2,
84 fgCartesianPoint3d p3, double normal[3])
89 v1[0] = p2.x - p1.x; v1[1] = p2.y - p1.y; v1[2] = p2.z - p1.z;
90 v2[0] = p3.x - p1.x; v2[1] = p3.y - p1.y; v2[2] = p3.z - p1.z;
92 MAT3cross_product(normal, v1, v2);
93 MAT3_NORMALIZE_VEC(normal, temp);
95 /* printf(" Normal = %.2f %.2f %.2f\n", normal[0], normal[1], normal[2]); */
99 /* return the index of all triangles containing the specified node */
100 void find_tris(int n, int *t1, int *t2, int *t3, int *t4, int *t5) {
103 *t1 = *t2 = *t3 = *t4 = *t5 = 0;
106 while ( i <= tricount ) {
107 if ( (n == tris[i][0]) || (n == tris[i][1]) || (n == tris[i][2]) ) {
110 } else if ( *t2 == 0 ) {
112 } else if ( *t3 == 0 ) {
114 } else if ( *t4 == 0 ) {
125 /* return the file base name ( foo/bar/file.ext = file.ext ) */
126 void extract_file(char *in, char *base) {
132 while ( (i >= 0) && (in[i] != '/') ) {
141 /* return the file path name ( foo/bar/file.ext = foo/bar ) */
142 void extract_path(char *in, char *base) {
149 while ( (i >= 0) && (in[i] != '/') ) {
157 /* check if a file exists */
158 int file_exists(char *file) {
159 struct stat stat_buf;
162 printf("checking %s ... ", file);
164 result = stat(file, &stat_buf);
167 /* stat failed, no file */
168 printf("not found.\n");
171 /* stat succeeded, file exists */
178 /* check to see if a shared object exists */
179 int shared_object_exists(char *basepath, char *ext, char *file) {
180 char scene_path[256];
183 if ( strcmp(ext, ".sw") == 0 ) {
184 fgBucketGenBasePath(&my_index, scene_path);
185 index = fgBucketGenIndex(&my_index);
186 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
187 if ( file_exists(file) ) {
190 fgBucketGenBasePath(&west_index, scene_path);
191 index = fgBucketGenIndex(&west_index);
192 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
193 if ( file_exists(file) ) {
196 fgBucketGenBasePath(&sw_index, scene_path);
197 index = fgBucketGenIndex(&sw_index);
198 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
199 if ( file_exists(file) ) {
202 fgBucketGenBasePath(&south_index, scene_path);
203 index = fgBucketGenIndex(&south_index);
204 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
205 if ( file_exists(file) ) {
210 if ( strcmp(ext, ".se") == 0 ) {
211 fgBucketGenBasePath(&my_index, scene_path);
212 index = fgBucketGenIndex(&my_index);
213 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
214 if ( file_exists(file) ) {
217 fgBucketGenBasePath(&east_index, scene_path);
218 index = fgBucketGenIndex(&east_index);
219 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
220 if ( file_exists(file) ) {
223 fgBucketGenBasePath(&se_index, scene_path);
224 index = fgBucketGenIndex(&se_index);
225 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
226 if ( file_exists(file) ) {
229 fgBucketGenBasePath(&south_index, scene_path);
230 index = fgBucketGenIndex(&south_index);
231 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
232 if ( file_exists(file) ) {
237 if ( strcmp(ext, ".ne") == 0 ) {
238 fgBucketGenBasePath(&my_index, scene_path);
239 index = fgBucketGenIndex(&my_index);
240 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
241 if ( file_exists(file) ) {
244 fgBucketGenBasePath(&east_index, scene_path);
245 index = fgBucketGenIndex(&east_index);
246 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
247 if ( file_exists(file) ) {
250 fgBucketGenBasePath(&ne_index, scene_path);
251 index = fgBucketGenIndex(&ne_index);
252 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
253 if ( file_exists(file) ) {
256 fgBucketGenBasePath(&north_index, scene_path);
257 index = fgBucketGenIndex(&north_index);
258 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
259 if ( file_exists(file) ) {
264 if ( strcmp(ext, ".nw") == 0 ) {
265 fgBucketGenBasePath(&my_index, scene_path);
266 index = fgBucketGenIndex(&my_index);
267 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
268 if ( file_exists(file) ) {
271 fgBucketGenBasePath(&west_index, scene_path);
272 index = fgBucketGenIndex(&west_index);
273 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
274 if ( file_exists(file) ) {
277 fgBucketGenBasePath(&nw_index, scene_path);
278 index = fgBucketGenIndex(&nw_index);
279 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
280 if ( file_exists(file) ) {
283 fgBucketGenBasePath(&north_index, scene_path);
284 index = fgBucketGenIndex(&north_index);
285 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
286 if ( file_exists(file) ) {
291 if ( strcmp(ext, ".south") == 0 ) {
292 fgBucketGenBasePath(&my_index, scene_path);
293 index = fgBucketGenIndex(&my_index);
294 sprintf(file, "%s/%s/%ld.1.south", basepath, scene_path, index);
295 if ( file_exists(file) ) {
298 fgBucketGenBasePath(&south_index, scene_path);
299 index = fgBucketGenIndex(&south_index);
300 sprintf(file, "%s/%s/%ld.1.north", basepath, scene_path, index);
301 if ( file_exists(file) ) {
306 if ( strcmp(ext, ".north") == 0 ) {
307 fgBucketGenBasePath(&my_index, scene_path);
308 index = fgBucketGenIndex(&my_index);
309 sprintf(file, "%s/%s/%ld.1.north", basepath, scene_path, index);
310 if ( file_exists(file) ) {
313 fgBucketGenBasePath(&north_index, scene_path);
314 index = fgBucketGenIndex(&north_index);
315 sprintf(file, "%s/%s/%ld.1.south", basepath, scene_path, index);
316 if ( file_exists(file) ) {
321 if ( strcmp(ext, ".west") == 0 ) {
322 fgBucketGenBasePath(&my_index, scene_path);
323 index = fgBucketGenIndex(&my_index);
324 sprintf(file, "%s/%s/%ld.1.west", basepath, scene_path, index);
325 if ( file_exists(file) ) {
328 fgBucketGenBasePath(&west_index, scene_path);
329 index = fgBucketGenIndex(&west_index);
330 sprintf(file, "%s/%s/%ld.1.east", basepath, scene_path, index);
331 if ( file_exists(file) ) {
336 if ( strcmp(ext, ".east") == 0 ) {
337 fgBucketGenBasePath(&my_index, scene_path);
338 index = fgBucketGenIndex(&my_index);
339 sprintf(file, "%s/%s/%ld.1.east", basepath, scene_path, index);
340 if ( file_exists(file) ) {
343 fgBucketGenBasePath(&east_index, scene_path);
344 index = fgBucketGenIndex(&east_index);
345 sprintf(file, "%s/%s/%ld.1.west", basepath, scene_path, index);
346 if ( file_exists(file) ) {
351 if ( strcmp(ext, ".body") == 0 ) {
352 fgBucketGenBasePath(&my_index, scene_path);
353 index = fgBucketGenIndex(&my_index);
354 sprintf(file, "%s/%s/%ld.1.body", basepath, scene_path, index);
355 if ( file_exists(file) ) {
364 /* given a file pointer, read all the vn (normals from it) */
365 void read_normals(FILE *fp) {
368 while ( fgets(line, 250, fp) != NULL ) {
369 if ( strncmp(line, "vn ", 3) == 0 ) {
370 sscanf( line, "vn %lf %lf %lf\n",
371 &normals[normalcount][0],
372 &normals[normalcount][1],
373 &normals[normalcount][2] );
375 printf("read_normals(%d) %.2f %.2f %.2f %s", normalcount,
376 normals[normalcount][0], normals[normalcount][1],
377 normals[normalcount][2], line);
385 /* my custom file opening routine ... don't open if a shared edge or
386 * vertex alread exists */
387 FILE *my_open(char *basename, char *basepath, char *ext) {
391 /* check if a shared object already exists */
392 if ( shared_object_exists(basepath, ext, filename) ) {
393 /* not an actual file open error, but we've already got the
394 * shared edge, so we don't want to create another one */
395 fp = fopen(filename, "r");
396 printf("Opening %s\n", filename);
400 printf("not opening\n");
406 /* Initialize a new mesh structure */
407 void triload(char *basename, char *basepath) {
408 char nodename[256], elename[256];
410 FILE *ne, *nw, *se, *sw, *north, *south, *east, *west;
412 int dim, junk1, junk2;
415 ne = my_open(basename, basepath, ".ne");
419 nw = my_open(basename, basepath, ".nw");
423 se = my_open(basename, basepath, ".se");
427 sw = my_open(basename, basepath, ".sw");
431 north = my_open(basename, basepath, ".north");
435 south = my_open(basename, basepath, ".south");
439 east = my_open(basename, basepath, ".east");
443 west = my_open(basename, basepath, ".west");
447 strcpy(nodename, basename);
448 strcat(nodename, ".node");
449 strcpy(elename, basename);
450 strcat(elename, ".ele");
452 printf("Loading node file: %s ...\n", nodename);
453 if ( (node = fopen(nodename, "r")) == NULL ) {
454 printf("Cannot open file '%s'\n", nodename);
458 fscanf(node, "%d %d %d %d", &nodecount, &dim, &junk1, &junk2);
460 if ( nodecount > MAX_NODES - 1 ) {
461 printf("Error, too many nodes, need to increase array size\n");
464 printf(" Expecting %d nodes\n", nodecount);
467 for ( i = 1; i <= nodecount; i++ ) {
468 fscanf(node, "%d %lf %lf %lf %d\n", &junk1,
469 &n[0], &n[1], &n[2], &junk2);
470 /* printf("%d %.2f %.2f %.2f\n", junk1, n[0], n[1], n[2]); */
471 nodes[i] = geod_to_cart(n);
472 /* printf("%d %.2f %.2f %.2f\n",
473 junk1, nodes[i].x, nodes[i].y, nodes[i].z); */
478 printf("Loading element file: %s ...\n", elename);
479 if ( (ele = fopen(elename, "r")) == NULL ) {
480 printf("Cannot open file '%s'\n", elename);
484 fscanf(ele, "%d %d %d", &tricount, &junk1, &junk2);
486 if ( tricount > MAX_TRIS - 1 ) {
487 printf("Error, too many elements, need to increase array size\n");
490 printf(" Expecting %d elements\n", tricount);
493 for ( i = 1; i <= tricount; i++ ) {
494 fscanf(ele, "%d %d %d %d\n", &junk1,
495 &tris[i][0], &tris[i][1], &tris[i][2]);
496 /* printf("%d %d %d %d\n", junk1, tris[i][0], tris[i][1], tris[i][2]);*/
503 /* dump in WaveFront .obj format */
504 void dump_obj(char *basename) {
506 double n1[3], n2[3], n3[3], n4[3], n5[3], norm[3], temp;
508 int i, t1, t2, t3, t4, t5, count;
510 strcpy(objname, basename);
511 strcat(objname, ".obj");
513 printf("Dumping to file: %s ...\n", objname);
515 obj = fopen(objname, "w");
518 printf(" writing vertices\n");
519 for ( i = 1; i <= nodecount; i++ ) {
520 fprintf(obj, "v %.6f %.6f %.6f\n",
521 nodes[i].x, nodes[i].y, nodes[i].z);
524 printf(" calculating and writing normals\n");
525 printf(" First %d normals taken from shared files.\n", normalcount);
527 /* calculate and generate normals */
528 for ( i = 1; i <= nodecount; i++ ) {
530 if ( i <= normalcount ) {
531 /* use precalculated (shared) normal */
532 norm[0] = normals[i-1][0];
533 norm[1] = normals[i-1][1];
534 norm[2] = normals[i-1][2];
536 /* printf("Finding normal\n"); */
538 find_tris(i, &t1, &t2, &t3, &t4, &t5);
540 n1[0] = n1[1] = n1[2] = 0.0;
541 n2[0] = n2[1] = n2[2] = 0.0;
542 n3[0] = n3[1] = n3[2] = 0.0;
543 n4[0] = n4[1] = n4[2] = 0.0;
544 n5[0] = n5[1] = n5[2] = 0.0;
547 calc_normal(nodes[tris[t1][0]], nodes[tris[t1][1]],
548 nodes[tris[t1][2]], n1);
551 calc_normal(nodes[tris[t2][0]], nodes[tris[t2][1]],
552 nodes[tris[t2][2]], n2);
557 calc_normal(nodes[tris[t3][0]], nodes[tris[t3][1]],
558 nodes[tris[t3][2]], n3);
563 calc_normal(nodes[tris[t4][0]], nodes[tris[t4][1]],
564 nodes[tris[t4][2]], n4);
569 calc_normal(nodes[tris[t5][0]], nodes[tris[t5][1]],
570 nodes[tris[t5][2]], n5);
574 /* printf(" norm[2] = %.2f %.2f %.2f\n", n1[2], n2[2], n3[2]); */
576 norm[0] = ( n1[0] + n2[0] + n3[0] + n4[0] + n5[0] ) / (double)count;
577 norm[1] = ( n1[1] + n2[1] + n3[1] + n4[1] + n5[1] ) / (double)count;
578 norm[2] = ( n1[2] + n2[2] + n3[2] + n4[2] + n5[2] ) / (double)count;
580 /* printf(" count = %d\n", count); */
581 /* printf(" Ave. normal = %.4f %.4f %.4f\n",
582 norm[0], norm[1], norm[2]);*/
583 MAT3_NORMALIZE_VEC(norm, temp);
584 /* printf(" Normalized ave. normal = %.4f %.4f %.4f\n", */
585 /* norm[0], norm[1], norm[2]); */
587 /* printf("%d vn %.4f %.4f %.4f\n", i, norm[0], norm[1], norm[2]); */
588 fprintf(obj, "vn %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);
592 printf(" writing faces\n");
593 for ( i = 1; i <= tricount; i++ ) {
594 fprintf(obj, "f %d %d %d\n", tris[i][0], tris[i][1], tris[i][2]);
600 int main(int argc, char **argv) {
601 char basename[256], basepath[256], temp[256];
605 strcpy(basename, argv[1]);
607 /* find the base path of the file */
608 extract_path(basename, basepath);
609 extract_path(basepath, basepath);
610 extract_path(basepath, basepath);
611 printf("%s\n", basepath);
613 /* find the index of the current file */
614 extract_file(basename, temp);
619 tmp_index = atoi(temp);
620 printf("%ld\n", tmp_index);
621 fgBucketParseIndex(tmp_index, &my_index);
623 printf("bucket = %d %d %d %d\n",
624 my_index.lon, my_index.lat, my_index.x, my_index.y);
625 /* generate the indexes of the neighbors */
626 fgBucketOffset(&my_index, &ne_index, 1, 1);
627 fgBucketOffset(&my_index, &nw_index, -1, 1);
628 fgBucketOffset(&my_index, &se_index, 1, -1);
629 fgBucketOffset(&my_index, &sw_index, -1, -1);
631 fgBucketOffset(&my_index, &north_index, 0, 1);
632 fgBucketOffset(&my_index, &south_index, 0, -1);
633 fgBucketOffset(&my_index, &east_index, 1, 0);
634 fgBucketOffset(&my_index, &west_index, -1, 0);
636 /* load the input data files */
637 triload(basename, basepath);
639 /* dump in WaveFront .obj format */
647 /* Revision 1.17 1998/07/04 00:56:40 curt
648 /* typedef'd struct fgBUCKET.
650 * Revision 1.16 1998/05/23 15:20:41 curt
651 * Output more digits after the decimal place.
653 * Revision 1.15 1998/05/02 01:54:39 curt
654 * Converting to polar3d.h routines.
656 * Revision 1.14 1998/04/18 04:01:32 curt
657 * Now use libMath rather than having local copies of math routines.
659 * Revision 1.13 1998/04/14 02:26:11 curt
660 * Code reorganizations. Added a Lib/ directory for more general libraries.
662 * Revision 1.12 1998/04/08 23:22:18 curt
663 * Adopted Gnu automake/autoconf system.
665 * Revision 1.11 1998/03/03 16:01:00 curt
666 * More c++ compile tweaks.
668 * Revision 1.10 1998/01/31 00:41:27 curt
669 * Made a few changes converting floats to doubles.
671 * Revision 1.9 1998/01/27 18:37:04 curt
672 * Lots of updates to get back in sync with changes made over in .../Src/
674 * Revision 1.8 1998/01/17 01:25:39 curt
675 * Added support for shared normals.
677 * Revision 1.7 1998/01/12 02:42:00 curt
678 * Average up to five triangles per vertex instead of three.
680 * Revision 1.6 1998/01/09 23:03:15 curt
681 * Restructured to split 1deg x 1deg dem's into 64 subsections.
683 * Revision 1.5 1997/12/08 19:17:50 curt
684 * Fixed a type in the normal generation code.
686 * Revision 1.4 1997/12/02 13:13:32 curt
687 * Fixed problem with averaged vertex normals.
689 * Revision 1.3 1997/11/15 18:05:05 curt
692 * Revision 1.2 1997/11/14 00:29:13 curt
693 * Transform scenery coordinates at this point in pipeline when scenery is
694 * being translated to .obj format, not when it is being loaded into the end
695 * renderer. Precalculate normals for each node as average of the normals
696 * of each containing polygon so Garoude shading is now supportable.
698 * Revision 1.1 1997/10/29 23:05:15 curt