-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw_terrain.pl
77 lines (61 loc) · 2.04 KB
/
draw_terrain.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 -w
#
# Script to create png file from 2d terrain data
#
# copied from https://github.com/rainbow-src/sensors/tree/master/terrain%20generators
use GD;
use strict;
my ($display_x, $display_y) = (800, 800); # 800x800 pixel display pane
my ($terrain, $norm_x, $norm_y) = (0, 0, 0);
my @sensors = ();
my @gws = ();
die "usage: $0 <terrain_file.txt> <output.png>\n"
unless (@ARGV == 2);
my $terrain_file = $ARGV[0];
my $output_file = $ARGV[1];
# COLLECT INFO FROM INPUT FILE
open(FH, "<$terrain_file") or
die "Error: could not open terrain file $terrain_file\n";
while(<FH>){
chomp;
if (/^# stats: (.*)/){
my $stats_line = $1;
if ($stats_line =~ /terrain=([0-9]+\.[0-9]+)m\^2/){
$terrain = $1;
}
$norm_x = sqrt($terrain);
$norm_y = sqrt($terrain);
} elsif (/^# node coords: (.*)/){
my $sensor_coord = $1;
my @coords = split(/\] /, $sensor_coord);
@sensors = map { /([0-9]+) \[([0-9]+\.[0-9]+) ([0-9]+\.[0-9]+)/; [$1, $2, $3]; } @coords;
} elsif (/^# gateway coords: (.*)/){
my $sensor_coord = $1;
my @coords = split(/\] /, $sensor_coord);
@gws = map { /([A-Z]+) \[([0-9]+\.[0-9]+) ([0-9]+\.[0-9]+)/; [$1, $2, $3]; } @coords;
}
}
close(FH);
### GENERATE SVG IMAGE OF TERRAIN ###
my $im = new GD::Image->new($display_x, $display_y);
my $white = $im->colorAllocate(255,255,255);
my $black = $im->colorAllocate(0,0,0);
my $red = $im->colorAllocate(255,0,0);
foreach my $po (@sensors){
my ($s, $x, $y) = @$po;
($x, $y) = (int(($x * $display_x)/$norm_x), int(($y * $display_y)/$norm_y));
$im->rectangle($x-2, $y-2, $x+2, $y+2, $black);
# $im->string(gdSmallFont,$x-2,$y-20,$s,$white);
}
#$im->filledRectangle(3-3, $display_y/2-3, 3+3, $display_y/2+3, $black);
foreach my $po (@gws){
my ($s, $x, $y) = @$po;
($x, $y) = (int(($x * $display_x)/$norm_x), int(($y * $display_y)/$norm_y));
$im->rectangle($x-5, $y-5, $x+5, $y+5, $red);
$im->string(gdGiantFont,$x-2,$y-20,$s,$white);
}
open(FILEOUT, ">$output_file") or
die "could not open file $output_file for writing!";
binmode FILEOUT;
print FILEOUT $im->png;
close FILEOUT;