class Nonsensor::MidpointDisplacement

Attributes

batch[R]
batch_size[R]
start[R]

Public Class Methods

new(start: 0.0, displacement: 50.0, decay_power: 1, batch_size: 100) click to toggle source
# File lib/nonsensor/midpoint_displacement.rb, line 6
def initialize(start: 0.0, displacement: 50.0, decay_power: 1, batch_size: 100)
  @start = start
  @initial_displacement = displacement
  @decay = (1.0 / 2 ** decay_power)
  @batch_size = batch_size.to_i

  @batch = []
end

Public Instance Methods

next!() click to toggle source
# File lib/nonsensor/midpoint_displacement.rb, line 15
def next!
  make_batch if @batch.empty?
  @batch.shift
end

Private Instance Methods

displace_midpoint(from, to) click to toggle source
# File lib/nonsensor/midpoint_displacement.rb, line 44
def displace_midpoint(from, to)
  midpoint = (from + to) / 2
  @batch[midpoint] = (@batch[from] + @batch[to]) / 2 + rand(displacement_bounds)
  midpoint
end
displacement_bounds() click to toggle source
# File lib/nonsensor/midpoint_displacement.rb, line 67
def displacement_bounds
  -@displacement..@displacement
end
enqueue_segment(from, to) click to toggle source
# File lib/nonsensor/midpoint_displacement.rb, line 50
def enqueue_segment(from, to)
  @next_iteration << [from, to] if segment_length(from, to) > 2
end
make_batch() click to toggle source
# File lib/nonsensor/midpoint_displacement.rb, line 22
def make_batch
  @batch = Array.new(@batch_size, start)
  @iteration = 1
  @current_iteration = []
  @next_iteration = []
  @displacement = @initial_displacement

  @current_iteration << [0, @batch_size - 1] if segment_length(0, @batch_size - 1) > 2

  while @current_iteration.any? do
    from, to = *(@current_iteration.shift)

    midpoint = displace_midpoint(from, to)

    enqueue_segment(from, midpoint)
    enqueue_segment(midpoint, to)
    next_iteration
  end

  @batch
end
next_iteration() click to toggle source
# File lib/nonsensor/midpoint_displacement.rb, line 58
def next_iteration
  return if @current_iteration.any?
  return if @next_iteration.empty?

  @current_iteration = @next_iteration
  @next_iteration = []
  @displacement = @displacement * @decay
end
segment_length(from, to) click to toggle source
# File lib/nonsensor/midpoint_displacement.rb, line 54
def segment_length(from, to)
  to - from + 1
end