Skip to content

Commit 1741d0e

Browse files
committed
Some code cleanup
1 parent afcacc0 commit 1741d0e

File tree

6 files changed

+53
-61
lines changed

6 files changed

+53
-61
lines changed

pysplashsurf/src/aabb.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,22 @@ macro_rules! create_aabb3d_interface {
3131

3232
/// Constructs the smallest AABB fitting around all the given points
3333
#[staticmethod]
34-
fn from_points<'py>(points: &Bound<'py, PyArray2<$type>>) -> $name {
35-
let points: PyReadonlyArray2<$type> = points.extract().unwrap();
36-
let points = points.as_slice().unwrap();
34+
fn from_points<'py>(points: &Bound<'py, PyArray2<$type>>) -> PyResult<$name> {
35+
let points: PyReadonlyArray2<$type> = points.extract()?;
36+
let points = points.as_slice()?;
3737
let points: &[Vector3<$type>] = bytemuck::cast_slice(points);
3838

39-
$name::new(Aabb3d::from_points(points))
39+
Ok($name::new(Aabb3d::from_points(points)))
4040
}
4141

4242
/// Constructs the smallest AABB fitting around all the given points, parallel version
4343
#[staticmethod]
44-
fn par_from_points<'py>(points: &Bound<'py, PyArray2<$type>>) -> $name {
45-
let points: PyReadonlyArray2<$type> = points.extract().unwrap();
46-
let points = points.as_slice().unwrap();
44+
fn par_from_points<'py>(points: &Bound<'py, PyArray2<$type>>) -> PyResult<$name> {
45+
let points: PyReadonlyArray2<$type> = points.extract()?;
46+
let points = points.as_slice()?;
4747
let points: &[Vector3<$type>] = bytemuck::cast_slice(points);
4848

49-
$name::new(Aabb3d::par_from_points(points))
49+
Ok($name::new(Aabb3d::par_from_points(points)))
5050
}
5151

5252
/// Constructs a degenerate AABB with min and max set to zero

pysplashsurf/src/marching_cubes.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ pub fn check_mesh_consistency_py_f32<'py>(
1919
check_manifold: bool,
2020
debug: bool,
2121
) -> PyResult<()> {
22-
if mesh.downcast_bound::<TriMesh3dF32>(py).is_ok() {
23-
let mesh = mesh.downcast_bound::<TriMesh3dF32>(py).unwrap();
22+
if let Ok(mesh) = mesh.downcast_bound::<TriMesh3dF32>(py) {
2423
splashsurf_lib::marching_cubes::check_mesh_consistency(
2524
&grid.inner,
2625
&mesh.borrow().inner,
@@ -29,8 +28,7 @@ pub fn check_mesh_consistency_py_f32<'py>(
2928
debug,
3029
)
3130
.map_err(|x| PyErr::new::<PyRuntimeError, _>(x))
32-
} else if mesh.downcast_bound::<TriMeshWithDataF32>(py).is_ok() {
33-
let mesh = mesh.downcast_bound::<TriMeshWithDataF32>(py).unwrap();
31+
} else if let Ok(mesh) = mesh.downcast_bound::<TriMeshWithDataF32>(py) {
3432
splashsurf_lib::marching_cubes::check_mesh_consistency(
3533
&grid.inner,
3634
&mesh.borrow().inner.mesh,
@@ -55,8 +53,7 @@ pub fn check_mesh_consistency_py_f64<'py>(
5553
check_manifold: bool,
5654
debug: bool,
5755
) -> PyResult<()> {
58-
if mesh.downcast_bound::<TriMesh3dF64>(py).is_ok() {
59-
let mesh = mesh.downcast_bound::<TriMesh3dF64>(py).unwrap();
56+
if let Ok(mesh) = mesh.downcast_bound::<TriMesh3dF64>(py) {
6057
splashsurf_lib::marching_cubes::check_mesh_consistency(
6158
&grid.inner,
6259
&mesh.borrow().inner,
@@ -65,8 +62,7 @@ pub fn check_mesh_consistency_py_f64<'py>(
6562
debug,
6663
)
6764
.map_err(|x| PyErr::new::<PyRuntimeError, _>(x))
68-
} else if mesh.downcast_bound::<TriMeshWithDataF64>(py).is_ok() {
69-
let mesh = mesh.downcast_bound::<TriMeshWithDataF64>(py).unwrap();
65+
} else if let Ok(mesh) = mesh.downcast_bound::<TriMeshWithDataF64>(py) {
7066
splashsurf_lib::marching_cubes::check_mesh_consistency(
7167
&grid.inner,
7268
&mesh.borrow().inner.mesh,

pysplashsurf/src/neighborhood_search.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ pub fn neighborhood_search_spatial_hashing_parallel_py_f64<'py>(
1111
domain: &Aabb3dF64,
1212
particle_positions: &Bound<'py, PyArray2<f64>>,
1313
search_radius: f64,
14-
) -> Vec<Vec<usize>> {
14+
) -> PyResult<Vec<Vec<usize>>> {
1515
let mut nl: Vec<Vec<usize>> = Vec::new();
1616

17-
let particle_positions: PyReadonlyArray2<f64> = particle_positions.extract().unwrap();
18-
let particle_positions = particle_positions.as_slice().unwrap();
17+
let particle_positions: PyReadonlyArray2<f64> = particle_positions.extract()?;
18+
let particle_positions = particle_positions.as_slice()?;
1919
let particle_positions: &[Vector3<f64>] = bytemuck::cast_slice(particle_positions);
2020

2121
neighborhood_search_spatial_hashing_parallel::<i64, f64>(
@@ -25,7 +25,7 @@ pub fn neighborhood_search_spatial_hashing_parallel_py_f64<'py>(
2525
&mut nl,
2626
);
2727

28-
nl
28+
Ok(nl)
2929
}
3030

3131
#[pyfunction]
@@ -35,11 +35,11 @@ pub fn neighborhood_search_spatial_hashing_parallel_py_f32<'py>(
3535
domain: &Aabb3dF32,
3636
particle_positions: &Bound<'py, PyArray2<f32>>,
3737
search_radius: f32,
38-
) -> Vec<Vec<usize>> {
38+
) -> PyResult<Vec<Vec<usize>>> {
3939
let mut nl: Vec<Vec<usize>> = Vec::new();
4040

41-
let particle_positions: PyReadonlyArray2<f32> = particle_positions.extract().unwrap();
42-
let particle_positions = particle_positions.as_slice().unwrap();
41+
let particle_positions: PyReadonlyArray2<f32> = particle_positions.extract()?;
42+
let particle_positions = particle_positions.as_slice()?;
4343
let particle_positions: &[Vector3<f32>] = bytemuck::cast_slice(particle_positions);
4444

4545
neighborhood_search_spatial_hashing_parallel::<i64, f32>(
@@ -49,5 +49,5 @@ pub fn neighborhood_search_spatial_hashing_parallel_py_f32<'py>(
4949
&mut nl,
5050
);
5151

52-
nl
52+
Ok(nl)
5353
}

pysplashsurf/src/post_processing.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,16 +268,14 @@ pub fn marching_cubes_cleanup_py_f64<'py>(
268268
max_iter: usize,
269269
keep_vertices: bool,
270270
) -> PyResult<Vec<Vec<usize>>> {
271-
if mesh.downcast_bound::<TriMesh3dF64>(py).is_ok() {
272-
let mesh = mesh.downcast_bound::<TriMesh3dF64>(py).unwrap();
271+
if let Ok(mesh) = mesh.downcast_bound::<TriMesh3dF64>(py) {
273272
Ok(splashsurf_lib::postprocessing::marching_cubes_cleanup(
274273
&mut mesh.borrow_mut().inner,
275274
&grid.inner,
276275
max_iter,
277276
keep_vertices,
278277
))
279-
} else if mesh.downcast_bound::<TriMeshWithDataF64>(py).is_ok() {
280-
let mesh = mesh.downcast_bound::<TriMeshWithDataF64>(py).unwrap();
278+
} else if let Ok(mesh) = mesh.downcast_bound::<TriMeshWithDataF64>(py) {
281279
Ok(splashsurf_lib::postprocessing::marching_cubes_cleanup(
282280
&mut mesh.borrow_mut().inner.mesh,
283281
&grid.inner,
@@ -299,16 +297,14 @@ pub fn marching_cubes_cleanup_py_f32<'py>(
299297
max_iter: usize,
300298
keep_vertices: bool,
301299
) -> PyResult<Vec<Vec<usize>>> {
302-
if mesh.downcast_bound::<TriMesh3dF32>(py).is_ok() {
303-
let mesh = mesh.downcast_bound::<TriMesh3dF32>(py).unwrap();
300+
if let Ok(mesh) = mesh.downcast_bound::<TriMesh3dF32>(py) {
304301
Ok(splashsurf_lib::postprocessing::marching_cubes_cleanup(
305302
&mut mesh.borrow_mut().inner,
306303
&grid.inner,
307304
max_iter,
308305
keep_vertices,
309306
))
310-
} else if mesh.downcast_bound::<TriMeshWithDataF32>(py).is_ok() {
311-
let mesh = mesh.downcast_bound::<TriMeshWithDataF32>(py).unwrap();
307+
} else if let Ok(mesh) = mesh.downcast_bound::<TriMeshWithDataF32>(py) {
312308
Ok(splashsurf_lib::postprocessing::marching_cubes_cleanup(
313309
&mut mesh.borrow_mut().inner.mesh,
314310
&grid.inner,

pysplashsurf/src/reconstruction.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,14 @@ pub fn reconstruct_surface_py<I: Index, R: Real>(
8585
aabb_max: Option<[R; 3]>,
8686
) -> SurfaceReconstruction<I, R> {
8787
let aabb;
88-
if aabb_min == None || aabb_max == None {
89-
aabb = None;
90-
} else {
88+
if let (Some(aabb_min), Some(aabb_max)) = (aabb_min, aabb_max) {
89+
// Convert the min and max arrays to Vector3
9190
aabb = Some(Aabb3d::new(
92-
Vector3::from(aabb_min.unwrap()),
93-
Vector3::from(aabb_max.unwrap()),
91+
Vector3::from(aabb_min),
92+
Vector3::from(aabb_max),
9493
));
94+
} else {
95+
aabb = None;
9596
}
9697

9798
let spatial_decomposition;
@@ -140,10 +141,10 @@ pub fn reconstruct_surface_py_f32<'py>(
140141
subdomain_num_cubes_per_dim: u32,
141142
aabb_min: Option<[f32; 3]>,
142143
aabb_max: Option<[f32; 3]>,
143-
) -> SurfaceReconstructionF32 {
144-
let particles: PyReadonlyArray2<f32> = particles.extract().unwrap();
144+
) -> PyResult<SurfaceReconstructionF32> {
145+
let particles: PyReadonlyArray2<f32> = particles.extract()?;
145146

146-
let particle_positions = particles.as_slice().unwrap();
147+
let particle_positions = particles.as_slice()?;
147148
let particle_positions: &[Vector3<f32>] = bytemuck::cast_slice(particle_positions);
148149

149150
let reconstruction = reconstruct_surface_py::<i64, f32>(
@@ -161,7 +162,7 @@ pub fn reconstruct_surface_py_f32<'py>(
161162
aabb_max,
162163
);
163164

164-
SurfaceReconstructionF32::new(reconstruction.to_owned())
165+
Ok(SurfaceReconstructionF32::new(reconstruction.to_owned()))
165166
}
166167

167168
#[pyfunction]
@@ -184,10 +185,10 @@ pub fn reconstruct_surface_py_f64<'py>(
184185
subdomain_num_cubes_per_dim: u32,
185186
aabb_min: Option<[f64; 3]>,
186187
aabb_max: Option<[f64; 3]>,
187-
) -> SurfaceReconstructionF64 {
188-
let particles: PyReadonlyArray2<f64> = particles.extract().unwrap();
188+
) -> PyResult<SurfaceReconstructionF64> {
189+
let particles: PyReadonlyArray2<f64> = particles.extract()?;
189190

190-
let particle_positions = particles.as_slice().unwrap();
191+
let particle_positions = particles.as_slice()?;
191192
let particle_positions: &[Vector3<f64>] = bytemuck::cast_slice(particle_positions);
192193

193194
let reconstruction = reconstruct_surface_py::<i64, f64>(
@@ -205,5 +206,5 @@ pub fn reconstruct_surface_py_f64<'py>(
205206
aabb_max,
206207
);
207208

208-
SurfaceReconstructionF64::new(reconstruction.to_owned())
209+
Ok(SurfaceReconstructionF64::new(reconstruction.to_owned()))
209210
}

pysplashsurf/src/sph_interpolation.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,29 +52,29 @@ macro_rules! create_sph_interpolator_interface {
5252
particle_quantity: Vec<$type>,
5353
interpolation_points: &Bound<'py, PyArray2<$type>>,
5454
first_order_correction: bool,
55-
) -> Vec<$type> {
55+
) -> PyResult<Vec<$type>> {
5656
let interpolation_points: PyReadonlyArray2<$type> =
57-
interpolation_points.extract().unwrap();
58-
let interpolation_points = interpolation_points.as_slice().unwrap();
57+
interpolation_points.extract()?;
58+
let interpolation_points = interpolation_points.as_slice()?;
5959
let interpolation_points: &[Vector3<$type>] =
6060
bytemuck::cast_slice(interpolation_points);
6161

62-
self.inner.interpolate_scalar_quantity(
62+
Ok(self.inner.interpolate_scalar_quantity(
6363
particle_quantity.as_slice(),
6464
interpolation_points,
6565
first_order_correction,
66-
)
66+
))
6767
}
6868

6969
/// Interpolates surface normals (i.e. normalized SPH gradient of the indicator function) of the fluid to the given points using SPH interpolation
7070
fn interpolate_normals<'py>(
7171
&self,
7272
py: Python<'py>,
7373
interpolation_points: &Bound<'py, PyArray2<$type>>,
74-
) -> Bound<'py, PyArray2<$type>> {
74+
) -> PyResult<Bound<'py, PyArray2<$type>>> {
7575
let interpolation_points: PyReadonlyArray2<$type> =
76-
interpolation_points.extract().unwrap();
77-
let interpolation_points = interpolation_points.as_slice().unwrap();
76+
interpolation_points.extract()?;
77+
let interpolation_points = interpolation_points.as_slice()?;
7878
let interpolation_points: &[Vector3<$type>] =
7979
bytemuck::cast_slice(interpolation_points);
8080

@@ -86,7 +86,7 @@ macro_rules! create_sph_interpolator_interface {
8686
let normals: ArrayView2<$type> =
8787
ArrayView::from_shape((normals.len() / 3, 3), normals).unwrap();
8888

89-
normals.to_pyarray(py)
89+
Ok(normals.to_pyarray(py))
9090
}
9191

9292
/// Interpolates a vectorial per particle quantity to the given points, panics if the there are less per-particles values than particles
@@ -96,16 +96,15 @@ macro_rules! create_sph_interpolator_interface {
9696
particle_quantity: &Bound<'py, PyArray2<$type>>,
9797
interpolation_points: &Bound<'py, PyArray2<$type>>,
9898
first_order_correction: bool,
99-
) -> Bound<'py, PyArray2<$type>> {
99+
) -> PyResult<Bound<'py, PyArray2<$type>>> {
100100
let interpolation_points: PyReadonlyArray2<$type> =
101-
interpolation_points.extract().unwrap();
102-
let interpolation_points = interpolation_points.as_slice().unwrap();
101+
interpolation_points.extract()?;
102+
let interpolation_points = interpolation_points.as_slice()?;
103103
let interpolation_points: &[Vector3<$type>] =
104104
bytemuck::cast_slice(interpolation_points);
105105

106-
let particle_quantity: PyReadonlyArray2<$type> =
107-
particle_quantity.extract().unwrap();
108-
let particle_quantity = particle_quantity.as_slice().unwrap();
106+
let particle_quantity: PyReadonlyArray2<$type> = particle_quantity.extract()?;
107+
let particle_quantity = particle_quantity.as_slice()?;
109108
let particle_quantity: &[Vector3<$type>] = bytemuck::cast_slice(particle_quantity);
110109

111110
let res_vec = self.inner.interpolate_vector_quantity(
@@ -119,7 +118,7 @@ macro_rules! create_sph_interpolator_interface {
119118
let res: ArrayView2<$type> =
120119
ArrayView::from_shape((res.len() / 3, 3), res).unwrap();
121120

122-
res.to_pyarray(py)
121+
Ok(res.to_pyarray(py))
123122
}
124123
}
125124
};

0 commit comments

Comments
 (0)