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>
40 #include <Math/fg_geodesy.hxx>
41 #include <Math/mat3.h>
42 #include <Math/point3d.hxx>
43 #include <Math/polar3d.hxx>
44 #include <Misc/fgstream.hxx>
46 // int nodecount, tricount;
47 double xmin, xmax, ymin, ymax;
49 // static double nodes_orig[MAX_NODES][3];
50 // static Point3D nodes_cart[MAX_NODES];
51 // static int tris[MAX_TRIS][3];
53 container_3d nodes_orig;
54 container_3d nodes_cart;
55 container_tri tri_list;
57 fgBUCKET ne_index, nw_index, sw_index, se_index;
58 fgBUCKET north_index, south_index, east_index, west_index;
61 // given three points defining a triangle, calculate the normal
62 void calc_normal(const Point3D& p1, const Point3D& p2,
63 const Point3D& p3, double normal[3])
68 v1[0] = p2.x() - p1.x(); v1[1] = p2.y() - p1.y(); v1[2] = p2.z() - p1.z();
69 v2[0] = p3.x() - p1.x(); v2[1] = p3.y() - p1.y(); v2[2] = p3.z() - p1.z();
71 MAT3cross_product(normal, v1, v2);
72 MAT3_NORMALIZE_VEC(normal,temp);
74 // printf(" Normal = %.2f %.2f %.2f\n", normal[0], normal[1], normal[2]);
78 // return the file base name ( foo/bar/file.ext = file.ext )
79 string extract_file(const string& input) {
82 pos = input.rfind("/");
85 return input.substr(pos);
89 // return the file path name ( foo/bar/file.ext = foo/bar )
90 string extract_path(const string& input) {
93 pos = input.rfind("/");
95 return input.substr(0, pos);
99 // return the index of all triangles containing the specified node
100 void find_tris(int n, int *t1, int *t2, int *t3, int *t4, int *t5) {
103 *t1 = *t2 = *t3 = *t4 = *t5 = 0;
106 iterator_tri last = tri_list.end();
107 iterator_tri current = tri_list.begin();
109 // skip first null record
112 for ( ; current != last; ++current )
114 if ( (n == (*current).n1) || (n == (*current).n2) ||
115 (n == (*current).n3) )
119 } else if ( *t2 == 0 ) {
121 } else if ( *t3 == 0 ) {
123 } else if ( *t4 == 0 ) {
134 // Initialize a new mesh structure
135 void triload(const string& basename) {
136 string nodename, elename;
137 Point3D node1, node2;
139 int nodecount, tricount, dim, junk1, junk2;
142 nodename = basename + ".node";
143 elename = basename + ".ele";
145 cout << "Loading node file: " + nodename + " ...\n";
147 fg_gzifstream node_in( nodename );
149 cout << "Cannot open file " + nodename + "\n";
153 // the triangle program starts counting at 1 by default which is
154 // pretty obnoxious. Let's just push null record zero's onto our
155 // list to compensate
156 nodes_orig.push_back(node1);
157 nodes_cart.push_back(node1);
158 tri_list.push_back(tri);
160 node_in.stream() >> nodecount >> dim >> junk1 >> junk2;
161 cout << " Expecting " << nodecount << " nodes\n";
163 for ( i = 1; i <= nodecount; i++ ) {
164 node_in.stream() >> junk1 >> node1 >> junk2;
165 nodes_orig.push_back(node1);
166 // printf("%d %.2f %.2f %.2f\n", junk1, node1.x, node1.y, node1.z);
168 node2 = fgGeodToCart(node1);
169 nodes_cart.push_back(node2);
170 // printf("%d %.2f %.2f %.2f\n", junk1, node2.x, node2.y, node2.z);
173 xmin = xmax = node1.x();
174 ymin = ymax = node1.y();
176 if ( node1.x() < xmin ) {
179 if ( node1.x() > xmax ) {
182 if ( node1.y() < ymin ) {
185 if ( node1.y() > ymax ) {
191 cout << "Loading element file: " + elename + " ...\n";
192 fg_gzifstream ele_in( elename );
194 cout << "Cannot open file " + elename + "\n";
198 ele_in.stream() >> tricount >> junk1 >> junk2;
199 cout << " Expecting " << tricount << " elements\n";
201 for ( i = 1; i <= tricount; i++ ) {
202 // fscanf(ele_file, "%d %d %d %d\n", &junk1,
203 // &(tri.n1), &(tri.n2), &(tri.n3));
204 ele_in.stream() >> junk1 >> tri.n1 >> tri.n2 >> tri.n3;
205 // printf("%d %d %d %d\n", junk1, tri.n1, tri.n2, tri.n3);
206 tri_list.push_back(tri);
211 // check if a file exists
212 int file_exists(char *file) {
213 struct stat stat_buf;
216 cout << "checking " << file << " ... ";
218 result = stat(file, &stat_buf);
221 // stat failed, no file
222 cout << "not found.\n";
225 // stat succeeded, file exists
232 // check to see if a shared object exists
233 int shared_object_exists(const char *basepath, const string& ext) {
234 char file[256], scene_path[256];
237 if ( ext == ".sw" ) {
238 fgBucketGenBasePath(&west_index, scene_path);
239 index = fgBucketGenIndex(&west_index);
240 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
241 if ( file_exists(file) ) {
244 fgBucketGenBasePath(&sw_index, scene_path);
245 index = fgBucketGenIndex(&sw_index);
246 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
247 if ( file_exists(file) ) {
250 fgBucketGenBasePath(&south_index, scene_path);
251 index = fgBucketGenIndex(&south_index);
252 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
253 if ( file_exists(file) ) {
258 if ( ext == ".se" ) {
259 fgBucketGenBasePath(&east_index, scene_path);
260 index = fgBucketGenIndex(&east_index);
261 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
262 if ( file_exists(file) ) {
265 fgBucketGenBasePath(&se_index, scene_path);
266 index = fgBucketGenIndex(&se_index);
267 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
268 if ( file_exists(file) ) {
271 fgBucketGenBasePath(&south_index, scene_path);
272 index = fgBucketGenIndex(&south_index);
273 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
274 if ( file_exists(file) ) {
279 if ( ext == ".ne" ) {
280 fgBucketGenBasePath(&east_index, scene_path);
281 index = fgBucketGenIndex(&east_index);
282 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
283 if ( file_exists(file) ) {
286 fgBucketGenBasePath(&ne_index, scene_path);
287 index = fgBucketGenIndex(&ne_index);
288 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
289 if ( file_exists(file) ) {
292 fgBucketGenBasePath(&north_index, scene_path);
293 index = fgBucketGenIndex(&north_index);
294 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
295 if ( file_exists(file) ) {
300 if ( ext == ".nw" ) {
301 fgBucketGenBasePath(&west_index, scene_path);
302 index = fgBucketGenIndex(&west_index);
303 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
304 if ( file_exists(file) ) {
307 fgBucketGenBasePath(&nw_index, scene_path);
308 index = fgBucketGenIndex(&nw_index);
309 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
310 if ( file_exists(file) ) {
313 fgBucketGenBasePath(&north_index, scene_path);
314 index = fgBucketGenIndex(&north_index);
315 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
316 if ( file_exists(file) ) {
321 if ( ext == ".south" ) {
322 fgBucketGenBasePath(&south_index, scene_path);
323 index = fgBucketGenIndex(&south_index);
324 sprintf(file, "%s/%s/%ld.1.north", basepath, scene_path, index);
325 if ( file_exists(file) ) {
330 if ( ext == ".north" ) {
331 fgBucketGenBasePath(&north_index, scene_path);
332 index = fgBucketGenIndex(&north_index);
333 sprintf(file, "%s/%s/%ld.1.south", basepath, scene_path, index);
334 if ( file_exists(file) ) {
339 if ( ext == ".west" ) {
340 fgBucketGenBasePath(&west_index, scene_path);
341 index = fgBucketGenIndex(&west_index);
342 sprintf(file, "%s/%s/%ld.1.east", basepath, scene_path, index);
343 if ( file_exists(file) ) {
348 if ( ext == ".east" ) {
349 fgBucketGenBasePath(&east_index, scene_path);
350 index = fgBucketGenIndex(&east_index);
351 sprintf(file, "%s/%s/%ld.1.west", basepath, scene_path, index);
352 if ( file_exists(file) ) {
361 // my custom file opening routine ... don't open if a shared edge or
362 // vertex alread exists
363 FILE *my_open(const string& basename, const string& basepath,
369 // create the output file name
370 filename = basename + ext;
372 // check if a shared object already exist from a different tile
374 if ( shared_object_exists(basepath.c_str(), ext) ) {
375 // not an actual file open error, but we've already got the
376 // shared edge, so we don't want to create another one
377 cout << "not opening\n";
381 fp = fopen(filename.c_str(), "w");
382 cout << "Opening " + filename + "\n";
388 // dump in WaveFront .obj format
389 void dump_obj(const string& basename, const string& basepath) {
391 double n1[3], n2[3], n3[3], n4[3], n5[3], norm[3], temp;
392 FILE *fp, *sw, *se, *ne, *nw, *north, *south, *east, *west, *body;
393 int i, t1, t2, t3, t4, t5, count, size;
396 sw = my_open(basename, basepath, ".sw");
397 se = my_open(basename, basepath, ".se");
398 ne = my_open(basename, basepath, ".ne");
399 nw = my_open(basename, basepath, ".nw");
401 north = my_open(basename, basepath, ".north");
402 south = my_open(basename, basepath, ".south");
403 east = my_open(basename, basepath, ".east");
404 west = my_open(basename, basepath, ".west");
406 body = my_open(basename, basepath, ".body");
408 cout << "Dumping edges file basename: " + basename + " ...\n";
411 cout << " writing vertices\n";
413 iterator_3d last = nodes_orig.end();
414 iterator_3d current = nodes_orig.begin();
416 for ( ; current != last; ++current) {
419 if ( (fabs(node.y() - ymin) < FG_EPSILON) &&
420 (fabs(node.x() - xmin) < FG_EPSILON) ) {
422 } else if ( (fabs(node.y() - ymin) < FG_EPSILON) &&
423 (fabs(node.x() - xmax) < FG_EPSILON) ) {
425 } else if ( (fabs(node.y() - ymax) < FG_EPSILON) &&
426 (fabs(node.x() - xmax) < FG_EPSILON)) {
428 } else if ( (fabs(node.y() - ymax) < FG_EPSILON) &&
429 (fabs(node.x() - xmin) < FG_EPSILON) ) {
431 } else if ( fabs(node.x() - xmin) < FG_EPSILON ) {
433 } else if ( fabs(node.x() - xmax) < FG_EPSILON ) {
435 } else if ( fabs(node.y() - ymin) < FG_EPSILON ) {
437 } else if ( fabs(node.y() - ymax) < FG_EPSILON ) {
448 fprintf(fp, "gdn %.2f %.2f %.2f\n", x, y, z);
452 cout << " calculating and writing normals\n";
454 // calculate and generate normals
455 size = nodes_orig.size();
456 for ( i = 1; i < size; i++ ) {
457 // printf("Finding normal\n");
459 find_tris(i, &t1, &t2, &t3, &t4, &t5);
461 n1[0] = n1[1] = n1[2] = 0.0;
462 n2[0] = n2[1] = n2[2] = 0.0;
463 n3[0] = n3[1] = n3[2] = 0.0;
464 n4[0] = n4[1] = n4[2] = 0.0;
465 n5[0] = n5[1] = n5[2] = 0.0;
468 calc_normal(nodes_cart[tri_list[t1].n1],
469 nodes_cart[tri_list[t1].n2],
470 nodes_cart[tri_list[t1].n3],
474 calc_normal(nodes_cart[tri_list[t2].n1],
475 nodes_cart[tri_list[t2].n2],
476 nodes_cart[tri_list[t2].n3],
482 calc_normal(nodes_cart[tri_list[t3].n1],
483 nodes_cart[tri_list[t3].n2],
484 nodes_cart[tri_list[t3].n3],
490 calc_normal(nodes_cart[tri_list[t4].n1],
491 nodes_cart[tri_list[t4].n2],
492 nodes_cart[tri_list[t4].n3],
498 calc_normal(nodes_cart[tri_list[t5].n1],
499 nodes_cart[tri_list[t5].n2],
500 nodes_cart[tri_list[t5].n3],
505 // printf(" norm[2] = %.2f %.2f %.2f\n", n1[2], n2[2], n3[2]);
507 norm[0] = ( n1[0] + n2[0] + n3[0] + n4[0] + n5[0] ) / (double)count;
508 norm[1] = ( n1[1] + n2[1] + n3[1] + n4[1] + n5[1] ) / (double)count;
509 norm[2] = ( n1[2] + n2[2] + n3[2] + n4[2] + n5[2] ) / (double)count;
511 // printf(" count = %d\n", count);
512 // printf(" Ave. normal = %.4f %.4f %.4f\n", norm[0], norm[1],
514 MAT3_NORMALIZE_VEC(norm, temp);
515 // printf(" Normalized ave. normal = %.4f %.4f %.4f\n",
516 // norm[0], norm[1], norm[2]);
520 if ( (fabs(nodes_orig[i].y() - ymin) < FG_EPSILON) &&
521 (fabs(nodes_orig[i].x() - xmin) < FG_EPSILON) ) {
523 } else if ( (fabs(nodes_orig[i].y() - ymin) < FG_EPSILON) &&
524 (fabs(nodes_orig[i].x() - xmax) < FG_EPSILON) ) {
526 } else if ( (fabs(nodes_orig[i].y() - ymax) < FG_EPSILON) &&
527 (fabs(nodes_orig[i].x() - xmax) < FG_EPSILON)) {
529 } else if ( (fabs(nodes_orig[i].y() - ymax) < FG_EPSILON) &&
530 (fabs(nodes_orig[i].x() - xmin) < FG_EPSILON) ) {
532 } else if ( fabs(nodes_orig[i].x() - xmin) < FG_EPSILON ) {
534 } else if ( fabs(nodes_orig[i].x() - xmax) < FG_EPSILON ) {
536 } else if ( fabs(nodes_orig[i].y() - ymin) < FG_EPSILON ) {
538 } else if ( fabs(nodes_orig[i].y() - ymax) < FG_EPSILON ) {
542 fprintf(fp, "vn %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);
546 if ( sw ) { fclose(sw); }
547 if ( se ) { fclose(se); }
548 if ( ne ) { fclose(ne); }
549 if ( nw ) { fclose(nw); }
551 if ( north ) { fclose(north); }
552 if ( south ) { fclose(south); }
553 if ( east ) { fclose(east); }
554 if ( west ) { fclose(west); }
556 if ( body ) { fclose(body); }
560 int main(int argc, char **argv) {
561 string basename, basepath, temp;
568 // find the base path of the file
569 basepath = extract_path(basename);
570 basepath = extract_path(basepath);
571 basepath = extract_path(basepath);
572 cout << "basepath = " + basepath + "\n";
574 // find the index of the current file
575 temp = extract_file(basename);
578 temp = temp.substr(0, len-2);
580 index = atoi( temp.c_str() );
581 cout << "index = " << index << "\n";
582 fgBucketParseIndex(index, &p);
584 cout << "bucket = " << p.lon << " " << p.lat << " " <<
585 p.x << " " << p.y << "\n";
587 // generate the indexes of the neighbors
588 fgBucketOffset(&p, &ne_index, 1, 1);
589 fgBucketOffset(&p, &nw_index, -1, 1);
590 fgBucketOffset(&p, &se_index, 1, -1);
591 fgBucketOffset(&p, &sw_index, -1, -1);
593 fgBucketOffset(&p, &north_index, 0, 1);
594 fgBucketOffset(&p, &south_index, 0, -1);
595 fgBucketOffset(&p, &east_index, 1, 0);
596 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.4 1998/10/18 01:17:27 curt
618 // Revision 1.3 1998/09/22 23:49:56 curt
619 // C++-ified, STL-ified, and string-ified.
621 // Revision 1.2 1998/09/21 23:16:23 curt
622 // Converted to c++ style comments.
624 // Revision 1.1 1998/07/08 14:59:13 curt
625 // *.[ch] renamed to *.[ch]xx
627 // Revision 1.11 1998/07/04 00:56:40 curt
628 // typedef'd struct fgBUCKET.
630 // Revision 1.10 1998/05/02 01:54:37 curt
631 // Converting to polar3d.h routines.
633 // Revision 1.9 1998/04/18 04:01:20 curt
634 // Now use libMath rather than having local copies of math routines.
636 // Revision 1.8 1998/04/14 02:26:08 curt
637 // Code reorganizations. Added a Lib/ directory for more general libraries.
639 // Revision 1.7 1998/04/08 23:21:13 curt
640 // Adopted Gnu automake/autoconf system.
642 // Revision 1.6 1998/03/03 15:36:13 curt
643 // Tweaks for compiling with g++
645 // Revision 1.5 1998/03/03 03:37:04 curt
646 // Cumulative tweaks.
648 // Revision 1.4 1998/01/31 00:41:26 curt
649 // Made a few changes converting floats to doubles.
651 // Revision 1.3 1998/01/27 18:37:04 curt
652 // Lots of updates to get back in sync with changes made over in .../Src/
654 // Revision 1.2 1998/01/14 15:54:43 curt
655 // Initial revision completed.
657 // Revision 1.1 1998/01/14 02:11:31 curt