-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrosspaths.pl
executable file
·77 lines (65 loc) · 2.07 KB
/
crosspaths.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/perl
#
# crosspaths.pl - find a set of optimal roads crossing a DEM N-S and E-W
#
my $dem = $ARGV[0];
my $vscale = $ARGV[1];
# find the dimensions of the DEM
my $outline = `pngtopam ${dem} | pamfile -size`;
my @sizes = split(' ',$outline);
my $xsize = $sizes[0];
my $ysize = $sizes[1];
print "Input dem (${dem}) is ${xsize} x ${ysize}\n";
my $basecommand = "./pathfinder -d ${dem} -v ${vscale}";
# do N-S road first
my $sx = int($xsize/2);
my $sy = 0;
while (1) {
my $lastsx = $sx;
# travel North and find optimal dest
$sy = $ysize-1;
my $command = $basecommand." -s ${sx} ${sy} --fn --op ns.png";
print "running ($command)\n";
my $results = `${command} | grep \"finish point\"`;
# (finish point is at 1296 0)
my @tokens = split(' ',$results);
# travel South and find optimal dest
$sx = $tokens[4];
$sy = 0;
$command = $basecommand." -s ${sx} ${sy} --fs --op ns.png";
print "running ($command)\n";
$results = `${command} | grep \"finish point\"`;
@tokens = split(' ',$results);
$sx = $tokens[4];
last if ($sx == $lastsx);
}
# now optimize E-W road
$sx = 0;
$sy = int($ysize/2);
while (1) {
my $lastsy = $sy;
# travel West and find optimal dest
$sx = $xsize-1;
my $command = $basecommand." -s ${sx} ${sy} --easy ns.png --fw --op ew.png";
print "running ($command)\n";
my $results = `${command} | grep \"finish point\"`;
# (finish point is at 1296 0)
my @tokens = split(' ',$results);
# travel East and find optimal dest
$sx = 0;
$sy = $tokens[5];
$command = $basecommand." -s ${sx} ${sy} --easy ns.png --fe --op ew.png";
print "running ($command)\n";
$results = `${command} | grep \"finish point\"`;
@tokens = split(' ',$results);
$sy = $tokens[5];
last if ($sy == $lastsy);
}
# finally merge the two paths
my $command = "pngtopam ns.png > .tempns.pam";
print "${command}\n"; system $command;
$command = "pngtopam ew.png > .tempew.pam";
print "${command}\n"; system $command;
$command = "pamarith -maximum .tempns.pam .tempew.pam | pamtopng > nsew.png";
print "${command}\n"; system $command;
unlink ".tempns.pam", ".tempew.pam";