Mercurial > repos > public > sbplib
comparison +anim/animate.m @ 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 | 4cd77c7bdcaf |
children | 43f8df3595cf |
comparison
equal
deleted
inserted
replaced
131:26e047194383 | 132:6ec2248b83c4 |
---|---|
3 % F takes the time to generate the frame for and returns the actual time for the generated frame. | 3 % F takes the time to generate the frame for and returns the actual time for the generated frame. |
4 % t = F(t_r) is a function that paints a frame for time t. t is the closest time <=t_r | 4 % t = F(t_r) is a function that paints a frame for time t. t is the closest time <=t_r |
5 % it will be called for increasnig t. | 5 % it will be called for increasnig t. |
6 | 6 |
7 %Todo: make it catch up and produce warnings if it lags behind? Instead of just requesting the next target time | 7 %Todo: make it catch up and produce warnings if it lags behind? Instead of just requesting the next target time |
8 function animate(F, tstart, tend, time_modifier , frame_rate) | 8 |
9 if ~exist('time_modifier') | 9 |
10 time_modifier = 1; | 10 % If adapt is true time_modifier is treated as an upper bound |
11 function animate(F, tstart, tend, time_modifier, target_frame_rate) | |
12 default_arg('time_modifier', 1); | |
13 default_arg('target_frame_rate',30); | |
14 | |
15 % t is simulation time | |
16 % tau is real time | |
17 | |
18 time_modifier_bound = time_modifier; | |
19 dTau_target = 1/target_frame_rate; % Real time between frames | |
20 | |
21 rs = util.ReplaceableString(); | |
22 rs.appendFormat(' tau: %d\n'); | |
23 rs.appendFormat(' target tau: %d\n'); | |
24 rs.appendFormat(' Target fps: %.2f\n'); | |
25 rs.appendFormat(' Actual fps: %.2f\n'); | |
26 | |
27 frameId = 0; | |
28 NframeAvg = 20; | |
29 frameTimes = ones(1,NframeAvg); | |
30 | |
31 animation_start = tic(); | |
32 prevTau = 0; | |
33 targetTau = 0; | |
34 t = F(tstart); | |
35 | |
36 while t < tend | |
37 % Sleep until the frame should start | |
38 pause(targetTau-toc(animation_start)); | |
39 tauFrameStart = targetTau; | |
40 | |
41 dt_target = dTau_target*time_modifier; % Targeted simulation time between frames | |
42 | |
43 t_prev = t; | |
44 t = F(t + dt_target); % Run simulation | |
45 | |
46 | |
47 % Calculate when this frame should end and the next start. (this depends on what simulation time we ended up on) | |
48 dt = t-t_prev; | |
49 targetTau = targetTau + dt/time_modifier; | |
50 | |
51 % Update information about this frame | |
52 tau = toc(animation_start); | |
53 rs.updateParam(tau, targetTau, 1/dTau_target, 1/(targetTau-tauFrameStart)); | |
11 end | 54 end |
12 | 55 |
13 if ~exist('frame_rate') | |
14 frame_rate = 30; | |
15 end | |
16 | 56 |
17 frame_time = 1/frame_rate; | 57 % Final time reporting |
18 dt = frame_time*time_modifier; | |
19 | |
20 animation_start = tic(); | |
21 t = F(tstart); | |
22 while t < tend | |
23 t = F(t + dt); | |
24 t_left = (t-tstart)/time_modifier-toc(animation_start); | |
25 pause(t_left) | |
26 end | |
27 time_to_animate = toc(animation_start); | 58 time_to_animate = toc(animation_start); |
28 expected_time = tend/time_modifier; | 59 expected_time = tend/time_modifier; |
29 | |
30 fprintf('\n'); | 60 fprintf('\n'); |
31 fprintf('Time to animate: %.3f\n', time_to_animate) | 61 fprintf('Time to animate: %.3f\n', time_to_animate) |
32 fprintf('Expected time : %.3f\n', expected_time) | 62 fprintf('Expected time : %.3f\n', expected_time) |
33 end | 63 end |