rust_3d/io/psl.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 psl file format
24
25use crate::*;
26
27use std::{
28 fmt,
29 io::{Error as ioError, Read},
30};
31
32use super::byte_reader::*;
33
34//------------------------------------------------------------------------------
35
36/// Loads a IsPushable<Is3D> as x y z coordinates from .psl files
37pub fn load_psl<IP, P, R>(read: &mut R, ip: &mut IP) -> PslResult<()>
38where
39 IP: IsPushable<P>,
40 P: IsBuildable3D,
41 R: Read,
42{
43 // header
44 {
45 let mut buffer = [0u8; 4];
46 read.read_exact(&mut buffer)?;
47 }
48
49 let _version = LittleReader::read_i32(read)?;
50
51 // comments
52 {
53 let mut buffer = [0u8; 128];
54 read.read_exact(&mut buffer)?;
55 }
56
57 let n_passes = LittleReader::read_i32(read)?;
58
59 let _digitizing_vector_flag = LittleReader::read_i32(read)?;
60
61 // reserved 92*i32
62 {
63 let mut buffer = [0u8; 368];
64 read.read_exact(&mut buffer)?;
65 }
66
67 for _ in 0..n_passes {
68 let n_lines = LittleReader::read_i32(read)?;
69 let _scanner_id = LittleReader::read_i32(read)?;
70
71 // reserved 14*i32
72 {
73 let mut buffer = [0u8; 56];
74 read.read_exact(&mut buffer)?;
75 }
76
77 for _ in 0..n_lines {
78 let n_points = LittleReader::read_i32(read)?;
79
80 // ijk 3*f32
81 {
82 let mut buffer = [0u8; 12];
83 read.read_exact(&mut buffer)?;
84 }
85
86 // reserved 12*i32
87 {
88 let mut buffer = [0u8; 48];
89 read.read_exact(&mut buffer)?;
90 }
91
92 for _ in 0..n_points {
93 let x = LittleReader::read_f32(read)?;
94 let y = LittleReader::read_f32(read)?;
95 let z = LittleReader::read_f32(read)?;
96
97 ip.push(P::new(x as f64, y as f64, z as f64));
98 }
99 }
100 }
101
102 Ok(())
103}
104
105//------------------------------------------------------------------------------
106
107/// Error type for .psl file operations
108pub enum PslError {
109 AccessFile,
110}
111
112/// Result type for .psl file operations
113pub type PslResult<T> = std::result::Result<T, PslError>;
114
115impl fmt::Debug for PslError {
116 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
117 match self {
118 Self::AccessFile => write!(f, "Unable to access file"),
119 }
120 }
121}
122
123impl fmt::Display for PslError {
124 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
125 write!(f, "{:?}", self)
126 }
127}
128
129impl From<ioError> for PslError {
130 fn from(_error: ioError) -> Self {
131 PslError::AccessFile
132 }
133}