]> git.mxchange.org Git - flightgear.git/blob - Tools/process-dem.pl
Working on preparationsn for triangulation.
[flightgear.git] / Tools / process-dem.pl
1 #!/usr/bin/perl
2
3 #---------------------------------------------------------------------------
4 # Toplevel script to automate DEM file processing and conversion
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
29 # format version number
30 $scenery_format_version = "0.1";
31
32 $max_area = 10000;            # maximum triangle area
33 $remove_tmps = 1;
34
35 $| = 1;                         # flush buffers after every write
36
37 $do_dem2node =   1;
38 $do_triangle_1 = 1;
39 $do_fixnode =    1;
40 $do_splittris =  1;
41 $do_assemtris =  1;
42 $do_triangle_2 = 1;
43
44 $do_tri2obj =    1;
45 $do_strips =     1;
46 $do_fixobj =     1;
47  
48 $do_install =    1;
49
50
51 if ( $#ARGV < 3 ) {
52     die "Usage: $0 <fg-root-dir> <work-dir> <error^2> dem-file(s)\n";
53 }
54
55 # Start with file.dem
56
57 $fg_root = shift(@ARGV);
58 $work_dir = shift(@ARGV);
59 $error = shift(@ARGV);
60 $error += 0.0;
61 $system_name = `uname -s`; chop($system_name);
62
63 while ( $dem_file = shift(@ARGV) ) {
64     print "Source file = $dem_file  Error tolerance = $error\n";
65
66     if ( $error < 0.5 ) {
67         die "I doubt you'll be happy with an error tolerance as " . 
68             "low as $error.\n";
69     }
70
71
72     if ( $do_dem2node ) {
73         dem2node() ;
74     } else {
75         $subdir = "./work/Scenery/w120n030/w111n033";
76         print "WARNING:  Hardcoding subdir = $subdir\n";
77     }
78
79     triangle_1() if ( $do_triangle_1 );
80     fixnode() if ( $do_fixnode );
81     splittris() if ( $do_splittris );
82     assemtris() if ( $do_assemtris );
83     triangle_2() if ( $do_triangle_2);
84     tri2obj() if ( $do_tri2obj );
85     strips() if ( $do_strips );
86     fixobj() if ( $do_fixobj );
87     install() if ( $do_install );
88 }
89
90
91 # exit normally
92 exit(0);
93
94
95 # replace all unix forward slashes with windoze backwards slashes if
96 # running on a cygwin32 system
97 sub fix_slashes { my($in) = @_;
98     if ( $system_name =~ m/CYGWIN32/ ) { 
99         $in =~ s/\/+/\//g;
100         $in =~ s/\//\\/g;
101     }
102
103     return($in);
104 }
105
106
107 # return the file name root (ending at last ".")
108 sub file_root {
109     my($file) = @_;
110     my($pos);
111
112     $pos = rindex($file, ".");
113     return substr($file, 0, $pos);
114 }
115
116
117 # 1.  dem2node work_dir dem_file tolerance^2 (meters)
118
119 #     - dem2node .. dem_file 160000
120 #
121 #     splits dem file into 64 file.node's which contain the
122 #     irregularly fitted vertices
123
124 sub dem2node {
125     $command = "Dem2node/dem2node $work_dir $dem_file $error";
126     $command = fix_slashes($command);
127     print "Running '$command'\n";
128
129     open(OUT, "$command |");
130     while ( <OUT> ) {
131         print $_;
132         if ( m/^Dir = / ) {
133             $subdir = $_;
134             $subdir =~ s/^Dir = //;
135             chop($subdir);
136         }
137         if ( m/Quad name field/ ) {
138             $quad_name = $_;
139             # strip header
140             $quad_name =~ s/.*Quad name field: //;
141             # crunch consequetive spaces
142             $quad_name =~ s/  +/ /g;
143             chop($quad_name);
144             print "QUAD NAME = $quad_name\n";
145         }
146     }
147     close(OUT);
148
149
150
151 # 2.  triangle -q file (Takes file.node and produces file.1.node and
152 #                      file.1.ele)
153
154 print "Subdirectory for this dem file is $subdir\n";
155
156 sub triangle_1 {
157     @FILES = `ls $subdir`;
158     foreach $file ( @FILES ) {
159         # print $file;
160         chop($file);
161         if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
162             # special handling is needed if .poly file exists
163             $fileroot = $file;
164             $fileroot =~ s/\.node$//;
165             print "$subdir/$fileroot\n";
166             $command = "Triangle/triangle";
167             $command = fix_slashes($command);
168             if ( -r "$subdir/$fileroot.poly" ) {
169                 $command .= " -pc";
170             }
171             $command .= " -a$max_area -q10 $subdir/$file";
172             print "Running '$command'\n";
173             open(OUT, "$command |");
174             while ( <OUT> ) {
175                 print $_;
176             }
177             close(OUT);
178
179             # remove input file.node
180             if ( $remove_tmps ) {
181                 $file1 = "$subdir/$file";
182                 $file1 = fix_slashes($file1);
183                 unlink($file1);
184             }
185         }
186     }
187 }
188
189
190 # 3.  fixnode file.dem subdir
191 #
192 #     Take the original .dem file (for interpolating Z values) and the
193 #     subdirecotry containing all the file.1.node's and replace with
194 #     fixed file.1.node
195
196 sub fixnode {
197     $command = "FixNode/fixnode";
198     $command = fix_slashes($command);
199     $command .= " $dem_file $subdir";
200     print "Running '$command'\n";
201     open(OUT, "$command |") || die "cannot run command\n";
202     while ( <OUT> ) {
203         print $_;
204     }
205     close(OUT);
206 }
207
208
209 # 4.1 splittris file (.1.node) (.1.ele)
210
211 #     Extract the corner, edge, and body vertices (in original
212 #     geodetic coordinates) and normals (in cartesian coordinates) and
213 #     save them in something very close to the .obj format as file.se,
214 #     file.sw, file.nw, file.ne, file.north, file.south, file.east,
215 #     file.west, and file.body.  This way we can reconstruct the
216 #     region using consistant edges and corners.  
217
218 #     Arbitration rules: If an opposite edge file already exists,
219 #     don't create our matching edge.  If a corner already exists,
220 #     don't create ours.  Basically, the early bird gets the worm and
221 #     gets to define the edge verticies and normals.  All the other
222 #     adjacent tiles must use these.
223
224 sub splittris {
225     @FILES = `ls $subdir`;
226     foreach $file ( @FILES ) {
227         chop($file);
228         if ( $file =~ m/\.1\.node$/ ) {
229             $file =~ s/\.node$//;  # strip off the ".node"
230         
231             $command = "SplitTris/splittris";
232             $command = fix_slashes($command);
233             $command .= " $subdir/$file";
234             print "Running '$command'\n";
235             open(OUT, "$command |");
236             while ( <OUT> ) {
237                 print $_;
238             }
239             close(OUT);
240
241             if ( $remove_tmps ) {
242                 $file1 = "$subdir/$file.node";
243                 $file1 = fix_slashes($file1);
244                 unlink($file1);
245                 $file1 = "$subdir/$file.node.orig";
246                 $file1 = fix_slashes($file1);
247                 unlink($file1);
248                 $file1 = "$subdir/$file.ele";
249                 $file1 = fix_slashes($file1);
250                 unlink($file1);
251             }
252         }
253     }
254 }
255
256
257 # 4.2 read in the split of version of the tiles, reconstruct the tile
258 #     using the proper shared corners and edges.  Save as a node file
259 #     so we can retriangulate.
260
261 sub assemtris {
262     @FILES = `ls $subdir`;
263     foreach $file ( @FILES ) {
264         chop($file);
265         if ( $file =~ m/\.1\.body$/ ) {
266             $file =~ s/\.1\.body$//;  # strip off the ".body"
267         
268             $command = "AssemTris/assemtris";
269             $command = fix_slashes($command);
270             $command .= " $subdir/$file";
271             print "Running '$command'\n";
272             open(OUT, "$command |");
273             while ( <OUT> ) {
274                 print $_;
275             }
276             close(OUT);
277         }
278         if ( $remove_tmps ) {
279             $file1 = "$subdir/$file.body";
280             $file1 = fix_slashes($file1);
281             unlink($file1);
282         }
283     }
284 }
285
286
287 # 4.3 Retriangulate reassembled files (without -q option) so no new
288 #     nodes are generated.
289
290 sub triangle_2 {
291     @FILES = `ls $subdir`;
292     foreach $file ( @FILES ) {
293         # print $file;
294         chop($file);
295         if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
296             $base = $file;
297             $base =~ s/\.node$//;
298             print("Test for $subdir/$base.q\n");
299
300             $command = "Triangle/triangle";
301             $command = fix_slashes($command);
302
303             if ( -r "$subdir/$base.q" ) {
304                 # if triangle hangs, we can create a filebase.q for
305                 # the file it hung on.  Then, we test for that file
306                 # here which causes the incremental algorithm to run
307                 # (which shouldn't ever hang.)
308                 $command .= " -i";
309             }
310
311             if ( -r "$subdir/$base.poly" ) {
312                 $command .= " -pc $subdir/$base";
313             } else {
314                 $command .= " $subdir/$file";
315             }
316
317             print "Running '$command'\n";
318             open(OUT, "$command |");
319             while ( <OUT> ) {
320                 print $_;
321             }
322             close(OUT);
323
324             # remove input file.node
325             if ( $remove_tmps ) {
326                 $file1 = "$subdir/$file";
327                 $file1 = fix_slashes($file1);
328                 unlink($file1);
329             }
330         }
331     }
332 }
333
334
335 # 5.  tri2obj file (.1.node) (.1.ele)
336 #
337 #     Take the file.1.node and file.1.ele and produce file.1.obj
338 #
339 #     Extracts normals out of the shared edge/vertex files, and uses
340 #     the precalcuated normals for these nodes instead of calculating
341 #     new ones.  By sharing normals as well as vertices, not only are
342 #     the gaps between tiles eliminated, but the colors and lighting
343 #     transition smoothly across tile boundaries.
344
345 sub tri2obj {
346     @FILES = `ls $subdir`;
347     foreach $file ( @FILES ) {
348         chop($file);
349         if ( $file =~ m/\.1\.node$/ ) {
350             $file =~ s/\.node$//;  # strip off the ".node"
351             
352             $command = "Tri2obj/tri2obj";
353             $command = fix_slashes($command);
354             $command .= " $subdir/$file";
355             print "Running '$command'\n";
356             open(OUT, "$command |");
357             while ( <OUT> ) {
358                 print $_;
359             }
360             close(OUT);
361             
362             if ( $remove_tmps ) {
363                 $file1 = "$subdir/$file.node";
364                 $file1 = fix_slashes($file1);
365                 unlink($file1);
366                 $file1 = "$subdir/$file.node.orig";
367                 $file1 = fix_slashes($file1);
368                 unlink($file1);
369                 $file1 = "$subdir/$file.ele";
370                 $file1 = fix_slashes($file1);
371                 unlink($file1);
372             }
373         }
374     }
375 }
376
377
378 # 6.  strip file.1.obj
379
380 #     Strip the file.1.obj's.  Note, strips doesn't handle the minimal
381 #     case of striping a square correctly.
382 #
383 # 7.  cp stripe.objf file.2.obj
384 #
385 #     strips produces a file called "stripe.objf" ... copy this to file.2.obj
386
387 sub strips {
388     @FILES = `ls $subdir`;
389     foreach $file ( @FILES ) {
390         chop($file);
391         if ( $file =~ m/\.1\.obj$/ ) {
392             $newfile = $file;
393             $newfile =~ s/\.1\.obj$//;
394             $command = "Stripe_w/strips";
395             $command = fix_slashes($command);
396             $command .= " $subdir/$file $subdir/$newfile.2.obj";
397             print "Running '$command'\n";
398             # $input = <STDIN>;
399             open(OUT, "$command |");
400             while ( <OUT> ) {
401                 print $_;
402             }
403             close(OUT);
404             
405             # copy to destination file
406             # $newfile = $file;
407             # $newfile =~ s/\.1\.obj$//;
408             # print "Copying to $subdir/$newfile.2.obj\n";
409             # open(IN, "<bands.d");
410             # open(IN, "<stripe.objf");
411             # open(OUT, ">$subdir/$newfile.2.obj");
412             # while ( <IN> ) {
413                 # print OUT $_;
414             # }
415             # close(IN);
416             # close(OUT);
417             
418             if ( $remove_tmps ) {
419                 $file1 = "$subdir/$file";
420                 $file1 = fix_slashes($file1);
421                 unlink($file1);
422             }
423         }
424     }
425 }
426
427
428 # 8.  fixobj file-new
429 #
430 #     Sort file.2.obj by strip winding
431
432 sub fixobj {
433     @FILES = `ls $subdir`;
434     foreach $file ( @FILES ) {
435         chop($file);
436         if ( $file =~ m/\.2\.obj$/ ) {
437             $newfile = $file;
438             $newfile =~ s/\.2\.obj$/.obj/;
439             
440             $command = "FixObj/fixobj";
441             $command = fix_slashes($command);
442             $command .= " $subdir/$file $subdir/$newfile";
443             print "Running '$command'\n";
444             open(OUT, "$command |");
445             while ( <OUT> ) {
446                 print $_;
447             }
448             close(OUT);
449
450             if ( $remove_tmps ) {
451                 $file1 = "$subdir/$file";
452                 $file1 = fix_slashes($file1);
453                 unlink($file1);
454             }
455         }
456     }
457 }
458
459
460 # 9.  install
461 #
462 #     rename, compress, and install scenery files
463
464 sub install {
465     $tmp = $subdir;
466     $tmp =~ s/$work_dir//;
467     # print "Temp dir = $tmp\n";
468     $install_dir = "$fg_root/$tmp";
469
470     # try to get rid of double //
471     $install_dir =~ s/\/+/\//g;
472     print "Install dir = $install_dir\n";
473
474     if ( $system_name !~ m/CYGWIN32/ ) {
475         $command = "mkdir -p $install_dir";
476     } else {
477         $command = "Makedir/makedir $install_dir";
478         $command = fix_slashes($command);
479     }
480
481     # print "Running '$command'\n";
482     open(OUT, "$command |");
483     while ( <OUT> ) {
484         print $_;
485     }
486     close(OUT);
487
488     # write out version and info record
489     $version_file = "$install_dir/VERSION";
490     open(VERSION, ">$version_file") || 
491         die "Cannot open $version_file for writing\n";
492     print VERSION "FGFS Scenery Version $scenery_format_version\n";
493     if ( $system_name !~ m/CYGWIN32/ ) {
494         $date = `date`; chop($date);
495     } else {
496         # ???
497         $date = "not available";
498     }
499     $hostname = `hostname`; chop($hostname);
500     print VERSION "Creator = $ENV{LOGNAME}\n";
501     print VERSION "Date = $date\n";
502     print VERSION "Machine = $hostname\n";
503     print VERSION "\n";
504     print VERSION "DEM File Name = $dem_file\n";
505     print VERSION "DEM Label = $quad_name\n";
506     print VERSION "Error Tolerance = $error (this value is squared)\n";
507     close(VERSION);
508
509     @FILES = `ls $subdir`;
510     foreach $file ( @FILES ) {
511         chop($file);
512         if ( $file =~ m/\d\d.obj$/ ) {
513             $new_file = file_root($file);
514             
515             $command = 
516                 "gzip -c --best -v < $subdir/$file > $install_dir/$new_file.gz";
517             $command = fix_slashes($command);
518
519             print "Running '$command'\n";
520             open(OUT, "$command |");
521             while ( <OUT> ) {
522                 print $_;
523             }
524             close(OUT);
525
526             if ( $remove_tmps ) {
527                 $file1 = "$subdir/$file";
528                 $file1 = fix_slashes($file1);
529                 unlink($file1);
530             }
531         } elsif ( $file =~ m/\d\d.apt$/ ) {
532             $command = "cp $subdir/$file $install_dir/$file";
533             $command = fix_slashes($command);
534             print "Running '$command'\n";
535             open(OUT, "$command |");
536             while ( <OUT> ) {
537                 print $_;
538             }
539             close(OUT);
540         }
541     }
542 }
543
544
545 #---------------------------------------------------------------------------
546 # $Log$
547 # Revision 1.32  1998/11/20 01:02:55  curt
548 # Speedups for win32.
549 #
550 # Revision 1.31  1998/10/28 19:39:06  curt
551 # Changes to better support win32 scenery development.
552 #
553 # Revision 1.30  1998/10/22 22:00:10  curt
554 # Modified the info that is put in the VERSION file.
555 #
556 # Revision 1.29  1998/10/02 21:41:56  curt
557 # Added Makedir + fixes for win32.
558 #
559 # Revision 1.28  1998/09/17 18:40:15  curt
560 # Changes to allow multiple copies of the scenery processing tools
561 # to be run concurrently.
562 #
563 # Revision 1.27  1998/09/09 20:58:35  curt
564 # Fixes and tweaks to handle area cutouts for airports.
565 #
566 # Revision 1.26  1998/08/26 22:31:29  curt
567 # Write out version and "meta" info into each dem's subdirectory containing
568 # all the tiles.
569 #
570 # Revision 1.25  1998/07/22 21:46:09  curt
571 # minor tweaks.
572 #
573 # Revision 1.24  1998/07/21 04:33:47  curt
574 # More tweaks for sub-area cutouts.
575 #
576 # Revision 1.23  1998/07/20 12:55:35  curt
577 # Several tweaks to start incorporating area cutouts into the pipeline.
578 #
579 # Revision 1.22  1998/07/08 14:49:13  curt
580 # tweaks.
581 #
582 # Revision 1.21  1998/06/08 17:18:37  curt
583 # Mods to test new Stripe fixes from Wilbur Streett.
584 #
585 # Revision 1.20  1998/06/05 18:20:24  curt
586 # Added DemInfo to dump out "A" record DEM info.
587 # Modified process-dem.pl to work in a temp directory and compress/copy the
588 # result to the final destination.
589 #
590 # Revision 1.19  1998/05/27 02:25:26  curt
591 # Added a flag to the first run of "triangle" to impose a maximum triangle
592 # size.  This forces really flat areas to be subdivided a certain amount
593 # anyways.  This makes for slightly more interesting scenery.
594 #
595 # Revision 1.18  1998/05/20 20:55:40  curt
596 # Makefile tweaks
597 #
598 # Revision 1.17  1998/04/28 01:23:25  curt
599 # Added a work around so that we can get past the "triangle" program
600 # hanging, by aborting and rerunning with that tile marked to use the "-i"
601 # option.
602 #
603 # Revision 1.16  1998/04/18 03:57:53  curt
604 # Added zlib library support.
605 #
606 # Revision 1.15  1998/04/08 23:24:07  curt
607 # Adopted Gnu automake/autoconf system.
608 #
609 # Revision 1.14  1998/04/06 21:09:38  curt
610 # Additional win32 support.
611 # Fixed a bad bug in dem file parsing that was causing the output to be
612 # flipped about x = y.
613 #
614 # Revision 1.13  1998/03/19 02:52:52  curt
615 # Updated to reflect some minor tool reorganization and the creation of class
616 # to handle DEM processing needs.
617 #
618 # Revision 1.12  1998/03/19 01:48:35  curt
619 # Added gpc-2.01 (generic polygon clipping library)
620 #
621 # Revision 1.11  1998/03/03 03:36:57  curt
622 # Cumulative tweaks.
623 #
624 # Revision 1.10  1998/02/01 03:42:26  curt
625 # Modifications to handle compressed dem files.
626 #
627 # Revision 1.9  1998/01/27 18:36:54  curt
628 # Lots of updates to get back in sync with changes made over in .../Src/
629 #
630 # Revision 1.8  1998/01/21 17:59:05  curt
631 # Uncomment lines to remove several intermediate files.
632 #
633 # Revision 1.7  1998/01/19 19:51:06  curt
634 # A couple final pre-release tweaks.
635 #
636 # Revision 1.6  1998/01/15 21:33:33  curt
637 # Assembling triangles and building a new .node file with the proper shared
638 # vertices now works.  Now we just have to use the shared normals and we'll
639 # be all set.
640 #
641 # Revision 1.5  1998/01/15 02:50:08  curt
642 # Tweaked to add next stage.
643 #
644 # Revision 1.4  1998/01/14 15:55:34  curt
645 # Finished splittris, started assemtris.
646 #
647 # Revision 1.3  1998/01/14 02:15:52  curt
648 # Updated front end script to keep plugging away on tile fitting.
649 #
650 # Revision 1.2  1998/01/12 20:42:08  curt
651 # Working on fitting tiles together in a seamless manner.
652 #
653 # Revision 1.1  1998/01/09 23:06:46  curt
654 # Initial revision.
655 #