1use std::collections::{HashMap, VecDeque};
5use std::sync::{Arc, Mutex};
6
7#[derive(Clone, Debug)]
9pub struct CachedDepth {
10 pub width: usize,
12 pub height: usize,
14 pub raw_depth: Vec<f32>,
16 pub depth: Vec<f32>,
18 pub normal: Vec<[f32; 3]>,
20 pub cost: Vec<f32>,
22}
23
24type Entries = (HashMap<(u64, u64), Arc<CachedDepth>>, VecDeque<(u64, u64)>);
25
26pub struct DepthMapCache {
28 inner: Mutex<Entries>,
29 capacity: usize,
30}
31
32impl Default for DepthMapCache {
33 fn default() -> Self {
34 Self::new()
35 }
36}
37
38impl DepthMapCache {
39 pub fn new() -> Self {
41 Self::with_capacity(512)
42 }
43 pub fn with_capacity(capacity: usize) -> Self {
45 Self { inner: Mutex::new((HashMap::new(), VecDeque::new())), capacity: capacity.max(1) }
46 }
47 pub fn get(&self, key: (u64, u64)) -> Option<Arc<CachedDepth>> {
49 self.inner.lock().unwrap_or_else(|e| e.into_inner()).0.get(&key).cloned()
50 }
51 pub fn insert(&self, key: (u64, u64), v: Arc<CachedDepth>) {
53 let mut g = self.inner.lock().unwrap_or_else(|e| e.into_inner());
54 if g.0.insert(key, v).is_none() {
55 g.1.push_back(key);
56 }
57 while g.1.len() > self.capacity {
58 if let Some(k) = g.1.pop_front() {
59 g.0.remove(&k);
60 }
61 }
62 }
63 pub fn len(&self) -> usize {
65 self.inner.lock().unwrap_or_else(|e| e.into_inner()).0.len()
66 }
67 pub fn is_empty(&self) -> bool {
69 self.len() == 0
70 }
71}