Skip to content

Commit

Permalink
Views on trait objects (#26)
Browse files Browse the repository at this point in the history
* Created extension traits for view methods of BorrowedBuffer and BorrowedMutBuffer

* Made OwningBuffer trait object safe

* Added example illustrating point buffer traits

* Allow slices for trait object buffers

* Fix for large LAS files. Fixed some documentation

* Fixed clippy warnings

---------

Co-authored-by: Pascal <[email protected]>
  • Loading branch information
Mortano and Pascal authored Jun 20, 2024
1 parent 1be6f0d commit edadc23
Show file tree
Hide file tree
Showing 38 changed files with 852 additions and 457 deletions.
2 changes: 1 addition & 1 deletion pasture-algorithms/benches/convexhull_bench.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use criterion::{criterion_group, criterion_main, Criterion};
use pasture_algorithms::convexhull;
use pasture_core::{
containers::{BorrowedMutBuffer, HashMapBuffer, VectorBuffer},
containers::{BorrowedMutBufferExt, HashMapBuffer, VectorBuffer},
layout::PointType,
nalgebra::Vector3,
};
Expand Down
2 changes: 1 addition & 1 deletion pasture-algorithms/examples/reprojection_example.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(not(target_arch = "wasm32"))]
mod ex {
use pasture_algorithms::reprojection::reproject_point_cloud_within;
use pasture_core::containers::{BorrowedBuffer, VectorBuffer};
use pasture_core::containers::{BorrowedBufferExt, VectorBuffer};
use pasture_core::nalgebra::Vector3;
use pasture_derive::PointType;

Expand Down
2 changes: 1 addition & 1 deletion pasture-algorithms/examples/segmentation_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use pasture_algorithms::segmentation::{
ransac_line_par, ransac_line_serial, ransac_plane_par, ransac_plane_serial,
};
use pasture_core::{
containers::{BorrowedMutBuffer, HashMapBuffer},
containers::{BorrowedMutBufferExt, HashMapBuffer},
layout::attributes::INTENSITY,
nalgebra::Vector3,
};
Expand Down
2 changes: 1 addition & 1 deletion pasture-algorithms/src/bounds.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::{Context, Result};
use pasture_core::{
containers::BorrowedBuffer,
containers::{BorrowedBuffer, BorrowedBufferExt},
layout::attributes::POSITION_3D,
math::AABB,
nalgebra::{Point3, Vector3},
Expand Down
4 changes: 2 additions & 2 deletions pasture-algorithms/src/convexhull.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::{anyhow, Result};
use pasture_core::containers::BorrowedBuffer;
use pasture_core::containers::{BorrowedBuffer, BorrowedBufferExt};
use pasture_core::{layout::attributes::POSITION_3D, nalgebra::Vector3};
use std::collections::{HashMap, HashSet};
use std::hash::{Hash, Hasher};
Expand Down Expand Up @@ -464,7 +464,7 @@ mod tests {
use crate::convexhull;
use anyhow::Result;
use pasture_core::{
containers::{BorrowedBuffer, BorrowedMutBuffer, HashMapBuffer},
containers::{BorrowedBuffer, BorrowedBufferExt, BorrowedMutBufferExt, HashMapBuffer},
layout::attributes::POSITION_3D,
layout::PointType,
nalgebra::Vector3,
Expand Down
2 changes: 1 addition & 1 deletion pasture-algorithms/src/minmax.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use pasture_core::{
containers::BorrowedBuffer,
containers::{BorrowedBuffer, BorrowedBufferExt},
layout::{PointAttributeDefinition, PrimitiveType},
math::MinMax,
};
Expand Down
4 changes: 3 additions & 1 deletion pasture-algorithms/src/normal_estimation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
use core::panic;
use kd_tree::{self, KdPoint, KdTree};
use num_traits::{self};
use pasture_core::containers::{BorrowedBuffer, BorrowedMutBuffer, HashMapBuffer, OwningBuffer};
use pasture_core::containers::{
BorrowedBuffer, BorrowedBufferExt, BorrowedMutBufferExt, HashMapBuffer, OwningBuffer,
};
use pasture_core::layout::{attributes::POSITION_3D, PointType};
use pasture_core::nalgebra::{DMatrix, Vector3};
use std::result::Result;
Expand Down
6 changes: 3 additions & 3 deletions pasture-algorithms/src/reprojection.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::ffi::CString;

use anyhow::Result;
use pasture_core::containers::BorrowedMutBuffer;
use pasture_core::containers::{BorrowedBufferExt, BorrowedMutBuffer, BorrowedMutBufferExt};
use pasture_core::math::AABB;
use pasture_core::nalgebra::{Point3, Vector3};

Expand Down Expand Up @@ -272,7 +272,7 @@ mod tests {

reproject_point_cloud_within(&mut interleaved, "EPSG:4326", "EPSG:3309");

let results = vec![
let results = [
Vector3::new(12185139.590523569, 7420953.944297638, 0.0),
Vector3::new(11104667.534080556, 7617693.973680517, 0.0),
Vector3::new(11055663.927418157, 5832081.512011217, 2.0),
Expand Down Expand Up @@ -318,7 +318,7 @@ mod tests {

reproject_point_cloud_between(&mut interleaved, &mut attribute, "EPSG:4326", "EPSG:3309");

let results = vec![
let results = [
Vector3::new(12185139.590523569, 7420953.944297638, 0.0),
Vector3::new(11104667.534080556, 7617693.973680517, 0.0),
Vector3::new(11055663.927418157, 5832081.512011217, 2.0),
Expand Down
2 changes: 1 addition & 1 deletion pasture-algorithms/src/segmentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::vec;

use pasture_core::{
layout::attributes::POSITION_3D,
nalgebra::Vector3, containers::BorrowedBuffer,
nalgebra::Vector3, containers::{BorrowedBuffer, BorrowedBufferExt},
};
use rand::Rng;
use rayon::prelude::*;
Expand Down
12 changes: 7 additions & 5 deletions pasture-algorithms/src/voxel_grid.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::{collections::HashMap, u16};

use pasture_core::{
containers::{BorrowedBuffer, OwningBuffer, UntypedPoint, UntypedPointBuffer},
containers::{
BorrowedBuffer, BorrowedBufferExt, OwningBuffer, UntypedPoint, UntypedPointBuffer,
},
layout::{
attributes::{self, POSITION_3D},
PointAttributeDataType, PointAttributeDefinition, PointLayout,
Expand All @@ -19,9 +21,9 @@ pub struct Voxel {
/// finds leaf of point p by iterating over the marked axis
fn find_leaf(
p: Vector3<f64>,
markers_x: &Vec<f64>,
markers_y: &Vec<f64>,
markers_z: &Vec<f64>,
markers_x: &[f64],
markers_y: &[f64],
markers_z: &[f64],
) -> (usize, usize, usize) {
let mut index_x = 0;
let mut index_y = 0;
Expand Down Expand Up @@ -691,7 +693,7 @@ mod tests {

use crate::voxel_grid::voxelgrid_filter;
use pasture_core::{
containers::{BorrowedBuffer, HashMapBuffer, MakeBufferFromLayout},
containers::{BorrowedBuffer, BorrowedBufferExt, HashMapBuffer, MakeBufferFromLayout},
layout::attributes,
nalgebra::Vector3,
};
Expand Down
4 changes: 2 additions & 2 deletions pasture-core/benches/layout_conversion_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use criterion::{criterion_group, criterion_main, Criterion};
use nalgebra::Vector3;
use pasture_core::{
containers::{
ColumnarBuffer, HashMapBuffer, InterleavedBuffer, MakeBufferFromLayout, OwningBuffer,
VectorBuffer,
BorrowedBufferExt, ColumnarBuffer, HashMapBuffer, InterleavedBuffer, MakeBufferFromLayout,
OwningBuffer, VectorBuffer,
},
layout::{conversion::BufferLayoutConverter, PointType},
};
Expand Down
15 changes: 13 additions & 2 deletions pasture-core/benches/point_buffer_iterators_bench.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use criterion::{criterion_group, criterion_main, Criterion};
use pasture_core::{
containers::{
BorrowedBuffer, BorrowedMutBuffer, ColumnarBuffer, HashMapBuffer, InterleavedBuffer,
VectorBuffer,
BorrowedBuffer, BorrowedBufferExt, BorrowedMutBufferExt, ColumnarBuffer, HashMapBuffer,
InterleavedBuffer, VectorBuffer,
},
layout::attributes::POSITION_3D,
layout::PointType,
Expand Down Expand Up @@ -109,6 +109,12 @@ fn points_ref_iterator_performance_small_type<'a>(buffer: &'a impl InterleavedBu
}
}

fn points_ref_iterator_performance_with_trait_object<'a>(buffer: &'a dyn InterleavedBuffer<'a>) {
for point in buffer.view::<CustomPointTypeSmall>().iter() {
criterion::black_box(point.position);
}
}

fn attribute_iterator_performance_opaque_buffer<'a, T: PrimitiveType + Default>(
buffer: &'a impl BorrowedBuffer<'a>,
attribute: &PointAttributeDefinition,
Expand Down Expand Up @@ -194,6 +200,11 @@ fn bench(c: &mut Criterion) {
c.bench_function("points_ref_iterator_small_type", |b| {
b.iter(|| points_ref_iterator_performance_small_type(&dummy_points_small_interleaved))
});
c.bench_function("points_ref_iterator_small_type_with_trait_object", |b| {
b.iter(|| {
points_ref_iterator_performance_with_trait_object(&dummy_points_small_interleaved)
})
});

c.bench_function("attribute_iterator_interleaved_opaque_buffer", |b| {
b.iter(|| {
Expand Down
15 changes: 14 additions & 1 deletion pasture-core/examples/basic_point_buffers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use pasture_core::containers::{
BorrowedBuffer, BorrowedMutBuffer, HashMapBuffer, MakeBufferFromLayout, SliceBuffer,
BorrowedBufferExt, BorrowedMutBufferExt, HashMapBuffer, MakeBufferFromLayout, SliceBuffer,
SliceBufferMut,
};
use pasture_core::nalgebra::Vector3;
Expand Down Expand Up @@ -70,6 +70,19 @@ fn main() {
// is what 'interleaved' means: We can get (mutable) references to the strongly typed point data, since all data
// for a single point is stored contiguously in memory!

// We can also access individual attributes by using `view_attribute`:
for position in buffer.view_attribute::<Vector3<f64>>(&POSITION_3D) {
println!("{position:?}");
}

buffer.transform_attribute(&INTENSITY, |_index: usize, intensity: u16| -> u16 {
intensity * 2
});

// Where interleaved memory layout allows references to whole points, it disallows references to individual
// attribute values (as these might not be correctly aligned). Therefore, we can only access attributes by
// value using a `VectorBuffer`, which requires a copy operation

// Just like arrays and vectors, our buffers can also be sliced. Unfortunately, the current constraints of the `Index`
// trait prevent us from implementing it for the pasture point buffers, so we can't slice our buffers using the
// `[range]` syntax. Instead, use the `slice` and `slice_mut` methods:
Expand Down
2 changes: 1 addition & 1 deletion pasture-core/examples/layout_conversion.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use pasture_core::containers::{BorrowedBuffer, OwningBuffer, VectorBuffer};
use pasture_core::containers::{BorrowedBuffer, BorrowedBufferExt, OwningBuffer, VectorBuffer};
use pasture_core::layout::attributes::{COLOR_RGB, POSITION_3D};
use pasture_core::layout::conversion::BufferLayoutConverter;
use pasture_core::layout::{PointAttributeDataType, PointType};
Expand Down
Loading

0 comments on commit edadc23

Please sign in to comment.