-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplitFit.java
More file actions
204 lines (178 loc) · 6.46 KB
/
Copy pathSplitFit.java
File metadata and controls
204 lines (178 loc) · 6.46 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import java.util.*;
public class SplitFit extends StripPacking {
private class Block {
public int startX;
public int bottomY;
public int topY;
public ArrayList<FloatingRect> rects;
public Block(int startX, int bottomY, int topY) {
this.startX = startX;
this.bottomY = bottomY;
this.topY = topY;
}
public Block(int bottomY, int height) {
this(0, bottomY, bottomY + height);
}
}
public SplitFit(FloatingRect[] floatingRects, int width) {
super(floatingRects, width);
}
public Comparator<FloatingRect> decreasingHeight = (fr1,fr2) -> {
if (fr1 == null) {
if (fr2 == null) return 0;
return 1;
}
if (fr2 == null) return -1;
if (fr1.height < fr2.height) return 1;
if (fr1.height > fr2.height) return -1;
return 0;
};
@Override
public void execute() {
int maxWidth = 0;
for (int i=0;i<floatingRects.length;++i) {
if (floatingRects[i].width > maxWidth) {
maxWidth = floatingRects[i].width;
}
}
int m = (int)(width/maxWidth);
//System.out.println("m = " + m);
float bound1 = (float)width/(m+1);
FloatingRect[] l1 = new FloatingRect[floatingRects.length];
FloatingRect[] l2 = new FloatingRect[floatingRects.length];
int l1Size = 0;
int l2Size = 0;
for (int i=0;i<floatingRects.length;++i) {
floatingRects[i].id = i;
if (floatingRects[i].width > bound1) {
l1[l1Size] = floatingRects[i];
l1Size++;
} else {
l2[l2Size] = floatingRects[i];
l2Size++;
}
}
Arrays.sort(l1, decreasingHeight);
Arrays.sort(l2, decreasingHeight);
// Run FFDH on l1
ArrayList<Block> blocks = new ArrayList<>();
for (int i=0;i<l1Size;++i) {
boolean placed = false;
FloatingRect frect = l1[i];
int topBlockY = 0;
for (int j=0;j<blocks.size();++j) {
Block block = blocks.get(j);
topBlockY = block.topY;
if (block.startX + frect.width <= width) {
block.rects.add(frect);
block.startX += frect.width;
placed = true;
break;
}
}
if (!placed) {
Block block = new Block(topBlockY, frect.height);
blocks.add(block);
block.rects = new ArrayList<>();
block.rects.add(frect);
block.startX = frect.width;
}
}
// Partitioning blocks into <= (m+1)/(m+2) and > (m+1)/(m+2)
float bound2 = (float)width*(m+1)/(m+2);
Block[] arrangedBlocks = new Block[blocks.size()];
int index = 0;
int currHeight = 0;
for (int i=0;i<blocks.size();++i) {
Block block = blocks.get(i);
if (block.startX > bound2) {
int currentbottom = currHeight;
currHeight += block.topY - block.bottomY;
block.bottomY = currentbottom;
block.topY = currHeight;
arrangedBlocks[index] = block;
index++;
}
}
int rBaseHeight = currHeight;
for (int i=0;i<blocks.size();++i) {
Block block = blocks.get(i);
if (block.startX <= bound2) {
int currentbottom = currHeight;
currHeight += block.topY - block.bottomY;
block.bottomY = currentbottom;
block.topY = currHeight;
arrangedBlocks[index] = block;
index++;
}
}
int rMaxHeight = currHeight;
// Actually place the blocks in L1.
for (int i=0;i<arrangedBlocks.length; ++i) {
int left = 0;
Block block = arrangedBlocks[i];
for (FloatingRect frect : block.rects) {
rects[frect.id] = frect.place(left, block.bottomY);
left += frect.width;
snapshot(rects[frect.id].y2);
}
}
//System.out.println(bound1 + ", " + bound2);
int rBaseX = (int)Math.ceil(bound2);
//this.height = computeHeight();if("".isEmpty())return;
// Run FFDH on l2
blocks = new ArrayList<>();
ArrayList<Block> rBlocks = new ArrayList<>();
int topBlockY = rMaxHeight; // R is the rectangle that appears after rearranging L1.
int topRBlockY = rBaseHeight;
for (int i=0;i<l2Size;++i) {
boolean placed = false;
FloatingRect frect = l2[i];
for (int j=0;j<rBlocks.size();++j) {
if (tryPlace(rBlocks.get(j), frect)) {
placed = true;
break;
}
}
if (!placed) {
for (int j=0;j<blocks.size();++j) {
if (tryPlace(blocks.get(j), frect)) {
placed = true;
break;
}
}
}
if (!placed && frect.width + rBaseX <= width && topRBlockY + frect.height <= rMaxHeight) {
Block block = new Block(topRBlockY, frect.height);
rBlocks.add(block);
Rect rect = frect.place(rBaseX, block.bottomY);
block.startX = rect.x2;
rects[frect.id] = rect;
topRBlockY = block.topY;
placed = true;
}
if (!placed) {
Block block = new Block(topBlockY, frect.height);
blocks.add(block);
Rect rect = frect.place(block.startX, block.bottomY);
block.startX = rect.x2;
rects[frect.id] = rect;
topBlockY = block.topY;
}
snapshot(topBlockY);
}
this.height = computeHeight();
}
private boolean tryPlace(Block block, FloatingRect frect) {
if (block.startX + frect.width <= width) {
Rect rect = frect.place(block.startX, block.bottomY);
rects[frect.id] = rect;
block.startX = rect.x2;
return true;
}
return false;
}
private void snapshot(int currHeight) {
snapshotFunction.run(rects, null, currHeight);
}
}