Class: FlameChannelParser::Interpolator

Inherits:
Object
  • Object
show all
Includes:
Segments
Defined in:
lib/interpolator.rb

Overview

Used to sample Flame animation curves. Pass a Channel object to the interpolator and you can then sample values at arbitrary frames.

i = Interpolator.new(parsed_channel)
i.value_at(245.5) # => will interpolate and return the value

Constant Summary

NEG_INF =
(-1.0/0.0)
POS_INF =
(1.0/0.0)

Instance Method Summary (collapse)

Constructor Details

- (Interpolator) initialize(channel)

The constructor will accept a ChannelBlock object and convert it internally to a number of segments from which samples can be made



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/interpolator.rb', line 17

def initialize(channel)
  @segments = []
  @extrap = channel.extrapolation
  
  # Edge case - channel has no anim at all
  if channel.size.zero?
    @segments << ConstantFunction.new(channel.base_value)
  else
    
    # First the prepolating segment
    @segments << pick_prepolation(channel.extrapolation, channel[0], channel[1])
    
    # Then all the intermediate segments, one segment between each pair of keys
    channel[0..-2].each_with_index do | key, index |
      @segments << key_pair_to_segment(key, channel[index + 1])
    end
    
    # so we just output it separately
    @segments << pick_extrapolation(channel.extrapolation, channel[-2], channel[-1])
  end
end

Instance Method Details

- (Object) first_defined_frame

Returns the first frame number that is concretely defined as a keyframe after the prepolation ends



52
53
54
55
56
# File 'lib/interpolator.rb', line 52

def first_defined_frame
  first_f = @segments[0].end_frame
  return 1 if first_f == NEG_INF
  return first_f
end

- (Object) last_defined_frame

Returns the last frame number that is concretely defined as a keyframe before the extrapolation starts



60
61
62
63
64
# File 'lib/interpolator.rb', line 60

def last_defined_frame
  last_f = @segments[-1].start_frame
  return 100 if last_f == POS_INF
  return last_f
end

- (Object) sample_at(frame)

Sample the value of the animation curve at this frame



40
41
42
43
44
45
46
47
48
# File 'lib/interpolator.rb', line 40

def sample_at(frame)
  if :cycle == @extrap
    return sample_from_segments(frame_number_in_cycle(frame))
  elsif :revcycle == @extrap
    return sample_from_segments(frame_number_in_revcycle(frame))
  else
    sample_from_segments(frame)
  end
end