From 67c696695db4793c32effb49bbaf58610b6d0507 Mon Sep 17 00:00:00 2001 From: Artem Makhno Date: Fri, 24 Nov 2023 13:52:16 +0300 Subject: [PATCH] Add clamp-like behavoiur when the parameter is not provided (#42) --- mesh_ops/MergeCoincidentEdges.cs | 15 +++++++++++---- spatial/PointSetHashtable.cs | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/mesh_ops/MergeCoincidentEdges.cs b/mesh_ops/MergeCoincidentEdges.cs index a836d21f..a291f844 100644 --- a/mesh_ops/MergeCoincidentEdges.cs +++ b/mesh_ops/MergeCoincidentEdges.cs @@ -13,7 +13,7 @@ public class MergeCoincidentEdges { public DMesh3 Mesh; - public double MergeDistance = MathUtil.ZeroTolerancef; + public double? MergeDistance = null; public bool OnlyUniquePairs = false; @@ -32,7 +32,6 @@ public MergeCoincidentEdges(DMesh3 mesh) public virtual bool Apply() { _mergeEdgesInfos.Clear(); - merge_r2 = MergeDistance * MergeDistance; // construct hash table for edge midpoints MeshBoundaryEdgeMidpoints pointset = new MeshBoundaryEdgeMidpoints(Mesh); @@ -41,7 +40,15 @@ public virtual bool Apply() if (Mesh.TriangleCount > 100000) hashN = 128; if (Mesh.TriangleCount > 1000000) hashN = 256; hash.Build(hashN); - + // If the MergeDistance is larger than CellSize, we get an error. + double mergeDistance = MergeDistance is null + ? MathUtil.ZeroTolerancef : MergeDistance.Value; + if (MergeDistance is null + && mergeDistance > hash.CellSize) + // If the distance hasn't been provided, + // we choose the maximum possible ball size + mergeDistance = hash.CellSize; + merge_r2 = mergeDistance * mergeDistance; Vector3d a = Vector3d.Zero, b = Vector3d.Zero; Vector3d c = Vector3d.Zero, d = Vector3d.Zero; @@ -54,7 +61,7 @@ public virtual bool Apply() { Vector3d midpt = Mesh.GetEdgePoint(eid, 0.5); int N; - while (hash.FindInBall(midpt, MergeDistance, buffer, out N) == false) + while (hash.FindInBall(midpt, mergeDistance, buffer, out N) == false) buffer = new int[buffer.Length]; if (N == 1 && buffer[0] != eid) throw new Exception("MergeCoincidentEdges.Apply: how could this happen?!"); diff --git a/spatial/PointSetHashtable.cs b/spatial/PointSetHashtable.cs index 0805dc16..4dfe4473 100644 --- a/spatial/PointSetHashtable.cs +++ b/spatial/PointSetHashtable.cs @@ -13,7 +13,7 @@ public class PointSetHashtable ShiftGridIndexer3 indexF; Vector3d Origin; - double CellSize; + public double CellSize { get; private set; } public PointSetHashtable(IPointSet points) {