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