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
//! las points.

use Result;
use error::Error;

/// A las point.
#[derive(Debug, Default, PartialEq)]
pub struct Point {
    /// The x value of the point, as a f64.
    pub x: f64,
    /// The y value of the point, as a f64.
    pub y: f64,
    /// The z value of the point, as a f64.
    pub z: f64,
    /// The pulse return magnitude.
    ///
    /// This value is system-specific.
    pub intensity: u16,
    /// The pulse return number for this point's pulse.
    pub return_number: ReturnNumber,
    /// The total number of returns in this point's pulse.
    pub number_of_returns: NumberOfReturns,
    /// The scan direction of the mirror, forwards or backwards.
    pub scan_direction: ScanDirection,
    /// Is this point at the edge of a flight line?
    pub edge_of_flight_line: bool,
    /// The classification of this point.
    ///
    /// ASPRS defines some integer-to-classification mappings.
    pub classification: Classification,
    /// Was this point created by some other means than LiDAR?
    pub synthetic: bool,
    /// Is this point a key point?
    ///
    /// If so, try not to thin it.
    pub key_point: bool,
    /// Should this point be included in processing?
    ///
    /// A.k.a. "deleted".
    pub withheld: bool,
    /// The truncated integer value of the scan angle.
    ///
    /// Negative values are left.
    pub scan_angle_rank: i8,
    /// Data used at the user's discretion.
    pub user_data: u8,
    /// The point source id.
    pub point_source_id: u16,
    /// The GPS time this point was collected.
    ///
    /// Optional, does not exist in all point formats.
    pub gps_time: Option<f64>,
    /// The red image channel, optional.
    pub red: Option<u16>,
    /// The green image channel, optional.
    pub green: Option<u16>,
    /// The blue image channel, optional.
    pub blue: Option<u16>,
    /// Any extra bytes that were included in the point record.
    ///
    /// These are legal under the standard, but they break many readers.
    pub extra_bytes: Option<Vec<u8>>,
}

impl Point {
    /// Creates a new point.
    ///
    /// # Examples
    ///
    /// ```
    /// use las::point::Point;
    /// let point = Point::new();
    /// ```
    pub fn new() -> Point {
        Default::default()
    }
}

/// A custom wrapper to represent a point's return number.
///
/// Since the number has an upper bound, we use this wrapper to ensure those bounds.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct ReturnNumber(u8);

impl ReturnNumber {
    /// Creates a return number from a u8.
    ///
    /// # Examples
    ///
    /// ```
    /// use las::point::ReturnNumber;
    /// assert!(ReturnNumber::from_u8(1).is_ok());
    /// assert!(ReturnNumber::from_u8(6).is_err());
    /// ```
    pub fn from_u8(n: u8) -> Result<ReturnNumber> {
        if n < 6 {
            Ok(ReturnNumber(n))
        } else {
            Err(Error::InvalidReturnNumber(n))
        }
    }

    /// Returns this return number as a u8.
    ///
    /// # Examples
    ///
    /// ```
    /// use las::point::ReturnNumber;
    /// assert_eq!(1, ReturnNumber::from_u8(1).unwrap().as_u8());
    /// ```
    pub fn as_u8(&self) -> u8 {
        self.0
    }
}

/// A custom wrapper to represent a point's number of returns.
///
/// Since the number has an upper bound, we use this wrapper to ensure those bounds.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct NumberOfReturns(u8);

impl NumberOfReturns {
    /// Creates a number of returns from a u8.
    ///
    /// # Examples
    ///
    /// ```
    /// use las::point::NumberOfReturns;
    /// assert!(NumberOfReturns::from_u8(0).is_ok());
    /// assert!(NumberOfReturns::from_u8(6).is_err());
    /// ```
    pub fn from_u8(n: u8) -> Result<NumberOfReturns> {
        if n < 6 {
            Ok(NumberOfReturns(n))
        } else {
            Err(Error::InvalidNumberOfReturns(n))
        }
    }

    /// Returns this NumberOfReturns as a u8.
    ///
    /// # Examples
    ///
    /// ```
    /// use las::point::NumberOfReturns;
    /// assert_eq!(1, NumberOfReturns::from_u8(1).unwrap().as_u8());
    /// ```
    pub fn as_u8(&self) -> u8 {
        self.0
    }
}

/// An enum to represent scan direction.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ScanDirection {
    /// The mirror is traveling in a negative direction, right to left.
    Backward = 0,
    /// The mirror is traveling in a positive direction, left to right.
    Forward = 1,
}

impl ScanDirection {
    /// Converts this scan direction to a u8.
    ///
    /// # Examples
    ///
    /// ```
    /// use las::point::ScanDirection;
    /// let forward = ScanDirection::Forward;
    /// assert_eq!(1, forward.as_u8());
    /// ```
    pub fn as_u8(&self) -> u8 {
        match *self {
            ScanDirection::Forward => 1,
            ScanDirection::Backward => 0,
        }
    }
}

impl Default for ScanDirection {
    fn default() -> ScanDirection {
        ScanDirection::Backward
    }
}

impl From<bool> for ScanDirection {
    fn from(b: bool) -> ScanDirection {
        if b {
            ScanDirection::Forward
        } else {
            ScanDirection::Backward
        }
    }
}

/// An enum to represent classifications.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Classification {
    /// A point is unclassified, and we haven't even tried.
    CreatedNeverClassified,
    /// This point could not be classified.
    Unclassified,
    /// Ground point.
    Ground,
    /// A low vegetation point, such as shrubbery.
    LowVegetation,
    /// Medium vegetation, like chaperelle.
    MediumVegetation,
    /// High vegetation, like forest.
    HighVegetation,
    /// A man-made building.
    Building,
    /// A noise point.
    LowPoint,
    /// A mass point, pretty synthetic.
    ModelKeyPoint,
    /// Water.
    Water,
    /// These points were culled when merging overlapping flight lines.
    Overlap,
    /// Reserved for ASPRS definition.
    ///
    /// There are several numerical values associated with `Reserved.`
    Reserved(u8),
}

impl Classification {
    /// Creates a classification from a u8.
    ///
    /// # Examples
    ///
    /// ```
    /// use las::point::Classification;
    /// assert_eq!(Classification::Ground, Classification::from_u8(2).unwrap());
    /// assert!(Classification::from_u8(127).is_err());
    /// ```
    pub fn from_u8(n: u8) -> Result<Classification> {
        Ok(match n {
            0 => Classification::CreatedNeverClassified,
            1 => Classification::Unclassified,
            2 => Classification::Ground,
            3 => Classification::LowVegetation,
            4 => Classification::MediumVegetation,
            5 => Classification::HighVegetation,
            6 => Classification::Building,
            7 => Classification::LowPoint,
            8 => Classification::ModelKeyPoint,
            9 => Classification::Water,
            12 => Classification::Overlap,
            10 | 11 | 13...31 => Classification::Reserved(n),
            _ => return Err(Error::InvalidClassification(n)),
        })
    }

    /// Returns this classifications u8 value.
    ///
    /// # Examples
    ///
    /// ```
    /// use las::point::Classification;
    /// let classification = Classification::Ground;
    /// assert_eq!(2, classification.as_u8());
    /// ```
    pub fn as_u8(&self) -> u8 {
        match *self {
            Classification::CreatedNeverClassified => 0,
            Classification::Unclassified => 1,
            Classification::Ground => 2,
            Classification::LowVegetation => 3,
            Classification::MediumVegetation => 4,
            Classification::HighVegetation => 5,
            Classification::Building => 6,
            Classification::LowPoint => 7,
            Classification::ModelKeyPoint => 8,
            Classification::Water => 9,
            Classification::Overlap => 12,
            Classification::Reserved(n) => n,
        }
    }
}

impl Default for Classification {
    fn default() -> Classification {
        Classification::CreatedNeverClassified
    }
}