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
Contains the name of the tracker
Public Class Methods
# File lib/tracksperanto/tracker.rb, line 13 def initialize(object_attribute_hash = {}) @name = "Tracker" @frame_table = {} super end
Public Instance Methods
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
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
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
Removes all the keyframes in the tracker
# File lib/tracksperanto/tracker.rb, line 120 def clear @frame_table = {} end
Iterates over keyframes
# File lib/tracksperanto/tracker.rb, line 43 def each ordered_frame_numbers.each do | frame | yield(extract_keyframe(frame)) end end
Tells whether this tracker is empty or not
# File lib/tracksperanto/tracker.rb, line 73 def empty? @frame_table.empty? end
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
# File lib/tracksperanto/tracker.rb, line 99 def inspect "<T #{name.inspect} with #{length} keyframes>" end
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
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
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
Tells how many keyframes this tracker contains
# File lib/tracksperanto/tracker.rb, line 115 def length @frame_table.length end
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
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
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
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
# 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
# File lib/tracksperanto/tracker.rb, line 126 def ordered_frame_numbers @frame_table.keys.sort end