smpl-core 0.9.0

Core functionality of smpl-rs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
use super::{
    expression::Expression,
    metadata::smpl_metadata,
    pose::PoseG,
    types::{AngleType, SmplType, UpAxis},
};
use crate::{codec::codec::SmplCodec, common::types::FaceType};
use burn::prelude::Backend;
use core::time::Duration;
use gloss_utils::nshare::{RefNdarray1, ToNalgebra};
use log::warn;
use nalgebra as na;
use nd::concatenate;
use ndarray as nd;
use ndarray_npy::NpzReader;
use num_derive::FromPrimitive;
use serde_json::Value;
use smpl_utils::{
    io::FileLoader,
    numerical::{euler2angleaxis, map},
};
use std::io::{Read, Seek};
/// Animation Wrap mode
#[derive(PartialEq, PartialOrd, Clone, Default, FromPrimitive)]
pub enum AnimWrap {
    Clamp,
    #[default]
    Loop,
    Reverse,
}
/// Animation config
#[derive(Clone)]
pub struct AnimationConfig {
    pub fps: f32,
    pub wrap_behaviour: AnimWrap,
    pub angle_type: AngleType,
    pub up_axis: UpAxis,
    pub smpl_type: SmplType,
    pub face_type: FaceType,
}
impl Default for AnimationConfig {
    fn default() -> Self {
        Self {
            fps: 60.0,
            wrap_behaviour: AnimWrap::Clamp,
            angle_type: AngleType::AxisAngle,
            up_axis: UpAxis::Y,
            smpl_type: SmplType::SmplX,
            face_type: FaceType::SmplX,
        }
    }
}
/// The runner for animations
#[derive(Clone)]
#[allow(clippy::struct_excessive_bools)]
pub struct AnimationRunner {
    pub anim_current_time: Duration,
    pub anim_reversed: bool,
    pub nr_repetitions: u32,
    pub paused: bool,
    pub temporary_pause: bool,
}
impl Default for AnimationRunner {
    fn default() -> Self {
        Self {
            anim_current_time: Duration::ZERO,
            anim_reversed: false,
            nr_repetitions: 0,
            paused: false,
            temporary_pause: false,
        }
    }
}
/// Animation struct for all data regarding a certain animation
#[derive(Clone)]
pub struct Animation {
    pub per_frame_joint_poses: nd::Array3<f32>,
    pub per_frame_root_trans: nd::Array2<f32>,
    pub per_frame_expression_coeffs: Option<nd::Array2<f32>>,
    pub start_offset: usize,
    pub runner: AnimationRunner,
    pub config: AnimationConfig,
}
impl Animation {
    /// # Panics
    /// Will panic if the translation and rotation do not cover the same number
    /// of timesteps
    #[allow(clippy::cast_possible_truncation)]
    pub fn new_from_matrices(
        per_frame_joint_poses: nd::Array3<f32>,
        per_frame_global_trans: nd::Array2<f32>,
        per_frame_expression_coeffs: Option<nd::Array2<f32>>,
        config: AnimationConfig,
    ) -> Self {
        assert!(
            per_frame_joint_poses.dim().0 == per_frame_global_trans.dim().0,
            "The translation and rotation should cover the same number of timesteps"
        );
        let mut per_frame_joint_poses = per_frame_joint_poses;
        let per_frame_global_trans = per_frame_global_trans;
        if config.smpl_type == SmplType::SmplPP && config.angle_type == AngleType::Euler {
            warn!("Angle type Euler is not allowed with SMPL++");
        }
        if config.smpl_type != SmplType::SmplPP && config.angle_type == AngleType::Euler {
            let animation_frames = per_frame_joint_poses.dim().0;
            let num_active_joints = per_frame_joint_poses.dim().1;
            let mut new_per_frame_joint_poses: nd::Array3<f32> = nd::Array3::<f32>::zeros((animation_frames, num_active_joints, 3));
            for (idx_timestep, poses_for_timestep) in per_frame_joint_poses.axis_iter(nd::Axis(0)).enumerate() {
                for (idx_joint, joint_pose) in poses_for_timestep.axis_iter(nd::Axis(0)).enumerate() {
                    let angle_axis = euler2angleaxis(joint_pose[0], joint_pose[1], joint_pose[2]);
                    new_per_frame_joint_poses[(idx_timestep, idx_joint, 0)] = angle_axis.x;
                    new_per_frame_joint_poses[(idx_timestep, idx_joint, 1)] = angle_axis.y;
                    new_per_frame_joint_poses[(idx_timestep, idx_joint, 2)] = angle_axis.z;
                }
            }
            per_frame_joint_poses = new_per_frame_joint_poses;
        }
        Self {
            per_frame_joint_poses,
            per_frame_root_trans: per_frame_global_trans,
            per_frame_expression_coeffs,
            start_offset: 0,
            runner: AnimationRunner::default(),
            config,
        }
    }
    #[allow(clippy::cast_possible_truncation)]
    fn new_from_npz_reader<R: Read + Seek>(npz: &mut NpzReader<R>, config: AnimationConfig) -> Self {
        let per_frame_joint_poses: nd::Array2<f64> = npz.by_name("poses.npy").unwrap();
        let animation_frames = per_frame_joint_poses.nrows();
        let num_joints_3 = per_frame_joint_poses.ncols();
        let per_frame_joint_poses = per_frame_joint_poses.mapv(|x| x as f32);
        let per_frame_joint_poses = per_frame_joint_poses.into_shape((animation_frames, num_joints_3 / 3, 3)).unwrap();
        let per_frame_global_trans: nd::Array2<f64> = npz.by_name("trans.npy").unwrap();
        let per_frame_global_trans = per_frame_global_trans.mapv(|x| x as f32);
        let per_frame_expression_coeffs: Option<nd::Array2<f64>> = npz.by_name("expressionParameters.npy").ok();
        let per_frame_expression_coeffs = per_frame_expression_coeffs.map(|x| x.mapv(|x| x as f32));
        Self::new_from_matrices(per_frame_joint_poses, per_frame_global_trans, per_frame_expression_coeffs, config)
    }
    /// # Panics
    /// Will panic if the path cannot be opened
    /// Will panic if the translation and rotation do not cover the same number
    /// of timesteps
    #[cfg(not(target_arch = "wasm32"))]
    #[allow(clippy::cast_possible_truncation)]
    pub fn new_from_npz(anim_npz_path: &str, config: AnimationConfig) -> Self {
        let mut npz =
            NpzReader::new(std::fs::File::open(anim_npz_path).unwrap_or_else(|_| panic!("Could not find/open file: {anim_npz_path}"))).unwrap();
        Self::new_from_npz_reader(&mut npz, config)
    }
    /// # Panics
    /// Will panic if the path cannot be opened
    /// Will panic if the translation and rotation do not cover the same number
    /// of timesteps
    #[allow(clippy::cast_possible_truncation)]
    pub async fn new_from_npz_async(anim_npz_path: &str, config: AnimationConfig) -> Self {
        let reader = FileLoader::open(anim_npz_path).await;
        let mut npz = NpzReader::new(reader).unwrap();
        Self::new_from_npz_reader(&mut npz, config)
    }
    /// # Panics
    /// Will panic if the path cannot be opened
    /// Will panic if the translation and rotation do not cover the same number
    /// of timesteps
    #[allow(clippy::cast_possible_truncation)]
    #[allow(clippy::identity_op)]
    pub fn new_from_json(path: &str, _anim_fps: f32, config: AnimationConfig) -> Self {
        let file = std::fs::File::open(path).unwrap();
        let reader = std::io::BufReader::new(file);
        let v: Value = serde_json::from_reader(reader).unwrap();
        let poses = &v["poses"];
        let animation_frames = poses.as_array().unwrap().len();
        let num_active_joints = poses[0].as_array().unwrap().len() / 3;
        let per_frame_global_trans: nd::Array2<f32> = nd::Array2::<f32>::zeros((animation_frames, 3));
        let poses_json_vec: Vec<f32> = poses.as_array().unwrap().iter().map(|x| x.as_f64().unwrap() as f32).collect();
        let per_frame_joint_poses = nd::Array3::from_shape_vec((animation_frames, num_active_joints, 3), poses_json_vec).unwrap();
        Self::new_from_matrices(per_frame_joint_poses, per_frame_global_trans, None, config)
    }
    /// Create an ``Animation`` component from a ``SmplCodec``
    /// # Panics
    /// Will panic if the individual body part poses in the codec don't have the
    /// correct shape to be concatenated together into a full pose for the whole
    /// body
    #[allow(clippy::cast_sign_loss)]
    pub fn new_from_smpl_codec(codec: &SmplCodec, wrap_behaviour: AnimWrap) -> Option<Self> {
        let nr_frames = codec.frame_count as usize;
        let metadata = smpl_metadata(&codec.smpl_type());
        let body_translation = codec
            .body_translation
            .as_ref()
            .unwrap_or(&ndarray::Array2::<f32>::zeros((nr_frames, 3)))
            .clone();
        let fps = codec.frame_rate?;
        if codec.smpl_type() == SmplType::SmplPP {
            let body_pose = codec
                .body_pose
                .as_ref()
                .unwrap_or(&ndarray::Array3::<f32>::zeros((nr_frames, metadata.pose_dim, 1)))
                .clone();
            let config = AnimationConfig {
                smpl_type: SmplType::SmplPP,
                wrap_behaviour,
                fps,
                ..Default::default()
            };
            Some(Self::new_from_matrices(body_pose, body_translation, None, config))
        } else {
            let body_pose = codec
                .body_pose
                .as_ref()
                .unwrap_or(&ndarray::Array3::<f32>::zeros((nr_frames, 1 + metadata.num_body_joints, 3)))
                .clone();
            let head_pose = codec
                .head_pose
                .as_ref()
                .unwrap_or(&ndarray::Array3::<f32>::zeros((nr_frames, metadata.num_face_joints, 3)))
                .clone();
            let left_hand_pose = codec
                .left_hand_pose
                .as_ref()
                .unwrap_or(&ndarray::Array3::<f32>::zeros((nr_frames, metadata.num_hand_joints, 3)))
                .clone();
            let right_hand_pose = codec
                .right_hand_pose
                .as_ref()
                .unwrap_or(&ndarray::Array3::<f32>::zeros((nr_frames, metadata.num_hand_joints, 3)))
                .clone();
            let per_frame_joint_poses = concatenate(
                nd::Axis(1),
                &[body_pose.view(), head_pose.view(), left_hand_pose.view(), right_hand_pose.view()],
            )
            .unwrap();
            let per_frame_expression_coeffs = codec.expression_parameters.clone();
            let config = AnimationConfig {
                smpl_type: codec.smpl_type(),
                wrap_behaviour,
                fps,
                ..Default::default()
            };
            Some(Self::new_from_matrices(
                per_frame_joint_poses,
                body_translation,
                per_frame_expression_coeffs,
                config,
            ))
        }
    }
    /// Create an ``Animation`` component from a ``.smpl`` file
    #[cfg(not(target_arch = "wasm32"))]
    #[allow(clippy::cast_possible_truncation)]
    pub fn new_from_smpl_file(path: &str, wrap_behaviour: AnimWrap) -> Option<Self> {
        let codec = SmplCodec::from_file(path);
        Self::new_from_smpl_codec(&codec, wrap_behaviour)
    }
    /// Create an ``Animation`` component from a ``.smpl`` buffer
    #[allow(clippy::cast_possible_truncation)]
    pub fn new_from_smpl_buf(buf: &[u8], wrap_behaviour: AnimWrap) -> Option<Self> {
        let codec = SmplCodec::from_buf(buf);
        Self::new_from_smpl_codec(&codec, wrap_behaviour)
    }
    pub fn num_active_joints(&self) -> usize {
        self.per_frame_joint_poses.dim().1
    }
    pub fn num_animation_frames(&self) -> usize {
        self.per_frame_joint_poses.dim().0
    }
    /// Advances the animation by the amount of time elapsed since last time we
    /// got the current pose
    pub fn advance(&mut self, dt_raw: Duration, first_time: bool) {
        let duration = self.duration();
        let runner = &mut self.runner;
        let config = &self.config;
        let mut dt = dt_raw;
        if first_time {
            dt = Duration::ZERO;
        }
        let will_overflow = runner.anim_current_time + dt > duration;
        let will_underflow = runner.anim_current_time < dt && runner.anim_reversed;
        if will_overflow || will_underflow {
            if will_overflow {
                match config.wrap_behaviour {
                    AnimWrap::Clamp => {
                        dt = Duration::ZERO;
                        runner.anim_current_time = duration;
                    }
                    AnimWrap::Loop => {
                        dt = Duration::from_secs_f64(dt.as_secs_f64() % duration.as_secs_f64());
                        runner.anim_current_time = Duration::ZERO;
                        runner.nr_repetitions += 1;
                    }
                    AnimWrap::Reverse => {
                        dt = Duration::from_secs_f64(dt.as_secs_f64() % duration.as_secs_f64());
                        runner.anim_current_time = duration;
                        runner.anim_reversed = !runner.anim_reversed;
                        runner.nr_repetitions += 1;
                    }
                }
            } else {
                match config.wrap_behaviour {
                    AnimWrap::Clamp => {
                        dt = Duration::ZERO;
                        runner.anim_current_time = Duration::ZERO;
                    }
                    AnimWrap::Loop => {
                        dt = Duration::from_secs_f64(dt.as_secs_f64() % duration.as_secs_f64());
                        runner.anim_current_time = duration;
                        runner.nr_repetitions += 1;
                    }
                    AnimWrap::Reverse => {
                        dt = Duration::from_secs_f64(dt.as_secs_f64() % duration.as_secs_f64());
                        runner.anim_reversed = !runner.anim_reversed;
                        runner.nr_repetitions += 1;
                    }
                }
            }
        }
        if runner.anim_reversed {
            runner.anim_current_time = runner.anim_current_time.saturating_sub(dt);
        } else {
            runner.anim_current_time = runner.anim_current_time.saturating_add(dt);
        }
    }
    pub fn has_expression(&self) -> bool {
        self.per_frame_expression_coeffs.is_some()
    }
    #[allow(clippy::cast_precision_loss)]
    #[allow(clippy::cast_possible_truncation)]
    #[allow(clippy::cast_sign_loss)]
    pub fn get_smooth_time_indices(&self) -> (usize, usize, f32) {
        let frame_time = map(
            self.runner.anim_current_time.as_secs_f32(),
            0.0,
            self.duration().as_secs_f32(),
            0.0,
            (self.num_animation_frames() - 1) as f32,
        );
        let frame_ceil = frame_time.ceil();
        let frame_ceil = frame_ceil.clamp(0.0, (self.num_animation_frames() - 1) as f32);
        let frame_floor = frame_time.floor();
        let frame_floor = frame_floor.clamp(0.0, (self.num_animation_frames() - 1) as f32);
        let w_ceil = frame_ceil - frame_time;
        let w_ceil = 1.0 - w_ceil;
        (frame_floor as usize, frame_ceil as usize, w_ceil)
    }
    /// Get the pose and translation at the current time, interpolates if
    /// necessary. Using Slerp interpolation is more accurate but also slower. Most of the time when vieweing an animation, it's enough to use `use_slerp=false`
    #[allow(clippy::cast_precision_loss)]
    #[allow(clippy::cast_possible_truncation)]
    #[allow(clippy::cast_sign_loss)]
    pub fn get_current_pose<B: Backend>(&mut self, use_slerp: bool) -> PoseG<B> {
        let (frame_floor, frame_ceil, w_ceil) = self.get_smooth_time_indices();
        let anim_frame_ceil = self.get_pose_at_idx(frame_ceil);
        let anim_frame_floor = self.get_pose_at_idx(frame_floor);
        anim_frame_floor.interpolate(&anim_frame_ceil, w_ceil, use_slerp)
    }
    /// Get pose at a certain frame ID
    #[allow(clippy::cast_precision_loss)]
    #[allow(clippy::cast_possible_truncation)]
    #[allow(clippy::cast_sign_loss)]
    pub fn get_pose_at_idx<B: Backend>(&self, idx: usize) -> PoseG<B> {
        let joint_poses = self.per_frame_joint_poses.index_axis(nd::Axis(0), idx).to_owned();
        let global_trans = self.per_frame_root_trans.index_axis(nd::Axis(0), idx).to_owned();
        PoseG::<B>::new_from_ndarray(joint_poses, global_trans, self.config.up_axis, self.config.smpl_type)
    }
    /// Get expression at current time
    pub fn get_current_expression(&mut self) -> Option<Expression> {
        let (frame_floor, frame_ceil, w_ceil) = self.get_smooth_time_indices();
        let expression_ceil = self.get_expression_at_idx(frame_ceil);
        let expresion_floor = self.get_expression_at_idx(frame_floor);
        expresion_floor.map(|expresion_floor| expresion_floor.interpolate(&expression_ceil.unwrap(), w_ceil))
    }
    /// Get expression at a given frame ID
    #[allow(clippy::cast_precision_loss)]
    #[allow(clippy::cast_possible_truncation)]
    #[allow(clippy::cast_sign_loss)]
    pub fn get_expression_at_idx(&self, idx: usize) -> Option<Expression> {
        if let Some(ref per_frame_expression_coeffs) = self.per_frame_expression_coeffs {
            let expr_coeffs = per_frame_expression_coeffs.index_axis(nd::Axis(0), idx).to_owned();
            Some(Expression::new_from_ndarray(expr_coeffs, self.config.face_type))
        } else {
            None
        }
    }
    #[must_use]
    pub fn slice_time_range(&self, start_sec: f32, end_sec: f32) -> Animation {
        let mut cur_anim = self.clone();
        cur_anim.set_cur_time_as_sec(start_sec);
        let (start_idx, _, _) = cur_anim.get_smooth_time_indices();
        cur_anim.set_cur_time_as_sec(end_sec);
        let (_, end_idx, _) = cur_anim.get_smooth_time_indices();
        let nr_frames = end_idx - start_idx + 1;
        let nr_joints = cur_anim.per_frame_joint_poses.shape()[1];
        let mut new_per_frame_joint_poses = nd::Array3::<f32>::zeros((nr_frames, nr_joints, 3));
        let mut new_per_frame_root_trans = nd::Array2::<f32>::zeros((nr_frames, 3));
        for (idx_insert_to, idx_extract_from) in (start_idx..=end_idx).enumerate() {
            let joint_poses = cur_anim.per_frame_joint_poses.index_axis(nd::Axis(0), idx_extract_from);
            let trans = cur_anim.per_frame_root_trans.index_axis(nd::Axis(0), idx_extract_from);
            new_per_frame_joint_poses.index_axis_mut(nd::Axis(0), idx_insert_to).assign(&joint_poses);
            new_per_frame_root_trans.index_axis_mut(nd::Axis(0), idx_insert_to).assign(&trans);
        }
        let _new_per_frame_expression_coeffs = if let Some(ref per_frame_expression_coeffs) = cur_anim.per_frame_expression_coeffs {
            let nr_expr_coeffs = per_frame_expression_coeffs.shape()[1];
            let mut new_per_frame_expression_coeffs = nd::Array2::<f32>::zeros((nr_frames, nr_expr_coeffs));
            for (idx_insert_to, idx_extract_from) in (start_idx..=end_idx).enumerate() {
                let expr = per_frame_expression_coeffs.index_axis(nd::Axis(0), idx_extract_from);
                new_per_frame_expression_coeffs.index_axis_mut(nd::Axis(0), idx_insert_to).assign(&expr);
            }
            Some(new_per_frame_expression_coeffs)
        } else {
            None
        };
        let new_per_frame_expression_coeffs = if let Some(ref per_frame_expression_coeffs) = cur_anim.per_frame_expression_coeffs {
            let nr_expr_coeffs = per_frame_expression_coeffs.shape()[1];
            let mut new_per_frame_expression_coeffs = nd::Array2::<f32>::zeros((nr_frames, nr_expr_coeffs));
            for (idx_insert_to, idx_extract_from) in (start_idx..=end_idx).enumerate() {
                let expr = per_frame_expression_coeffs.index_axis(nd::Axis(0), idx_extract_from);
                new_per_frame_expression_coeffs.index_axis_mut(nd::Axis(0), idx_insert_to).assign(&expr);
            }
            Some(new_per_frame_expression_coeffs)
        } else {
            None
        };
        Animation::new_from_matrices(
            new_per_frame_joint_poses,
            new_per_frame_root_trans,
            new_per_frame_expression_coeffs,
            cur_anim.config.clone(),
        )
    }
    /// Rotates multiple of 90 until the axis of the body is aligned with some
    /// arbitrary vector
    pub fn align_y_axis_quadrant(&mut self, current_axis: &nd::Array1<f32>, desired_axis: &nd::Array1<f32>) {
        let mut cur_axis_xz = na::Vector2::new(current_axis[0], -current_axis[2]).normalize();
        let desired_axis_xz = na::Vector2::new(desired_axis[0], -desired_axis[2]).normalize();
        let mut best_dot = f32::MIN;
        let mut best_angle: f32 = 0.0;
        let mut cur_angle = 0.0;
        let rot_90 = na::Rotation2::new(std::f32::consts::FRAC_PI_2);
        for _iters in 0..4 {
            cur_axis_xz = rot_90 * cur_axis_xz;
            cur_angle += 90.0;
            let dot = cur_axis_xz.dot(&desired_axis_xz);
            if dot > best_dot {
                best_dot = dot;
                best_angle = cur_angle;
            }
        }
        let alignment_rot = na::Rotation3::from_euler_angles(0.0, best_angle.to_radians(), 0.0);
        ndarray::Zip::from(self.per_frame_joint_poses.outer_iter_mut())
            .and(self.per_frame_root_trans.outer_iter_mut())
            .for_each(|mut poses_for_timestep, mut trans_for_timestep| {
                let pelvis_axis_angle = poses_for_timestep.row(0).into_nalgebra();
                let pelvis_axis_angle = pelvis_axis_angle.fixed_rows::<3>(0);
                let pelvis_rot = na::Rotation3::from_scaled_axis(pelvis_axis_angle);
                let new_pelvis_rot = alignment_rot * pelvis_rot;
                poses_for_timestep.row_mut(0).assign(&new_pelvis_rot.scaled_axis().ref_ndarray1());
                let trans_for_timestep_na = trans_for_timestep.to_owned().into_nalgebra();
                let new_trans_for_timestep_na = alignment_rot * trans_for_timestep_na;
                trans_for_timestep.assign(&new_trans_for_timestep_na.ref_ndarray1());
            });
    }
    pub fn get_cur_time(&self) -> Duration {
        self.runner.anim_current_time
    }
    pub fn set_cur_time_as_sec(&mut self, time_sec: f32) {
        self.runner.anim_current_time = Duration::from_secs_f32(time_sec);
    }
    pub fn pause(&mut self) {
        self.runner.paused = true;
    }
    pub fn play(&mut self) {
        self.runner.paused = false;
    }
    /// Duration of the animation
    #[allow(clippy::cast_precision_loss)]
    pub fn duration(&self) -> Duration {
        Duration::from_secs_f32(self.num_animation_frames() as f32 / self.config.fps)
    }
    pub fn is_finished(&self) -> bool {
        self.config.wrap_behaviour == AnimWrap::Clamp && self.runner.anim_current_time >= self.duration()
    }
    pub fn nr_repetitions(&self) -> u32 {
        self.runner.nr_repetitions
    }
    /// Shift each frame of the animation by the given translation vector.
    ///
    /// # Errors
    ///
    /// Will return `Err` for array size mismatch.
    pub fn translate(&mut self, translation: &nd::Array1<f32>) -> Result<(), String> {
        if translation.len() != self.per_frame_root_trans.ncols() {
            return Err("Translation vector should be length-3 array".to_owned());
        }
        for mut row in self.per_frame_root_trans.rows_mut() {
            row += translation;
        }
        Ok(())
    }
}