Class: Moon::Sampler2

Inherits:
Object show all
Defined in:
lib/moon/packages/data_painter/sampler2.rb

Overview

Sampler2 is a class used for sampling data from a Tabular object. Samplers do not modify the underlying data.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src, options = {}) ⇒ Sampler2

Returns a new instance of Sampler2.

Parameters:


15
16
17
18
# File 'lib/moon/packages/data_painter/sampler2.rb', line 15

def initialize(src, options = {})
  @src = src
  @factory2 = options.fetch(:factory2) { Table }
end

Instance Attribute Details

#factory2Object

Returns object used for creating new Table objects.

Returns:

  • (Object)

    object used for creating new Table objects.


10
11
12
# File 'lib/moon/packages/data_painter/sampler2.rb', line 10

def factory2
  @factory2
end

#srcObject (readonly)

Returns the value of attribute src


7
8
9
# File 'lib/moon/packages/data_painter/sampler2.rb', line 7

def src
  @src
end

Instance Method Details

#[](x, y) ⇒ Integer

Gets data at the given position.

Parameters:

Returns:


25
26
27
# File 'lib/moon/packages/data_painter/sampler2.rb', line 25

def [](x, y)
  src[x, y]
end

#column(x) ⇒ Array<Integer>

Samples all the data in column x

Parameters:

Returns:


55
56
57
# File 'lib/moon/packages/data_painter/sampler2.rb', line 55

def column(x)
  src.ysize.times.map { |y| src[x, y] }
end

#rotate(angle) ⇒ Object

Rotate the src data, returns a new object with the rotated data

Parameters:

Returns:


107
108
109
110
111
112
113
114
115
116
# File 'lib/moon/packages/data_painter/sampler2.rb', line 107

def rotate(angle)
  case angle % 360
  when 0   then dup
  when 90  then rotate_cw
  when 180 then rotate_flip
  when 270 then rotate_ccw
  else
    raise RuntimeError, "unsupported rotation angle #{angle}"
  end
end

#row(y) ⇒ Array<Integer>

Samples all data in row y

Parameters:

Returns:


63
64
65
# File 'lib/moon/packages/data_painter/sampler2.rb', line 63

def row(y)
  src.xsize.times.map { |x| src[x, y] }
end

#subsample(rect) ⇒ Tabular #subsample(x, y, w, h) ⇒ Tabular

Takes a section of the src and creates a new object from it.

Overloads:

Returns:


39
40
41
42
43
44
45
46
47
48
49
# File 'lib/moon/packages/data_painter/sampler2.rb', line 39

def subsample(*args)
  rx, ry, rw, rh = *Rect.extract(args.singularize)
  result = factory2.new(rw, rh, default: src.default)
  result.ysize.times do |y|
    dy = y + ry
    result.xsize.times do |x|
      result[x, y] = src[x + rx, dy]
    end
  end
  result
end