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.
6 * Written by Curtis Olson, started January 1998.
8 * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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.
25 * (Log is kept at end of this file) */
30 #include <stdlib.h> /* for atoi() */
32 #include <sys/stat.h> /* for stat() */
33 #include <unistd.h> /* for stat() */
35 #include "splittris.h"
37 #include <Include/fg_constants.h>
38 #include <Include/fg_types.h>
39 #include <Bucket/bucketutils.h>
41 #include <Math/fg_geodesy.h>
42 #include <Math/mat3.h>
43 #include <Math/polar3d.h>
45 int nodecount, tricount;
46 double xmin, xmax, ymin, ymax;
48 static double nodes_orig[MAX_NODES][3];
49 static int tris[MAX_TRIS][3];
50 /* static int new_tris[MAX_TRIS][3]; */
52 static fgCartesianPoint3d nodes_cart[MAX_NODES];
54 fgBUCKET ne_index, nw_index, sw_index, se_index;
55 fgBUCKET north_index, south_index, east_index, west_index;
57 /* convert a geodetic point lon(arcsec), lat(arcsec), elev(meter) to
58 * a cartesian point */
59 fgCartesianPoint3d geod_to_cart(double geod[3]) {
60 fgCartesianPoint3d cp;
62 double gc_lon, gc_lat, sl_radius;
64 /* printf("A geodetic point is (%.2f, %.2f, %.2f)\n",
65 geod[0], geod[1], geod[2]); */
67 gc_lon = geod[0]*ARCSEC_TO_RAD;
68 fgGeodToGeoc(geod[1]*ARCSEC_TO_RAD, geod[2], &sl_radius, &gc_lat);
70 /* printf("A geocentric point is (%.2f, %.2f, %.2f)\n", gc_lon,
71 gc_lat, sl_radius+geod[2]); */
75 pp.radius = sl_radius+geod[2];
76 cp = fgPolarToCart3d(pp);
78 /* printf("A cart point is (%.8f, %.8f, %.8f)\n", cp.x, cp.y, cp.z); */
84 /* given three points defining a triangle, calculate the normal */
85 void calc_normal(fgCartesianPoint3d p1, fgCartesianPoint3d p2,
86 fgCartesianPoint3d p3, double normal[3])
91 v1[0] = p2.x - p1.x; v1[1] = p2.y - p1.y; v1[2] = p2.z - p1.z;
92 v2[0] = p3.x - p1.x; v2[1] = p3.y - p1.y; v2[2] = p3.z - p1.z;
94 MAT3cross_product(normal, v1, v2);
95 MAT3_NORMALIZE_VEC(normal,temp);
97 /* printf(" Normal = %.2f %.2f %.2f\n", normal[0], normal[1], normal[2]); */
101 /* return the file base name ( foo/bar/file.ext = file.ext ) */
102 void extract_file(char *in, char *base) {
108 while ( (i >= 0) && (in[i] != '/') ) {
117 /* return the file path name ( foo/bar/file.ext = foo/bar ) */
118 void extract_path(char *in, char *base) {
125 while ( (i >= 0) && (in[i] != '/') ) {
133 /* return the index of all triangles containing the specified node */
134 void find_tris(int n, int *t1, int *t2, int *t3, int *t4, int *t5) {
137 *t1 = *t2 = *t3 = *t4 = *t5 = 0;
140 while ( i <= tricount ) {
141 if ( (n == tris[i][0]) || (n == tris[i][1]) || (n == tris[i][2]) ) {
144 } else if ( *t2 == 0 ) {
146 } else if ( *t3 == 0 ) {
148 } else if ( *t4 == 0 ) {
159 /* Initialize a new mesh structure */
160 void triload(char *basename) {
161 char nodename[256], elename[256];
163 int dim, junk1, junk2;
166 strcpy(nodename, basename);
167 strcat(nodename, ".node");
168 strcpy(elename, basename);
169 strcat(elename, ".ele");
171 printf("Loading node file: %s ...\n", nodename);
172 if ( (node = fopen(nodename, "r")) == NULL ) {
173 printf("Cannot open file '%s'\n", nodename);
177 fscanf(node, "%d %d %d %d", &nodecount, &dim, &junk1, &junk2);
179 if ( nodecount > MAX_NODES - 1 ) {
180 printf("Error, too many nodes, need to increase array size\n");
183 printf(" Expecting %d nodes\n", nodecount);
186 for ( i = 1; i <= nodecount; i++ ) {
187 fscanf(node, "%d %lf %lf %lf %d\n", &junk1,
188 &nodes_orig[i][0], &nodes_orig[i][1], &nodes_orig[i][2], &junk2);
189 /* printf("%d %.2f %.2f %.2f\n", junk1, n[0], n[1], n[2]); */
190 nodes_cart[i] = geod_to_cart(nodes_orig[i]);
191 /* printf("%d %.2f %.2f %.2f\n",
192 junk1, nodes_cart[i].x, nodes_cart[i].y, nodes_cart[i].z); */
195 xmin = xmax = nodes_orig[i][0];
196 ymin = ymax = nodes_orig[i][1];
198 if ( nodes_orig[i][0] < xmin ) {
199 xmin = nodes_orig[i][0];
201 if ( nodes_orig[i][0] > xmax ) {
202 xmax = nodes_orig[i][0];
204 if ( nodes_orig[i][1] < ymin ) {
205 ymin = nodes_orig[i][1];
207 if ( nodes_orig[i][1] > ymax ) {
208 ymax = nodes_orig[i][1];
215 printf("Loading element file: %s ...\n", elename);
216 if ( (ele = fopen(elename, "r")) == NULL ) {
217 printf("Cannot open file '%s'\n", elename);
221 fscanf(ele, "%d %d %d", &tricount, &junk1, &junk2);
223 if ( tricount > MAX_TRIS - 1 ) {
224 printf("Error, too many elements, need to increase array size\n");
227 printf(" Expecting %d elements\n", tricount);
230 for ( i = 1; i <= tricount; i++ ) {
231 fscanf(ele, "%d %d %d %d\n", &junk1,
232 &tris[i][0], &tris[i][1], &tris[i][2]);
233 /* printf("%d %d %d %d\n", junk1, tris[i][0], tris[i][1], tris[i][2]);*/
240 /* check if a file exists */
241 int file_exists(char *file) {
242 struct stat stat_buf;
245 printf("checking %s ... ", file);
247 result = stat(file, &stat_buf);
250 /* stat failed, no file */
251 printf("not found.\n");
254 /* stat succeeded, file exists */
261 /* check to see if a shared object exists */
262 int shared_object_exists(char *basepath, char *ext) {
263 char file[256], scene_path[256];
266 if ( strcmp(ext, ".sw") == 0 ) {
267 fgBucketGenBasePath(&west_index, scene_path);
268 index = fgBucketGenIndex(&west_index);
269 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
270 if ( file_exists(file) ) {
273 fgBucketGenBasePath(&sw_index, scene_path);
274 index = fgBucketGenIndex(&sw_index);
275 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
276 if ( file_exists(file) ) {
279 fgBucketGenBasePath(&south_index, scene_path);
280 index = fgBucketGenIndex(&south_index);
281 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
282 if ( file_exists(file) ) {
287 if ( strcmp(ext, ".se") == 0 ) {
288 fgBucketGenBasePath(&east_index, scene_path);
289 index = fgBucketGenIndex(&east_index);
290 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
291 if ( file_exists(file) ) {
294 fgBucketGenBasePath(&se_index, scene_path);
295 index = fgBucketGenIndex(&se_index);
296 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
297 if ( file_exists(file) ) {
300 fgBucketGenBasePath(&south_index, scene_path);
301 index = fgBucketGenIndex(&south_index);
302 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
303 if ( file_exists(file) ) {
308 if ( strcmp(ext, ".ne") == 0 ) {
309 fgBucketGenBasePath(&east_index, scene_path);
310 index = fgBucketGenIndex(&east_index);
311 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
312 if ( file_exists(file) ) {
315 fgBucketGenBasePath(&ne_index, scene_path);
316 index = fgBucketGenIndex(&ne_index);
317 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
318 if ( file_exists(file) ) {
321 fgBucketGenBasePath(&north_index, scene_path);
322 index = fgBucketGenIndex(&north_index);
323 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
324 if ( file_exists(file) ) {
329 if ( strcmp(ext, ".nw") == 0 ) {
330 fgBucketGenBasePath(&west_index, scene_path);
331 index = fgBucketGenIndex(&west_index);
332 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
333 if ( file_exists(file) ) {
336 fgBucketGenBasePath(&nw_index, scene_path);
337 index = fgBucketGenIndex(&nw_index);
338 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
339 if ( file_exists(file) ) {
342 fgBucketGenBasePath(&north_index, scene_path);
343 index = fgBucketGenIndex(&north_index);
344 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
345 if ( file_exists(file) ) {
350 if ( strcmp(ext, ".south") == 0 ) {
351 fgBucketGenBasePath(&south_index, scene_path);
352 index = fgBucketGenIndex(&south_index);
353 sprintf(file, "%s/%s/%ld.1.north", basepath, scene_path, index);
354 if ( file_exists(file) ) {
359 if ( strcmp(ext, ".north") == 0 ) {
360 fgBucketGenBasePath(&north_index, scene_path);
361 index = fgBucketGenIndex(&north_index);
362 sprintf(file, "%s/%s/%ld.1.south", basepath, scene_path, index);
363 if ( file_exists(file) ) {
368 if ( strcmp(ext, ".west") == 0 ) {
369 fgBucketGenBasePath(&west_index, scene_path);
370 index = fgBucketGenIndex(&west_index);
371 sprintf(file, "%s/%s/%ld.1.east", basepath, scene_path, index);
372 if ( file_exists(file) ) {
377 if ( strcmp(ext, ".east") == 0 ) {
378 fgBucketGenBasePath(&east_index, scene_path);
379 index = fgBucketGenIndex(&east_index);
380 sprintf(file, "%s/%s/%ld.1.west", basepath, scene_path, index);
381 if ( file_exists(file) ) {
390 /* my custom file opening routine ... don't open if a shared edge or
391 * vertex alread exists */
392 FILE *my_open(char *basename, char *basepath, char *ext) {
396 /* create the output file name */
397 strcpy(filename, basename);
398 strcat(filename, ext);
400 /* check if a shared object already exist from a different tile */
402 if ( shared_object_exists(basepath, ext) ) {
403 /* not an actual file open error, but we've already got the
404 * shared edge, so we don't want to create another one */
405 printf("not opening\n");
409 fp = fopen(filename, "w");
410 printf("Opening %s\n", filename);
416 /* dump in WaveFront .obj format */
417 void dump_obj(char *basename, char *basepath) {
418 double n1[3], n2[3], n3[3], n4[3], n5[3], norm[3], temp;
419 FILE *fp, *sw, *se, *ne, *nw, *north, *south, *east, *west, *body;
420 int i, t1, t2, t3, t4, t5, count;
422 sw = my_open(basename, basepath, ".sw");
423 se = my_open(basename, basepath, ".se");
424 ne = my_open(basename, basepath, ".ne");
425 nw = my_open(basename, basepath, ".nw");
427 north = my_open(basename, basepath, ".north");
428 south = my_open(basename, basepath, ".south");
429 east = my_open(basename, basepath, ".east");
430 west = my_open(basename, basepath, ".west");
432 body = my_open(basename, basepath, ".body");
434 printf("Dumping edges file basename: %s ...\n", basename);
437 printf(" writing vertices\n");
438 for ( i = 1; i <= nodecount; i++ ) {
440 if ( (fabs(nodes_orig[i][1] - ymin) < FG_EPSILON) &&
441 (fabs(nodes_orig[i][0] - xmin) < FG_EPSILON) ) {
443 } else if ( (fabs(nodes_orig[i][1] - ymin) < FG_EPSILON) &&
444 (fabs(nodes_orig[i][0] - xmax) < FG_EPSILON) ) {
446 } else if ( (fabs(nodes_orig[i][1] - ymax) < FG_EPSILON) &&
447 (fabs(nodes_orig[i][0] - xmax) < FG_EPSILON)) {
449 } else if ( (fabs(nodes_orig[i][1] - ymax) < FG_EPSILON) &&
450 (fabs(nodes_orig[i][0] - xmin) < FG_EPSILON) ) {
452 } else if ( fabs(nodes_orig[i][0] - xmin) < FG_EPSILON ) {
454 } else if ( fabs(nodes_orig[i][0] - xmax) < FG_EPSILON ) {
456 } else if ( fabs(nodes_orig[i][1] - ymin) < FG_EPSILON ) {
458 } else if ( fabs(nodes_orig[i][1] - ymax) < FG_EPSILON ) {
465 fprintf(fp, "gdn %.2f %.2f %.2f\n",
466 nodes_orig[i][0], nodes_orig[i][1], nodes_orig[i][2]);
470 printf(" calculating and writing normals\n");
471 /* calculate and generate normals */
472 for ( i = 1; i <= nodecount; i++ ) {
473 /* printf("Finding normal\n"); */
475 find_tris(i, &t1, &t2, &t3, &t4, &t5);
477 n1[0] = n1[1] = n1[2] = 0.0;
478 n2[0] = n2[1] = n2[2] = 0.0;
479 n3[0] = n3[1] = n3[2] = 0.0;
480 n4[0] = n4[1] = n4[2] = 0.0;
481 n5[0] = n5[1] = n5[2] = 0.0;
484 calc_normal(nodes_cart[tris[t1][0]], nodes_cart[tris[t1][1]],
485 nodes_cart[tris[t1][2]], n1);
488 calc_normal(nodes_cart[tris[t2][0]], nodes_cart[tris[t2][1]],
489 nodes_cart[tris[t2][2]], n2);
494 calc_normal(nodes_cart[tris[t3][0]], nodes_cart[tris[t3][1]],
495 nodes_cart[tris[t3][2]], n3);
500 calc_normal(nodes_cart[tris[t4][0]], nodes_cart[tris[t4][1]],
501 nodes_cart[tris[t4][2]], n4);
506 calc_normal(nodes_cart[tris[t5][0]], nodes_cart[tris[t5][1]],
507 nodes_cart[tris[t5][2]], n5);
511 /* printf(" norm[2] = %.2f %.2f %.2f\n", n1[2], n2[2], n3[2]); */
513 norm[0] = ( n1[0] + n2[0] + n3[0] + n4[0] + n5[0] ) / (double)count;
514 norm[1] = ( n1[1] + n2[1] + n3[1] + n4[1] + n5[1] ) / (double)count;
515 norm[2] = ( n1[2] + n2[2] + n3[2] + n4[2] + n5[2] ) / (double)count;
517 /* printf(" count = %d\n", count); */
518 /* printf(" Ave. normal = %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);*/
519 MAT3_NORMALIZE_VEC(norm, temp);
520 /* printf(" Normalized ave. normal = %.4f %.4f %.4f\n", */
521 /* norm[0], norm[1], norm[2]); */
525 if ( (fabs(nodes_orig[i][1] - ymin) < FG_EPSILON) &&
526 (fabs(nodes_orig[i][0] - xmin) < FG_EPSILON) ) {
528 } else if ( (fabs(nodes_orig[i][1] - ymin) < FG_EPSILON) &&
529 (fabs(nodes_orig[i][0] - xmax) < FG_EPSILON) ) {
531 } else if ( (fabs(nodes_orig[i][1] - ymax) < FG_EPSILON) &&
532 (fabs(nodes_orig[i][0] - xmax) < FG_EPSILON)) {
534 } else if ( (fabs(nodes_orig[i][1] - ymax) < FG_EPSILON) &&
535 (fabs(nodes_orig[i][0] - xmin) < FG_EPSILON) ) {
537 } else if ( fabs(nodes_orig[i][0] - xmin) < FG_EPSILON ) {
539 } else if ( fabs(nodes_orig[i][0] - xmax) < FG_EPSILON ) {
541 } else if ( fabs(nodes_orig[i][1] - ymin) < FG_EPSILON ) {
543 } else if ( fabs(nodes_orig[i][1] - ymax) < FG_EPSILON ) {
547 fprintf(fp, "vn %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);
551 if ( sw ) { fclose(sw); }
552 if ( se ) { fclose(se); }
553 if ( ne ) { fclose(ne); }
554 if ( nw ) { fclose(nw); }
556 if ( north ) { fclose(north); }
557 if ( south ) { fclose(south); }
558 if ( east ) { fclose(east); }
559 if ( west ) { fclose(west); }
561 if ( body ) { fclose(body); }
565 int main(int argc, char **argv) {
566 char basename[256], basepath[256], temp[256];
571 strcpy(basename, argv[1]);
573 /* find the base path of the file */
574 extract_path(basename, basepath);
575 extract_path(basepath, basepath);
576 extract_path(basepath, basepath);
577 printf("%s\n", basepath);
579 /* find the index of the current file */
580 extract_file(basename, temp);
586 printf("%ld\n", index);
587 fgBucketParseIndex(index, &p);
589 printf("bucket = %d %d %d %d\n", p.lon, p.lat, p.x, p.y);
590 /* generate the indexes of the neighbors */
591 fgBucketOffset(&p, &ne_index, 1, 1);
592 fgBucketOffset(&p, &nw_index, -1, 1);
593 fgBucketOffset(&p, &se_index, 1, -1);
594 fgBucketOffset(&p, &sw_index, -1, -1);
596 fgBucketOffset(&p, &north_index, 0, 1);
597 fgBucketOffset(&p, &south_index, 0, -1);
598 fgBucketOffset(&p, &east_index, 1, 0);
599 fgBucketOffset(&p, &west_index, -1, 0);
602 printf("Corner indexes = %ld %ld %ld %ld\n",
603 ne_index, nw_index, sw_index, se_index);
604 printf("Edge indexes = %ld %ld %ld %ld\n",
605 north_index, south_index, east_index, west_index);
608 /* load the input data files */
611 /* dump in WaveFront .obj format */
612 dump_obj(basename, basepath);
619 /* Revision 1.11 1998/07/04 00:56:40 curt
620 /* typedef'd struct fgBUCKET.
622 * Revision 1.10 1998/05/02 01:54:37 curt
623 * Converting to polar3d.h routines.
625 * Revision 1.9 1998/04/18 04:01:20 curt
626 * Now use libMath rather than having local copies of math routines.
628 * Revision 1.8 1998/04/14 02:26:08 curt
629 * Code reorganizations. Added a Lib/ directory for more general libraries.
631 * Revision 1.7 1998/04/08 23:21:13 curt
632 * Adopted Gnu automake/autoconf system.
634 * Revision 1.6 1998/03/03 15:36:13 curt
635 * Tweaks for compiling with g++
637 * Revision 1.5 1998/03/03 03:37:04 curt
640 * Revision 1.4 1998/01/31 00:41:26 curt
641 * Made a few changes converting floats to doubles.
643 * Revision 1.3 1998/01/27 18:37:04 curt
644 * Lots of updates to get back in sync with changes made over in .../Src/
646 * Revision 1.2 1998/01/14 15:54:43 curt
647 * Initial revision completed.
649 * Revision 1.1 1998/01/14 02:11:31 curt