Class: Moon::Sampler3

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

Overview

A Sampler3 is meant for wrapping 2d data objects and producing new 3d data objects. Samplers do not modify the underlying data.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Sampler3.

Parameters:


19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/moon/packages/data_painter/sampler3.rb', line 19

def initialize(src, options = {})
  # Sources are expected to support:
  #   [](x, y, z)
  #   []=(x, y, z, value)
  #   xsize
  #   ysize
  #   zsize
  @src = src
  # A factory is used to produce objects of the same type as the src
  # wrapped by the Sampler, the Factory uses a common API, and knows
  # how to create the object from the parameters
  @factory2 = options.fetch(:factory2) { Table }
  @factory3 = options.fetch(:factory3) { DataMatrix }
end

Instance Attribute Details

#factory2Object

Returns:


11
12
13
# File 'lib/moon/packages/data_painter/sampler3.rb', line 11

def factory2
  @factory2
end

#factory3Object

Returns:


14
15
16
# File 'lib/moon/packages/data_painter/sampler3.rb', line 14

def factory3
  @factory3
end

#srcObject (readonly)

Returns the value of attribute src


8
9
10
# File 'lib/moon/packages/data_painter/sampler3.rb', line 8

def src
  @src
end

Instance Method Details

#[](x, y, z) ⇒ Integer

Gets data at provided position.

Parameters:

Returns:


40
41
42
# File 'lib/moon/packages/data_painter/sampler3.rb', line 40

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

#column(x, z) ⇒ Enumerator

Parameters:

Returns:

  • (Enumerator)

86
87
88
89
# File 'lib/moon/packages/data_painter/sampler3.rb', line 86

def column(x, z)
  return to_enum(:column, x, z) unless block_given?
  src.ysize.times.each { |y| yield src[x, y, z] }
end

#layer(z) ⇒ *Data2

Returns:

  • (*Data2)

63
64
65
66
67
68
69
70
71
# File 'lib/moon/packages/data_painter/sampler3.rb', line 63

def layer(z)
  table = factory2.new(src.xsize, src.ysize, default: src.default)
  table.ysize.times do |y|
    table.xsize.times do |x|
      table[x, y] = src[x, y, z]
    end
  end
  table
end

#pillar(x, y) ⇒ Enumerator

Parameters:

Returns:

  • (Enumerator)

77
78
79
80
# File 'lib/moon/packages/data_painter/sampler3.rb', line 77

def pillar(x, y)
  return to_enum(:pillar, x, y) unless block_given?
  src.zsize.times.each { |z| yield src[x, y, z] }
end

#row(y, z) ⇒ Enumerator

Parameters:

Returns:

  • (Enumerator)

95
96
97
98
# File 'lib/moon/packages/data_painter/sampler3.rb', line 95

def row(y, z)
  return to_enum(:row, y, z) unless block_given?
  src.xsize.times.each { |x| yield src[x, y, z] }
end

#subsample(*args) ⇒ *Data3

Returns:

  • (*Data3)

46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/moon/packages/data_painter/sampler3.rb', line 46

def subsample(*args)
  cx, cy, cz, cw, ch, cd = *Cuboid.extract(args.singularize)
  result = factory3.new(cw, ch, cd, default: src.default)
  result.zsize.times do |z|
    dz = cz + z
    result.ysize.times do |y|
      dy = cy + y
      result.xsize.times do |x|
        result[x, y, z] = src[x + cx, dy, dz]
      end
    end
  end
  result
end