]> git.mxchange.org Git - flightgear.git/blob - SplitTris/splittris.cxx
Point3D tweaks.
[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
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>
45
46 // int nodecount, tricount;
47 double xmin, xmax, ymin, ymax;
48
49 // static double nodes_orig[MAX_NODES][3];
50 // static Point3D nodes_cart[MAX_NODES];
51 // static int tris[MAX_TRIS][3];
52
53 container_3d nodes_orig;
54 container_3d nodes_cart;
55 container_tri tri_list;
56
57 fgBUCKET ne_index, nw_index, sw_index, se_index;
58 fgBUCKET north_index, south_index, east_index, west_index;
59
60
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])
64 {
65     double v1[3], v2[3];
66     double temp;
67
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();
70
71     MAT3cross_product(normal, v1, v2);
72     MAT3_NORMALIZE_VEC(normal,temp);
73
74     // printf("  Normal = %.2f %.2f %.2f\n", normal[0], normal[1], normal[2]);
75 }
76
77
78 // return the file base name ( foo/bar/file.ext = file.ext )
79 string extract_file(const string& input) {
80     int pos;
81
82     pos = input.rfind("/");
83     ++pos;
84
85     return input.substr(pos);
86 }
87
88
89 // return the file path name ( foo/bar/file.ext = foo/bar )
90 string extract_path(const string& input) {
91     int pos;
92
93     pos = input.rfind("/");
94
95     return input.substr(0, pos);
96 }
97
98
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) {
101     int i;
102
103     *t1 = *t2 = *t3 = *t4 = *t5 = 0;
104
105     i = 1;
106     iterator_tri last = tri_list.end();
107     iterator_tri current = tri_list.begin();
108
109     // skip first null record
110     ++current;
111
112     for ( ; current != last; ++current )
113     {
114         if ( (n == (*current).n1) || (n == (*current).n2) || 
115              (n == (*current).n3) )
116         {
117             if ( *t1 == 0 ) {
118                 *t1 = i;
119             } else if ( *t2 == 0 ) {
120                 *t2 = i;
121             } else if ( *t3 == 0 ) {
122                 *t3 = i;
123             } else if ( *t4 == 0 ) {
124                 *t4 = i;
125             } else {
126                 *t5 = i;
127             }
128         }
129         ++i;
130     }
131 }
132
133
134 // Initialize a new mesh structure
135 void triload(const string& basename) {
136     string nodename, elename;
137     Point3D node1, node2;
138     triangle tri;
139     int nodecount, tricount, dim, junk1, junk2;
140     int i;
141
142     nodename = basename + ".node";
143     elename  = basename + ".ele";
144
145     cout << "Loading node file:  " + nodename + " ...\n";
146
147     fg_gzifstream node_in( nodename );
148     if ( !node_in ) {
149         cout << "Cannot open file " + nodename + "\n";
150         exit(-1);
151     }
152
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);
159
160     node_in.stream() >> nodecount >> dim >> junk1 >> junk2;
161     cout << "    Expecting " << nodecount << " nodes\n";
162
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);
167
168         node2 = fgGeodToCart(node1);
169         nodes_cart.push_back(node2);
170         // printf("%d %.2f %.2f %.2f\n", junk1, node2.x, node2.y, node2.z);
171
172         if ( i == 1 ) {
173             xmin = xmax = node1.x();
174             ymin = ymax = node1.y();
175         } else {
176             if ( node1.x() < xmin ) {
177                 xmin = node1.x();
178             }
179             if ( node1.x() > xmax ) {
180                 xmax = node1.x();
181             }
182             if ( node1.y() < ymin ) {
183                 ymin = node1.y();
184             }
185             if ( node1.y() > ymax ) {
186                 ymax = node1.y();
187             }
188         }
189     }
190
191     cout << "Loading element file:  " + elename + " ...\n";
192     fg_gzifstream ele_in( elename );
193     if ( !ele_in ) {
194         cout << "Cannot open file " + elename + "\n";
195         exit(-1);
196     }
197
198     ele_in.stream() >> tricount >> junk1 >> junk2;
199     cout << "    Expecting " << tricount << " elements\n";
200
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);
207     }
208 }
209
210
211 // check if a file exists
212 int file_exists(char *file) {
213     struct stat stat_buf;
214     int result;
215
216     cout << "checking " << file << " ... ";
217
218     result = stat(file, &stat_buf);
219
220     if ( result != 0 ) {
221         // stat failed, no file
222         cout << "not found.\n";
223         return 0;
224     } else {
225         // stat succeeded, file exists
226         cout << "exists.\n";
227         return 1;
228     }
229 }
230
231
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];
235     long int index;
236
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) ) {
242             return(1);
243         }
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) ) {
248             return(1);
249         }
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) ) {
254             return(1);
255         }
256     }
257
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) ) {
263             return(1);
264         }
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) ) {
269             return(1);
270         }
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) ) {
275             return(1);
276         }
277     }
278
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) ) {
284             return(1);
285         }
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) ) {
290             return(1);
291         }
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) ) {
296             return(1);
297         }
298     }
299
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) ) {
305             return(1);
306         }
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) ) {
311             return(1);
312         }
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) ) {
317             return(1);
318         }
319     }
320
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) ) {
326             return(1);
327         }
328     }
329
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) ) {
335             return(1);
336         }
337     }
338
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) ) {
344             return(1);
345         }
346     }
347
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) ) {
353             return(1);
354         }
355     }
356
357     return(0);
358 }
359
360
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, 
364               const string& ext)
365 {
366     FILE *fp;
367     string filename;
368
369     // create the output file name
370     filename = basename + ext;
371
372     // check if a shared object already exist from a different tile
373
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";
378         return(NULL);
379     } else {
380         // open the file
381         fp = fopen(filename.c_str(), "w");
382         cout << "Opening " + filename + "\n";
383         return(fp);
384     }
385 }
386
387
388 // dump in WaveFront .obj format
389 void dump_obj(const string& basename, const string& basepath) {
390     Point3D node;
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;
394     double x, y, z;
395
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");
400
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");
405
406     body = my_open(basename, basepath, ".body");
407
408     cout << "Dumping edges file basename:  " + basename + " ...\n";
409
410     // dump vertices
411     cout << "  writing vertices\n";
412
413     iterator_3d last = nodes_orig.end();
414     iterator_3d current = nodes_orig.begin();
415     ++current;
416     for ( ; current != last; ++current) {
417         node = *current;
418
419         if ( (fabs(node.y() - ymin) < FG_EPSILON) && 
420              (fabs(node.x() - xmin) < FG_EPSILON) ) {
421             fp = sw;
422         } else if ( (fabs(node.y() - ymin) < FG_EPSILON) &&
423                     (fabs(node.x() - xmax) < FG_EPSILON) ) {
424             fp = se;
425         } else if ( (fabs(node.y() - ymax) < FG_EPSILON) &&
426                     (fabs(node.x() - xmax) < FG_EPSILON)) {
427             fp = ne;
428         } else if ( (fabs(node.y() - ymax) < FG_EPSILON) &&
429                     (fabs(node.x() - xmin) < FG_EPSILON) ) {
430             fp = nw;
431         } else if ( fabs(node.x() - xmin) < FG_EPSILON ) {
432             fp = west;
433         } else if ( fabs(node.x() - xmax) < FG_EPSILON ) {
434             fp = east;
435         } else if ( fabs(node.y() - ymin) < FG_EPSILON ) {
436             fp = south;
437         } else if ( fabs(node.y() - ymax) < FG_EPSILON ) {
438             fp = north;
439         } else {
440             fp = body;
441         }
442
443         x = node.x();
444         y = node.y();
445         z = node.z();
446
447         if ( fp != NULL ) {
448             fprintf(fp, "gdn %.2f %.2f %.2f\n", x, y, z);
449         }
450     }
451
452     cout << "  calculating and writing normals\n";
453
454     // calculate and generate normals
455     size = nodes_orig.size();
456     for ( i = 1; i < size; i++ ) {
457         // printf("Finding normal\n");
458
459         find_tris(i, &t1, &t2, &t3, &t4, &t5);
460
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;
466
467         count = 1;
468         calc_normal(nodes_cart[tri_list[t1].n1],
469                     nodes_cart[tri_list[t1].n2], 
470                     nodes_cart[tri_list[t1].n3],
471                     n1);
472
473         if ( t2 > 0 ) {
474             calc_normal(nodes_cart[tri_list[t2].n1], 
475                         nodes_cart[tri_list[t2].n2], 
476                         nodes_cart[tri_list[t2].n3],
477                         n2);
478             count = 2;
479         }
480
481         if ( t3 > 0 ) {
482             calc_normal(nodes_cart[tri_list[t3].n1],
483                         nodes_cart[tri_list[t3].n2],
484                         nodes_cart[tri_list[t3].n3],
485                         n3);
486             count = 3;
487         }
488
489         if ( t4 > 0 ) {
490             calc_normal(nodes_cart[tri_list[t4].n1],
491                         nodes_cart[tri_list[t4].n2],
492                         nodes_cart[tri_list[t4].n3],
493                         n4);
494             count = 4;
495         }
496
497         if ( t5 > 0 ) {
498             calc_normal(nodes_cart[tri_list[t5].n1],
499                         nodes_cart[tri_list[t5].n2],
500                         nodes_cart[tri_list[t5].n3],
501                         n5);
502             count = 5;
503         }
504
505         // printf("  norm[2] = %.2f %.2f %.2f\n", n1[2], n2[2], n3[2]);
506
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;
510         
511         // printf("  count = %d\n", count);
512         // printf("  Ave. normal = %.4f %.4f %.4f\n", norm[0], norm[1], 
513         //        norm[2]);
514         MAT3_NORMALIZE_VEC(norm, temp);
515         // printf("  Normalized ave. normal = %.4f %.4f %.4f\n", 
516         //        norm[0], norm[1], norm[2]);
517         
518         fp = NULL;
519
520         if ( (fabs(nodes_orig[i].y() - ymin) < FG_EPSILON) && 
521              (fabs(nodes_orig[i].x() - xmin) < FG_EPSILON) ) {
522             fp = sw;
523         } else if ( (fabs(nodes_orig[i].y() - ymin) < FG_EPSILON) &&
524                     (fabs(nodes_orig[i].x() - xmax) < FG_EPSILON) ) {
525             fp = se;
526         } else if ( (fabs(nodes_orig[i].y() - ymax) < FG_EPSILON) &&
527                     (fabs(nodes_orig[i].x() - xmax) < FG_EPSILON)) {
528             fp = ne;
529         } else if ( (fabs(nodes_orig[i].y() - ymax) < FG_EPSILON) &&
530                     (fabs(nodes_orig[i].x() - xmin) < FG_EPSILON) ) {
531             fp = nw;
532         } else if ( fabs(nodes_orig[i].x() - xmin) < FG_EPSILON ) {
533             fp = west;
534         } else if ( fabs(nodes_orig[i].x() - xmax) < FG_EPSILON ) {
535             fp = east;
536         } else if ( fabs(nodes_orig[i].y() - ymin) < FG_EPSILON ) {
537             fp = south;
538         } else if ( fabs(nodes_orig[i].y() - ymax) < FG_EPSILON ) {
539             fp = north;
540         }
541         if ( fp != NULL ) {
542             fprintf(fp, "vn %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);
543         }
544     }
545
546     if ( sw ) { fclose(sw); }
547     if ( se ) { fclose(se); }
548     if ( ne ) { fclose(ne); }
549     if ( nw ) { fclose(nw); }
550
551     if ( north ) { fclose(north); }
552     if ( south ) { fclose(south); }
553     if ( east ) { fclose(east); }
554     if ( west ) { fclose(west); }
555
556     if ( body ) { fclose(body); }
557 }
558
559
560 int main(int argc, char **argv) {
561     string basename, basepath, temp;
562     fgBUCKET p;
563     long int index;
564     int len;
565
566     basename = argv[1];
567
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";
573
574     // find the index of the current file
575     temp = extract_file(basename);
576     len = temp.length();
577     if ( len >= 2 ) {
578         temp = temp.substr(0, len-2);
579     }
580     index = atoi( temp.c_str() );
581     cout << "index = " << index << "\n";
582     fgBucketParseIndex(index, &p);
583
584     cout << "bucket = " << p.lon << " " << p.lat << " " << 
585         p.x << " " << p.y << "\n";
586
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);
592
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);
597
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);
602           
603
604     // load the input data files
605     triload(basename);
606
607     // dump in WaveFront .obj format
608     dump_obj(basename, basepath);
609
610     return(0);
611 }
612
613
614 // $Log$
615 // Revision 1.4  1998/10/18 01:17:27  curt
616 // Point3D tweaks.
617 //
618 // Revision 1.3  1998/09/22 23:49:56  curt
619 // C++-ified, STL-ified, and string-ified.
620 //
621 // Revision 1.2  1998/09/21 23:16:23  curt
622 // Converted to c++ style comments.
623 //
624 // Revision 1.1  1998/07/08 14:59:13  curt
625 // *.[ch] renamed to *.[ch]xx
626 //
627 // Revision 1.11  1998/07/04 00:56:40  curt
628 // typedef'd struct fgBUCKET.
629 //
630 // Revision 1.10  1998/05/02 01:54:37  curt
631 // Converting to polar3d.h routines.
632 //
633 // Revision 1.9  1998/04/18 04:01:20  curt
634 // Now use libMath rather than having local copies of math routines.
635 //
636 // Revision 1.8  1998/04/14 02:26:08  curt
637 // Code reorganizations.  Added a Lib/ directory for more general libraries.
638 //
639 // Revision 1.7  1998/04/08 23:21:13  curt
640 // Adopted Gnu automake/autoconf system.
641 //
642 // Revision 1.6  1998/03/03 15:36:13  curt
643 // Tweaks for compiling with g++
644 //
645 // Revision 1.5  1998/03/03 03:37:04  curt
646 // Cumulative tweaks.
647 //
648 // Revision 1.4  1998/01/31 00:41:26  curt
649 // Made a few changes converting floats to doubles.
650 //
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/
653 //
654 // Revision 1.2  1998/01/14 15:54:43  curt
655 // Initial revision completed.
656 //
657 // Revision 1.1  1998/01/14 02:11:31  curt
658 // Initial revision.
659 //
660