]> git.mxchange.org Git - flightgear.git/blob - Tools/process-dem.pl
31a4624cb8b09833df744717b2e0e795ebded358
[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 $max_area = 10000;            # maximum triangle area
30
31 $| = 1;                         # flush buffers after every write
32
33 $do_dem2node =   1;
34 $do_triangle_1 = 1;
35 $do_fixnode =    1;
36 $do_splittris =  1;
37 $do_assemtris =  1;
38 $do_triangle_2 = 1;
39
40 $do_tri2obj =    1;
41 $do_strips =     1;
42 $do_fixobj =     1;
43
44 $do_install =    1;
45
46
47 if ( $#ARGV < 3 ) {
48     die "Usage: $0 <fg-root-dir> <work-dir> <error^2> dem-file(s)\n";
49 }
50
51 # Start with file.dem
52
53 $fg_root = shift(@ARGV);
54 $work_dir = shift(@ARGV);
55 $error = shift(@ARGV);
56 $error += 0.0;
57
58 while ( $dem_file = shift(@ARGV) ) {
59     print "Source file = $dem_file  Error tolerance = $error\n";
60
61     if ( $error < 0.5 ) {
62         die "I doubt you'll be happy with an error tolerance as " . 
63             "low as $error.\n";
64     }
65
66
67     if ( $do_dem2node ) {
68         dem2node() ;
69     } else {
70         $subdir = "./work/Scenery/e010n080/e019n084";
71         print "WARNING:  Hardcoding subdir = $subdir\n";
72     }
73
74     triangle_1() if ( $do_triangle_1 );
75     fixnode() if ( $do_fixnode );
76     splittris() if ( $do_splittris );
77     assemtris() if ( $do_assemtris );
78     triangle_2() if ( $do_triangle_2);
79     tri2obj() if ( $do_tri2obj );
80     strips() if ( $do_strips );
81     fixobj() if ( $do_fixobj );
82     install() if ( $do_install );
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 work_dir 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 $work_dir $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 -a$max_area -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             $base = $file;
259             $base =~ s/\.node$//;
260             print("Test for $subdir/$base.q\n");
261             if ( -r "$subdir/$base.q" ) {
262
263                 # if triangle hangs, we can create a filebase.q for
264                 # the file it hung on.  Then, we test for that file
265                 # here which causes the incremental algorithm to run
266                 # (which shouldn't ever hang.)
267
268                 $command = "Triangle/triangle -i $subdir/$file";
269             } else {
270                 $command = "Triangle/triangle $subdir/$file";
271             }
272             $command = fix_command($command);
273             print "Running '$command'\n";
274             open(OUT, "$command |");
275             while ( <OUT> ) {
276                 print $_;
277             }
278             close(OUT);
279
280             # remove input file.node
281             unlink("$subdir/$file");
282         }
283     }
284 }
285
286
287 # 5.  tri2obj file (.1.node) (.1.ele)
288 #
289 #     Take the file.1.node and file.1.ele and produce file.1.obj
290 #
291 #     Extracts normals out of the shared edge/vertex files, and uses
292 #     the precalcuated normals for these nodes instead of calculating
293 #     new ones.  By sharing normals as well as vertices, not only are
294 #     the gaps between tiles eliminated, but the colors and lighting
295 #     transition smoothly across tile boundaries.
296
297 sub tri2obj {
298     @FILES = `ls $subdir`;
299     foreach $file ( @FILES ) {
300         chop($file);
301         if ( $file =~ m/\.1\.node$/ ) {
302             $file =~ s/\.node$//;  # strip off the ".node"
303             
304             $command = "Tri2obj/tri2obj $subdir/$file";
305             $command = fix_command($command);
306             print "Running '$command'\n";
307             open(OUT, "$command |");
308             while ( <OUT> ) {
309                 print $_;
310             }
311             close(OUT);
312             
313             unlink("$subdir/$file.node");
314             unlink("$subdir/$file.node.orig");
315             unlink("$subdir/$file.ele");
316         }
317     }
318 }
319
320
321 # 6.  strip file.1.obj
322
323 #     Strip the file.1.obj's.  Note, strips doesn't handle the minimal
324 #     case of striping a square correctly.
325 #
326 # 7.  cp bands.d file.2.obj
327 #
328 #     strips produces a file called "bands.d" ... copy this to file.2.obj
329
330 sub strips {
331     @FILES = `ls $subdir`;
332     foreach $file ( @FILES ) {
333         chop($file);
334         if ( $file =~ m/\.1\.obj$/ ) {
335             $command = "Stripe_u/strips $subdir/$file";
336             $command = fix_command($command);
337             print "Running '$command'\n";
338             open(OUT, "$command |");
339             while ( <OUT> ) {
340                 print $_;
341             }
342             close(OUT);
343             
344             # copy to destination file
345             $newfile = $file;
346             $newfile =~ s/\.1\.obj$//;
347             print "Copying to $subdir/$newfile.2.obj\n";
348             open(IN, "<bands.d");
349             open(OUT, ">$subdir/$newfile.2.obj");
350             while ( <IN> ) {
351                 print OUT $_;
352             }
353             close(IN);
354             close(OUT);
355             
356             unlink("$subdir/$file");
357         }
358     }
359 }
360
361
362 # 8.  fixobj file-new
363 #
364 #     Sort file.2.obj by strip winding
365
366 sub fixobj {
367     @FILES = `ls $subdir`;
368     foreach $file ( @FILES ) {
369         chop($file);
370         if ( $file =~ m/\.2\.obj$/ ) {
371             $newfile = $file;
372             $newfile =~ s/\.2\.obj$/.obj/;
373             
374             $command = "FixObj/fixobj $subdir/$file $subdir/$newfile";
375             $command = fix_command($command);
376             print "Running '$command'\n";
377             open(OUT, "$command |");
378             while ( <OUT> ) {
379                 print $_;
380             }
381             close(OUT);
382
383             unlink("$subdir/$file");
384         }
385     }
386 }
387
388
389 # 9.  install
390 #
391 #     rename, compress, and install scenery files
392
393 sub install {
394     $tmp = $subdir;
395     $tmp =~ s/$work_dir//;
396     # print "Temp dir = $tmp\n";
397     $install_dir = "$fg_root/$tmp";
398     print "Install dir = $install_dir\n";
399     system("mkdir -p $install_dir");
400
401     @FILES = `ls $subdir`;
402     foreach $file ( @FILES ) {
403         chop($file);
404         if ( $file =~ m/\d\d.obj$/ ) {
405             $new_file = file_root($file);
406             
407             $command = "gzip -v --best < $subdir/$file > $install_dir/$new_file.gz";
408             # $command = fix_command($command);
409             print "Running '$command'\n";
410             open(OUT, "$command |");
411             while ( <OUT> ) {
412                 print $_;
413             }
414             close(OUT);
415
416             unlink("$subdir/$file");
417         }
418     }
419 }
420
421
422 #---------------------------------------------------------------------------
423 # $Log$
424 # Revision 1.20  1998/06/05 18:20:24  curt
425 # Added DemInfo to dump out "A" record DEM info.
426 # Modified process-dem.pl to work in a temp directory and compress/copy the
427 # result to the final destination.
428 #
429 # Revision 1.19  1998/05/27 02:25:26  curt
430 # Added a flag to the first run of "triangle" to impose a maximum triangle
431 # size.  This forces really flat areas to be subdivided a certain amount
432 # anyways.  This makes for slightly more interesting scenery.
433 #
434 # Revision 1.18  1998/05/20 20:55:40  curt
435 # Makefile tweaks
436 #
437 # Revision 1.17  1998/04/28 01:23:25  curt
438 # Added a work around so that we can get past the "triangle" program
439 # hanging, by aborting and rerunning with that tile marked to use the "-i"
440 # option.
441 #
442 # Revision 1.16  1998/04/18 03:57:53  curt
443 # Added zlib library support.
444 #
445 # Revision 1.15  1998/04/08 23:24:07  curt
446 # Adopted Gnu automake/autoconf system.
447 #
448 # Revision 1.14  1998/04/06 21:09:38  curt
449 # Additional win32 support.
450 # Fixed a bad bug in dem file parsing that was causing the output to be
451 # flipped about x = y.
452 #
453 # Revision 1.13  1998/03/19 02:52:52  curt
454 # Updated to reflect some minor tool reorganization and the creation of class
455 # to handle DEM processing needs.
456 #
457 # Revision 1.12  1998/03/19 01:48:35  curt
458 # Added gpc-2.01 (generic polygon clipping library)
459 #
460 # Revision 1.11  1998/03/03 03:36:57  curt
461 # Cumulative tweaks.
462 #
463 # Revision 1.10  1998/02/01 03:42:26  curt
464 # Modifications to handle compressed dem files.
465 #
466 # Revision 1.9  1998/01/27 18:36:54  curt
467 # Lots of updates to get back in sync with changes made over in .../Src/
468 #
469 # Revision 1.8  1998/01/21 17:59:05  curt
470 # Uncomment lines to remove several intermediate files.
471 #
472 # Revision 1.7  1998/01/19 19:51:06  curt
473 # A couple final pre-release tweaks.
474 #
475 # Revision 1.6  1998/01/15 21:33:33  curt
476 # Assembling triangles and building a new .node file with the proper shared
477 # vertices now works.  Now we just have to use the shared normals and we'll
478 # be all set.
479 #
480 # Revision 1.5  1998/01/15 02:50:08  curt
481 # Tweaked to add next stage.
482 #
483 # Revision 1.4  1998/01/14 15:55:34  curt
484 # Finished splittris, started assemtris.
485 #
486 # Revision 1.3  1998/01/14 02:15:52  curt
487 # Updated front end script to keep plugging away on tile fitting.
488 #
489 # Revision 1.2  1998/01/12 20:42:08  curt
490 # Working on fitting tiles together in a seamless manner.
491 #
492 # Revision 1.1  1998/01/09 23:06:46  curt
493 # Initial revision.
494 #