Class: Spritz::MaxRectsPacker
- Inherits:
-
Object
- Object
- Spritz::MaxRectsPacker
- Defined in:
- lib/spritz/max_rects_packer.rb
Overview
Adapted from C++ version originally by Jukka Jylänki.
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #coverage_ratio ⇒ Object
-
#initialize(width, height) ⇒ MaxRectsPacker
constructor
A new instance of MaxRectsPacker.
- #insert(value, width, height) ⇒ Object
- #rects ⇒ Object
Constructor Details
#initialize(width, height) ⇒ MaxRectsPacker
Returns a new instance of MaxRectsPacker.
6 7 8 9 10 11 |
# File 'lib/spritz/max_rects_packer.rb', line 6 def initialize(width, height) @width = width @height = height @free_rects = [Rect.new(nil, 0, 0, @width, @height)] @used_rects = [] end |
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
64 65 66 |
# File 'lib/spritz/max_rects_packer.rb', line 64 def height @height end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
63 64 65 |
# File 'lib/spritz/max_rects_packer.rb', line 63 def width @width end |
Instance Method Details
#coverage_ratio ⇒ Object
54 55 56 57 |
# File 'lib/spritz/max_rects_packer.rb', line 54 def coverage_ratio return @used_rects.inject(0) { |sum, rect| sum + rect.width * rect.height } / (@width * @height).to_f end |
#insert(value, width, height) ⇒ Object
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 |
# File 'lib/spritz/max_rects_packer.rb', line 13 def insert(value, width, height) best_score1 = 2 ** 32 best_score2 = 2 ** 32 best_rect = nil [:bottom_left, :best_short_side_fit, :best_long_side_fit, :best_area_fit, :contact_point_rule].each do |method| new_rect, score1, score2 = self.send("find_position_for_#{method}", width, height, 2 ** 32, 2 ** 32) if new_rect score1 = -score1 if method == :contact_point_rule if new_rect and score1 < best_score1 or (score1 == best_score1 and score2 < best_score2) best_score1 = score1 best_score2 = score2 best_rect = new_rect end end end if best_rect @free_rects.dup.each do |free| if split_free_node(free, best_rect) @free_rects.delete(free) end end @free_rects.dup.each_with_index do |free, i| (@free_rects[(i + 1)..-1] || []).each do |free2| @free_rects.delete(free) if free.contained_in?(free2) @free_rects.delete(free2) if free2.contained_in?(free) end end best_rect.value = value @used_rects.push(best_rect) true else false end end |
#rects ⇒ Object
59 60 61 |
# File 'lib/spritz/max_rects_packer.rb', line 59 def rects @used_rects end |