ofps-suite 0.1.0

GUI tool for OFPS
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
use super::super::utils::{
    properties::transfer_props,
    timer::Timer,
    worker::{AppWorker, Workable},
};
use nalgebra as na;
use ofps::prelude::v1::*;
use once_cell::sync::OnceCell;
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::sync::{
    atomic::{AtomicBool, Ordering},
    mpsc::{self, Receiver, Sender, SyncSender},
    Arc, Mutex,
};
use std::thread::{spawn, JoinHandle};
use std::time::{Duration, Instant};
use wimrend::material::Material;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct EstimatorSettings {
    pub scale_factor: f32,
    pub camera_offset: f32,
    pub layer_frames: bool,
    pub keep_frames: usize,
    #[serde(skip)]
    pub clear_count: usize,
    #[serde(default)]
    pub properties: BTreeMap<String, Property>,
}

impl Default for EstimatorSettings {
    fn default() -> Self {
        Self {
            scale_factor: 0.0,
            camera_offset: 1.0,
            layer_frames: true,
            keep_frames: 100,
            clear_count: 0,
            properties: Default::default(),
        }
    }
}

pub enum FrameState {
    Pending(Vec<RGBA>, usize),
    Loaded(Arc<Material>),
}

#[derive(Default, Clone)]
pub struct EstimatorState {
    pub poses: Vec<(na::Point3<f32>, na::UnitQuaternion<f32>)>,
    pub transforms: Vec<(na::Vector3<f32>, na::UnitQuaternion<f32>)>,
    pub times: Vec<Duration>,
    pub layered_frames: Vec<(usize, Arc<Mutex<FrameState>>)>,
    pub clear_count: usize,
    pub properties: Option<BTreeMap<String, Property>>,
}

impl EstimatorState {
    fn apply_pose(
        &self,
        tr: na::Vector3<f32>,
        rot: na::UnitQuaternion<f32>,
    ) -> (na::Point3<f32>, na::UnitQuaternion<f32>) {
        let (pos, old_rot) = self.poses.last().copied().unwrap_or_default();
        (pos + old_rot * tr, rot * old_rot)
    }

    fn layer_frame(&self, settings: &EstimatorSettings) -> bool {
        settings.layer_frames
    }

    fn push_pose(
        &mut self,
        pos: na::Point3<f32>,
        rot: na::UnitQuaternion<f32>,
        tr: na::Vector3<f32>,
        frot: na::UnitQuaternion<f32>,
        frame: Option<Arc<Mutex<FrameState>>>,
        time: Duration,
    ) {
        let idx = self.poses.len();

        self.poses.push((pos, rot));
        self.transforms.push((tr, frot));
        self.times.push(time);

        if let Some(mat) = frame {
            self.layered_frames.push((idx, mat));
        }
    }

    pub fn layered_frames(
        &self,
    ) -> impl Iterator<
        Item = (
            na::Point3<f32>,
            na::UnitQuaternion<f32>,
            Arc<Mutex<FrameState>>,
        ),
    > + '_ {
        self.layered_frames
            .iter()
            .cloned()
            .map(move |(i, mat)| (self.poses[i].0, self.poses[i].1, mat))
    }

    fn remove_least_significant_frame(&mut self) {
        let frame = if self.layered_frames.len() <= 2 {
            0
        } else {
            self.layered_frames
                .iter()
                .enumerate()
                .map(|(i, s)| (i, self.poses[s.0].1))
                .fold(None, |candidate, (frame, rot)| {
                    let mut dists = self
                        .layered_frames
                        .iter()
                        .map(|s| self.poses[s.0].1.angle_to(&rot))
                        .collect::<Vec<_>>();

                    dists.sort_by(|a, b| {
                        if a > b {
                            std::cmp::Ordering::Greater
                        } else if a < b {
                            std::cmp::Ordering::Less
                        } else {
                            std::cmp::Ordering::Equal
                        }
                    });

                    let dist = dists.into_iter().take(5).sum::<f32>();

                    if let Some((frame, cur_dist)) = candidate {
                        if dist >= cur_dist {
                            return Some((frame, cur_dist));
                        }
                    }

                    Some((frame, dist))
                })
                .unwrap_or_default()
                .0
        };

        self.layered_frames.remove(frame);
    }
}

struct DecoderResult {
    frame: Result<(MotionVectors, Vec<RGBA>, usize)>,
    time: Duration,
    props: BTreeMap<String, Property>,
}

#[derive(Default)]
struct DecoderSettings {
    props: BTreeMap<String, Property>,
    realtime_processing: bool,
}

fn decoder_job(
    mut decoder: DecoderPlugin,
    flag: Arc<AtomicBool>,
    sender: SyncSender<DecoderResult>,
    settings: Arc<Mutex<DecoderSettings>>,
) {
    let mut motion_vectors = vec![];
    let mut motion_vectors_2 = vec![];
    let mut frame = vec![];
    let mut frame_2 = vec![];
    let mut frame_height = 0;
    let mut frame_height_2;
    let mut decoder_timer = None;

    while flag.load(Ordering::Relaxed) {
        let mut props = Default::default();

        if let Ok(settings) = settings.lock() {
            transfer_props(decoder.props_mut(), &settings.props, &mut props);

            Timer::handle_option(
                &mut decoder_timer,
                settings.realtime_processing,
                decoder
                    .get_framerate()
                    .filter(|f| *f > 0.0)
                    .map(|f| Duration::from_secs_f64(1.0 / f)),
            );
        }

        // Get the motion field.
        motion_vectors_2.clear();

        frame_2.clear();
        frame_height_2 = 0;

        let timer = Instant::now();

        let frame = match decoder.process_frame(
            &mut motion_vectors_2,
            Some((&mut frame_2, &mut frame_height_2)),
            0,
        ) {
            Ok(s) => {
                if s {
                    std::mem::swap(&mut motion_vectors, &mut motion_vectors_2);
                    std::mem::swap(&mut frame, &mut frame_2);
                    std::mem::swap(&mut frame_height, &mut frame_height_2);
                }

                Ok((motion_vectors.clone(), frame.clone(), frame_height))
            }
            Err(e) => Err(e),
        };

        let time = timer.elapsed();

        if sender.send(DecoderResult { frame, time, props }).is_err() {
            break;
        }
    }
}

struct DecoderJob {
    flag: Arc<AtomicBool>,
    handle: Option<JoinHandle<()>>,
    results: Option<Receiver<DecoderResult>>,
    settings: Arc<Mutex<DecoderSettings>>,
}

impl Drop for DecoderJob {
    fn drop(&mut self) {
        self.flag.store(false, Ordering::Relaxed);

        self.results.take();

        if let Some(handle) = self.handle.take() {
            handle.join().ok();
        }
    }
}

impl From<DecoderPlugin> for DecoderJob {
    fn from(decoder: DecoderPlugin) -> Self {
        let flag = Arc::new(AtomicBool::new(true));

        let (sender, results) = mpsc::sync_channel(0);
        let settings = Arc::new(Mutex::new(Default::default()));

        let results = Some(results);

        let handle = Some({
            let flag = flag.clone();
            let settings = settings.clone();
            spawn(move || decoder_job(decoder, flag, sender, settings))
        });

        Self {
            flag,
            handle,
            results,
            settings,
        }
    }
}

/// Tracking worker state (hidden from UI).
pub struct TrackingState {
    decoder: DecoderJob,
    decoder_times: Vec<Duration>,
    estimator_states: Vec<Option<EstimatorState>>,
    frames_to_load: Sender<Arc<Mutex<FrameState>>>,
    frames: usize,
}

/// UI side of the worker.
pub struct TrackingWorker {
    pub worker: AppWorker<TrackingState>,
    pub frames_to_load: Receiver<Arc<Mutex<FrameState>>>,
}

impl TrackingState {
    fn new(decoder: DecoderPlugin, frames_to_load: Sender<Arc<Mutex<FrameState>>>) -> Self {
        Self {
            decoder: decoder.into(),
            decoder_times: vec![],
            estimator_states: vec![],
            frames_to_load,
            frames: 0,
        }
    }

    pub fn worker(decoder: DecoderPlugin, settings: TrackingSettings) -> TrackingWorker {
        let (tx, frames_to_load) = mpsc::channel();
        TrackingWorker {
            worker: AppWorker::new(Self::new(decoder, tx), settings, TrackingState::update),
            frames_to_load,
        }
    }

    fn update(&mut self, out: &mut TrackingOutput, settings: TrackingSettings) -> bool {
        // Ensure the states are of consistent size with the settings size.
        self.estimator_states.resize(settings.settings.len(), None);

        // Reset that states to default values when needed.
        for (s, e) in self
            .estimator_states
            .iter_mut()
            .zip(settings.settings.iter().map(|(e, _, _)| e.lock()))
        {
            if let Ok(false) = e.map(|v| v.is_some()) {
                *s = None;
            } else if s.is_none() {
                *s = Some(Default::default());
            }
        }

        if let Ok(mut dec_settings) = self.decoder.settings.lock() {
            dec_settings.props = settings.decoder_properties;
            dec_settings.realtime_processing = settings.realtime_processing;
        }

        // `results` only becomes `None` when it is being dropped.
        let (motion_vectors, frame, frame_height, time) =
            match self.decoder.results.as_mut().unwrap().recv() {
                Ok(DecoderResult { frame, time, props }) => {
                    out.decoder_properties = Some(props);
                    match frame {
                        Ok((mv, f, fh)) => (mv, f, fh, time),
                        Err(_) => return false,
                    }
                }
                Err(_) => return false,
            };

        self.decoder_times.push(time);
        self.frames += 1;

        let mut mat = OnceCell::new();
        let camera = &settings.camera;

        // Go through each estimator and execute it.
        self.estimator_states
            .iter_mut()
            .zip(settings.settings.iter())
            .filter_map(|(s, settings)| s.as_mut().zip(Some(settings)))
            .par_bridge()
            .for_each(|(estimator_state, (estimator, _, est_settings))| {
                if let Ok(Some(estimator)) = estimator.lock().as_deref_mut() {
                    let mut props = estimator_state.properties.take().unwrap_or_default();

                    transfer_props(estimator.props_mut(), &est_settings.properties, &mut props);

                    estimator_state.properties = Some(props);

                    let timer = Instant::now();
                    if let Ok((frot, tr)) = estimator.estimate(&motion_vectors, camera, None) {
                        if estimator_state.clear_count != est_settings.clear_count {
                            estimator_state.layered_frames.clear();
                            estimator_state.clear_count = est_settings.clear_count;
                        }

                        let (pos, rot) = estimator_state.apply_pose(tr, frot);
                        let mat = if estimator_state.layer_frame(est_settings) {
                            if frame_height > 0 {
                                mat.get_or_init(|| {
                                    Arc::new(Mutex::new(FrameState::Pending(
                                        frame.clone(),
                                        frame_height,
                                    )))
                                });
                            }

                            mat.get()
                        } else {
                            None
                        };

                        let mut cnt = 0;
                        while estimator_state.layered_frames.len() >= est_settings.keep_frames
                            && cnt < 20
                        {
                            estimator_state.remove_least_significant_frame();
                            cnt += 1;
                        }

                        estimator_state.push_pose(
                            pos,
                            rot,
                            tr,
                            frot,
                            mat.cloned(),
                            timer.elapsed(),
                        );
                    }
                }
            });

        // Send a frame for UI to load as a texture.
        if let Some(mat) = mat.take() {
            self.frames_to_load.send(mat).unwrap();
        }

        out.estimators = self.estimator_states.clone();
        out.decoder_times = self.decoder_times.clone();

        true
    }
}

impl Workable for TrackingState {
    type Output = TrackingOutput;
    type Settings = TrackingSettings;
}

#[derive(Default)]
pub struct TrackingOutput {
    pub estimators: Vec<Option<EstimatorState>>,
    pub decoder_times: Vec<Duration>,
    pub decoder_properties: Option<BTreeMap<String, Property>>,
}

pub type EstSettingsTuple = (
    Arc<Mutex<Option<EstimatorPlugin>>>,
    Arc<AtomicBool>,
    EstimatorSettings,
);

#[derive(Clone)]
pub struct TrackingSettings {
    pub settings: Vec<EstSettingsTuple>,
    pub camera: StandardCamera,
    pub realtime_processing: bool,
    pub decoder_properties: BTreeMap<String, Property>,
}

impl Default for TrackingSettings {
    fn default() -> Self {
        Self {
            settings: vec![],
            camera: StandardCamera::new(16.0 / 9.0, 39.6 * 9.0 / 16.0),
            realtime_processing: false,
            decoder_properties: Default::default(),
        }
    }
}