rust_3d/io/las/
types.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 types for the .las file format
24
25use std::{
26    convert::{TryFrom, TryInto},
27    fmt,
28    io::Error as ioError,
29};
30
31use super::super::from_bytes::*;
32
33//------------------------------------------------------------------------------
34
35#[derive(Debug)]
36pub struct Header {
37    pub offset_point_data: u32,
38    pub point_record_length: u16,
39    pub n_point_records: u64,
40    pub scale_factor_x: f64,
41    pub scale_factor_y: f64,
42    pub scale_factor_z: f64,
43    pub offset_x: f64,
44    pub offset_y: f64,
45    pub offset_z: f64,
46}
47
48impl TryFrom<HeaderRaw> for Header {
49    type Error = LasError;
50
51    fn try_from(x: HeaderRaw) -> LasResult<Header> {
52        if x.version_major > 1 || x.version_minor > 4 {
53            return Err(LasError::UnsupportedVersion);
54        }
55
56        // These are conversions according to the legacy mode
57        let n_point_records = if x.legacy_n_point_records == 0 {
58            x.n_point_records
59        } else {
60            x.legacy_n_point_records as u64
61        };
62
63        if x.point_record_format > 10 {
64            return Err(LasError::UnknownPointFormat);
65        }
66
67        Ok(Header {
68            offset_point_data: x.offset_point_data,
69            point_record_length: x.point_record_length,
70            n_point_records,
71            scale_factor_x: x.scale_factor_x,
72            scale_factor_y: x.scale_factor_y,
73            scale_factor_z: x.scale_factor_z,
74            offset_x: x.offset_x,
75            offset_y: x.offset_y,
76            offset_z: x.offset_z,
77        })
78    }
79}
80
81//------------------------------------------------------------------------------
82
83#[derive(Debug)]
84// This header reflects the 1.4 spec
85pub struct HeaderRaw {
86    //COUNT_BYTES_THIS COUNT_BYTES_TOTAL_HERE
87    pub file_signature: [u8; 4],             //4 4 'signed' char in spec
88    pub file_source_id: u16,                 //2 6
89    pub global_encoding: u16,                //2 8
90    pub guid1: u32,                          //4 12
91    pub guid2: u16,                          //2 14
92    pub guid3: u16,                          //2 16
93    pub guid4: [u8; 8],                      //8 24
94    pub version_major: u8,                   //1 25
95    pub version_minor: u8,                   //1 26
96    pub system_identifier: [u8; 32],         //32 58 'signed' char in spec
97    pub generating_software: [u8; 32],       //32 90 'signed' char in spec
98    pub file_creation_day: u16,              //2 92
99    pub file_creation_year: u16,             //2 94
100    pub header_size: u16,                    //2 96
101    pub offset_point_data: u32,              //4 100
102    pub n_variable_length_records: u32,      //4 104
103    pub point_record_format: u8,             //1 105
104    pub point_record_length: u16,            //2 107
105    pub legacy_n_point_records: u32,         //4 111
106    pub legacy_n_point_return: [u32; 5],     //20 131
107    pub scale_factor_x: f64,                 //8 139
108    pub scale_factor_y: f64,                 //8 147
109    pub scale_factor_z: f64,                 //8 155
110    pub offset_x: f64,                       //8 163
111    pub offset_y: f64,                       //8 171
112    pub offset_z: f64,                       //8 179
113    pub max_x: f64,                          //8 187
114    pub min_x: f64,                          //8 195
115    pub max_y: f64,                          //8 203
116    pub min_y: f64,                          //8 211
117    pub max_z: f64,                          //8 219
118    pub min_z: f64,                          //8 227
119    pub start_wavefront_data: u64,           //8 235
120    pub start_extended_variable_length: u64, //8 243
121    pub n_extended_variable_length: u32,     //4 247
122    pub n_point_records: u64,                //8 255
123    pub n_points_return: [u64; 15],          //120 375
124}
125
126//------------------------------------------------------------------------------
127
128#[derive(Debug, Default, Clone)]
129pub struct PointData {
130    pub x: i32, //4 4
131    pub y: i32, //4 8
132    pub z: i32, //4 12
133}
134
135impl PointData {
136    pub fn from_bytes(buffer: [u8; 12]) -> Self {
137        // safe unwraps since buffer size is enforced at compile time
138        Self {
139            x: i32::from_le_bytes(buffer[0..4].try_into().unwrap()),
140            y: i32::from_le_bytes(buffer[4..8].try_into().unwrap()),
141            z: i32::from_le_bytes(buffer[8..12].try_into().unwrap()),
142        }
143    }
144}
145
146//------------------------------------------------------------------------------
147
148/// Error type for .las file operation
149pub enum LasError {
150    AccessFile,
151    BinaryData,
152    UnknownPointFormat,
153    UnsupportedVersion,
154}
155
156impl fmt::Debug for LasError {
157    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
158        match self {
159            Self::AccessFile => write!(f, "Unable to access file"),
160            Self::BinaryData => write!(f, "Unable to parse binary data"),
161            Self::UnknownPointFormat => write!(f, "Unknown point format"),
162            Self::UnsupportedVersion => write!(f, "Unsupported version"),
163        }
164    }
165}
166
167impl fmt::Display for LasError {
168    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
169        write!(f, "{:?}", self)
170    }
171}
172
173/// Result type for .las file operation
174pub type LasResult<T> = std::result::Result<T, LasError>;
175
176impl From<ioError> for LasError {
177    fn from(_error: ioError) -> Self {
178        LasError::AccessFile
179    }
180}
181
182impl From<std::array::TryFromSliceError> for LasError {
183    fn from(_error: std::array::TryFromSliceError) -> Self {
184        LasError::BinaryData
185    }
186}
187
188impl From<FromBytesError> for LasError {
189    fn from(_error: FromBytesError) -> Self {
190        LasError::BinaryData
191    }
192}