Skip to main content

rust_3d/io/
pts.rs

1/*
2Copyright 2020 Martin Buck
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"),
6to deal in the Software without restriction, including without limitation the
7rights to use, copy, modify, merge, publish, distribute, sublicense,
8and/or sell copies of the Software, and to permit persons to whom the Software
9is furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall
12be included all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
20OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21*/
22
23//! Module for IO operations of the .pts file format
24
25use crate::*;
26
27use std::{
28    fmt,
29    io::{BufRead, Error as ioError},
30};
31
32use super::{types::*, utils::*};
33
34//------------------------------------------------------------------------------
35
36/// Loads IsPushable<Is3D> from the .pts file format
37pub fn load_pts<IP, P, R>(read: &mut R, ip: &mut IP) -> PtsResult<()>
38where
39    IP: IsPushable<P>,
40    P: IsBuildable3D,
41    R: BufRead,
42{
43    let mut line_buffer = Vec::new();
44    let mut i_line = 0;
45
46    let mut n_vertices = None;
47    let mut n_added = 0;
48
49    while let Ok(line) = fetch_line(read, &mut line_buffer) {
50        i_line += 1;
51
52        if line.is_empty() {
53            continue;
54        }
55
56        match n_vertices {
57            None => {
58                let mut words = to_words_skip_empty(line);
59                n_vertices = Some(
60                    words
61                        .next()
62                        .and_then(|word| from_ascii(word))
63                        .ok_or(PtsError::VertexCount)
64                        .line(i_line, line)?,
65                );
66                ip.reserve(n_vertices.unwrap());
67            }
68            Some(n) => {
69                if n_added < n {
70                    let mut words = to_words_skip_empty(line);
71
72                    let x = words
73                        .next()
74                        .and_then(|word| from_ascii(word))
75                        .ok_or(PtsError::Vertex)
76                        .line(i_line, line)?;
77
78                    let y = words
79                        .next()
80                        .and_then(|word| from_ascii(word))
81                        .ok_or(PtsError::Vertex)
82                        .line(i_line, line)?;
83
84                    let z = words
85                        .next()
86                        .and_then(|word| from_ascii(word))
87                        .ok_or(PtsError::Vertex)
88                        .line(i_line, line)?;
89
90                    ip.push(P::new(x, y, z));
91                    n_added += 1;
92                } else {
93                    // New block
94                    n_added = 0;
95                    let mut words = to_words_skip_empty(line);
96                    n_vertices = Some(
97                        words
98                            .next()
99                            .and_then(|word| from_ascii(word))
100                            .ok_or(PtsError::VertexCount)
101                            .line(i_line, line)?,
102                    );
103                    ip.reserve(n_vertices.unwrap());
104                }
105            }
106        }
107    }
108
109    Ok(())
110}
111
112//------------------------------------------------------------------------------
113
114/// Error type for .pts file operations
115pub enum PtsError {
116    AccessFile,
117    VertexCount,
118    Vertex,
119}
120
121/// Result type for .pts file operations
122pub type PtsResult<T> = IOResult<T, PtsError>;
123
124impl fmt::Debug for PtsError {
125    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
126        match self {
127            Self::AccessFile => write!(f, "Unable to access file"),
128            Self::VertexCount => write!(f, "Unable to parse vertex count"),
129            Self::Vertex => write!(f, "Unable to parse vertex"),
130        }
131    }
132}
133
134impl fmt::Display for PtsError {
135    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
136        write!(f, "{:?}", self)
137    }
138}
139
140impl From<ioError> for PtsError {
141    fn from(_error: ioError) -> Self {
142        PtsError::AccessFile
143    }
144}