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 <Math/fg_geodesy.h>
40 #include <Math/mat3.h>
41 #include <Math/polar.h>
42 #include <Scenery/bucketutils.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 struct fgCartesianPoint nodes_cart[MAX_NODES];
54 struct fgBUCKET ne_index, nw_index, sw_index, se_index;
55 struct 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 struct fgCartesianPoint geod_to_cart(double geod[3]) {
60 struct fgCartesianPoint p;
61 double gc_lon, gc_lat, sl_radius;
63 /* printf("A geodetic point is (%.2f, %.2f, %.2f)\n",
64 geod[0], geod[1], geod[2]); */
66 gc_lon = geod[0]*ARCSEC_TO_RAD;
67 fgGeodToGeoc(geod[1]*ARCSEC_TO_RAD, geod[2], &sl_radius, &gc_lat);
69 /* printf("A geocentric point is (%.2f, %.2f, %.2f)\n", gc_lon,
70 gc_lat, sl_radius+geod[2]); */
72 p = fgPolarToCart(gc_lon, gc_lat, sl_radius+geod[2]);
74 /* printf("A cart point is (%.8f, %.8f, %.8f)\n", p.x, p.y, p.z); */
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])
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;
90 MAT3cross_product(normal, v1, v2);
91 MAT3_NORMALIZE_VEC(normal,temp);
93 /* printf(" Normal = %.2f %.2f %.2f\n", normal[0], normal[1], normal[2]); */
97 /* return the file base name ( foo/bar/file.ext = file.ext ) */
98 void extract_file(char *in, char *base) {
104 while ( (i >= 0) && (in[i] != '/') ) {
113 /* return the file path name ( foo/bar/file.ext = foo/bar ) */
114 void extract_path(char *in, char *base) {
121 while ( (i >= 0) && (in[i] != '/') ) {
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) {
133 *t1 = *t2 = *t3 = *t4 = *t5 = 0;
136 while ( i <= tricount ) {
137 if ( (n == tris[i][0]) || (n == tris[i][1]) || (n == tris[i][2]) ) {
140 } else if ( *t2 == 0 ) {
142 } else if ( *t3 == 0 ) {
144 } else if ( *t4 == 0 ) {
155 /* Initialize a new mesh structure */
156 void triload(char *basename) {
157 char nodename[256], elename[256];
159 int dim, junk1, junk2;
162 strcpy(nodename, basename);
163 strcat(nodename, ".node");
164 strcpy(elename, basename);
165 strcat(elename, ".ele");
167 printf("Loading node file: %s ...\n", nodename);
168 if ( (node = fopen(nodename, "r")) == NULL ) {
169 printf("Cannot open file '%s'\n", nodename);
173 fscanf(node, "%d %d %d %d", &nodecount, &dim, &junk1, &junk2);
175 if ( nodecount > MAX_NODES - 1 ) {
176 printf("Error, too many nodes, need to increase array size\n");
179 printf(" Expecting %d nodes\n", nodecount);
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); */
191 xmin = xmax = nodes_orig[i][0];
192 ymin = ymax = nodes_orig[i][1];
194 if ( nodes_orig[i][0] < xmin ) {
195 xmin = nodes_orig[i][0];
197 if ( nodes_orig[i][0] > xmax ) {
198 xmax = nodes_orig[i][0];
200 if ( nodes_orig[i][1] < ymin ) {
201 ymin = nodes_orig[i][1];
203 if ( nodes_orig[i][1] > ymax ) {
204 ymax = nodes_orig[i][1];
211 printf("Loading element file: %s ...\n", elename);
212 if ( (ele = fopen(elename, "r")) == NULL ) {
213 printf("Cannot open file '%s'\n", elename);
217 fscanf(ele, "%d %d %d", &tricount, &junk1, &junk2);
219 if ( tricount > MAX_TRIS - 1 ) {
220 printf("Error, too many elements, need to increase array size\n");
223 printf(" Expecting %d elements\n", tricount);
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]);*/
236 /* check if a file exists */
237 int file_exists(char *file) {
238 struct stat stat_buf;
241 printf("checking %s ... ", file);
243 result = stat(file, &stat_buf);
246 /* stat failed, no file */
247 printf("not found.\n");
250 /* stat succeeded, file exists */
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];
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) ) {
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) ) {
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) ) {
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) ) {
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) ) {
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) ) {
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) ) {
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) ) {
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) ) {
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) ) {
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) ) {
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) ) {
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) ) {
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) ) {
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) ) {
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) ) {
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) {
392 /* create the output file name */
393 strcpy(filename, basename);
394 strcat(filename, ext);
396 /* check if a shared object already exist from a different tile */
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");
405 fp = fopen(filename, "w");
406 printf("Opening %s\n", filename);
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;
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");
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");
428 body = my_open(basename, basepath, ".body");
430 printf("Dumping edges file basename: %s ...\n", basename);
433 printf(" writing vertices\n");
434 for ( i = 1; i <= nodecount; i++ ) {
436 if ( (fabs(nodes_orig[i][1] - ymin) < FG_EPSILON) &&
437 (fabs(nodes_orig[i][0] - xmin) < FG_EPSILON) ) {
439 } else if ( (fabs(nodes_orig[i][1] - ymin) < FG_EPSILON) &&
440 (fabs(nodes_orig[i][0] - xmax) < FG_EPSILON) ) {
442 } else if ( (fabs(nodes_orig[i][1] - ymax) < FG_EPSILON) &&
443 (fabs(nodes_orig[i][0] - xmax) < FG_EPSILON)) {
445 } else if ( (fabs(nodes_orig[i][1] - ymax) < FG_EPSILON) &&
446 (fabs(nodes_orig[i][0] - xmin) < FG_EPSILON) ) {
448 } else if ( fabs(nodes_orig[i][0] - xmin) < FG_EPSILON ) {
450 } else if ( fabs(nodes_orig[i][0] - xmax) < FG_EPSILON ) {
452 } else if ( fabs(nodes_orig[i][1] - ymin) < FG_EPSILON ) {
454 } else if ( fabs(nodes_orig[i][1] - ymax) < FG_EPSILON ) {
461 fprintf(fp, "gdn %.2f %.2f %.2f\n",
462 nodes_orig[i][0], nodes_orig[i][1], nodes_orig[i][2]);
466 printf(" calculating and writing normals\n");
467 /* calculate and generate normals */
468 for ( i = 1; i <= nodecount; i++ ) {
469 /* printf("Finding normal\n"); */
471 find_tris(i, &t1, &t2, &t3, &t4, &t5);
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;
480 calc_normal(nodes_cart[tris[t1][0]], nodes_cart[tris[t1][1]],
481 nodes_cart[tris[t1][2]], n1);
484 calc_normal(nodes_cart[tris[t2][0]], nodes_cart[tris[t2][1]],
485 nodes_cart[tris[t2][2]], n2);
490 calc_normal(nodes_cart[tris[t3][0]], nodes_cart[tris[t3][1]],
491 nodes_cart[tris[t3][2]], n3);
496 calc_normal(nodes_cart[tris[t4][0]], nodes_cart[tris[t4][1]],
497 nodes_cart[tris[t4][2]], n4);
502 calc_normal(nodes_cart[tris[t5][0]], nodes_cart[tris[t5][1]],
503 nodes_cart[tris[t5][2]], n5);
507 /* printf(" norm[2] = %.2f %.2f %.2f\n", n1[2], n2[2], n3[2]); */
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;
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]); */
521 if ( (fabs(nodes_orig[i][1] - ymin) < FG_EPSILON) &&
522 (fabs(nodes_orig[i][0] - xmin) < FG_EPSILON) ) {
524 } else if ( (fabs(nodes_orig[i][1] - ymin) < FG_EPSILON) &&
525 (fabs(nodes_orig[i][0] - xmax) < FG_EPSILON) ) {
527 } else if ( (fabs(nodes_orig[i][1] - ymax) < FG_EPSILON) &&
528 (fabs(nodes_orig[i][0] - xmax) < FG_EPSILON)) {
530 } else if ( (fabs(nodes_orig[i][1] - ymax) < FG_EPSILON) &&
531 (fabs(nodes_orig[i][0] - xmin) < FG_EPSILON) ) {
533 } else if ( fabs(nodes_orig[i][0] - xmin) < FG_EPSILON ) {
535 } else if ( fabs(nodes_orig[i][0] - xmax) < FG_EPSILON ) {
537 } else if ( fabs(nodes_orig[i][1] - ymin) < FG_EPSILON ) {
539 } else if ( fabs(nodes_orig[i][1] - ymax) < FG_EPSILON ) {
543 fprintf(fp, "vn %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);
547 if ( sw ) { fclose(sw); }
548 if ( se ) { fclose(se); }
549 if ( ne ) { fclose(ne); }
550 if ( nw ) { fclose(nw); }
552 if ( north ) { fclose(north); }
553 if ( south ) { fclose(south); }
554 if ( east ) { fclose(east); }
555 if ( west ) { fclose(west); }
557 if ( body ) { fclose(body); }
561 int main(int argc, char **argv) {
562 char basename[256], basepath[256], temp[256];
567 strcpy(basename, argv[1]);
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);
575 /* find the index of the current file */
576 extract_file(basename, temp);
582 printf("%ld\n", index);
583 fgBucketParseIndex(index, &p);
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);
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);
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);
604 /* load the input data files */
607 /* dump in WaveFront .obj format */
608 dump_obj(basename, basepath);
615 /* Revision 1.6 1998/03/03 15:36:13 curt
616 /* Tweaks for compiling with g++
618 * Revision 1.5 1998/03/03 03:37:04 curt
621 * Revision 1.4 1998/01/31 00:41:26 curt
622 * Made a few changes converting floats to doubles.
624 * Revision 1.3 1998/01/27 18:37:04 curt
625 * Lots of updates to get back in sync with changes made over in .../Src/
627 * Revision 1.2 1998/01/14 15:54:43 curt
628 * Initial revision completed.
630 * Revision 1.1 1998/01/14 02:11:31 curt