@@ -24,8 +24,8 @@ struct GuillotinePacker{T}
24
24
merge:: Bool
25
25
rect_choice:: FreeRectChoiceHeuristic
26
26
split_method:: GuillotineSplitHeuristic
27
- free_rectangles:: Vector{Rect2D {T}}
28
- used_rectangles:: Vector{Rect2D {T}}
27
+ free_rectangles:: Vector{Rect2 {T}}
28
+ used_rectangles:: Vector{Rect2 {T}}
29
29
30
30
end
31
31
@@ -34,12 +34,12 @@ function GuillotinePacker(width::T, height::T; merge::Bool=true,
34
34
split_method:: GuillotineSplitHeuristic = SplitMinimizeArea) where T
35
35
36
36
packer = GuillotinePacker (width, height, merge, rect_choice, split_method,
37
- Rect2D {T}[], Rect2D {T}[])
38
- push! (packer. free_rectangles, Rect2D (T (0 ), T (0 ), width, height))
37
+ Rect2 {T}[], Rect2 {T}[])
38
+ push! (packer. free_rectangles, Rect2 (T (0 ), T (0 ), width, height))
39
39
return packer
40
40
end
41
41
42
- function Base. push! (packer:: GuillotinePacker , rects:: AbstractVector{Rect2D {T}} ) where T
42
+ function Base. push! (packer:: GuillotinePacker , rects:: AbstractVector{Rect2 {T}} ) where T
43
43
rects = copy (rects)
44
44
free_rectangles = packer. free_rectangles
45
45
used_rectangles = packer. used_rectangles
@@ -104,9 +104,9 @@ function Base.push!(packer::GuillotinePacker, rects::AbstractVector{Rect2D{T}})
104
104
# Otherwise, we're good to go and do the actual packing.
105
105
xy = minimum (free_rectangles[best_free_rect])
106
106
if best_flipped
107
- new_node = Rect2D (xy, reverse (widths (rects[best_rect])))
107
+ new_node = Rect2 (xy, reverse (widths (rects[best_rect])))
108
108
else
109
- new_node = Rect2D (xy, widths (rects[best_rect]))
109
+ new_node = Rect2 (xy, widths (rects[best_rect]))
110
110
end
111
111
112
112
# Remove the free space we lost in the bin.
@@ -126,7 +126,7 @@ function Base.push!(packer::GuillotinePacker, rects::AbstractVector{Rect2D{T}})
126
126
end
127
127
end
128
128
129
- function Base. push! (packer:: GuillotinePacker , rect:: Rect2D )
129
+ function Base. push! (packer:: GuillotinePacker , rect:: Rect2 )
130
130
w, h = widths (rect)
131
131
free_rectangles = packer. free_rectangles
132
132
used_rectangles = packer. used_rectangles
@@ -164,7 +164,7 @@ function Occupancy(packer::GuillotinePacker)
164
164
end
165
165
166
166
# / Returns the heuristic score value for placing a rectangle of size width*height into free_rect. Does not try to rotate.
167
- function score_by_heuristic (width, height, free_rect:: Rect2D , rect_choice:: FreeRectChoiceHeuristic )
167
+ function score_by_heuristic (width, height, free_rect:: Rect2 , rect_choice:: FreeRectChoiceHeuristic )
168
168
rect_choice == RectBestAreaFit && return ScoreBestAreaFit (width, height, free_rect)
169
169
rect_choice == RectBestShortSideFit && return ScoreBestShortSideFit (width, height, free_rect)
170
170
rect_choice == RectBestLongSideFit && return ScoreBestLongSideFit (width, height, free_rect)
@@ -173,69 +173,69 @@ function score_by_heuristic(width, height, free_rect::Rect2D, rect_choice::FreeR
173
173
rect_choice == RectWorstLongSideFit && return ScoreWorstLongSideFit (width, height, free_rect)
174
174
end
175
175
176
- function ScoreBestAreaFit (w, h, free_rect:: Rect2D )
176
+ function ScoreBestAreaFit (w, h, free_rect:: Rect2 )
177
177
return width (free_rect) * height (free_rect) - w * h
178
178
end
179
179
180
- function ScoreBestShortSideFit (w, h, free_rect:: Rect2D )
180
+ function ScoreBestShortSideFit (w, h, free_rect:: Rect2 )
181
181
leftoverHoriz = abs (width (free_rect) - w)
182
182
leftoverVert = abs (height (free_rect) - h)
183
183
leftover = min (leftoverHoriz, leftoverVert)
184
184
return leftover
185
185
end
186
186
187
- function ScoreBestLongSideFit (w, h, free_rect:: Rect2D )
187
+ function ScoreBestLongSideFit (w, h, free_rect:: Rect2 )
188
188
leftoverHoriz = abs (width (free_rect) - w)
189
189
leftoverVert = abs (height (free_rect) - h)
190
190
leftover = max (leftoverHoriz, leftoverVert)
191
191
return leftover
192
192
193
193
end
194
194
195
- function ScoreWorstAreaFit (width, height, free_rect:: Rect2D )
195
+ function ScoreWorstAreaFit (width, height, free_rect:: Rect2 )
196
196
return - ScoreBestAreaFit (width, height, free_rect)
197
197
end
198
198
199
- function ScoreWorstShortSideFit (width, height, free_rect:: Rect2D )
199
+ function ScoreWorstShortSideFit (width, height, free_rect:: Rect2 )
200
200
return - ScoreBestShortSideFit (width, height, free_rect)
201
201
end
202
202
203
- function ScoreWorstLongSideFit (width, height, free_rect:: Rect2D )
203
+ function ScoreWorstLongSideFit (width, height, free_rect:: Rect2 )
204
204
return - ScoreBestLongSideFit (width, height, free_rect)
205
205
end
206
206
207
207
function FindPositionForNewNode (packer:: GuillotinePacker , w, h, rect_choice:: FreeRectChoiceHeuristic )
208
208
free_rectangles = packer. free_rectangles
209
- best_node = Rect2D (0 ,0 ,0 ,0 )
209
+ best_node = Rect2 (0 ,0 ,0 ,0 )
210
210
free_node_idx = 0
211
211
best_score = typemax (Int)
212
212
# / Try each free rectangle to find the best one for placement.
213
213
for i in 1 : length (free_rectangles)
214
214
rect_i = free_rectangles[i]
215
215
# If this is a perfect fit upright, choose it immediately.
216
216
if (w == width (rect_i) && h == height (rect_i))
217
- best_node = Rect2D (minimum (rect_i), Vec (w, h))
217
+ best_node = Rect2 (minimum (rect_i), Vec (w, h))
218
218
free_node_idx = i
219
219
break
220
220
# If this is a perfect fit sideways, choose it.
221
221
elseif (h == width (rect_i) && w == height (rect_i))
222
- best_node = Rect2D (minimum (rect_i), Vec (h, w))
222
+ best_node = Rect2 (minimum (rect_i), Vec (h, w))
223
223
best_score = typemin (Int)
224
224
free_node_idx = i
225
225
break
226
226
# Does the rectangle fit upright?
227
227
elseif (w <= width (rect_i) && h <= height (rect_i))
228
228
score = score_by_heuristic (w, h, rect_i, rect_choice)
229
229
if (score < best_score)
230
- best_node = Rect2D (minimum (rect_i), Vec (w, h))
230
+ best_node = Rect2 (minimum (rect_i), Vec (w, h))
231
231
best_score = score
232
232
free_node_idx = i
233
233
end
234
234
# Does the rectangle fit sideways?
235
235
elseif (h <= width (rect_i) && w <= height (rect_i))
236
236
score = score_by_heuristic (h, w, rect_i, rect_choice)
237
237
if (score < best_score)
238
- best_node = Rect2D (minimum (rect_i), Vec (h, w))
238
+ best_node = Rect2 (minimum (rect_i), Vec (h, w))
239
239
best_score = score
240
240
free_node_idx = i
241
241
end
@@ -244,7 +244,7 @@ function FindPositionForNewNode(packer::GuillotinePacker, w, h, rect_choice::Fre
244
244
return best_node, free_node_idx
245
245
end
246
246
247
- function split_freerect_by_heuristic (packer:: GuillotinePacker , free_rect:: Rect2D , placed_rect:: Rect2D , method:: GuillotineSplitHeuristic )
247
+ function split_freerect_by_heuristic (packer:: GuillotinePacker , free_rect:: Rect2 , placed_rect:: Rect2 , method:: GuillotineSplitHeuristic )
248
248
# Compute the lengths of the leftover area.
249
249
w, h = widths (free_rect) .- widths (placed_rect)
250
250
@@ -283,16 +283,16 @@ end
283
283
284
284
# / This function will add the two generated rectangles into the free_rectangles array. The caller is expected to
285
285
# / remove the original rectangle from the free_rectangles array after that.
286
- function split_freerect_along_axis (packer:: GuillotinePacker , free_rect:: Rect2D , placed_rect:: Rect2D , split_horizontal:: Bool )
286
+ function split_freerect_along_axis (packer:: GuillotinePacker , free_rect:: Rect2 , placed_rect:: Rect2 , split_horizontal:: Bool )
287
287
free_rectangles = packer. free_rectangles
288
288
# Form the two new rectangles.
289
289
x, y = minimum (free_rect)
290
290
if split_horizontal
291
- bottom = Rect2D (x, y + height (placed_rect), width (free_rect), height (free_rect) - height (placed_rect))
292
- right = Rect2D (x + width (placed_rect), y, width (free_rect) - width (placed_rect), height (placed_rect))
291
+ bottom = Rect2 (x, y + height (placed_rect), width (free_rect), height (free_rect) - height (placed_rect))
292
+ right = Rect2 (x + width (placed_rect), y, width (free_rect) - width (placed_rect), height (placed_rect))
293
293
else # Split vertically
294
- bottom = Rect2D (x, y + height (placed_rect), width (placed_rect), height (free_rect) - height (placed_rect))
295
- right = Rect2D (x + width (placed_rect), y, width (free_rect) - width (placed_rect), height (free_rect))
294
+ bottom = Rect2 (x, y + height (placed_rect), width (placed_rect), height (free_rect) - height (placed_rect))
295
+ right = Rect2 (x + width (placed_rect), y, width (free_rect) - width (placed_rect), height (free_rect))
296
296
end
297
297
298
298
# Add the new rectangles into the free rectangle pool if they weren't degenerate.
@@ -318,25 +318,25 @@ function MergeFreeList(packer::GuillotinePacker)
318
318
if (minimum (rect_i)[2 ] == maximum (free_rectangles[j])[2 ])
319
319
new_y = minimum (rect_i)[2 ] - height (free_rectangles[j])
320
320
new_h = height (rect_i) + height (free_rectangles[j])
321
- free_rectangles[i] = Rect2D (minimum (rect_i)[1 ], new_y, Vec (width (rect_i), new_h))
321
+ free_rectangles[i] = Rect2 (minimum (rect_i)[1 ], new_y, Vec (width (rect_i), new_h))
322
322
splice! (free_rectangles, j)
323
323
j -= 1
324
324
elseif (maximum (rect_i)[2 ] == minimum (free_rectangles[j])[2 ])
325
325
new_h = height (rect_i) + height (free_rectangles[j])
326
- free_rectangles[i] = Rect2D (minimum (rect_i), Vec (width (rect_i), new_h))
326
+ free_rectangles[i] = Rect2 (minimum (rect_i), Vec (width (rect_i), new_h))
327
327
splice! (free_rectangles, j)
328
328
j -= 1
329
329
end
330
330
elseif (height (rect_i) == height (free_rectangles[j]) && minimum (rect_i)[2 ] == minimum (free_rectangles[j])[2 ])
331
331
if (minimum (rect_i)[1 ] == minimum (free_rectangles[j])[1 ] + width (free_rectangles[j]))
332
332
new_x = minimum (rect_i)[1 ] - width (free_rectangles[j])
333
333
new_w = width (rect_i) + width (free_rectangles[j])
334
- free_rectangles[i] = Rect2D (new_x, minimum (rect_i)[2 ], new_w, height (rect_i))
334
+ free_rectangles[i] = Rect2 (new_x, minimum (rect_i)[2 ], new_w, height (rect_i))
335
335
splice! (free_rectangles, j)
336
336
j -= 1
337
337
elseif maximum (rect_i)[1 ] == minimum (free_rectangles[j])[1 ]
338
338
new_w = Vec (width (rect_i) + width (free_rectangles[j]), height (rect_i))
339
- free_rectangles[i] = Rect2D (minimum (rect_i), new_w)
339
+ free_rectangles[i] = Rect2 (minimum (rect_i), new_w)
340
340
splice! (free_rectangles, j)
341
341
j -= 1
342
342
end
0 commit comments