class Mork::Grid

@private The Grid is a set of expectations on what the response sheet should look like It knows nothing about the actual scanned image. All returned values are in the arbitrary units given in the configuration file

Public Class Methods

new(options=nil) click to toggle source

Calling Grid.new without arguments creates the default boilerplate Grid

# File lib/mork/grid.rb, line 11
def initialize(options=nil)
  @params = default_grid
  if File.exists?('layout.yml')
    @params.deeper_merge! symbolize YAML.load_file('layout.yml')
  end
  case options
  when NilClass
    # do nothing
  when Hash
    @params.deeper_merge! symbolize options
  when String
    @params.deeper_merge! symbolize YAML.load_file(options)
  else
    fail ArgumentError, "Invalid parameter in the Grid constructor: #{options.class.inspect}"
  end
end

Public Instance Methods

barcode_bits() click to toggle source
# File lib/mork/grid.rb, line 48
def barcode_bits
  @params[:barcode][:bits].to_i
end
choice_threshold() click to toggle source
# File lib/mork/grid.rb, line 60
def choice_threshold
  @params[:items][:threshold].to_f
end
default_grid() click to toggle source

@private this is the default grid! default units are millimiters

# File lib/mork/grid_const.rb, line 6
def default_grid
  {
    # size of the paper sheet
    page_size: {
      width:      210, # A4
      height:     297
    },
    # size, location, search parameters of registration marks
    reg_marks: {
      margin:      10, # from sheet edge to registration mark center
      radius:       3, # of the registration circle
      offset:       2, # distance between page edge and registraton mark search area
      crop:        20, # size of square where the regmark should be located
      dilate:       5, # set to >0 to apply a dilate IM operation
      blur:         2, # set to >0 to apply a blur IM operation
      contrast:    20  # minimum contrast between registration mark circles and the white paper
    },
    # you can place multiple elements in the header; title is the only default
    header: {
      title: {
        top:       15,
        left:      15,
        width:    160,
        height:    12,
        size:      12,
        box:    false
      }
    },
    # questions and answers
    items: {
      threshold:     0.75, # how much darker a marked cell should be compared to cal cells
      columns:       4,
      column_width: 44,
      rows:         30,
      left:         11, # distance from the top-left registration mark...
      top:          55, # ...to the center of the first choice cell
      x_spacing:     7, # between choices
      y_spacing:     7, # between rows
      cell_width:    6, # choice cell size
      cell_height:   5, # choice cell size
      max_cells:     5, # the maximum number of choices per question
      font_size:     9, # for the question number and choice letters
      number_width:  8, # width of question number text box
      number_margin: 2  # distance between right side of q num and left side of first choice cell
    },
    # unique sheet ID as a binary barcode
    barcode: {
      bits:         38,
      left:         15,
      width:         3,
      height:        3,
      spacing:       4
    },
    # student's unique id
    uid: {
      digits:        6,
      left:        150,
      top:          30,
      width:        50,
      height:       40,
      cell_width:    4,
      cell_height:   3,
      box:        true
    }
  }
end
max_choices_per_question() click to toggle source
# File lib/mork/grid.rb, line 44
def max_choices_per_question
  @params[:items][:max_cells].to_i
end
max_questions() click to toggle source
# File lib/mork/grid.rb, line 40
def max_questions
  columns * rows
end
options() click to toggle source
# File lib/mork/grid.rb, line 36
def options
  @params
end
rm_blur() click to toggle source
# File lib/mork/grid.rb, line 56
def rm_blur
  @params[:reg_marks][:blur].to_i
end
rm_dilate() click to toggle source
# File lib/mork/grid.rb, line 52
def rm_dilate
  @params[:reg_marks][:dilate].to_i
end
show(subset=nil) click to toggle source

Puts out the Grid parameters in YAML format; the entire hash is displayed if no arguments are given; you can specify what to show by passing one of: :page_size, :reg_marks, :header, :items, :barcode

# File lib/mork/grid.rb, line 31
def show(subset=nil)
  out = subset ? @params[subset] : @params
  puts out.to_yaml
end

Private Instance Methods

barcode_bit_x(i) click to toggle source

barcode =

# File lib/mork/grid.rb, line 95
def barcode_bit_x(i)
  @params[:barcode][:left] + @params[:barcode][:spacing] * i
end
barcode_height() click to toggle source
# File lib/mork/grid.rb, line 103
def barcode_height()   @params[:barcode][:height].to_f     end
barcode_width() click to toggle source
# File lib/mork/grid.rb, line 104
def barcode_width()    @params[:barcode][:width].to_f      end
barcode_y() click to toggle source

Simple parameter extraction =

# File lib/mork/grid.rb, line 102
def barcode_y()        reg_frame_height - barcode_height   end
cal_cell_x() click to toggle source
# File lib/mork/grid.rb, line 88
def cal_cell_x
  reg_frame_width - cell_spacing
end
cell_height() click to toggle source
# File lib/mork/grid.rb, line 106
def cell_height()      @params[:items][:cell_height].to_f  end
cell_spacing() click to toggle source
# File lib/mork/grid.rb, line 107
def cell_spacing()     @params[:items][:x_spacing].to_f    end
cell_width() click to toggle source
# File lib/mork/grid.rb, line 105
def cell_width()       @params[:items][:cell_width].to_f   end
cell_x(q,c) click to toggle source

cell_x(q,c)

the distance from the registration frame to the left edge of the c-th choice cell of the q-th question

# File lib/mork/grid.rb, line 80
def cell_x(q,c)
  item_x(q) + cell_spacing * c
end
cell_y(q) click to toggle source

cell_y(q)

the distance from the registration frame to the top edge of all choice cells in the q-th question

# File lib/mork/grid.rb, line 72
def cell_y(q)
  first_y + item_spacing * (q % rows) - cell_height / 2
end
column_width() click to toggle source
# File lib/mork/grid.rb, line 109
def column_width()     @params[:items][:column_width].to_f end
columns() click to toggle source
# File lib/mork/grid.rb, line 113
def columns()          @params[:items][:columns]           end
first_x() click to toggle source
# File lib/mork/grid.rb, line 110
def first_x()          @params[:items][:left].to_f         end
first_y() click to toggle source
# File lib/mork/grid.rb, line 111
def first_y()          @params[:items][:top].to_f          end
item_spacing() click to toggle source
# File lib/mork/grid.rb, line 108
def item_spacing()     @params[:items][:y_spacing].to_f    end
item_x(q) click to toggle source
# File lib/mork/grid.rb, line 84
def item_x(q)
  first_x + column_width * (q / rows) - cell_width / 2
end
page_height() click to toggle source
# File lib/mork/grid.rb, line 121
def page_height()      @params[:page_size][:height].to_f   end
page_width() click to toggle source
# File lib/mork/grid.rb, line 120
def page_width()       @params[:page_size][:width].to_f    end
reg_crop() click to toggle source
# File lib/mork/grid.rb, line 115
def reg_crop()         @params[:reg_marks][:crop].to_f     end
reg_frame_height() click to toggle source
# File lib/mork/grid.rb, line 118
def reg_frame_height() page_height - reg_margin * 2        end
reg_frame_width() click to toggle source
# File lib/mork/grid.rb, line 117
def reg_frame_width()  page_width  - reg_margin * 2        end
reg_margin() click to toggle source
# File lib/mork/grid.rb, line 122
def reg_margin()       @params[:reg_marks][:margin].to_f   end
reg_min_contrast() click to toggle source
# File lib/mork/grid.rb, line 119
def reg_min_contrast() @params[:reg_marks][:contrast]      end
reg_off() click to toggle source
# File lib/mork/grid.rb, line 116
def reg_off()          @params[:reg_marks][:offset].to_f   end
reg_radius() click to toggle source
# File lib/mork/grid.rb, line 123
def reg_radius()       @params[:reg_marks][:radius].to_f   end
rows() click to toggle source
# File lib/mork/grid.rb, line 112
def rows()             @params[:items][:rows]              end
uid_cell_height() click to toggle source
# File lib/mork/grid.rb, line 130
def uid_cell_height()  @params[:uid][:cell_height].to_f    end
uid_cell_width() click to toggle source
# File lib/mork/grid.rb, line 129
def uid_cell_width()   @params[:uid][:cell_width].to_f     end
uid_digits() click to toggle source
# File lib/mork/grid.rb, line 124
def uid_digits()       @params[:uid][:digits].to_i         end
uid_height() click to toggle source
# File lib/mork/grid.rb, line 128
def uid_height()       @params[:uid][:height].to_f         end
uid_width() click to toggle source
# File lib/mork/grid.rb, line 127
def uid_width()        @params[:uid][:width].to_f          end
uid_x() click to toggle source
# File lib/mork/grid.rb, line 125
def uid_x()            @params[:uid][:left].to_f           end
uid_y() click to toggle source
# File lib/mork/grid.rb, line 126
def uid_y()            @params[:uid][:top].to_f            end