Skip to main content

draco_rs/
encode.rs

1use crate::prelude::*;
2use autocxx::prelude::*;
3
4pub type Encoder = WrappedDracoObject<ffi::draco::Encoder>;
5
6impl Encoder {
7    pub fn new() -> Self {
8        let encoder = ffi::draco::Encoder::new().within_unique_ptr();
9        Self(encoder)
10    }
11
12    pub fn set_attribute_quantization(
13        mut self,
14        attr: ffi::draco::GeometryAttribute_Type,
15        num_bits: i32,
16    ) -> Self {
17        self.0
18            .pin_mut()
19            .SetAttributeQuantization(attr, num_bits.into());
20        self
21    }
22
23    pub fn set_speed_options(mut self, encoding_speed: i32, decoding_speed: i32) -> Self {
24        self.0
25            .pin_mut()
26            .SetSpeedOptions(encoding_speed.into(), decoding_speed.into());
27        self
28    }
29}
30impl Default for Encoder {
31    fn default() -> Self {
32        Self::new()
33    }
34}
35
36pub type EncoderBuffer = WrappedDracoObject<ffi::draco::EncoderBuffer>;
37
38impl EncoderBuffer {
39    pub fn new() -> Self {
40        let buffer = ffi::draco::EncoderBuffer::new().within_unique_ptr();
41        Self(buffer)
42    }
43
44    pub fn as_mut_ptr(&mut self) -> *mut ffi::draco::EncoderBuffer {
45        self.0.as_mut_ptr()
46    }
47
48    pub fn clear(&mut self) {
49        self.0.pin_mut().Clear();
50    }
51
52    pub fn resize(&mut self, size: i64) {
53        self.0.pin_mut().Resize(size);
54    }
55
56    /// Returns a pointer to the data in the buffer
57    pub fn data(&self) -> *const std::os::raw::c_char {
58        self.0.data()
59    }
60
61    // Returns the size of the buffer in bytes
62    pub fn size(&self) -> usize {
63        self.0.size()
64    }
65
66    /// Returns a slice of the buffer
67    ///
68    /// # Safety
69    ///
70    /// The pointer must be valid for the lifetime of the buffer
71    pub fn as_slice(&self) -> &[u8] {
72        // cast the pointer to a slice of u8
73        unsafe { std::slice::from_raw_parts(self.data() as *const u8, self.size()) }
74    }
75}
76
77impl Default for EncoderBuffer {
78    fn default() -> Self {
79        Self::new()
80    }
81}