Skip to main content

qualia_core_db/container_10d/mesh_section/
view.rs

1//! Borrowed complete geometry using the canonical mesh codec.
2//!
3//! Admission checks every triangle before exposing any geometry. The view owns
4//! no allocation and is independent of optional rendering/scientific sidecars.
5use super::{
6    dequantize, parse_mesh_header, MeshMiniHeader, MeshSectionError as E, FLAG_U16_INDICES,
7    MESH_MINI_HEADER_SIZE,
8};
9
10pub struct MeshSectionView<'a> {
11    header: MeshMiniHeader,
12    vertices: &'a [u8],
13    indices: &'a [u8],
14    index_width: usize,
15}
16
17impl<'a> MeshSectionView<'a> {
18    /// Validates a complete, exact-length section, including all index bounds.
19    pub fn parse(bytes: &'a [u8]) -> Result<Self, E> {
20        let (header, total) = parse_mesh_header(bytes)?;
21        if total != bytes.len() {
22            return Err(E::TrailingBytes {
23                expected: total,
24                got: bytes.len(),
25            });
26        }
27        let end = MESH_MINI_HEADER_SIZE + header.vertex_count as usize * 6;
28        let index_width = if header.flags & FLAG_U16_INDICES != 0 {
29            2
30        } else {
31            4
32        };
33        let view = Self {
34            header,
35            vertices: &bytes[MESH_MINI_HEADER_SIZE..end],
36            indices: &bytes[end..],
37            index_width,
38        };
39        for raw in view.indices.chunks_exact(index_width) {
40            let index = decode_index(raw);
41            if index >= header.vertex_count {
42                return Err(E::IndexOutOfBounds {
43                    index,
44                    vertices: header.vertex_count,
45                });
46            }
47        }
48        Ok(view)
49    }
50
51    pub const fn header(&self) -> &MeshMiniHeader {
52        &self.header
53    }
54
55    pub fn vertex(&self, index: usize) -> Option<[f32; 3]> {
56        let start = index.checked_mul(6)?;
57        let raw = self.vertices.get(start..start.checked_add(6)?)?;
58        let mut out = [0.; 3];
59        for (axis, dst) in out.iter_mut().enumerate() {
60            let q = u16::from_le_bytes([raw[axis * 2], raw[axis * 2 + 1]]);
61            *dst = dequantize(q, self.header.min[axis], self.header.max[axis]);
62        }
63        Some(out)
64    }
65
66    pub fn triangle(&self, index: usize) -> Option<[u32; 3]> {
67        let stride = self.index_width * 3;
68        let start = index.checked_mul(stride)?;
69        let raw = self.indices.get(start..start.checked_add(stride)?)?;
70        let mut out = [0; 3];
71        for (dst, bytes) in out.iter_mut().zip(raw.chunks_exact(self.index_width)) {
72            *dst = decode_index(bytes);
73        }
74        Some(out)
75    }
76}
77
78fn decode_index(raw: &[u8]) -> u32 {
79    if raw.len() == 2 {
80        u16::from_le_bytes([raw[0], raw[1]]) as u32
81    } else {
82        u32::from_le_bytes([raw[0], raw[1], raw[2], raw[3]])
83    }
84}
85
86#[cfg(test)]
87mod tests;