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 "../../Src/Include/constants.h"
38 #include "../../Src/Include/types.h"
39 #include "../../Src/Math/fg_geodesy.h"
40 #include "../../Src/Math/mat3.h"
41 #include "../../Src/Math/polar.h"
42 #include "../../Src/Scenery/tileutils.h"
45 int nodecount, tricount;
46 double xmin, xmax, ymin, ymax;
48 double nodes_orig[MAX_NODES][3];
49 int tris[MAX_TRIS][3];
50 int new_tris[MAX_TRIS][3];
52 struct fgCartesianPoint nodes_cart[MAX_NODES];
54 long int ne_index, nw_index, sw_index, se_index;
55 long int 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;
240 result = stat(file, &stat_buf);
243 /* stat failed, no file */
246 /* stat succeeded, file exists */
252 /* my custom file opening routine ... don't open if a shared edge or
253 * vertex alread exists */
254 FILE *my_open(char *basename, char *basepath, char *ext) {
258 /* create the output file name */
259 strcpy(filename, basename);
260 strcpy(filename, ext);
262 /* check if a shared object already exist from a different tile */
265 /* not an actual file open error, but we've already got the
266 * shared edge, so we don't want to create another one */
270 fp = fopen(filename, "w");
276 /* dump in WaveFront .obj format */
277 void dump_obj(char *basename, char *basepath) {
278 char sw_name[256], se_name[256], ne_name[256], nw_name[256];
279 char north_name[256], south_name[256], east_name[256], west_name[256];
281 double n1[3], n2[3], n3[3], n4[3], n5[3], norm[3], temp;
282 FILE *sw, *se, *ne, *nw, *north, *south, *east, *west, *body;
283 int i, t1, t2, t3, t4, t5, count;
285 sw = my_open(basename, basepath, ".sw");
286 se = my_open(basename, basepath, ".se");
287 ne = my_open(basename, basepath, ".ne");
288 nw = my_open(basename, basepath, ".nw");
290 north = my_open(basename, basepath, ".north");
291 south = my_open(basename, basepath, ".south");
292 east = my_open(basename, basepath, ".east");
293 west = my_open(basename, basepath, ".west");
295 body = my_open(basename, basepath, ".body");
297 printf("Dumping edges file basename: %s ...\n", basename);
299 sw = fopen(sw_name, "w");
300 se = fopen(se_name, "w");
301 ne = fopen(ne_name, "w");
302 nw = fopen(nw_name, "w");
304 north = fopen(north_name, "w");
305 south = fopen(south_name, "w");
306 east = fopen(east_name, "w");
307 west = fopen(west_name, "w");
309 body = fopen(body_name, "w");
312 printf(" writing vertices\n");
313 for ( i = 1; i <= nodecount; i++ ) {
315 if ( (fabs(nodes_orig[i][1] - ymin) < FG_EPSILON) &&
316 (fabs(nodes_orig[i][0] - xmin) < FG_EPSILON) ) {
317 fprintf(sw, "geodn %.2f %.2f %.2f\n",
318 nodes_orig[i][0], nodes_orig[i][1], nodes_orig[i][2]);
319 } else if ( (fabs(nodes_orig[i][1] - ymin) < FG_EPSILON) &&
320 (fabs(nodes_orig[i][0] - xmax) < FG_EPSILON) ) {
321 fprintf(se, "geodn %.2f %.2f %.2f\n",
322 nodes_orig[i][0], nodes_orig[i][1], nodes_orig[i][2]);
323 } else if ( (fabs(nodes_orig[i][1] - ymax) < FG_EPSILON) &&
324 (fabs(nodes_orig[i][0] - xmax) < FG_EPSILON)) {
325 fprintf(ne, "geodn %.2f %.2f %.2f\n",
326 nodes_orig[i][0], nodes_orig[i][1], nodes_orig[i][2]);
327 } else if ( (fabs(nodes_orig[i][1] - ymax) < FG_EPSILON) &&
328 (fabs(nodes_orig[i][0] - xmin) < FG_EPSILON) ) {
329 fprintf(nw, "geodn %.2f %.2f %.2f\n",
330 nodes_orig[i][0], nodes_orig[i][1], nodes_orig[i][2]);
331 } else if ( fabs(nodes_orig[i][0] - xmin) < FG_EPSILON ) {
332 fprintf(west, "geodn %.2f %.2f %.2f\n",
333 nodes_orig[i][0], nodes_orig[i][1], nodes_orig[i][2]);
334 } else if ( fabs(nodes_orig[i][0] - xmax) < FG_EPSILON ) {
335 fprintf(east, "geodn %.2f %.2f %.2f\n",
336 nodes_orig[i][0], nodes_orig[i][1], nodes_orig[i][2]);
337 } else if ( fabs(nodes_orig[i][1] - ymin) < FG_EPSILON ) {
338 fprintf(south, "geodn %.2f %.2f %.2f\n",
339 nodes_orig[i][0], nodes_orig[i][1], nodes_orig[i][2]);
340 } else if ( fabs(nodes_orig[i][1] - ymax) < FG_EPSILON ) {
341 fprintf(north, "geodn %.2f %.2f %.2f\n",
342 nodes_orig[i][0], nodes_orig[i][1], nodes_orig[i][2]);
344 fprintf(body, "geodn %.2f %.2f %.2f\n",
345 nodes_orig[i][0], nodes_orig[i][1], nodes_orig[i][2]);
350 printf(" calculating and writing normals\n");
351 /* calculate and generate normals */
352 for ( i = 1; i <= nodecount; i++ ) {
353 /* printf("Finding normal\n"); */
355 find_tris(i, &t1, &t2, &t3, &t4, &t5);
357 n1[0] = n1[1] = n1[2] = 0.0;
358 n2[0] = n2[1] = n2[2] = 0.0;
359 n3[0] = n3[1] = n3[2] = 0.0;
360 n4[0] = n4[1] = n4[2] = 0.0;
361 n5[0] = n5[1] = n5[2] = 0.0;
364 calc_normal(nodes_cart[tris[t1][0]], nodes_cart[tris[t1][1]],
365 nodes_cart[tris[t1][2]], n1);
368 calc_normal(nodes_cart[tris[t2][0]], nodes_cart[tris[t2][1]],
369 nodes_cart[tris[t2][2]], n2);
374 calc_normal(nodes_cart[tris[t3][0]], nodes_cart[tris[t3][1]],
375 nodes_cart[tris[t3][2]], n3);
380 calc_normal(nodes_cart[tris[t4][0]], nodes_cart[tris[t4][1]],
381 nodes_cart[tris[t4][2]], n4);
386 calc_normal(nodes_cart[tris[t5][0]], nodes_cart[tris[t5][1]],
387 nodes_cart[tris[t5][2]], n5);
391 /* printf(" norm[2] = %.2f %.2f %.2f\n", n1[2], n2[2], n3[2]); */
393 norm[0] = ( n1[0] + n2[0] + n3[0] + n4[0] + n5[0] ) / (double)count;
394 norm[1] = ( n1[1] + n2[1] + n3[1] + n4[1] + n5[1] ) / (double)count;
395 norm[2] = ( n1[2] + n2[2] + n3[2] + n4[2] + n5[2] ) / (double)count;
397 /* printf(" count = %d\n", count); */
398 /* printf(" Ave. normal = %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);*/
399 MAT3_NORMALIZE_VEC(norm, temp);
400 /* printf(" Normalized ave. normal = %.4f %.4f %.4f\n", */
401 /* norm[0], norm[1], norm[2]); */
403 if ( (fabs(nodes_orig[i][1] - ymin) < FG_EPSILON) &&
404 (fabs(nodes_orig[i][0] - xmin) < FG_EPSILON) ) {
405 fprintf(sw, "vn %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);
406 } else if ( (fabs(nodes_orig[i][1] - ymin) < FG_EPSILON) &&
407 (fabs(nodes_orig[i][0] - xmax) < FG_EPSILON) ) {
408 fprintf(se, "vn %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);
409 } else if ( (fabs(nodes_orig[i][1] - ymax) < FG_EPSILON) &&
410 (fabs(nodes_orig[i][0] - xmax) < FG_EPSILON)) {
411 fprintf(ne, "vn %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);
412 } else if ( (fabs(nodes_orig[i][1] - ymax) < FG_EPSILON) &&
413 (fabs(nodes_orig[i][0] - xmin) < FG_EPSILON) ) {
414 fprintf(nw, "vn %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);
415 } else if ( fabs(nodes_orig[i][0] - xmin) < FG_EPSILON ) {
416 fprintf(west, "vn %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);
417 } else if ( fabs(nodes_orig[i][0] - xmax) < FG_EPSILON ) {
418 fprintf(east, "vn %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);
419 } else if ( fabs(nodes_orig[i][1] - ymin) < FG_EPSILON ) {
420 fprintf(south, "vn %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);
421 } else if ( fabs(nodes_orig[i][1] - ymax) < FG_EPSILON ) {
422 fprintf(north, "vn %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);
438 int main(int argc, char **argv) {
439 char basename[256], basepath[256], temp[256];
440 struct bucket p1, p2;
444 strcpy(basename, argv[1]);
446 /* find the base path of the file */
447 extract_path(basename, basepath);
448 extract_path(basepath, basepath);
449 extract_path(basepath, basepath);
450 printf("%s\n", basepath);
452 /* find the index of the current file */
453 extract_file(basename, temp);
459 printf("%ld\n", index);
460 parse_index(index, &p1);
462 /* generate the indexes of the neighbors */
463 offset_bucket(&p1, &p2, 1, 1); ne_index = gen_index(&p2);
464 offset_bucket(&p1, &p2, 1, -1); nw_index = gen_index(&p2);
465 offset_bucket(&p1, &p2, -1, 1); se_index = gen_index(&p2);
466 offset_bucket(&p1, &p2, -1, -1); sw_index = gen_index(&p2);
468 offset_bucket(&p1, &p2, 0, 1); north_index = gen_index(&p2);
469 offset_bucket(&p1, &p2, 0, -1); south_index = gen_index(&p2);
470 offset_bucket(&p1, &p2, 1, 0); east_index = gen_index(&p2);
471 offset_bucket(&p1, &p2, -1, 1); west_index = gen_index(&p2);
473 printf("Corner indexes = %ld %ld %ld %ld\n",
474 ne_index, nw_index, sw_index, se_index);
475 printf("Edge indexes = %ld %ld %ld %ld\n",
476 north_index, south_index, east_index, west_index);
478 /* load the input data files */
481 /* dump in WaveFront .obj format */
482 dump_obj(basename, basepath);
489 /* Revision 1.1 1998/01/14 02:11:31 curt