]> git.mxchange.org Git - flightgear.git/blob - Tools/process-dem.pl
Tweaked to add next stage.
[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 $do_demfit =     0;
32 $do_triangle_1 = 0;
33 $do_fixnode =    0;
34 $do_splittris =  0;
35 $do_assemtris =  1;
36
37 $do_tri2obj =    0;
38 $do_strips =     0;
39 $do_fixobj =     0;
40
41
42 # return the file name root (ending at last ".")
43 sub file_root {
44     my($file) = @_;
45     my($pos);
46
47     $pos = rindex($file, ".");
48     return substr($file, 0, $pos);
49 }
50
51
52 # set the FG_ROOT environment variable if it hasn't already been set.
53 if ( $ENV{FG_ROOT} eq "" ) {
54     # look for a file called fgtop as a place marker
55     if ( -e "fgtop" ) {
56         $ENV{FG_ROOT} = ".";
57     } elsif ( -e "../fgtop" ) {
58         $ENV{FG_ROOT} = "..";
59     }
60 }
61
62
63 # 1.  Start with file.dem
64
65 $dem_file = shift(@ARGV);
66 $error = shift(@ARGV);
67 $error += 0.0;
68
69 print "Source file = $dem_file  Error tolerance = $error\n";
70
71 if ( $error < 0.5 ) {
72     die "I doubt you'll be happy with an error tolerance as low as $error.\n";
73 }
74
75 # 2.  dem2node $FG_ROOT dem_file tolerance^2 (meters)
76
77 #     - dem2node .. dem_file 160000
78 #
79 #     splits dem file into 64 file.node's which contain the
80 #     irregularly fitted vertices
81
82 if ( $do_demfit ) {
83     $command = "./Dem2node/demfit $ENV{FG_ROOT} $dem_file $error";
84
85     print "Running '$command'\n";
86
87     open(OUT, "$command |");
88     while ( <OUT> ) {
89         print $_;
90         if ( m/^Dir = / ) {
91             $subdir = $_;
92             $subdir =~ s/^Dir = //;
93             chop($subdir);
94         }
95     }
96     close(OUT);
97 } else {
98     $subdir = "../Scenery/w120n030/w111n033";
99     print "WARNING:  Hardcoding subdir = $subdir\n";
100 }
101
102 # 3.  triangle -q file (Takes file.node and produces file.1.node and
103 #                      file.1.ele)
104
105 print "Subdirectory for this dem file is $subdir\n";
106
107 if ( $do_triangle_1 ) {
108     @FILES = `ls $subdir`;
109     foreach $file ( @FILES ) {
110         print $file;
111         chop($file);
112         if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
113             $command = "./Triangle/triangle -q $subdir/$file";
114             print "Running '$command'\n";
115             open(OUT, "$command |");
116             while ( <OUT> ) {
117                 print $_;
118             }
119             close(OUT);
120
121             # remove input file.node
122             unlink("$subdir/$file");
123         }
124     }
125 }
126
127 # 4.  fixnode file.dem subdir
128 #
129 #     Take the original .dem file (for interpolating Z values) and the
130 #     subdirecotry containing all the file.1.node's and replace with
131 #     fixed file.1.node
132
133 if ( $do_fixnode ) {
134     $command = "./FixNode/fixnode $dem_file $subdir";
135     print "Running '$command'\n";
136     open(OUT, "$command |");
137     while ( <OUT> ) {
138         print $_;
139     }
140     close(OUT);
141 }
142
143
144 # 4.1 splittris file (.1.node) (.1.ele)
145
146 #     Extract the corner, edge, and body vertices (in original
147 #     geodetic coordinates) and normals (in cartesian coordinates) and
148 #     save them in something very close to the .obj format as file.se,
149 #     file.sw, file.nw, file.ne, file.north, file.south, file.east,
150 #     file.west, and file.body.  This way we can reconstruct the
151 #     region using consistant edges and corners.  
152
153 #     Arbitration rules: If an opposite edge file already exists,
154 #     don't create our matching edge.  If a corner already exists,
155 #     don't create ours.  Basically, the early bird gets the worm and
156 #     gets to define the edge verticies and normals.  All the other
157 #     adjacent tiles must use these.
158
159 if ( $do_splittris ) {
160     @FILES = `ls $subdir`;
161     foreach $file ( @FILES ) {
162         chop($file);
163         if ( $file =~ m/\.1\.node$/ ) {
164             $file =~ s/\.node$//;  # strip off the ".node"
165         
166             $command = "./SplitTris/splittris $subdir/$file";
167             print "Running '$command'\n";
168             open(OUT, "$command |");
169             while ( <OUT> ) {
170                 print $_;
171             }
172             close(OUT);
173
174             unlink("$subdir/$file.node");
175             unlink("$subdir/$file.node.orig");
176             unlink("$subdir/$file.ele");
177         }
178     }
179 }
180
181
182 # 4.2 read in the split of version of the tiles, reconstruct the tile
183 #     using the proper shared corners and edges.  Save as a node file
184 #     so we can retriangulate.
185
186 if ( $do_assemtris ) {
187     @FILES = `ls $subdir`;
188     foreach $file ( @FILES ) {
189         chop($file);
190         if ( $file =~ m/\.1\.body$/ ) {
191             $file =~ s/\.body$//;  # strip off the ".node"
192         
193             $command = "./AssemTris/assemtris $subdir/$file";
194             print "Running '$command'\n";
195             open(OUT, "$command |");
196             while ( <OUT> ) {
197                 print $_;
198             }
199             close(OUT);
200         }
201     }
202 }
203
204
205 # 4.3 Retriangulate fixed up files (without -q option)
206
207
208 # 5.  tri2obj file (.1.node) (.1.ele)
209 #
210 #     Take the file.1.node and file.1.ele and produce file.1.obj
211
212 if ( $do_tri2obj ) {
213     @FILES = `ls $subdir`;
214     foreach $file ( @FILES ) {
215         chop($file);
216         if ( $file =~ m/\.1\.node$/ ) {
217             $file =~ s/\.node$//;  # strip off the ".node"
218             
219             $command = "./Tri2obj/tri2obj $subdir/$file";
220             print "Running '$command'\n";
221             open(OUT, "$command |");
222             while ( <OUT> ) {
223                 print $_;
224             }
225             close(OUT);
226             
227             unlink("$subdir/$file.node");
228             unlink("$subdir/$file.node.orig");
229             unlink("$subdir/$file.ele");
230         }
231     }
232 }
233
234
235 # 6.  strip file.1.obj
236
237 #     Strip the file.1.obj's
238 #
239 # 7.  cp bands.d file.2.obj
240 #
241 #     strips produces a file called "bands.d" ... copy this to file.2.obj
242
243 if ( $do_strips ) {
244     @FILES = `ls $subdir`;
245     foreach $file ( @FILES ) {
246         chop($file);
247         if ( $file =~ m/\.1\.obj$/ ) {
248             $command = "./Stripe_u/strips $subdir/$file";
249             print "Running '$command'\n";
250             open(OUT, "$command |");
251             while ( <OUT> ) {
252                 print $_;
253             }
254             close(OUT);
255             
256             # copy to destination file
257             $newfile = $file;
258             $newfile =~ s/\.1\.obj$//;
259             print "Copying to $subdir/$newfile.2.obj\n";
260             open(IN, "<bands.d");
261             open(OUT, ">$subdir/$newfile.2.obj");
262             while ( <IN> ) {
263                 print OUT $_;
264             }
265             close(IN);
266             close(OUT);
267             
268             unlink("$subdir/$file");
269         }
270     }
271 }
272
273
274 # 8.  fixobj file-new
275 #
276 #     Sort file.2.obj by strip winding
277
278 if ( $do_fixobj ) {
279     @FILES = `ls $subdir`;
280     foreach $file ( @FILES ) {
281         chop($file);
282         if ( $file =~ m/\.2\.obj$/ ) {
283             $newfile = $file;
284             $newfile =~ s/\.2\.obj$/.obj/;
285             
286             $command = "./FixObj/fixobj $subdir/$file $subdir/$newfile";
287             print "Running '$command'\n";
288             open(OUT, "$command |");
289             while ( <OUT> ) {
290                 print $_;
291             }
292             close(OUT);
293
294             unlink("$subdir/$file");
295         }
296     }
297 }
298
299
300 #---------------------------------------------------------------------------
301 # $Log$
302 # Revision 1.5  1998/01/15 02:50:08  curt
303 # Tweaked to add next stage.
304 #
305 # Revision 1.4  1998/01/14 15:55:34  curt
306 # Finished splittris, started assemtris.
307 #
308 # Revision 1.3  1998/01/14 02:15:52  curt
309 # Updated front end script to keep plugging away on tile fitting.
310 #
311 # Revision 1.2  1998/01/12 20:42:08  curt
312 # Working on fitting tiles together in a seamless manner.
313 #
314 # Revision 1.1  1998/01/09 23:06:46  curt
315 # Initial revision.
316 #