Skip to content

Commit

Permalink
avoid bulk import
Browse files Browse the repository at this point in the history
  • Loading branch information
stelzo committed Apr 29, 2024
1 parent 4cc0b21 commit 4002f9c
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 58 deletions.
52 changes: 0 additions & 52 deletions src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,68 +149,16 @@ pub(crate) fn check_coord(
/// pub intensity: f32,
/// }
///
/// impl From<MyPointXYZI> for Point<f32, 3, 1> {
/// fn from(point: MyPointXYZI) -> Self {
/// Point {
/// coords: [point.x, point.y, point.z],
/// meta: [point.intensity.into()],
/// }
/// }
/// }
///
/// impl From<Point<f32, 3, 1>> for MyPointXYZI {
/// fn from(point: Point<f32, 3, 1>) -> Self {
/// Self {
/// x: point.coords[0],
/// y: point.coords[1],
/// z: point.coords[2],
/// intensity: point.meta[0].get(),
/// }
/// }
/// }
///
/// impl MetaNames<1> for MyPointXYZI {
/// fn meta_names() -> [&'static str; 1] {
/// ["intensity"]
/// }
/// }
///
/// impl PointConvertible<f32, {size_of!(f32)}, 3, 1> for MyPointXYZI {}
/// ```
pub trait MetaNames<const METADIM: usize> {
fn meta_names() -> [&'static str; METADIM];
}

#[inline(always)]
pub(crate) fn add_point_to_byte_buffer<
T: FromBytes,
const SIZE: usize,
const DIM: usize,
const METADIM: usize,
C: PointConvertible<T, SIZE, DIM, METADIM>,
>(
coords: C,
cloud: &mut PointCloud2Msg,
) -> Result<bool, ConversionError> {
let point: Point<T, DIM, METADIM> = coords.into();

// (x, y, z...)
point
.coords
.iter()
.for_each(|x| cloud.data.extend_from_slice(T::bytes(x).as_slice()));

// meta data description
point.meta.iter().for_each(|meta| {
let truncated_bytes = &meta.bytes[0..meta.datatype.size()];
cloud.data.extend_from_slice(truncated_bytes);
});

cloud.width += 1;

Ok(true)
}

/// This trait is used to convert a byte slice to a primitive type.
/// All PointField types are supported.
pub trait FromBytes: Default + Sized + Copy + GetFieldDatatype {
Expand Down
52 changes: 48 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ pub mod writer;
pub use convert::MetaNames;
pub use ros_types::PointCloud2Msg;

use crate::convert::*;
use crate::pcl_utils::*;
use crate::convert::{
FieldDatatype,
FromBytes,
};

use crate::ros_types::PointFieldMsg;

/// All errors that can occur for creating Reader and Writer.
Expand All @@ -112,7 +115,7 @@ pub enum ConversionError {

/// Internal point representation. It is used to store the coordinates and meta data of a point.
/// In each iteration, the Reader will convert the internal representation to the desired point type.
/// Implement the `From` traits for your point type to use the conversion as part of the PointConvertible trait.
/// Implement the `From` traits for your point type to use the conversion as part of the [`PointConvertible`] trait.
///
/// # Example
/// ```
Expand Down Expand Up @@ -153,6 +156,47 @@ pub struct Point<T, const DIM: usize, const METADIM: usize> {

/// Trait to convert a point to a tuple of coordinates and meta data.
/// Implement this trait for your point type to use the conversion.
///
/// # Example
/// ```
/// use ros_pointcloud2::{Point, PointConvertible, MetaNames, size_of};
///
/// #[derive(Clone, Debug, PartialEq, Copy)]
/// pub struct MyPointXYZI {
/// pub x: f32,
/// pub y: f32,
/// pub z: f32,
/// pub intensity: f32,
/// }
///
/// impl From<MyPointXYZI> for Point<f32, 3, 1> {
/// fn from(point: MyPointXYZI) -> Self {
/// Point {
/// coords: [point.x, point.y, point.z],
/// meta: [point.intensity.into()],
/// }
/// }
/// }
///
/// impl From<Point<f32, 3, 1>> for MyPointXYZI {
/// fn from(point: Point<f32, 3, 1>) -> Self {
/// Self {
/// x: point.coords[0],
/// y: point.coords[1],
/// z: point.coords[2],
/// intensity: point.meta[0].get(),
/// }
/// }
/// }
///
/// impl MetaNames<1> for MyPointXYZI {
/// fn meta_names() -> [&'static str; 1] {
/// ["intensity"]
/// }
/// }
///
/// impl PointConvertible<f32, {size_of!(f32)}, 3, 1> for MyPointXYZI {}
/// ```
pub trait PointConvertible<T, const SIZE: usize, const DIM: usize, const METADIM: usize>:
From<Point<T, DIM, METADIM>> + Into<Point<T, DIM, METADIM>> + MetaNames<METADIM> + Clone + 'static
where
Expand Down Expand Up @@ -189,7 +233,7 @@ impl Default for PointMeta {
}

impl PointMeta {
/// Create a new PointMeta from a value
/// Create a new PointMeta from a value.
///
/// # Example
/// ```
Expand Down
17 changes: 16 additions & 1 deletion src/reader.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
use crate::*;
use crate::{
pcl_utils::*,
Point,
PointCloud2Msg,
PointConvertible,
ConversionError,
MetaNames,
PointMeta,
convert::{
FromBytes,
FieldDatatype,
load_loadable,
Endianness,
check_coord,
},
};

/// Convenience type for a Reader that reads coordinates as f32. Specify the number of dimensions, metadata dimensions and C, the point type.
pub type ReaderF32<const DIM: usize, const METADIM: usize, C> =
Expand Down
43 changes: 42 additions & 1 deletion src/writer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
use crate::*;
use crate::{
pcl_utils::*,
Point,
PointCloud2Msg,
PointConvertible,
ConversionError,
ros_types::PointFieldMsg,
convert::{
FromBytes,
FieldDatatype,
},
};

/// Convenience type for a Writer that reads coordinates as f32. Specify the number of dimensions, metadata dimensions and C, the point type.
pub type WriterF32<const DIM: usize, const METADIM: usize, C> =
Expand Down Expand Up @@ -303,3 +314,33 @@ where
}
}
}

#[inline(always)]
fn add_point_to_byte_buffer<
T: FromBytes,
const SIZE: usize,
const DIM: usize,
const METADIM: usize,
C: PointConvertible<T, SIZE, DIM, METADIM>,
>(
coords: C,
cloud: &mut PointCloud2Msg,
) -> Result<bool, ConversionError> {
let point: Point<T, DIM, METADIM> = coords.into();

// (x, y, z...)
point
.coords
.iter()
.for_each(|x| cloud.data.extend_from_slice(T::bytes(x).as_slice()));

// meta data description
point.meta.iter().for_each(|meta| {
let truncated_bytes = &meta.bytes[0..meta.datatype.size()];
cloud.data.extend_from_slice(truncated_bytes);
});

cloud.width += 1;

Ok(true)
}

0 comments on commit 4002f9c

Please sign in to comment.