Class: Tracksperanto::Tracker

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable, BlockInit, Casts
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

Instance Method Summary collapse

Methods included from Casts

#cast_to_bool, #cast_to_float, #cast_to_int, #cast_to_string

Constructor Details

#initialize(object_attribute_hash = {}) ⇒ Tracker

Returns a new instance of Tracker



13
14
15
16
17
# File 'lib/tracksperanto/tracker.rb', line 13

def initialize(object_attribute_hash = {})
  @name = "Tracker"
  @frame_table = {}
  super
end

Instance Attribute Details

#nameObject

Contains the name of the tracker



7
8
9
# File 'lib/tracksperanto/tracker.rb', line 7

def name
  @name
end

Instance Method Details

#<=>(other_tracker) ⇒ Object

Trackers sort by the position of the first keyframe



50
51
52
# File 'lib/tracksperanto/tracker.rb', line 50

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



81
82
83
84
85
86
# 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) ⇒ Object

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



89
90
91
# File 'lib/tracksperanto/tracker.rb', line 89

def at_frame(at_frame)
  extract_keyframe(at_frame)
end

#clearObject

Removes all the keyframes in the tracker



120
121
122
# File 'lib/tracksperanto/tracker.rb', line 120

def clear
  @frame_table = {}
end

#eachObject

Iterates over keyframes



43
44
45
46
47
# File 'lib/tracksperanto/tracker.rb', line 43

def each
  ordered_frame_numbers.each do | frame |
    yield(extract_keyframe(frame))
  end
end

#empty?Boolean

Tells whether this tracker is empty or not

Returns:

  • (Boolean)


73
74
75
# File 'lib/tracksperanto/tracker.rb', line 73

def empty?
  @frame_table.empty?
end

#first_frameObject

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



55
56
57
# File 'lib/tracksperanto/tracker.rb', line 55

def first_frame
  ordered_frame_numbers[0]
end

#inspectObject



99
100
101
# File 'lib/tracksperanto/tracker.rb', line 99

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



67
68
69
70
# File 'lib/tracksperanto/tracker.rb', line 67

def keyframe!(options)
  kf = Tracksperanto::Keyframe.new(options)
  set(kf)
end

#keyframesObject

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)



33
34
35
# File 'lib/tracksperanto/tracker.rb', line 33

def keyframes
  to_a
end

#keyframes=(new_kf_array) ⇒ Object

Replace all the keyframes of the tracker with new ones



20
21
22
23
24
25
# 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

#lengthObject

Tells how many keyframes this tracker contains



115
116
117
# File 'lib/tracksperanto/tracker.rb', line 115

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

Raises:



94
95
96
97
# 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) ⇒ Object

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



38
39
40
# File 'lib/tracksperanto/tracker.rb', line 38

def set(kf)
  @frame_table[kf.frame] = [kf.abs_x, kf.abs_y, kf.residual]
end

#to_rubyObject

Used in tests



104
105
106
107
108
109
110
111
112
# 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