11use std:: path:: Path ;
22
33use anyhow:: { anyhow, Context } ;
4- use ply_rs as ply ;
5- use ply_rs:: ply:: Property ;
4+ use ply_rs:: parser :: Parser as PlyParser ;
5+ use ply_rs:: ply:: { DefaultElement , Ply , Property } ;
66
77use splashsurf_lib:: mesh:: AttributeData ;
88use splashsurf_lib:: mesh:: MeshAttribute ;
@@ -11,17 +11,31 @@ use splashsurf_lib::mesh::TriMesh3d;
1111use splashsurf_lib:: nalgebra:: Vector3 ;
1212use splashsurf_lib:: Real ;
1313
14- /// Tries to read particle positions from the PLY file at the given path
14+ /// Tries to load the file at the given path as a PLY file and read particle positions from it
1515pub fn particles_from_ply < R : Real , P : AsRef < Path > > (
1616 ply_path : P ,
1717) -> Result < Vec < Vector3 < R > > , anyhow:: Error > {
18- let mut ply_file = std:: fs:: File :: open ( ply_path) . unwrap ( ) ;
19- let parser = ply:: parser:: Parser :: < ply:: ply:: DefaultElement > :: new ( ) ;
18+ let ply = PlyParser :: new ( )
19+ . read_ply ( & mut std:: fs:: File :: open ( ply_path) . context ( "Failed to open file for reading" ) ?)
20+ . context ( "Failed to parse PLY file" ) ?;
21+ parse_particles_from_ply ( & ply)
22+ }
2023
21- let ply = parser
22- . read_ply ( & mut ply_file)
23- . context ( "Failed to read PLY file" ) ?;
24- let elements = ply
24+ /// Tries to load the file at the given path as a PLY file and read a surface mesh from it
25+ pub fn surface_mesh_from_ply < R : Real , P : AsRef < Path > > (
26+ ply_path : P ,
27+ ) -> Result < MeshWithData < R , TriMesh3d < R > > , anyhow:: Error > {
28+ let ply = PlyParser :: new ( )
29+ . read_ply ( & mut std:: fs:: File :: open ( ply_path) . context ( "Failed to open file for reading" ) ?)
30+ . context ( "Failed to parse PLY file" ) ?;
31+ parse_mesh_from_ply ( & ply)
32+ }
33+
34+ /// Tries to extract particle positions from the given PLY structure
35+ fn parse_particles_from_ply < R : Real > (
36+ ply_file : & Ply < DefaultElement > ,
37+ ) -> Result < Vec < Vector3 < R > > , anyhow:: Error > {
38+ let elements = ply_file
2539 . payload
2640 . get ( "vertex" )
2741 . ok_or ( anyhow ! ( "PLY file is missing a 'vertex' element" ) ) ?;
@@ -55,7 +69,7 @@ pub fn particles_from_ply<R: Real, P: AsRef<Path>>(
5569 Ok ( particles)
5670}
5771
58- /// Tries to read a surface mesh from the PLY file at the given path
72+ /// Tries to extract a surface mesh from the given PLY structure
5973///
6074/// The PLY file is expected to use the following structure which is used by Blender for export:
6175/// ```text
@@ -71,16 +85,10 @@ pub fn particles_from_ply<R: Real, P: AsRef<Path>>(
7185/// element face 12
7286/// property list uchar uint vertex_indices
7387/// ```
74- pub fn surface_mesh_from_ply < R : Real , P : AsRef < Path > > (
75- ply_path : P ,
88+ fn parse_mesh_from_ply < R : Real > (
89+ ply_file : & Ply < DefaultElement > ,
7690) -> Result < MeshWithData < R , TriMesh3d < R > > , anyhow:: Error > {
77- let mut ply_file = std:: fs:: File :: open ( ply_path) . unwrap ( ) ;
78- let parser = ply:: parser:: Parser :: < ply:: ply:: DefaultElement > :: new ( ) ;
79-
80- let ply = parser
81- . read_ply ( & mut ply_file)
82- . context ( "Failed to parse PLY file" ) ?;
83- let vertices_normals = ply
91+ let vertices_normals = ply_file
8492 . payload
8593 . get ( "vertex" )
8694 . ok_or ( anyhow ! ( "PLY file is missing a 'vertex' element" ) ) ?;
@@ -127,7 +135,7 @@ pub fn surface_mesh_from_ply<R: Real, P: AsRef<Path>>(
127135 normals. push ( normal) ;
128136 }
129137
130- let faces = ply
138+ let faces = ply_file
131139 . payload
132140 . get ( "face" )
133141 . ok_or ( anyhow ! ( "PLY file is missing a 'face' element" ) ) ?;
0 commit comments