]> git.mxchange.org Git - flightgear.git/blob - Tools/process-dem.pl
Several tweaks to start incorporating area cutouts into the pipeline.
[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 $remove_tmps = 0;
31
32 $| = 1;                         # flush buffers after every write
33
34 $do_dem2node =   0;
35 $do_triangle_1 = 0;
36 $do_fixnode =    0;
37 $do_splittris =  0;
38 $do_assemtris =  1;
39 $do_triangle_2 = 1;
40
41 $do_tri2obj =    1;
42 $do_strips =     1;
43 $do_fixobj =     1;
44  
45 $do_install =    1;
46
47
48 if ( $#ARGV < 3 ) {
49     die "Usage: $0 <fg-root-dir> <work-dir> <error^2> dem-file(s)\n";
50 }
51
52 # Start with file.dem
53
54 $fg_root = shift(@ARGV);
55 $work_dir = shift(@ARGV);
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 = "./work/Scenery/w100n040/w094n045";
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     exit;
80     triangle_2() if ( $do_triangle_2);
81     tri2obj() if ( $do_tri2obj );
82     strips() if ( $do_strips );
83     fixobj() if ( $do_fixobj );
84     install() if ( $do_install );
85 }
86
87
88 # exit normally
89 exit(0);
90
91
92 # fix command to work with windoze, replaces first "/" with "\\"
93 sub fix_command {
94     my($in) = @_;
95
96     $system = `uname -s`;
97     chop($system);
98
99     if ( $system =~ m/CYGWIN32/ ) { 
100         $in =~ s/\//\\\\/;
101     }
102
103     return($in);
104 }
105
106
107 # return the file name root (ending at last ".")
108 sub file_root {
109     my($file) = @_;
110     my($pos);
111
112     $pos = rindex($file, ".");
113     return substr($file, 0, $pos);
114 }
115
116
117 # 1.  dem2node work_dir dem_file tolerance^2 (meters)
118
119 #     - dem2node .. dem_file 160000
120 #
121 #     splits dem file into 64 file.node's which contain the
122 #     irregularly fitted vertices
123
124 sub dem2node {
125     $command = "Dem2node/dem2node $work_dir $dem_file $error";
126     $command = fix_command($command);
127     print "Running '$command'\n";
128
129     open(OUT, "$command |");
130     while ( <OUT> ) {
131         print $_;
132         if ( m/^Dir = / ) {
133             $subdir = $_;
134             $subdir =~ s/^Dir = //;
135             chop($subdir);
136         }
137     }
138     close(OUT);
139
140
141
142 # 2.  triangle -q file (Takes file.node and produces file.1.node and
143 #                      file.1.ele)
144
145 print "Subdirectory for this dem file is $subdir\n";
146
147 sub triangle_1 {
148     @FILES = `ls $subdir`;
149     foreach $file ( @FILES ) {
150         # print $file;
151         chop($file);
152         if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
153             # special handling is needed if .poly file exists
154             $fileroot = $file;
155             $fileroot =~ s/\.node$//;
156             print "$subdir/$fileroot\n";
157             if ( -r "$subdir/$fileroot.poly" ) {
158                 $command = "Triangle/triangle -pc -a$max_area -q5 $subdir/$fileroot";
159             } else {
160                 $command = "Triangle/triangle -c -a$max_area -q5 $subdir/$file";
161             }
162             $command = fix_command($command);
163             print "Running '$command'\n";
164             open(OUT, "$command |");
165             while ( <OUT> ) {
166                 print $_;
167             }
168             close(OUT);
169
170             # remove input file.node
171             if ( $remove_tmps ) {
172                 unlink("$subdir/$file");
173             }
174         }
175     }
176 }
177
178
179 # 3.  fixnode file.dem subdir
180 #
181 #     Take the original .dem file (for interpolating Z values) and the
182 #     subdirecotry containing all the file.1.node's and replace with
183 #     fixed file.1.node
184
185 sub fixnode {
186     $command = "FixNode/fixnode $dem_file $subdir";
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             if ( $remove_tmps ) {
229                 unlink("$subdir/$file.node");
230                 unlink("$subdir/$file.node.orig");
231                 unlink("$subdir/$file.ele");
232             }
233         }
234     }
235 }
236
237
238 # 4.2 read in the split of version of the tiles, reconstruct the tile
239 #     using the proper shared corners and edges.  Save as a node file
240 #     so we can retriangulate.
241
242 sub assemtris {
243     @FILES = `ls $subdir`;
244     foreach $file ( @FILES ) {
245         chop($file);
246         if ( $file =~ m/\.1\.body$/ ) {
247             $file =~ s/\.body$//;  # strip off the ".body"
248         
249             $command = "AssemTris/assemtris $subdir/$file";
250             $command = fix_command($command);
251             print "Running '$command'\n";
252             open(OUT, "$command |");
253             while ( <OUT> ) {
254                 print $_;
255             }
256             close(OUT);
257         }
258         if ( $remove_tmps ) {
259             unlink("$subdir/$file.body");
260         }
261     }
262 }
263
264
265 # 4.3 Retriangulate reassembled files (without -q option) so no new
266 #     nodes are generated.
267
268 sub triangle_2 {
269     @FILES = `ls $subdir`;
270     foreach $file ( @FILES ) {
271         print $file;
272         chop($file);
273         if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
274             $base = $file;
275             $base =~ s/\.node$//;
276             print("Test for $subdir/$base.q\n");
277             if ( -r "$subdir/$base.q" ) {
278
279                 # if triangle hangs, we can create a filebase.q for
280                 # the file it hung on.  Then, we test for that file
281                 # here which causes the incremental algorithm to run
282                 # (which shouldn't ever hang.)
283
284                 $command = "Triangle/triangle -i $subdir/$file";
285             } else {
286                 $command = "Triangle/triangle $subdir/$file";
287             }
288             $command = fix_command($command);
289             print "Running '$command'\n";
290             open(OUT, "$command |");
291             while ( <OUT> ) {
292                 print $_;
293             }
294             close(OUT);
295
296             # remove input file.node
297             if ( $remove_tmps ) {
298                 unlink("$subdir/$file");
299             }
300         }
301     }
302 }
303
304
305 # 5.  tri2obj file (.1.node) (.1.ele)
306 #
307 #     Take the file.1.node and file.1.ele and produce file.1.obj
308 #
309 #     Extracts normals out of the shared edge/vertex files, and uses
310 #     the precalcuated normals for these nodes instead of calculating
311 #     new ones.  By sharing normals as well as vertices, not only are
312 #     the gaps between tiles eliminated, but the colors and lighting
313 #     transition smoothly across tile boundaries.
314
315 sub tri2obj {
316     @FILES = `ls $subdir`;
317     foreach $file ( @FILES ) {
318         chop($file);
319         if ( $file =~ m/\.1\.node$/ ) {
320             $file =~ s/\.node$//;  # strip off the ".node"
321             
322             $command = "Tri2obj/tri2obj $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             if ( $remove_tmps ) {
332                 unlink("$subdir/$file.node");
333                 unlink("$subdir/$file.node.orig");
334                 unlink("$subdir/$file.ele");
335             }
336         }
337     }
338 }
339
340
341 # 6.  strip file.1.obj
342
343 #     Strip the file.1.obj's.  Note, strips doesn't handle the minimal
344 #     case of striping a square correctly.
345 #
346 # 7.  cp bands.d file.2.obj
347 #
348 #     strips produces a file called "bands.d" ... copy this to file.2.obj
349
350 sub strips {
351     @FILES = `ls $subdir`;
352     foreach $file ( @FILES ) {
353         chop($file);
354         if ( $file =~ m/\.1\.obj$/ ) {
355             $command = "Stripe_w/strips $subdir/$file";
356             $command = fix_command($command);
357             print "Running '$command'\n";
358             # $input = <STDIN>;
359             open(OUT, "$command |");
360             while ( <OUT> ) {
361                 print $_;
362             }
363             close(OUT);
364             
365             # copy to destination file
366             $newfile = $file;
367             $newfile =~ s/\.1\.obj$//;
368             print "Copying to $subdir/$newfile.2.obj\n";
369             # open(IN, "<bands.d");
370             open(IN, "<stripe.objf");
371             open(OUT, ">$subdir/$newfile.2.obj");
372             while ( <IN> ) {
373                 print OUT $_;
374             }
375             close(IN);
376             close(OUT);
377             
378             if ( $remove_tmps ) {
379                 unlink("$subdir/$file");
380             }
381         }
382     }
383 }
384
385
386 # 8.  fixobj file-new
387 #
388 #     Sort file.2.obj by strip winding
389
390 sub fixobj {
391     @FILES = `ls $subdir`;
392     foreach $file ( @FILES ) {
393         chop($file);
394         if ( $file =~ m/\.2\.obj$/ ) {
395             $newfile = $file;
396             $newfile =~ s/\.2\.obj$/.obj/;
397             
398             $command = "FixObj/fixobj $subdir/$file $subdir/$newfile";
399             $command = fix_command($command);
400             print "Running '$command'\n";
401             open(OUT, "$command |");
402             while ( <OUT> ) {
403                 print $_;
404             }
405             close(OUT);
406
407             if ( $remove_tmps ) {
408                 unlink("$subdir/$file");
409             }
410         }
411     }
412 }
413
414
415 # 9.  install
416 #
417 #     rename, compress, and install scenery files
418
419 sub install {
420     $tmp = $subdir;
421     $tmp =~ s/$work_dir//;
422     # print "Temp dir = $tmp\n";
423     $install_dir = "$fg_root/$tmp";
424     print "Install dir = $install_dir\n";
425     system("mkdir -p $install_dir");
426
427     @FILES = `ls $subdir`;
428     foreach $file ( @FILES ) {
429         chop($file);
430         if ( $file =~ m/\d\d.obj$/ ) {
431             $new_file = file_root($file);
432             
433             $command = "gzip -v --best < $subdir/$file > $install_dir/$new_file.gz";
434             # $command = fix_command($command);
435             print "Running '$command'\n";
436             open(OUT, "$command |");
437             while ( <OUT> ) {
438                 print $_;
439             }
440             close(OUT);
441
442             if ( $remove_tmps ) {
443                 unlink("$subdir/$file");
444             }
445         }
446     }
447 }
448
449
450 #---------------------------------------------------------------------------
451 # $Log$
452 # Revision 1.23  1998/07/20 12:55:35  curt
453 # Several tweaks to start incorporating area cutouts into the pipeline.
454 #
455 # Revision 1.22  1998/07/08 14:49:13  curt
456 # tweaks.
457 #
458 # Revision 1.21  1998/06/08 17:18:37  curt
459 # Mods to test new Stripe fixes from Wilbur Streett.
460 #
461 # Revision 1.20  1998/06/05 18:20:24  curt
462 # Added DemInfo to dump out "A" record DEM info.
463 # Modified process-dem.pl to work in a temp directory and compress/copy the
464 # result to the final destination.
465 #
466 # Revision 1.19  1998/05/27 02:25:26  curt
467 # Added a flag to the first run of "triangle" to impose a maximum triangle
468 # size.  This forces really flat areas to be subdivided a certain amount
469 # anyways.  This makes for slightly more interesting scenery.
470 #
471 # Revision 1.18  1998/05/20 20:55:40  curt
472 # Makefile tweaks
473 #
474 # Revision 1.17  1998/04/28 01:23:25  curt
475 # Added a work around so that we can get past the "triangle" program
476 # hanging, by aborting and rerunning with that tile marked to use the "-i"
477 # option.
478 #
479 # Revision 1.16  1998/04/18 03:57:53  curt
480 # Added zlib library support.
481 #
482 # Revision 1.15  1998/04/08 23:24:07  curt
483 # Adopted Gnu automake/autoconf system.
484 #
485 # Revision 1.14  1998/04/06 21:09:38  curt
486 # Additional win32 support.
487 # Fixed a bad bug in dem file parsing that was causing the output to be
488 # flipped about x = y.
489 #
490 # Revision 1.13  1998/03/19 02:52:52  curt
491 # Updated to reflect some minor tool reorganization and the creation of class
492 # to handle DEM processing needs.
493 #
494 # Revision 1.12  1998/03/19 01:48:35  curt
495 # Added gpc-2.01 (generic polygon clipping library)
496 #
497 # Revision 1.11  1998/03/03 03:36:57  curt
498 # Cumulative tweaks.
499 #
500 # Revision 1.10  1998/02/01 03:42:26  curt
501 # Modifications to handle compressed dem files.
502 #
503 # Revision 1.9  1998/01/27 18:36:54  curt
504 # Lots of updates to get back in sync with changes made over in .../Src/
505 #
506 # Revision 1.8  1998/01/21 17:59:05  curt
507 # Uncomment lines to remove several intermediate files.
508 #
509 # Revision 1.7  1998/01/19 19:51:06  curt
510 # A couple final pre-release tweaks.
511 #
512 # Revision 1.6  1998/01/15 21:33:33  curt
513 # Assembling triangles and building a new .node file with the proper shared
514 # vertices now works.  Now we just have to use the shared normals and we'll
515 # be all set.
516 #
517 # Revision 1.5  1998/01/15 02:50:08  curt
518 # Tweaked to add next stage.
519 #
520 # Revision 1.4  1998/01/14 15:55:34  curt
521 # Finished splittris, started assemtris.
522 #
523 # Revision 1.3  1998/01/14 02:15:52  curt
524 # Updated front end script to keep plugging away on tile fitting.
525 #
526 # Revision 1.2  1998/01/12 20:42:08  curt
527 # Working on fitting tiles together in a seamless manner.
528 #
529 # Revision 1.1  1998/01/09 23:06:46  curt
530 # Initial revision.
531 #