Class: Moon::DataMatrix

Inherits:
Object show all
Includes:
MatrixLike, NData, Serializable, Serializable::Properties
Defined in:
lib/moon/packages/data_bags/data_matrix.rb,
lib/moon/packages/data_painter/ext/data_matrix.rb

Overview

aka. Table3

Defined Under Namespace

Classes: Iterator

Properties collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NData

#write_data

Constructor Details

#initialize(xsize, ysize, zsize, options = {}) {|_self| ... } ⇒ DataMatrix

Returns a new instance of DataMatrix.

Parameters:

Yields:

  • (_self)

Yield Parameters:


61
62
63
64
65
66
67
68
# File 'lib/moon/packages/data_bags/data_matrix.rb', line 61

def initialize(xsize, ysize, zsize, options = {})
  @xsize = xsize.to_i
  @ysize = ysize.to_i
  @zsize = zsize.to_i
  @default = options.fetch(:default, 0)
  create_data
  yield self if block_given?
end

Instance Attribute Details

#dataArray<Integer>

Returns underlaying Array of data.

Returns:


52
# File 'lib/moon/packages/data_bags/data_matrix.rb', line 52

property_accessor :data

#defaultInteger

Returns:


55
# File 'lib/moon/packages/data_bags/data_matrix.rb', line 55

property_accessor :default

#sizeInteger

Returns xsize * ysize * zsize.

Returns:

  • (Integer)

    xsize * ysize * zsize


48
# File 'lib/moon/packages/data_bags/data_matrix.rb', line 48

property_accessor :size

#xsizeInteger

Returns number of columns in the matrix.

Returns:

  • (Integer)

    number of columns in the matrix


39
# File 'lib/moon/packages/data_bags/data_matrix.rb', line 39

property_accessor :xsize

#ysizeInteger

Returns number of rows in the matrix.

Returns:

  • (Integer)

    number of rows in the matrix


42
# File 'lib/moon/packages/data_bags/data_matrix.rb', line 42

property_accessor :ysize

#zsizeInteger

Returns number of layers in the matrix.

Returns:

  • (Integer)

    number of layers in the matrix


45
# File 'lib/moon/packages/data_bags/data_matrix.rb', line 45

property_accessor :zsize

Class Method Details

.load(data, depth = 0) ⇒ Moon::DataMatrix

Parameters:

Returns:


202
203
204
205
206
207
# File 'lib/moon/packages/data_bags/data_matrix.rb', line 202

def self.load(data, depth = 0)
  instance = new data['xsize'], data['ysize'], data['zsize'],
                 default: data['default']
  instance.import data, depth
  instance
end

Instance Method Details

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

Parameters:

Returns:


150
151
152
153
154
155
156
# File 'lib/moon/packages/data_bags/data_matrix.rb', line 150

def [](x, y, z)
  x = x.to_i
  y = y.to_i
  z = z.to_i
  return @default unless contains?(x, y, z)
  @data[calc_index(x, y, z)]
end

#[]=(x, y, z, n) ⇒ Object

Parameters:


162
163
164
165
166
167
168
169
# File 'lib/moon/packages/data_bags/data_matrix.rb', line 162

def []=(x, y, z, n)
  x = x.to_i
  y = y.to_i
  z = z.to_i
  n = n.to_i
  return unless contains?(x, y, z)
  @data[calc_index(x, y, z)] = n
end

#contains?(x, y, z) ⇒ Boolean

Parameters:

Returns:


130
131
132
133
134
# File 'lib/moon/packages/data_bags/data_matrix.rb', line 130

def contains?(x, y, z)
  return ((x >= 0) && (x < @xsize)) &&
         ((y >= 0) && (y < @ysize)) &&
         ((z >= 0) && (z < @zsize))
end

#cuboidMoon::Cuboid

Returns:


122
123
124
# File 'lib/moon/packages/data_bags/data_matrix.rb', line 122

def cuboid
  Cuboid.new 0, 0, 0, xsize, ysize, zsize
end

#initialize_copy(org) ⇒ Object

Ruby's copy initializer

Parameters:


84
85
86
87
88
# File 'lib/moon/packages/data_bags/data_matrix.rb', line 84

def initialize_copy(org)
  super org
  create_data
  map_with_xyz { |_, x, y, z| org.data[x + y * @xsize + z * @xsize * @ysize] }
end

#iterInterator

Initializes and returns an Iterator

Returns:

  • (Interator)

174
175
176
# File 'lib/moon/packages/data_bags/data_matrix.rb', line 174

def iter
  @iter ||= Iterator.new(self)
end

#map_with_xyzself

Returns:

  • (self)

179
180
181
182
183
184
# File 'lib/moon/packages/data_bags/data_matrix.rb', line 179

def map_with_xyz
  iter.each_with_xyz do |n, x, y, z|
    index = x + y * @xsize + z * @xsize * @ysize
    @data[index] = yield n, x, y, z
  end
end

#rectMoon::Rect

Returns:


117
118
119
# File 'lib/moon/packages/data_bags/data_matrix.rb', line 117

def rect
  Rect.new 0, 0, xsize, ysize
end

#resize(nxsize, nysize, nzsize) ⇒ Object

Parameters:


97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/moon/packages/data_bags/data_matrix.rb', line 97

def resize(nxsize, nysize, nzsize)
  oxsize, oysize, ozsize = xsize, ysize, zsize
  @xsize, @ysize, @zsize = nxsize, nysize, nzsize
  old_data = @data
  create_data
  map_with_xyz do |n, x, y, z|
    if x < oxsize && y < oysize && z < ozsize
      old_data[x + y * oxsize + z * oxsize * oysize]
    else
      @default
    end
  end
end

#samplerSampler3

Initializes and returns a sampler

Returns:


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

def sampler
  @sampler ||= Sampler3.new(self)
end

#sizesMoon::Vector3

Returns:


112
113
114
# File 'lib/moon/packages/data_bags/data_matrix.rb', line 112

def sizes
  Vector3.new xsize, ysize, zsize
end

#to_sString

Returns:


187
188
189
190
191
192
193
194
195
196
197
# File 'lib/moon/packages/data_bags/data_matrix.rb', line 187

def to_s
  result = ''
  @zsize.times do |z|
    @ysize.times do |y|
      result.concat(@data[y * @xsize + z * @xsize * @ysize, @xsize].join(', '))
      result.concat("\n")
    end
    result.concat("\n")
  end
  result
end