From 5930b5c09d1a4a86773ba2cdfbf9c7138ba5afe4 Mon Sep 17 00:00:00 2001 From: Emmett Lalish Date: Thu, 2 Nov 2023 12:35:44 -0700 Subject: [PATCH] Fix NaN halfedgeTangents (#588) fixed CreateTangents --- src/manifold/src/smoothing.cpp | 9 ++++++--- test/sdf_test.cpp | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/manifold/src/smoothing.cpp b/src/manifold/src/smoothing.cpp index 8272eb0d3..77924baac 100644 --- a/src/manifold/src/smoothing.cpp +++ b/src/manifold/src/smoothing.cpp @@ -196,10 +196,13 @@ struct SmoothBezier { const glm::vec3 edgeNormal = (triNormal[edge.face] + triNormal[halfedge[edge.pairedHalfedge].face]) / 2.0f; - glm::vec3 dir = glm::normalize(glm::cross(glm::cross(edgeNormal, edgeVec), - vertNormal[edge.startVert])); + glm::vec3 dir = SafeNormalize(glm::cross(glm::cross(edgeNormal, edgeVec), + vertNormal[edge.startVert])); - const float weight = glm::abs(glm::dot(dir, glm::normalize(edgeVec))); + float weight = glm::abs(glm::dot(dir, SafeNormalize(edgeVec))); + if (weight == 0) { + weight = 1; + } // Quadratic weighted bezier for circular interpolation const glm::vec4 bz2 = weight * diff --git a/test/sdf_test.cpp b/test/sdf_test.cpp index 00f486250..23257ec3c 100644 --- a/test/sdf_test.cpp +++ b/test/sdf_test.cpp @@ -115,3 +115,21 @@ TEST(SDF, Resize) { EXPECT_EQ(layers.Status(), Manifold::Error::NoError); EXPECT_EQ(layers.Genus(), -8); } + +TEST(SDF, SineSurface) { + Mesh surface(LevelSet( + [](glm::vec3 p) { + float mid = glm::sin(p.x) + glm::sin(p.y); + return (p.z > mid - 0.5 && p.z < mid + 0.5) ? 1 : 0; + }, + {glm::vec3(-4 * glm::pi()), glm::vec3(4 * glm::pi())}, 1)); + Manifold smoothed = Manifold::Smooth(surface).Refine(2); + + EXPECT_EQ(smoothed.Status(), Manifold::Error::NoError); + EXPECT_EQ(smoothed.Genus(), -2); + +#ifdef MANIFOLD_EXPORT + if (options.exportModels) + ExportMesh("sinesurface.glb", smoothed.GetMeshGL(), {}); +#endif +} \ No newline at end of file