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
mod classification;
mod format;
mod scan_direction;
pub use self::classification::Classification;
pub use self::format::Format;
pub use self::scan_direction::ScanDirection;
use raw;
use raw::point::Waveform;
use thiserror::Error;
use {Color, Result, Transform, Vector};
#[derive(Debug, Clone, Copy, Error)]
pub enum Error {
#[error("invalid classification: {0}")]
Classification(u8),
#[error("invalid format: {0}")]
Format(Format),
#[error("invalid format number: {0}")]
FormatNumber(u8),
#[error("overlap points are handled by the `is_overlap` member of `las::Point`, not by classifications")]
OverlapClassification,
#[error("invalid return number {return_number} for version {version:?}")]
#[allow(missing_docs)]
ReturnNumber {
return_number: u8,
version: Option<::Version>,
},
#[error("invalid scanner channel: {0}")]
ScannerChannel(u8),
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Point {
pub x: f64,
pub y: f64,
pub z: f64,
pub intensity: u16,
pub return_number: u8,
pub number_of_returns: u8,
pub scan_direction: ScanDirection,
pub is_edge_of_flight_line: bool,
pub classification: Classification,
pub is_synthetic: bool,
pub is_key_point: bool,
pub is_withheld: bool,
pub is_overlap: bool,
pub scanner_channel: u8,
pub scan_angle: f32,
pub user_data: u8,
pub point_source_id: u16,
pub gps_time: Option<f64>,
pub color: Option<Color>,
pub waveform: Option<Waveform>,
pub nir: Option<u16>,
pub extra_bytes: Vec<u8>,
}
impl Point {
pub fn new(mut raw_point: raw::Point, transforms: &Vector<Transform>) -> Point {
let is_overlap = raw_point.flags.is_overlap();
raw_point.flags.clear_overlap_class();
Point {
x: transforms.x.direct(raw_point.x),
y: transforms.y.direct(raw_point.y),
z: transforms.z.direct(raw_point.z),
intensity: raw_point.intensity,
return_number: raw_point.flags.return_number(),
number_of_returns: raw_point.flags.number_of_returns(),
scan_direction: raw_point.flags.scan_direction(),
is_edge_of_flight_line: raw_point.flags.is_edge_of_flight_line(),
classification: raw_point
.flags
.to_classification()
.expect("Overlap classification should have been cleared"),
is_synthetic: raw_point.flags.is_synthetic(),
is_key_point: raw_point.flags.is_key_point(),
is_withheld: raw_point.flags.is_withheld(),
is_overlap: is_overlap,
scan_angle: raw_point.scan_angle.into(),
scanner_channel: raw_point.flags.scanner_channel(),
user_data: raw_point.user_data,
point_source_id: raw_point.point_source_id,
gps_time: raw_point.gps_time,
color: raw_point.color,
waveform: raw_point.waveform,
nir: raw_point.nir,
extra_bytes: raw_point.extra_bytes,
}
}
pub fn into_raw(self, transforms: &Vector<Transform>) -> Result<raw::Point> {
Ok(raw::Point {
x: transforms.x.inverse(self.x)?,
y: transforms.y.inverse(self.y)?,
z: transforms.z.inverse(self.z)?,
intensity: self.intensity,
flags: self.flags()?,
scan_angle: self.scan_angle.into(),
user_data: self.user_data,
point_source_id: self.point_source_id,
gps_time: self.gps_time,
color: self.color,
waveform: self.waveform,
nir: self.nir,
extra_bytes: self.extra_bytes,
})
}
pub fn flags(&self) -> Result<raw::point::Flags> {
if self.return_number > 15 {
Err(Error::ReturnNumber {
return_number: self.return_number,
version: None,
}
.into())
} else if self.number_of_returns > 15 {
Err(Error::ReturnNumber {
return_number: self.number_of_returns,
version: None,
}
.into())
} else if self.scanner_channel > 3 {
Err(Error::ScannerChannel(self.scanner_channel).into())
} else {
let a = (self.number_of_returns << 4) + self.return_number;
let mut b = self.scanner_channel << 4;
if self.is_synthetic {
b += 1;
}
if self.is_key_point {
b += 2;
}
if self.is_withheld {
b += 4;
}
if self.is_overlap {
b += 8;
}
if self.scan_direction == ScanDirection::LeftToRight {
b += 64;
}
if self.is_edge_of_flight_line {
b += 128;
}
Ok(raw::point::Flags::ThreeByte(
a,
b,
self.classification.into(),
))
}
}
pub fn matches(&self, format: &Format) -> bool {
self.gps_time.is_some() == format.has_gps_time
&& self.color.is_some() == format.has_color
&& self.waveform.is_some() == format.has_waveform
&& self.nir.is_some() == format.has_nir
&& self.extra_bytes.len() == format.extra_bytes as usize
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn flags_invalid_return_number() {
assert!(Point {
return_number: 16,
..Default::default()
}
.flags()
.is_err());
}
#[test]
fn flags_invalid_number_of_returns() {
assert!(Point {
number_of_returns: 16,
..Default::default()
}
.flags()
.is_err());
}
#[test]
fn flags_invalid_scanner_channel() {
assert!(Point {
scanner_channel: 4,
..Default::default()
}
.flags()
.is_err());
}
#[test]
fn overlap() {
use raw::point::Flags;
let raw_point = raw::Point {
flags: Flags::TwoByte(0, 12),
..Default::default()
};
let point = Point::new(raw_point, &Default::default());
assert_eq!(Classification::Unclassified, point.classification);
assert!(point.is_overlap);
}
}