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 $max_area = 10000; # maximum triangle area
32 $| = 1; # flush buffers after every write
49 die "Usage: $0 <fg-root-dir> <work-dir> <error^2> dem-file(s)\n";
54 $fg_root = shift(@ARGV);
55 $work_dir = shift(@ARGV);
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 = "./work/Scenery/w100n040/w094n045";
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 );
80 triangle_2() if ( $do_triangle_2);
81 tri2obj() if ( $do_tri2obj );
82 strips() if ( $do_strips );
83 fixobj() if ( $do_fixobj );
84 install() if ( $do_install );
92 # fix command to work with windoze, replaces first "/" with "\\"
99 if ( $system =~ m/CYGWIN32/ ) {
107 # return the file name root (ending at last ".")
112 $pos = rindex($file, ".");
113 return substr($file, 0, $pos);
117 # 1. dem2node work_dir dem_file tolerance^2 (meters)
119 # - dem2node .. dem_file 160000
121 # splits dem file into 64 file.node's which contain the
122 # irregularly fitted vertices
125 $command = "Dem2node/dem2node $work_dir $dem_file $error";
126 $command = fix_command($command);
127 print "Running '$command'\n";
129 open(OUT, "$command |");
134 $subdir =~ s/^Dir = //;
142 # 2. triangle -q file (Takes file.node and produces file.1.node and
145 print "Subdirectory for this dem file is $subdir\n";
148 @FILES = `ls $subdir`;
149 foreach $file ( @FILES ) {
152 if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
153 # special handling is needed if .poly file exists
155 $fileroot =~ s/\.node$//;
156 print "$subdir/$fileroot\n";
157 if ( -r "$subdir/$fileroot.poly" ) {
158 $command = "Triangle/triangle -pc -a$max_area -q5 $subdir/$fileroot";
160 $command = "Triangle/triangle -c -a$max_area -q5 $subdir/$file";
162 $command = fix_command($command);
163 print "Running '$command'\n";
164 open(OUT, "$command |");
170 # remove input file.node
171 if ( $remove_tmps ) {
172 unlink("$subdir/$file");
179 # 3. fixnode file.dem subdir
181 # Take the original .dem file (for interpolating Z values) and the
182 # subdirecotry containing all the file.1.node's and replace with
186 $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 if ( $remove_tmps ) {
229 unlink("$subdir/$file.node");
230 unlink("$subdir/$file.node.orig");
231 unlink("$subdir/$file.ele");
238 # 4.2 read in the split of version of the tiles, reconstruct the tile
239 # using the proper shared corners and edges. Save as a node file
240 # so we can retriangulate.
243 @FILES = `ls $subdir`;
244 foreach $file ( @FILES ) {
246 if ( $file =~ m/\.1\.body$/ ) {
247 $file =~ s/\.body$//; # strip off the ".body"
249 $command = "AssemTris/assemtris $subdir/$file";
250 $command = fix_command($command);
251 print "Running '$command'\n";
252 open(OUT, "$command |");
258 if ( $remove_tmps ) {
259 unlink("$subdir/$file.body");
265 # 4.3 Retriangulate reassembled files (without -q option) so no new
266 # nodes are generated.
269 @FILES = `ls $subdir`;
270 foreach $file ( @FILES ) {
273 if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
275 $base =~ s/\.node$//;
276 print("Test for $subdir/$base.q\n");
277 if ( -r "$subdir/$base.q" ) {
279 # if triangle hangs, we can create a filebase.q for
280 # the file it hung on. Then, we test for that file
281 # here which causes the incremental algorithm to run
282 # (which shouldn't ever hang.)
284 $command = "Triangle/triangle -i $subdir/$file";
286 $command = "Triangle/triangle $subdir/$file";
288 $command = fix_command($command);
289 print "Running '$command'\n";
290 open(OUT, "$command |");
296 # remove input file.node
297 if ( $remove_tmps ) {
298 unlink("$subdir/$file");
305 # 5. tri2obj file (.1.node) (.1.ele)
307 # Take the file.1.node and file.1.ele and produce file.1.obj
309 # Extracts normals out of the shared edge/vertex files, and uses
310 # the precalcuated normals for these nodes instead of calculating
311 # new ones. By sharing normals as well as vertices, not only are
312 # the gaps between tiles eliminated, but the colors and lighting
313 # transition smoothly across tile boundaries.
316 @FILES = `ls $subdir`;
317 foreach $file ( @FILES ) {
319 if ( $file =~ m/\.1\.node$/ ) {
320 $file =~ s/\.node$//; # strip off the ".node"
322 $command = "Tri2obj/tri2obj $subdir/$file";
323 $command = fix_command($command);
324 print "Running '$command'\n";
325 open(OUT, "$command |");
331 if ( $remove_tmps ) {
332 unlink("$subdir/$file.node");
333 unlink("$subdir/$file.node.orig");
334 unlink("$subdir/$file.ele");
341 # 6. strip file.1.obj
343 # Strip the file.1.obj's. Note, strips doesn't handle the minimal
344 # case of striping a square correctly.
346 # 7. cp bands.d file.2.obj
348 # strips produces a file called "bands.d" ... copy this to file.2.obj
351 @FILES = `ls $subdir`;
352 foreach $file ( @FILES ) {
354 if ( $file =~ m/\.1\.obj$/ ) {
355 $command = "Stripe_w/strips $subdir/$file";
356 $command = fix_command($command);
357 print "Running '$command'\n";
359 open(OUT, "$command |");
365 # copy to destination file
367 $newfile =~ s/\.1\.obj$//;
368 print "Copying to $subdir/$newfile.2.obj\n";
369 # open(IN, "<bands.d");
370 open(IN, "<stripe.objf");
371 open(OUT, ">$subdir/$newfile.2.obj");
378 if ( $remove_tmps ) {
379 unlink("$subdir/$file");
388 # Sort file.2.obj by strip winding
391 @FILES = `ls $subdir`;
392 foreach $file ( @FILES ) {
394 if ( $file =~ m/\.2\.obj$/ ) {
396 $newfile =~ s/\.2\.obj$/.obj/;
398 $command = "FixObj/fixobj $subdir/$file $subdir/$newfile";
399 $command = fix_command($command);
400 print "Running '$command'\n";
401 open(OUT, "$command |");
407 if ( $remove_tmps ) {
408 unlink("$subdir/$file");
417 # rename, compress, and install scenery files
421 $tmp =~ s/$work_dir//;
422 # print "Temp dir = $tmp\n";
423 $install_dir = "$fg_root/$tmp";
424 print "Install dir = $install_dir\n";
425 system("mkdir -p $install_dir");
427 @FILES = `ls $subdir`;
428 foreach $file ( @FILES ) {
430 if ( $file =~ m/\d\d.obj$/ ) {
431 $new_file = file_root($file);
433 $command = "gzip -v --best < $subdir/$file > $install_dir/$new_file.gz";
434 # $command = fix_command($command);
435 print "Running '$command'\n";
436 open(OUT, "$command |");
442 if ( $remove_tmps ) {
443 unlink("$subdir/$file");
450 #---------------------------------------------------------------------------
452 # Revision 1.23 1998/07/20 12:55:35 curt
453 # Several tweaks to start incorporating area cutouts into the pipeline.
455 # Revision 1.22 1998/07/08 14:49:13 curt
458 # Revision 1.21 1998/06/08 17:18:37 curt
459 # Mods to test new Stripe fixes from Wilbur Streett.
461 # Revision 1.20 1998/06/05 18:20:24 curt
462 # Added DemInfo to dump out "A" record DEM info.
463 # Modified process-dem.pl to work in a temp directory and compress/copy the
464 # result to the final destination.
466 # Revision 1.19 1998/05/27 02:25:26 curt
467 # Added a flag to the first run of "triangle" to impose a maximum triangle
468 # size. This forces really flat areas to be subdivided a certain amount
469 # anyways. This makes for slightly more interesting scenery.
471 # Revision 1.18 1998/05/20 20:55:40 curt
474 # Revision 1.17 1998/04/28 01:23:25 curt
475 # Added a work around so that we can get past the "triangle" program
476 # hanging, by aborting and rerunning with that tile marked to use the "-i"
479 # Revision 1.16 1998/04/18 03:57:53 curt
480 # Added zlib library support.
482 # Revision 1.15 1998/04/08 23:24:07 curt
483 # Adopted Gnu automake/autoconf system.
485 # Revision 1.14 1998/04/06 21:09:38 curt
486 # Additional win32 support.
487 # Fixed a bad bug in dem file parsing that was causing the output to be
488 # flipped about x = y.
490 # Revision 1.13 1998/03/19 02:52:52 curt
491 # Updated to reflect some minor tool reorganization and the creation of class
492 # to handle DEM processing needs.
494 # Revision 1.12 1998/03/19 01:48:35 curt
495 # Added gpc-2.01 (generic polygon clipping library)
497 # Revision 1.11 1998/03/03 03:36:57 curt
500 # Revision 1.10 1998/02/01 03:42:26 curt
501 # Modifications to handle compressed dem files.
503 # Revision 1.9 1998/01/27 18:36:54 curt
504 # Lots of updates to get back in sync with changes made over in .../Src/
506 # Revision 1.8 1998/01/21 17:59:05 curt
507 # Uncomment lines to remove several intermediate files.
509 # Revision 1.7 1998/01/19 19:51:06 curt
510 # A couple final pre-release tweaks.
512 # Revision 1.6 1998/01/15 21:33:33 curt
513 # Assembling triangles and building a new .node file with the proper shared
514 # vertices now works. Now we just have to use the shared normals and we'll
517 # Revision 1.5 1998/01/15 02:50:08 curt
518 # Tweaked to add next stage.
520 # Revision 1.4 1998/01/14 15:55:34 curt
521 # Finished splittris, started assemtris.
523 # Revision 1.3 1998/01/14 02:15:52 curt
524 # Updated front end script to keep plugging away on tile fitting.
526 # Revision 1.2 1998/01/12 20:42:08 curt
527 # Working on fitting tiles together in a seamless manner.
529 # Revision 1.1 1998/01/09 23:06:46 curt