Skip to content
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
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::io::BufReader;
use ::e57::{CartesianCoordinate, E57Reader};
use numpy::ndarray::Array2;
use numpy::{IntoPyArray, PyArray2};
use pyo3::exceptions::PyRuntimeError;
use pyo3::prelude::*;

// Keep these docs in sync with e57.pyi.
Expand Down Expand Up @@ -76,15 +77,15 @@ unsafe fn read_points(py: Python<'_>, filepath: &str) -> PyResult<E57> {
for pointcloud in pointclouds {
let mut iter = file
.pointcloud_simple(&pointcloud)
.expect("this file should contain a pointcloud");
.map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
iter.spherical_to_cartesian(true);
iter.cartesian_to_spherical(false);
iter.intensity_to_color(true);
iter.normalize_intensity(false);
iter.apply_pose(true);

for p in iter {
let p = p.expect("Unable to read next point");
let p = p.map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
// Invalid points are skipped entirely, so colour and intensity
// stay row-aligned with the points that are kept.
let CartesianCoordinate::Valid { x, y, z } = p.cartesian else {
Expand Down
14 changes: 14 additions & 0 deletions tests/test_e57.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pathlib

import numpy as np
import pytest

Expand Down Expand Up @@ -202,3 +204,15 @@ def test_invalid_points_rows_aligned():
np.testing.assert_allclose(pointcloud.points, xyz, atol=1e-9)
np.testing.assert_allclose(pointcloud.color, rgb, atol=1e-6)
np.testing.assert_allclose(pointcloud.intensity[:, 0], intensity, atol=1e-6)


def test_corrupt_point_data(tmp_path):
"""Valid header and XML but garbage point data raises RuntimeError, not a panic."""
good = pathlib.Path("testdata/bunnyFloat.e57").read_bytes()
xml_offset = int.from_bytes(good[24:32], "little") # header: XML physical offset
bad = bytearray(good)
bad[2000 : xml_offset - 2000] = b"\xff" * (xml_offset - 4000)
path = tmp_path / "corrupt.e57"
path.write_bytes(bad)
with pytest.raises(RuntimeError, match="Failed to read data packet"):
e57.read_points(str(path))
Loading