Skip to content

Commit a590c44

Browse files
committed
Add argument to set distance limit for snapping in MC cleanup
1 parent 051891d commit a590c44

2 files changed

Lines changed: 43 additions & 4 deletions

File tree

splashsurf/src/reconstruction.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ pub struct ReconstructSubcommandArgs {
186186
require_equals = true
187187
)]
188188
pub mesh_cleanup: Switch,
189+
/// If MC mesh cleanup is enabled, vertex snapping can be limited to this distance relative to the MC edge length (should be in range of [0.0,0.5])
190+
#[arg(
191+
help_heading = ARGS_DECIMATE,
192+
long,
193+
)]
194+
pub mesh_cleanup_snap_dist: Option<f64>,
189195
/// Enable decimation of some typical bad marching cubes triangle configurations (resulting in "barnacles" after Laplacian smoothing)
190196
#[arg(
191197
help_heading = ARGS_DECIMATE,
@@ -433,6 +439,7 @@ mod arguments {
433439
pub check_mesh_orientation: bool,
434440
pub check_mesh_debug: bool,
435441
pub mesh_cleanup: bool,
442+
pub mesh_cleanup_snap_dist: Option<f64>,
436443
pub decimate_barnacles: bool,
437444
pub keep_vertices: bool,
438445
pub compute_normals: bool,
@@ -564,6 +571,7 @@ mod arguments {
564571
|| args.check_mesh_orientation.into_bool(),
565572
check_mesh_debug: args.check_mesh_debug.into_bool(),
566573
mesh_cleanup: args.mesh_cleanup.into_bool(),
574+
mesh_cleanup_snap_dist: args.mesh_cleanup_snap_dist,
567575
decimate_barnacles: args.decimate_barnacles.into_bool(),
568576
keep_vertices: args.keep_verts.into_bool(),
569577
compute_normals: args.normals.into_bool(),
@@ -971,6 +979,9 @@ pub(crate) fn reconstruction_pipeline_generic<I: Index, R: Real>(
971979
vertex_connectivity = Some(splashsurf_lib::postprocessing::marching_cubes_cleanup(
972980
mesh_with_data.mesh.to_mut(),
973981
reconstruction.grid(),
982+
postprocessing
983+
.mesh_cleanup_snap_dist
984+
.map(|d| R::from_float(d)),
974985
5,
975986
postprocessing.keep_vertices,
976987
));

splashsurf_lib/src/postprocessing.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,15 @@ pub fn par_laplacian_smoothing_normals_inplace<R: Real>(
8787
pub fn marching_cubes_cleanup<I: Index, R: Real>(
8888
mesh: &mut TriMesh3d<R>,
8989
grid: &UniformCartesianCubeGrid3d<I, R>,
90+
max_rel_snap_distance: Option<R>,
9091
max_iter: usize,
9192
keep_vertices: bool,
9293
) -> Vec<Vec<usize>> {
9394
profile!("marching_cubes_cleanup");
9495

9596
let half_dx = grid.cell_size() / (R::one() + R::one());
97+
let max_snap_distance_sq =
98+
max_rel_snap_distance.map(|factor| (factor * grid.cell_size()).powi(2));
9699

97100
let nearest_grid_point = {
98101
profile!("determine nearest grid points");
@@ -154,10 +157,35 @@ pub fn marching_cubes_cleanup<I: Index, R: Real>(
154157
continue;
155158
}
156159

157-
for he in mesh.outgoing_half_edges(v0) {
158-
let v1 = he.to;
159-
if nearest_grid_point[v0] == nearest_grid_point[v1] {
160-
vertex_buffer.push(v1);
160+
let grid_point = grid
161+
.try_unflatten_point_index(nearest_grid_point[v0])
162+
.map(|p| grid.point_coordinates(&p));
163+
164+
if let Some(max_snap_distance_sq) = max_snap_distance_sq {
165+
if let Some(grid_point) = grid_point {
166+
// Check if this vertex is close enough to the grid vertex
167+
if (mesh.vertices[v0] - grid_point).norm_squared() <= max_snap_distance_sq {
168+
// Check for collapse with all neighbors
169+
for he in mesh.outgoing_half_edges(v0) {
170+
let v1 = he.to;
171+
if nearest_grid_point[v0] == nearest_grid_point[v1] {
172+
// Ensure that other vertex is close enough to grid vertex
173+
if (mesh.vertices[v1] - grid_point).norm_squared()
174+
<= max_snap_distance_sq
175+
{
176+
vertex_buffer.push(v1);
177+
}
178+
}
179+
}
180+
}
181+
}
182+
} else {
183+
// Check for collapse with all neighbors
184+
for he in mesh.outgoing_half_edges(v0) {
185+
let v1 = he.to;
186+
if nearest_grid_point[v0] == nearest_grid_point[v1] {
187+
vertex_buffer.push(v1);
188+
}
161189
}
162190
}
163191

0 commit comments

Comments
 (0)