Skip to content

Commit

Permalink
Merge pull request #12 from dancergraham/feature/read_e57
Browse files Browse the repository at this point in the history
Feature/read spherical coordinates
  • Loading branch information
dancergraham authored Aug 5, 2023
2 parents 06b27ef + 541f168 commit d62133b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ classifiers = [
"Topic :: File Formats",
]

[project.urls]
repository = "https://github.com/dancergraham/e57-python"


[tool.maturin]
features = ["pyo3/extension-module"]
41 changes: 28 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::fs::File;
use std::io::BufReader;
use std::ops::AddAssign;

use ::e57::{E57Reader, Point};
use ndarray::{array, Array2, Ix2};
use numpy::{IntoPyArray, PyArray};
use ndarray::Ix2;
use numpy::{PyArray};
use pyo3::prelude::*;

/// Extracts the xml contents from an e57 file.
Expand All @@ -18,7 +17,7 @@ fn raw_xml(filepath: &str) -> PyResult<String> {
Err(e) => {
return Err(PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(
e.to_string(),
))
));
}
};
let xml_string = String::from_utf8(xml.unwrap())?;
Expand All @@ -34,28 +33,44 @@ fn read_points<'py>(py: Python<'py>, filepath: &str) -> PyResult<&'py PyArray<f6
Err(e) => {
return Err(PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(
e.to_string(),
))
));
}
};
let pc = file.pointclouds();
let pc = pc.first().expect("files contain pointclouds");
let mut arr = Array2::zeros((pc.records as usize, 3));

let ncols = 3;
let mut vec = Vec::with_capacity(pc.records as usize * ncols);
let mut nrows = 0;
let iter = file
.pointcloud(pc)
.expect("this file contains a pointcloud");
for (i, p) in iter.enumerate() {
for p in iter {
let p = p.expect("Unable to read next point");
let p = Point::from_values(p, &pc.prototype)
.expect("failed to convert raw point to simple point");
if let Some(c) = p.cartesian {
let coordinates = array![c.x, c.y, c.z];
let mut row = arr.row_mut(i);
row.add_assign(&coordinates);
if let Some(invalid) = p.cartesian_invalid {
if invalid != 0 {
continue;
}
}
vec.extend([c.x, c.y, c.z]);
nrows += 1
} else if let Some(s) = p.spherical {
if let Some(invalid) = p.spherical_invalid {
if invalid != 0 {
continue;
}
}
let cos_ele = f64::cos(s.elevation);
let x = s.range * cos_ele * f64::cos(s.azimuth);
let y = s.range * cos_ele * f64::sin(s.azimuth);
let z = s.range * f64::sin(s.elevation);
vec.extend([x, y, z]);
nrows += 1
}
}

Ok(arr.into_pyarray(py))
Ok(PyArray::from_vec(py, vec).reshape((nrows, ncols)).unwrap())
}

/// e57 pointcloud file reading.
Expand Down
3 changes: 1 addition & 2 deletions tests/test_e57.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import e57
import numpy as np
import pytest

import e57


def test_raw_xml():
raw_xml = e57.raw_xml(r"testdata/bunnyFloat.e57")
Expand Down

0 comments on commit d62133b

Please sign in to comment.