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