easy-box-packer 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/easy-box-packer.rb +241 -41
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 143734a1e9d2822deb70e0e0b8caa961038186d5
|
4
|
+
data.tar.gz: f97e24f8d49fec4fb4de1605d30d9059fc22c9a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d2f127ec392a9da90b0225091f925d6d646b481666185ad49be1e589caa15a26c3a997315a45dd68138b3922191f1c304a114ea723cafd9f8022faf5fc0cf764
|
7
|
+
data.tar.gz: d58a477bac3aadb2a8400837f736f7b38e4c52c28dd3113c3c958b5d2747ac86f3daf39a85794d3284155bfca5b6bc315e1a378c8b3d2501b9c927cba9eb2195
|
data/easy-box-packer.rb
CHANGED
@@ -4,7 +4,7 @@ module EasyBoxPacker
|
|
4
4
|
packings = []
|
5
5
|
errors = []
|
6
6
|
# pack from biggest to smallest
|
7
|
-
items.sort_by { |h| h[:dimensions].sort }.reverse.each do |item|
|
7
|
+
items.sort_by { |h| h[:dimensions].sort.reverse }.reverse.each do |item|
|
8
8
|
# If the item is just too big for the container lets give up on this
|
9
9
|
if item[:weight].to_f > container[:weight_limit].to_f
|
10
10
|
errors << "Item: #{item} is too heavy for container"
|
@@ -19,12 +19,13 @@ module EasyBoxPacker
|
|
19
19
|
# item as well then skip on to the next packing
|
20
20
|
next if packing[:weight].to_f + item[:weight].to_f > container[:weight_limit].to_f
|
21
21
|
|
22
|
+
# remove volume size = 0 (not possible to pack)
|
23
|
+
packing[:spaces].reject! { |space| space[:dimensions].inject(:*) == 0 }
|
22
24
|
# try minimum space first
|
23
25
|
packing[:spaces].sort_by { |h| h[:dimensions].sort }.each do |space|
|
24
26
|
# Try placing the item in this space,
|
25
27
|
# if it doesn't fit skip on the next space
|
26
28
|
next unless placement = place(item, space)
|
27
|
-
|
28
29
|
# Add the item to the packing and
|
29
30
|
# break up the surrounding spaces
|
30
31
|
packing[:placements] += [placement]
|
@@ -52,7 +53,6 @@ module EasyBoxPacker
|
|
52
53
|
errors << "Item: #{item} cannot be placed in container"
|
53
54
|
next
|
54
55
|
end
|
55
|
-
|
56
56
|
# Otherwise lets put the item in a new packing
|
57
57
|
# and break up the remaing free space around it
|
58
58
|
packings += [{
|
@@ -153,7 +153,7 @@ module EasyBoxPacker
|
|
153
153
|
end
|
154
154
|
|
155
155
|
def place(item, space)
|
156
|
-
item_width, item_height
|
156
|
+
item_length, item_width, item_height = item[:dimensions].sort.reverse
|
157
157
|
|
158
158
|
permutations = [
|
159
159
|
[item_width, item_height, item_length],
|
@@ -187,44 +187,244 @@ module EasyBoxPacker
|
|
187
187
|
end
|
188
188
|
|
189
189
|
def break_up_space(space, placement)
|
190
|
-
[
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
190
|
+
return_possible_spaces = [
|
191
|
+
# HEIGHT SPACE => WIDTH => LENGTH
|
192
|
+
[
|
193
|
+
{
|
194
|
+
dimensions: [
|
195
|
+
space[:dimensions][0],
|
196
|
+
space[:dimensions][1],
|
197
|
+
space[:dimensions][2] - placement[:dimensions][2]
|
198
|
+
],
|
199
|
+
position: [
|
200
|
+
space[:position][0],
|
201
|
+
space[:position][1],
|
202
|
+
space[:position][2] + placement[:dimensions][2]
|
203
|
+
]
|
204
|
+
},
|
205
|
+
{
|
206
|
+
dimensions: [
|
207
|
+
space[:dimensions][0],
|
208
|
+
space[:dimensions][1] - placement[:dimensions][1],
|
209
|
+
placement[:dimensions][2]
|
210
|
+
],
|
211
|
+
position: [
|
212
|
+
space[:position][0],
|
213
|
+
space[:position][1] + placement[:dimensions][1],
|
214
|
+
space[:position][2]
|
215
|
+
]
|
216
|
+
},
|
217
|
+
{
|
218
|
+
dimensions: [
|
219
|
+
space[:dimensions][0] - placement[:dimensions][0],
|
220
|
+
placement[:dimensions][1],
|
221
|
+
placement[:dimensions][2]
|
222
|
+
],
|
223
|
+
position: [
|
224
|
+
space[:position][0] + placement[:dimensions][0],
|
225
|
+
space[:position][1],
|
226
|
+
space[:position][2]
|
227
|
+
]
|
228
|
+
}
|
229
|
+
],
|
230
|
+
# HEIGHT SPACE => LENGTH => WIDTH
|
231
|
+
[
|
232
|
+
{
|
233
|
+
dimensions: [
|
234
|
+
space[:dimensions][0],
|
235
|
+
space[:dimensions][1],
|
236
|
+
space[:dimensions][2] - placement[:dimensions][2]
|
237
|
+
],
|
238
|
+
position: [
|
239
|
+
space[:position][0],
|
240
|
+
space[:position][1],
|
241
|
+
space[:position][2] + placement[:dimensions][2]
|
242
|
+
]
|
243
|
+
},
|
244
|
+
{
|
245
|
+
dimensions: [
|
246
|
+
space[:dimensions][0] - placement[:dimensions][0],
|
247
|
+
space[:dimensions][1],
|
248
|
+
placement[:dimensions][2]
|
249
|
+
],
|
250
|
+
position: [
|
251
|
+
space[:position][0] + placement[:dimensions][0],
|
252
|
+
space[:position][1],
|
253
|
+
space[:position][2]
|
254
|
+
]
|
255
|
+
},
|
256
|
+
{
|
257
|
+
dimensions: [
|
258
|
+
placement[:dimensions][0],
|
259
|
+
space[:dimensions][1] - placement[:dimensions][1],
|
260
|
+
placement[:dimensions][2]
|
261
|
+
],
|
262
|
+
position: [
|
263
|
+
space[:position][0],
|
264
|
+
space[:position][1] + placement[:dimensions][1],
|
265
|
+
space[:position][2]
|
266
|
+
]
|
267
|
+
}
|
268
|
+
],
|
269
|
+
# LENGTH SPACE => HEIGHT => WIDTH
|
270
|
+
[
|
271
|
+
{
|
272
|
+
dimensions: [
|
273
|
+
space[:dimensions][0] - placement[:dimensions][0],
|
274
|
+
space[:dimensions][1],
|
275
|
+
space[:dimensions][2]
|
276
|
+
],
|
277
|
+
position: [
|
278
|
+
space[:position][0] + placement[:dimensions][0],
|
279
|
+
space[:position][1],
|
280
|
+
space[:position][2]
|
281
|
+
]
|
282
|
+
},
|
283
|
+
{
|
284
|
+
dimensions: [
|
285
|
+
placement[:dimensions][0],
|
286
|
+
space[:dimensions][1],
|
287
|
+
space[:dimensions][2] - placement[:dimensions][2]
|
288
|
+
],
|
289
|
+
position: [
|
290
|
+
space[:position][0],
|
291
|
+
space[:position][1],
|
292
|
+
space[:position][2] + placement[:dimensions][2]
|
293
|
+
]
|
294
|
+
},
|
295
|
+
{
|
296
|
+
dimensions: [
|
297
|
+
placement[:dimensions][0],
|
298
|
+
space[:dimensions][1] - placement[:dimensions][1],
|
299
|
+
placement[:dimensions][2]
|
300
|
+
],
|
301
|
+
position: [
|
302
|
+
space[:position][0],
|
303
|
+
space[:position][1] + placement[:dimensions][1],
|
304
|
+
space[:position][2]
|
305
|
+
]
|
306
|
+
}
|
307
|
+
],
|
308
|
+
# LENGTH SPACE => WIDTH => HEIGHT
|
309
|
+
[
|
310
|
+
{
|
311
|
+
dimensions: [
|
312
|
+
space[:dimensions][0] - placement[:dimensions][0],
|
313
|
+
space[:dimensions][1],
|
314
|
+
space[:dimensions][2]
|
315
|
+
],
|
316
|
+
position: [
|
317
|
+
space[:position][0] + placement[:dimensions][0],
|
318
|
+
space[:position][1],
|
319
|
+
space[:position][2]
|
320
|
+
]
|
321
|
+
},
|
322
|
+
{
|
323
|
+
dimensions: [
|
324
|
+
placement[:dimensions][0],
|
325
|
+
space[:dimensions][1] - placement[:dimensions][1],
|
326
|
+
space[:dimensions][2]
|
327
|
+
],
|
328
|
+
position: [
|
329
|
+
space[:position][0],
|
330
|
+
space[:position][1] + placement[:dimensions][1],
|
331
|
+
space[:position][2]
|
332
|
+
]
|
333
|
+
},
|
334
|
+
{
|
335
|
+
dimensions: [
|
336
|
+
placement[:dimensions][0],
|
337
|
+
placement[:dimensions][1],
|
338
|
+
space[:dimensions][2] - placement[:dimensions][2]
|
339
|
+
],
|
340
|
+
position: [
|
341
|
+
space[:position][0],
|
342
|
+
space[:position][1],
|
343
|
+
space[:position][2] + placement[:dimensions][2]
|
344
|
+
]
|
345
|
+
}
|
346
|
+
],
|
347
|
+
# WIDTH SPACE => LENGTH => HEIGHT
|
348
|
+
[
|
349
|
+
{
|
350
|
+
dimensions: [
|
351
|
+
space[:dimensions][0],
|
352
|
+
space[:dimensions][1] - placement[:dimensions][1],
|
353
|
+
space[:dimensions][2]
|
354
|
+
],
|
355
|
+
position: [
|
356
|
+
space[:position][0],
|
357
|
+
space[:position][1] + placement[:dimensions][1],
|
358
|
+
space[:position][2]
|
359
|
+
]
|
360
|
+
},
|
361
|
+
{
|
362
|
+
dimensions: [
|
363
|
+
space[:dimensions][0] - placement[:dimensions][0],
|
364
|
+
placement[:dimensions][1],
|
365
|
+
space[:dimensions][2]
|
366
|
+
],
|
367
|
+
position: [
|
368
|
+
space[:position][0] + placement[:dimensions][0],
|
369
|
+
space[:position][1],
|
370
|
+
space[:position][2]
|
371
|
+
]
|
372
|
+
},
|
373
|
+
{
|
374
|
+
dimensions: [
|
375
|
+
placement[:dimensions][0],
|
376
|
+
placement[:dimensions][1],
|
377
|
+
space[:dimensions][2] - placement[:dimensions][2]
|
378
|
+
],
|
379
|
+
position: [
|
380
|
+
space[:position][0],
|
381
|
+
space[:position][1],
|
382
|
+
space[:position][2] + placement[:dimensions][2]
|
383
|
+
]
|
384
|
+
}
|
385
|
+
],
|
386
|
+
# WIDTH SPACE => HEIGHT => LENGTH
|
387
|
+
[
|
388
|
+
{
|
389
|
+
dimensions: [
|
390
|
+
space[:dimensions][0],
|
391
|
+
space[:dimensions][1] - placement[:dimensions][1],
|
392
|
+
space[:dimensions][2]
|
393
|
+
],
|
394
|
+
position: [
|
395
|
+
space[:position][0],
|
396
|
+
space[:position][1] + placement[:dimensions][1],
|
397
|
+
space[:position][2]
|
398
|
+
]
|
399
|
+
},
|
400
|
+
{
|
401
|
+
dimensions: [
|
402
|
+
space[:dimensions][0],
|
403
|
+
placement[:dimensions][1],
|
404
|
+
space[:dimensions][2] - placement[:dimensions][2]
|
405
|
+
],
|
406
|
+
position: [
|
407
|
+
space[:position][0],
|
408
|
+
space[:position][1],
|
409
|
+
space[:position][2] + placement[:dimensions][2]
|
410
|
+
]
|
411
|
+
},
|
412
|
+
{
|
413
|
+
dimensions: [
|
414
|
+
space[:dimensions][0] - placement[:dimensions][0],
|
415
|
+
placement[:dimensions][1],
|
416
|
+
placement[:dimensions][2]
|
417
|
+
],
|
418
|
+
position: [
|
419
|
+
space[:position][0] + placement[:dimensions][0],
|
420
|
+
space[:position][1],
|
421
|
+
space[:position][2]
|
422
|
+
]
|
423
|
+
}
|
424
|
+
]
|
227
425
|
]
|
426
|
+
# PICK biggest
|
427
|
+
return_possible_spaces.sort_by { |a| a.map {|aa| aa[:dimensions].sort}}.last
|
228
428
|
end
|
229
429
|
end
|
230
430
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easy-box-packer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aloha Chen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-04-
|
11
|
+
date: 2017-04-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|