]> git.mxchange.org Git - flightgear.git/blob - Tools/process-dem.pl
3d5f000f030f95a879221f2b8d6f516bfe9cd0c1
[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 ".node"
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     }
203 }
204
205
206 # 4.3 Retriangulate reassembled files (without -q option) so no new
207 #     nodes are generated.
208
209 if ( $do_triangle_2 ) {
210     @FILES = `ls $subdir`;
211     foreach $file ( @FILES ) {
212         print $file;
213         chop($file);
214         if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
215             $command = "./Triangle/triangle $subdir/$file";
216             print "Running '$command'\n";
217             open(OUT, "$command |");
218             while ( <OUT> ) {
219                 print $_;
220             }
221             close(OUT);
222
223             # remove input file.node
224             unlink("$subdir/$file");
225         }
226     }
227 }
228
229
230 # 5.  tri2obj file (.1.node) (.1.ele)
231 #
232 #     Take the file.1.node and file.1.ele and produce file.1.obj
233 #
234 #     Extracts normals out of the shared edge/vertex files, and uses
235 #     the precalcuated normals for these nodes instead of calculating
236 #     new ones.  By sharing normals as well as vertices, not only are
237 #     the gaps between tiles eliminated, but the colors and lighting
238 #     transition smoothly across tile boundaries.
239
240 if ( $do_tri2obj ) {
241     @FILES = `ls $subdir`;
242     foreach $file ( @FILES ) {
243         chop($file);
244         if ( $file =~ m/\.1\.node$/ ) {
245             $file =~ s/\.node$//;  # strip off the ".node"
246             
247             $command = "./Tri2obj/tri2obj $subdir/$file";
248             print "Running '$command'\n";
249             open(OUT, "$command |");
250             while ( <OUT> ) {
251                 print $_;
252             }
253             close(OUT);
254             
255             unlink("$subdir/$file.node");
256             unlink("$subdir/$file.node.orig");
257             unlink("$subdir/$file.ele");
258         }
259     }
260 }
261
262
263 # 6.  strip file.1.obj
264
265 #     Strip the file.1.obj's.  Note, strips doesn't handle the minimal
266 #     case of striping a square correctly.
267 #
268 # 7.  cp bands.d file.2.obj
269 #
270 #     strips produces a file called "bands.d" ... copy this to file.2.obj
271
272 if ( $do_strips ) {
273     @FILES = `ls $subdir`;
274     foreach $file ( @FILES ) {
275         chop($file);
276         if ( $file =~ m/\.1\.obj$/ ) {
277             $command = "./Stripe_u/strips $subdir/$file";
278             print "Running '$command'\n";
279             open(OUT, "$command |");
280             while ( <OUT> ) {
281                 print $_;
282             }
283             close(OUT);
284             
285             # copy to destination file
286             $newfile = $file;
287             $newfile =~ s/\.1\.obj$//;
288             print "Copying to $subdir/$newfile.2.obj\n";
289             open(IN, "<bands.d");
290             open(OUT, ">$subdir/$newfile.2.obj");
291             while ( <IN> ) {
292                 print OUT $_;
293             }
294             close(IN);
295             close(OUT);
296             
297             unlink("$subdir/$file");
298         }
299     }
300 }
301
302
303 # 8.  fixobj file-new
304 #
305 #     Sort file.2.obj by strip winding
306
307 if ( $do_fixobj ) {
308     @FILES = `ls $subdir`;
309     foreach $file ( @FILES ) {
310         chop($file);
311         if ( $file =~ m/\.2\.obj$/ ) {
312             $newfile = $file;
313             $newfile =~ s/\.2\.obj$/.obj/;
314             
315             $command = "./FixObj/fixobj $subdir/$file $subdir/$newfile";
316             print "Running '$command'\n";
317             open(OUT, "$command |");
318             while ( <OUT> ) {
319                 print $_;
320             }
321             close(OUT);
322
323             unlink("$subdir/$file");
324         }
325     }
326 }
327
328
329 #---------------------------------------------------------------------------
330 # $Log$
331 # Revision 1.8  1998/01/21 17:59:05  curt
332 # Uncomment lines to remove several intermediate files.
333 #
334 # Revision 1.7  1998/01/19 19:51:06  curt
335 # A couple final pre-release tweaks.
336 #
337 # Revision 1.6  1998/01/15 21:33:33  curt
338 # Assembling triangles and building a new .node file with the proper shared
339 # vertices now works.  Now we just have to use the shared normals and we'll
340 # be all set.
341 #
342 # Revision 1.5  1998/01/15 02:50:08  curt
343 # Tweaked to add next stage.
344 #
345 # Revision 1.4  1998/01/14 15:55:34  curt
346 # Finished splittris, started assemtris.
347 #
348 # Revision 1.3  1998/01/14 02:15:52  curt
349 # Updated front end script to keep plugging away on tile fitting.
350 #
351 # Revision 1.2  1998/01/12 20:42:08  curt
352 # Working on fitting tiles together in a seamless manner.
353 #
354 # Revision 1.1  1998/01/09 23:06:46  curt
355 # Initial revision.
356 #