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
46 die "You must remember to set the FG_ROOT environment variable!\n";
51 die "Usage: $0 <error^2> dem-file1 [ dem-file2 dem-file3 ... ]\n";
56 $error = shift(@ARGV);
59 while ( $dem_file = shift(@ARGV) ) {
60 print "Source file = $dem_file Error tolerance = $error\n";
63 die "I doubt you'll be happy with an error tolerance as " .
71 $subdir = "../Scenery/w120n030/w111n033";
72 print "WARNING: Hardcoding subdir = $subdir\n";
75 triangle_1() if ( $do_triangle_1 );
76 fixnode() if ( $do_fixnode );
77 splittris() if ( $do_splittris );
78 assemtris() if ( $do_assemtris );
79 triangle_2() if ( $do_triangle_2);
80 tri2obj() if ( $do_tri2obj );
81 strips() if ( $do_strips );
82 fixobj() if ( $do_fixobj );
90 # fix command to work with windoze, replaces first "/" with "\\"
97 if ( $system =~ m/CYGWIN32/ ) {
105 # return the file name root (ending at last ".")
110 $pos = rindex($file, ".");
111 return substr($file, 0, $pos);
115 # 1. dem2node $FG_ROOT dem_file tolerance^2 (meters)
117 # - dem2node .. dem_file 160000
119 # splits dem file into 64 file.node's which contain the
120 # irregularly fitted vertices
123 $command = "Dem2node/dem2node $ENV{FG_ROOT} $dem_file $error";
124 $command = fix_command($command);
125 print "Running '$command'\n";
127 open(OUT, "$command |");
132 $subdir =~ s/^Dir = //;
140 # 2. triangle -q file (Takes file.node and produces file.1.node and
143 print "Subdirectory for this dem file is $subdir\n";
146 @FILES = `ls $subdir`;
147 foreach $file ( @FILES ) {
150 if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
151 $command = "Triangle/triangle -q $subdir/$file";
152 $command = fix_command($command);
153 print "Running '$command'\n";
154 open(OUT, "$command |");
160 # remove input file.node
161 unlink("$subdir/$file");
167 # 3. fixnode file.dem subdir
169 # Take the original .dem file (for interpolating Z values) and the
170 # subdirecotry containing all the file.1.node's and replace with
174 $command = "FixNode/fixnode $dem_file $subdir";
175 $command = fix_command($command);
176 print "Running '$command'\n";
177 open(OUT, "$command |");
185 # 4.1 splittris file (.1.node) (.1.ele)
187 # Extract the corner, edge, and body vertices (in original
188 # geodetic coordinates) and normals (in cartesian coordinates) and
189 # save them in something very close to the .obj format as file.se,
190 # file.sw, file.nw, file.ne, file.north, file.south, file.east,
191 # file.west, and file.body. This way we can reconstruct the
192 # region using consistant edges and corners.
194 # Arbitration rules: If an opposite edge file already exists,
195 # don't create our matching edge. If a corner already exists,
196 # don't create ours. Basically, the early bird gets the worm and
197 # gets to define the edge verticies and normals. All the other
198 # adjacent tiles must use these.
201 @FILES = `ls $subdir`;
202 foreach $file ( @FILES ) {
204 if ( $file =~ m/\.1\.node$/ ) {
205 $file =~ s/\.node$//; # strip off the ".node"
207 $command = "SplitTris/splittris $subdir/$file";
208 $command = fix_command($command);
209 print "Running '$command'\n";
210 open(OUT, "$command |");
216 unlink("$subdir/$file.node");
217 unlink("$subdir/$file.node.orig");
218 unlink("$subdir/$file.ele");
224 # 4.2 read in the split of version of the tiles, reconstruct the tile
225 # using the proper shared corners and edges. Save as a node file
226 # so we can retriangulate.
229 @FILES = `ls $subdir`;
230 foreach $file ( @FILES ) {
232 if ( $file =~ m/\.1\.body$/ ) {
233 $file =~ s/\.body$//; # strip off the ".body"
235 $command = "AssemTris/assemtris $subdir/$file";
236 $command = fix_command($command);
237 print "Running '$command'\n";
238 open(OUT, "$command |");
244 unlink("$subdir/$file.body");
249 # 4.3 Retriangulate reassembled files (without -q option) so no new
250 # nodes are generated.
253 @FILES = `ls $subdir`;
254 foreach $file ( @FILES ) {
257 if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
259 $base =~ s/\.node$//;
260 print("Test for $subdir/$base.q\n");
261 if ( -r "$subdir/$base.q" ) {
263 # if triangle hangs, we can create a filebase.q for
264 # the file it hung on. Then, we test for that file
265 # here which causes the incremental algorithm to run
266 # (which shouldn't ever hang.)
268 $command = "Triangle/triangle -i $subdir/$file";
270 $command = "Triangle/triangle $subdir/$file";
272 $command = fix_command($command);
273 print "Running '$command'\n";
274 open(OUT, "$command |");
280 # remove input file.node
281 unlink("$subdir/$file");
287 # 5. tri2obj file (.1.node) (.1.ele)
289 # Take the file.1.node and file.1.ele and produce file.1.obj
291 # Extracts normals out of the shared edge/vertex files, and uses
292 # the precalcuated normals for these nodes instead of calculating
293 # new ones. By sharing normals as well as vertices, not only are
294 # the gaps between tiles eliminated, but the colors and lighting
295 # transition smoothly across tile boundaries.
298 @FILES = `ls $subdir`;
299 foreach $file ( @FILES ) {
301 if ( $file =~ m/\.1\.node$/ ) {
302 $file =~ s/\.node$//; # strip off the ".node"
304 $command = "Tri2obj/tri2obj $subdir/$file";
305 $command = fix_command($command);
306 print "Running '$command'\n";
307 open(OUT, "$command |");
313 unlink("$subdir/$file.node");
314 unlink("$subdir/$file.node.orig");
315 unlink("$subdir/$file.ele");
321 # 6. strip file.1.obj
323 # Strip the file.1.obj's. Note, strips doesn't handle the minimal
324 # case of striping a square correctly.
326 # 7. cp bands.d file.2.obj
328 # strips produces a file called "bands.d" ... copy this to file.2.obj
331 @FILES = `ls $subdir`;
332 foreach $file ( @FILES ) {
334 if ( $file =~ m/\.1\.obj$/ ) {
335 $command = "Stripe_u/strips $subdir/$file";
336 $command = fix_command($command);
337 print "Running '$command'\n";
338 open(OUT, "$command |");
344 # copy to destination file
346 $newfile =~ s/\.1\.obj$//;
347 print "Copying to $subdir/$newfile.2.obj\n";
348 open(IN, "<bands.d");
349 open(OUT, ">$subdir/$newfile.2.obj");
356 unlink("$subdir/$file");
364 # Sort file.2.obj by strip winding
367 @FILES = `ls $subdir`;
368 foreach $file ( @FILES ) {
370 if ( $file =~ m/\.2\.obj$/ ) {
372 $newfile =~ s/\.2\.obj$/.obj/;
374 $command = "FixObj/fixobj $subdir/$file $subdir/$newfile";
375 $command = fix_command($command);
376 print "Running '$command'\n";
377 open(OUT, "$command |");
383 # unlink("$subdir/$file");
389 #---------------------------------------------------------------------------
391 # Revision 1.18 1998/05/20 20:55:40 curt
394 # Revision 1.17 1998/04/28 01:23:25 curt
395 # Added a work around so that we can get past the "triangle" program
396 # hanging, by aborting and rerunning with that tile marked to use the "-i"
399 # Revision 1.16 1998/04/18 03:57:53 curt
400 # Added zlib library support.
402 # Revision 1.15 1998/04/08 23:24:07 curt
403 # Adopted Gnu automake/autoconf system.
405 # Revision 1.14 1998/04/06 21:09:38 curt
406 # Additional win32 support.
407 # Fixed a bad bug in dem file parsing that was causing the output to be
408 # flipped about x = y.
410 # Revision 1.13 1998/03/19 02:52:52 curt
411 # Updated to reflect some minor tool reorganization and the creation of class
412 # to handle DEM processing needs.
414 # Revision 1.12 1998/03/19 01:48:35 curt
415 # Added gpc-2.01 (generic polygon clipping library)
417 # Revision 1.11 1998/03/03 03:36:57 curt
420 # Revision 1.10 1998/02/01 03:42:26 curt
421 # Modifications to handle compressed dem files.
423 # Revision 1.9 1998/01/27 18:36:54 curt
424 # Lots of updates to get back in sync with changes made over in .../Src/
426 # Revision 1.8 1998/01/21 17:59:05 curt
427 # Uncomment lines to remove several intermediate files.
429 # Revision 1.7 1998/01/19 19:51:06 curt
430 # A couple final pre-release tweaks.
432 # Revision 1.6 1998/01/15 21:33:33 curt
433 # Assembling triangles and building a new .node file with the proper shared
434 # vertices now works. Now we just have to use the shared normals and we'll
437 # Revision 1.5 1998/01/15 02:50:08 curt
438 # Tweaked to add next stage.
440 # Revision 1.4 1998/01/14 15:55:34 curt
441 # Finished splittris, started assemtris.
443 # Revision 1.3 1998/01/14 02:15:52 curt
444 # Updated front end script to keep plugging away on tile fitting.
446 # Revision 1.2 1998/01/12 20:42:08 curt
447 # Working on fitting tiles together in a seamless manner.
449 # Revision 1.1 1998/01/09 23:06:46 curt