Basic usage of pasture
In a nutshell, pasture
provides data structures for point cloud data and functions to interact with these data structures. Think Vec<T>
, but for arbitrary points whose format is stored at runtime. The following example shows how point data is read from a file and how individual points and attributes are accessed:
use anyhow::{bail, Context, Result}; use pasture_core::{ containers::{BorrowedBuffer, VectorBuffer}, layout::attributes::POSITION_3D, nalgebra::Vector3, }; use pasture_io::base::{read_all}; fn main() -> Result<()> { // Reading a point cloud file is as simple as calling `read_all` let points = read_all::<VectorBuffer, _>("pointcloud.las").context("Failed to read points")?; if points.point_layout().has_attribute(&POSITION_3D) { for position in points .view_attribute::<Vector3<f64>>(&POSITION_3D) .into_iter() .take(10) { println!("({};{};{})", position.x, position.y, position.z); } } else { bail!("Point cloud file has no positions!"); } Ok(()) }
If you want to create a buffer for points yourself, you need to specify a PointLayout
manually, which pasture
can derive from a struct
definition like in the following example:
/// This point type uses the #[derive(PointType)] macro to auto-generate an appropriate PointLayout #[repr(C, packed)] #[derive(Copy, Clone, PointType, Debug, bytemuck::NoUninit, bytemuck::AnyBitPattern)] struct SimplePoint { #[pasture(BUILTIN_POSITION_3D)] pub position: Vector3<f64>, #[pasture(BUILTIN_INTENSITY)] pub intensity: u16, } fn main() { // Create some points let points = vec![ SimplePoint { position: Vector3::new(1.0, 2.0, 3.0), intensity: 42, }, SimplePoint { position: Vector3::new(-1.0, -2.0, -3.0), intensity: 84, }, ]; // A VectorBuffer can be created from a PointLayout let mut buffer = VectorBuffer::new_from_layout(SimplePoint::layout()); // We can push individual points through a (mutable) PointView: buffer.view_mut().push_point(points[0]); buffer.view_mut().push_point(points[1]); // A more elegant solution is to collect the data from an iterator: buffer = points.iter().copied().collect::<VectorBuffer>(); }