Class: OpenShip::Carton

Inherits:
Box
  • Object
show all
Defined in:
lib/open-ship/sortr.rb

Instance Attribute Summary collapse

Attributes inherited from Box

#height, #label, #length, #product_quantity, #weight, #width

Instance Method Summary collapse

Methods inherited from Box

#volume

Constructor Details

#initializeCarton

Returns a new instance of Carton.



36
37
38
# File 'lib/open-ship/sortr.rb', line 36

def initialize()
  self.box_positions = []
end

Instance Attribute Details

#box_positionsObject

Returns the value of attribute box_positions.



34
35
36
# File 'lib/open-ship/sortr.rb', line 34

def box_positions
  @box_positions
end

#length_marginObject

Returns the value of attribute length_margin.



34
35
36
# File 'lib/open-ship/sortr.rb', line 34

def length_margin
  @length_margin
end

#marginObject

Returns the value of attribute margin.



34
35
36
# File 'lib/open-ship/sortr.rb', line 34

def margin
  @margin
end

#width_marginObject

Returns the value of attribute width_margin.



34
35
36
# File 'lib/open-ship/sortr.rb', line 34

def width_margin
  @width_margin
end

Instance Method Details

#add_box(box) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/open-ship/sortr.rb', line 117

def add_box(box)
  if box.volume > self.free_space
    return nil
  end
  pos = self.position_for_box(box)
  if pos
    bp = OpenShip::BoxPosition.new
    bp.box = box
    bp.position = pos
    self.box_positions << bp
  end
  pos
end

#free_spaceObject



82
83
84
# File 'lib/open-ship/sortr.rb', line 82

def free_space
  self.volume - (self.box_positions.collect { |bp| bp.box }.sum { |bx| bx.volume })
end

#get_space(opts = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/open-ship/sortr.rb', line 58

def get_space(opts={})
  space = []
  z_pos = 0
  while (z_pos < self.height)
    y_pos = 0
    while(y_pos < self.length)
      x_pos = 0
      while(x_pos < self.width)
        pos = Position.new
        pos.x = x_pos
        pos.y = y_pos
        pos.z = z_pos
        unless self.space_packed(pos)
          space << pos
        end
        x_pos += 1
      end
      y_pos += 1
    end
    z_pos += 1
  end
  space
end

#position_for_box(box) ⇒ Object



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
# File 'lib/open-ship/sortr.rb', line 86

def position_for_box(box)
  spot = nil
  free_space = self.get_space
  free_space.each { |sp|
    if ((self.width - sp.x) >= box.width)
      if ((self.length - sp.y) >= box.length)
        if ((self.height - sp.z) >= box.height)
          # Test for potential overlaps
          overlap = false
          self.box_positions.each { |bp|

            if ( ((bp.position.x + bp.box.width) > sp.x) && ((sp.x + box.width) > bp.position.x) )
              if ( ((bp.position.z + bp.box.height) > sp.z) && ((sp.z + box.height) > bp.position.z) )
                if ( ((bp.position.y + bp.box.length) > sp.y) && ((sp.y + box.length) > bp.position.y) )
                  overlap = true;
                  break
                end
              end
            end
          }
          unless overlap
            spot = sp
            break
          end
        end
      end
    end
  }
  spot
end

#space_packed(pos) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/open-ship/sortr.rb', line 42

def space_packed(pos)
  packed = false
  self.box_positions.each { |bp|
    unless packed
      if ((pos.x >= bp.position.x) && (pos.x < (bp.box.width + bp.position.x)))
        if ((pos.y >= bp.position.y) && (pos.y < (bp.box.length + bp.position.y)))
          if ((pos.z >= bp.position.z) && (pos.z < (bp.box.height + bp.position.z)))
            packed = true
          end
        end
      end
    end
  }
  packed
end