]> git.mxchange.org Git - flightgear.git/blob - SplitTris/splittris.cxx
41e94a75d46887c412d5987f57146f5cf1753c41
[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;
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.stream() >> nodecount >> dim >> junk1 >> junk2;
160     cout << "    Expecting " << nodecount << " nodes\n";
161
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);
166
167         node2 = fgGeodToCart(node1);
168         nodes_cart.push_back(node2);
169         // printf("%d %.2f %.2f %.2f\n", junk1, node2.x, node2.y, node2.z);
170
171         if ( i == 1 ) {
172             xmin = xmax = node1.x();
173             ymin = ymax = node1.y();
174         } else {
175             if ( node1.x() < xmin ) {
176                 xmin = node1.x();
177             }
178             if ( node1.x() > xmax ) {
179                 xmax = node1.x();
180             }
181             if ( node1.y() < ymin ) {
182                 ymin = node1.y();
183             }
184             if ( node1.y() > ymax ) {
185                 ymax = node1.y();
186             }
187         }
188     }
189
190     cout << "Loading element file:  " + elename + " ...\n";
191     fg_gzifstream ele_in( elename );
192     if ( !ele_in ) {
193         cout << "Cannot open file " + elename + "\n";
194         exit(-1);
195     }
196
197     ele_in.stream() >> tricount >> junk1 >> junk2;
198     cout << "    Expecting " << tricount << " elements\n";
199
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);
206     }
207 }
208
209
210 // check if a file exists
211 int file_exists(char *file) {
212     struct stat stat_buf;
213     int result;
214
215     cout << "checking " << file << " ... ";
216
217     result = stat(file, &stat_buf);
218
219     if ( result != 0 ) {
220         // stat failed, no file
221         cout << "not found.\n";
222         return 0;
223     } else {
224         // stat succeeded, file exists
225         cout << "exists.\n";
226         return 1;
227     }
228 }
229
230
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];
234     long int index;
235
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) ) {
241             return(1);
242         }
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) ) {
247             return(1);
248         }
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) ) {
253             return(1);
254         }
255     }
256
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) ) {
262             return(1);
263         }
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) ) {
268             return(1);
269         }
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) ) {
274             return(1);
275         }
276     }
277
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) ) {
283             return(1);
284         }
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) ) {
289             return(1);
290         }
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) ) {
295             return(1);
296         }
297     }
298
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) ) {
304             return(1);
305         }
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) ) {
310             return(1);
311         }
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) ) {
316             return(1);
317         }
318     }
319
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) ) {
325             return(1);
326         }
327     }
328
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) ) {
334             return(1);
335         }
336     }
337
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) ) {
343             return(1);
344         }
345     }
346
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) ) {
352             return(1);
353         }
354     }
355
356     return(0);
357 }
358
359
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, 
363               const string& ext)
364 {
365     FILE *fp;
366     string filename;
367
368     // create the output file name
369     filename = basename + ext;
370
371     // check if a shared object already exist from a different tile
372
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";
377         return(NULL);
378     } else {
379         // open the file
380         fp = fopen(filename.c_str(), "w");
381         cout << "Opening " + filename + "\n";
382         return(fp);
383     }
384 }
385
386
387 // dump in WaveFront .obj format
388 void dump_obj(const string& basename, const string& basepath) {
389     Point3D node;
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;
393     double x, y, z;
394
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");
399
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");
404
405     body = my_open(basename, basepath, ".body");
406
407     cout << "Dumping edges file basename:  " + basename + " ...\n";
408
409     // dump vertices
410     cout << "  writing vertices\n";
411
412     iterator_3d last = nodes_orig.end();
413     iterator_3d current = nodes_orig.begin();
414     ++current;
415     for ( ; current != last; ++current) {
416         node = *current;
417
418         if ( (fabs(node.y() - ymin) < FG_EPSILON) && 
419              (fabs(node.x() - xmin) < FG_EPSILON) ) {
420             fp = sw;
421         } else if ( (fabs(node.y() - ymin) < FG_EPSILON) &&
422                     (fabs(node.x() - xmax) < FG_EPSILON) ) {
423             fp = se;
424         } else if ( (fabs(node.y() - ymax) < FG_EPSILON) &&
425                     (fabs(node.x() - xmax) < FG_EPSILON)) {
426             fp = ne;
427         } else if ( (fabs(node.y() - ymax) < FG_EPSILON) &&
428                     (fabs(node.x() - xmin) < FG_EPSILON) ) {
429             fp = nw;
430         } else if ( fabs(node.x() - xmin) < FG_EPSILON ) {
431             fp = west;
432         } else if ( fabs(node.x() - xmax) < FG_EPSILON ) {
433             fp = east;
434         } else if ( fabs(node.y() - ymin) < FG_EPSILON ) {
435             fp = south;
436         } else if ( fabs(node.y() - ymax) < FG_EPSILON ) {
437             fp = north;
438         } else {
439             fp = body;
440         }
441
442         x = node.x();
443         y = node.y();
444         z = node.z();
445
446         if ( fp != NULL ) {
447             fprintf(fp, "gdn %.2f %.2f %.2f\n", x, y, z);
448         }
449     }
450
451     cout << "  calculating and writing normals\n";
452
453     // calculate and generate normals
454     size = nodes_orig.size();
455     for ( i = 1; i < size; i++ ) {
456         // printf("Finding normal\n");
457
458         find_tris(i, &t1, &t2, &t3, &t4, &t5);
459
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;
465
466         count = 1;
467         calc_normal(nodes_cart[tri_list[t1].n1],
468                     nodes_cart[tri_list[t1].n2], 
469                     nodes_cart[tri_list[t1].n3],
470                     n1);
471
472         if ( t2 > 0 ) {
473             calc_normal(nodes_cart[tri_list[t2].n1], 
474                         nodes_cart[tri_list[t2].n2], 
475                         nodes_cart[tri_list[t2].n3],
476                         n2);
477             count = 2;
478         }
479
480         if ( t3 > 0 ) {
481             calc_normal(nodes_cart[tri_list[t3].n1],
482                         nodes_cart[tri_list[t3].n2],
483                         nodes_cart[tri_list[t3].n3],
484                         n3);
485             count = 3;
486         }
487
488         if ( t4 > 0 ) {
489             calc_normal(nodes_cart[tri_list[t4].n1],
490                         nodes_cart[tri_list[t4].n2],
491                         nodes_cart[tri_list[t4].n3],
492                         n4);
493             count = 4;
494         }
495
496         if ( t5 > 0 ) {
497             calc_normal(nodes_cart[tri_list[t5].n1],
498                         nodes_cart[tri_list[t5].n2],
499                         nodes_cart[tri_list[t5].n3],
500                         n5);
501             count = 5;
502         }
503
504         // printf("  norm[2] = %.2f %.2f %.2f\n", n1[2], n2[2], n3[2]);
505
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;
509         
510         // printf("  count = %d\n", count);
511         // printf("  Ave. normal = %.4f %.4f %.4f\n", norm[0], norm[1], 
512         //        norm[2]);
513         MAT3_NORMALIZE_VEC(norm, temp);
514         // printf("  Normalized ave. normal = %.4f %.4f %.4f\n", 
515         //        norm[0], norm[1], norm[2]);
516         
517         fp = NULL;
518
519         if ( (fabs(nodes_orig[i].y() - ymin) < FG_EPSILON) && 
520              (fabs(nodes_orig[i].x() - xmin) < FG_EPSILON) ) {
521             fp = sw;
522         } else if ( (fabs(nodes_orig[i].y() - ymin) < FG_EPSILON) &&
523                     (fabs(nodes_orig[i].x() - xmax) < FG_EPSILON) ) {
524             fp = se;
525         } else if ( (fabs(nodes_orig[i].y() - ymax) < FG_EPSILON) &&
526                     (fabs(nodes_orig[i].x() - xmax) < FG_EPSILON)) {
527             fp = ne;
528         } else if ( (fabs(nodes_orig[i].y() - ymax) < FG_EPSILON) &&
529                     (fabs(nodes_orig[i].x() - xmin) < FG_EPSILON) ) {
530             fp = nw;
531         } else if ( fabs(nodes_orig[i].x() - xmin) < FG_EPSILON ) {
532             fp = west;
533         } else if ( fabs(nodes_orig[i].x() - xmax) < FG_EPSILON ) {
534             fp = east;
535         } else if ( fabs(nodes_orig[i].y() - ymin) < FG_EPSILON ) {
536             fp = south;
537         } else if ( fabs(nodes_orig[i].y() - ymax) < FG_EPSILON ) {
538             fp = north;
539         }
540         if ( fp != NULL ) {
541             fprintf(fp, "vn %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);
542         }
543     }
544
545     if ( sw ) { fclose(sw); }
546     if ( se ) { fclose(se); }
547     if ( ne ) { fclose(ne); }
548     if ( nw ) { fclose(nw); }
549
550     if ( north ) { fclose(north); }
551     if ( south ) { fclose(south); }
552     if ( east ) { fclose(east); }
553     if ( west ) { fclose(west); }
554
555     if ( body ) { fclose(body); }
556 }
557
558
559 int main(int argc, char **argv) {
560     string basename, basepath, temp;
561     fgBUCKET p;
562     long int index;
563     int len;
564
565     basename = argv[1];
566
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";
572
573     // find the index of the current file
574     temp = extract_file(basename);
575     len = temp.length();
576     if ( len >= 2 ) {
577         temp = temp.substr(0, len-2);
578     }
579     index = atoi( temp.c_str() );
580     cout << "index = " << index << "\n";
581     fgBucketParseIndex(index, &p);
582
583     cout << "bucket = " << p.lon << " " << p.lat << " " << 
584         p.x << " " << p.y << "\n";
585
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);
591
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);
596
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);
601           
602
603     // load the input data files
604     triload(basename);
605
606     // dump in WaveFront .obj format
607     dump_obj(basename, basepath);
608
609     return(0);
610 }
611
612
613 // $Log$
614 // Revision 1.5  1998/10/20 15:50:33  curt
615 // whitespace tweak.
616 //
617 // Revision 1.4  1998/10/18 01:17:27  curt
618 // Point3D tweaks.
619 //
620 // Revision 1.3  1998/09/22 23:49:56  curt
621 // C++-ified, STL-ified, and string-ified.
622 //
623 // Revision 1.2  1998/09/21 23:16:23  curt
624 // Converted to c++ style comments.
625 //
626 // Revision 1.1  1998/07/08 14:59:13  curt
627 // *.[ch] renamed to *.[ch]xx
628 //
629 // Revision 1.11  1998/07/04 00:56:40  curt
630 // typedef'd struct fgBUCKET.
631 //
632 // Revision 1.10  1998/05/02 01:54:37  curt
633 // Converting to polar3d.h routines.
634 //
635 // Revision 1.9  1998/04/18 04:01:20  curt
636 // Now use libMath rather than having local copies of math routines.
637 //
638 // Revision 1.8  1998/04/14 02:26:08  curt
639 // Code reorganizations.  Added a Lib/ directory for more general libraries.
640 //
641 // Revision 1.7  1998/04/08 23:21:13  curt
642 // Adopted Gnu automake/autoconf system.
643 //
644 // Revision 1.6  1998/03/03 15:36:13  curt
645 // Tweaks for compiling with g++
646 //
647 // Revision 1.5  1998/03/03 03:37:04  curt
648 // Cumulative tweaks.
649 //
650 // Revision 1.4  1998/01/31 00:41:26  curt
651 // Made a few changes converting floats to doubles.
652 //
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/
655 //
656 // Revision 1.2  1998/01/14 15:54:43  curt
657 // Initial revision completed.
658 //
659 // Revision 1.1  1998/01/14 02:11:31  curt
660 // Initial revision.
661 //
662