Skip to main content

draco_oxide_core/codec/connectivity/edgebreaker/
mod.rs

1use crate::bit_coder::{ByteReader, ByteWriter, ReaderErr};
2
3pub mod prediction;
4pub mod symbol_encoder;
5
6#[derive(Clone, Copy, Debug, Eq, PartialEq)]
7pub struct TopologySplit {
8    pub merging_symbol_idx: usize,
9    pub split_symbol_idx: usize,
10    pub merging_edge_orientation: Orientation,
11}
12
13#[derive(Clone, Copy, Debug, Eq, PartialEq)]
14pub enum Orientation {
15    Left,
16    Right,
17}
18
19#[derive(Clone, Copy, Debug, Eq, PartialEq)]
20#[allow(dead_code)] // This enum is not used yet, as we only support the default configuration.
21pub enum EdgebreakerKind {
22    Standard,
23    Predictive,
24    Valence,
25}
26
27impl EdgebreakerKind {
28    #[allow(unused)] // TODO: Remove this function when the decoder is complete
29    pub fn read_from<R>(reader: &mut R) -> Result<Self, Err>
30    where
31        R: ByteReader,
32    {
33        let traversal_type = reader.read_u8()?;
34        match traversal_type {
35            0 => Ok(Self::Standard),
36            1 => Ok(Self::Predictive),
37            2 => Ok(Self::Valence),
38            _ => Err(Err::InvalidTraversalType(traversal_type)),
39        }
40    }
41
42    pub fn write_to<W>(self, writer: &mut W)
43    where
44        W: ByteWriter,
45    {
46        let traversal_type = match self {
47            Self::Standard => 0,
48            Self::Predictive => 1,
49            Self::Valence => 2,
50        };
51        writer.write_u8(traversal_type);
52    }
53}
54
55pub const MAX_VALENCE: usize = 7;
56pub const MIN_VALENCE: usize = 2;
57
58#[derive(Clone, Debug, Eq, PartialEq)]
59pub enum TraversalType {
60    DepthFirst,
61    #[allow(dead_code)]
62    // This variant is not used yet. We might not implement this and may simply remove it in the future.
63    PredictionDegree,
64}
65
66impl TraversalType {
67    #[allow(unused)] // TODO: Remove this function when the decoder is complete
68    pub fn read_from<R>(reader: &mut R) -> Result<Self, Err>
69    where
70        R: ByteReader,
71    {
72        let traversal_type = reader.read_u8()?;
73        match traversal_type {
74            0 => Ok(Self::DepthFirst),
75            1 => Ok(Self::PredictionDegree),
76            _ => Err(Err::InvalidTraversalType(traversal_type)),
77        }
78    }
79
80    pub fn write_to<W>(self, writer: &mut W)
81    where
82        W: ByteWriter,
83    {
84        let traversal_type = match self {
85            Self::DepthFirst => 0,
86            Self::PredictionDegree => 1,
87        };
88        writer.write_u8(traversal_type);
89    }
90}
91
92#[derive(Debug, thiserror::Error, PartialEq)]
93pub enum Err {
94    #[error("Invalid traversal type: {0}")]
95    InvalidTraversalType(u8),
96    #[error("Reader error")]
97    ReaderError(#[from] ReaderErr),
98}
99
100#[allow(unused)] // This enum is not used yet, as we only support the default configuration.
101pub enum SymbolRansEncodingConfig {
102    LengthCoded,
103    DirectCoded,
104}
105
106impl SymbolRansEncodingConfig {
107    #[allow(unused)] // This function is not used yet, as we only support the default configuration.
108    pub fn read_from<R>(reader: &mut R) -> Result<Self, Err>
109    where
110        R: ByteReader,
111    {
112        let config = reader.read_u8()?;
113        match config {
114            0 => Ok(Self::LengthCoded),
115            1 => Ok(Self::DirectCoded),
116            _ => Err(Err::InvalidTraversalType(config)),
117        }
118    }
119
120    #[allow(unused)] // TODO: Remove this.
121    pub fn write_to<W>(self, writer: &mut W)
122    where
123        W: ByteWriter,
124    {
125        let config = match self {
126            Self::LengthCoded => 0,
127            Self::DirectCoded => 1,
128        };
129        writer.write_u8(config);
130    }
131}