@@ -82,63 +82,107 @@ pub fn read_particle_positions_with_attributes<R: Real, P: AsRef<Path>>(
8282
8383 profile ! ( "loading particle positions and attributes" ) ;
8484
85- // Check file extension: only VTK is supported for reading attributes at the moment
86- {
87- let extension = input_file. extension ( ) . ok_or ( anyhow ! (
88- "Unable to detect file format of particle input file (file name has to end with supported extension)" ,
89- ) ) ?. to_str ( ) . ok_or ( anyhow ! ( "Invalid extension of input file" ) ) ?. to_lowercase ( ) ;
90-
91- match extension. as_str ( ) {
92- "vtk" | "vtu" => { }
93- _ => {
94- return Err ( anyhow ! (
95- "Unsupported file format extension \" {}\" for reading particles and attributes" ,
96- extension
97- ) ) ;
85+ // Check file extension: only VTK and BGEO is supported for reading attributes at the moment
86+ let extension = input_file. extension ( ) . ok_or ( anyhow ! (
87+ "Unable to detect file format of particle input file (file name has to end with supported extension)" ,
88+ ) ) ?. to_str ( ) . ok_or ( anyhow ! ( "Invalid extension of input file" ) ) ?. to_lowercase ( ) ;
89+
90+ let attributes_to_interpolate = attribute_names. iter ( ) . cloned ( ) . collect :: < HashSet < _ > > ( ) ;
91+
92+ let ( particle_positions, attributes) = match extension. as_str ( ) {
93+ "vtk" | "vtu" => {
94+ let vtk_pieces = VtkFile :: load_file ( input_file)
95+ . map ( |f| f. into_pieces ( ) )
96+ . with_context ( || "Error while loading VTK file" . to_string ( ) ) ?;
97+
98+ if vtk_pieces. len ( ) > 1 {
99+ warn ! (
100+ "VTK file contains more than one \" piece\" . Only the first one will be loaded."
101+ ) ;
98102 }
99- }
100- }
101-
102- let vtk_pieces = VtkFile :: load_file ( input_file)
103- . map ( |f| f. into_pieces ( ) )
104- . with_context ( || "Failed to load particle positions from file" . to_string ( ) ) ?;
105103
106- if vtk_pieces. len ( ) > 1 {
107- warn ! ( "VTK file contains more than one \" piece\" . Only the first one will be loaded." ) ;
108- }
109-
110- let first_piece = vtk_pieces
111- . into_iter ( )
112- . next ( )
113- . ok_or ( anyhow ! ( "VTK file does not contain a supported \" piece\" ." ) ) ?;
114-
115- // Load particles
116- let particle_positions = first_piece. load_as_particles ( ) ?;
117-
118- // Load attributes that should be interpolated
119- let attributes = {
120- // Check if all attributes to interpolate are present in the input file
121- {
122- let attributes_to_interpolate = attribute_names. iter ( ) . cloned ( ) . collect :: < HashSet < _ > > ( ) ;
123- let attributes = first_piece
124- . point_attribute_names ( )
104+ let first_piece = vtk_pieces
125105 . into_iter ( )
126- . collect :: < HashSet < _ > > ( ) ;
127-
128- let missing_attributes = attributes_to_interpolate
129- . difference ( & attributes)
130- . cloned ( )
131- . collect :: < Vec < _ > > ( ) ;
132- if !missing_attributes. is_empty ( ) {
133- return Err ( anyhow ! (
134- "Missing attribute(s) \" {}\" in input file" ,
135- missing_attributes. join( "\" , \" " ) ,
136- ) ) ;
137- }
106+ . next ( )
107+ . ok_or ( anyhow ! ( "VTK file does not contain a supported \" piece\" ." ) ) ?;
108+
109+ // Load particles
110+ let particle_positions = first_piece. load_as_particles ( ) ?;
111+
112+ // Load attributes that should be interpolated
113+ let attributes = {
114+ // Check if all attributes to interpolate are present in the input file
115+ {
116+ let available_attributes = first_piece
117+ . point_attribute_names ( )
118+ . into_iter ( )
119+ . collect :: < HashSet < _ > > ( ) ;
120+
121+ let missing_attributes = attributes_to_interpolate
122+ . difference ( & available_attributes)
123+ . cloned ( )
124+ . collect :: < Vec < _ > > ( ) ;
125+ if !missing_attributes. is_empty ( ) {
126+ return Err ( anyhow ! (
127+ "Missing attribute(s) \" {}\" in input file" ,
128+ missing_attributes. join( "\" , \" " ) ,
129+ ) ) ;
130+ }
131+ }
132+
133+ first_piece. load_point_attributes :: < R > ( attribute_names)
134+ } ?;
135+
136+ ( particle_positions, attributes)
138137 }
139-
140- first_piece. load_point_attributes :: < R > ( attribute_names)
141- } ?;
138+ "bgeo" => {
139+ let bgeo_file = bgeo_format:: load_bgeo_file ( input_file)
140+ . with_context ( || "Error while loading BGEO file" . to_string ( ) ) ?;
141+
142+ let particle_positions = bgeo_format:: particles_from_bgeo_file :: < R > ( & bgeo_file)
143+ . with_context ( || {
144+ "Error while loading particle positions from BGEO file" . to_string ( )
145+ } ) ?;
146+
147+ // Load attributes that should be interpolated
148+ let attributes = {
149+ // Check if all attributes to interpolate are present in the input file
150+ {
151+ let available_attributes = bgeo_file
152+ . attribute_definitions
153+ . iter ( )
154+ . map ( |a| a. name . clone ( ) )
155+ . collect :: < HashSet < _ > > ( ) ;
156+
157+ let missing_attributes = attributes_to_interpolate
158+ . difference ( & available_attributes)
159+ . cloned ( )
160+ . collect :: < Vec < _ > > ( ) ;
161+ if !missing_attributes. is_empty ( ) {
162+ return Err ( anyhow ! (
163+ "Missing attribute(s) \" {}\" in input file" ,
164+ missing_attributes. join( "\" , \" " ) ,
165+ ) ) ;
166+ }
167+ }
168+
169+ let attributes = bgeo_format:: attributes_from_bgeo_file (
170+ & bgeo_file,
171+ & Vec :: from_iter ( attributes_to_interpolate) ,
172+ ) ?;
173+
174+ attributes
175+ } ;
176+
177+ ( particle_positions, attributes)
178+ }
179+ _ => {
180+ return Err ( anyhow ! (
181+ "Unsupported file format extension \" {}\" for reading particles and attributes" ,
182+ extension
183+ ) ) ;
184+ }
185+ } ;
142186
143187 info ! (
144188 "Successfully loaded point {} attribute(s): \" {}\" " ,
0 commit comments