lib/pipeline/base.rb in tracksperanto-1.0.2 vs lib/pipeline/base.rb in tracksperanto-1.0.4

- old
+ new

@@ -1,10 +1,11 @@ # This is currently a bit of a mess. module Tracksperanto::Pipeline class Base attr_accessor :converted_points attr_accessor :converted_keyframes + attr_accessor :progress_block def run(from_input_file_path, pix_w, pix_h, parser_class) # Read the input file read_data = File.read(from_input_file_path) @@ -20,29 +21,46 @@ golden = Tracksperanto::Middleware::Golden.new(slipper) # Yield middlewares to the block yield(scaler, slipper, golden) if block_given? - # Run the export - @converted_points, @converted_keyframes = run_export(read_data, parser, golden) - + @converted_points, @converted_keyframes = run_export(read_data, parser, golden) do | p, m | + @progress_block.call(p, m) if @progress_block + end end - # Runs the export and returns the number of points and keyframes processed + # Runs the export and returns the number of points and keyframes processed. + # If a block is passed, the block will receive the percent complete and the last + # status message that you can pass back to the UI + # # :yields: percent_complete, status_message def run_export(tracker_data_blob, parser, processor) - points, keyframes = 0, 0 + points, keyframes, percent_complete = 0, 0, 0.0 + yield(percent_complete, "Starting the parse routine") if block_given? trackers = parser.parse(tracker_data_blob) + + yield(percent_complete = 20.0, "Starting export for #{trackers.length} trackers") if block_given? + + percent_per_tracker = 80.0 / trackers.length + processor.start_export(parser.width, parser.height) + + yield(percent_complete, "Starting export") if block_given? + trackers.each do | t | + kf_weight = percent_per_tracker / t.keyframes.length points += 1 processor.start_tracker_segment(t.name) t.keyframes.each do | kf | keyframes += 1 processor.export_point(kf.frame, kf.abs_x, kf.abs_y, kf.residual) + yield(percent_complete += kf_weight, "Writing keyframe") if block_given? end end processor.end_export + + yield(100.0, "Wrote #{points} points and #{keyframes} keyframes") if block_given? + [points, keyframes] end def create_parser(parser_class, w, h) p = parser_class.new \ No newline at end of file