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 $command = "./Dem2node/demfit $ENV{FG_ROOT} $dem_file $error";
86 print "Running '$command'\n";
88 open(OUT, "$command |");
93 $subdir =~ s/^Dir = //;
99 $subdir = "../Scenery/w100n040/w093n045";
100 print "WARNING: Hardcoding subdir = $subdir\n";
103 # 3. triangle -q file (Takes file.node and produces file.1.node and
106 print "Subdirectory for this dem file is $subdir\n";
108 if ( $do_triangle_1 ) {
109 @FILES = `ls $subdir`;
110 foreach $file ( @FILES ) {
113 if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
114 $command = "./Triangle/triangle -q $subdir/$file";
115 print "Running '$command'\n";
116 open(OUT, "$command |");
122 # remove input file.node
123 unlink("$subdir/$file");
128 # 4. fixnode file.dem subdir
130 # Take the original .dem file (for interpolating Z values) and the
131 # subdirecotry containing all the file.1.node's and replace with
135 $command = "./FixNode/fixnode $dem_file $subdir";
136 print "Running '$command'\n";
137 open(OUT, "$command |");
145 # 4.1 splittris file (.1.node) (.1.ele)
147 # Extract the corner, edge, and body vertices (in original
148 # geodetic coordinates) and normals (in cartesian coordinates) and
149 # save them in something very close to the .obj format as file.se,
150 # file.sw, file.nw, file.ne, file.north, file.south, file.east,
151 # file.west, and file.body. This way we can reconstruct the
152 # region using consistant edges and corners.
154 # Arbitration rules: If an opposite edge file already exists,
155 # don't create our matching edge. If a corner already exists,
156 # don't create ours. Basically, the early bird gets the worm and
157 # gets to define the edge verticies and normals. All the other
158 # adjacent tiles must use these.
160 if ( $do_splittris ) {
161 @FILES = `ls $subdir`;
162 foreach $file ( @FILES ) {
164 if ( $file =~ m/\.1\.node$/ ) {
165 $file =~ s/\.node$//; # strip off the ".node"
167 $command = "./SplitTris/splittris $subdir/$file";
168 print "Running '$command'\n";
169 open(OUT, "$command |");
175 unlink("$subdir/$file.node");
176 unlink("$subdir/$file.node.orig");
177 unlink("$subdir/$file.ele");
183 # 4.2 read in the split of version of the tiles, reconstruct the tile
184 # using the proper shared corners and edges. Save as a node file
185 # so we can retriangulate.
187 if ( $do_assemtris ) {
188 @FILES = `ls $subdir`;
189 foreach $file ( @FILES ) {
191 if ( $file =~ m/\.1\.body$/ ) {
192 $file =~ s/\.body$//; # strip off the ".body"
194 $command = "./AssemTris/assemtris $subdir/$file";
195 print "Running '$command'\n";
196 open(OUT, "$command |");
202 unlink("$subdir/$file.body");
207 # 4.3 Retriangulate reassembled files (without -q option) so no new
208 # nodes are generated.
210 if ( $do_triangle_2 ) {
211 @FILES = `ls $subdir`;
212 foreach $file ( @FILES ) {
215 if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
216 $command = "./Triangle/triangle $subdir/$file";
217 print "Running '$command'\n";
218 open(OUT, "$command |");
224 # remove input file.node
225 unlink("$subdir/$file");
231 # 5. tri2obj file (.1.node) (.1.ele)
233 # Take the file.1.node and file.1.ele and produce file.1.obj
235 # Extracts normals out of the shared edge/vertex files, and uses
236 # the precalcuated normals for these nodes instead of calculating
237 # new ones. By sharing normals as well as vertices, not only are
238 # the gaps between tiles eliminated, but the colors and lighting
239 # transition smoothly across tile boundaries.
242 @FILES = `ls $subdir`;
243 foreach $file ( @FILES ) {
245 if ( $file =~ m/\.1\.node$/ ) {
246 $file =~ s/\.node$//; # strip off the ".node"
248 $command = "./Tri2obj/tri2obj $subdir/$file";
249 print "Running '$command'\n";
250 open(OUT, "$command |");
256 unlink("$subdir/$file.node");
257 unlink("$subdir/$file.node.orig");
258 unlink("$subdir/$file.ele");
264 # 6. strip file.1.obj
266 # Strip the file.1.obj's. Note, strips doesn't handle the minimal
267 # case of striping a square correctly.
269 # 7. cp bands.d file.2.obj
271 # strips produces a file called "bands.d" ... copy this to file.2.obj
274 @FILES = `ls $subdir`;
275 foreach $file ( @FILES ) {
277 if ( $file =~ m/\.1\.obj$/ ) {
278 $command = "./Stripe_u/strips $subdir/$file";
279 print "Running '$command'\n";
280 open(OUT, "$command |");
286 # copy to destination file
288 $newfile =~ s/\.1\.obj$//;
289 print "Copying to $subdir/$newfile.2.obj\n";
290 open(IN, "<bands.d");
291 open(OUT, ">$subdir/$newfile.2.obj");
298 unlink("$subdir/$file");
306 # Sort file.2.obj by strip winding
309 @FILES = `ls $subdir`;
310 foreach $file ( @FILES ) {
312 if ( $file =~ m/\.2\.obj$/ ) {
314 $newfile =~ s/\.2\.obj$/.obj/;
316 $command = "./FixObj/fixobj $subdir/$file $subdir/$newfile";
317 print "Running '$command'\n";
318 open(OUT, "$command |");
324 unlink("$subdir/$file");
330 #---------------------------------------------------------------------------
332 # Revision 1.9 1998/01/27 18:36:54 curt
333 # Lots of updates to get back in sync with changes made over in .../Src/
335 # Revision 1.8 1998/01/21 17:59:05 curt
336 # Uncomment lines to remove several intermediate files.
338 # Revision 1.7 1998/01/19 19:51:06 curt
339 # A couple final pre-release tweaks.
341 # Revision 1.6 1998/01/15 21:33:33 curt
342 # Assembling triangles and building a new .node file with the proper shared
343 # vertices now works. Now we just have to use the shared normals and we'll
346 # Revision 1.5 1998/01/15 02:50:08 curt
347 # Tweaked to add next stage.
349 # Revision 1.4 1998/01/14 15:55:34 curt
350 # Finished splittris, started assemtris.
352 # Revision 1.3 1998/01/14 02:15:52 curt
353 # Updated front end script to keep plugging away on tile fitting.
355 # Revision 1.2 1998/01/12 20:42:08 curt
356 # Working on fitting tiles together in a seamless manner.
358 # Revision 1.1 1998/01/09 23:06:46 curt