Skip to main content

conspire/geometry/ntree/pair/
mod.rs

1use crate::geometry::ntree::node::slot::Slot;
2use crate::geometry::ntree::{Orthotree, node::cell::Cell};
3
4/// Constraint on how the orthants of a node may mix leaves and subtrees.
5#[derive(Clone, Copy)]
6pub enum Pairing {
7    /// Orthants may mix, provided the resulting nodes admit a valid dual.
8    Generalized,
9    /// Orthants must be either all leaves or all subtrees.
10    Regular,
11    /// Orthants may mix freely.
12    None,
13}
14
15impl<const D: usize, const L: usize, const M: usize, const N: usize, T, U, V>
16    Orthotree<D, L, M, N, T, U, V>
17where
18    T: Cell,
19    U: Slot,
20    V: Copy,
21{
22    pub fn pair(&mut self, pairing: Pairing) -> Result<bool, &'static str> {
23        match pairing {
24            Pairing::Generalized => unimplemented!(),
25            Pairing::Regular => {
26                let mut index = 0;
27                let mut paired = true;
28                while index < self.len() {
29                    if let Some(nodes) = self.nodes[index].orthants() {
30                        let mut any_leaf = false;
31                        let mut any_tree = false;
32                        let mut leaves = Vec::with_capacity(N);
33                        for &node in nodes.iter() {
34                            if self[node].is_leaf() {
35                                any_leaf = true;
36                                leaves.push(node);
37                            } else if self[node].is_tree() {
38                                any_tree = true;
39                            }
40                        }
41                        if any_tree && any_leaf {
42                            for node in leaves {
43                                paired = false;
44                                self.subdivide(node.slot())?;
45                            }
46                        }
47                    }
48                    index += 1;
49                }
50                Ok(paired)
51            }
52            Pairing::None => Ok(true),
53        }
54    }
55}