point-formats 0.3.0

Dependency-light LiDAR/point-cloud/mesh format conversion crate with explicit adapters for heavyweight formats.
Documentation
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
use crate::format::Format;
use std::collections::BTreeMap;

/// Three-dimensional vector or coordinate.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Vec3 {
    pub x: f64,
    pub y: f64,
    pub z: f64,
}

impl Vec3 {
    pub const ZERO: Self = Self {
        x: 0.0,
        y: 0.0,
        z: 0.0,
    };

    #[inline]
    pub const fn new(x: f64, y: f64, z: f64) -> Self {
        Self { x, y, z }
    }

    #[inline]
    pub fn is_finite(self) -> bool {
        self.x.is_finite() && self.y.is_finite() && self.z.is_finite()
    }

    #[inline]
    #[allow(clippy::should_implement_trait)]
    pub fn sub(self, other: Self) -> Self {
        Self::new(self.x - other.x, self.y - other.y, self.z - other.z)
    }

    #[inline]
    pub fn cross(self, other: Self) -> Self {
        Self::new(
            self.y * other.z - self.z * other.y,
            self.z * other.x - self.x * other.z,
            self.x * other.y - self.y * other.x,
        )
    }

    #[inline]
    pub fn dot(self, other: Self) -> f64 {
        self.x * other.x + self.y * other.y + self.z * other.z
    }

    #[inline]
    pub fn norm(self) -> f64 {
        self.dot(self).sqrt()
    }

    #[inline]
    pub fn normalized(self) -> Option<Self> {
        let norm = self.norm();
        if norm == 0.0 || !norm.is_finite() {
            None
        } else {
            Some(Self::new(self.x / norm, self.y / norm, self.z / norm))
        }
    }
}

/// RGB color stored as 16-bit components so LAS/E57 style precision is not
/// discarded when passing through richer formats.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Color {
    pub red: u16,
    pub green: u16,
    pub blue: u16,
}

impl Color {
    #[inline]
    pub const fn new(red: u16, green: u16, blue: u16) -> Self {
        Self { red, green, blue }
    }

    #[inline]
    pub const fn from_u8(red: u8, green: u8, blue: u8) -> Self {
        Self {
            red: red as u16,
            green: green as u16,
            blue: blue as u16,
        }
    }

    #[inline]
    pub fn to_u8_lossy(self) -> [u8; 3] {
        [
            self.red.min(255) as u8,
            self.green.min(255) as u8,
            self.blue.min(255) as u8,
        ]
    }

    #[inline]
    pub fn to_unit_rgb(self) -> [f64; 3] {
        [
            self.red as f64 / u16::MAX as f64,
            self.green as f64 / u16::MAX as f64,
            self.blue as f64 / u16::MAX as f64,
        ]
    }

    #[inline]
    pub fn from_unit_rgb(red: f64, green: f64, blue: f64) -> Option<Self> {
        fn component(v: f64) -> Option<u16> {
            if !v.is_finite() || !(0.0..=1.0).contains(&v) {
                return None;
            }
            Some((v * u16::MAX as f64).round() as u16)
        }
        Some(Self::new(
            component(red)?,
            component(green)?,
            component(blue)?,
        ))
    }
}

/// Dynamic per-point attribute retained by adapters that need fields outside
/// the normalized point model.
#[derive(Debug, Clone, PartialEq)]
pub enum AttributeValue {
    Int(i64),
    UInt(u64),
    Float(f64),
    Text(String),
}

/// Wrapper for point attributes to optimize memory footprint and allocations.
#[derive(Debug, Default, PartialEq)]
pub struct PointAttributes(pub Option<Box<BTreeMap<String, AttributeValue>>>);

impl Clone for PointAttributes {
    #[inline]
    fn clone(&self) -> Self {
        Self(self.0.as_ref().map(|map| Box::new((**map).clone())))
    }
}

impl std::ops::Deref for PointAttributes {
    type Target = BTreeMap<String, AttributeValue>;

    #[inline]
    fn deref(&self) -> &Self::Target {
        static EMPTY: std::sync::OnceLock<BTreeMap<String, AttributeValue>> =
            std::sync::OnceLock::new();
        let empty = EMPTY.get_or_init(BTreeMap::new);
        match &self.0 {
            Some(map) => map,
            None => empty,
        }
    }
}

impl std::ops::DerefMut for PointAttributes {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        if self.0.is_none() {
            self.0 = Some(Box::default());
        }
        self.0.as_mut().unwrap()
    }
}

impl From<BTreeMap<String, AttributeValue>> for PointAttributes {
    #[inline]
    fn from(map: BTreeMap<String, AttributeValue>) -> Self {
        if map.is_empty() {
            Self(None)
        } else {
            Self(Some(Box::new(map)))
        }
    }
}

impl From<PointAttributes> for BTreeMap<String, AttributeValue> {
    #[inline]
    fn from(attrs: PointAttributes) -> Self {
        match attrs.0 {
            Some(boxed) => *boxed,
            None => BTreeMap::new(),
        }
    }
}

/// Normalized LiDAR/point-cloud point.
#[derive(Debug, Clone, PartialEq)]
pub struct Point {
    pub position: Vec3,
    pub intensity: Option<f32>,
    pub color: Option<Color>,
    pub classification: Option<u8>,
    pub return_number: Option<u8>,
    pub number_of_returns: Option<u8>,
    pub gps_time: Option<f64>,
    pub scan_angle: Option<f32>,
    pub normal: Option<Vec3>,
    pub attributes: PointAttributes,
}

impl Point {
    #[inline]
    pub fn new(x: f64, y: f64, z: f64) -> Self {
        Self {
            position: Vec3::new(x, y, z),
            intensity: None,
            color: None,
            classification: None,
            return_number: None,
            number_of_returns: None,
            gps_time: None,
            scan_angle: None,
            normal: None,
            attributes: PointAttributes::default(),
        }
    }

    #[inline]
    pub fn with_intensity(mut self, intensity: f32) -> Self {
        self.intensity = Some(intensity);
        self
    }

    #[inline]
    pub fn with_color(mut self, color: Color) -> Self {
        self.color = Some(color);
        self
    }

    #[inline]
    pub fn with_classification(mut self, classification: u8) -> Self {
        self.classification = Some(classification);
        self
    }

    #[inline]
    pub fn with_normal(mut self, normal: Vec3) -> Self {
        self.normal = Some(normal);
        self
    }
}

/// Axis-aligned bounds for a point cloud or mesh.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Bounds3 {
    pub min: Vec3,
    pub max: Vec3,
}

impl Bounds3 {
    #[inline]
    pub fn empty() -> Self {
        Self {
            min: Vec3::new(f64::INFINITY, f64::INFINITY, f64::INFINITY),
            max: Vec3::new(f64::NEG_INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY),
        }
    }

    pub fn from_points<'a>(points: impl IntoIterator<Item = &'a Point>) -> Option<Self> {
        let mut bounds = Self::empty();
        let mut any = false;
        for point in points {
            bounds.include(point.position);
            any = true;
        }
        any.then_some(bounds)
    }

    pub fn from_vertices<'a>(vertices: impl IntoIterator<Item = &'a Vertex>) -> Option<Self> {
        let mut bounds = Self::empty();
        let mut any = false;
        for vertex in vertices {
            bounds.include(vertex.position);
            any = true;
        }
        any.then_some(bounds)
    }

    #[inline]
    pub fn include(&mut self, p: Vec3) {
        self.min.x = self.min.x.min(p.x);
        self.min.y = self.min.y.min(p.y);
        self.min.z = self.min.z.min(p.z);
        self.max.x = self.max.x.max(p.x);
        self.max.y = self.max.y.max(p.y);
        self.max.z = self.max.z.max(p.z);
    }
}

/// Metadata shared by point clouds and meshes. The crate stores CRS and scanner
/// transforms but does not invent them for formats that do not carry them.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Metadata {
    pub source_format: Option<Format>,
    pub point_count_hint: Option<usize>,
    pub crs_wkt: Option<String>,
    pub scanner_transform: Option<[[f64; 4]; 4]>,
    pub comments: Vec<String>,
    pub warnings: Vec<String>,
    pub attributes: BTreeMap<String, AttributeValue>,
}

/// Owned point cloud. Suitable for moderate-size conversion and tests. Large
/// production LAS/COPC/E57 adapters should implement streaming codecs using the
/// adapter traits in [`crate::adapters`].
#[derive(Debug, Clone, PartialEq)]
pub struct PointCloud {
    pub points: Vec<Point>,
    pub metadata: Metadata,
}

impl PointCloud {
    pub fn new(points: Vec<Point>) -> Self {
        Self {
            points,
            metadata: Metadata::default(),
        }
    }

    pub fn empty() -> Self {
        Self::new(Vec::new())
    }

    pub fn len(&self) -> usize {
        self.points.len()
    }

    pub fn is_empty(&self) -> bool {
        self.points.is_empty()
    }

    pub fn bounds(&self) -> Option<Bounds3> {
        Bounds3::from_points(&self.points)
    }

    pub fn has_color(&self) -> bool {
        self.points.iter().any(|p| p.color.is_some())
    }

    pub fn has_intensity(&self) -> bool {
        self.points.iter().any(|p| p.intensity.is_some())
    }

    pub fn has_classification(&self) -> bool {
        self.points.iter().any(|p| p.classification.is_some())
    }

    pub fn has_gps_time(&self) -> bool {
        self.points.iter().any(|p| p.gps_time.is_some())
    }

    pub fn has_normals(&self) -> bool {
        self.points.iter().any(|p| p.normal.is_some())
    }
}

/// Mesh vertex. The crate keeps optional color/normal because PLY/OBJ can carry
/// them, while STL only stores per-facet normals.
#[derive(Debug, Clone, PartialEq)]
pub struct Vertex {
    pub position: Vec3,
    pub normal: Option<Vec3>,
    pub color: Option<Color>,
}

impl Vertex {
    #[inline]
    pub fn new(position: Vec3) -> Self {
        Self {
            position,
            normal: None,
            color: None,
        }
    }
}

/// Triangle face using zero-based vertex indices.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Face {
    pub indices: [usize; 3],
}

impl Face {
    #[inline]
    pub const fn new(a: usize, b: usize, c: usize) -> Self {
        Self { indices: [a, b, c] }
    }
}

/// Triangle mesh.
#[derive(Debug, Clone, PartialEq)]
pub struct Mesh {
    pub vertices: Vec<Vertex>,
    pub faces: Vec<Face>,
    pub metadata: Metadata,
}

impl Mesh {
    #[inline]
    pub fn new(vertices: Vec<Vertex>, faces: Vec<Face>) -> Self {
        Self {
            vertices,
            faces,
            metadata: Metadata::default(),
        }
    }

    #[inline]
    pub fn bounds(&self) -> Option<Bounds3> {
        Bounds3::from_vertices(&self.vertices)
    }

    pub fn vertex_cloud(&self) -> PointCloud {
        let points = self
            .vertices
            .iter()
            .map(|vertex| {
                let mut point = Point::new(vertex.position.x, vertex.position.y, vertex.position.z);
                point.normal = vertex.normal;
                point.color = vertex.color;
                point
            })
            .collect();
        let mut metadata = self.metadata.clone();
        metadata.warnings.push(
            "mesh faces were discarded while converting vertices to a point cloud".to_string(),
        );
        PointCloud { points, metadata }
    }
}

/// Geometry returned by readers. Formats like PLY/OBJ can be either a point
/// cloud or mesh depending on whether face data is present.
#[derive(Debug, Clone, PartialEq)]
pub enum Geometry {
    PointCloud(PointCloud),
    Mesh(Mesh),
}

impl Geometry {
    pub fn point_count(&self) -> usize {
        match self {
            Self::PointCloud(cloud) => cloud.points.len(),
            Self::Mesh(mesh) => mesh.vertices.len(),
        }
    }

    pub fn face_count(&self) -> usize {
        match self {
            Self::PointCloud(_) => 0,
            Self::Mesh(mesh) => mesh.faces.len(),
        }
    }

    pub fn metadata(&self) -> &Metadata {
        match self {
            Self::PointCloud(cloud) => &cloud.metadata,
            Self::Mesh(mesh) => &mesh.metadata,
        }
    }

    pub fn metadata_mut(&mut self) -> &mut Metadata {
        match self {
            Self::PointCloud(cloud) => &mut cloud.metadata,
            Self::Mesh(mesh) => &mut mesh.metadata,
        }
    }
}