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