Lofting 2 Polyline into a closed mesh correctly


#1

Hi,

I would like to ask how can I correctly loft two closed polylines into mesh, that shading would be right?

In the code below I have polylines that has a correct corresponding order of points, so they loft in a good way.
Then I create faces in between. The mesh seems right. But the shading not. How can I correctly create a mesh that shading would be right?

Unify normals and other cleaning functions not helping much.

    public static Mesh Loft(Polyline[] twoPolys, double weld, bool cap = true) {
        
        Mesh mesh = new Mesh();
        mesh.Vertices.AddVertices(twoPolys[0]);
        mesh.Vertices.AddVertices(twoPolys[1]);

        if (cap)
        {
            Mesh Top = MeshCreate.MeshFromPolylines(new[] {twoPolys[0]}, weld);
            Polyline p = new Polyline(twoPolys[1]);


            p.CollapseShortSegments(Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance);
            Mesh Bottom = Mesh.CreateFromClosedPolyline(p);
            Bottom.Flip(true, true, true);
            int[] tempV = Enumerable.Range(0, Bottom.Vertices.Count).ToArray();
            int[] tempF = Enumerable.Range(0, Bottom.Faces.Count).ToArray();
            Bottom.Ngons.AddNgon(MeshNgon.Create(tempV, tempF));
            Bottom.Normals.ComputeNormals();


            int n = twoPolys[1].Count;

            if (twoPolys[0].Count >= 2)
            {
                int f = mesh.Faces.Count - 1;
                if (twoPolys[0].Count == twoPolys[1].Count)
                    for (int j = 0; j < n - 1; j++)
                    {
                        mesh.Faces.AddFace( n + j, n + j + 1, j + 1,  j );
                        f++;
                        mesh.Ngons.AddNgon(MeshNgon.Create(new[]  {  n + j,  n + j + 1, j + 1,  j }, new[] {f}));
                    }
            }


            mesh.Append(Top);
            mesh.Append(Bottom);
        }
        else
        {
            int n = twoPolys[1].Count;

            if (twoPolys[0].Count >= 2) {
                int f = mesh.Faces.Count - 1;
                if (twoPolys[0].Count == twoPolys[1].Count)
                    for (int j = 0; j < n - 1; j++) {
                        mesh.Faces.AddFace( n + j, n + j + 1,  j + 1, j );
                        f++;
                        mesh.Ngons.AddNgon(MeshNgon.Create(new[]  {  n + j,n + j + 1, j + 1,  j   }, new[] { f }));
                    }
            }
        }


        //This is optional.
        if (weld > 0)
            mesh.WeldFull(weld);
        else
            mesh.Flip(true,true,true);

        return mesh;
    }

(Dale Fugier) #2

Can you post one of these meshes?