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