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