Skip to content

Commit 0c1a49a

Browse files
committed
Small refactoring
1 parent 72c6613 commit 0c1a49a

File tree

4 files changed

+157
-159
lines changed

4 files changed

+157
-159
lines changed

pysplashsurf/src/pipeline.rs

Lines changed: 52 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use pyo3::{
1010
prelude::*,
1111
types::{PyDict, PyString},
1212
};
13-
use splashsurf::PipelineResult;
1413
use splashsurf_lib::{
1514
Aabb3d, GridDecompositionParameters, Index, Real, SpatialDecomposition,
1615
mesh::{AttributeData, MeshAttribute},
@@ -54,7 +53,7 @@ fn reconstruction_pipeline_generic<I: Index, R: Real>(
5453
mesh_aabb_min: Option<[f64; 3]>,
5554
mesh_aabb_max: Option<[f64; 3]>,
5655
mesh_aabb_clamp_vertices: bool,
57-
) -> Result<PipelineResult<I, R>, anyhow::Error> {
56+
) -> Result<splashsurf::reconstruct::ReconstructionResult<I, R>, anyhow::Error> {
5857
let aabb = if let (Some(aabb_min), Some(aabb_max)) = (aabb_min, aabb_max) {
5958
// Convert the min and max arrays to Vector3
6059
Some(Aabb3d::new(
@@ -65,15 +64,13 @@ fn reconstruction_pipeline_generic<I: Index, R: Real>(
6564
None
6665
};
6766

68-
let spatial_decomposition = if use_custom_grid_decomposition {
67+
let spatial_decomposition = use_custom_grid_decomposition.then(|| {
6968
let mut grid_params = GridDecompositionParameters::default();
7069
grid_params.subdomain_num_cubes_per_dim = subdomain_num_cubes_per_dim;
71-
Some(SpatialDecomposition::UniformGrid(grid_params))
72-
} else {
73-
None
74-
};
70+
SpatialDecomposition::UniformGrid(grid_params)
71+
});
7572

76-
let params: splashsurf_lib::Parameters<R> = splashsurf_lib::Parameters {
73+
let params = splashsurf_lib::Parameters {
7774
particle_radius,
7875
rest_density,
7976
compact_support_radius: R::from_f64(2.0).unwrap() * smoothing_length * particle_radius,
@@ -96,35 +93,34 @@ fn reconstruction_pipeline_generic<I: Index, R: Real>(
9693
None
9794
};
9895

99-
let postprocessing_args: splashsurf::ReconstructionRunnerPostprocessingArgs =
100-
splashsurf::ReconstructionRunnerPostprocessingArgs {
101-
check_mesh_closed,
102-
check_mesh_manifold,
103-
check_mesh_orientation,
104-
check_mesh_debug,
105-
mesh_cleanup,
106-
mesh_cleanup_snap_dist: max_rel_snap_dist,
107-
decimate_barnacles,
108-
keep_vertices,
109-
compute_normals,
110-
sph_normals,
111-
normals_smoothing_iters,
112-
interpolate_attributes: Vec::new(),
113-
mesh_smoothing_iters,
114-
mesh_smoothing_weights,
115-
mesh_smoothing_weights_normalization,
116-
generate_quads,
117-
quad_max_edge_diag_ratio,
118-
quad_max_normal_angle,
119-
quad_max_interior_angle,
120-
output_mesh_smoothing_weights,
121-
output_raw_normals,
122-
output_raw_mesh,
123-
mesh_aabb,
124-
mesh_aabb_clamp_vertices,
125-
};
96+
let postprocessing_args = splashsurf::reconstruct::ReconstructionPostprocessingParameters {
97+
check_mesh_closed,
98+
check_mesh_manifold,
99+
check_mesh_orientation,
100+
check_mesh_debug,
101+
mesh_cleanup,
102+
mesh_cleanup_snap_dist: max_rel_snap_dist,
103+
decimate_barnacles,
104+
keep_vertices,
105+
compute_normals,
106+
sph_normals,
107+
normals_smoothing_iters,
108+
interpolate_attributes: Vec::new(),
109+
mesh_smoothing_iters,
110+
mesh_smoothing_weights,
111+
mesh_smoothing_weights_normalization,
112+
generate_quads,
113+
quad_max_edge_diag_ratio,
114+
quad_max_normal_angle,
115+
quad_max_interior_angle,
116+
output_mesh_smoothing_weights,
117+
output_raw_normals,
118+
output_raw_mesh,
119+
mesh_aabb,
120+
mesh_aabb_clamp_vertices,
121+
};
126122

127-
splashsurf::reconstruction_pipeline(
123+
splashsurf::reconstruct::reconstruction_pipeline(
128124
particle_positions,
129125
attributes,
130126
&params,
@@ -227,19 +223,19 @@ pub fn reconstruction_pipeline_py_f32<'py>(
227223
mesh_aabb_min: Option<[f64; 3]>,
228224
mesh_aabb_max: Option<[f64; 3]>,
229225
mesh_aabb_clamp_vertices: bool,
230-
) -> (
226+
) -> PyResult<(
231227
Option<TriMeshWithDataF32>,
232228
Option<MixedTriQuadMeshWithDataF32>,
233229
Option<SurfaceReconstructionF32>,
234-
) {
235-
let particles: PyReadonlyArray2<f32> = particles.extract().unwrap();
230+
)> {
231+
let particles: PyReadonlyArray2<f32> = particles.extract()?;
236232

237-
let particle_positions = particles.as_slice().unwrap();
233+
let particle_positions = particles.as_slice()?;
238234
let particle_positions: &[Vector3<f32>] = bytemuck::cast_slice(particle_positions);
239235

240236
let attrs = attrs_conversion(attributes_to_interpolate);
241237

242-
let PipelineResult {
238+
let splashsurf::reconstruct::ReconstructionResult {
243239
tri_mesh,
244240
tri_quad_mesh,
245241
raw_reconstruction: reconstruction,
@@ -283,25 +279,11 @@ pub fn reconstruction_pipeline_py_f32<'py>(
283279
)
284280
.unwrap();
285281

286-
let tri_mesh = if let Some(tri_mesh) = tri_mesh {
287-
Some(TriMeshWithDataF32::new(tri_mesh))
288-
} else {
289-
None
290-
};
291-
292-
let tri_quad_mesh = if let Some(tri_quad_mesh) = tri_quad_mesh {
293-
Some(MixedTriQuadMeshWithDataF32::new(tri_quad_mesh))
294-
} else {
295-
None
296-
};
297-
298-
let reconstruction = if let Some(reconstruction) = reconstruction {
299-
Some(SurfaceReconstructionF32::new(reconstruction))
300-
} else {
301-
None
302-
};
303-
304-
(tri_mesh, tri_quad_mesh, reconstruction)
282+
Ok((
283+
tri_mesh.map(TriMeshWithDataF32::new),
284+
tri_quad_mesh.map(MixedTriQuadMeshWithDataF32::new),
285+
reconstruction.map(SurfaceReconstructionF32::new),
286+
))
305287
}
306288

307289
#[pyfunction]
@@ -354,19 +336,19 @@ pub fn reconstruction_pipeline_py_f64<'py>(
354336
mesh_aabb_min: Option<[f64; 3]>,
355337
mesh_aabb_max: Option<[f64; 3]>,
356338
mesh_aabb_clamp_vertices: bool,
357-
) -> (
339+
) -> PyResult<(
358340
Option<TriMeshWithDataF64>,
359341
Option<MixedTriQuadMeshWithDataF64>,
360342
Option<SurfaceReconstructionF64>,
361-
) {
362-
let particles: PyReadonlyArray2<f64> = particles.extract().unwrap();
343+
)> {
344+
let particles: PyReadonlyArray2<f64> = particles.extract()?;
363345

364-
let particle_positions = particles.as_slice().unwrap();
346+
let particle_positions = particles.as_slice()?;
365347
let particle_positions: &[Vector3<f64>] = bytemuck::cast_slice(particle_positions);
366348

367349
let attrs = attrs_conversion(attributes_to_interpolate);
368350

369-
let PipelineResult {
351+
let splashsurf::reconstruct::ReconstructionResult {
370352
tri_mesh,
371353
tri_quad_mesh,
372354
raw_reconstruction: reconstruction,
@@ -410,23 +392,9 @@ pub fn reconstruction_pipeline_py_f64<'py>(
410392
)
411393
.unwrap();
412394

413-
let tri_mesh = if let Some(tri_mesh) = tri_mesh {
414-
Some(TriMeshWithDataF64::new(tri_mesh))
415-
} else {
416-
None
417-
};
418-
419-
let tri_quad_mesh = if let Some(tri_quad_mesh) = tri_quad_mesh {
420-
Some(MixedTriQuadMeshWithDataF64::new(tri_quad_mesh))
421-
} else {
422-
None
423-
};
424-
425-
let reconstruction = if let Some(reconstruction) = reconstruction {
426-
Some(SurfaceReconstructionF64::new(reconstruction))
427-
} else {
428-
None
429-
};
430-
431-
(tri_mesh, tri_quad_mesh, reconstruction)
395+
Ok((
396+
tri_mesh.map(TriMeshWithDataF64::new),
397+
tri_quad_mesh.map(MixedTriQuadMeshWithDataF64::new),
398+
reconstruction.map(SurfaceReconstructionF64::new),
399+
))
432400
}

splashsurf/src/cli.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ impl Switch {
8282

8383
/// Runs the splashsurf CLI with the provided command line arguments.
8484
///
85-
/// This function behaves like the binary `splashsurf` command line tool including output to stdout and stderr.
85+
/// This function behaves like the binary `splashsurf` command line tool including output to stdout
86+
/// and stderr. It will also exit the process depending on the command line arguments, so it should
87+
/// not be used in typical library contexts.
8688
/// Note that the first argument is always ignored - this is typically the binary name when called using
8789
/// `std::env::args()` from the terminal:
8890
/// ```

splashsurf/src/lib.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
1+
//! Library target of the `splashsurf` CLI.
2+
//!
3+
//! To use the CLI you can install it via Cargo:
4+
//! ```bash
5+
//! cargo install splashsurf
6+
//! ```
7+
//! For documentation of the CLI see the [README](https://github.com/InteractiveComputerGraphics/splashsurf) in the project repository.
8+
//!
9+
//! This library target exposes some high-level functionality such as [running the CLI](cli) with a
10+
//! list of command line arguments or specifically running the [reconstruction pipeline](reconstruct)
11+
//! of the CLI (including a set of postprocessing steps) as a library function.
12+
//! This functionality is mainly used by the `pySplashsurf` Pythong bindings and the CLI binary
13+
//! target itself.
14+
//!
15+
//! If you only want to use a subset of this functionality (e.g. only the reconstruction itself,
16+
//! without any postprocessing) in your crates, please refer to the [`splashsurf_lib`] crate instead.
17+
118
pub mod cli;
219
mod convert;
320
mod io;
4-
mod reconstruct;
21+
pub mod reconstruct;
522
#[macro_use]
623
mod allocator;
724
mod logging;
825

9-
pub use reconstruct::arguments::ReconstructionRunnerPostprocessingArgs;
10-
pub use reconstruct::{PipelineResult, reconstruction_pipeline};
1126
pub(crate) use register_counting_allocator;

0 commit comments

Comments
 (0)