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 # return the file name root (ending at last ".")
99 $pos = rindex($file, ".");
100 return substr($file, 0, $pos);
104 # 1. dem2node $FG_ROOT dem_file tolerance^2 (meters)
106 # - dem2node .. dem_file 160000
108 # splits dem file into 64 file.node's which contain the
109 # irregularly fitted vertices
112 if ( $dem_file =~ m/.gz$/ ) {
113 $command = "gzip -dc $dem_file | ./Dem2node/demfit $ENV{FG_ROOT} - $error";
115 $command = "./Dem2node/demfit $ENV{FG_ROOT} $dem_file $error";
118 print "Running '$command'\n";
120 open(OUT, "$command |");
125 $subdir =~ s/^Dir = //;
133 # 2. triangle -q file (Takes file.node and produces file.1.node and
136 print "Subdirectory for this dem file is $subdir\n";
139 @FILES = `ls $subdir`;
140 foreach $file ( @FILES ) {
143 if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
144 $command = "./Triangle/triangle -q $subdir/$file";
145 print "Running '$command'\n";
146 open(OUT, "$command |");
152 # remove input file.node
153 unlink("$subdir/$file");
159 # 3. fixnode file.dem subdir
161 # Take the original .dem file (for interpolating Z values) and the
162 # subdirecotry containing all the file.1.node's and replace with
166 if ( $dem_file =~ m/.gz$/ ) {
167 $command = "gzip -dc $dem_file | ./FixNode/fixnode - $subdir";
169 $command = "./FixNode/fixnode $dem_file $subdir";
171 print "Running '$command'\n";
172 open(OUT, "$command |");
180 # 4.1 splittris file (.1.node) (.1.ele)
182 # Extract the corner, edge, and body vertices (in original
183 # geodetic coordinates) and normals (in cartesian coordinates) and
184 # save them in something very close to the .obj format as file.se,
185 # file.sw, file.nw, file.ne, file.north, file.south, file.east,
186 # file.west, and file.body. This way we can reconstruct the
187 # region using consistant edges and corners.
189 # Arbitration rules: If an opposite edge file already exists,
190 # don't create our matching edge. If a corner already exists,
191 # don't create ours. Basically, the early bird gets the worm and
192 # gets to define the edge verticies and normals. All the other
193 # adjacent tiles must use these.
196 @FILES = `ls $subdir`;
197 foreach $file ( @FILES ) {
199 if ( $file =~ m/\.1\.node$/ ) {
200 $file =~ s/\.node$//; # strip off the ".node"
202 $command = "./SplitTris/splittris $subdir/$file";
203 print "Running '$command'\n";
204 open(OUT, "$command |");
210 unlink("$subdir/$file.node");
211 unlink("$subdir/$file.node.orig");
212 unlink("$subdir/$file.ele");
218 # 4.2 read in the split of version of the tiles, reconstruct the tile
219 # using the proper shared corners and edges. Save as a node file
220 # so we can retriangulate.
223 @FILES = `ls $subdir`;
224 foreach $file ( @FILES ) {
226 if ( $file =~ m/\.1\.body$/ ) {
227 $file =~ s/\.body$//; # strip off the ".body"
229 $command = "./AssemTris/assemtris $subdir/$file";
230 print "Running '$command'\n";
231 open(OUT, "$command |");
237 unlink("$subdir/$file.body");
242 # 4.3 Retriangulate reassembled files (without -q option) so no new
243 # nodes are generated.
246 @FILES = `ls $subdir`;
247 foreach $file ( @FILES ) {
250 if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
251 $command = "./Triangle/triangle $subdir/$file";
252 print "Running '$command'\n";
253 open(OUT, "$command |");
259 # remove input file.node
260 unlink("$subdir/$file");
266 # 5. tri2obj file (.1.node) (.1.ele)
268 # Take the file.1.node and file.1.ele and produce file.1.obj
270 # Extracts normals out of the shared edge/vertex files, and uses
271 # the precalcuated normals for these nodes instead of calculating
272 # new ones. By sharing normals as well as vertices, not only are
273 # the gaps between tiles eliminated, but the colors and lighting
274 # transition smoothly across tile boundaries.
277 @FILES = `ls $subdir`;
278 foreach $file ( @FILES ) {
280 if ( $file =~ m/\.1\.node$/ ) {
281 $file =~ s/\.node$//; # strip off the ".node"
283 $command = "./Tri2obj/tri2obj $subdir/$file";
284 print "Running '$command'\n";
285 open(OUT, "$command |");
291 unlink("$subdir/$file.node");
292 unlink("$subdir/$file.node.orig");
293 unlink("$subdir/$file.ele");
299 # 6. strip file.1.obj
301 # Strip the file.1.obj's. Note, strips doesn't handle the minimal
302 # case of striping a square correctly.
304 # 7. cp bands.d file.2.obj
306 # strips produces a file called "bands.d" ... copy this to file.2.obj
309 @FILES = `ls $subdir`;
310 foreach $file ( @FILES ) {
312 if ( $file =~ m/\.1\.obj$/ ) {
313 $command = "./Stripe_u/strips $subdir/$file";
314 print "Running '$command'\n";
315 open(OUT, "$command |");
321 # copy to destination file
323 $newfile =~ s/\.1\.obj$//;
324 print "Copying to $subdir/$newfile.2.obj\n";
325 open(IN, "<bands.d");
326 open(OUT, ">$subdir/$newfile.2.obj");
333 unlink("$subdir/$file");
341 # Sort file.2.obj by strip winding
344 @FILES = `ls $subdir`;
345 foreach $file ( @FILES ) {
347 if ( $file =~ m/\.2\.obj$/ ) {
349 $newfile =~ s/\.2\.obj$/.obj/;
351 $command = "./FixObj/fixobj $subdir/$file $subdir/$newfile";
352 print "Running '$command'\n";
353 open(OUT, "$command |");
359 unlink("$subdir/$file");
365 #---------------------------------------------------------------------------
367 # Revision 1.11 1998/03/03 03:36:57 curt
370 # Revision 1.10 1998/02/01 03:42:26 curt
371 # Modifications to handle compressed dem files.
373 # Revision 1.9 1998/01/27 18:36:54 curt
374 # Lots of updates to get back in sync with changes made over in .../Src/
376 # Revision 1.8 1998/01/21 17:59:05 curt
377 # Uncomment lines to remove several intermediate files.
379 # Revision 1.7 1998/01/19 19:51:06 curt
380 # A couple final pre-release tweaks.
382 # Revision 1.6 1998/01/15 21:33:33 curt
383 # Assembling triangles and building a new .node file with the proper shared
384 # vertices now works. Now we just have to use the shared normals and we'll
387 # Revision 1.5 1998/01/15 02:50:08 curt
388 # Tweaked to add next stage.
390 # Revision 1.4 1998/01/14 15:55:34 curt
391 # Finished splittris, started assemtris.
393 # Revision 1.3 1998/01/14 02:15:52 curt
394 # Updated front end script to keep plugging away on tile fitting.
396 # Revision 1.2 1998/01/12 20:42:08 curt
397 # Working on fitting tiles together in a seamless manner.
399 # Revision 1.1 1998/01/09 23:06:46 curt