gradientspace / geometry3Sharp

C# library for 2D/3D geometric computation, mesh algorithms, and so on. Boost license.
http://www.gradientspace.com
Boost Software License 1.0
1.69k stars 380 forks source link

MeshConstraintUtil.PreserveBoundaryLoops(r) cuts the corners off my Mesh #157

Open mfagerlund opened 3 years ago

mfagerlund commented 3 years ago

Hi, I've used the code presented in the tutorial at https://www.gradientspace.com/tutorials/2018/7/5/remeshing-and-constraints - and I really like the results of PreserveBoundaryLoops, except that it cuts the corners off my mesh.

image

When I use MeshConstraintUtil.FixAllBoundaryEdges(r) I get a result that's more in line with what I expected

image

Is this to be expected? The second method solves my immediate problem, which is great - thanks! But the first mesh looks nicer to me.

Here's the mesh: Makes A Sphere 2.zip

Here's the code I'm using:

using System;
using g3;
using NUnit.Framework;

namespace geometry3Tests
{
    [TestFixture]
    public class RemeshingTests
    {
        [Test]
        public void RemeshTest()
        {
            // https://www.gradientspace.com/tutorials/2018/7/5/remeshing-and-constraints
            var mesh = StandardMeshReader.ReadMesh(@"C:\Unity\Flattnr\Assets\Models\Makes A Sphere 2.stl");

            void Report(string stage)
            {
                Console.Error.WriteLine($"{stage}, Edges={mesh.EdgeCount}");
            }

            Report("Init");
            Remesher r = new Remesher(mesh) { PreventNormalFlips = true };
            //MeshConstraintUtil.FixAllBoundaryEdges(r);
            MeshConstraintUtil.PreserveBoundaryLoops(r);
            r.SetTargetEdgeLength(1.5);
            r.SmoothSpeedT = 0.1;
            r.SetProjectionTarget(MeshProjectionTarget.Auto(mesh));
            r.ProjectionMode = Remesher.TargetProjectionMode.Inline;

            for (int k = 0; k < 100; ++k)
            {
                r.BasicRemeshPass();
                Report($"Remesh Pass {k}");
            }

            MeshEditor.RemoveFinTriangles(mesh);
            Report("Remove Fin Triangles");

            StandardMeshWriter.WriteMesh(@"C:\Unity\Flattnr\Assets\Models\Makes A Sphere 2.rewrite.stl", mesh, new WriteOptions { bWriteBinary = true });
        }
    }
}
48design commented 3 years ago

Did you try removing the following line?

MeshEditor.RemoveFinTriangles(mesh)

This method sometimes removes more triangles than I wish for in my scenarios.

Mattias-NCVIB commented 3 years ago

Yes, without that method I couldn't use the mesh for my simulations because there were too many fin triangles left.

Mattias-NCVIB commented 3 years ago

Tested again - turns out that my corners are deleted whether I use "MeshEditor.RemoveFinTriangles(mesh);" or not :(

rms80 commented 3 years ago

the remesher doesn't know your corners are special (ie, it treats them just like any other vertex). You need to explicitly constrain them. For some info see https://github.com/gradientspace/geometry3Sharp/issues/160