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/w120n030/w111n033";
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 ".node"
194 $command = "./AssemTris/assemtris $subdir/$file";
195 print "Running '$command'\n";
196 open(OUT, "$command |");
206 # 4.3 Retriangulate reassembled files (without -q option) so no new
207 # nodes are generated.
209 if ( $do_triangle_2 ) {
210 @FILES = `ls $subdir`;
211 foreach $file ( @FILES ) {
214 if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
215 $command = "./Triangle/triangle $subdir/$file";
216 print "Running '$command'\n";
217 open(OUT, "$command |");
223 # remove input file.node
224 unlink("$subdir/$file");
230 # 5. tri2obj file (.1.node) (.1.ele)
232 # Take the file.1.node and file.1.ele and produce file.1.obj
234 # Extracts normals out of the shared edge/vertex files, and uses
235 # the precalcuated normals for these nodes instead of calculating
236 # new ones. By sharing normals as well as vertices, not only are
237 # the gaps between tiles eliminated, but the colors and lighting
238 # transition smoothly across tile boundaries.
241 @FILES = `ls $subdir`;
242 foreach $file ( @FILES ) {
244 if ( $file =~ m/\.1\.node$/ ) {
245 $file =~ s/\.node$//; # strip off the ".node"
247 $command = "./Tri2obj/tri2obj $subdir/$file";
248 print "Running '$command'\n";
249 open(OUT, "$command |");
255 # unlink("$subdir/$file.node");
256 # unlink("$subdir/$file.node.orig");
257 # unlink("$subdir/$file.ele");
263 # 6. strip file.1.obj
265 # Strip the file.1.obj's
267 # 7. cp bands.d file.2.obj
269 # strips produces a file called "bands.d" ... copy this to file.2.obj
272 @FILES = `ls $subdir`;
273 foreach $file ( @FILES ) {
275 if ( $file =~ m/\.1\.obj$/ ) {
276 $command = "./Stripe_u/strips $subdir/$file";
277 print "Running '$command'\n";
278 open(OUT, "$command |");
284 # copy to destination file
286 $newfile =~ s/\.1\.obj$//;
287 print "Copying to $subdir/$newfile.2.obj\n";
288 open(IN, "<bands.d");
289 open(OUT, ">$subdir/$newfile.2.obj");
296 unlink("$subdir/$file");
304 # Sort file.2.obj by strip winding
307 @FILES = `ls $subdir`;
308 foreach $file ( @FILES ) {
310 if ( $file =~ m/\.2\.obj$/ ) {
312 $newfile =~ s/\.2\.obj$/.obj/;
314 $command = "./FixObj/fixobj $subdir/$file $subdir/$newfile";
315 print "Running '$command'\n";
316 open(OUT, "$command |");
322 unlink("$subdir/$file");
328 #---------------------------------------------------------------------------
330 # Revision 1.6 1998/01/15 21:33:33 curt
331 # Assembling triangles and building a new .node file with the proper shared
332 # vertices now works. Now we just have to use the shared normals and we'll
335 # Revision 1.5 1998/01/15 02:50:08 curt
336 # Tweaked to add next stage.
338 # Revision 1.4 1998/01/14 15:55:34 curt
339 # Finished splittris, started assemtris.
341 # Revision 1.3 1998/01/14 02:15:52 curt
342 # Updated front end script to keep plugging away on tile fitting.
344 # Revision 1.2 1998/01/12 20:42:08 curt
345 # Working on fitting tiles together in a seamless manner.
347 # Revision 1.1 1998/01/09 23:06:46 curt