@@ -2,10 +2,18 @@ use std::fs::File;
22use std:: io:: BufReader ;
33
44use :: e57:: { CartesianCoordinate , E57Reader } ;
5- use ndarray:: Ix2 ;
6- use numpy:: { PyArray } ;
5+ use ndarray:: { Ix2 } ;
6+ use numpy:: PyArray ;
77use pyo3:: prelude:: * ;
88
9+ #[ pyclass]
10+ pub struct E57 {
11+ #[ pyo3( get) ]
12+ pub points : Py < PyArray < f64 , Ix2 > > ,
13+ #[ pyo3( get) ]
14+ pub color : Py < PyArray < f32 , Ix2 > > ,
15+ }
16+
917/// Extracts the xml contents from an e57 file.
1018#[ pyfunction]
1119fn raw_xml ( filepath : & str ) -> PyResult < String > {
@@ -26,7 +34,7 @@ fn raw_xml(filepath: &str) -> PyResult<String> {
2634
2735/// Extracts the point data from an e57 file.
2836#[ pyfunction]
29- fn read_points < ' py > ( py : Python < ' py > , filepath : & str ) -> PyResult < & ' py PyArray < f64 , Ix2 > > {
37+ unsafe fn read_points < ' py > ( py : Python < ' py > , filepath : & str ) -> PyResult < E57 > {
3038 let file = E57Reader :: from_file ( filepath) ;
3139 let mut file = match file {
3240 Ok ( file) => file,
@@ -38,8 +46,8 @@ fn read_points<'py>(py: Python<'py>, filepath: &str) -> PyResult<&'py PyArray<f6
3846 } ;
3947 let pc = file. pointclouds ( ) ;
4048 let pc = pc. first ( ) . expect ( "files contain pointclouds" ) ;
41- let ncols = 3 ;
42- let mut vec = Vec :: with_capacity ( pc. records as usize * ncols ) ;
49+ let mut color_vec = Vec :: with_capacity ( pc . records as usize * 3 ) ;
50+ let mut vec = Vec :: with_capacity ( pc. records as usize * 3 ) ;
4351 let mut nrows = 0 ;
4452 for pointcloud in file. pointclouds ( ) {
4553 let mut iter = file
@@ -56,15 +64,31 @@ fn read_points<'py>(py: Python<'py>, filepath: &str) -> PyResult<&'py PyArray<f6
5664 vec. extend ( [ x, y, z] ) ;
5765 nrows += 1
5866 }
67+ // if let Some(intensity) = p.intensity{
68+ // vec.append(intensity as f64)
69+ // }
70+ if let Some ( color) = p. color {
71+ color_vec. extend ( [ color. red , color. green , color. blue ] )
72+ }
5973 }
6074 }
61-
62- Ok ( PyArray :: from_vec ( py, vec) . reshape ( ( nrows, ncols) ) . unwrap ( ) )
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+ } )
85+ }
6386}
6487
6588/// e57 pointcloud file reading.
6689#[ pymodule]
67- fn e57 ( _py : Python , m : & PyModule ) -> PyResult < ( ) > {
90+ fn e57 ( _py : Python < ' _ > , m : & PyModule ) -> PyResult < ( ) > {
91+ m. add_class :: < E57 > ( ) ?;
6892 m. add_function ( wrap_pyfunction ! ( raw_xml, m) ?) ?;
6993 m. add_function ( wrap_pyfunction ! ( read_points, m) ?) ?;
7094 Ok ( ( ) )
0 commit comments