]> git.mxchange.org Git - flightgear.git/blob - SplitTris/splittris.cxx
Hacking towards the first working version.
[flightgear.git] / SplitTris / splittris.cxx
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.
5 //
6 // Written by Curtis Olson, started January 1998.
7 //
8 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
9 //
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.
14 //
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.
19 //
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.
23 //
24 // $Id$
25 // (Log is kept at end of this file)
26
27
28 #include <math.h>
29 #include <stdio.h>
30 #include <stdlib.h>   // for atoi()
31 #include <string.h>
32 #include <sys/stat.h> // for stat()
33 #include <unistd.h>   // for stat()
34
35 #include "splittris.hxx"
36
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>
44
45 // int nodecount, tricount;
46 double xmin, xmax, ymin, ymax;
47
48 // static double nodes_orig[MAX_NODES][3];
49 // static Point3D nodes_cart[MAX_NODES];
50 // static int tris[MAX_TRIS][3];
51
52 container_3d nodes_orig;
53 container_3d nodes_cart;
54 container_tri tri_list;
55
56 fgBUCKET ne_index, nw_index, sw_index, se_index;
57 fgBUCKET north_index, south_index, east_index, west_index;
58
59
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])
63 {
64     double v1[3], v2[3];
65     double temp;
66
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();
69
70     MAT3cross_product(normal, v1, v2);
71     MAT3_NORMALIZE_VEC(normal,temp);
72
73     // printf("  Normal = %.2f %.2f %.2f\n", normal[0], normal[1], normal[2]);
74 }
75
76
77 // return the file base name ( foo/bar/file.ext = file.ext )
78 string extract_file(const string& input) {
79     int pos;
80
81     pos = input.rfind("/");
82     ++pos;
83
84     return input.substr(pos);
85 }
86
87
88 // return the file path name ( foo/bar/file.ext = foo/bar )
89 string extract_path(const string& input) {
90     int pos;
91
92     pos = input.rfind("/");
93
94     return input.substr(0, pos);
95 }
96
97
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) {
100     int i;
101
102     *t1 = *t2 = *t3 = *t4 = *t5 = 0;
103
104     i = 1;
105     iterator_tri last = tri_list.end();
106     iterator_tri current = tri_list.begin();
107
108     // skip first null record
109     ++current;
110
111     for ( ; current != last; ++current )
112     {
113         if ( (n == (*current).n1) || (n == (*current).n2) || 
114              (n == (*current).n3) )
115         {
116             if ( *t1 == 0 ) {
117                 *t1 = i;
118             } else if ( *t2 == 0 ) {
119                 *t2 = i;
120             } else if ( *t3 == 0 ) {
121                 *t3 = i;
122             } else if ( *t4 == 0 ) {
123                 *t4 = i;
124             } else {
125                 *t5 = i;
126             }
127         }
128         ++i;
129     }
130 }
131
132
133 // Initialize a new mesh structure
134 void triload(const string& basename) {
135     string nodename, elename;
136     Point3D node1, node2, p;
137     triangle tri;
138     int nodecount, tricount, dim, junk1, junk2;
139     int i;
140
141     nodename = basename + ".node";
142     elename  = basename + ".ele";
143
144     cout << "Loading node file:  " + nodename + " ...\n";
145
146     fg_gzifstream node_in( nodename );
147     if ( !node_in ) {
148         cout << "Cannot open file " + nodename + "\n";
149         exit(-1);
150     }
151
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);
158
159     node_in >> nodecount >> dim >> junk1 >> junk2;
160     cout << "    Expecting " << nodecount << " nodes\n";
161
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);
166         
167         // convert to radians (before we can convert to cartesian)
168         p = Point3D( node1.x() * ARCSEC_TO_RAD,
169                      node1.y() * ARCSEC_TO_RAD,
170                      node1.z() );
171
172         node2 = fgGeodToCart(p);
173         nodes_cart.push_back(node2);
174         // printf("%d %.2f %.2f %.2f\n", junk1, node2.x, node2.y, node2.z);
175
176         if ( i == 1 ) {
177             xmin = xmax = node1.x();
178             ymin = ymax = node1.y();
179         } else {
180             if ( node1.x() < xmin ) {
181                 xmin = node1.x();
182             }
183             if ( node1.x() > xmax ) {
184                 xmax = node1.x();
185             }
186             if ( node1.y() < ymin ) {
187                 ymin = node1.y();
188             }
189             if ( node1.y() > ymax ) {
190                 ymax = node1.y();
191             }
192         }
193     }
194
195     cout << "Loading element file:  " + elename + " ...\n";
196     fg_gzifstream ele_in( elename );
197     if ( !ele_in ) {
198         cout << "Cannot open file " + elename + "\n";
199         exit(-1);
200     }
201
202     ele_in >> tricount >> junk1 >> junk2;
203     cout << "    Expecting " << tricount << " elements\n";
204
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);
211     }
212 }
213
214
215 // check if a file exists
216 int file_exists(char *file) {
217     struct stat stat_buf;
218     int result;
219
220     cout << "checking " << file << " ... ";
221
222     result = stat(file, &stat_buf);
223
224     if ( result != 0 ) {
225         // stat failed, no file
226         cout << "not found.\n";
227         return 0;
228     } else {
229         // stat succeeded, file exists
230         cout << "exists.\n";
231         return 1;
232     }
233 }
234
235
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];
239     long int index;
240
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) ) {
246             return(1);
247         }
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) ) {
252             return(1);
253         }
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) ) {
258             return(1);
259         }
260     }
261
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) ) {
267             return(1);
268         }
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) ) {
273             return(1);
274         }
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) ) {
279             return(1);
280         }
281     }
282
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) ) {
288             return(1);
289         }
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) ) {
294             return(1);
295         }
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) ) {
300             return(1);
301         }
302     }
303
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) ) {
309             return(1);
310         }
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) ) {
315             return(1);
316         }
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) ) {
321             return(1);
322         }
323     }
324
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) ) {
330             return(1);
331         }
332     }
333
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) ) {
339             return(1);
340         }
341     }
342
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) ) {
348             return(1);
349         }
350     }
351
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) ) {
357             return(1);
358         }
359     }
360
361     return(0);
362 }
363
364
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, 
368               const string& ext)
369 {
370     FILE *fp;
371     string filename;
372
373     // create the output file name
374     filename = basename + ext;
375
376     // check if a shared object already exist from a different tile
377
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";
382         return(NULL);
383     } else {
384         // open the file
385         fp = fopen(filename.c_str(), "w");
386         cout << "Opening " + filename + "\n";
387         return(fp);
388     }
389 }
390
391
392 // dump in WaveFront .obj format
393 void dump_obj(const string& basename, const string& basepath) {
394     Point3D node;
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;
398     double x, y, z;
399
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");
404
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");
409
410     body = my_open(basename, basepath, ".body");
411
412     cout << "Dumping edges file basename:  " + basename + " ...\n";
413
414     // dump vertices
415     cout << "  writing vertices\n";
416
417     iterator_3d last = nodes_orig.end();
418     iterator_3d current = nodes_orig.begin();
419     ++current;
420     for ( ; current != last; ++current) {
421         node = *current;
422
423         if ( (fabs(node.y() - ymin) < FG_EPSILON) && 
424              (fabs(node.x() - xmin) < FG_EPSILON) ) {
425             fp = sw;
426         } else if ( (fabs(node.y() - ymin) < FG_EPSILON) &&
427                     (fabs(node.x() - xmax) < FG_EPSILON) ) {
428             fp = se;
429         } else if ( (fabs(node.y() - ymax) < FG_EPSILON) &&
430                     (fabs(node.x() - xmax) < FG_EPSILON)) {
431             fp = ne;
432         } else if ( (fabs(node.y() - ymax) < FG_EPSILON) &&
433                     (fabs(node.x() - xmin) < FG_EPSILON) ) {
434             fp = nw;
435         } else if ( fabs(node.x() - xmin) < FG_EPSILON ) {
436             fp = west;
437         } else if ( fabs(node.x() - xmax) < FG_EPSILON ) {
438             fp = east;
439         } else if ( fabs(node.y() - ymin) < FG_EPSILON ) {
440             fp = south;
441         } else if ( fabs(node.y() - ymax) < FG_EPSILON ) {
442             fp = north;
443         } else {
444             fp = body;
445         }
446
447         x = node.x();
448         y = node.y();
449         z = node.z();
450
451         if ( fp != NULL ) {
452             fprintf(fp, "gdn %.2f %.2f %.2f\n", x, y, z);
453         }
454     }
455
456     cout << "  calculating and writing normals\n";
457
458     // calculate and generate normals
459     size = nodes_orig.size();
460     for ( i = 1; i < size; i++ ) {
461         // printf("Finding normal\n");
462
463         find_tris(i, &t1, &t2, &t3, &t4, &t5);
464
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;
470
471         count = 1;
472         calc_normal(nodes_cart[tri_list[t1].n1],
473                     nodes_cart[tri_list[t1].n2], 
474                     nodes_cart[tri_list[t1].n3],
475                     n1);
476
477         if ( t2 > 0 ) {
478             calc_normal(nodes_cart[tri_list[t2].n1], 
479                         nodes_cart[tri_list[t2].n2], 
480                         nodes_cart[tri_list[t2].n3],
481                         n2);
482             count = 2;
483         }
484
485         if ( t3 > 0 ) {
486             calc_normal(nodes_cart[tri_list[t3].n1],
487                         nodes_cart[tri_list[t3].n2],
488                         nodes_cart[tri_list[t3].n3],
489                         n3);
490             count = 3;
491         }
492
493         if ( t4 > 0 ) {
494             calc_normal(nodes_cart[tri_list[t4].n1],
495                         nodes_cart[tri_list[t4].n2],
496                         nodes_cart[tri_list[t4].n3],
497                         n4);
498             count = 4;
499         }
500
501         if ( t5 > 0 ) {
502             calc_normal(nodes_cart[tri_list[t5].n1],
503                         nodes_cart[tri_list[t5].n2],
504                         nodes_cart[tri_list[t5].n3],
505                         n5);
506             count = 5;
507         }
508
509         // printf("  norm[2] = %.2f %.2f %.2f\n", n1[2], n2[2], n3[2]);
510
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;
514         
515         // printf("  count = %d\n", count);
516         // printf("  Ave. normal = %.4f %.4f %.4f\n", norm[0], norm[1], 
517         //        norm[2]);
518         MAT3_NORMALIZE_VEC(norm, temp);
519         // printf("  Normalized ave. normal = %.4f %.4f %.4f\n", 
520         //        norm[0], norm[1], norm[2]);
521         
522         fp = NULL;
523
524         if ( (fabs(nodes_orig[i].y() - ymin) < FG_EPSILON) && 
525              (fabs(nodes_orig[i].x() - xmin) < FG_EPSILON) ) {
526             fp = sw;
527         } else if ( (fabs(nodes_orig[i].y() - ymin) < FG_EPSILON) &&
528                     (fabs(nodes_orig[i].x() - xmax) < FG_EPSILON) ) {
529             fp = se;
530         } else if ( (fabs(nodes_orig[i].y() - ymax) < FG_EPSILON) &&
531                     (fabs(nodes_orig[i].x() - xmax) < FG_EPSILON)) {
532             fp = ne;
533         } else if ( (fabs(nodes_orig[i].y() - ymax) < FG_EPSILON) &&
534                     (fabs(nodes_orig[i].x() - xmin) < FG_EPSILON) ) {
535             fp = nw;
536         } else if ( fabs(nodes_orig[i].x() - xmin) < FG_EPSILON ) {
537             fp = west;
538         } else if ( fabs(nodes_orig[i].x() - xmax) < FG_EPSILON ) {
539             fp = east;
540         } else if ( fabs(nodes_orig[i].y() - ymin) < FG_EPSILON ) {
541             fp = south;
542         } else if ( fabs(nodes_orig[i].y() - ymax) < FG_EPSILON ) {
543             fp = north;
544         }
545         if ( fp != NULL ) {
546             fprintf(fp, "vn %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);
547         }
548     }
549
550     if ( sw ) { fclose(sw); }
551     if ( se ) { fclose(se); }
552     if ( ne ) { fclose(ne); }
553     if ( nw ) { fclose(nw); }
554
555     if ( north ) { fclose(north); }
556     if ( south ) { fclose(south); }
557     if ( east ) { fclose(east); }
558     if ( west ) { fclose(west); }
559
560     if ( body ) { fclose(body); }
561 }
562
563
564 int main(int argc, char **argv) {
565     string basename, basepath, temp;
566     fgBUCKET p;
567     long int index;
568     int len;
569
570     basename = argv[1];
571
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";
577
578     // find the index of the current file
579     temp = extract_file(basename);
580     len = temp.length();
581     if ( len >= 2 ) {
582         temp = temp.substr(0, len-2);
583     }
584     index = atoi( temp.c_str() );
585     cout << "index = " << index << "\n";
586     fgBucketParseIndex(index, &p);
587
588     cout << "bucket = " << p.lon << " " << p.lat << " " << 
589         p.x << " " << p.y << "\n";
590
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);
596
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);
601
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);
606           
607
608     // load the input data files
609     triload(basename);
610
611     // dump in WaveFront .obj format
612     dump_obj(basename, basepath);
613
614     return(0);
615 }
616
617
618 // $Log$
619 // Revision 1.7  1998/11/06 21:33:57  curt
620 // Updates to go along with changes in fgstream.
621 //
622 // Revision 1.6  1998/10/21 14:56:20  curt
623 // Fixed a units conversion bug.
624 //
625 // Revision 1.5  1998/10/20 15:50:33  curt
626 // whitespace tweak.
627 //
628 // Revision 1.4  1998/10/18 01:17:27  curt
629 // Point3D tweaks.
630 //
631 // Revision 1.3  1998/09/22 23:49:56  curt
632 // C++-ified, STL-ified, and string-ified.
633 //
634 // Revision 1.2  1998/09/21 23:16:23  curt
635 // Converted to c++ style comments.
636 //
637 // Revision 1.1  1998/07/08 14:59:13  curt
638 // *.[ch] renamed to *.[ch]xx
639 //
640 // Revision 1.11  1998/07/04 00:56:40  curt
641 // typedef'd struct fgBUCKET.
642 //
643 // Revision 1.10  1998/05/02 01:54:37  curt
644 // Converting to polar3d.h routines.
645 //
646 // Revision 1.9  1998/04/18 04:01:20  curt
647 // Now use libMath rather than having local copies of math routines.
648 //
649 // Revision 1.8  1998/04/14 02:26:08  curt
650 // Code reorganizations.  Added a Lib/ directory for more general libraries.
651 //
652 // Revision 1.7  1998/04/08 23:21:13  curt
653 // Adopted Gnu automake/autoconf system.
654 //
655 // Revision 1.6  1998/03/03 15:36:13  curt
656 // Tweaks for compiling with g++
657 //
658 // Revision 1.5  1998/03/03 03:37:04  curt
659 // Cumulative tweaks.
660 //
661 // Revision 1.4  1998/01/31 00:41:26  curt
662 // Made a few changes converting floats to doubles.
663 //
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/
666 //
667 // Revision 1.2  1998/01/14 15:54:43  curt
668 // Initial revision completed.
669 //
670 // Revision 1.1  1998/01/14 02:11:31  curt
671 // Initial revision.
672 //
673