use super::buffer::Buffer;
use super::corner_table::CornerTable;
use super::rans::BitDecoder;
use super::DracoError;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Symbol {
C = 0,
S = 1,
L = 3,
R = 5,
E = 7,
}
impl Symbol {
fn from_code(code: u32) -> Option<Symbol> {
Some(match code {
0 => Symbol::C,
1 => Symbol::S,
3 => Symbol::L,
5 => Symbol::R,
7 => Symbol::E,
_ => return None,
})
}
}
pub trait SymbolSource {
fn next_symbol(&mut self, table: &CornerTable) -> Result<Symbol, DracoError>;
fn new_active_corner_reached(&mut self, _table: &CornerTable, _corner: u32) {}
fn merge_vertices(&mut self, _dest: u32, _source: u32) {}
}
#[derive(Debug, Clone, Default)]
pub struct StandardSymbols {
pub symbols: Vec<Symbol>,
at: usize,
}
impl SymbolSource for StandardSymbols {
fn next_symbol(&mut self, _table: &CornerTable) -> Result<Symbol, DracoError> {
let symbol = *self.symbols.get(self.at).ok_or(DracoError::Corrupt)?;
self.at += 1;
Ok(symbol)
}
}
const SYMBOL_ORDER: [Symbol; 5] = [Symbol::C, Symbol::S, Symbol::L, Symbol::R, Symbol::E];
#[derive(Debug, Clone, Default)]
pub struct ValenceSymbols {
vertex_valences: Vec<i32>,
last_symbol: Option<Symbol>,
active_context: i32,
min_valence: i32,
max_valence: i32,
context_symbols: Vec<Vec<u32>>,
context_counters: Vec<i32>,
}
impl ValenceSymbols {
pub fn start(
buffer: &mut Buffer<'_>,
num_vertices: u32,
num_faces: u32,
) -> Result<Self, DracoError> {
let (min_valence, max_valence) = (2i32, 7i32);
let num_contexts = (max_valence - min_valence + 1) as usize;
let mut context_symbols = Vec::with_capacity(num_contexts);
let mut context_counters = vec![0i32; num_contexts];
for counter in context_counters.iter_mut() {
let num_symbols = buffer.varint32()?;
if num_symbols > num_faces {
return Err(DracoError::Corrupt);
}
if num_symbols > 0 {
context_symbols.push(super::rans::decode_symbols(
num_symbols as usize,
1,
buffer,
)?);
*counter = num_symbols as i32;
} else {
context_symbols.push(Vec::new());
}
}
Ok(ValenceSymbols {
vertex_valences: vec![0i32; num_vertices as usize],
last_symbol: None,
active_context: -1,
min_valence,
max_valence,
context_symbols,
context_counters,
})
}
}
impl SymbolSource for ValenceSymbols {
fn next_symbol(&mut self, _table: &CornerTable) -> Result<Symbol, DracoError> {
let symbol = if self.active_context != -1 {
let context = self.active_context as usize;
let counter = self
.context_counters
.get_mut(context)
.ok_or(DracoError::Corrupt)?;
*counter -= 1;
if *counter < 0 {
return Err(DracoError::Corrupt);
}
let at = *counter as usize;
let symbol_id = *self
.context_symbols
.get(context)
.and_then(|c| c.get(at))
.ok_or(DracoError::Corrupt)?;
if symbol_id > 4 {
return Err(DracoError::Corrupt);
}
SYMBOL_ORDER[symbol_id as usize]
} else {
Symbol::E
};
self.last_symbol = Some(symbol);
Ok(symbol)
}
fn new_active_corner_reached(&mut self, table: &CornerTable, corner: u32) {
let next = table.next(corner);
let prev = table.previous(corner);
let deltas: [(u32, i32); 3] = match self.last_symbol {
Some(Symbol::C) | Some(Symbol::S) => [(corner, 0), (next, 1), (prev, 1)],
Some(Symbol::R) => [(corner, 1), (next, 1), (prev, 2)],
Some(Symbol::L) => [(corner, 1), (next, 2), (prev, 1)],
Some(Symbol::E) => [(corner, 2), (next, 2), (prev, 2)],
None => [(corner, 0), (next, 0), (prev, 0)],
};
for (at, by) in deltas {
if by == 0 {
continue;
}
let vert = table.vertex(at);
if let Some(slot) = self.vertex_valences.get_mut(vert as usize) {
*slot += by;
}
}
let vert = table.vertex(next);
let valence = self
.vertex_valences
.get(vert as usize)
.copied()
.unwrap_or(0)
.clamp(self.min_valence, self.max_valence);
self.active_context = valence - self.min_valence;
}
fn merge_vertices(&mut self, dest: u32, source: u32) {
let gained = self
.vertex_valences
.get(source as usize)
.copied()
.unwrap_or(0);
if let Some(slot) = self.vertex_valences.get_mut(dest as usize) {
*slot += gained;
}
}
}
#[derive(Debug, Clone)]
pub enum Symbols {
Standard(StandardSymbols),
Valence(ValenceSymbols),
}
impl Symbols {
pub fn standard(&self) -> Option<&[Symbol]> {
match self {
Symbols::Standard(s) => Some(&s.symbols),
Symbols::Valence(_) => None,
}
}
}
impl SymbolSource for Symbols {
fn next_symbol(&mut self, table: &CornerTable) -> Result<Symbol, DracoError> {
match self {
Symbols::Standard(s) => s.next_symbol(table),
Symbols::Valence(s) => s.next_symbol(table),
}
}
fn new_active_corner_reached(&mut self, table: &CornerTable, corner: u32) {
match self {
Symbols::Standard(s) => s.new_active_corner_reached(table, corner),
Symbols::Valence(s) => s.new_active_corner_reached(table, corner),
}
}
fn merge_vertices(&mut self, dest: u32, source: u32) {
match self {
Symbols::Standard(s) => s.merge_vertices(dest, source),
Symbols::Valence(s) => s.merge_vertices(dest, source),
}
}
}
pub struct TraversalData<'a> {
pub symbols: Symbols,
pub start_faces: BitDecoder<'a>,
pub attribute_seams: Vec<BitDecoder<'a>>,
pub symbol_bits: usize,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TopologySplit {
pub source_symbol_id: u32,
pub split_symbol_id: u32,
pub source_edge: u32,
}
pub fn topology_splits(buffer: &mut Buffer<'_>, num_faces: u32) -> Result<Vec<TopologySplit>, DracoError> {
let count = buffer.varint32()?;
if count == 0 {
return Ok(Vec::new());
}
if count > num_faces {
return Err(DracoError::Corrupt);
}
let mut splits = Vec::with_capacity(count as usize);
let mut last_source = 0u32;
for _ in 0..count {
let source_symbol_id = buffer.varint32()?.wrapping_add(last_source);
let delta = buffer.varint32()?;
if delta > source_symbol_id {
return Err(DracoError::Corrupt);
}
splits.push(TopologySplit {
source_symbol_id,
split_symbol_id: source_symbol_id - delta,
source_edge: 0,
});
last_source = source_symbol_id;
}
buffer.start_bits();
for split in splits.iter_mut() {
split.source_edge = buffer.bits(1) & 1;
}
buffer.end_bits();
Ok(splits)
}
pub fn decode<'a>(
buffer: &mut Buffer<'a>,
traversal: super::Traversal,
num_symbols: u32,
num_attribute_data: u8,
num_vertices: u32,
num_faces: u32,
) -> Result<TraversalData<'a>, DracoError> {
let mut symbol_bits = 0usize;
let standard = match traversal {
super::Traversal::Standard => {
let (symbols, bits) = decode_symbol_stream(buffer, num_symbols)?;
symbol_bits = bits;
Some(symbols)
}
super::Traversal::Valence => None,
};
let start_faces = BitDecoder::start(buffer)?;
let mut attribute_seams = Vec::with_capacity(num_attribute_data as usize);
for _ in 0..num_attribute_data {
attribute_seams.push(BitDecoder::start(buffer)?);
}
let symbols = match standard {
Some(symbols) => Symbols::Standard(StandardSymbols { symbols, at: 0 }),
None => Symbols::Valence(ValenceSymbols::start(buffer, num_vertices, num_faces)?),
};
Ok(TraversalData {
symbols,
start_faces,
attribute_seams,
symbol_bits,
})
}
fn decode_symbol_stream(
buffer: &mut Buffer<'_>,
num_symbols: u32,
) -> Result<(Vec<Symbol>, usize), DracoError> {
let size = buffer.varint()? as usize;
if size > buffer.remaining() {
return Err(DracoError::Corrupt);
}
let start = buffer.at;
buffer.start_bits();
let mut symbols = Vec::with_capacity(num_symbols as usize);
for _ in 0..num_symbols {
let mut code = buffer.bit();
if code != 0 {
code |= buffer.bits(2) << 1;
}
symbols.push(Symbol::from_code(code).ok_or(DracoError::Corrupt)?);
}
buffer.end_bits();
let symbol_bits = (buffer.at - start) * 8;
if buffer.at > start + size {
return Err(DracoError::Corrupt);
}
buffer.at = start + size;
Ok((symbols, symbol_bits))
}