|
| 1 | +use std::path::Path; |
| 2 | + |
1 | 3 | use anyhow::{anyhow, Context}; |
2 | 4 | use coarse_prof::profile; |
3 | 5 | use log::info; |
| 6 | +use na::Vector3; |
4 | 7 | use splashsurf_lib::mesh::PointCloud3d; |
5 | 8 | use splashsurf_lib::{density_map, Index, Real}; |
6 | 9 |
|
@@ -39,40 +42,12 @@ pub(crate) fn entry_point_generic<I: Index, R: Real>( |
39 | 42 | ) -> Result<(), anyhow::Error> { |
40 | 43 | profile!("surface reconstruction cli"); |
41 | 44 |
|
42 | | - info!("Loading dataset from \"{}\"...", paths.input_file.display()); |
43 | | - let particle_positions = if let Some(extension) = paths.input_file.extension() { |
44 | | - profile!("loading particle positions"); |
45 | | - |
46 | | - let extension = extension.to_str().ok_or(anyhow!( |
47 | | - "Invalid extension of of input file '{}'", |
48 | | - paths.input_file.display() |
49 | | - ))?; |
50 | | - |
51 | | - match extension.to_lowercase().as_str() { |
52 | | - "vtk" => { |
53 | | - let sph_dataset = io::read_vtk(&paths.input_file)?; |
54 | | - io::particles_from_dataset(&sph_dataset)? |
55 | | - } |
56 | | - "xyz" => io::particles_from_xyz(&paths.input_file)?, |
57 | | - _ => { |
58 | | - return Err(anyhow!( |
59 | | - "Unsupported file format extension '{}' of input file '{}'", |
60 | | - extension, |
61 | | - paths.input_file.display() |
62 | | - )); |
63 | | - } |
64 | | - } |
65 | | - } else { |
66 | | - return Err(anyhow!( |
67 | | - "Unable to detect file format of input file '{}' (file name has to end with supported extension)", |
| 45 | + let particle_positions = load_particle_positions(&paths.input_file).with_context(|| { |
| 46 | + format!( |
| 47 | + "Failed to load particle positions from file '{}'", |
68 | 48 | paths.input_file.display() |
69 | | - )); |
70 | | - }; |
71 | | - |
72 | | - info!( |
73 | | - "Loaded dataset with {} particle positions.", |
74 | | - particle_positions.len() |
75 | | - ); |
| 49 | + ) |
| 50 | + })?; |
76 | 51 |
|
77 | 52 | let reconstruction = |
78 | 53 | splashsurf_lib::reconstruct_surface::<I, R>(particle_positions.as_slice(), ¶ms)?; |
@@ -145,3 +120,43 @@ pub(crate) fn entry_point_generic<I: Index, R: Real>( |
145 | 120 |
|
146 | 121 | Ok(()) |
147 | 122 | } |
| 123 | + |
| 124 | +fn load_particle_positions<R: Real, P: AsRef<Path>>( |
| 125 | + input_file: P, |
| 126 | +) -> Result<Vec<Vector3<R>>, anyhow::Error> { |
| 127 | + let input_file = input_file.as_ref(); |
| 128 | + info!("Loading dataset from \"{}\"...", input_file.display()); |
| 129 | + |
| 130 | + let particle_positions = if let Some(extension) = input_file.extension() { |
| 131 | + profile!("loading particle positions"); |
| 132 | + |
| 133 | + let extension = extension |
| 134 | + .to_str() |
| 135 | + .ok_or(anyhow!("Invalid extension of particle file",))?; |
| 136 | + |
| 137 | + match extension.to_lowercase().as_str() { |
| 138 | + "vtk" => { |
| 139 | + let sph_dataset = io::read_vtk(&input_file)?; |
| 140 | + io::particles_from_dataset(&sph_dataset)? |
| 141 | + } |
| 142 | + "xyz" => io::particles_from_xyz(&input_file)?, |
| 143 | + _ => { |
| 144 | + return Err(anyhow!( |
| 145 | + "Unsupported file format extension '{}' of particle file", |
| 146 | + extension |
| 147 | + )); |
| 148 | + } |
| 149 | + } |
| 150 | + } else { |
| 151 | + return Err(anyhow!( |
| 152 | + "Unable to detect file format of particle file (file name has to end with supported extension)", |
| 153 | + )); |
| 154 | + }; |
| 155 | + |
| 156 | + info!( |
| 157 | + "Loaded dataset with {} particle positions.", |
| 158 | + particle_positions.len() |
| 159 | + ); |
| 160 | + |
| 161 | + Ok(particle_positions) |
| 162 | +} |
0 commit comments