changeset 132:6ec2248b83c4

Refactored +anim.animate to be more clear.
author Jonatan Werpers <jonatan@werpers.com>
date Fri, 05 Feb 2016 10:49:16 +0100
parents 26e047194383
children 43f8df3595cf
files +anim/animate.m +noname/animate.m
diffstat 2 files changed, 46 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
diff -r 26e047194383 -r 6ec2248b83c4 +anim/animate.m
--- a/+anim/animate.m	Fri Feb 05 09:43:34 2016 +0100
+++ b/+anim/animate.m	Fri Feb 05 10:49:16 2016 +0100
@@ -5,28 +5,58 @@
 % it will be called for increasnig t.
 
 %Todo: make it catch up and produce warnings if it lags behind? Instead of just requesting the next target time
-function  animate(F, tstart, tend, time_modifier , frame_rate)
-    if ~exist('time_modifier')
-        time_modifier = 1;
-    end
+
+
+% If adapt is true time_modifier is treated as an upper bound
+function animate(F, tstart, tend, time_modifier, target_frame_rate)
+    default_arg('time_modifier', 1);
+    default_arg('target_frame_rate',30);
+
+    % t is simulation time
+    % tau is real time
+
+    time_modifier_bound = time_modifier;
+    dTau_target = 1/target_frame_rate; % Real time between frames
+
+    rs = util.ReplaceableString();
+    rs.appendFormat('              tau: %d\n');
+    rs.appendFormat('       target tau: %d\n');
+    rs.appendFormat('       Target fps: %.2f\n');
+    rs.appendFormat('       Actual fps: %.2f\n');
+
+    frameId = 0;
+    NframeAvg = 20;
+    frameTimes = ones(1,NframeAvg);
 
-    if ~exist('frame_rate')
-        frame_rate = 30;
+    animation_start = tic();
+    prevTau = 0;
+    targetTau = 0;
+    t = F(tstart);
+
+    while t < tend
+        % Sleep until the frame should start
+        pause(targetTau-toc(animation_start));
+        tauFrameStart = targetTau;
+
+        dt_target = dTau_target*time_modifier; % Targeted simulation time between frames
+
+        t_prev = t;
+        t = F(t + dt_target); % Run simulation
+
+
+        % Calculate when this frame should end and the next start. (this depends on what simulation time we ended up on)
+        dt = t-t_prev;
+        targetTau = targetTau + dt/time_modifier;
+
+        % Update information about this frame
+        tau = toc(animation_start);
+        rs.updateParam(tau, targetTau, 1/dTau_target, 1/(targetTau-tauFrameStart));
     end
 
-    frame_time = 1/frame_rate;
-    dt = frame_time*time_modifier;
 
-    animation_start = tic();
-    t = F(tstart);
-    while t < tend
-        t = F(t + dt);
-        t_left = (t-tstart)/time_modifier-toc(animation_start);
-        pause(t_left)
-    end
+    % Final time reporting
     time_to_animate = toc(animation_start);
     expected_time = tend/time_modifier;
-
     fprintf('\n');
     fprintf('Time to animate: %.3f\n', time_to_animate)
     fprintf('Expected time  : %.3f\n', expected_time)
diff -r 26e047194383 -r 6ec2248b83c4 +noname/animate.m
--- a/+noname/animate.m	Fri Feb 05 09:43:34 2016 +0100
+++ b/+noname/animate.m	Fri Feb 05 10:49:16 2016 +0100
@@ -67,8 +67,6 @@
         if makemovies
             save_frame();
         end
-        % pause(0.1)
-        str = util.replace_string(str,'t = %.5f',ts.t);
 
         if do_pause
             pause