Mercurial > repos > public > sbplib
comparison +anim/animate.m @ 167:15baeb35f74e feature/grids
Merge in changes from default.
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Tue, 23 Feb 2016 13:25:43 +0100 |
parents | 43f8df3595cf |
children | 2fe13db674da |
comparison
equal
deleted
inserted
replaced
166:7cb97c1988d9 | 167:15baeb35f74e |
---|---|
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 rs.appendFormat('Target time_modifier: %d\n'); | |
27 rs.appendFormat('actual time_modifier: %d\n'); | |
28 | |
29 animation_start = tic(); | |
30 prevTau = 0; | |
31 targetTau = 0; | |
32 tauFrameStart = -dTau_target; | |
33 t = F(tstart); | |
34 | |
35 while t < tend | |
36 % Sleep until the frame should start | |
37 pause(targetTau-toc(animation_start)); | |
38 tau = toc(animation_start); | |
39 dTau = tau - tauFrameStart; | |
40 | |
41 % Calculate error in tau | |
42 e_Tau = tau - targetTau; | |
43 | |
44 % Regulate time_modifier based on e_Tau | |
45 % time_modifier = min(time_modifier_bound, max(0.5, abs(1-e_Tau/dTau)) * time_modifier); | |
46 | |
47 % Mark the start of the frame | |
48 tauFrameStart = tau; | |
49 | |
50 dt_target = dTau_target*time_modifier; % Targeted simulation time between frames | |
51 | |
52 t_prev = t; | |
53 t = F(t + dt_target); % Run simulation | |
54 | |
55 % Calculate when this frame should end and the next start. (this depends on what simulation time we ended up on) | |
56 dt = t-t_prev; | |
57 % targetTau = targetTau + dt/time_modifier; | |
58 targetTau = targetTau + dTau_target; | |
59 | |
60 % Update information about this frame | |
61 tau = toc(animation_start); | |
62 rs.updateParam(tau, targetTau, 1/dTau_target, 1/dTau, time_modifier_bound, time_modifier); | |
11 end | 63 end |
12 | 64 |
13 if ~exist('frame_rate') | |
14 frame_rate = 30; | |
15 end | |
16 | 65 |
17 frame_time = 1/frame_rate; | 66 % 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); | 67 time_to_animate = toc(animation_start); |
28 expected_time = tend/time_modifier; | 68 expected_time = tend/time_modifier; |
29 | |
30 fprintf('\n'); | 69 fprintf('\n'); |
31 fprintf('Time to animate: %.3f\n', time_to_animate) | 70 fprintf('Time to animate: %.3f\n', time_to_animate) |
32 fprintf('Expected time : %.3f\n', expected_time) | 71 fprintf('Expected time : %.3f\n', expected_time) |
33 end | 72 end |