]> git.mxchange.org Git - flightgear.git/blob - Tools/process-dem.pl
Added a flag to the first run of "triangle" to impose a maximum triangle
[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
45 # set the FG_ROOT environment variable if it hasn't already been set.
46 if ( $ENV{FG_ROOT} eq "" ) {
47     # look for a file called fgtop as a place marker
48     die "You must remember to set the FG_ROOT environment variable!\n";
49 }
50
51
52 if ( $#ARGV < 1 ) {
53     die "Usage: $0 <error^2> dem-file1 [ dem-file2 dem-file3 ... ]\n";
54 }
55
56 # Start with file.dem
57
58 $error = shift(@ARGV);
59 $error += 0.0;
60
61 while ( $dem_file = shift(@ARGV) ) {
62     print "Source file = $dem_file  Error tolerance = $error\n";
63
64     if ( $error < 0.5 ) {
65         die "I doubt you'll be happy with an error tolerance as " . 
66             "low as $error.\n";
67     }
68
69
70     if ( $do_dem2node ) {
71         dem2node() ;
72     } else {
73         $subdir = "../Scenery/w120n030/w111n033";
74         print "WARNING:  Hardcoding subdir = $subdir\n";
75     }
76
77     triangle_1() if ( $do_triangle_1 );
78     fixnode() if ( $do_fixnode );
79     splittris() if ( $do_splittris );
80     assemtris() if ( $do_assemtris );
81     triangle_2() if ( $do_triangle_2);
82     tri2obj() if ( $do_tri2obj );
83     strips() if ( $do_strips );
84     fixobj() if ( $do_fixobj );
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 $FG_ROOT 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 $ENV{FG_ROOT} $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             $command = "Triangle/triangle -a$max_area -q $subdir/$file";
154             $command = fix_command($command);
155             print "Running '$command'\n";
156             open(OUT, "$command |");
157             while ( <OUT> ) {
158                 print $_;
159             }
160             close(OUT);
161
162             # remove input file.node
163             unlink("$subdir/$file");
164         }
165     }
166 }
167
168
169 # 3.  fixnode file.dem subdir
170 #
171 #     Take the original .dem file (for interpolating Z values) and the
172 #     subdirecotry containing all the file.1.node's and replace with
173 #     fixed file.1.node
174
175 sub fixnode {
176     $command = "FixNode/fixnode $dem_file $subdir";
177     $command = fix_command($command);
178     print "Running '$command'\n";
179     open(OUT, "$command |");
180     while ( <OUT> ) {
181         print $_;
182     }
183     close(OUT);
184 }
185
186
187 # 4.1 splittris file (.1.node) (.1.ele)
188
189 #     Extract the corner, edge, and body vertices (in original
190 #     geodetic coordinates) and normals (in cartesian coordinates) and
191 #     save them in something very close to the .obj format as file.se,
192 #     file.sw, file.nw, file.ne, file.north, file.south, file.east,
193 #     file.west, and file.body.  This way we can reconstruct the
194 #     region using consistant edges and corners.  
195
196 #     Arbitration rules: If an opposite edge file already exists,
197 #     don't create our matching edge.  If a corner already exists,
198 #     don't create ours.  Basically, the early bird gets the worm and
199 #     gets to define the edge verticies and normals.  All the other
200 #     adjacent tiles must use these.
201
202 sub splittris {
203     @FILES = `ls $subdir`;
204     foreach $file ( @FILES ) {
205         chop($file);
206         if ( $file =~ m/\.1\.node$/ ) {
207             $file =~ s/\.node$//;  # strip off the ".node"
208         
209             $command = "SplitTris/splittris $subdir/$file";
210             $command = fix_command($command);
211             print "Running '$command'\n";
212             open(OUT, "$command |");
213             while ( <OUT> ) {
214                 print $_;
215             }
216             close(OUT);
217
218             unlink("$subdir/$file.node");
219             unlink("$subdir/$file.node.orig");
220             unlink("$subdir/$file.ele");
221         }
222     }
223 }
224
225
226 # 4.2 read in the split of version of the tiles, reconstruct the tile
227 #     using the proper shared corners and edges.  Save as a node file
228 #     so we can retriangulate.
229
230 sub assemtris {
231     @FILES = `ls $subdir`;
232     foreach $file ( @FILES ) {
233         chop($file);
234         if ( $file =~ m/\.1\.body$/ ) {
235             $file =~ s/\.body$//;  # strip off the ".body"
236         
237             $command = "AssemTris/assemtris $subdir/$file";
238             $command = fix_command($command);
239             print "Running '$command'\n";
240             open(OUT, "$command |");
241             while ( <OUT> ) {
242                 print $_;
243             }
244             close(OUT);
245         }
246         unlink("$subdir/$file.body");
247     }
248 }
249
250
251 # 4.3 Retriangulate reassembled files (without -q option) so no new
252 #     nodes are generated.
253
254 sub triangle_2 {
255     @FILES = `ls $subdir`;
256     foreach $file ( @FILES ) {
257         print $file;
258         chop($file);
259         if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
260             $base = $file;
261             $base =~ s/\.node$//;
262             print("Test for $subdir/$base.q\n");
263             if ( -r "$subdir/$base.q" ) {
264
265                 # if triangle hangs, we can create a filebase.q for
266                 # the file it hung on.  Then, we test for that file
267                 # here which causes the incremental algorithm to run
268                 # (which shouldn't ever hang.)
269
270                 $command = "Triangle/triangle -i $subdir/$file";
271             } else {
272                 $command = "Triangle/triangle $subdir/$file";
273             }
274             $command = fix_command($command);
275             print "Running '$command'\n";
276             open(OUT, "$command |");
277             while ( <OUT> ) {
278                 print $_;
279             }
280             close(OUT);
281
282             # remove input file.node
283             unlink("$subdir/$file");
284         }
285     }
286 }
287
288
289 # 5.  tri2obj file (.1.node) (.1.ele)
290 #
291 #     Take the file.1.node and file.1.ele and produce file.1.obj
292 #
293 #     Extracts normals out of the shared edge/vertex files, and uses
294 #     the precalcuated normals for these nodes instead of calculating
295 #     new ones.  By sharing normals as well as vertices, not only are
296 #     the gaps between tiles eliminated, but the colors and lighting
297 #     transition smoothly across tile boundaries.
298
299 sub tri2obj {
300     @FILES = `ls $subdir`;
301     foreach $file ( @FILES ) {
302         chop($file);
303         if ( $file =~ m/\.1\.node$/ ) {
304             $file =~ s/\.node$//;  # strip off the ".node"
305             
306             $command = "Tri2obj/tri2obj $subdir/$file";
307             $command = fix_command($command);
308             print "Running '$command'\n";
309             open(OUT, "$command |");
310             while ( <OUT> ) {
311                 print $_;
312             }
313             close(OUT);
314             
315             unlink("$subdir/$file.node");
316             unlink("$subdir/$file.node.orig");
317             unlink("$subdir/$file.ele");
318         }
319     }
320 }
321
322
323 # 6.  strip file.1.obj
324
325 #     Strip the file.1.obj's.  Note, strips doesn't handle the minimal
326 #     case of striping a square correctly.
327 #
328 # 7.  cp bands.d file.2.obj
329 #
330 #     strips produces a file called "bands.d" ... copy this to file.2.obj
331
332 sub strips {
333     @FILES = `ls $subdir`;
334     foreach $file ( @FILES ) {
335         chop($file);
336         if ( $file =~ m/\.1\.obj$/ ) {
337             $command = "Stripe_u/strips $subdir/$file";
338             $command = fix_command($command);
339             print "Running '$command'\n";
340             open(OUT, "$command |");
341             while ( <OUT> ) {
342                 print $_;
343             }
344             close(OUT);
345             
346             # copy to destination file
347             $newfile = $file;
348             $newfile =~ s/\.1\.obj$//;
349             print "Copying to $subdir/$newfile.2.obj\n";
350             open(IN, "<bands.d");
351             open(OUT, ">$subdir/$newfile.2.obj");
352             while ( <IN> ) {
353                 print OUT $_;
354             }
355             close(IN);
356             close(OUT);
357             
358             unlink("$subdir/$file");
359         }
360     }
361 }
362
363
364 # 8.  fixobj file-new
365 #
366 #     Sort file.2.obj by strip winding
367
368 sub fixobj {
369     @FILES = `ls $subdir`;
370     foreach $file ( @FILES ) {
371         chop($file);
372         if ( $file =~ m/\.2\.obj$/ ) {
373             $newfile = $file;
374             $newfile =~ s/\.2\.obj$/.obj/;
375             
376             $command = "FixObj/fixobj $subdir/$file $subdir/$newfile";
377             $command = fix_command($command);
378             print "Running '$command'\n";
379             open(OUT, "$command |");
380             while ( <OUT> ) {
381                 print $_;
382             }
383             close(OUT);
384
385             # unlink("$subdir/$file");
386         }
387     }
388 }
389
390
391 #---------------------------------------------------------------------------
392 # $Log$
393 # Revision 1.19  1998/05/27 02:25:26  curt
394 # Added a flag to the first run of "triangle" to impose a maximum triangle
395 # size.  This forces really flat areas to be subdivided a certain amount
396 # anyways.  This makes for slightly more interesting scenery.
397 #
398 # Revision 1.18  1998/05/20 20:55:40  curt
399 # Makefile tweaks
400 #
401 # Revision 1.17  1998/04/28 01:23:25  curt
402 # Added a work around so that we can get past the "triangle" program
403 # hanging, by aborting and rerunning with that tile marked to use the "-i"
404 # option.
405 #
406 # Revision 1.16  1998/04/18 03:57:53  curt
407 # Added zlib library support.
408 #
409 # Revision 1.15  1998/04/08 23:24:07  curt
410 # Adopted Gnu automake/autoconf system.
411 #
412 # Revision 1.14  1998/04/06 21:09:38  curt
413 # Additional win32 support.
414 # Fixed a bad bug in dem file parsing that was causing the output to be
415 # flipped about x = y.
416 #
417 # Revision 1.13  1998/03/19 02:52:52  curt
418 # Updated to reflect some minor tool reorganization and the creation of class
419 # to handle DEM processing needs.
420 #
421 # Revision 1.12  1998/03/19 01:48:35  curt
422 # Added gpc-2.01 (generic polygon clipping library)
423 #
424 # Revision 1.11  1998/03/03 03:36:57  curt
425 # Cumulative tweaks.
426 #
427 # Revision 1.10  1998/02/01 03:42:26  curt
428 # Modifications to handle compressed dem files.
429 #
430 # Revision 1.9  1998/01/27 18:36:54  curt
431 # Lots of updates to get back in sync with changes made over in .../Src/
432 #
433 # Revision 1.8  1998/01/21 17:59:05  curt
434 # Uncomment lines to remove several intermediate files.
435 #
436 # Revision 1.7  1998/01/19 19:51:06  curt
437 # A couple final pre-release tweaks.
438 #
439 # Revision 1.6  1998/01/15 21:33:33  curt
440 # Assembling triangles and building a new .node file with the proper shared
441 # vertices now works.  Now we just have to use the shared normals and we'll
442 # be all set.
443 #
444 # Revision 1.5  1998/01/15 02:50:08  curt
445 # Tweaked to add next stage.
446 #
447 # Revision 1.4  1998/01/14 15:55:34  curt
448 # Finished splittris, started assemtris.
449 #
450 # Revision 1.3  1998/01/14 02:15:52  curt
451 # Updated front end script to keep plugging away on tile fitting.
452 #
453 # Revision 1.2  1998/01/12 20:42:08  curt
454 # Working on fitting tiles together in a seamless manner.
455 #
456 # Revision 1.1  1998/01/09 23:06:46  curt
457 # Initial revision.
458 #