Skip to content

Commit 541f168

Browse files
committed
refactor: simplify array building
1 parent 87f3ec9 commit 541f168

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

src/lib.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::io::BufReader;
33

44
use ::e57::{E57Reader, Point};
55
use ndarray::Ix2;
6-
use numpy::PyArray;
6+
use numpy::{PyArray};
77
use pyo3::prelude::*;
88

99
/// Extracts the xml contents from an e57 file.
@@ -38,7 +38,9 @@ fn read_points<'py>(py: Python<'py>, filepath: &str) -> PyResult<&'py PyArray<f6
3838
};
3939
let pc = file.pointclouds();
4040
let pc = pc.first().expect("files contain pointclouds");
41-
let mut vec = Vec::with_capacity(pc.records as usize);
41+
let ncols = 3;
42+
let mut vec = Vec::with_capacity(pc.records as usize * ncols);
43+
let mut nrows = 0;
4244
let iter = file
4345
.pointcloud(pc)
4446
.expect("this file contains a pointcloud");
@@ -52,8 +54,8 @@ fn read_points<'py>(py: Python<'py>, filepath: &str) -> PyResult<&'py PyArray<f6
5254
continue;
5355
}
5456
}
55-
56-
vec.push(vec![c.x, c.y, c.z]);
57+
vec.extend([c.x, c.y, c.z]);
58+
nrows += 1
5759
} else if let Some(s) = p.spherical {
5860
if let Some(invalid) = p.spherical_invalid {
5961
if invalid != 0 {
@@ -64,11 +66,11 @@ fn read_points<'py>(py: Python<'py>, filepath: &str) -> PyResult<&'py PyArray<f6
6466
let x = s.range * cos_ele * f64::cos(s.azimuth);
6567
let y = s.range * cos_ele * f64::sin(s.azimuth);
6668
let z = s.range * f64::sin(s.elevation);
67-
vec.push(vec![x, y, z]);
69+
vec.extend([x, y, z]);
70+
nrows += 1
6871
}
6972
}
70-
let pyarray = PyArray::from_vec2(py, &vec).unwrap();
71-
Ok(pyarray)
73+
Ok(PyArray::from_vec(py, vec).reshape((nrows, ncols)).unwrap())
7274
}
7375

7476
/// e57 pointcloud file reading.

0 commit comments

Comments
 (0)