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, p;
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 >> nodecount >> dim >> junk1 >> junk2;
160 cout << " Expecting " << nodecount << " nodes\n";
162 for ( i = 1; i <= nodecount; i++ ) {
163 node_in >> junk1 >> node1 >> junk2;
164 nodes_orig.push_back(node1);
165 // printf("%d %.2f %.2f %.2f\n", junk1, node1.x, node1.y, node1.z);
167 // convert to radians (before we can convert to cartesian)
168 p = Point3D( node1.x() * ARCSEC_TO_RAD,
169 node1.y() * ARCSEC_TO_RAD,
172 node2 = fgGeodToCart(p);
173 nodes_cart.push_back(node2);
174 // printf("%d %.2f %.2f %.2f\n", junk1, node2.x, node2.y, node2.z);
177 xmin = xmax = node1.x();
178 ymin = ymax = node1.y();
180 if ( node1.x() < xmin ) {
183 if ( node1.x() > xmax ) {
186 if ( node1.y() < ymin ) {
189 if ( node1.y() > ymax ) {
195 cout << "Loading element file: " + elename + " ...\n";
196 fg_gzifstream ele_in( elename );
198 cout << "Cannot open file " + elename + "\n";
202 ele_in >> tricount >> junk1 >> junk2;
203 cout << " Expecting " << tricount << " elements\n";
205 for ( i = 1; i <= tricount; i++ ) {
206 // fscanf(ele_file, "%d %d %d %d\n", &junk1,
207 // &(tri.n1), &(tri.n2), &(tri.n3));
208 ele_in >> junk1 >> tri.n1 >> tri.n2 >> tri.n3;
209 // printf("%d %d %d %d\n", junk1, tri.n1, tri.n2, tri.n3);
210 tri_list.push_back(tri);
215 // check if a file exists
216 int file_exists(char *file) {
217 struct stat stat_buf;
220 cout << "checking " << file << " ... ";
222 result = stat(file, &stat_buf);
225 // stat failed, no file
226 cout << "not found.\n";
229 // stat succeeded, file exists
236 // check to see if a shared object exists
237 int shared_object_exists(const char *basepath, const string& ext) {
238 char file[256], scene_path[256];
241 if ( ext == ".sw" ) {
242 fgBucketGenBasePath(&west_index, scene_path);
243 index = fgBucketGenIndex(&west_index);
244 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
245 if ( file_exists(file) ) {
248 fgBucketGenBasePath(&sw_index, scene_path);
249 index = fgBucketGenIndex(&sw_index);
250 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
251 if ( file_exists(file) ) {
254 fgBucketGenBasePath(&south_index, scene_path);
255 index = fgBucketGenIndex(&south_index);
256 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
257 if ( file_exists(file) ) {
262 if ( ext == ".se" ) {
263 fgBucketGenBasePath(&east_index, scene_path);
264 index = fgBucketGenIndex(&east_index);
265 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
266 if ( file_exists(file) ) {
269 fgBucketGenBasePath(&se_index, scene_path);
270 index = fgBucketGenIndex(&se_index);
271 sprintf(file, "%s/%s/%ld.1.nw", 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.ne", basepath, scene_path, index);
278 if ( file_exists(file) ) {
283 if ( ext == ".ne" ) {
284 fgBucketGenBasePath(&east_index, scene_path);
285 index = fgBucketGenIndex(&east_index);
286 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
287 if ( file_exists(file) ) {
290 fgBucketGenBasePath(&ne_index, scene_path);
291 index = fgBucketGenIndex(&ne_index);
292 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
293 if ( file_exists(file) ) {
296 fgBucketGenBasePath(&north_index, scene_path);
297 index = fgBucketGenIndex(&north_index);
298 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
299 if ( file_exists(file) ) {
304 if ( ext == ".nw" ) {
305 fgBucketGenBasePath(&west_index, scene_path);
306 index = fgBucketGenIndex(&west_index);
307 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
308 if ( file_exists(file) ) {
311 fgBucketGenBasePath(&nw_index, scene_path);
312 index = fgBucketGenIndex(&nw_index);
313 sprintf(file, "%s/%s/%ld.1.se", 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.sw", basepath, scene_path, index);
320 if ( file_exists(file) ) {
325 if ( ext == ".south" ) {
326 fgBucketGenBasePath(&south_index, scene_path);
327 index = fgBucketGenIndex(&south_index);
328 sprintf(file, "%s/%s/%ld.1.north", basepath, scene_path, index);
329 if ( file_exists(file) ) {
334 if ( ext == ".north" ) {
335 fgBucketGenBasePath(&north_index, scene_path);
336 index = fgBucketGenIndex(&north_index);
337 sprintf(file, "%s/%s/%ld.1.south", basepath, scene_path, index);
338 if ( file_exists(file) ) {
343 if ( ext == ".west" ) {
344 fgBucketGenBasePath(&west_index, scene_path);
345 index = fgBucketGenIndex(&west_index);
346 sprintf(file, "%s/%s/%ld.1.east", basepath, scene_path, index);
347 if ( file_exists(file) ) {
352 if ( ext == ".east" ) {
353 fgBucketGenBasePath(&east_index, scene_path);
354 index = fgBucketGenIndex(&east_index);
355 sprintf(file, "%s/%s/%ld.1.west", basepath, scene_path, index);
356 if ( file_exists(file) ) {
365 // my custom file opening routine ... don't open if a shared edge or
366 // vertex alread exists
367 FILE *my_open(const string& basename, const string& basepath,
373 // create the output file name
374 filename = basename + ext;
376 // check if a shared object already exist from a different tile
378 if ( shared_object_exists(basepath.c_str(), ext) ) {
379 // not an actual file open error, but we've already got the
380 // shared edge, so we don't want to create another one
381 cout << "not opening\n";
385 fp = fopen(filename.c_str(), "w");
386 cout << "Opening " + filename + "\n";
392 // dump in WaveFront .obj format
393 void dump_obj(const string& basename, const string& basepath) {
395 double n1[3], n2[3], n3[3], n4[3], n5[3], norm[3], temp;
396 FILE *fp, *sw, *se, *ne, *nw, *north, *south, *east, *west, *body;
397 int i, t1, t2, t3, t4, t5, count, size;
400 sw = my_open(basename, basepath, ".sw");
401 se = my_open(basename, basepath, ".se");
402 ne = my_open(basename, basepath, ".ne");
403 nw = my_open(basename, basepath, ".nw");
405 north = my_open(basename, basepath, ".north");
406 south = my_open(basename, basepath, ".south");
407 east = my_open(basename, basepath, ".east");
408 west = my_open(basename, basepath, ".west");
410 body = my_open(basename, basepath, ".body");
412 cout << "Dumping edges file basename: " + basename + " ...\n";
415 cout << " writing vertices\n";
417 iterator_3d last = nodes_orig.end();
418 iterator_3d current = nodes_orig.begin();
420 for ( ; current != last; ++current) {
423 if ( (fabs(node.y() - ymin) < FG_EPSILON) &&
424 (fabs(node.x() - xmin) < FG_EPSILON) ) {
426 } else if ( (fabs(node.y() - ymin) < FG_EPSILON) &&
427 (fabs(node.x() - xmax) < FG_EPSILON) ) {
429 } else if ( (fabs(node.y() - ymax) < FG_EPSILON) &&
430 (fabs(node.x() - xmax) < FG_EPSILON)) {
432 } else if ( (fabs(node.y() - ymax) < FG_EPSILON) &&
433 (fabs(node.x() - xmin) < FG_EPSILON) ) {
435 } else if ( fabs(node.x() - xmin) < FG_EPSILON ) {
437 } else if ( fabs(node.x() - xmax) < FG_EPSILON ) {
439 } else if ( fabs(node.y() - ymin) < FG_EPSILON ) {
441 } else if ( fabs(node.y() - ymax) < FG_EPSILON ) {
452 fprintf(fp, "gdn %.2f %.2f %.2f\n", x, y, z);
456 cout << " calculating and writing normals\n";
458 // calculate and generate normals
459 size = nodes_orig.size();
460 for ( i = 1; i < size; i++ ) {
461 // printf("Finding normal\n");
463 find_tris(i, &t1, &t2, &t3, &t4, &t5);
465 n1[0] = n1[1] = n1[2] = 0.0;
466 n2[0] = n2[1] = n2[2] = 0.0;
467 n3[0] = n3[1] = n3[2] = 0.0;
468 n4[0] = n4[1] = n4[2] = 0.0;
469 n5[0] = n5[1] = n5[2] = 0.0;
472 calc_normal(nodes_cart[tri_list[t1].n1],
473 nodes_cart[tri_list[t1].n2],
474 nodes_cart[tri_list[t1].n3],
478 calc_normal(nodes_cart[tri_list[t2].n1],
479 nodes_cart[tri_list[t2].n2],
480 nodes_cart[tri_list[t2].n3],
486 calc_normal(nodes_cart[tri_list[t3].n1],
487 nodes_cart[tri_list[t3].n2],
488 nodes_cart[tri_list[t3].n3],
494 calc_normal(nodes_cart[tri_list[t4].n1],
495 nodes_cart[tri_list[t4].n2],
496 nodes_cart[tri_list[t4].n3],
502 calc_normal(nodes_cart[tri_list[t5].n1],
503 nodes_cart[tri_list[t5].n2],
504 nodes_cart[tri_list[t5].n3],
509 // printf(" norm[2] = %.2f %.2f %.2f\n", n1[2], n2[2], n3[2]);
511 norm[0] = ( n1[0] + n2[0] + n3[0] + n4[0] + n5[0] ) / (double)count;
512 norm[1] = ( n1[1] + n2[1] + n3[1] + n4[1] + n5[1] ) / (double)count;
513 norm[2] = ( n1[2] + n2[2] + n3[2] + n4[2] + n5[2] ) / (double)count;
515 // printf(" count = %d\n", count);
516 // printf(" Ave. normal = %.4f %.4f %.4f\n", norm[0], norm[1],
518 MAT3_NORMALIZE_VEC(norm, temp);
519 // printf(" Normalized ave. normal = %.4f %.4f %.4f\n",
520 // norm[0], norm[1], norm[2]);
524 if ( (fabs(nodes_orig[i].y() - ymin) < FG_EPSILON) &&
525 (fabs(nodes_orig[i].x() - xmin) < FG_EPSILON) ) {
527 } else if ( (fabs(nodes_orig[i].y() - ymin) < FG_EPSILON) &&
528 (fabs(nodes_orig[i].x() - xmax) < FG_EPSILON) ) {
530 } else if ( (fabs(nodes_orig[i].y() - ymax) < FG_EPSILON) &&
531 (fabs(nodes_orig[i].x() - xmax) < FG_EPSILON)) {
533 } else if ( (fabs(nodes_orig[i].y() - ymax) < FG_EPSILON) &&
534 (fabs(nodes_orig[i].x() - xmin) < FG_EPSILON) ) {
536 } else if ( fabs(nodes_orig[i].x() - xmin) < FG_EPSILON ) {
538 } else if ( fabs(nodes_orig[i].x() - xmax) < FG_EPSILON ) {
540 } else if ( fabs(nodes_orig[i].y() - ymin) < FG_EPSILON ) {
542 } else if ( fabs(nodes_orig[i].y() - ymax) < FG_EPSILON ) {
546 fprintf(fp, "vn %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);
550 if ( sw ) { fclose(sw); }
551 if ( se ) { fclose(se); }
552 if ( ne ) { fclose(ne); }
553 if ( nw ) { fclose(nw); }
555 if ( north ) { fclose(north); }
556 if ( south ) { fclose(south); }
557 if ( east ) { fclose(east); }
558 if ( west ) { fclose(west); }
560 if ( body ) { fclose(body); }
564 int main(int argc, char **argv) {
565 string basename, basepath, temp;
572 // find the base path of the file
573 basepath = extract_path(basename);
574 basepath = extract_path(basepath);
575 basepath = extract_path(basepath);
576 cout << "basepath = " + basepath + "\n";
578 // find the index of the current file
579 temp = extract_file(basename);
582 temp = temp.substr(0, len-2);
584 index = atoi( temp.c_str() );
585 cout << "index = " << index << "\n";
586 fgBucketParseIndex(index, &p);
588 cout << "bucket = " << p.lon << " " << p.lat << " " <<
589 p.x << " " << p.y << "\n";
591 // generate the indexes of the neighbors
592 fgBucketOffset(&p, &ne_index, 1, 1);
593 fgBucketOffset(&p, &nw_index, -1, 1);
594 fgBucketOffset(&p, &se_index, 1, -1);
595 fgBucketOffset(&p, &sw_index, -1, -1);
597 fgBucketOffset(&p, &north_index, 0, 1);
598 fgBucketOffset(&p, &south_index, 0, -1);
599 fgBucketOffset(&p, &east_index, 1, 0);
600 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.7 1998/11/06 21:33:57 curt
620 // Updates to go along with changes in fgstream.
622 // Revision 1.6 1998/10/21 14:56:20 curt
623 // Fixed a units conversion bug.
625 // Revision 1.5 1998/10/20 15:50:33 curt
628 // Revision 1.4 1998/10/18 01:17:27 curt
631 // Revision 1.3 1998/09/22 23:49:56 curt
632 // C++-ified, STL-ified, and string-ified.
634 // Revision 1.2 1998/09/21 23:16:23 curt
635 // Converted to c++ style comments.
637 // Revision 1.1 1998/07/08 14:59:13 curt
638 // *.[ch] renamed to *.[ch]xx
640 // Revision 1.11 1998/07/04 00:56:40 curt
641 // typedef'd struct fgBUCKET.
643 // Revision 1.10 1998/05/02 01:54:37 curt
644 // Converting to polar3d.h routines.
646 // Revision 1.9 1998/04/18 04:01:20 curt
647 // Now use libMath rather than having local copies of math routines.
649 // Revision 1.8 1998/04/14 02:26:08 curt
650 // Code reorganizations. Added a Lib/ directory for more general libraries.
652 // Revision 1.7 1998/04/08 23:21:13 curt
653 // Adopted Gnu automake/autoconf system.
655 // Revision 1.6 1998/03/03 15:36:13 curt
656 // Tweaks for compiling with g++
658 // Revision 1.5 1998/03/03 03:37:04 curt
659 // Cumulative tweaks.
661 // Revision 1.4 1998/01/31 00:41:26 curt
662 // Made a few changes converting floats to doubles.
664 // Revision 1.3 1998/01/27 18:37:04 curt
665 // Lots of updates to get back in sync with changes made over in .../Src/
667 // Revision 1.2 1998/01/14 15:54:43 curt
668 // Initial revision completed.
670 // Revision 1.1 1998/01/14 02:11:31 curt