]> git.mxchange.org Git - flightgear.git/blob - Tools/process-dem.pl
c98be79a6184e7482924699290242809d3bff7f8
[flightgear.git] / Tools / process-dem.pl
1 #!/usr/bin/perl
2
3 #---------------------------------------------------------------------------
4 # Toplevel script to automate DEM file processing and conversion
5 #
6 # Written by Curtis Olson, started January 1998.
7 #
8 # Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
9 #
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.
14 #
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.
19 #
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.
23 #
24 # $Id$
25 # (Log is kept at end of this file)
26 #---------------------------------------------------------------------------
27
28
29 $| = 1;                         # flush buffers after every write
30
31 $do_dem2node =     1;
32 $do_triangle_1 = 1;
33 $do_fixnode =    1;
34 $do_splittris =  1;
35 $do_assemtris =  1;
36 $do_triangle_2 = 1;
37
38 $do_tri2obj =    1;
39 $do_strips =     1;
40 $do_fixobj =     1;
41
42
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
46     die "You must remember to set the FG_ROOT environment variable!\n";
47 }
48
49
50 if ( $#ARGV < 1 ) {
51     die "Usage: $0 <error^2> dem-file1 [ dem-file2 dem-file3 ... ]\n";
52 }
53
54 # Start with file.dem
55
56 $error = shift(@ARGV);
57 $error += 0.0;
58
59 while ( $dem_file = shift(@ARGV) ) {
60     print "Source file = $dem_file  Error tolerance = $error\n";
61
62     if ( $error < 0.5 ) {
63         die "I doubt you'll be happy with an error tolerance as " . 
64             "low as $error.\n";
65     }
66
67
68     if ( $do_dem2node ) {
69         dem2node() ;
70     } else {
71         $subdir = "../Scenery/w100n040/w093n045";
72         print "WARNING:  Hardcoding subdir = $subdir\n";
73     }
74
75     triangle_1() if ( $do_triangle_1 );
76     fixnode() if ( $do_fixnode );
77     splittris() if ( $do_splittris );
78     assemtris() if ( $do_assemtris );
79     triangle_2() if ( $do_triangle_2);
80     tri2obj() if ( $do_tri2obj );
81     strips() if ( $do_strips );
82     fixobj() if ( $do_fixobj );
83 }
84
85
86 # exit normally
87 exit(0);
88
89
90 # fix command to work with windoze, replaces first "/" with "\\"
91 sub fix_command {
92     my($in) = @_;
93
94     $system = `uname -s`;
95     chop($system);
96
97     if ( $system =~ m/CYGWIN32/ ) { 
98         $in =~ s/\//\\\\/;
99     }
100
101     return($in);
102 }
103
104
105 # return the file name root (ending at last ".")
106 sub file_root {
107     my($file) = @_;
108     my($pos);
109
110     $pos = rindex($file, ".");
111     return substr($file, 0, $pos);
112 }
113
114
115 # 1.  dem2node $FG_ROOT dem_file tolerance^2 (meters)
116
117 #     - dem2node .. dem_file 160000
118 #
119 #     splits dem file into 64 file.node's which contain the
120 #     irregularly fitted vertices
121
122 sub dem2node {
123     $command = "Dem2node/dem2node $ENV{FG_ROOT} $dem_file $error";
124     $command = fix_command($command);
125     print "Running '$command'\n";
126
127     open(OUT, "$command |");
128     while ( <OUT> ) {
129         print $_;
130         if ( m/^Dir = / ) {
131             $subdir = $_;
132             $subdir =~ s/^Dir = //;
133             chop($subdir);
134         }
135     }
136     close(OUT);
137
138
139
140 # 2.  triangle -q file (Takes file.node and produces file.1.node and
141 #                      file.1.ele)
142
143 print "Subdirectory for this dem file is $subdir\n";
144
145 sub triangle_1 {
146     @FILES = `ls $subdir`;
147     foreach $file ( @FILES ) {
148         print $file;
149         chop($file);
150         if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
151             $command = "Triangle/triangle -q $subdir/$file";
152             $command = fix_command($command);
153             print "Running '$command'\n";
154             open(OUT, "$command |");
155             while ( <OUT> ) {
156                 print $_;
157             }
158             close(OUT);
159
160             # remove input file.node
161             unlink("$subdir/$file");
162         }
163     }
164 }
165
166
167 # 3.  fixnode file.dem subdir
168 #
169 #     Take the original .dem file (for interpolating Z values) and the
170 #     subdirecotry containing all the file.1.node's and replace with
171 #     fixed file.1.node
172
173 sub fixnode {
174     $command = "FixNode/fixnode $dem_file $subdir";
175     $command = fix_command($command);
176     print "Running '$command'\n";
177     open(OUT, "$command |");
178     while ( <OUT> ) {
179         print $_;
180     }
181     close(OUT);
182 }
183
184
185 # 4.1 splittris file (.1.node) (.1.ele)
186
187 #     Extract the corner, edge, and body vertices (in original
188 #     geodetic coordinates) and normals (in cartesian coordinates) and
189 #     save them in something very close to the .obj format as file.se,
190 #     file.sw, file.nw, file.ne, file.north, file.south, file.east,
191 #     file.west, and file.body.  This way we can reconstruct the
192 #     region using consistant edges and corners.  
193
194 #     Arbitration rules: If an opposite edge file already exists,
195 #     don't create our matching edge.  If a corner already exists,
196 #     don't create ours.  Basically, the early bird gets the worm and
197 #     gets to define the edge verticies and normals.  All the other
198 #     adjacent tiles must use these.
199
200 sub splittris {
201     @FILES = `ls $subdir`;
202     foreach $file ( @FILES ) {
203         chop($file);
204         if ( $file =~ m/\.1\.node$/ ) {
205             $file =~ s/\.node$//;  # strip off the ".node"
206         
207             $command = "SplitTris/splittris $subdir/$file";
208             $command = fix_command($command);
209             print "Running '$command'\n";
210             open(OUT, "$command |");
211             while ( <OUT> ) {
212                 print $_;
213             }
214             close(OUT);
215
216             unlink("$subdir/$file.node");
217             unlink("$subdir/$file.node.orig");
218             unlink("$subdir/$file.ele");
219         }
220     }
221 }
222
223
224 # 4.2 read in the split of version of the tiles, reconstruct the tile
225 #     using the proper shared corners and edges.  Save as a node file
226 #     so we can retriangulate.
227
228 sub assemtris {
229     @FILES = `ls $subdir`;
230     foreach $file ( @FILES ) {
231         chop($file);
232         if ( $file =~ m/\.1\.body$/ ) {
233             $file =~ s/\.body$//;  # strip off the ".body"
234         
235             $command = "AssemTris/assemtris $subdir/$file";
236             $command = fix_command($command);
237             print "Running '$command'\n";
238             open(OUT, "$command |");
239             while ( <OUT> ) {
240                 print $_;
241             }
242             close(OUT);
243         }
244         unlink("$subdir/$file.body");
245     }
246 }
247
248
249 # 4.3 Retriangulate reassembled files (without -q option) so no new
250 #     nodes are generated.
251
252 sub triangle_2 {
253     @FILES = `ls $subdir`;
254     foreach $file ( @FILES ) {
255         print $file;
256         chop($file);
257         if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
258             $command = "Triangle/triangle $subdir/$file";
259             $command = fix_command($command);
260             print "Running '$command'\n";
261             open(OUT, "$command |");
262             while ( <OUT> ) {
263                 print $_;
264             }
265             close(OUT);
266
267             # remove input file.node
268             unlink("$subdir/$file");
269         }
270     }
271 }
272
273
274 # 5.  tri2obj file (.1.node) (.1.ele)
275 #
276 #     Take the file.1.node and file.1.ele and produce file.1.obj
277 #
278 #     Extracts normals out of the shared edge/vertex files, and uses
279 #     the precalcuated normals for these nodes instead of calculating
280 #     new ones.  By sharing normals as well as vertices, not only are
281 #     the gaps between tiles eliminated, but the colors and lighting
282 #     transition smoothly across tile boundaries.
283
284 sub tri2obj {
285     @FILES = `ls $subdir`;
286     foreach $file ( @FILES ) {
287         chop($file);
288         if ( $file =~ m/\.1\.node$/ ) {
289             $file =~ s/\.node$//;  # strip off the ".node"
290             
291             $command = "Tri2obj/tri2obj $subdir/$file";
292             $command = fix_command($command);
293             print "Running '$command'\n";
294             open(OUT, "$command |");
295             while ( <OUT> ) {
296                 print $_;
297             }
298             close(OUT);
299             
300             unlink("$subdir/$file.node");
301             unlink("$subdir/$file.node.orig");
302             unlink("$subdir/$file.ele");
303         }
304     }
305 }
306
307
308 # 6.  strip file.1.obj
309
310 #     Strip the file.1.obj's.  Note, strips doesn't handle the minimal
311 #     case of striping a square correctly.
312 #
313 # 7.  cp bands.d file.2.obj
314 #
315 #     strips produces a file called "bands.d" ... copy this to file.2.obj
316
317 sub strips {
318     @FILES = `ls $subdir`;
319     foreach $file ( @FILES ) {
320         chop($file);
321         if ( $file =~ m/\.1\.obj$/ ) {
322             $command = "Stripe_u/strips $subdir/$file";
323             $command = fix_command($command);
324             print "Running '$command'\n";
325             open(OUT, "$command |");
326             while ( <OUT> ) {
327                 print $_;
328             }
329             close(OUT);
330             
331             # copy to destination file
332             $newfile = $file;
333             $newfile =~ s/\.1\.obj$//;
334             print "Copying to $subdir/$newfile.2.obj\n";
335             open(IN, "<bands.d");
336             open(OUT, ">$subdir/$newfile.2.obj");
337             while ( <IN> ) {
338                 print OUT $_;
339             }
340             close(IN);
341             close(OUT);
342             
343             unlink("$subdir/$file");
344         }
345     }
346 }
347
348
349 # 8.  fixobj file-new
350 #
351 #     Sort file.2.obj by strip winding
352
353 sub fixobj {
354     @FILES = `ls $subdir`;
355     foreach $file ( @FILES ) {
356         chop($file);
357         if ( $file =~ m/\.2\.obj$/ ) {
358             $newfile = $file;
359             $newfile =~ s/\.2\.obj$/.obj/;
360             
361             $command = "FixObj/fixobj $subdir/$file $subdir/$newfile";
362             $command = fix_command($command);
363             print "Running '$command'\n";
364             open(OUT, "$command |");
365             while ( <OUT> ) {
366                 print $_;
367             }
368             close(OUT);
369
370             unlink("$subdir/$file");
371         }
372     }
373 }
374
375
376 #---------------------------------------------------------------------------
377 # $Log$
378 # Revision 1.16  1998/04/18 03:57:53  curt
379 # Added zlib library support.
380 #
381 # Revision 1.15  1998/04/08 23:24:07  curt
382 # Adopted Gnu automake/autoconf system.
383 #
384 # Revision 1.14  1998/04/06 21:09:38  curt
385 # Additional win32 support.
386 # Fixed a bad bug in dem file parsing that was causing the output to be
387 # flipped about x = y.
388 #
389 # Revision 1.13  1998/03/19 02:52:52  curt
390 # Updated to reflect some minor tool reorganization and the creation of class
391 # to handle DEM processing needs.
392 #
393 # Revision 1.12  1998/03/19 01:48:35  curt
394 # Added gpc-2.01 (generic polygon clipping library)
395 #
396 # Revision 1.11  1998/03/03 03:36:57  curt
397 # Cumulative tweaks.
398 #
399 # Revision 1.10  1998/02/01 03:42:26  curt
400 # Modifications to handle compressed dem files.
401 #
402 # Revision 1.9  1998/01/27 18:36:54  curt
403 # Lots of updates to get back in sync with changes made over in .../Src/
404 #
405 # Revision 1.8  1998/01/21 17:59:05  curt
406 # Uncomment lines to remove several intermediate files.
407 #
408 # Revision 1.7  1998/01/19 19:51:06  curt
409 # A couple final pre-release tweaks.
410 #
411 # Revision 1.6  1998/01/15 21:33:33  curt
412 # Assembling triangles and building a new .node file with the proper shared
413 # vertices now works.  Now we just have to use the shared normals and we'll
414 # be all set.
415 #
416 # Revision 1.5  1998/01/15 02:50:08  curt
417 # Tweaked to add next stage.
418 #
419 # Revision 1.4  1998/01/14 15:55:34  curt
420 # Finished splittris, started assemtris.
421 #
422 # Revision 1.3  1998/01/14 02:15:52  curt
423 # Updated front end script to keep plugging away on tile fitting.
424 #
425 # Revision 1.2  1998/01/12 20:42:08  curt
426 # Working on fitting tiles together in a seamless manner.
427 #
428 # Revision 1.1  1998/01/09 23:06:46  curt
429 # Initial revision.
430 #