]> git.mxchange.org Git - flightgear.git/blob - Tools/process-dem.pl
Cumulative tweaks.
[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 =     1;
32 $do_triangle_1 = 1;
33 $do_fixnode =    1;
34 $do_splittris =  1;
35 $do_assemtris =  1;
36 $do_triangle_2 = 1;
37
38 $do_tri2obj =    1;
39 $do_strips =     1;
40 $do_fixobj =     1;
41
42
43 # set the FG_ROOT environment variable if it hasn't already been set.
44 if ( $ENV{FG_ROOT} eq "" ) {
45     # look for a file called fgtop as a place marker
46     if ( -e "fgtop" ) {
47         $ENV{FG_ROOT} = ".";
48     } elsif ( -e "../fgtop" ) {
49         $ENV{FG_ROOT} = "..";
50     }
51 }
52
53
54 if ( $#ARGV < 1 ) {
55     die "Usage: $0 <error^2> dem-file1 [ dem-file2 dem-file3 ... ]\n";
56 }
57
58 # Start with file.dem
59
60 $error = shift(@ARGV);
61 $error += 0.0;
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_demfit ) {
73         demfit() ;
74     } else {
75         $subdir = "../Scenery/w100n040/w093n045";
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 }
88
89
90 # exit normally
91 exit(0);
92
93
94 # return the file name root (ending at last ".")
95 sub file_root {
96     my($file) = @_;
97     my($pos);
98
99     $pos = rindex($file, ".");
100     return substr($file, 0, $pos);
101 }
102
103
104 # 1.  dem2node $FG_ROOT dem_file tolerance^2 (meters)
105
106 #     - dem2node .. dem_file 160000
107 #
108 #     splits dem file into 64 file.node's which contain the
109 #     irregularly fitted vertices
110
111 sub demfit {
112     if ( $dem_file =~ m/.gz$/ ) {
113         $command = "gzip -dc $dem_file | ./Dem2node/demfit $ENV{FG_ROOT} - $error";
114     } else {
115         $command = "./Dem2node/demfit $ENV{FG_ROOT} $dem_file $error";
116     }
117
118     print "Running '$command'\n";
119
120     open(OUT, "$command |");
121     while ( <OUT> ) {
122         print $_;
123         if ( m/^Dir = / ) {
124             $subdir = $_;
125             $subdir =~ s/^Dir = //;
126             chop($subdir);
127         }
128     }
129     close(OUT);
130
131
132
133 # 2.  triangle -q file (Takes file.node and produces file.1.node and
134 #                      file.1.ele)
135
136 print "Subdirectory for this dem file is $subdir\n";
137
138 sub triangle_1 {
139     @FILES = `ls $subdir`;
140     foreach $file ( @FILES ) {
141         print $file;
142         chop($file);
143         if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
144             $command = "./Triangle/triangle -q $subdir/$file";
145             print "Running '$command'\n";
146             open(OUT, "$command |");
147             while ( <OUT> ) {
148                 print $_;
149             }
150             close(OUT);
151
152             # remove input file.node
153             unlink("$subdir/$file");
154         }
155     }
156 }
157
158
159 # 3.  fixnode file.dem subdir
160 #
161 #     Take the original .dem file (for interpolating Z values) and the
162 #     subdirecotry containing all the file.1.node's and replace with
163 #     fixed file.1.node
164
165 sub fixnode {
166     if ( $dem_file =~ m/.gz$/ ) {
167         $command = "gzip -dc $dem_file | ./FixNode/fixnode - $subdir";
168     } else {
169         $command = "./FixNode/fixnode $dem_file $subdir";
170     }
171     print "Running '$command'\n";
172     open(OUT, "$command |");
173     while ( <OUT> ) {
174         print $_;
175     }
176     close(OUT);
177 }
178
179
180 # 4.1 splittris file (.1.node) (.1.ele)
181
182 #     Extract the corner, edge, and body vertices (in original
183 #     geodetic coordinates) and normals (in cartesian coordinates) and
184 #     save them in something very close to the .obj format as file.se,
185 #     file.sw, file.nw, file.ne, file.north, file.south, file.east,
186 #     file.west, and file.body.  This way we can reconstruct the
187 #     region using consistant edges and corners.  
188
189 #     Arbitration rules: If an opposite edge file already exists,
190 #     don't create our matching edge.  If a corner already exists,
191 #     don't create ours.  Basically, the early bird gets the worm and
192 #     gets to define the edge verticies and normals.  All the other
193 #     adjacent tiles must use these.
194
195 sub splittris {
196     @FILES = `ls $subdir`;
197     foreach $file ( @FILES ) {
198         chop($file);
199         if ( $file =~ m/\.1\.node$/ ) {
200             $file =~ s/\.node$//;  # strip off the ".node"
201         
202             $command = "./SplitTris/splittris $subdir/$file";
203             print "Running '$command'\n";
204             open(OUT, "$command |");
205             while ( <OUT> ) {
206                 print $_;
207             }
208             close(OUT);
209
210             unlink("$subdir/$file.node");
211             unlink("$subdir/$file.node.orig");
212             unlink("$subdir/$file.ele");
213         }
214     }
215 }
216
217
218 # 4.2 read in the split of version of the tiles, reconstruct the tile
219 #     using the proper shared corners and edges.  Save as a node file
220 #     so we can retriangulate.
221
222 sub assemtris {
223     @FILES = `ls $subdir`;
224     foreach $file ( @FILES ) {
225         chop($file);
226         if ( $file =~ m/\.1\.body$/ ) {
227             $file =~ s/\.body$//;  # strip off the ".body"
228         
229             $command = "./AssemTris/assemtris $subdir/$file";
230             print "Running '$command'\n";
231             open(OUT, "$command |");
232             while ( <OUT> ) {
233                 print $_;
234             }
235             close(OUT);
236         }
237         unlink("$subdir/$file.body");
238     }
239 }
240
241
242 # 4.3 Retriangulate reassembled files (without -q option) so no new
243 #     nodes are generated.
244
245 sub triangle_2 {
246     @FILES = `ls $subdir`;
247     foreach $file ( @FILES ) {
248         print $file;
249         chop($file);
250         if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
251             $command = "./Triangle/triangle $subdir/$file";
252             print "Running '$command'\n";
253             open(OUT, "$command |");
254             while ( <OUT> ) {
255                 print $_;
256             }
257             close(OUT);
258
259             # remove input file.node
260             unlink("$subdir/$file");
261         }
262     }
263 }
264
265
266 # 5.  tri2obj file (.1.node) (.1.ele)
267 #
268 #     Take the file.1.node and file.1.ele and produce file.1.obj
269 #
270 #     Extracts normals out of the shared edge/vertex files, and uses
271 #     the precalcuated normals for these nodes instead of calculating
272 #     new ones.  By sharing normals as well as vertices, not only are
273 #     the gaps between tiles eliminated, but the colors and lighting
274 #     transition smoothly across tile boundaries.
275
276 sub tri2obj {
277     @FILES = `ls $subdir`;
278     foreach $file ( @FILES ) {
279         chop($file);
280         if ( $file =~ m/\.1\.node$/ ) {
281             $file =~ s/\.node$//;  # strip off the ".node"
282             
283             $command = "./Tri2obj/tri2obj $subdir/$file";
284             print "Running '$command'\n";
285             open(OUT, "$command |");
286             while ( <OUT> ) {
287                 print $_;
288             }
289             close(OUT);
290             
291             unlink("$subdir/$file.node");
292             unlink("$subdir/$file.node.orig");
293             unlink("$subdir/$file.ele");
294         }
295     }
296 }
297
298
299 # 6.  strip file.1.obj
300
301 #     Strip the file.1.obj's.  Note, strips doesn't handle the minimal
302 #     case of striping a square correctly.
303 #
304 # 7.  cp bands.d file.2.obj
305 #
306 #     strips produces a file called "bands.d" ... copy this to file.2.obj
307
308 sub strips {
309     @FILES = `ls $subdir`;
310     foreach $file ( @FILES ) {
311         chop($file);
312         if ( $file =~ m/\.1\.obj$/ ) {
313             $command = "./Stripe_u/strips $subdir/$file";
314             print "Running '$command'\n";
315             open(OUT, "$command |");
316             while ( <OUT> ) {
317                 print $_;
318             }
319             close(OUT);
320             
321             # copy to destination file
322             $newfile = $file;
323             $newfile =~ s/\.1\.obj$//;
324             print "Copying to $subdir/$newfile.2.obj\n";
325             open(IN, "<bands.d");
326             open(OUT, ">$subdir/$newfile.2.obj");
327             while ( <IN> ) {
328                 print OUT $_;
329             }
330             close(IN);
331             close(OUT);
332             
333             unlink("$subdir/$file");
334         }
335     }
336 }
337
338
339 # 8.  fixobj file-new
340 #
341 #     Sort file.2.obj by strip winding
342
343 sub fixobj {
344     @FILES = `ls $subdir`;
345     foreach $file ( @FILES ) {
346         chop($file);
347         if ( $file =~ m/\.2\.obj$/ ) {
348             $newfile = $file;
349             $newfile =~ s/\.2\.obj$/.obj/;
350             
351             $command = "./FixObj/fixobj $subdir/$file $subdir/$newfile";
352             print "Running '$command'\n";
353             open(OUT, "$command |");
354             while ( <OUT> ) {
355                 print $_;
356             }
357             close(OUT);
358
359             unlink("$subdir/$file");
360         }
361     }
362 }
363
364
365 #---------------------------------------------------------------------------
366 # $Log$
367 # Revision 1.11  1998/03/03 03:36:57  curt
368 # Cumulative tweaks.
369 #
370 # Revision 1.10  1998/02/01 03:42:26  curt
371 # Modifications to handle compressed dem files.
372 #
373 # Revision 1.9  1998/01/27 18:36:54  curt
374 # Lots of updates to get back in sync with changes made over in .../Src/
375 #
376 # Revision 1.8  1998/01/21 17:59:05  curt
377 # Uncomment lines to remove several intermediate files.
378 #
379 # Revision 1.7  1998/01/19 19:51:06  curt
380 # A couple final pre-release tweaks.
381 #
382 # Revision 1.6  1998/01/15 21:33:33  curt
383 # Assembling triangles and building a new .node file with the proper shared
384 # vertices now works.  Now we just have to use the shared normals and we'll
385 # be all set.
386 #
387 # Revision 1.5  1998/01/15 02:50:08  curt
388 # Tweaked to add next stage.
389 #
390 # Revision 1.4  1998/01/14 15:55:34  curt
391 # Finished splittris, started assemtris.
392 #
393 # Revision 1.3  1998/01/14 02:15:52  curt
394 # Updated front end script to keep plugging away on tile fitting.
395 #
396 # Revision 1.2  1998/01/12 20:42:08  curt
397 # Working on fitting tiles together in a seamless manner.
398 #
399 # Revision 1.1  1998/01/09 23:06:46  curt
400 # Initial revision.
401 #