Skip to content

Commit 9d013a8

Browse files
authored
Merge pull request #18 from dancergraham/feature/fields
feature: Intensity
2 parents 10fcdc3 + 7713755 commit 9d013a8

5 files changed

Lines changed: 35 additions & 17 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "e57-python"
3-
version = "0.1.0-a5"
3+
version = "0.1.0-a6"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -10,6 +10,6 @@ crate-type = ["cdylib"]
1010

1111
[dependencies]
1212
pyo3 = "0.20.0"
13-
e57 = "0.10.2"
13+
e57 = "0.10.3"
1414
numpy = "0.20.0"
1515
ndarray = "0.15.6"

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ This python library wraps the [rust e57 library](https://github.com/cry-inc/e57)
1111
- [x] Proof of concept xml reading
1212
- [x] Read e57 point coordinates to numpy array - see `read_points` method.
1313
- [x] Read color field to numpy array.
14-
- [ ] Read intensity and other fields to numpy array.
14+
- [x] Read intensity to numpy array.
15+
- [ ] Read other fields to numpy array.
1516
- [ ] Write to e57 (format ?)
1617

1718
## Getting Started

src/lib.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ pub struct E57 {
1212
pub points: Py<PyArray<f64, Ix2>>,
1313
#[pyo3(get)]
1414
pub color: Py<PyArray<f32, Ix2>>,
15+
#[pyo3(get)]
16+
pub intensity: Py<PyArray<f32, Ix2>>,
1517
}
1618

1719
/// Extracts the xml contents from an e57 file.
@@ -34,7 +36,7 @@ fn raw_xml(filepath: &str) -> PyResult<String> {
3436

3537
/// Extracts the point data from an e57 file.
3638
#[pyfunction]
37-
unsafe fn read_points<'py>(py: Python<'py>, filepath: &str) -> PyResult<E57> {
39+
unsafe fn read_points(py: Python<'_>, filepath: &str) -> PyResult<E57> {
3840
let file = E57Reader::from_file(filepath);
3941
let mut file = match file {
4042
Ok(file) => file,
@@ -46,8 +48,9 @@ unsafe fn read_points<'py>(py: Python<'py>, filepath: &str) -> PyResult<E57> {
4648
};
4749
let pc = file.pointclouds();
4850
let pc = pc.first().expect("files contain pointclouds");
51+
let mut point_vec = Vec::with_capacity(pc.records as usize * 3);
4952
let mut color_vec = Vec::with_capacity(pc.records as usize * 3);
50-
let mut vec = Vec::with_capacity(pc.records as usize * 3);
53+
let mut intensity_vec = Vec::with_capacity(pc.records as usize);
5154
let mut nrows = 0;
5255
for pointcloud in file.pointclouds() {
5356
let mut iter = file
@@ -61,7 +64,7 @@ unsafe fn read_points<'py>(py: Python<'py>, filepath: &str) -> PyResult<E57> {
6164
for p in iter {
6265
let p = p.expect("Unable to read next point");
6366
if let CartesianCoordinate::Valid { x, y, z } = p.cartesian {
64-
vec.extend([x, y, z]);
67+
point_vec.extend([x, y, z]);
6568
nrows += 1
6669
}
6770
// if let Some(intensity) = p.intensity{
@@ -70,19 +73,26 @@ unsafe fn read_points<'py>(py: Python<'py>, filepath: &str) -> PyResult<E57> {
7073
if let Some(color) = p.color {
7174
color_vec.extend([color.red, color.green, color.blue])
7275
}
76+
if let Some(intensity) = p.intensity {
77+
intensity_vec.push(intensity)
78+
}
7379
}
7480
}
75-
if color_vec.len() == vec.len() {
76-
Ok(E57 {
77-
points: Py::from(PyArray::from_vec(py, vec).reshape((nrows, 3)).unwrap()),
78-
color: Py::from(PyArray::from_vec(py, color_vec).reshape((nrows, 3)).unwrap()),
79-
})
80-
} else {
81-
Ok(E57 {
82-
points: Py::from(PyArray::from_vec(py, vec).reshape((nrows, 3)).unwrap()),
83-
color: Py::from(PyArray::new(py, (0,3), false)),
84-
})
81+
let n_points = point_vec.len() / 3;
82+
let n_colors = color_vec.len() / 3;
83+
let n_intensities = intensity_vec.len();
84+
let mut e57 = E57 {
85+
points: Py::from(PyArray::from_vec(py, point_vec).reshape((nrows, 3)).unwrap()),
86+
color: Py::from(PyArray::new(py, (0, 3), false)),
87+
intensity: Py::from(PyArray::new(py, (0, 1), false)),
88+
};
89+
if n_colors == n_points {
90+
e57.color = Py::from(PyArray::from_vec(py, color_vec).reshape((nrows, 3)).unwrap())
91+
}
92+
if n_intensities == n_points {
93+
e57.intensity = Py::from(PyArray::from_vec(py, intensity_vec).reshape((nrows, 1)).unwrap())
8594
}
95+
Ok(e57)
8696
}
8797

8898
/// e57 pointcloud file reading.

tests/test_e57.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ def test_read_color():
2929
assert len(color) == 1_220
3030

3131

32+
def test_read_intensity():
33+
pointcloud = e57.read_points(r"testdata/pipeSpherical.e57")
34+
intensity = pointcloud.intensity
35+
assert isinstance(intensity, np.ndarray)
36+
assert len(intensity) == 1_220
37+
38+
3239
def test_box_dimensions():
3340
pointcloud: np.ndarray = e57.read_points(r"testdata/bunnyFloat.e57")
3441
points = pointcloud.points

0 commit comments

Comments
 (0)