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