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