viewport-lib-io 0.1.0

File format loaders and exporters for viewport-lib
Documentation
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
use std::collections::{HashMap, HashSet};
use std::path::Path;

use crate::error::IoError;
use crate::types::IoPointCloud;

#[derive(Clone, Copy, PartialEq)]
enum Format {
    Ascii,
    LittleEndian,
    BigEndian,
}

#[derive(Clone, Copy)]
enum ScalarType {
    Int8,
    Uint8,
    Int16,
    Uint16,
    Int32,
    Uint32,
    Float32,
    Float64,
}

impl ScalarType {
    fn from_str(value: &str) -> Option<Self> {
        match value {
            "char" | "int8" => Some(Self::Int8),
            "uchar" | "uint8" => Some(Self::Uint8),
            "short" | "int16" => Some(Self::Int16),
            "ushort" | "uint16" => Some(Self::Uint16),
            "int" | "int32" => Some(Self::Int32),
            "uint" | "uint32" => Some(Self::Uint32),
            "float" | "float32" => Some(Self::Float32),
            "double" | "float64" => Some(Self::Float64),
            _ => None,
        }
    }

    fn byte_size(self) -> usize {
        match self {
            Self::Int8 | Self::Uint8 => 1,
            Self::Int16 | Self::Uint16 => 2,
            Self::Int32 | Self::Uint32 | Self::Float32 => 4,
            Self::Float64 => 8,
        }
    }

    fn is_uchar(self) -> bool {
        matches!(self, Self::Uint8 | Self::Int8)
    }
}

struct PropertySpec {
    name: String,
    ty: ScalarType,
}

/// Decode a PLY file into a point cloud.
///
/// This entrypoint is point-cloud-first: files that contain faces are rejected
/// so the family boundary stays explicit.
pub fn point_cloud_from_path(path: &Path) -> Result<IoPointCloud, IoError> {
    #[cfg(feature = "ply")]
    {
        let bytes = std::fs::read(path)?;
        point_cloud_from_bytes(&bytes)
    }

    #[cfg(not(feature = "ply"))]
    {
        let _ = path;
        Err(IoError::MissingFeature {
            feature: "ply",
            context: "PLY point-cloud decoding",
        })
    }
}

fn point_cloud_from_bytes(bytes: &[u8]) -> Result<IoPointCloud, IoError> {
    let end_marker_lf = b"end_header\n";
    let end_marker_crlf = b"end_header\r\n";

    let (header_bytes, data_offset) = if let Some(position) = find_subsequence(bytes, end_marker_crlf)
    {
        (&bytes[..position], position + end_marker_crlf.len())
    } else if let Some(position) = find_subsequence(bytes, end_marker_lf) {
        (&bytes[..position], position + end_marker_lf.len())
    } else {
        return Err(IoError::Parse("ply: missing end_header".into()));
    };

    let header = std::str::from_utf8(header_bytes)
        .map_err(|_| IoError::Parse("ply: header is not valid UTF-8".into()))?;

    let mut lines = header.lines();
    if lines.next().unwrap_or("").trim() != "ply" {
        return Err(IoError::Parse("ply: not a PLY file".into()));
    }

    let format_line = lines.next().unwrap_or("").trim().to_string();
    let format = if format_line.starts_with("format ascii") {
        Format::Ascii
    } else if format_line.starts_with("format binary_little_endian") {
        Format::LittleEndian
    } else if format_line.starts_with("format binary_big_endian") {
        Format::BigEndian
    } else {
        return Err(IoError::Parse(format!(
            "ply: unknown file format `{format_line}`"
        )));
    };

    let mut vertex_count = 0usize;
    let mut face_count = 0usize;
    let mut vertex_props = Vec::new();
    let mut in_vertex = false;

    for line in lines {
        let line = line.trim();
        let parts: Vec<&str> = line.split_whitespace().collect();
        match parts.as_slice() {
            ["element", "vertex", count] => {
                vertex_count = count.parse().unwrap_or(0);
                in_vertex = true;
            }
            ["element", "face", count] => {
                face_count = count.parse().unwrap_or(0);
                in_vertex = false;
            }
            ["element", _, _] => {
                in_vertex = false;
            }
            ["property", scalar, name] if in_vertex => {
                if let Some(ty) = ScalarType::from_str(scalar) {
                    vertex_props.push(PropertySpec {
                        name: (*name).to_string(),
                        ty,
                    });
                }
            }
            _ => {}
        }
    }

    if face_count != 0 {
        return Err(IoError::Parse(
            "ply: file contains faces; use a mesh or scene importer instead".into(),
        ));
    }

    let find_property = |name: &str| vertex_props.iter().position(|prop| prop.name == name);
    let x_index = find_property("x").unwrap_or(0);
    let y_index = find_property("y").unwrap_or(1);
    let z_index = find_property("z").unwrap_or(2);
    let r_index = find_property("red");
    let g_index = find_property("green");
    let b_index = find_property("blue");
    let a_index = find_property("alpha");

    let has_color = r_index.is_some() && g_index.is_some() && b_index.is_some();
    let mut standard = HashSet::new();
    standard.insert(x_index);
    standard.insert(y_index);
    standard.insert(z_index);
    if let Some(index) = r_index {
        standard.insert(index);
    }
    if let Some(index) = g_index {
        standard.insert(index);
    }
    if let Some(index) = b_index {
        standard.insert(index);
    }
    if let Some(index) = a_index {
        standard.insert(index);
    }

    let extra_props: Vec<(usize, &PropertySpec)> = vertex_props
        .iter()
        .enumerate()
        .filter(|(index, _)| !standard.contains(index))
        .collect();

    let mut positions = Vec::with_capacity(vertex_count);
    let mut colors = if has_color {
        Vec::with_capacity(vertex_count)
    } else {
        Vec::new()
    };
    let mut scalar_bufs: Vec<Vec<f32>> = extra_props
        .iter()
        .map(|_| Vec::with_capacity(vertex_count))
        .collect();

    match format {
        Format::Ascii => parse_vertices_ascii(
            bytes,
            data_offset,
            vertex_count,
            &vertex_props,
            x_index,
            y_index,
            z_index,
            r_index,
            g_index,
            b_index,
            a_index,
            has_color,
            &extra_props,
            &mut positions,
            &mut colors,
            &mut scalar_bufs,
        )?,
        binary_format => parse_vertices_binary(
            bytes,
            data_offset,
            vertex_count,
            &vertex_props,
            binary_format,
            x_index,
            y_index,
            z_index,
            r_index,
            g_index,
            b_index,
            a_index,
            has_color,
            &extra_props,
            &mut positions,
            &mut colors,
            &mut scalar_bufs,
        )?,
    }

    let mut point_cloud = IoPointCloud {
        name: "PLY Point Cloud".to_string(),
        positions,
        colors,
        scalars: Vec::new(),
        scalar_attributes: HashMap::new(),
    };
    if scalar_bufs.len() == 1 {
        point_cloud.scalars = scalar_bufs.remove(0);
        if let Some((_, prop)) = extra_props.first() {
            point_cloud
                .scalar_attributes
                .insert(prop.name.clone(), point_cloud.scalars.clone());
        }
    } else {
        for (values, (_, prop)) in scalar_bufs.into_iter().zip(extra_props.iter()) {
            point_cloud.scalar_attributes.insert(prop.name.clone(), values);
        }
    }

    Ok(point_cloud)
}

#[allow(clippy::too_many_arguments)]
fn parse_vertices_ascii(
    bytes: &[u8],
    data_offset: usize,
    vertex_count: usize,
    vertex_props: &[PropertySpec],
    x_index: usize,
    y_index: usize,
    z_index: usize,
    r_index: Option<usize>,
    g_index: Option<usize>,
    b_index: Option<usize>,
    a_index: Option<usize>,
    has_color: bool,
    extra_props: &[(usize, &PropertySpec)],
    positions: &mut Vec<[f32; 3]>,
    colors: &mut Vec<[f32; 4]>,
    scalar_bufs: &mut Vec<Vec<f32>>,
) -> Result<(), IoError> {
    let text = std::str::from_utf8(&bytes[data_offset..])
        .map_err(|_| IoError::Parse("ply: data is not valid UTF-8".into()))?;
    let mut lines = text.lines();

    for _ in 0..vertex_count {
        let line = lines
            .next()
            .ok_or_else(|| IoError::Parse("ply: unexpected EOF in vertex data".into()))?;
        let parts: Vec<&str> = line.split_whitespace().collect();

        positions.push([
            parse_ascii_f32(&parts, x_index),
            parse_ascii_f32(&parts, y_index),
            parse_ascii_f32(&parts, z_index),
        ]);

        if has_color {
            let red_index = r_index.unwrap_or_default();
            let green_index = g_index.unwrap_or_default();
            let blue_index = b_index.unwrap_or_default();
            colors.push([
                parse_color_ascii(&parts, red_index, vertex_props[red_index].ty.is_uchar()),
                parse_color_ascii(
                    &parts,
                    green_index,
                    vertex_props[green_index].ty.is_uchar(),
                ),
                parse_color_ascii(&parts, blue_index, vertex_props[blue_index].ty.is_uchar()),
                a_index
                    .map(|index| parse_color_ascii(&parts, index, vertex_props[index].ty.is_uchar()))
                    .unwrap_or(1.0),
            ]);
        }

        for (buffer_index, (property_index, _)) in extra_props.iter().enumerate() {
            scalar_bufs[buffer_index].push(parse_ascii_f32(&parts, *property_index));
        }
    }

    Ok(())
}

fn parse_ascii_f32(parts: &[&str], index: usize) -> f32 {
    parts
        .get(index)
        .and_then(|value| value.parse::<f32>().ok())
        .unwrap_or(0.0)
}

fn parse_color_ascii(parts: &[&str], index: usize, is_uchar: bool) -> f32 {
    let value = parse_ascii_f32(parts, index);
    if is_uchar {
        value / 255.0
    } else {
        value
    }
}

#[allow(clippy::too_many_arguments)]
fn parse_vertices_binary(
    bytes: &[u8],
    data_offset: usize,
    vertex_count: usize,
    vertex_props: &[PropertySpec],
    format: Format,
    x_index: usize,
    y_index: usize,
    z_index: usize,
    r_index: Option<usize>,
    g_index: Option<usize>,
    b_index: Option<usize>,
    a_index: Option<usize>,
    has_color: bool,
    extra_props: &[(usize, &PropertySpec)],
    positions: &mut Vec<[f32; 3]>,
    colors: &mut Vec<[f32; 4]>,
    scalar_bufs: &mut Vec<Vec<f32>>,
) -> Result<(), IoError> {
    let mut prop_offsets = Vec::with_capacity(vertex_props.len());
    let mut stride = 0usize;
    for prop in vertex_props {
        prop_offsets.push(stride);
        stride += prop.ty.byte_size();
    }

    let data = &bytes[data_offset..];
    if data.len() < vertex_count * stride {
        return Err(IoError::Parse("ply: binary vertex data truncated".into()));
    }

    for vertex in 0..vertex_count {
        let base = vertex * stride;
        let record = &data[base..base + stride];
        let read_prop = |index: usize| -> f32 {
            let offset = prop_offsets[index];
            read_f32(&record[offset..], vertex_props[index].ty, format)
        };

        positions.push([read_prop(x_index), read_prop(y_index), read_prop(z_index)]);

        if has_color {
            let red_index = r_index.unwrap_or_default();
            let green_index = g_index.unwrap_or_default();
            let blue_index = b_index.unwrap_or_default();
            let to_color = |index: usize| -> f32 {
                let value = read_prop(index);
                if vertex_props[index].ty.is_uchar() {
                    value / 255.0
                } else {
                    value
                }
            };
            colors.push([
                to_color(red_index),
                to_color(green_index),
                to_color(blue_index),
                a_index.map(to_color).unwrap_or(1.0),
            ]);
        }

        for (buffer_index, (property_index, _)) in extra_props.iter().enumerate() {
            scalar_bufs[buffer_index].push(read_prop(*property_index));
        }
    }

    Ok(())
}

fn read_f32(buffer: &[u8], scalar_type: ScalarType, format: Format) -> f32 {
    match scalar_type {
        ScalarType::Float32 => {
            let bytes: [u8; 4] = buffer[..4].try_into().unwrap_or_default();
            if format == Format::LittleEndian {
                f32::from_le_bytes(bytes)
            } else {
                f32::from_be_bytes(bytes)
            }
        }
        ScalarType::Float64 => {
            let bytes: [u8; 8] = buffer[..8].try_into().unwrap_or_default();
            let value = if format == Format::LittleEndian {
                f64::from_le_bytes(bytes)
            } else {
                f64::from_be_bytes(bytes)
            };
            value as f32
        }
        ScalarType::Uint8 => buffer[0] as f32,
        ScalarType::Int8 => (buffer[0] as i8) as f32,
        ScalarType::Uint16 => {
            let bytes: [u8; 2] = buffer[..2].try_into().unwrap_or_default();
            (if format == Format::LittleEndian {
                u16::from_le_bytes(bytes)
            } else {
                u16::from_be_bytes(bytes)
            }) as f32
        }
        ScalarType::Int16 => {
            let bytes: [u8; 2] = buffer[..2].try_into().unwrap_or_default();
            (if format == Format::LittleEndian {
                i16::from_le_bytes(bytes)
            } else {
                i16::from_be_bytes(bytes)
            }) as f32
        }
        ScalarType::Uint32 => {
            let bytes: [u8; 4] = buffer[..4].try_into().unwrap_or_default();
            (if format == Format::LittleEndian {
                u32::from_le_bytes(bytes)
            } else {
                u32::from_be_bytes(bytes)
            }) as f32
        }
        ScalarType::Int32 => {
            let bytes: [u8; 4] = buffer[..4].try_into().unwrap_or_default();
            (if format == Format::LittleEndian {
                i32::from_le_bytes(bytes)
            } else {
                i32::from_be_bytes(bytes)
            }) as f32
        }
    }
}

fn find_subsequence(haystack: &[u8], needle: &[u8]) -> Option<usize> {
    haystack
        .windows(needle.len())
        .position(|window| window == needle)
}