Skip to content

Commit 0f047ac

Browse files
committed
Move particle loading to separate function
1 parent e1e50d1 commit 0f047ac

1 file changed

Lines changed: 48 additions & 33 deletions

File tree

splashsurf/src/reconstruction.rs

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
use std::path::Path;
2+
13
use anyhow::{anyhow, Context};
24
use coarse_prof::profile;
35
use log::info;
6+
use na::Vector3;
47
use splashsurf_lib::mesh::PointCloud3d;
58
use splashsurf_lib::{density_map, Index, Real};
69

@@ -39,40 +42,12 @@ pub(crate) fn entry_point_generic<I: Index, R: Real>(
3942
) -> Result<(), anyhow::Error> {
4043
profile!("surface reconstruction cli");
4144

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 '{}'",
6848
paths.input_file.display()
69-
));
70-
};
71-
72-
info!(
73-
"Loaded dataset with {} particle positions.",
74-
particle_positions.len()
75-
);
49+
)
50+
})?;
7651

7752
let reconstruction =
7853
splashsurf_lib::reconstruct_surface::<I, R>(particle_positions.as_slice(), &params)?;
@@ -145,3 +120,43 @@ pub(crate) fn entry_point_generic<I: Index, R: Real>(
145120

146121
Ok(())
147122
}
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

Comments
 (0)