vox_format/
data.rs

1//! Contains trait for for reading voxel data, and a simple implementation for
2//! it.
3
4#[cfg(feature = "serialize")]
5use serde::{
6    Deserialize,
7    Serialize,
8};
9
10use crate::types::{
11    Model,
12    Palette,
13    Size,
14    Version,
15    Voxel,
16};
17
18/// A simple implementation of `VoxBuffer` that collects voxels into `Vec`s.
19pub type VoxData = VoxModels<Model>;
20
21impl VoxModelBuffer for Model {
22    fn new(size: Size) -> Self {
23        Model {
24            size,
25            voxels: vec![],
26        }
27    }
28
29    fn set_voxel(&mut self, voxel: Voxel, _palette: &Palette) {
30        self.voxels.push(voxel);
31    }
32}
33
34/// A trait for data structures that can constructed from a VOX file.
35/// `[crate::vox::VoxData]` implements this for convienience, but you can also
36/// implement this for your own voxel model types.
37///
38/// These are always called in this order:
39/// 1. `set_version`
40/// 2. `set_palette`
41/// 3. `set_num_models`
42/// 4. `set_model_size`
43///   1. `set_voxel`
44///
45/// `set_model_size` is always called before the voxels from this model are
46/// passed via `set_voxel`. `set_model_size` is called for each model, and
47/// `set_voxel` is called for each voxel in a model.
48pub trait VoxBuffer {
49    /// Called after the file version was read.
50    ///
51    /// The reader checks if the file version is supported, so most likely you
52    /// can ignore this.
53    fn set_version(&mut self, _version: Version) {}
54
55    /// Called after the number of models was detected.
56    fn set_num_models(&mut self, _num_models: usize) {}
57
58    /// Called for each model before its voxels are being passed with
59    /// [`VoxBuffer::set_voxel`].
60    fn set_model_size(&mut self, _model_size: Size) {}
61
62    /// Called for each voxel.
63    fn set_voxel(&mut self, voxel: Voxel);
64
65    /// Called when the color palette was read. This will be read before any
66    /// calls to [`Self::set_voxel`].
67    fn set_palette(&mut self, palette: Palette);
68}
69
70/// Trait for reading a single model.
71pub trait VoxModelBuffer {
72    fn new(size: Size) -> Self;
73    fn set_voxel(&mut self, voxel: Voxel, palette: &Palette);
74}
75
76/// A [`VoxBuffer`] implementation that collects the models into a `Vec` and is
77/// generic over the kind of voxel data.
78#[derive(Debug)]
79#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
80pub struct VoxModels<V> {
81    pub version: Version,
82    pub models: Vec<V>,
83    pub palette: Palette,
84}
85
86impl<V> Default for VoxModels<V> {
87    fn default() -> Self {
88        Self {
89            version: Version::default(),
90            models: vec![],
91            palette: Palette::default(),
92        }
93    }
94}
95
96impl<V: VoxModelBuffer> VoxBuffer for VoxModels<V> {
97    fn set_version(&mut self, version: Version) {
98        self.version = version;
99    }
100
101    fn set_num_models(&mut self, num_models: usize) {
102        self.models.reserve_exact(num_models);
103    }
104
105    fn set_model_size(&mut self, model_size: Size) {
106        self.models.push(V::new(model_size));
107    }
108
109    fn set_voxel(&mut self, voxel: Voxel) {
110        let model = self.models.last_mut().expect("model");
111        model.set_voxel(voxel, &self.palette);
112    }
113
114    fn set_palette(&mut self, palette: Palette) {
115        self.palette = palette;
116    }
117}