class Tracksperanto::Tracker

Internal representation of a tracker point with keyframes. A Tracker is an array of Keyframe objects with a few methods added for convenience

Attributes

name[RW]

Contains the name of the tracker

Public Class Methods

new(object_attribute_hash = {}) click to toggle source
Calls superclass method
# File lib/tracksperanto/tracker.rb, line 13
def initialize(object_attribute_hash = {})
  @name = "Tracker"
  @frame_table = {}
  super
end

Public Instance Methods

<=>(other_tracker) click to toggle source

Trackers sort by the position of the first keyframe

# File lib/tracksperanto/tracker.rb, line 50
def <=>(other_tracker)
  self.first_frame <=> other_tracker.first_frame
end
[](offset) click to toggle source

Fetch a keyframe at a spefiic offset. NOTICE: not at a specific frame but at an offset in the frames table. The frames table is ordered by frame order. If you need to fetch a keyframe at a specific frame, use at_frame

# File lib/tracksperanto/tracker.rb, line 81
def [](offset)
  frame = ordered_frame_numbers[offset]
  return nil if frame.nil?
  
  extract_keyframe(frame)
end
at_frame(at_frame) click to toggle source

Fetch a keyframe at a specific frame. If no such frame exists nil will be returned

# File lib/tracksperanto/tracker.rb, line 89
def at_frame(at_frame)
  extract_keyframe(at_frame)
end
clear() click to toggle source

Removes all the keyframes in the tracker

# File lib/tracksperanto/tracker.rb, line 120
def clear
  @frame_table = {}
end
each() { |extract_keyframe(frame)| ... } click to toggle source

Iterates over keyframes

# File lib/tracksperanto/tracker.rb, line 43
def each
  ordered_frame_numbers.each do | frame |
    yield(extract_keyframe(frame))
  end
end
empty?() click to toggle source

Tells whether this tracker is empty or not

# File lib/tracksperanto/tracker.rb, line 73
def empty?
  @frame_table.empty?
end
first_frame() click to toggle source

Returns the first frame number this tracker contains (where the first keyframe is)

# File lib/tracksperanto/tracker.rb, line 55
def first_frame
  ordered_frame_numbers[0]
end
inspect() click to toggle source
# File lib/tracksperanto/tracker.rb, line 99
def inspect
  "<T #{name.inspect} with #{length} keyframes>"
end
keyframe!(options) click to toggle source

Create and save a keyframe in this tracker. The options hash is the same as the one for the Keyframe constructor

# File lib/tracksperanto/tracker.rb, line 67
def keyframe!(options)
  kf = Tracksperanto::Keyframe.new(options)
  set(kf)
end
keyframes() click to toggle source

Returns an array of keyframes, ordered by their frame value. WARNING: in older Tracksperanto versions the returned value was a handle into the tracker object. Now it returns a copy of the tracker's keyframes and modifications done to the array WILL NOT propagate to the tracker object itself. If you need to replace a keyframe, use set(keyframe). If you need to replace the whole keyframes array, use keyframes=(new_keyframes)

# File lib/tracksperanto/tracker.rb, line 33
def keyframes
  to_a
end
keyframes=(new_kf_array) click to toggle source

Replace all the keyframes of the tracker with new ones

# File lib/tracksperanto/tracker.rb, line 20
def keyframes=(new_kf_array)
  @frame_table = {}
  new_kf_array.each do | keyframe |
    @frame_table[keyframe.frame] = keyframe.abs_x, keyframe.abs_y, keyframe.residual
  end
end
length() click to toggle source

Tells how many keyframes this tracker contains

# File lib/tracksperanto/tracker.rb, line 115
def length
  @frame_table.length
end
name=(n) click to toggle source

Automatically truncate spaces in the tracker name and replace them with underscores

# File lib/tracksperanto/tracker.rb, line 61
def name=(n)
  @name = n.to_s.gsub(/(\s+)/, '_')
end
push(kf) click to toggle source

Add a keyframe. Will raise a Dupe exception if the keyframe to be set will overwrite another one

# File lib/tracksperanto/tracker.rb, line 94
def push(kf)
  raise Dupe, "The tracker #{name.inspect} already contains a keyframe at #{kf.frame}" if @frame_table[kf.frame]
  set(kf)
end
set(kf) click to toggle source

Sets a keyframe. If an old keyframe exists at this frame offset it will be replaced.

# File lib/tracksperanto/tracker.rb, line 38
def set(kf)
  @frame_table[kf.frame] = [kf.abs_x, kf.abs_y, kf.residual]
end
to_ruby() click to toggle source

Used in tests

# File lib/tracksperanto/tracker.rb, line 104
def to_ruby
  buf = []
  buf.push("Tracksperanto::Tracker.new(:name => %s) do |t|" % name.inspect)
  each do | kf |
    buf.push("  t.keyframe!(:frame => %d, :abs_x => %0.05f, :abs_y => %0.05f, :residual => %0.05f)" % [kf.frame, kf.abs_x, kf.abs_y, kf.residual])
  end
  buf.push("end")
  buf.join("\n")
end

Private Instance Methods

extract_keyframe(frame) click to toggle source
# File lib/tracksperanto/tracker.rb, line 130
def extract_keyframe(frame)
  triplet = @frame_table[frame]
  return nil unless triplet
  
  Tracksperanto::Keyframe.new(:frame => frame, :abs_x => triplet[0], :abs_y => triplet[1], :residual => triplet[2])
end
ordered_frame_numbers() click to toggle source
# File lib/tracksperanto/tracker.rb, line 126
def ordered_frame_numbers
  @frame_table.keys.sort
end