Class: Tracksperanto::Tracker
- Inherits:
-
Object
- Object
- Tracksperanto::Tracker
- Defined in:
- lib/tracksperanto/tracker.rb
Overview
Internal representation of a tracker point with keyframes. A Tracker is an array of Keyframe objects with a few methods added for convenience
Defined Under Namespace
Classes: Dupe
Instance Attribute Summary collapse
-
#name ⇒ Object
Contains the name of the tracker.
Instance Method Summary collapse
-
#<=>(other_tracker) ⇒ Object
Trackers sort by the position of the first keyframe.
-
#[](offset) ⇒ Object
Fetch a keyframe at a spefiic offset.
-
#at_frame(at_frame) ⇒ Object
Fetch a keyframe at a specific frame.
-
#clear ⇒ Object
Removes all the keyframes in the tracker.
-
#each ⇒ Object
Iterates over keyframes.
-
#empty? ⇒ Boolean
Tells whether this tracker is empty or not.
-
#first_frame ⇒ Object
Returns the first frame number this tracker contains (where the first keyframe is).
-
#initialize(object_attribute_hash = {}) ⇒ Tracker
constructor
A new instance of Tracker.
- #inspect ⇒ Object
-
#keyframe!(options) ⇒ Object
Create and save a keyframe in this tracker.
-
#keyframes ⇒ Object
Returns an array of keyframes, ordered by their frame value.
-
#keyframes=(new_kf_array) ⇒ Object
Replace all the keyframes of the tracker with new ones.
-
#length ⇒ Object
Tells how many keyframes this tracker contains.
-
#push(kf) ⇒ Object
Add a keyframe.
-
#set(kf) ⇒ Object
Sets a keyframe.
-
#to_ruby ⇒ Object
Used in tests.
Methods included from Casts
#cast_to_bool, #cast_to_float, #cast_to_int, #cast_to_string, included
Constructor Details
#initialize(object_attribute_hash = {}) ⇒ Tracker
Returns a new instance of Tracker.
14 15 16 17 18 |
# File 'lib/tracksperanto/tracker.rb', line 14 def initialize(object_attribute_hash = {}) @name = "Tracker" @frame_table = {} super end |
Instance Attribute Details
#name ⇒ Object
Contains the name of the tracker
8 9 10 |
# File 'lib/tracksperanto/tracker.rb', line 8 def name @name end |
Instance Method Details
#<=>(other_tracker) ⇒ Object
Trackers sort by the position of the first keyframe
51 52 53 |
# File 'lib/tracksperanto/tracker.rb', line 51 def <=>(other_tracker) self.first_frame <=> other_tracker.first_frame end |
#[](offset) ⇒ Object
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
82 83 84 85 86 87 |
# File 'lib/tracksperanto/tracker.rb', line 82 def [](offset) frame = ordered_frame_numbers[offset] return nil if frame.nil? extract_keyframe(frame) end |
#at_frame(at_frame) ⇒ Object
Fetch a keyframe at a specific frame. If no such frame exists nil will be returned
90 91 92 |
# File 'lib/tracksperanto/tracker.rb', line 90 def at_frame(at_frame) extract_keyframe(at_frame) end |
#clear ⇒ Object
Removes all the keyframes in the tracker
121 122 123 |
# File 'lib/tracksperanto/tracker.rb', line 121 def clear @frame_table = {} end |
#each ⇒ Object
Iterates over keyframes
44 45 46 47 48 |
# File 'lib/tracksperanto/tracker.rb', line 44 def each ordered_frame_numbers.each do | frame | yield(extract_keyframe(frame)) end end |
#empty? ⇒ Boolean
Tells whether this tracker is empty or not
74 75 76 |
# File 'lib/tracksperanto/tracker.rb', line 74 def empty? @frame_table.empty? end |
#first_frame ⇒ Object
Returns the first frame number this tracker contains (where the first keyframe is)
56 57 58 |
# File 'lib/tracksperanto/tracker.rb', line 56 def first_frame ordered_frame_numbers[0] end |
#inspect ⇒ Object
100 101 102 |
# File 'lib/tracksperanto/tracker.rb', line 100 def inspect "<T #{name.inspect} with #{length} keyframes>" end |
#keyframe!(options) ⇒ Object
Create and save a keyframe in this tracker. The options hash is the same as the one for the Keyframe constructor
68 69 70 71 |
# File 'lib/tracksperanto/tracker.rb', line 68 def keyframe!() kf = Tracksperanto::Keyframe.new() set(kf) end |
#keyframes ⇒ Object
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)
34 35 36 |
# File 'lib/tracksperanto/tracker.rb', line 34 def keyframes to_a end |
#keyframes=(new_kf_array) ⇒ Object
Replace all the keyframes of the tracker with new ones
21 22 23 24 25 26 |
# File 'lib/tracksperanto/tracker.rb', line 21 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 ⇒ Object
Tells how many keyframes this tracker contains
116 117 118 |
# File 'lib/tracksperanto/tracker.rb', line 116 def length @frame_table.length end |
#push(kf) ⇒ Object
Add a keyframe. Will raise a Dupe exception if the keyframe to be set will overwrite another one
95 96 97 98 |
# File 'lib/tracksperanto/tracker.rb', line 95 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) ⇒ Object
Sets a keyframe. If an old keyframe exists at this frame offset it will be replaced.
39 40 41 |
# File 'lib/tracksperanto/tracker.rb', line 39 def set(kf) @frame_table[kf.frame] = [kf.abs_x, kf.abs_y, kf.residual] end |
#to_ruby ⇒ Object
Used in tests
105 106 107 108 109 110 111 112 113 |
# File 'lib/tracksperanto/tracker.rb', line 105 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 |