oxigdal-copc 0.1.4

Pure Rust COPC (Cloud Optimized Point Cloud) reader for OxiGDAL - LAS/LAZ format with spatial index
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
//! In-memory octree spatial index for point cloud data.
//!
//! The [`Octree`] partitions 3-D space recursively using [`OctreeNode`] cells,
//! each of which stores up to `max_points_per_node` points before being split
//! into eight children.  The tree supports bbox / sphere / k-NN queries, per-
//! classification filtering, and voxel-grid downsampling.

use std::collections::HashMap;

use crate::point::{BoundingBox3D, Point3D};

// ---------------------------------------------------------------------------
// OctreeNode
// ---------------------------------------------------------------------------

/// A single node in the octree.
///
/// Leaf nodes hold points directly; internal nodes delegate to eight children.
pub struct OctreeNode {
    /// Spatial extent covered by this node.
    pub bounds: BoundingBox3D,
    /// Points stored in this node (empty once split into children).
    pub points: Vec<Point3D>,
    /// Eight children, present only after this node has been split.
    pub children: Option<Box<[OctreeNode; 8]>>,
    /// Depth of this node within the tree (root = 0).
    pub depth: u8,
}

impl OctreeNode {
    /// Create an empty leaf node at the given depth.
    pub fn new(bounds: BoundingBox3D, depth: u8) -> Self {
        Self {
            bounds,
            points: Vec::new(),
            children: None,
            depth,
        }
    }

    /// Total number of points in this subtree (recursive).
    pub fn point_count(&self) -> usize {
        match &self.children {
            None => self.points.len(),
            Some(children) => {
                self.points.len() + children.iter().map(|c| c.point_count()).sum::<usize>()
            }
        }
    }

    /// `true` when this node has no children (i.e. is a leaf).
    #[inline]
    pub fn is_leaf(&self) -> bool {
        self.children.is_none()
    }

    /// Maximum depth reached anywhere in this subtree.
    pub fn depth_max(&self) -> u8 {
        match &self.children {
            None => self.depth,
            Some(children) => children
                .iter()
                .map(|c| c.depth_max())
                .max()
                .unwrap_or(self.depth),
        }
    }
}

// ---------------------------------------------------------------------------
// Octree
// ---------------------------------------------------------------------------

/// An in-memory octree index over a collection of [`Point3D`] values.
///
/// # Example
/// ```
/// use oxigdal_copc::point::{BoundingBox3D, Point3D};
/// use oxigdal_copc::octree::Octree;
///
/// let bounds = BoundingBox3D::new(0.0, 0.0, 0.0, 100.0, 100.0, 100.0).unwrap();
/// let mut tree = Octree::new(bounds);
/// tree.insert(Point3D::new(10.0, 20.0, 5.0));
/// assert_eq!(tree.len(), 1);
/// ```
pub struct Octree {
    /// Root node of the tree.
    root: OctreeNode,
    /// Maximum number of points in a leaf before it is split.
    max_points_per_node: usize,
    /// Maximum subdivision depth (nodes at this depth are never split).
    max_depth: u8,
    /// Cached total point count.
    total_points: usize,
}

impl Octree {
    /// Create a new empty octree covering `bounds`.
    ///
    /// Defaults: `max_points_per_node = 64`, `max_depth = 16`.
    pub fn new(bounds: BoundingBox3D) -> Self {
        Self {
            root: OctreeNode::new(bounds, 0),
            max_points_per_node: 64,
            max_depth: 16,
            total_points: 0,
        }
    }

    /// Builder: override the maximum points-per-leaf threshold.
    pub fn with_max_points(mut self, n: usize) -> Self {
        self.max_points_per_node = n;
        self
    }

    /// Builder: override the maximum tree depth.
    pub fn with_max_depth(mut self, depth: u8) -> Self {
        self.max_depth = depth;
        self
    }

    /// Total number of points stored in the tree.
    #[inline]
    pub fn len(&self) -> usize {
        self.total_points
    }

    /// `true` when the tree contains no points.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.total_points == 0
    }

    /// Insert a single point.
    ///
    /// Points that fall outside the root bounds are silently dropped — ensure
    /// the tree was constructed with a bounding box that covers the data.
    pub fn insert(&mut self, point: Point3D) {
        if !self.root.bounds.contains(&point) {
            return;
        }
        let max_pts = self.max_points_per_node;
        let max_d = self.max_depth;
        Self::node_insert(&mut self.root, point, max_pts, max_d);
        self.total_points += 1;
    }

    /// Insert a batch of points efficiently.
    pub fn insert_batch(&mut self, points: Vec<Point3D>) {
        for p in points {
            self.insert(p);
        }
    }

    // -----------------------------------------------------------------------
    // Queries
    // -----------------------------------------------------------------------

    /// Find all points inside `bbox`.
    pub fn query_bbox<'a>(&'a self, bbox: &BoundingBox3D) -> Vec<&'a Point3D> {
        let mut result = Vec::new();
        Self::node_query_bbox(&self.root, bbox, &mut result);
        result
    }

    /// Find all points within `radius` of centre `(cx, cy, cz)`.
    pub fn query_sphere(&self, cx: f64, cy: f64, cz: f64, radius: f64) -> Vec<&Point3D> {
        let r2 = radius * radius;
        // Build a bounding box for the sphere to prune branches.
        let sphere_bbox = BoundingBox3D {
            min_x: cx - radius,
            min_y: cy - radius,
            min_z: cz - radius,
            max_x: cx + radius,
            max_y: cy + radius,
            max_z: cz + radius,
        };
        let mut result = Vec::new();
        Self::node_query_sphere(&self.root, cx, cy, cz, r2, &sphere_bbox, &mut result);
        result
    }

    /// Find the `k` nearest neighbours to `(cx, cy, cz)`.
    ///
    /// Returns at most `min(k, self.len())` entries sorted by ascending distance.
    pub fn k_nearest(&self, cx: f64, cy: f64, cz: f64, k: usize) -> Vec<(&Point3D, f64)> {
        if k == 0 || self.is_empty() {
            return Vec::new();
        }

        // Heap-based k-NN search: we keep a max-heap of size k so that we can
        // prune entire octants whose minimum possible distance exceeds the
        // current k-th best.
        //
        // For simplicity we collect all points via a full traversal (the octree
        // already prunes many octants via `node_min_dist_sq`), then sort.
        let mut candidates: Vec<(&Point3D, f64)> = Vec::new();
        Self::node_collect_knn(&self.root, cx, cy, cz, k, f64::INFINITY, &mut candidates);

        candidates.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
        candidates.truncate(k);
        candidates
    }

    /// Return all points whose [`Point3D::classification`] equals `class`.
    pub fn by_classification(&self, class: u8) -> Vec<&Point3D> {
        let mut result = Vec::new();
        Self::node_collect_by_class(&self.root, class, &mut result);
        result
    }

    /// Compute aggregate statistics over all points in the tree.
    pub fn stats(&self) -> PointCloudStats {
        let mut all_points: Vec<&Point3D> = Vec::with_capacity(self.total_points);
        Self::node_collect_all(&self.root, &mut all_points);

        if all_points.is_empty() {
            return PointCloudStats {
                count: 0,
                bounds: None,
                mean_z: 0.0,
                std_z: 0.0,
                density: 0.0,
                classification_counts: HashMap::new(),
            };
        }

        let count = all_points.len();
        let sum_z: f64 = all_points.iter().map(|p| p.z).sum();
        let mean_z = sum_z / count as f64;
        let variance_z: f64 = all_points
            .iter()
            .map(|p| {
                let d = p.z - mean_z;
                d * d
            })
            .sum::<f64>()
            / count as f64;
        let std_z = variance_z.sqrt();

        let mut classification_counts: HashMap<u8, usize> = HashMap::new();
        for p in &all_points {
            *classification_counts.entry(p.classification).or_insert(0) += 1;
        }

        // Build bounding box from collected points.
        let bounds = {
            let first = all_points[0];
            let (mut min_x, mut min_y, mut min_z) = (first.x, first.y, first.z);
            let (mut max_x, mut max_y, mut max_z) = (first.x, first.y, first.z);
            for p in all_points.iter().skip(1) {
                if p.x < min_x {
                    min_x = p.x;
                }
                if p.y < min_y {
                    min_y = p.y;
                }
                if p.z < min_z {
                    min_z = p.z;
                }
                if p.x > max_x {
                    max_x = p.x;
                }
                if p.y > max_y {
                    max_y = p.y;
                }
                if p.z > max_z {
                    max_z = p.z;
                }
            }
            BoundingBox3D::new(min_x, min_y, min_z, max_x, max_y, max_z)
        };

        let density = if let Some(ref bb) = bounds {
            let dx = bb.max_x - bb.min_x;
            let dy = bb.max_y - bb.min_y;
            let xy_area = dx * dy;
            if xy_area > 0.0 {
                count as f64 / xy_area
            } else {
                0.0
            }
        } else {
            0.0
        };

        PointCloudStats {
            count,
            bounds,
            mean_z,
            std_z,
            density,
            classification_counts,
        }
    }

    /// Reduce the point cloud by retaining at most one point per axis-aligned
    /// voxel cell of size `voxel_size`.
    ///
    /// The retained point is the first one encountered in each voxel cell.
    /// Uses the tree's root bounds as the grid origin.
    pub fn voxel_downsample(&self, voxel_size: f64) -> Vec<Point3D> {
        if voxel_size <= 0.0 {
            return Vec::new();
        }

        let mut all_points: Vec<&Point3D> = Vec::with_capacity(self.total_points);
        Self::node_collect_all(&self.root, &mut all_points);

        let origin_x = self.root.bounds.min_x;
        let origin_y = self.root.bounds.min_y;
        let origin_z = self.root.bounds.min_z;

        let mut occupied: HashMap<(i64, i64, i64), ()> = HashMap::new();
        let mut result: Vec<Point3D> = Vec::new();

        for &p in &all_points {
            let ix = ((p.x - origin_x) / voxel_size).floor() as i64;
            let iy = ((p.y - origin_y) / voxel_size).floor() as i64;
            let iz = ((p.z - origin_z) / voxel_size).floor() as i64;
            let key = (ix, iy, iz);
            if occupied.insert(key, ()).is_none() {
                result.push((*p).clone());
            }
        }

        result
    }

    // -----------------------------------------------------------------------
    // Private helpers
    // -----------------------------------------------------------------------

    fn node_insert(node: &mut OctreeNode, point: Point3D, max_pts: usize, max_d: u8) {
        if node.is_leaf() {
            node.points.push(point);
            // Split if over threshold and not at maximum depth.
            if node.points.len() > max_pts && node.depth < max_d {
                Self::split_node(node, max_pts, max_d);
            }
        } else if let Some(children) = node.children.as_mut() {
            // Route into the child whose bounds contain the point.
            let child_idx = children.iter().enumerate().find_map(|(i, c)| {
                if c.bounds.contains(&point) {
                    Some(i)
                } else {
                    None
                }
            });

            if let Some(idx) = child_idx {
                Self::node_insert(&mut children[idx], point, max_pts, max_d);
            } else {
                // Fallback: point lies on a shared boundary.
                node.points.push(point);
            }
        } else {
            node.points.push(point);
        }
    }

    fn split_node(node: &mut OctreeNode, max_pts: usize, max_d: u8) {
        let octants = node.bounds.split_octants();
        let next_depth = node.depth.saturating_add(1);

        // Safety: we're constructing exactly 8 nodes from 8 octants.
        let children: Box<[OctreeNode; 8]> = Box::new([
            OctreeNode::new(octants[0].clone(), next_depth),
            OctreeNode::new(octants[1].clone(), next_depth),
            OctreeNode::new(octants[2].clone(), next_depth),
            OctreeNode::new(octants[3].clone(), next_depth),
            OctreeNode::new(octants[4].clone(), next_depth),
            OctreeNode::new(octants[5].clone(), next_depth),
            OctreeNode::new(octants[6].clone(), next_depth),
            OctreeNode::new(octants[7].clone(), next_depth),
        ]);

        node.children = Some(children);

        // Redistribute existing points into children.
        let old_points = std::mem::take(&mut node.points);
        // Separate into those that fit a child and those that stay in the node.
        let mut overflow: Vec<Point3D> = Vec::new();
        if let Some(children_ref) = node.children.as_mut() {
            for p in old_points {
                // Find the first child that contains this point.
                let child_idx = children_ref
                    .iter()
                    .enumerate()
                    .find_map(|(i, c)| if c.bounds.contains(&p) { Some(i) } else { None });

                if let Some(idx) = child_idx {
                    Self::node_insert(&mut children_ref[idx], p, max_pts, max_d);
                } else {
                    overflow.push(p);
                }
            }
        }
        node.points.extend(overflow);
    }

    fn node_query_bbox<'a>(
        node: &'a OctreeNode,
        bbox: &BoundingBox3D,
        result: &mut Vec<&'a Point3D>,
    ) {
        if !node.bounds.intersects_3d(bbox) {
            return;
        }
        for p in &node.points {
            if bbox.contains(p) {
                result.push(p);
            }
        }
        if let Some(children) = &node.children {
            for child in children.iter() {
                Self::node_query_bbox(child, bbox, result);
            }
        }
    }

    fn node_query_sphere<'a>(
        node: &'a OctreeNode,
        cx: f64,
        cy: f64,
        cz: f64,
        r2: f64,
        sphere_bbox: &BoundingBox3D,
        result: &mut Vec<&'a Point3D>,
    ) {
        if !node.bounds.intersects_3d(sphere_bbox) {
            return;
        }
        for p in &node.points {
            let dx = p.x - cx;
            let dy = p.y - cy;
            let dz = p.z - cz;
            if dx * dx + dy * dy + dz * dz <= r2 {
                result.push(p);
            }
        }
        if let Some(children) = &node.children {
            for child in children.iter() {
                Self::node_query_sphere(child, cx, cy, cz, r2, sphere_bbox, result);
            }
        }
    }

    /// Collect candidate points for k-NN using the current best-known
    /// distance as a pruning bound.
    fn node_collect_knn<'a>(
        node: &'a OctreeNode,
        cx: f64,
        cy: f64,
        cz: f64,
        k: usize,
        mut best_dist: f64,
        result: &mut Vec<(&'a Point3D, f64)>,
    ) {
        // Prune if the closest possible point in this node is farther than
        // the current worst among the k-best.
        if Self::node_min_dist_sq(node, cx, cy, cz) > best_dist * best_dist {
            return;
        }
        for p in &node.points {
            let dx = p.x - cx;
            let dy = p.y - cy;
            let dz = p.z - cz;
            let dist = (dx * dx + dy * dy + dz * dz).sqrt();
            result.push((p, dist));
            // Lazily update best_dist once we have k candidates.
            if result.len() >= k {
                // Keep partial max to tighten prune bound.
                if let Some(&(_, d)) = result
                    .iter()
                    .max_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal))
                {
                    if d < best_dist {
                        best_dist = d;
                    }
                }
            }
        }
        if let Some(children) = &node.children {
            for child in children.iter() {
                Self::node_collect_knn(child, cx, cy, cz, k, best_dist, result);
            }
        }
    }

    /// Squared minimum distance from the point `(cx,cy,cz)` to the nearest
    /// point on or inside `node.bounds`.
    fn node_min_dist_sq(node: &OctreeNode, cx: f64, cy: f64, cz: f64) -> f64 {
        let b = &node.bounds;
        let dx = if cx < b.min_x {
            b.min_x - cx
        } else if cx > b.max_x {
            cx - b.max_x
        } else {
            0.0
        };
        let dy = if cy < b.min_y {
            b.min_y - cy
        } else if cy > b.max_y {
            cy - b.max_y
        } else {
            0.0
        };
        let dz = if cz < b.min_z {
            b.min_z - cz
        } else if cz > b.max_z {
            cz - b.max_z
        } else {
            0.0
        };
        dx * dx + dy * dy + dz * dz
    }

    fn node_collect_by_class<'a>(node: &'a OctreeNode, class: u8, result: &mut Vec<&'a Point3D>) {
        for p in &node.points {
            if p.classification == class {
                result.push(p);
            }
        }
        if let Some(children) = &node.children {
            for child in children.iter() {
                Self::node_collect_by_class(child, class, result);
            }
        }
    }

    fn node_collect_all<'a>(node: &'a OctreeNode, result: &mut Vec<&'a Point3D>) {
        result.extend(node.points.iter());
        if let Some(children) = &node.children {
            for child in children.iter() {
                Self::node_collect_all(child, result);
            }
        }
    }
}

// ---------------------------------------------------------------------------
// PointCloudStats
// ---------------------------------------------------------------------------

/// Aggregate statistics over a point cloud.
pub struct PointCloudStats {
    /// Total number of points.
    pub count: usize,
    /// Tight bounding box of all points, or `None` if the cloud is empty.
    pub bounds: Option<BoundingBox3D>,
    /// Mean Z value.
    pub mean_z: f64,
    /// Population standard deviation of Z values.
    pub std_z: f64,
    /// Point density: points per unit area (XY plane).
    pub density: f64,
    /// Number of points per ASPRS classification code.
    pub classification_counts: HashMap<u8, usize>,
}