Class: Boxify::Pack
- Inherits:
-
Object
- Object
- Boxify::Pack
- Extended by:
- Forwardable
- Defined in:
- lib/boxify/pack.rb
Constant Summary collapse
- STARTING_LEVEL =
0
Instance Attribute Summary collapse
-
#boxes ⇒ Object
readonly
Returns the value of attribute boxes.
-
#container ⇒ Object
readonly
Returns the value of attribute container.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#placed_boxes ⇒ Object
readonly
Returns the value of attribute placed_boxes.
-
#volume_of_placed_boxes ⇒ Object
readonly
Returns the value of attribute volume_of_placed_boxes.
Instance Method Summary collapse
-
#fill_space(space) ⇒ Object
Fills space with boxes recursively.
-
#initialize(boxes:) ⇒ Pack
constructor
A new instance of Pack.
- #pack ⇒ Object
- #pack_level ⇒ Object
Constructor Details
#initialize(boxes:) ⇒ Pack
Returns a new instance of Pack.
15 16 17 18 19 20 |
# File 'lib/boxify/pack.rb', line 15 def initialize(boxes:) @level = STARTING_LEVEL @boxes = boxes @placed_boxes = PlacedBoxCollection.new @container = Container.new(width: boxes.longest_edge, depth: boxes.second_longest_edge, height: 0) end |
Instance Attribute Details
#boxes ⇒ Object (readonly)
Returns the value of attribute boxes.
7 8 9 |
# File 'lib/boxify/pack.rb', line 7 def boxes @boxes end |
#container ⇒ Object (readonly)
Returns the value of attribute container.
7 8 9 |
# File 'lib/boxify/pack.rb', line 7 def container @container end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
7 8 9 |
# File 'lib/boxify/pack.rb', line 7 def level @level end |
#placed_boxes ⇒ Object (readonly)
Returns the value of attribute placed_boxes.
7 8 9 |
# File 'lib/boxify/pack.rb', line 7 def placed_boxes @placed_boxes end |
#volume_of_placed_boxes ⇒ Object (readonly)
Returns the value of attribute volume_of_placed_boxes.
7 8 9 |
# File 'lib/boxify/pack.rb', line 7 def volume_of_placed_boxes @volume_of_placed_boxes end |
Instance Method Details
#fill_space(space) ⇒ Object
Fills space with boxes recursively
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/boxify/pack.rb', line 63 def fill_space(space) # Find box that fits into this space eligible_box = find_eligible_box(space) if eligible_box pack_box(eligible_box) spaces = SpaceCollection.find_spaces_within_space(space: space, box: eligible_box) # Fill each space with boxes spaces.each do |space| fill_space(space) end end end |
#pack ⇒ Object
22 23 24 25 26 |
# File 'lib/boxify/pack.rb', line 22 def pack pack_level container.placed_boxes = placed_boxes true end |
#pack_level ⇒ Object
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 |
# File 'lib/boxify/pack.rb', line 28 def pack_level # Increment level of box increment_level # Get biggest box as object box = boxes.find_biggest_box_with_minimum_height # Set container height (ck = ck + ci) increment_height(box.height) # Remove box from array (ki = ki - 1) pack_box(box) # Terminate if all boxes have been packed return true if boxes.total_count == 0 # No space left (not even when rotated / length and width swapped) if container.area - box.area <= 0 pack_level else # Space left, check if a package fits in space = Space.new(width: container.width, depth: container.depth, height: container.height) spaces = SpaceCollection.find_spaces_within_space(space: space, box: box) # Fill each space with boxes spaces.each do |space| fill_space(space) end pack_level if boxes.total_count > 0 end end |