]> git.mxchange.org Git - flightgear.git/blob - Tools/process-dem.pl
Initial revision.
[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 $| = 1;                         # flush buffers after every write
30
31
32 # return the file name root (ending at last ".")
33 sub file_root {
34     my($file) = @_;
35     my($pos);
36
37     $pos = rindex($file, ".");
38     return substr($file, 0, $pos);
39 }
40
41
42 # set the FG_ROOT environment variable if it hasn't already been set.
43 if ( $ENV{FG_ROOT} eq "" ) {
44     # look for a file called fgtop as a place marker
45     if ( -e "fgtop" ) {
46         $ENV{FG_ROOT} = ".";
47     } elsif ( -e "../fgtop" ) {
48         $ENV{FG_ROOT} = "..";
49     }
50 }
51
52
53 # 1.  Start with file.dem
54
55 $dem_file = shift(@ARGV);
56 $error = shift(@ARGV);
57 $error += 0.0;
58
59 print "Source file = $dem_file  Error tolerance = $error\n";
60
61 if ( $error < 0.5 ) {
62     die "I doubt you'll be happy with an error tolerance as low as $error.\n";
63 }
64
65 # 2.  dem2node $FG_ROOT dem_file tolerance^2 (meters)
66
67 #     - dem2node .. dem_file 160000
68 #
69 #     splits dem file into 64 file.node's which contain the
70 #     irregularly fitted vertices
71
72 $command = "./Dem2node/demfit $ENV{FG_ROOT} $dem_file $error";
73
74 print "Running '$command'\n";
75
76 open(OUT, "$command |");
77 while ( <OUT> ) {
78     print $_;
79     if ( m/Scenery/ ) {
80         $subdir = $_;
81         $subdir =~ s/Dir = //;
82     }
83 }
84 close(OUT);
85
86
87 # 3.  triangle -q file (Takes file.node and produces file.1.node and
88 #                      file.1.ele)
89
90 $subdir = "../Scenery/w120n030/w111n033";
91 print "Subdirectory for this dem file is $subdir\n";
92
93 @FILES = `ls $subdir`;
94 foreach $file ( @FILES ) {
95     print $file;
96     chop($file);
97     if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
98         $command = "./Triangle/triangle -q $subdir/$file";
99         print "Running '$command'\n";
100         open(OUT, "$command |");
101         while ( <OUT> ) {
102             print $_;
103         }
104         close(OUT);
105
106         # remove input file.node
107         unlink("$subdir/$file");
108     }
109 }
110
111
112 # 4.  fixnode file.dem subdir
113 #
114 #     Take the original .dem file (for interpolating Z values) and the
115 #     subdirecotry containing all the file.1.node's and replace with
116 #     fixed file.1.node
117
118 $command = "./FixNode/fixnode $dem_file $subdir";
119 print "Running '$command'\n";
120 open(OUT, "$command |");
121 while ( <OUT> ) {
122     print $_;
123 }
124 close(OUT);
125
126
127 # 5.  tri2obj file (.1.node)
128 #
129 #     Take the file.1.node and file.1.ele and produce file.1.obj
130
131 @FILES = `ls $subdir`;
132 foreach $file ( @FILES ) {
133     chop($file);
134     if ( $file =~ m/\.1\.node$/ ) {
135         $file =~ s/\.node$//;  # strip off the ".node"
136
137         $command = "./Tri2obj/tri2obj $subdir/$file";
138         print "Running '$command'\n";
139         open(OUT, "$command |");
140         while ( <OUT> ) {
141             print $_;
142         }
143         close(OUT);
144
145         unlink("$subdir/$file.node");
146         unlink("$subdir/$file.node.orig");
147         unlink("$subdir/$file.ele");
148
149     }
150 }
151
152
153 # 6.  strip file.1.obj
154
155 #     Strip the file.1.obj's
156 #
157 # 7.  cp bands.d file.2.obj
158 #
159 #     strips produces a file called "bands.d" ... copy this to file.2.obj
160
161 @FILES = `ls $subdir`;
162 foreach $file ( @FILES ) {
163     chop($file);
164     if ( $file =~ m/\.1\.obj$/ ) {
165         $command = "./Stripe_u/strips $subdir/$file";
166         print "Running '$command'\n";
167         open(OUT, "$command |");
168         while ( <OUT> ) {
169             print $_;
170         }
171         close(OUT);
172
173         # copy to destination file
174         $newfile = $file;
175         $newfile =~ s/\.1\.obj$//;
176         print "Copying to $subdir/$newfile.2.obj\n";
177         open(IN, "<bands.d");
178         open(OUT, ">$subdir/$newfile.2.obj");
179         while ( <IN> ) {
180             print OUT $_;
181         }
182         close(IN);
183         close(OUT);
184
185         unlink("$subdir/$file");
186     }
187 }
188
189
190 # 8.  fixobj file-new
191 #
192 #     Sort file.2.obj by strip winding
193
194 @FILES = `ls $subdir`;
195 foreach $file ( @FILES ) {
196     chop($file);
197     if ( $file =~ m/\.2\.obj$/ ) {
198         $newfile = $file;
199         $newfile =~ s/\.2\.obj$/.obj/;
200
201         $command = "./FixObj/fixobj $subdir/$file $subdir/$newfile";
202         print "Running '$command'\n";
203         open(OUT, "$command |");
204         while ( <OUT> ) {
205             print $_;
206         }
207         close(OUT);
208
209         unlink("$subdir/$file");
210     }
211 }
212
213
214 #---------------------------------------------------------------------------
215 # $Log$
216 # Revision 1.1  1998/01/09 23:06:46  curt
217 # Initial revision.
218 #