]> git.mxchange.org Git - flightgear.git/blob - Tools/process-dem.pl
Finished splittris, started assemtris.
[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 $| = 1;                         # flush buffers after every write
30
31 $do_demfit =     0;
32 $do_triangle_1 = 0;
33 $do_fixnode =    0;
34 $do_splittris =  0;
35 $do_assemtris =  1;
36
37 $do_tri2obj =    0;
38 $do_strips =     0;
39 $do_fixobj =     0;
40
41
42 # return the file name root (ending at last ".")
43 sub file_root {
44     my($file) = @_;
45     my($pos);
46
47     $pos = rindex($file, ".");
48     return substr($file, 0, $pos);
49 }
50
51
52 # set the FG_ROOT environment variable if it hasn't already been set.
53 if ( $ENV{FG_ROOT} eq "" ) {
54     # look for a file called fgtop as a place marker
55     if ( -e "fgtop" ) {
56         $ENV{FG_ROOT} = ".";
57     } elsif ( -e "../fgtop" ) {
58         $ENV{FG_ROOT} = "..";
59     }
60 }
61
62
63 # 1.  Start with file.dem
64
65 $dem_file = shift(@ARGV);
66 $error = shift(@ARGV);
67 $error += 0.0;
68
69 print "Source file = $dem_file  Error tolerance = $error\n";
70
71 if ( $error < 0.5 ) {
72     die "I doubt you'll be happy with an error tolerance as low as $error.\n";
73 }
74
75 # 2.  dem2node $FG_ROOT dem_file tolerance^2 (meters)
76
77 #     - dem2node .. dem_file 160000
78 #
79 #     splits dem file into 64 file.node's which contain the
80 #     irregularly fitted vertices
81
82 if ( $do_demfit ) {
83     $command = "./Dem2node/demfit $ENV{FG_ROOT} $dem_file $error";
84
85     print "Running '$command'\n";
86
87     open(OUT, "$command |");
88     while ( <OUT> ) {
89         print $_;
90         if ( m/Scenery/ ) {
91             $subdir = $_;
92             $subdir =~ s/Dir = //;
93         }
94     }
95     close(OUT);
96 } else {
97     $subdir = "../Scenery/w120n030/w111n033";
98     printf("WARNING:  Hardcoding subdir = $subdir
99 }
100
101 # 3.  triangle -q file (Takes file.node and produces file.1.node and
102 #                      file.1.ele)
103
104 print "Subdirectory for this dem file is $subdir\n";
105
106 if ( $do_triangle_1 ) {
107     @FILES = `ls $subdir`;
108     foreach $file ( @FILES ) {
109         print $file;
110         chop($file);
111         if ( ($file =~ m/\.node$/) && ($file !~ m/\.\d\.node$/) ) {
112             $command = "./Triangle/triangle -q $subdir/$file";
113             print "Running '$command'\n";
114             open(OUT, "$command |");
115             while ( <OUT> ) {
116                 print $_;
117             }
118             close(OUT);
119
120             # remove input file.node
121             unlink("$subdir/$file");
122         }
123     }
124 }
125
126 # 4.  fixnode file.dem subdir
127 #
128 #     Take the original .dem file (for interpolating Z values) and the
129 #     subdirecotry containing all the file.1.node's and replace with
130 #     fixed file.1.node
131
132 if ( $do_fixnode ) {
133     $command = "./FixNode/fixnode $dem_file $subdir";
134     print "Running '$command'\n";
135     open(OUT, "$command |");
136     while ( <OUT> ) {
137         print $_;
138     }
139     close(OUT);
140 }
141
142
143 # 4.1 splittris file (.1.node) (.1.ele)
144
145 #     Extract the corner, edge, and body vertices (in original
146 #     geodetic coordinates) and normals (in cartesian coordinates) and
147 #     save them in something very close to the .obj format as file.se,
148 #     file.sw, file.nw, file.ne, file.north, file.south, file.east,
149 #     file.west, and file.body.  This way we can reconstruct the
150 #     region using consistant edges and corners.  
151
152 #     Arbitration rules: If an opposite edge file already exists,
153 #     don't create our matching edge.  If a corner already exists,
154 #     don't create ours.  Basically, the early bird gets the worm and
155 #     gets to define the edge verticies and normals.  All the other
156 #     adjacent tiles must use these.
157
158 if ( $do_splittris ) {
159     @FILES = `ls $subdir`;
160     foreach $file ( @FILES ) {
161         chop($file);
162         if ( $file =~ m/\.1\.node$/ ) {
163             $file =~ s/\.node$//;  # strip off the ".node"
164         
165             $command = "./SplitTris/splittris $subdir/$file";
166             print "Running '$command'\n";
167             open(OUT, "$command |");
168             while ( <OUT> ) {
169                 print $_;
170             }
171             close(OUT);
172         }
173     }
174 }
175
176
177 # 4.2 read in tile sections/ele) skipping edges, read edges out of
178 #     edge files, save including proper shared edges (as node/ele)
179 #     files.  If my edge and adjacent edge both exist, use other,
180 #     delete mine.  If only mine exists, use it.
181
182
183 # 4.3 Retriangulate fixed up files (without -q option)
184
185
186 # 5.  tri2obj file (.1.node) (.1.ele)
187 #
188 #     Take the file.1.node and file.1.ele and produce file.1.obj
189
190 if ( $do_tri2obj ) {
191     @FILES = `ls $subdir`;
192     foreach $file ( @FILES ) {
193         chop($file);
194         if ( $file =~ m/\.1\.node$/ ) {
195             $file =~ s/\.node$//;  # strip off the ".node"
196             
197             $command = "./Tri2obj/tri2obj $subdir/$file";
198             print "Running '$command'\n";
199             open(OUT, "$command |");
200             while ( <OUT> ) {
201                 print $_;
202             }
203             close(OUT);
204             
205             unlink("$subdir/$file.node");
206             unlink("$subdir/$file.node.orig");
207             unlink("$subdir/$file.ele");
208         }
209     }
210 }
211
212
213 # 6.  strip file.1.obj
214
215 #     Strip the file.1.obj's
216 #
217 # 7.  cp bands.d file.2.obj
218 #
219 #     strips produces a file called "bands.d" ... copy this to file.2.obj
220
221 if ( $do_strips ) {
222     @FILES = `ls $subdir`;
223     foreach $file ( @FILES ) {
224         chop($file);
225         if ( $file =~ m/\.1\.obj$/ ) {
226             $command = "./Stripe_u/strips $subdir/$file";
227             print "Running '$command'\n";
228             open(OUT, "$command |");
229             while ( <OUT> ) {
230                 print $_;
231             }
232             close(OUT);
233             
234             # copy to destination file
235             $newfile = $file;
236             $newfile =~ s/\.1\.obj$//;
237             print "Copying to $subdir/$newfile.2.obj\n";
238             open(IN, "<bands.d");
239             open(OUT, ">$subdir/$newfile.2.obj");
240             while ( <IN> ) {
241                 print OUT $_;
242             }
243             close(IN);
244             close(OUT);
245             
246             unlink("$subdir/$file");
247         }
248     }
249 }
250
251
252 # 8.  fixobj file-new
253 #
254 #     Sort file.2.obj by strip winding
255
256 if ( $do_fixobj ) {
257     @FILES = `ls $subdir`;
258     foreach $file ( @FILES ) {
259         chop($file);
260         if ( $file =~ m/\.2\.obj$/ ) {
261             $newfile = $file;
262             $newfile =~ s/\.2\.obj$/.obj/;
263             
264             $command = "./FixObj/fixobj $subdir/$file $subdir/$newfile";
265             print "Running '$command'\n";
266             open(OUT, "$command |");
267             while ( <OUT> ) {
268                 print $_;
269             }
270             close(OUT);
271
272             unlink("$subdir/$file");
273         }
274     }
275 }
276
277
278 #---------------------------------------------------------------------------
279 # $Log$
280 # Revision 1.4  1998/01/14 15:55:34  curt
281 # Finished splittris, started assemtris.
282 #
283 # Revision 1.3  1998/01/14 02:15:52  curt
284 # Updated front end script to keep plugging away on tile fitting.
285 #
286 # Revision 1.2  1998/01/12 20:42:08  curt
287 # Working on fitting tiles together in a seamless manner.
288 #
289 # Revision 1.1  1998/01/09 23:06:46  curt
290 # Initial revision.
291 #