3 #---------------------------------------------------------------------------
4 # Toplevel script to automate DEM file processing and conversion
6 # Written by Curtis Olson, started January 1998.
8 # Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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.
25 # (Log is kept at end of this file)
26 #---------------------------------------------------------------------------
29 $| = 1; # flush buffers after every write
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
48 } elsif ( -e "../fgtop" ) {
55 die "Usage: $0 <error^2> dem-file1 [ dem-file2 dem-file3 ... ]\n";
60 $error = shift(@ARGV);
63 while ( $dem_file = shift(@ARGV) ) {
64 print "Source file = $dem_file Error tolerance = $error\n";
67 die "I doubt you'll be happy with an error tolerance as " .
75 $subdir = "../Scenery/w100n040/w093n045";
76 print "WARNING: Hardcoding subdir = $subdir\n";
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 );
94 # fix command to work with windoze, replaces first "/" with "\\"
101 if ( $system =~ m/CYGWIN32/ ) {
109 # return the file name root (ending at last ".")
114 $pos = rindex($file, ".");
115 return substr($file, 0, $pos);
119 # 1. dem2node $FG_ROOT dem_file tolerance^2 (meters)
121 # - dem2node .. dem_file 160000
123 # splits dem file into 64 file.node's which contain the
124 # irregularly fitted vertices
127 if ( $dem_file =~ m/.gz$/ ) {
128 $command = "gzip -dc $dem_file | Dem2node/dem2node $ENV{FG_ROOT} - $error";
130 $command = "Dem2node/dem2node $ENV{FG_ROOT} $dem_file $error";
132 $command = fix_command($command);
133 print "Running '$command'\n";
135 open(OUT, "$command |");
140 $subdir =~ s/^Dir = //;
148 # 2. triangle -q file (Takes file.node and produces file.1.node and
151 print "Subdirectory for this dem file is $subdir\n";
154 @FILES = `ls $subdir`;
155 foreach $file ( @FILES ) {
158 if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
159 $command = "Triangle/triangle -q $subdir/$file";
160 $command = fix_command($command);
161 print "Running '$command'\n";
162 open(OUT, "$command |");
168 # remove input file.node
169 unlink("$subdir/$file");
175 # 3. fixnode file.dem subdir
177 # Take the original .dem file (for interpolating Z values) and the
178 # subdirecotry containing all the file.1.node's and replace with
182 if ( $dem_file =~ m/.gz$/ ) {
183 $command = "gzip -dc $dem_file | FixNode/fixnode - $subdir";
185 $command = "FixNode/fixnode $dem_file $subdir";
187 $command = fix_command($command);
188 print "Running '$command'\n";
189 open(OUT, "$command |");
197 # 4.1 splittris file (.1.node) (.1.ele)
199 # Extract the corner, edge, and body vertices (in original
200 # geodetic coordinates) and normals (in cartesian coordinates) and
201 # save them in something very close to the .obj format as file.se,
202 # file.sw, file.nw, file.ne, file.north, file.south, file.east,
203 # file.west, and file.body. This way we can reconstruct the
204 # region using consistant edges and corners.
206 # Arbitration rules: If an opposite edge file already exists,
207 # don't create our matching edge. If a corner already exists,
208 # don't create ours. Basically, the early bird gets the worm and
209 # gets to define the edge verticies and normals. All the other
210 # adjacent tiles must use these.
213 @FILES = `ls $subdir`;
214 foreach $file ( @FILES ) {
216 if ( $file =~ m/\.1\.node$/ ) {
217 $file =~ s/\.node$//; # strip off the ".node"
219 $command = "SplitTris/splittris $subdir/$file";
220 $command = fix_command($command);
221 print "Running '$command'\n";
222 open(OUT, "$command |");
228 unlink("$subdir/$file.node");
229 unlink("$subdir/$file.node.orig");
230 unlink("$subdir/$file.ele");
236 # 4.2 read in the split of version of the tiles, reconstruct the tile
237 # using the proper shared corners and edges. Save as a node file
238 # so we can retriangulate.
241 @FILES = `ls $subdir`;
242 foreach $file ( @FILES ) {
244 if ( $file =~ m/\.1\.body$/ ) {
245 $file =~ s/\.body$//; # strip off the ".body"
247 $command = "AssemTris/assemtris $subdir/$file";
248 $command = fix_command($command);
249 print "Running '$command'\n";
250 open(OUT, "$command |");
256 unlink("$subdir/$file.body");
261 # 4.3 Retriangulate reassembled files (without -q option) so no new
262 # nodes are generated.
265 @FILES = `ls $subdir`;
266 foreach $file ( @FILES ) {
269 if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
270 $command = "Triangle/triangle $subdir/$file";
271 $command = fix_command($command);
272 print "Running '$command'\n";
273 open(OUT, "$command |");
279 # remove input file.node
280 unlink("$subdir/$file");
286 # 5. tri2obj file (.1.node) (.1.ele)
288 # Take the file.1.node and file.1.ele and produce file.1.obj
290 # Extracts normals out of the shared edge/vertex files, and uses
291 # the precalcuated normals for these nodes instead of calculating
292 # new ones. By sharing normals as well as vertices, not only are
293 # the gaps between tiles eliminated, but the colors and lighting
294 # transition smoothly across tile boundaries.
297 @FILES = `ls $subdir`;
298 foreach $file ( @FILES ) {
300 if ( $file =~ m/\.1\.node$/ ) {
301 $file =~ s/\.node$//; # strip off the ".node"
303 $command = "Tri2obj/tri2obj $subdir/$file";
304 $command = fix_command($command);
305 print "Running '$command'\n";
306 open(OUT, "$command |");
312 unlink("$subdir/$file.node");
313 unlink("$subdir/$file.node.orig");
314 unlink("$subdir/$file.ele");
320 # 6. strip file.1.obj
322 # Strip the file.1.obj's. Note, strips doesn't handle the minimal
323 # case of striping a square correctly.
325 # 7. cp bands.d file.2.obj
327 # strips produces a file called "bands.d" ... copy this to file.2.obj
330 @FILES = `ls $subdir`;
331 foreach $file ( @FILES ) {
333 if ( $file =~ m/\.1\.obj$/ ) {
334 $command = "Stripe_u/strips $subdir/$file";
335 $command = fix_command($command);
336 print "Running '$command'\n";
337 open(OUT, "$command |");
343 # copy to destination file
345 $newfile =~ s/\.1\.obj$//;
346 print "Copying to $subdir/$newfile.2.obj\n";
347 open(IN, "<bands.d");
348 open(OUT, ">$subdir/$newfile.2.obj");
355 unlink("$subdir/$file");
363 # Sort file.2.obj by strip winding
366 @FILES = `ls $subdir`;
367 foreach $file ( @FILES ) {
369 if ( $file =~ m/\.2\.obj$/ ) {
371 $newfile =~ s/\.2\.obj$/.obj/;
373 $command = "FixObj/fixobj $subdir/$file $subdir/$newfile";
374 $command = fix_command($command);
375 print "Running '$command'\n";
376 open(OUT, "$command |");
382 unlink("$subdir/$file");
388 #---------------------------------------------------------------------------
390 # Revision 1.14 1998/04/06 21:09:38 curt
391 # Additional win32 support.
392 # Fixed a bad bug in dem file parsing that was causing the output to be
393 # flipped about x = y.
395 # Revision 1.13 1998/03/19 02:52:52 curt
396 # Updated to reflect some minor tool reorganization and the creation of class
397 # to handle DEM processing needs.
399 # Revision 1.12 1998/03/19 01:48:35 curt
400 # Added gpc-2.01 (generic polygon clipping library)
402 # Revision 1.11 1998/03/03 03:36:57 curt
405 # Revision 1.10 1998/02/01 03:42:26 curt
406 # Modifications to handle compressed dem files.
408 # Revision 1.9 1998/01/27 18:36:54 curt
409 # Lots of updates to get back in sync with changes made over in .../Src/
411 # Revision 1.8 1998/01/21 17:59:05 curt
412 # Uncomment lines to remove several intermediate files.
414 # Revision 1.7 1998/01/19 19:51:06 curt
415 # A couple final pre-release tweaks.
417 # Revision 1.6 1998/01/15 21:33:33 curt
418 # Assembling triangles and building a new .node file with the proper shared
419 # vertices now works. Now we just have to use the shared normals and we'll
422 # Revision 1.5 1998/01/15 02:50:08 curt
423 # Tweaked to add next stage.
425 # Revision 1.4 1998/01/14 15:55:34 curt
426 # Finished splittris, started assemtris.
428 # Revision 1.3 1998/01/14 02:15:52 curt
429 # Updated front end script to keep plugging away on tile fitting.
431 # Revision 1.2 1998/01/12 20:42:08 curt
432 # Working on fitting tiles together in a seamless manner.
434 # Revision 1.1 1998/01/09 23:06:46 curt