Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
uvahotspot
GitHub Repository: uvahotspot/HotSpot
Path: blob/master/scripts/tofig.pl
612 views
1
#!/usr/bin/perl
2
3
# program to convert the floorplan file and FIG-like output
4
# of HotSpot into a FIG file
5
6
#use warnings;
7
8
#params
9
#$maxx = 12600;
10
#$maxy = 9600;
11
$maxx = 9600;
12
$maxy = 12600;
13
$res = 1200;
14
#aspect ratio of blocks to give vertical labels
15
$skinny = 3;
16
17
sub usage () {
18
print("usage: tofig.pl [-a <area ratio>] [-f <fontsize>] [-s <nskip>] <file>\n");
19
print("converts the floorplan file into 'FIG' format and prints to stdout\n");
20
print("[-a <area ratio>] -- approx page occupancy by figure (default 0.95)\n");
21
print("[-f <fontsize>] -- font size to be used (default 10)\n");
22
print("[-s <nskip>] -- no. of entries to be skipped in input (default 0)\n");
23
print("<file> -- input file (eg: ev6.flp or output of print_flp_fig function)\n");
24
exit(1);
25
}
26
27
&usage() if (@ARGV > 7 || !@ARGV % 2 || ! -f $ARGV[@ARGV-1]);
28
29
$occupancy = 0.95;
30
$fontsize = 10;
31
$nskip = 0;
32
33
for($i=0; $i < @ARGV-1; $i+=2) {
34
if ($ARGV[$i] eq "-a") {
35
$occupancy=$ARGV[$i+1];
36
next;
37
}
38
39
if ($ARGV[$i] eq "-f") {
40
$fontsize=$ARGV[$i+1];
41
next;
42
}
43
44
if ($ARGV[$i] eq "-s") {
45
$nskip=$ARGV[$i+1];
46
next;
47
}
48
49
&usage();
50
exit(1);
51
}
52
53
open (FILE, "<$ARGV[@ARGV-1]") || die("error: file $ARGV[@ARGV-1] could not be opened\n");
54
$maxfig = -inf;
55
$minfig = inf;
56
$figinput = 0;
57
while (<FILE>) {
58
if (/FIG starts/) {
59
$figinput = 1;
60
last;
61
}
62
}
63
64
# This is a HotSpot floorplan file and not an output of print_flp_fig.
65
# So let us generate the print_flp_fig output ourselves and save it in a
66
# temporary file.
67
if (!$figinput) {
68
seek(FILE, 0, 0);
69
$timestamp = time();
70
$file = "$ARGV[@ARGV-1].$timestamp";
71
open(NEWFILE, ">$file") || die("error: file $file could not be created\n");
72
print(NEWFILE "FIG starts\n");
73
while (<FILE>) {
74
# skip comments and empty lines
75
next if (/^\s*#|^\s*$/);
76
chomp;
77
@strs = split(/\s+/);
78
if (@strs != 3 && @strs != 5) {
79
unlink($file);
80
die ("error: wrong floorplan input format\n");
81
}
82
# skip connectivity information
83
next if (@strs == 3);
84
($name, $width, $height, $leftx, $bottomy) = @strs;
85
$rightx = $leftx + $width;
86
$topy = $bottomy + $height;
87
printf(NEWFILE "%.16f %.16f %.16f %.16f %.16f %.16f %.16f %.16f %.16f %.16f\n",
88
$leftx, $bottomy, $leftx, $topy, $rightx, $topy, $rightx, $bottomy,
89
$leftx, $bottomy);
90
printf(NEWFILE "%s\n", $name);
91
}
92
print(NEWFILE "FIG ends\n");
93
close(NEWFILE);
94
close(FILE);
95
open(FILE, "<$file") || die("error: file $file could not be opened\n");
96
while (<FILE>) {
97
last if (/FIG starts/);
98
}
99
}
100
101
$pos=tell(FILE);
102
$j=0;
103
while (<FILE>) {
104
last if (/FIG ends/);
105
next if (/[a-zA-Z_]|^\s*$/);
106
if ($j < $nskip) {
107
$j++;
108
next;
109
}
110
chomp;
111
@nums = split(/\s+/);
112
for ($i=0; $i < @nums; $i++) {
113
$maxfig = $nums[$i] if ($nums[$i] > $maxfig);
114
$minfig = $nums[$i] if ($nums[$i] < $minfig);
115
}
116
}
117
118
$maxfig-=$minfig;
119
$scale = (($maxx < $maxy)?$maxx:$maxy) / $maxfig * sqrt($occupancy);
120
$xorig = ($maxx-$maxfig*$scale)/2;
121
$yorig = ($maxy-$maxfig*$scale)/2;
122
123
print "#FIG 3.1\nPortrait\nCenter\nInches\n$res 2\n";
124
seek(FILE, $pos, 0);
125
126
$j=0;
127
while (<FILE>) {
128
last if (/FIG ends/);
129
if ($j < $nskip * 2) {
130
$j++;
131
next;
132
}
133
chomp;
134
@coords = split(/\s+/);
135
next if ($#coords == -1);
136
@coords = map($_-$minfig, @coords);
137
@coords = map($_*$scale, @coords);
138
$leftx = $rightx = $coords[0];
139
$bottomy = $topy = $coords[1];
140
for($i=2; $i < @coords; $i++) {
141
if ($i % 2) {
142
$bottomy = $coords[$i] if ($coords[$i] < $bottomy);
143
$topy = $coords[$i] if ($coords[$i] > $topy);
144
} else {
145
$leftx = $coords[$i] if ($coords[$i] < $leftx);
146
$rightx = $coords[$i] if ($coords[$i] > $rightx);
147
}
148
}
149
for ($i=0; $i < @coords; $i++) {
150
if ($i % 2) {
151
$coords[$i] = int($maxy - $coords[$i] - $yorig);
152
}
153
else {
154
$coords[$i] = int($coords[$i] + $xorig);
155
}
156
}
157
printf("2 2 0 1 -1 7 0 0 -1 0.000 0 0 0 0 0 %d\n", int (@coords)/2);
158
print "\t@coords\n";
159
$xpos = int($xorig + ($leftx + $rightx) / 2.0);
160
$ypos = int($maxy - ($bottomy + $topy) / 2.0 - $yorig);
161
$angle = 0;
162
$angle = 1.5708 if (($topy - $bottomy) > $skinny * ($rightx - $leftx));
163
$name = <FILE>;
164
chomp($name);
165
print "4 1 -1 0 0 0 $fontsize $angle 4 -1 -1 $xpos $ypos $name\\001\n";
166
}
167
close(FILE);
168
169
# delete the temporary file created
170
unlink ($file) if (!$figinput);
171
172