Class: Sqed::Boundaries
- Inherits:
-
Object
- Object
- Sqed::Boundaries
- Includes:
- Enumerable
- Defined in:
- lib/sqed/boundaries.rb
Overview
An Sqed::Boundaries is a simple wrapper for a hash that contains the co-ordinates for each section of a layout.
Layouts are Hashes defined in EXTRACTION_PATTERNS[<layout>]
Instance Attribute Summary collapse
-
#complete ⇒ Boolean
executed to completion.
-
#coordinates ⇒ Object
readonly
stores a hash References the section by integer index! In the pattern integer => [x1,y1, width, height] (ImageMagick convention rectangle descriptors) e.g.
-
#layout ⇒ Object
A symbol from Sqed::Config::LAYOUTS.keys :right_t.
Instance Method Summary collapse
-
#count ⇒ Object
Overrides Enumerable.
- #each(&block) ⇒ Object
- #for(section) ⇒ Object
- #height_for(index) ⇒ Object
-
#initialize(layout = nil) ⇒ Boundaries
constructor
A new instance of Boundaries.
- #initialize_coordinates ⇒ Object
-
#offset(boundary) ⇒ Sqed::Boundaries instance
The idea here is to create a deep copy of self, offsetting by boundary as we go.
- #populated? ⇒ Boolean
- #set(index, coordinates) ⇒ Object
- #width_for(index) ⇒ Object
- #x_for(index) ⇒ Object
- #y_for(index) ⇒ Object
- #zoom(width_factor, height_factor) ⇒ Object
Constructor Details
#initialize(layout = nil) ⇒ Boundaries
Returns a new instance of Boundaries.
26 27 28 29 30 31 32 33 |
# File 'lib/sqed/boundaries.rb', line 26 def initialize(layout = nil) raise Sqed::Error, 'unrecognized layout' if layout && !SqedConfig::LAYOUTS.include?(layout) @complete = false @layout = layout @coordinates = {} initialize_coordinates if !@layout.nil? end |
Instance Attribute Details
#complete ⇒ Boolean
executed to completion
24 25 26 |
# File 'lib/sqed/boundaries.rb', line 24 def complete @complete end |
#coordinates ⇒ Object (readonly)
stores a hash References the section by integer index! In the pattern integer => [x1,y1, width, height] (ImageMagick convention rectangle descriptors) e.g.
0 => [10,10,40,40]
16 17 18 |
# File 'lib/sqed/boundaries.rb', line 16 def coordinates @coordinates end |
#layout ⇒ Object
A symbol from Sqed::Config::LAYOUTS.keys
:right_t
20 21 22 |
# File 'lib/sqed/boundaries.rb', line 20 def layout @layout end |
Instance Method Details
#count ⇒ Object
Overrides Enumerable
68 69 70 |
# File 'lib/sqed/boundaries.rb', line 68 def count @coordinates.length end |
#each(&block) ⇒ Object
61 62 63 64 65 |
# File 'lib/sqed/boundaries.rb', line 61 def each(&block) @coordinates.each do |section_index, coords| block.call([section_index, coords]) end end |
#for(section) ⇒ Object
57 58 59 |
# File 'lib/sqed/boundaries.rb', line 57 def for(section) @coordinates[section] end |
#height_for(index) ⇒ Object
84 85 86 |
# File 'lib/sqed/boundaries.rb', line 84 def height_for(index) @coordinates[index][3] end |
#initialize_coordinates ⇒ Object
35 36 37 38 39 |
# File 'lib/sqed/boundaries.rb', line 35 def initialize_coordinates SqedConfig::LAYOUTS[@layout].each do |k| @coordinates.merge!(k => [nil, nil, nil, nil] ) end end |
#offset(boundary) ⇒ Sqed::Boundaries instance
Returns the idea here is to create a deep copy of self, offsetting by boundary as we go.
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/sqed/boundaries.rb', line 44 def offset(boundary) b = Sqed::Boundaries.new (0..coordinates.length - 1).each do |i| b.set(i, [(x_for(i) + boundary.x_for(0)), (y_for(i) + boundary.y_for(0)), width_for(i), height_for(i)]) end b.complete = complete b end |
#populated? ⇒ Boolean
92 93 94 95 96 97 98 99 |
# File 'lib/sqed/boundaries.rb', line 92 def populated? each do |index, coords| coords.each do |c| return false if c.nil? end end true end |
#set(index, coordinates) ⇒ Object
88 89 90 |
# File 'lib/sqed/boundaries.rb', line 88 def set(index, coordinates) @coordinates[index] = coordinates end |
#width_for(index) ⇒ Object
80 81 82 |
# File 'lib/sqed/boundaries.rb', line 80 def width_for(index) @coordinates[index][2] end |
#x_for(index) ⇒ Object
72 73 74 |
# File 'lib/sqed/boundaries.rb', line 72 def x_for(index) @coordinates[index][0] end |
#y_for(index) ⇒ Object
76 77 78 |
# File 'lib/sqed/boundaries.rb', line 76 def y_for(index) @coordinates[index][1] end |
#zoom(width_factor, height_factor) ⇒ Object
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/sqed/boundaries.rb', line 101 def zoom(width_factor, height_factor) coordinates.keys.each do |i| set(i, [ (x_for(i).to_f * width_factor).to_i, (y_for(i).to_f * height_factor).to_i, (width_for(i).to_f * width_factor).to_i, (height_for(i).to_f * height_factor).to_i ]) end end |