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 # return the file name root (ending at last ".")
48 $pos = rindex($file, ".");
49 return substr($file, 0, $pos);
53 # set the FG_ROOT environment variable if it hasn't already been set.
54 if ( $ENV{FG_ROOT} eq "" ) {
55 # look for a file called fgtop as a place marker
58 } elsif ( -e "../fgtop" ) {
64 # 1. Start with file.dem
66 $dem_file = shift(@ARGV);
67 $error = shift(@ARGV);
70 print "Source file = $dem_file Error tolerance = $error\n";
73 die "I doubt you'll be happy with an error tolerance as low as $error.\n";
76 # 2. dem2node $FG_ROOT dem_file tolerance^2 (meters)
78 # - dem2node .. dem_file 160000
80 # splits dem file into 64 file.node's which contain the
81 # irregularly fitted vertices
84 if ( $dem_file =~ m/.gz$/ ) {
85 $command = "gzip -dc $dem_file | ./Dem2node/demfit $ENV{FG_ROOT} - $error";
87 $command = "./Dem2node/demfit $ENV{FG_ROOT} $dem_file $error";
90 print "Running '$command'\n";
92 open(OUT, "$command |");
97 $subdir =~ s/^Dir = //;
103 $subdir = "../Scenery/w100n040/w093n045";
104 print "WARNING: Hardcoding subdir = $subdir\n";
107 # 3. triangle -q file (Takes file.node and produces file.1.node and
110 print "Subdirectory for this dem file is $subdir\n";
112 if ( $do_triangle_1 ) {
113 @FILES = `ls $subdir`;
114 foreach $file ( @FILES ) {
117 if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
118 $command = "./Triangle/triangle -q $subdir/$file";
119 print "Running '$command'\n";
120 open(OUT, "$command |");
126 # remove input file.node
127 unlink("$subdir/$file");
132 # 4. fixnode file.dem subdir
134 # Take the original .dem file (for interpolating Z values) and the
135 # subdirecotry containing all the file.1.node's and replace with
139 if ( $dem_file =~ m/.gz$/ ) {
140 $command = "gzip -dc $dem_file | ./FixNode/fixnode - $subdir";
142 $command = "./FixNode/fixnode $dem_file $subdir";
144 print "Running '$command'\n";
145 open(OUT, "$command |");
153 # 4.1 splittris file (.1.node) (.1.ele)
155 # Extract the corner, edge, and body vertices (in original
156 # geodetic coordinates) and normals (in cartesian coordinates) and
157 # save them in something very close to the .obj format as file.se,
158 # file.sw, file.nw, file.ne, file.north, file.south, file.east,
159 # file.west, and file.body. This way we can reconstruct the
160 # region using consistant edges and corners.
162 # Arbitration rules: If an opposite edge file already exists,
163 # don't create our matching edge. If a corner already exists,
164 # don't create ours. Basically, the early bird gets the worm and
165 # gets to define the edge verticies and normals. All the other
166 # adjacent tiles must use these.
168 if ( $do_splittris ) {
169 @FILES = `ls $subdir`;
170 foreach $file ( @FILES ) {
172 if ( $file =~ m/\.1\.node$/ ) {
173 $file =~ s/\.node$//; # strip off the ".node"
175 $command = "./SplitTris/splittris $subdir/$file";
176 print "Running '$command'\n";
177 open(OUT, "$command |");
183 unlink("$subdir/$file.node");
184 unlink("$subdir/$file.node.orig");
185 unlink("$subdir/$file.ele");
191 # 4.2 read in the split of version of the tiles, reconstruct the tile
192 # using the proper shared corners and edges. Save as a node file
193 # so we can retriangulate.
195 if ( $do_assemtris ) {
196 @FILES = `ls $subdir`;
197 foreach $file ( @FILES ) {
199 if ( $file =~ m/\.1\.body$/ ) {
200 $file =~ s/\.body$//; # strip off the ".body"
202 $command = "./AssemTris/assemtris $subdir/$file";
203 print "Running '$command'\n";
204 open(OUT, "$command |");
210 unlink("$subdir/$file.body");
215 # 4.3 Retriangulate reassembled files (without -q option) so no new
216 # nodes are generated.
218 if ( $do_triangle_2 ) {
219 @FILES = `ls $subdir`;
220 foreach $file ( @FILES ) {
223 if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
224 $command = "./Triangle/triangle $subdir/$file";
225 print "Running '$command'\n";
226 open(OUT, "$command |");
232 # remove input file.node
233 unlink("$subdir/$file");
239 # 5. tri2obj file (.1.node) (.1.ele)
241 # Take the file.1.node and file.1.ele and produce file.1.obj
243 # Extracts normals out of the shared edge/vertex files, and uses
244 # the precalcuated normals for these nodes instead of calculating
245 # new ones. By sharing normals as well as vertices, not only are
246 # the gaps between tiles eliminated, but the colors and lighting
247 # transition smoothly across tile boundaries.
250 @FILES = `ls $subdir`;
251 foreach $file ( @FILES ) {
253 if ( $file =~ m/\.1\.node$/ ) {
254 $file =~ s/\.node$//; # strip off the ".node"
256 $command = "./Tri2obj/tri2obj $subdir/$file";
257 print "Running '$command'\n";
258 open(OUT, "$command |");
264 unlink("$subdir/$file.node");
265 unlink("$subdir/$file.node.orig");
266 unlink("$subdir/$file.ele");
272 # 6. strip file.1.obj
274 # Strip the file.1.obj's. Note, strips doesn't handle the minimal
275 # case of striping a square correctly.
277 # 7. cp bands.d file.2.obj
279 # strips produces a file called "bands.d" ... copy this to file.2.obj
282 @FILES = `ls $subdir`;
283 foreach $file ( @FILES ) {
285 if ( $file =~ m/\.1\.obj$/ ) {
286 $command = "./Stripe_u/strips $subdir/$file";
287 print "Running '$command'\n";
288 open(OUT, "$command |");
294 # copy to destination file
296 $newfile =~ s/\.1\.obj$//;
297 print "Copying to $subdir/$newfile.2.obj\n";
298 open(IN, "<bands.d");
299 open(OUT, ">$subdir/$newfile.2.obj");
306 unlink("$subdir/$file");
314 # Sort file.2.obj by strip winding
317 @FILES = `ls $subdir`;
318 foreach $file ( @FILES ) {
320 if ( $file =~ m/\.2\.obj$/ ) {
322 $newfile =~ s/\.2\.obj$/.obj/;
324 $command = "./FixObj/fixobj $subdir/$file $subdir/$newfile";
325 print "Running '$command'\n";
326 open(OUT, "$command |");
332 unlink("$subdir/$file");
338 #---------------------------------------------------------------------------
340 # Revision 1.10 1998/02/01 03:42:26 curt
341 # Modifications to handle compressed dem files.
343 # Revision 1.9 1998/01/27 18:36:54 curt
344 # Lots of updates to get back in sync with changes made over in .../Src/
346 # Revision 1.8 1998/01/21 17:59:05 curt
347 # Uncomment lines to remove several intermediate files.
349 # Revision 1.7 1998/01/19 19:51:06 curt
350 # A couple final pre-release tweaks.
352 # Revision 1.6 1998/01/15 21:33:33 curt
353 # Assembling triangles and building a new .node file with the proper shared
354 # vertices now works. Now we just have to use the shared normals and we'll
357 # Revision 1.5 1998/01/15 02:50:08 curt
358 # Tweaked to add next stage.
360 # Revision 1.4 1998/01/14 15:55:34 curt
361 # Finished splittris, started assemtris.
363 # Revision 1.3 1998/01/14 02:15:52 curt
364 # Updated front end script to keep plugging away on tile fitting.
366 # Revision 1.2 1998/01/12 20:42:08 curt
367 # Working on fitting tiles together in a seamless manner.
369 # Revision 1.1 1998/01/09 23:06:46 curt