dimforge / dimforge/nalgebra

How to convert column slice to Point w/o copy

Open
#595 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
4.8k
Forks
565
PR merge metrics
No merged PRs in 30d

Description

I am trying to store a list of 2d or 3d points in a matrix, where each point is a column in the matrix. Like so:
```
x, x, x, x, ...
y, y, y, y, ...
z, z, z, z, ...
```
I'm trying to iterate over the columns of the point cloud and treat each column as a Point object -- ideally without any copying.

Here is the best I have come up with. It works, but it makes a copy of each point in the iterator.

```rust
type Point = na::Point;

pub struct PointCloud
where
NaD: na::DimName,
{
// The data structure that stores the points. Each point is a column in this matrix, so there
// are as many columns as points.
pub data: na::MatrixMN,
}

impl PointCloud
where
NaD: na::DimName,
na::DefaultAllocator: na::allocator::Allocator,
{
pub fn points_iter<'a>(&'a self) -> impl Iterator> + 'a {
self.data.column_iter().map(|col| {
// TODO(kgreenek): Figure out a way here not to make a copy.
let coords = na::VectorN::::from_column_slice(col.as_slice());
Point::::from(coords)
})
}
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.