Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Now in technicolor! 🏳️‍🌈 #16

Merged
merged 2 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "e57-python"
version = "0.1.0-a4"
version = "0.1.0-a5"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This python library wraps the [rust e57 library](https://github.com/cry-inc/e57)

- [x] Proof of concept xml reading
- [x] Read e57 point coordinates to numpy array - see `read_points` method.
- [x] Read color field to numpy array.
- [ ] Read intensity and other fields to numpy array.
- [ ] Write to e57 (format ?)

Expand Down
40 changes: 32 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ use std::fs::File;
use std::io::BufReader;

use ::e57::{CartesianCoordinate, E57Reader};
use ndarray::Ix2;
use numpy::{PyArray};
use ndarray::{Ix2};
use numpy::PyArray;
use pyo3::prelude::*;

#[pyclass]
pub struct E57 {
#[pyo3(get)]
pub points: Py<PyArray<f64, Ix2>>,
#[pyo3(get)]
pub color: Py<PyArray<f32, Ix2>>,
}

/// Extracts the xml contents from an e57 file.
#[pyfunction]
fn raw_xml(filepath: &str) -> PyResult<String> {
Expand All @@ -26,7 +34,7 @@ fn raw_xml(filepath: &str) -> PyResult<String> {

/// Extracts the point data from an e57 file.
#[pyfunction]
fn read_points<'py>(py: Python<'py>, filepath: &str) -> PyResult<&'py PyArray<f64, Ix2>> {
unsafe fn read_points<'py>(py: Python<'py>, filepath: &str) -> PyResult<E57> {
let file = E57Reader::from_file(filepath);
let mut file = match file {
Ok(file) => file,
Expand All @@ -38,8 +46,8 @@ fn read_points<'py>(py: Python<'py>, filepath: &str) -> PyResult<&'py PyArray<f6
};
let pc = file.pointclouds();
let pc = pc.first().expect("files contain pointclouds");
let ncols = 3;
let mut vec = Vec::with_capacity(pc.records as usize * ncols);
let mut color_vec = Vec::with_capacity(pc.records as usize * 3);
let mut vec = Vec::with_capacity(pc.records as usize * 3);
let mut nrows = 0;
for pointcloud in file.pointclouds() {
let mut iter = file
Expand All @@ -56,15 +64,31 @@ fn read_points<'py>(py: Python<'py>, filepath: &str) -> PyResult<&'py PyArray<f6
vec.extend([x, y, z]);
nrows += 1
}
// if let Some(intensity) = p.intensity{
// vec.append(intensity as f64)
// }
if let Some(color) = p.color {
color_vec.extend([color.red, color.green, color.blue])
}
}
}

Ok(PyArray::from_vec(py, vec).reshape((nrows, ncols)).unwrap())
if color_vec.len() == vec.len() {
Ok(E57 {
points: Py::from(PyArray::from_vec(py, vec).reshape((nrows, 3)).unwrap()),
color: Py::from(PyArray::from_vec(py, color_vec).reshape((nrows, 3)).unwrap()),
})
} else {
Ok(E57 {
points: Py::from(PyArray::from_vec(py, vec).reshape((nrows, 3)).unwrap()),
color: Py::from(PyArray::new(py, (0,3), false)),
})
}
}

/// e57 pointcloud file reading.
#[pymodule]
fn e57(_py: Python, m: &PyModule) -> PyResult<()> {
fn e57(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_class::<E57>()?;
m.add_function(wrap_pyfunction!(raw_xml, m)?)?;
m.add_function(wrap_pyfunction!(read_points, m)?)?;
Ok(())
Expand Down
Binary file added testdata/pipeSpherical.e57
Binary file not shown.
28 changes: 22 additions & 6 deletions tests/test_e57.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,30 @@ def test_raw_xml():

def test_read_points():
pointcloud = e57.read_points(r"testdata/bunnyFloat.e57")
assert isinstance(pointcloud, np.ndarray)
assert len(pointcloud) == 30_571
points = pointcloud.points
assert isinstance(points, np.ndarray)
assert len(points) == 30_571


def test_read_spherical():
pointcloud = e57.read_points(r"testdata/pipeSpherical.e57")
points = pointcloud.points
assert isinstance(points, np.ndarray)
assert len(points) == 1_220


def test_read_color():
pointcloud = e57.read_points(r"testdata/pipeSpherical.e57")
color = pointcloud.color
assert isinstance(color, np.ndarray)
assert len(color) == 1_220


def test_box_dimensions():
pointcloud: np.ndarray = e57.read_points(r"testdata/bunnyFloat.e57")
max_coords = pointcloud.max(0, None, False, -np.inf)
min_coords = pointcloud.min(0, None, False, np.inf)
points = pointcloud.points
max_coords = points.max(0, None, False, -np.inf)
min_coords = points.min(0, None, False, np.inf)
X, Y, Z = max_coords - min_coords
assert X == pytest.approx(0.155698)
assert Y == pytest.approx(0.14731)
Expand All @@ -26,8 +42,8 @@ def test_box_dimensions():

def test_global_box_center():
pointcloud: np.ndarray = e57.read_points(r"testdata/bunnyFloat.e57")
max_coords = pointcloud.max(0, None, False, -np.inf)
min_coords = pointcloud.min(0, None, False, np.inf)
max_coords = pointcloud.points.max(0, None, False, -np.inf)
min_coords = pointcloud.points.min(0, None, False, np.inf)
X, Y, Z = (max_coords + min_coords) / 2
assert X == pytest.approx(-0.016840)
assert Y == pytest.approx(0.113666)
Expand Down