Skip to content

Commit af750a6

Browse files
committed
Simplify some type conversions
1 parent 829d2f4 commit af750a6

File tree

11 files changed

+50
-45
lines changed

11 files changed

+50
-45
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pysplashsurf/src/pipeline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ fn reconstruction_pipeline_generic<I: Index, R: Real>(
124124

125125
let particle_rest_density = rest_density;
126126
let particle_rest_volume =
127-
R::from_f64((4.0 / 3.0) * std::f64::consts::PI).unwrap() * particle_radius.powi(3);
127+
R::from_float(4.0) * R::frac_pi_3() * particle_radius.powi(3);
128128
let particle_rest_mass = particle_rest_volume * particle_rest_density;
129129

130130
let particle_densities = reconstruction

splashsurf/src/reconstruction.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -999,8 +999,8 @@ pub(crate) fn reconstruction_pipeline_generic<I: Index, R: Real>(
999999
);
10001000

10011001
let particle_rest_density = params.rest_density;
1002-
let particle_rest_volume = R::from_f64((4.0 / 3.0) * std::f64::consts::PI).unwrap()
1003-
* params.particle_radius.powi(3);
1002+
let particle_rest_volume =
1003+
R::from_float(4.0) * R::frac_pi_3() * params.particle_radius.powi(3);
10041004
let particle_rest_mass = particle_rest_volume * particle_rest_density;
10051005

10061006
let particle_densities = reconstruction

splashsurf_lib/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ io = ["vtk_extras", "vtkio", "ply-rs", "nom", "serde_json", "flate2"]
3333
[dependencies]
3434
log = "0.4"
3535
nalgebra = { version = "0.33", features = ["rand", "bytemuck"] }
36+
simba = "0.9.0"
3637
num-traits = "0.2"
3738
num-integer = "0.1"
3839
anyhow = "1.0"

splashsurf_lib/src/dense_subdomains.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub(crate) struct ParametersSubdomainGrid<I: Index, R: Real> {
6262
surface_threshold: R,
6363
/// MC cube size (in simulation units)
6464
cube_size: R,
65-
/// Size of a subdomain in multiplies of MC cubes
65+
/// Size of a subdomain in multiples of MC cubes
6666
subdomain_cubes: I,
6767
/// Margin for ghost particles around each subdomain
6868
ghost_particle_margin: R,
@@ -130,7 +130,7 @@ pub(crate) fn initialize_parameters<I: Index, R: Real>(
130130
let cube_size = parameters.cube_size;
131131
let surface_threshold = parameters.iso_surface_threshold;
132132

133-
let particle_rest_volume = to_real!(4) * R::frac_pi_3() * particle_radius.powi(3);
133+
let particle_rest_volume = R::from_float(4.0) * R::frac_pi_3() * particle_radius.powi(3);
134134
let particle_rest_mass = particle_rest_volume * particle_rest_density;
135135

136136
let ghost_particle_margin =

splashsurf_lib/src/io/bgeo_format.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,8 @@ pub fn particles_from_bgeo_file<R: Real>(
4545
};
4646

4747
// Convert the array storage into individual vectors
48-
let positions: Vec<_> = position_storage
49-
.chunks(3)
50-
.map(|p| Vector3::new(p[0], p[1], p[2]).try_convert().unwrap())
51-
.collect();
52-
48+
let positions: Vec<_> =
49+
io_utils::try_convert_scalar_slice_to_vectors(position_storage, |v| v.try_convert())?;
5350
Ok(positions)
5451
}
5552

splashsurf_lib/src/kernel.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct CubicSplineKernel<R: Real> {
2626

2727
impl<R: Real> CubicSplineKernel<R> {
2828
/// Initializes a cubic spline kernel with the given compact support radius
29-
#[replace_float_literals(R::from_f64(literal).expect("Literal must fit in R"))]
29+
#[replace_float_literals(R::from_float(literal))]
3030
pub fn new(compact_support_radius: R) -> Self {
3131
let h = compact_support_radius;
3232
let sigma = 8.0 / (h * h * h);
@@ -38,7 +38,7 @@ impl<R: Real> CubicSplineKernel<R> {
3838
}
3939

4040
/// The cubic spline function used by the cubic spline kernel
41-
#[replace_float_literals(R::from_f64(literal).expect("Literal must fit in R"))]
41+
#[replace_float_literals(R::from_float(literal))]
4242
fn cubic_function(q: R) -> R {
4343
if q < R::one() {
4444
(3.0 / (2.0 * R::pi())) * ((2.0 / 3.0) - q * q + 0.5 * q * q * q)
@@ -51,7 +51,7 @@ impl<R: Real> CubicSplineKernel<R> {
5151
}
5252

5353
/// The derivative of the cubic spline function used by the cubic spline kernel w.r.t to the parameter `q`
54-
#[replace_float_literals(R::from_f64(literal).expect("Literal must fit in R"))]
54+
#[replace_float_literals(R::from_float(literal))]
5555
fn cubic_function_dq(q: R) -> R {
5656
if q < 1.0 {
5757
(3.0 / (4.0 * R::pi())) * (-4.0 * q + 3.0 * q * q)

splashsurf_lib/src/marching_cubes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ pub fn check_mesh_consistency<I: Index, R: Real>(
167167
.get_point(*cell_index.index())
168168
.expect("Unable to get point index of cell");
169169
let cell_center = grid.point_coordinates(&point_index)
170-
+ Vector3::repeat(grid.cell_size().times_f64(0.5));
170+
+ Vector3::repeat(grid.cell_size() * R::from_float(0.5));
171171

172172
error_strings.push(format!("\tTriangle {}, boundary edge {:?} is located in cell with {:?} with center coordinates {:?} and edge length {}.", tri_idx, edge, cell_index, cell_center, grid.cell_size()));
173173
} else {
@@ -246,7 +246,7 @@ fn check_mesh_with_cell_data<I: Index, R: Real>(
246246
.get_point(*cell_index.index())
247247
.expect("Unable to get point index of cell");
248248
let cell_center = grid.point_coordinates(&point_index)
249-
+ Vector3::repeat(grid.cell_size().times_f64(0.5));
249+
+ Vector3::repeat(grid.cell_size() * R::from_float(0.5));
250250

251251
let cell_data = marching_cubes_data
252252
.cell_data

splashsurf_lib/src/postprocessing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rayon::prelude::*;
1212
/// Laplacian Smoothing with feature weights
1313
///
1414
/// Move each vertex towards the mean position of its neighbors.
15-
/// Factor beta in \[0;1] proportional to amount of smoothing (for beta=1 each vertex is placed at the mean position).
15+
/// Factor beta in \[0;1] proportional to the amount of smoothing (for beta=1 each vertex is placed at the mean position).
1616
/// Additionally, feature weights can be specified to apply a varying amount of smoothing over the mesh.
1717
pub fn par_laplacian_smoothing_inplace<R: Real>(
1818
mesh: &mut TriMesh3d<R>,
@@ -668,7 +668,7 @@ pub fn convert_tris_to_quads<R: Real>(
668668

669669
let min_dot = normal_angle_limit_rad.cos();
670670
let max_non_squareness = non_squareness_limit;
671-
let sqrt_two = R::from_f64(2.0_f64.sqrt()).unwrap();
671+
let sqrt_two = R::from_float(2.0_f64.sqrt());
672672

673673
let tris_to_quad = |tri_i: &[usize; 3], tri_j: &[usize; 3]| -> [usize; 4] {
674674
let mut quad = [0, 0, 0, 0];

splashsurf_lib/src/reconstruction.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::mesh::TriMesh3d;
66
use crate::uniform_grid::UniformGrid;
77
use crate::workspace::LocalReconstructionWorkspace;
88
use crate::{
9-
Index, Parameters, Real, ReconstructionError, SurfaceReconstruction, density_map,
9+
Index, Parameters, Real, RealConvert, ReconstructionError, SurfaceReconstruction, density_map,
1010
marching_cubes, neighborhood_search, profile,
1111
};
1212
use anyhow::Context;
@@ -120,8 +120,8 @@ pub(crate) fn compute_particle_densities_and_neighbors<I: Index, R: Real>(
120120
profile!("compute_particle_densities_and_neighbors");
121121

122122
let particle_rest_density = parameters.rest_density;
123-
let particle_rest_volume = R::from_f64((4.0 / 3.0) * std::f64::consts::PI).unwrap()
124-
* parameters.particle_radius.powi(3);
123+
let particle_rest_volume =
124+
R::from_float(4.0) * R::frac_pi_3() * parameters.particle_radius.powi(3);
125125
let particle_rest_mass = particle_rest_volume * particle_rest_density;
126126

127127
trace!("Starting neighborhood search...");
@@ -153,8 +153,8 @@ pub(crate) fn reconstruct_single_surface_append<I: Index, R: Real>(
153153
output_mesh: &mut TriMesh3d<R>,
154154
) -> Result<(), ReconstructionError<I, R>> {
155155
let particle_rest_density = parameters.rest_density;
156-
let particle_rest_volume = R::from_f64((4.0 / 3.0) * std::f64::consts::PI).unwrap()
157-
* parameters.particle_radius.powi(3);
156+
let particle_rest_volume =
157+
R::from_float(4.0) * R::frac_pi_3() * parameters.particle_radius.powi(3);
158158
let particle_rest_mass = particle_rest_volume * particle_rest_density;
159159

160160
let particle_densities = {

0 commit comments

Comments
 (0)