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