reve/src/renderer/scene.rs 3.2 KiB raw
1
use std::collections::VecDeque;
2
use std::sync::atomic::{AtomicU32, Ordering};
3
4
use super::*;
5
6
use ombre::collections::{IntMap, IntSet};
7
8
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
9
pub struct NodeId(u32);
10
11
impl NodeId {
12
    /// Get the next unique identifier.
13
    fn next() -> Self {
14
        static NEXT: AtomicU32 = AtomicU32::new(1);
15
        NodeId(NEXT.fetch_add(1, Ordering::SeqCst))
16
    }
17
18
    pub(super) fn new(n: u32) -> Option<Self> {
19
        if n > 0 { Some(Self(n)) } else { None }
20
    }
21
}
22
23
impl From<NodeId> for u32 {
24
    fn from(value: NodeId) -> Self {
25
        value.0
26
    }
27
}
28
29
#[derive(Debug, Default)]
30
pub enum Object {
31
    #[default]
32
    Null,
33
    Mesh {
34
        bindings: Bindings<2, 2>,
35
        selection: Vec<Flag>,
36
        vertices: Vec<Vertex>,
37
        elements: usize,
38
        style: RenderStyle,
39
        mode: InteractionMode,
40
    },
41
    #[allow(dead_code)]
42
    Light,
43
}
44
45
#[derive(Debug)]
46
pub struct Node {
47
    pub id: NodeId,
48
    pub object: Object,
49
    #[allow(dead_code)]
50
    pub transform: Transform3D<f32, Local, World>,
51
    pub children: IntSet<NodeId>,
52
}
53
54
impl Node {
55
    pub fn new(object: Object) -> Self {
56
        Self {
57
            id: NodeId::next(),
58
            object,
59
            transform: Transform3D::default(),
60
            children: IntSet::default(),
61
        }
62
    }
63
}
64
65
/// Scene graph.
66
#[derive(Debug)]
67
pub struct Graph {
68
    root: NodeId,
69
    nodes: IntMap<NodeId, Node>,
70
}
71
72
impl Default for Graph {
73
    fn default() -> Self {
74
        Self::new(Node::new(Object::Null))
75
    }
76
}
77
78
impl Graph {
79
    pub fn new(root: Node) -> Self {
80
        Self {
81
            root: root.id,
82
            nodes: IntMap::from_iter([(root.id, root)]),
83
        }
84
    }
85
86
    pub fn get(&self, id: &NodeId) -> Option<&Node> {
87
        self.nodes.get(id)
88
    }
89
90
    pub fn root(&self) -> &Node {
91
        &self.nodes[&self.root]
92
    }
93
94
    pub fn insert(&mut self, node: Node, parent: NodeId) -> bool {
95
        if let Some(parent) = self.nodes.get_mut(&parent) {
96
            parent.children.insert(node.id);
97
            self.nodes.insert(node.id, node);
98
99
            return true;
100
        }
101
        false
102
    }
103
104
    pub fn get_mut(&mut self, id: &NodeId) -> Option<&mut Node> {
105
        self.nodes.get_mut(id)
106
    }
107
108
    #[allow(dead_code)]
109
    pub fn remove(&mut self, id: NodeId) -> Option<Node> {
110
        if let Some(node) = self.nodes.remove(&id) {
111
            for child in node.children.iter().copied() {
112
                self.remove(child);
113
            }
114
            for node in self.nodes.values_mut() {
115
                node.children.remove(&id);
116
            }
117
            return Some(node);
118
        }
119
        None
120
    }
121
122
    pub fn iter(&self) -> Iter<'_> {
123
        Iter {
124
            graph: self,
125
            queue: VecDeque::from(vec![self.root]),
126
        }
127
    }
128
}
129
130
/// Graph iterator.
131
pub struct Iter<'a> {
132
    graph: &'a Graph,
133
    queue: VecDeque<NodeId>,
134
}
135
136
impl<'a> Iterator for Iter<'a> {
137
    type Item = &'a Node;
138
139
    fn next(&mut self) -> Option<Self::Item> {
140
        self.queue.pop_front().and_then(|id| {
141
            if let Some(node) = self.graph.get(&id) {
142
                for child_id in &node.children {
143
                    self.queue.push_back(*child_id);
144
                }
145
                return Some(node);
146
            }
147
            None
148
        })
149
    }
150
}