1use derive_debug::Dbg;
2use num_enum::TryFromPrimitive;
3
4use crate::utils::formatter::{bytes_dbg, compact_dbg};
5use crate::{MltError, MltResult};
6
7#[derive(Debug, Clone, Copy, PartialEq, TryFromPrimitive)]
13#[repr(u8)]
14pub enum LogicalTechnique {
15 None = 0b0000_0000,
16 Delta = 0b0010_0000,
17 ComponentwiseDelta = 0b0100_0000,
18 Rle = 0b0110_0000,
19 Morton = 0b1000_0000,
20}
21
22#[derive(Debug, Clone, Copy, PartialEq, TryFromPrimitive)]
28#[repr(u8)]
29pub enum LogicalCombination {
30 None = 0b0000_0000,
31 Delta = 0b0010_0000,
32 DeltaRle = 0b0010_1100,
33 ComponentwiseDelta = 0b0100_0000,
34 Rle = 0b0110_0000,
35 Morton = 0b1000_0000,
36 MortonDelta = 0b1000_0100,
37 MortonRle = 0b1000_1100,
38}
39
40#[derive(Debug, Clone, Copy, PartialEq, Eq)]
46pub enum RleLayout {
47 Split,
49 #[cfg(feature = "unstable-v2")]
51 Interleaved,
52}
53
54#[derive(Debug, Clone, Copy, PartialEq)]
56pub enum RleMeta {
57 Split { runs: u32, num_rle_values: u32 },
60 #[cfg(feature = "unstable-v2")]
65 Interleaved { num_rle_values: u32 },
66}
67
68impl RleMeta {
69 #[cfg(feature = "unstable-v2")]
71 #[must_use]
72 pub(crate) fn num_rle_values(self) -> u32 {
73 match self {
74 Self::Split { num_rle_values, .. } => num_rle_values,
75 #[cfg(feature = "unstable-v2")]
76 Self::Interleaved { num_rle_values } => num_rle_values,
77 }
78 }
79}
80
81#[derive(Debug, Clone, Copy, PartialEq)]
83pub struct Morton {
84 pub(crate) bits: u32,
86 pub(crate) shift: u32,
88}
89
90impl Morton {
91 pub fn new(bits: u32, shift: u32) -> MltResult<Self> {
92 if bits <= 16 {
93 Ok(Self { bits, shift })
94 } else {
95 Err(MltError::InvalidMortonBits(bits))
96 }
97 }
98}
99
100#[derive(Debug, Clone, Copy, PartialEq)]
102pub enum LogicalEncoding {
103 None,
104 Delta,
105 DeltaRle(RleMeta),
106 ComponentwiseDelta,
107 Rle(RleMeta),
108 Morton(Morton),
109 MortonDelta(Morton),
110 MortonRle(Morton),
111}
112
113#[derive(Debug, PartialEq)]
119pub struct LogicalValue {
120 pub(crate) meta: StreamMeta,
121}
122
123#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, TryFromPrimitive)]
127#[repr(u8)]
128pub enum DictionaryType {
129 None = 0b0000_0000,
130 Single = 0b0000_0001,
131 Shared = 0b0000_0010,
132 Vertex = 0b0000_0011,
133 Morton = 0b0000_0100,
134 Fsst = 0b0000_0101,
135}
136
137#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, TryFromPrimitive)]
139#[repr(u8)]
140pub enum OffsetType {
141 Vertex = 0b0000_0000,
142 Index = 0b0000_0001,
143 String = 0b0000_0010,
144 Key = 0b0000_0011,
145}
146
147#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, TryFromPrimitive)]
149#[repr(u8)]
150pub enum LengthType {
151 VarBinary = 0b0000_0000,
152 Geometries = 0b0000_0001,
153 Parts = 0b0000_0010,
154 Rings = 0b0000_0011,
155 Triangles = 0b0000_0100,
156 Symbol = 0b0000_0101,
157 Dictionary = 0b0000_0110,
158}
159
160#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
162pub enum StreamType {
163 Present,
164 Data(DictionaryType),
165 Offset(OffsetType),
166 Length(LengthType),
167}
168
169#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, TryFromPrimitive)]
171#[repr(u8)]
172pub enum PhysicalEncoding {
173 None = 0b0000_0000,
174 FastPFor256 = 0b0000_0001,
177 VarInt = 0b0000_0010,
180}
181
182#[derive(Debug, Clone, Copy, PartialEq)]
185pub struct IntEncoding {
186 pub logical: LogicalEncoding,
187 pub physical: PhysicalEncoding,
188}
189
190impl IntEncoding {
191 #[must_use]
192 pub(crate) const fn new(logical: LogicalEncoding, physical: PhysicalEncoding) -> Self {
193 Self { logical, physical }
194 }
195
196 #[must_use]
197 pub(crate) const fn none() -> Self {
198 Self::new(LogicalEncoding::None, PhysicalEncoding::None)
199 }
200}
201
202#[derive(Clone, Copy, Dbg, PartialEq)]
204pub struct StreamMeta {
205 #[dbg(formatter = "compact_dbg")]
206 pub stream_type: StreamType,
207 #[dbg(formatter = "compact_dbg")]
208 pub encoding: IntEncoding,
209 pub(crate) num_values: u32,
210}
211
212impl StreamMeta {
213 #[inline]
214 pub(crate) fn new(stream_type: StreamType, encoding: IntEncoding, num_values: u32) -> Self {
215 Self {
216 stream_type,
217 encoding,
218 num_values,
219 }
220 }
221
222 #[inline]
223 pub(crate) fn new2(
224 stream_type: StreamType,
225 logical: LogicalEncoding,
226 physical: PhysicalEncoding,
227 num_values: usize,
228 ) -> MltResult<Self> {
229 let enc = IntEncoding::new(logical, physical);
230 Ok(Self::new(stream_type, enc, u32::try_from(num_values)?))
231 }
232
233 #[inline]
234 pub(crate) fn new_none(stream_type: StreamType, num_values: usize) -> MltResult<Self> {
235 let enc = IntEncoding::none();
236 Ok(Self::new(stream_type, enc, u32::try_from(num_values)?))
237 }
238}
239
240#[derive(Clone, Dbg, PartialEq)]
242pub struct RawStream<'a> {
243 pub meta: StreamMeta,
244 #[dbg(formatter = "bytes_dbg")]
245 pub(crate) data: &'a [u8],
246}
247
248impl<'a> RawStream<'a> {
249 #[must_use]
250 pub(crate) fn new(meta: StreamMeta, data: &'a [u8]) -> Self {
251 Self { meta, data }
252 }
253}