File size: 3,060 Bytes
c2ea21f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
function [newBox, constraint] = align_adjacent_room3(box, tempBox, updated, type, threshold)
% position of box1 relative to box2
% 0 left-above
% 1 left-below
% 2 left-of
% 3 above
% 4 inside
% 5 surrounding
% 6 below
% 7 right-of
% 8 right-above
% 9 right-below


newBox = box;
constraint = zeros(4, 2);
idx = 1;

if type == 0
    alignV(true);
    alignH(true);
elseif type == 1
    alignV(true);
    alignH(false);
elseif type == 2
    align([2,1], [1,3], threshold);
    align([2,2], [1,2], threshold/2);
    align([2,4], [1,4], threshold/2);
elseif type == 3
    align([2,2], [1,4], threshold);
    align([2,1], [1,1], threshold/2);
    align([2,3], [1,3], threshold/2);
elseif type == 4
    align([2,1], [1,1], true);
    align([2,2], [1,2], true);
    align([2,3], [1,3], true);
    align([2,4], [1,4], true);
elseif type == 5
    align([1,1], [2,1], true);
    align([1,2], [2,2], true);
    align([1,3], [2,3], true);
    align([1,4], [2,4], true);
elseif type == 6
    align([2,4], [1,2], threshold);
    align([2,1], [1,1], threshold/2);
    align([2,3], [1,3], threshold/2);
elseif type == 7
    align([2,3], [1,1], threshold);
    align([2,2], [1,2], threshold/2);
    align([2,4], [1,4], threshold/2);
elseif type == 8
    alignV(false);
    alignH(true);
elseif type == 9
    alignV(false);
    alignH(false);
end

constraint = constraint(1:idx-1, :);

function alignV(isLeft)
    if isLeft
        idx1 = 1;
        idx2 = 3;
    else
        idx1 = 3;
        idx2 = 1;
    end
    
    if abs(tempBox(2,idx1) - tempBox(1,idx2)) <= abs(tempBox(2,idx2) - tempBox(1,idx2))
        align([2,idx1], [1,idx2], threshold/2)
    else
        align([2,idx2], [1,idx2], threshold/2)
    end
end

function alignH(isAbove)
    if isAbove
        idx1 = 2;
        idx2 = 4;
    else
        idx1 = 4;
        idx2 = 2;
    end
    
    if abs(tempBox(2,idx1) - tempBox(1,idx2)) <= abs(tempBox(2,idx2) - tempBox(1,idx2))
        align([2,idx1], [1,idx2], threshold/2)
    else
        align([2,idx2], [1,idx2], threshold/2)
    end
end

function align(idx1, idx2, threshold, attach)
    if nargin < 4
        attach = false;
    end
    if abs(tempBox(idx1(1),idx1(2))- tempBox(idx2(1), idx2(2))) <= threshold    
        if updated(idx1(1), idx1(2)) && ~updated(idx2(1), idx2(2))
            newBox(idx2(1), idx2(2)) = newBox(idx1(1),idx1(2));
        elseif updated(idx2(1), idx2(2)) && ~updated(idx1(1), idx1(2))
            newBox(idx1(1), idx1(2)) = newBox(idx2(1),idx2(2));
        elseif ~updated(idx1(1), idx1(2)) && ~updated(idx2(1), idx2(2))
            if attach
                newBox(idx2(1), idx2(2)) = newBox(idx1(1),idx1(2));
            else
                y = (newBox(idx1(1),idx1(2)) + newBox(idx2(1), idx2(2)))/2;
                newBox(idx1(1),idx1(2)) = y;
                newBox(idx2(1), idx2(2)) = y;
            end
        end
        
        if idx1(1) == 1
            constraint(idx, :) = [idx1(2) idx2(2)];
        else
            constraint(idx, :) = [idx2(2) idx1(2)];
        end
        idx = idx + 1;
    end
end

end