-
Notifications
You must be signed in to change notification settings - Fork 1
/
packing.jl
52 lines (47 loc) · 1.25 KB
/
packing.jl
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
using Stuffing
using Random
# prepare some pseudo images
mask = fill("aa", 500, 800); # can be any AbstractMatrix
m, n = size(mask)
for i in 1:m
for j in 1:n
if (i - m / 2)^2 / (m / 2)^2 + (j - n / 2)^2 / (n / 2)^2 < 1
mask[i,j] = "bb"
end
end
end
objs = []
for i in 1:30
s = 20 + randexp() * 60
obj = fill(true, round(Int, s) + 1, round(Int, s * (0.5 + rand() / 2)) + 1) # Bool Matrix implied that background = false
push!(objs, obj)
end
sort!(objs, by=prod ∘ size, rev=true); # better in descending order of size
# packing
qts = qtrees(objs, mask=mask, maskbackground="aa");
place!(qts);
fit!(qts)
# draw
println("visualization:")
oqt = overlap(qts);
println(repr("text/plain", oqt))
# or
println(QTrees.charimage(oqt, maxlen=97))
# or
using Colors
imageof(qt) = Gray.(QTrees.decode.(qt[1]))
imageof(oqt)
# get layout
println("layout:")
positions = getpositions(qts)
println(positions)
println("or get layout directly")
packing(mask, objs, maskbackground="aa")
# or
qts = qtrees(objs, mask=mask, maskbackground="aa");
packing!(qts)
getpositions(qts)
# set the maximum number of iterations and trainer
qts = qtrees(objs, mask=mask, maskbackground="aa");
packing!(qts, 2000, trainer=Trainer.trainepoch_D!)
getpositions(qts)