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
31 $| = 1; # flush buffers after every write
45 # set the FG_ROOT environment variable if it hasn't already been set.
46 if ( $ENV{FG_ROOT} eq "" ) {
47 # look for a file called fgtop as a place marker
48 die "You must remember to set the FG_ROOT environment variable!\n";
53 die "Usage: $0 <error^2> dem-file1 [ dem-file2 dem-file3 ... ]\n";
58 $error = shift(@ARGV);
61 while ( $dem_file = shift(@ARGV) ) {
62 print "Source file = $dem_file Error tolerance = $error\n";
65 die "I doubt you'll be happy with an error tolerance as " .
73 $subdir = "../Scenery/w120n030/w111n033";
74 print "WARNING: Hardcoding subdir = $subdir\n";
77 triangle_1() if ( $do_triangle_1 );
78 fixnode() if ( $do_fixnode );
79 splittris() if ( $do_splittris );
80 assemtris() if ( $do_assemtris );
81 triangle_2() if ( $do_triangle_2);
82 tri2obj() if ( $do_tri2obj );
83 strips() if ( $do_strips );
84 fixobj() if ( $do_fixobj );
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 $FG_ROOT 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 $ENV{FG_ROOT} $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 $command = "Triangle/triangle -a$max_area -q $subdir/$file";
154 $command = fix_command($command);
155 print "Running '$command'\n";
156 open(OUT, "$command |");
162 # remove input file.node
163 unlink("$subdir/$file");
169 # 3. fixnode file.dem subdir
171 # Take the original .dem file (for interpolating Z values) and the
172 # subdirecotry containing all the file.1.node's and replace with
176 $command = "FixNode/fixnode $dem_file $subdir";
177 $command = fix_command($command);
178 print "Running '$command'\n";
179 open(OUT, "$command |");
187 # 4.1 splittris file (.1.node) (.1.ele)
189 # Extract the corner, edge, and body vertices (in original
190 # geodetic coordinates) and normals (in cartesian coordinates) and
191 # save them in something very close to the .obj format as file.se,
192 # file.sw, file.nw, file.ne, file.north, file.south, file.east,
193 # file.west, and file.body. This way we can reconstruct the
194 # region using consistant edges and corners.
196 # Arbitration rules: If an opposite edge file already exists,
197 # don't create our matching edge. If a corner already exists,
198 # don't create ours. Basically, the early bird gets the worm and
199 # gets to define the edge verticies and normals. All the other
200 # adjacent tiles must use these.
203 @FILES = `ls $subdir`;
204 foreach $file ( @FILES ) {
206 if ( $file =~ m/\.1\.node$/ ) {
207 $file =~ s/\.node$//; # strip off the ".node"
209 $command = "SplitTris/splittris $subdir/$file";
210 $command = fix_command($command);
211 print "Running '$command'\n";
212 open(OUT, "$command |");
218 unlink("$subdir/$file.node");
219 unlink("$subdir/$file.node.orig");
220 unlink("$subdir/$file.ele");
226 # 4.2 read in the split of version of the tiles, reconstruct the tile
227 # using the proper shared corners and edges. Save as a node file
228 # so we can retriangulate.
231 @FILES = `ls $subdir`;
232 foreach $file ( @FILES ) {
234 if ( $file =~ m/\.1\.body$/ ) {
235 $file =~ s/\.body$//; # strip off the ".body"
237 $command = "AssemTris/assemtris $subdir/$file";
238 $command = fix_command($command);
239 print "Running '$command'\n";
240 open(OUT, "$command |");
246 unlink("$subdir/$file.body");
251 # 4.3 Retriangulate reassembled files (without -q option) so no new
252 # nodes are generated.
255 @FILES = `ls $subdir`;
256 foreach $file ( @FILES ) {
259 if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
261 $base =~ s/\.node$//;
262 print("Test for $subdir/$base.q\n");
263 if ( -r "$subdir/$base.q" ) {
265 # if triangle hangs, we can create a filebase.q for
266 # the file it hung on. Then, we test for that file
267 # here which causes the incremental algorithm to run
268 # (which shouldn't ever hang.)
270 $command = "Triangle/triangle -i $subdir/$file";
272 $command = "Triangle/triangle $subdir/$file";
274 $command = fix_command($command);
275 print "Running '$command'\n";
276 open(OUT, "$command |");
282 # remove input file.node
283 unlink("$subdir/$file");
289 # 5. tri2obj file (.1.node) (.1.ele)
291 # Take the file.1.node and file.1.ele and produce file.1.obj
293 # Extracts normals out of the shared edge/vertex files, and uses
294 # the precalcuated normals for these nodes instead of calculating
295 # new ones. By sharing normals as well as vertices, not only are
296 # the gaps between tiles eliminated, but the colors and lighting
297 # transition smoothly across tile boundaries.
300 @FILES = `ls $subdir`;
301 foreach $file ( @FILES ) {
303 if ( $file =~ m/\.1\.node$/ ) {
304 $file =~ s/\.node$//; # strip off the ".node"
306 $command = "Tri2obj/tri2obj $subdir/$file";
307 $command = fix_command($command);
308 print "Running '$command'\n";
309 open(OUT, "$command |");
315 unlink("$subdir/$file.node");
316 unlink("$subdir/$file.node.orig");
317 unlink("$subdir/$file.ele");
323 # 6. strip file.1.obj
325 # Strip the file.1.obj's. Note, strips doesn't handle the minimal
326 # case of striping a square correctly.
328 # 7. cp bands.d file.2.obj
330 # strips produces a file called "bands.d" ... copy this to file.2.obj
333 @FILES = `ls $subdir`;
334 foreach $file ( @FILES ) {
336 if ( $file =~ m/\.1\.obj$/ ) {
337 $command = "Stripe_u/strips $subdir/$file";
338 $command = fix_command($command);
339 print "Running '$command'\n";
340 open(OUT, "$command |");
346 # copy to destination file
348 $newfile =~ s/\.1\.obj$//;
349 print "Copying to $subdir/$newfile.2.obj\n";
350 open(IN, "<bands.d");
351 open(OUT, ">$subdir/$newfile.2.obj");
358 unlink("$subdir/$file");
366 # Sort file.2.obj by strip winding
369 @FILES = `ls $subdir`;
370 foreach $file ( @FILES ) {
372 if ( $file =~ m/\.2\.obj$/ ) {
374 $newfile =~ s/\.2\.obj$/.obj/;
376 $command = "FixObj/fixobj $subdir/$file $subdir/$newfile";
377 $command = fix_command($command);
378 print "Running '$command'\n";
379 open(OUT, "$command |");
385 # unlink("$subdir/$file");
391 #---------------------------------------------------------------------------
393 # Revision 1.19 1998/05/27 02:25:26 curt
394 # Added a flag to the first run of "triangle" to impose a maximum triangle
395 # size. This forces really flat areas to be subdivided a certain amount
396 # anyways. This makes for slightly more interesting scenery.
398 # Revision 1.18 1998/05/20 20:55:40 curt
401 # Revision 1.17 1998/04/28 01:23:25 curt
402 # Added a work around so that we can get past the "triangle" program
403 # hanging, by aborting and rerunning with that tile marked to use the "-i"
406 # Revision 1.16 1998/04/18 03:57:53 curt
407 # Added zlib library support.
409 # Revision 1.15 1998/04/08 23:24:07 curt
410 # Adopted Gnu automake/autoconf system.
412 # Revision 1.14 1998/04/06 21:09:38 curt
413 # Additional win32 support.
414 # Fixed a bad bug in dem file parsing that was causing the output to be
415 # flipped about x = y.
417 # Revision 1.13 1998/03/19 02:52:52 curt
418 # Updated to reflect some minor tool reorganization and the creation of class
419 # to handle DEM processing needs.
421 # Revision 1.12 1998/03/19 01:48:35 curt
422 # Added gpc-2.01 (generic polygon clipping library)
424 # Revision 1.11 1998/03/03 03:36:57 curt
427 # Revision 1.10 1998/02/01 03:42:26 curt
428 # Modifications to handle compressed dem files.
430 # Revision 1.9 1998/01/27 18:36:54 curt
431 # Lots of updates to get back in sync with changes made over in .../Src/
433 # Revision 1.8 1998/01/21 17:59:05 curt
434 # Uncomment lines to remove several intermediate files.
436 # Revision 1.7 1998/01/19 19:51:06 curt
437 # A couple final pre-release tweaks.
439 # Revision 1.6 1998/01/15 21:33:33 curt
440 # Assembling triangles and building a new .node file with the proper shared
441 # vertices now works. Now we just have to use the shared normals and we'll
444 # Revision 1.5 1998/01/15 02:50:08 curt
445 # Tweaked to add next stage.
447 # Revision 1.4 1998/01/14 15:55:34 curt
448 # Finished splittris, started assemtris.
450 # Revision 1.3 1998/01/14 02:15:52 curt
451 # Updated front end script to keep plugging away on tile fitting.
453 # Revision 1.2 1998/01/12 20:42:08 curt
454 # Working on fitting tiles together in a seamless manner.
456 # Revision 1.1 1998/01/09 23:06:46 curt