@@ -69,52 +69,49 @@ pub fn surface_mesh_from_ply<R: Real, P: AsRef<Path>>(
6969 . get ( "vertex" )
7070 . ok_or ( anyhow ! ( "PLY file is missing a 'vertex' element" ) ) ?;
7171
72- let vertices_normals: Vec < ( Vector3 < _ > , Vector3 < _ > ) > = vertices_normals
73- . into_iter ( )
74- . map ( |e| {
75- let vertex = (
76- e. get ( "x" ) . unwrap ( ) ,
77- e. get ( "y" ) . unwrap ( ) ,
78- e. get ( "z" ) . unwrap ( ) ,
79- e. get ( "nx" ) . unwrap ( ) ,
80- e. get ( "ny" ) . unwrap ( ) ,
81- e. get ( "nz" ) . unwrap ( ) ,
82- ) ;
83-
84- let v = match vertex {
85- (
86- Property :: Float ( x) ,
87- Property :: Float ( y) ,
88- Property :: Float ( z) ,
89- Property :: Float ( nx) ,
90- Property :: Float ( ny) ,
91- Property :: Float ( nz) ,
92- ) => (
93- Vector3 :: new (
94- R :: from_f32 ( * x) . unwrap ( ) ,
95- R :: from_f32 ( * y) . unwrap ( ) ,
96- R :: from_f32 ( * z) . unwrap ( ) ,
97- ) ,
98- Vector3 :: new (
99- R :: from_f32 ( * nx) . unwrap ( ) ,
100- R :: from_f32 ( * ny) . unwrap ( ) ,
101- R :: from_f32 ( * nz) . unwrap ( ) ,
102- ) ,
72+ let mut vertices = Vec :: with_capacity ( vertices_normals. len ( ) ) ;
73+ let mut normals = Vec :: with_capacity ( vertices_normals. len ( ) ) ;
74+
75+ for e in vertices_normals {
76+ let vertex = (
77+ e. get ( "x" ) . unwrap ( ) ,
78+ e. get ( "y" ) . unwrap ( ) ,
79+ e. get ( "z" ) . unwrap ( ) ,
80+ e. get ( "nx" ) . unwrap ( ) ,
81+ e. get ( "ny" ) . unwrap ( ) ,
82+ e. get ( "nz" ) . unwrap ( ) ,
83+ ) ;
84+
85+ let ( vertex, normal) = match vertex {
86+ (
87+ Property :: Float ( x) ,
88+ Property :: Float ( y) ,
89+ Property :: Float ( z) ,
90+ Property :: Float ( nx) ,
91+ Property :: Float ( ny) ,
92+ Property :: Float ( nz) ,
93+ ) => (
94+ Vector3 :: new (
95+ R :: from_f32 ( * x) . unwrap ( ) ,
96+ R :: from_f32 ( * y) . unwrap ( ) ,
97+ R :: from_f32 ( * z) . unwrap ( ) ,
10398 ) ,
104- _ => {
105- return Err ( anyhow ! (
106- "Vertex properties have wrong PLY data type (expected float)"
107- ) )
108- }
109- } ;
110-
111- Ok ( v)
112- } )
113- . map ( |vn| vn. unwrap ( ) )
114- . collect ( ) ;
99+ Vector3 :: new (
100+ R :: from_f32 ( * nx) . unwrap ( ) ,
101+ R :: from_f32 ( * ny) . unwrap ( ) ,
102+ R :: from_f32 ( * nz) . unwrap ( ) ,
103+ ) ,
104+ ) ,
105+ _ => {
106+ return Err ( anyhow ! (
107+ "Vertex properties have wrong PLY data type (expected float)"
108+ ) )
109+ }
110+ } ;
115111
116- let vertices: Vec < Vector3 < _ > > = vertices_normals. iter ( ) . map ( |vn| vn. 0 . clone ( ) ) . collect ( ) ;
117- let normals: Vec < Vector3 < _ > > = vertices_normals. iter ( ) . map ( |vn| vn. 1 . clone ( ) ) . collect ( ) ;
112+ vertices. push ( vertex) ;
113+ normals. push ( normal) ;
114+ }
118115
119116 let faces = ply
120117 . payload
@@ -167,7 +164,7 @@ pub mod test {
167164 use super :: * ;
168165
169166 #[ test]
170- fn test_convert_cube ( ) -> Result < ( ) , anyhow:: Error > {
167+ fn test_read_ply_cube ( ) -> Result < ( ) , anyhow:: Error > {
171168 let input_file = Path :: new ( "../data/cube.ply" ) ;
172169
173170 let mesh: MeshWithData < f32 , _ > = surface_mesh_from_ply ( input_file) . with_context ( || {
0 commit comments