1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use {DEFAULT_PALETTE, DotVoxData, Model, model, palette, Size, Voxel};
use nom::IResult;
use nom::types::CompleteByteSlice;
use std::collections::HashMap;
use std::str;
use std::str::Utf8Error;

const MAGIC_NUMBER: &'static str = "VOX ";

#[derive(Debug, PartialEq)]
pub enum Chunk {
    Main(Vec<Chunk>),
    Size(Size),
    Voxels(Vec<Voxel>),
    Pack(Model),
    Palette(Vec<u32>),
    Material(Material),
    Unknown(String),
    Invalid(Vec<u8>),
}

/// A material used to render this model.
#[derive(Clone, Debug, PartialEq)]
pub struct Material {
    /// The Material's ID
    pub id: u32,
    /// Properties of the material, mapped by property name.
    pub properties: Dict,
}

/// General dictionary
pub type Dict = HashMap<String, String>;

/// Recognizes an unsigned 1 byte integer (equivalent to take!(1)
#[inline]
pub fn le_u8(i: CompleteByteSlice) -> IResult<CompleteByteSlice, u8> {
    Ok((CompleteByteSlice(&i[1..]), i[0]))
}

/// Recognizes little endian unsigned 4 bytes integer
#[inline]
pub fn le_u32(i: CompleteByteSlice) -> IResult<CompleteByteSlice, u32> {
    let res = ((i[3] as u32) << 24) + ((i[2] as u32) << 16) + ((i[1] as u32) << 8) + i[0] as u32;
    Ok((CompleteByteSlice(&i[4..]), res))
}

pub fn to_str(i: CompleteByteSlice) -> Result<String, Utf8Error> {
    let res = str::from_utf8(i.0)?;
    Ok(res.to_owned())
}

named!(pub parse_vox_file <CompleteByteSlice, DotVoxData>, do_parse!(
  tag!(MAGIC_NUMBER) >>
  version: le_u32 >>
  main: parse_chunk >>
  (map_chunk_to_data(version, main))
));

fn map_chunk_to_data(version: u32, main: Chunk) -> DotVoxData {
    match main {
        Chunk::Main(children) => {
            let mut size_holder: Option<Size> = None;
            let mut models: Vec<Model> = vec![];
            let mut palette_holder: Vec<u32> = DEFAULT_PALETTE.to_vec();
            let mut materials: Vec<Material> = vec![];
            for chunk in children {
                match chunk {
                    Chunk::Size(size) => size_holder = Some(size),
                    Chunk::Voxels(voxels) => {
                        if let Some(size) = size_holder {
                            models.push(Model { size, voxels })
                        }
                    }
                    Chunk::Pack(model) => models.push(model),
                    Chunk::Palette(palette) => palette_holder = palette,
                    Chunk::Material(material) => materials.push(material),
                    _ => debug!("Unmapped chunk {:?}", chunk)
                }
            }

            DotVoxData {
                version,
                models,
                palette: palette_holder,
                materials,
            }
        }
        _ => DotVoxData {
            version,
            models: vec![],
            palette: vec![],
            materials: vec![],
        }
    }
}

named!(parse_chunk <CompleteByteSlice, Chunk>, do_parse!(
    id: map_res!(take!(4), to_str) >>
    content_size: le_u32 >>
    children_size: le_u32 >>
    chunk_content: take!(content_size) >>
    child_content: take!(children_size) >>
    (build_chunk(id, chunk_content, children_size, child_content))
));

fn build_chunk(string: String,
               chunk_content: CompleteByteSlice,
               children_size: u32,
               child_content: CompleteByteSlice) -> Chunk {
    let id = string.as_str();
    if children_size == 0 {
        match id {
            "SIZE" => build_size_chunk(chunk_content),
            "XYZI" => build_voxel_chunk(chunk_content),
            "PACK" => build_pack_chunk(chunk_content),
            "RGBA" => build_palette_chunk(chunk_content),
            "MATL" => build_material_chunk(chunk_content),
            _ => {
                debug!("Unknown childless chunk {:?}", id);
                Chunk::Unknown(id.to_owned())
            }
        }
    } else {
        let result: IResult<CompleteByteSlice, Vec<Chunk>> = many0!(child_content, parse_chunk);
        let child_chunks = match result {
            Ok((_, result)) => result,
            result => {
                debug!("Failed to parse child chunks, due to {:?}", result);
                vec![]
            }
        };
        match id {
            "MAIN" => Chunk::Main(child_chunks),
            "PACK" => build_pack_chunk(chunk_content),
            _ => {
                debug!("Unknown chunk with children {:?}", id);
                Chunk::Unknown(id.to_owned())
            }
        }
    }
}

fn build_material_chunk(chunk_content: CompleteByteSlice) -> Chunk {
    if let Ok((_, material)) = parse_material(chunk_content) {
        return Chunk::Material(material);
    }
    Chunk::Invalid(chunk_content.to_vec())
}

fn build_palette_chunk(chunk_content: CompleteByteSlice) -> Chunk {
    if let Ok((_, palette)) = palette::extract_palette(chunk_content) {
        return Chunk::Palette(palette);
    }
    Chunk::Invalid(chunk_content.to_vec())
}

fn build_pack_chunk(chunk_content: CompleteByteSlice) -> Chunk {
    if let Ok((chunk_content, Chunk::Size(size))) = parse_chunk(chunk_content) {
        if let Ok((_, Chunk::Voxels(voxels))) = parse_chunk(chunk_content) {
            return Chunk::Pack(Model { size, voxels: voxels.to_vec() });
        }
    }
    Chunk::Invalid(chunk_content.to_vec())
}

fn build_size_chunk(chunk_content: CompleteByteSlice) -> Chunk {
    match model::parse_size(chunk_content) {
        Ok((_, size)) => Chunk::Size(size),
        _ => Chunk::Invalid(chunk_content.to_vec())
    }
}

fn build_voxel_chunk(chunk_content: CompleteByteSlice) -> Chunk {
    match model::parse_voxels(chunk_content) {
        Ok((_, voxels)) => Chunk::Voxels(voxels),
        _ => Chunk::Invalid(chunk_content.to_vec())
    }
}

named!(pub parse_material <CompleteByteSlice, Material>, do_parse!(
    id: le_u32 >>
    properties: parse_dict >>
    (Material { id, properties })
));


named!(parse_dict <CompleteByteSlice, Dict>, do_parse!(
    count: le_u32 >>
    entries: many_m_n!(count as usize, count as usize, parse_dict_entry) >>
    (build_dict_from_entries(entries))
));

named!(parse_dict_entry <CompleteByteSlice, (String, String)>, tuple!(parse_string, parse_string));

named!(parse_string <CompleteByteSlice, String>, do_parse!(
    count: le_u32 >>
    buffer: map_res!(take!(count), to_str) >>
    (buffer)
));

fn build_dict_from_entries(entries: Vec<(String, String)>) -> Dict {
    let mut map = HashMap::with_capacity(entries.len());
    for (key, value) in entries {
        map.insert(key, value);
    }
    map
}

#[cfg(test)]
mod tests {
    use avow::vec;
    use super::*;

    #[test]
    fn can_parse_size_chunk() {
        let bytes = include_bytes!("resources/valid_size.bytes").to_vec();
        let result = parse_chunk(CompleteByteSlice(&bytes));
        assert!(result.is_ok());
        let (_, size) = result.unwrap();
        assert_eq!(
            size,
            Chunk::Size(Size {
                x: 24,
                y: 24,
                z: 24,
            })
        );
    }

    #[test]
    fn can_parse_voxels_chunk() {
        let bytes = include_bytes!("resources/valid_voxels.bytes").to_vec();
        let result = parse_chunk(CompleteByteSlice(&bytes));
        assert!(result.is_ok());
        let (_, voxels) = result.unwrap();
        match voxels {
            Chunk::Voxels(voxels) => vec::are_eq(
                voxels,
                vec![Voxel { x: 0, y: 0, z: 0, i: 225 },
                     Voxel { x: 0, y: 1, z: 1, i: 215 },
                     Voxel { x: 1, y: 0, z: 1, i: 235 },
                     Voxel { x: 1, y: 1, z: 0, i: 5 },
                ],
            ),
            chunk => panic!("Expecting Voxel chunk, got {:?}", chunk)
        };
    }

    #[test]
    fn can_parse_palette_chunk() {
        let bytes = include_bytes!("resources/valid_palette.bytes").to_vec();
        let result = parse_chunk(CompleteByteSlice(&bytes));
        assert!(result.is_ok());
        let (_, palette) = result.unwrap();
        match palette {
            Chunk::Palette(palette) => vec::are_eq(palette, DEFAULT_PALETTE.to_vec()),
            chunk => panic!("Expecting Palette chunk, got {:?}", chunk)
        };
    }

    #[test]
    fn can_parse_a_material_chunk() {
        let bytes = include_bytes!("resources/valid_material.bytes").to_vec();
        let result = parse_material(CompleteByteSlice(&bytes));
        match result {
            Ok((_, material)) => {
                assert_eq!(material.id, 0);
                assert_eq!(material.properties.get("_type"), Some(&"_diffuse".to_owned()));
                assert_eq!(material.properties.get("_weight"), Some(&"1".to_owned()));
                assert_eq!(material.properties.get("_rough"), Some(&"0.1".to_owned()));
                assert_eq!(material.properties.get("_spec"), Some(&"0.5".to_owned()));
                assert_eq!(material.properties.get("_ior"), Some(&"0.3".to_owned()));
            }
            _ => panic!("Expected Done, got {:?}", result)
        }
    }
}