1 // splittris.cxx -- 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.hxx"
37 #include <Include/fg_constants.h>
38 #include <Bucket/bucketutils.h>
39 #include <Math/fg_geodesy.hxx>
40 #include <Math/mat3.h>
41 #include <Math/point3d.hxx>
42 #include <Math/polar3d.hxx>
43 #include <Misc/fgstream.hxx>
45 // int nodecount, tricount;
46 double xmin, xmax, ymin, ymax;
48 // static double nodes_orig[MAX_NODES][3];
49 // static Point3D nodes_cart[MAX_NODES];
50 // static int tris[MAX_TRIS][3];
52 container_3d nodes_orig;
53 container_3d nodes_cart;
54 container_tri tri_list;
56 fgBUCKET ne_index, nw_index, sw_index, se_index;
57 fgBUCKET north_index, south_index, east_index, west_index;
60 // given three points defining a triangle, calculate the normal
61 void calc_normal(const Point3D& p1, const Point3D& p2,
62 const Point3D& p3, double normal[3])
67 v1[0] = p2.x() - p1.x(); v1[1] = p2.y() - p1.y(); v1[2] = p2.z() - p1.z();
68 v2[0] = p3.x() - p1.x(); v2[1] = p3.y() - p1.y(); v2[2] = p3.z() - p1.z();
70 MAT3cross_product(normal, v1, v2);
71 MAT3_NORMALIZE_VEC(normal,temp);
73 // printf(" Normal = %.2f %.2f %.2f\n", normal[0], normal[1], normal[2]);
77 // return the file base name ( foo/bar/file.ext = file.ext )
78 string extract_file(const string& input) {
81 pos = input.rfind("/");
84 return input.substr(pos);
88 // return the file path name ( foo/bar/file.ext = foo/bar )
89 string extract_path(const string& input) {
92 pos = input.rfind("/");
94 return input.substr(0, pos);
98 // return the index of all triangles containing the specified node
99 void find_tris(int n, int *t1, int *t2, int *t3, int *t4, int *t5) {
102 *t1 = *t2 = *t3 = *t4 = *t5 = 0;
105 iterator_tri last = tri_list.end();
106 iterator_tri current = tri_list.begin();
108 // skip first null record
111 for ( ; current != last; ++current )
113 if ( (n == (*current).n1) || (n == (*current).n2) ||
114 (n == (*current).n3) )
118 } else if ( *t2 == 0 ) {
120 } else if ( *t3 == 0 ) {
122 } else if ( *t4 == 0 ) {
133 // Initialize a new mesh structure
134 void triload(const string& basename) {
135 string nodename, elename;
136 Point3D node1, node2;
138 int nodecount, tricount, dim, junk1, junk2;
141 nodename = basename + ".node";
142 elename = basename + ".ele";
144 cout << "Loading node file: " + nodename + " ...\n";
146 fg_gzifstream node_in( nodename );
148 cout << "Cannot open file " + nodename + "\n";
152 // the triangle program starts counting at 1 by default which is
153 // pretty obnoxious. Let's just push null record zero's onto our
154 // list to compensate
155 nodes_orig.push_back(node1);
156 nodes_cart.push_back(node1);
157 tri_list.push_back(tri);
159 node_in.stream() >> nodecount >> dim >> junk1 >> junk2;
160 cout << " Expecting " << nodecount << " nodes\n";
162 for ( i = 1; i <= nodecount; i++ ) {
163 node_in.stream() >> junk1 >> node1 >> junk2;
164 nodes_orig.push_back(node1);
165 // printf("%d %.2f %.2f %.2f\n", junk1, node1.x, node1.y, node1.z);
167 node2 = fgGeodToCart(node1);
168 nodes_cart.push_back(node2);
169 // printf("%d %.2f %.2f %.2f\n", junk1, node2.x, node2.y, node2.z);
172 xmin = xmax = node1.x();
173 ymin = ymax = node1.y();
175 if ( node1.x() < xmin ) {
178 if ( node1.x() > xmax ) {
181 if ( node1.y() < ymin ) {
184 if ( node1.y() > ymax ) {
190 cout << "Loading element file: " + elename + " ...\n";
191 fg_gzifstream ele_in( elename );
193 cout << "Cannot open file " + elename + "\n";
197 ele_in.stream() >> tricount >> junk1 >> junk2;
198 cout << " Expecting " << tricount << " elements\n";
200 for ( i = 1; i <= tricount; i++ ) {
201 // fscanf(ele_file, "%d %d %d %d\n", &junk1,
202 // &(tri.n1), &(tri.n2), &(tri.n3));
203 ele_in.stream() >> junk1 >> tri.n1 >> tri.n2 >> tri.n3;
204 // printf("%d %d %d %d\n", junk1, tri.n1, tri.n2, tri.n3);
205 tri_list.push_back(tri);
210 // check if a file exists
211 int file_exists(char *file) {
212 struct stat stat_buf;
215 cout << "checking " << file << " ... ";
217 result = stat(file, &stat_buf);
220 // stat failed, no file
221 cout << "not found.\n";
224 // stat succeeded, file exists
231 // check to see if a shared object exists
232 int shared_object_exists(const char *basepath, const string& ext) {
233 char file[256], scene_path[256];
236 if ( ext == ".sw" ) {
237 fgBucketGenBasePath(&west_index, scene_path);
238 index = fgBucketGenIndex(&west_index);
239 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
240 if ( file_exists(file) ) {
243 fgBucketGenBasePath(&sw_index, scene_path);
244 index = fgBucketGenIndex(&sw_index);
245 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
246 if ( file_exists(file) ) {
249 fgBucketGenBasePath(&south_index, scene_path);
250 index = fgBucketGenIndex(&south_index);
251 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
252 if ( file_exists(file) ) {
257 if ( ext == ".se" ) {
258 fgBucketGenBasePath(&east_index, scene_path);
259 index = fgBucketGenIndex(&east_index);
260 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
261 if ( file_exists(file) ) {
264 fgBucketGenBasePath(&se_index, scene_path);
265 index = fgBucketGenIndex(&se_index);
266 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
267 if ( file_exists(file) ) {
270 fgBucketGenBasePath(&south_index, scene_path);
271 index = fgBucketGenIndex(&south_index);
272 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
273 if ( file_exists(file) ) {
278 if ( ext == ".ne" ) {
279 fgBucketGenBasePath(&east_index, scene_path);
280 index = fgBucketGenIndex(&east_index);
281 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
282 if ( file_exists(file) ) {
285 fgBucketGenBasePath(&ne_index, scene_path);
286 index = fgBucketGenIndex(&ne_index);
287 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
288 if ( file_exists(file) ) {
291 fgBucketGenBasePath(&north_index, scene_path);
292 index = fgBucketGenIndex(&north_index);
293 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
294 if ( file_exists(file) ) {
299 if ( ext == ".nw" ) {
300 fgBucketGenBasePath(&west_index, scene_path);
301 index = fgBucketGenIndex(&west_index);
302 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
303 if ( file_exists(file) ) {
306 fgBucketGenBasePath(&nw_index, scene_path);
307 index = fgBucketGenIndex(&nw_index);
308 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
309 if ( file_exists(file) ) {
312 fgBucketGenBasePath(&north_index, scene_path);
313 index = fgBucketGenIndex(&north_index);
314 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
315 if ( file_exists(file) ) {
320 if ( ext == ".south" ) {
321 fgBucketGenBasePath(&south_index, scene_path);
322 index = fgBucketGenIndex(&south_index);
323 sprintf(file, "%s/%s/%ld.1.north", basepath, scene_path, index);
324 if ( file_exists(file) ) {
329 if ( ext == ".north" ) {
330 fgBucketGenBasePath(&north_index, scene_path);
331 index = fgBucketGenIndex(&north_index);
332 sprintf(file, "%s/%s/%ld.1.south", basepath, scene_path, index);
333 if ( file_exists(file) ) {
338 if ( ext == ".west" ) {
339 fgBucketGenBasePath(&west_index, scene_path);
340 index = fgBucketGenIndex(&west_index);
341 sprintf(file, "%s/%s/%ld.1.east", basepath, scene_path, index);
342 if ( file_exists(file) ) {
347 if ( ext == ".east" ) {
348 fgBucketGenBasePath(&east_index, scene_path);
349 index = fgBucketGenIndex(&east_index);
350 sprintf(file, "%s/%s/%ld.1.west", basepath, scene_path, index);
351 if ( file_exists(file) ) {
360 // my custom file opening routine ... don't open if a shared edge or
361 // vertex alread exists
362 FILE *my_open(const string& basename, const string& basepath,
368 // create the output file name
369 filename = basename + ext;
371 // check if a shared object already exist from a different tile
373 if ( shared_object_exists(basepath.c_str(), ext) ) {
374 // not an actual file open error, but we've already got the
375 // shared edge, so we don't want to create another one
376 cout << "not opening\n";
380 fp = fopen(filename.c_str(), "w");
381 cout << "Opening " + filename + "\n";
387 // dump in WaveFront .obj format
388 void dump_obj(const string& basename, const string& basepath) {
390 double n1[3], n2[3], n3[3], n4[3], n5[3], norm[3], temp;
391 FILE *fp, *sw, *se, *ne, *nw, *north, *south, *east, *west, *body;
392 int i, t1, t2, t3, t4, t5, count, size;
395 sw = my_open(basename, basepath, ".sw");
396 se = my_open(basename, basepath, ".se");
397 ne = my_open(basename, basepath, ".ne");
398 nw = my_open(basename, basepath, ".nw");
400 north = my_open(basename, basepath, ".north");
401 south = my_open(basename, basepath, ".south");
402 east = my_open(basename, basepath, ".east");
403 west = my_open(basename, basepath, ".west");
405 body = my_open(basename, basepath, ".body");
407 cout << "Dumping edges file basename: " + basename + " ...\n";
410 cout << " writing vertices\n";
412 iterator_3d last = nodes_orig.end();
413 iterator_3d current = nodes_orig.begin();
415 for ( ; current != last; ++current) {
418 if ( (fabs(node.y() - ymin) < FG_EPSILON) &&
419 (fabs(node.x() - xmin) < FG_EPSILON) ) {
421 } else if ( (fabs(node.y() - ymin) < FG_EPSILON) &&
422 (fabs(node.x() - xmax) < FG_EPSILON) ) {
424 } else if ( (fabs(node.y() - ymax) < FG_EPSILON) &&
425 (fabs(node.x() - xmax) < FG_EPSILON)) {
427 } else if ( (fabs(node.y() - ymax) < FG_EPSILON) &&
428 (fabs(node.x() - xmin) < FG_EPSILON) ) {
430 } else if ( fabs(node.x() - xmin) < FG_EPSILON ) {
432 } else if ( fabs(node.x() - xmax) < FG_EPSILON ) {
434 } else if ( fabs(node.y() - ymin) < FG_EPSILON ) {
436 } else if ( fabs(node.y() - ymax) < FG_EPSILON ) {
447 fprintf(fp, "gdn %.2f %.2f %.2f\n", x, y, z);
451 cout << " calculating and writing normals\n";
453 // calculate and generate normals
454 size = nodes_orig.size();
455 for ( i = 1; i < size; i++ ) {
456 // printf("Finding normal\n");
458 find_tris(i, &t1, &t2, &t3, &t4, &t5);
460 n1[0] = n1[1] = n1[2] = 0.0;
461 n2[0] = n2[1] = n2[2] = 0.0;
462 n3[0] = n3[1] = n3[2] = 0.0;
463 n4[0] = n4[1] = n4[2] = 0.0;
464 n5[0] = n5[1] = n5[2] = 0.0;
467 calc_normal(nodes_cart[tri_list[t1].n1],
468 nodes_cart[tri_list[t1].n2],
469 nodes_cart[tri_list[t1].n3],
473 calc_normal(nodes_cart[tri_list[t2].n1],
474 nodes_cart[tri_list[t2].n2],
475 nodes_cart[tri_list[t2].n3],
481 calc_normal(nodes_cart[tri_list[t3].n1],
482 nodes_cart[tri_list[t3].n2],
483 nodes_cart[tri_list[t3].n3],
489 calc_normal(nodes_cart[tri_list[t4].n1],
490 nodes_cart[tri_list[t4].n2],
491 nodes_cart[tri_list[t4].n3],
497 calc_normal(nodes_cart[tri_list[t5].n1],
498 nodes_cart[tri_list[t5].n2],
499 nodes_cart[tri_list[t5].n3],
504 // printf(" norm[2] = %.2f %.2f %.2f\n", n1[2], n2[2], n3[2]);
506 norm[0] = ( n1[0] + n2[0] + n3[0] + n4[0] + n5[0] ) / (double)count;
507 norm[1] = ( n1[1] + n2[1] + n3[1] + n4[1] + n5[1] ) / (double)count;
508 norm[2] = ( n1[2] + n2[2] + n3[2] + n4[2] + n5[2] ) / (double)count;
510 // printf(" count = %d\n", count);
511 // printf(" Ave. normal = %.4f %.4f %.4f\n", norm[0], norm[1],
513 MAT3_NORMALIZE_VEC(norm, temp);
514 // printf(" Normalized ave. normal = %.4f %.4f %.4f\n",
515 // norm[0], norm[1], norm[2]);
519 if ( (fabs(nodes_orig[i].y() - ymin) < FG_EPSILON) &&
520 (fabs(nodes_orig[i].x() - xmin) < FG_EPSILON) ) {
522 } else if ( (fabs(nodes_orig[i].y() - ymin) < FG_EPSILON) &&
523 (fabs(nodes_orig[i].x() - xmax) < FG_EPSILON) ) {
525 } else if ( (fabs(nodes_orig[i].y() - ymax) < FG_EPSILON) &&
526 (fabs(nodes_orig[i].x() - xmax) < FG_EPSILON)) {
528 } else if ( (fabs(nodes_orig[i].y() - ymax) < FG_EPSILON) &&
529 (fabs(nodes_orig[i].x() - xmin) < FG_EPSILON) ) {
531 } else if ( fabs(nodes_orig[i].x() - xmin) < FG_EPSILON ) {
533 } else if ( fabs(nodes_orig[i].x() - xmax) < FG_EPSILON ) {
535 } else if ( fabs(nodes_orig[i].y() - ymin) < FG_EPSILON ) {
537 } else if ( fabs(nodes_orig[i].y() - ymax) < FG_EPSILON ) {
541 fprintf(fp, "vn %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);
545 if ( sw ) { fclose(sw); }
546 if ( se ) { fclose(se); }
547 if ( ne ) { fclose(ne); }
548 if ( nw ) { fclose(nw); }
550 if ( north ) { fclose(north); }
551 if ( south ) { fclose(south); }
552 if ( east ) { fclose(east); }
553 if ( west ) { fclose(west); }
555 if ( body ) { fclose(body); }
559 int main(int argc, char **argv) {
560 string basename, basepath, temp;
567 // find the base path of the file
568 basepath = extract_path(basename);
569 basepath = extract_path(basepath);
570 basepath = extract_path(basepath);
571 cout << "basepath = " + basepath + "\n";
573 // find the index of the current file
574 temp = extract_file(basename);
577 temp = temp.substr(0, len-2);
579 index = atoi( temp.c_str() );
580 cout << "index = " << index << "\n";
581 fgBucketParseIndex(index, &p);
583 cout << "bucket = " << p.lon << " " << p.lat << " " <<
584 p.x << " " << p.y << "\n";
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);
597 // printf("Corner indexes = %ld %ld %ld %ld\n",
598 // ne_index, nw_index, sw_index, se_index);
599 // printf("Edge indexes = %ld %ld %ld %ld\n",
600 // north_index, south_index, east_index, west_index);
603 // load the input data files
606 // dump in WaveFront .obj format
607 dump_obj(basename, basepath);
614 // Revision 1.5 1998/10/20 15:50:33 curt
617 // Revision 1.4 1998/10/18 01:17:27 curt
620 // Revision 1.3 1998/09/22 23:49:56 curt
621 // C++-ified, STL-ified, and string-ified.
623 // Revision 1.2 1998/09/21 23:16:23 curt
624 // Converted to c++ style comments.
626 // Revision 1.1 1998/07/08 14:59:13 curt
627 // *.[ch] renamed to *.[ch]xx
629 // Revision 1.11 1998/07/04 00:56:40 curt
630 // typedef'd struct fgBUCKET.
632 // Revision 1.10 1998/05/02 01:54:37 curt
633 // Converting to polar3d.h routines.
635 // Revision 1.9 1998/04/18 04:01:20 curt
636 // Now use libMath rather than having local copies of math routines.
638 // Revision 1.8 1998/04/14 02:26:08 curt
639 // Code reorganizations. Added a Lib/ directory for more general libraries.
641 // Revision 1.7 1998/04/08 23:21:13 curt
642 // Adopted Gnu automake/autoconf system.
644 // Revision 1.6 1998/03/03 15:36:13 curt
645 // Tweaks for compiling with g++
647 // Revision 1.5 1998/03/03 03:37:04 curt
648 // Cumulative tweaks.
650 // Revision 1.4 1998/01/31 00:41:26 curt
651 // Made a few changes converting floats to doubles.
653 // Revision 1.3 1998/01/27 18:37:04 curt
654 // Lots of updates to get back in sync with changes made over in .../Src/
656 // Revision 1.2 1998/01/14 15:54:43 curt
657 // Initial revision completed.
659 // Revision 1.1 1998/01/14 02:11:31 curt