Skip to content

New API changes #5839

@seghier

Description

@seghier

Hi, today I tested the new version and the API has completely changed.
I tried updating some functions, but I’m lost with the new API especially when working with meshes.
It’s not clear how to access faces, triangles, counts, IDs, etc.

using Rhino.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using static MR.DotNet;

namespace MeshLibh
{
    public class MeshlibUtils
    {
        public static float RemapToRange(double input, double min, double max)
        {
            if (input < 0 || input > 1)
                throw new ArgumentOutOfRangeException(nameof(input), "Input must be in the range [0, 1]");

            return (float)(min + input * (max - min));
        }

        public static Rhino.Geometry.Mesh MeshLibToRhinoMesh(MR.DotNet.Mesh mesh)
        {
            Rhino.Geometry.Mesh rhinoMesh = new Rhino.Geometry.Mesh();
            try
            {
                foreach (MR.DotNet.Vector3f point in mesh.Points)
                    rhinoMesh.Vertices.Add(point.X, -point.Z, point.Y);

                foreach (ThreeVertIds threeVertIds in mesh.Triangulation)
                {
                    rhinoMesh.Faces.AddFace(threeVertIds.v0.Id, threeVertIds.v1.Id, threeVertIds.v2.Id);
                }

                rhinoMesh.Compact();
                rhinoMesh.RebuildNormals();
            }
            catch
            {
            }
            return rhinoMesh;
        }

        public static List<Point3d> MeshLibToPoints(MR.DotNet.Mesh mesh)
        {
            List<Point3d> points = new List<Point3d>();
            try
            {
                foreach (MR.DotNet.Vector3f point in mesh.Points)
                    points.Add(MrpointToRhinopoint(point));
            }
            catch
            {
            }
            return points;
        }

        public static Rhino.Geometry.Mesh MeshLidToRhinoMesh(MR.DotNet.Mesh mesh)
        {
            Rhino.Geometry.Mesh rhinoMesh = new Rhino.Geometry.Mesh();
            try
            {
                foreach (MR.DotNet.Vector3f point in mesh.Points)
                    rhinoMesh.Vertices.Add(MrpointToRhinopoint(point));

                foreach (ThreeVertIds threeVertIds in mesh.Triangulation)
                {
                    rhinoMesh.Faces.AddFace(threeVertIds.v0.Id, threeVertIds.v1.Id, threeVertIds.v2.Id);
                }

                rhinoMesh.Compact();
                rhinoMesh.RebuildNormals();
            }
            catch
            {
            }
            return rhinoMesh;
        }

        public static MR.DotNet.Mesh PointToMeshlibMesh(List<Point3d> points)
        {
            if (points == null || points.Count < 3)
            {
                return null;
            }

            MR.DotNet.PointCloud pointCloud = new MR.DotNet.PointCloud();
            MR.DotNet.Mesh mesh = null;

            try
            {
                foreach (var p in points)
                {
                    if (double.IsNaN(p.X) || double.IsInfinity(p.X) ||
                        double.IsNaN(p.Y) || double.IsInfinity(p.Y) ||
                        double.IsNaN(p.Z) || double.IsInfinity(p.Z))
                    {
                        continue;
                    }

                    pointCloud.AddPoint(new MR.DotNet.Vector3f((float)p.X, (float)p.Y, (float)p.Z));
                }

                if (pointCloud.Points.Count < 3)
                {
                    return null;
                }

                mesh = TriangulatePointCloud(pointCloud, new TriangulationParameters());

                if (mesh == null)
                {
                    return null;
                }

                if (mesh.HoleRepresentiveEdges != null && mesh.HoleRepresentiveEdges.Count > 2)
                {
                    try
                    {
                        MR.DotNet.FillHoles(ref mesh, mesh.HoleRepresentiveEdges.ToList(), new FillHoleParams());
                    }
                    catch
                    {
                    }
                }

                return mesh;
            }
            catch
            {
                return null;
            }
            finally
            {
                (pointCloud as IDisposable)?.Dispose();
            }
        }

        private static List<MR.DotNet.Vector3f> MeshlibPoints(List<Point3d> points)
        {
            if (points == null || points.Count < 3)
            {
                return null;
            }

            List<MR.DotNet.Vector3f> pts = new List<MR.DotNet.Vector3f>();

            try
            {
                foreach (var p in points)
                {
                    if (double.IsNaN(p.X) || double.IsInfinity(p.X) ||
                        double.IsNaN(p.Y) || double.IsInfinity(p.Y) ||
                        double.IsNaN(p.Z) || double.IsInfinity(p.Z))
                    {
                        continue;
                    }
                    pts.Add(new MR.DotNet.Vector3f((float)p.X, (float)p.Y, (float)p.Z));
                }

                if (pts.Count < 3)
                {
                    return null;
                }
                return pts;
            }
            catch
            {
                return null;
            }
        }

        public static Rhino.Geometry.Mesh PcloudToMesh(MR.DotNet.PointCloud pointCloud, List<MR.DotNet.Color> colors)
        {
            try
            {
                var mesh = TriangulatePointCloud(pointCloud, new TriangulationParameters());
                return MeshLibToRhinoMesh(mesh, colors);
            }
            catch
            {
                return new Rhino.Geometry.Mesh();
            }
        }

        public static Rhino.Geometry.PointCloud GetPcloud(MR.DotNet.PointCloud pointCloud, List<MR.DotNet.Color> colors)
        {
            var pcloud = new Rhino.Geometry.PointCloud();
            try
            {
                var points = pointCloud.Points;
                for (int i = 0; i < colors.Count; i++)
                {
                    var color = colors[i];
                    pcloud.Add(
                        new Point3d(MrpointToRhinopoint(points[i])),
                        MrcolorToColor(color)
                    );
                }
            }
            catch
            {
            }
            return pcloud;
        }

        private static Rhino.Geometry.Mesh MeshLibToRhinoMesh(MR.DotNet.Mesh mesh, List<MR.DotNet.Color> colors)
        {
            var rhinoMesh = new Rhino.Geometry.Mesh();
            try
            {
                rhinoMesh.Vertices.Capacity = mesh.Points.Count;
                rhinoMesh.Faces.Capacity = mesh.Triangulation.Count;

                foreach (MR.DotNet.Vector3f point in mesh.Points)
                    rhinoMesh.Vertices.Add(point.X, point.Y, point.Z);

                foreach (ThreeVertIds threeVertIds in mesh.Triangulation)
                    rhinoMesh.Faces.AddFace(threeVertIds.v0.Id, threeVertIds.v1.Id, threeVertIds.v2.Id);

                for (int i = 0; i < Math.Min(colors.Count, rhinoMesh.Vertices.Count); i++)
                {
                    var color = colors[i];
                    rhinoMesh.VertexColors.Add(MrcolorToColor(color));
                }

                rhinoMesh.Compact();
                rhinoMesh.RebuildNormals();
            }
            catch
            {
            }
            return rhinoMesh;
        }

        private static Rhino.Geometry.Mesh MeshLidToRhinoMesh(MR.DotNet.Mesh mesh, List<System.Drawing.Color> colors)
        {
            Rhino.Geometry.Mesh rhinoMesh = new Rhino.Geometry.Mesh();
            try
            {
                foreach (MR.DotNet.Vector3f point in mesh.Points)
                    rhinoMesh.Vertices.Add(point.X, point.Y, point.Z);

                foreach (ThreeVertIds threeVertIds in mesh.Triangulation)
                {
                    rhinoMesh.Faces.AddFace(threeVertIds.v0.Id, threeVertIds.v1.Id, threeVertIds.v2.Id);
                }

                rhinoMesh.VertexColors.Clear();
                for (int i = 0; i < mesh.Points.Count; i++)
                {
                    System.Drawing.Color vertexColor = (i < colors.Count) ? colors[i] : System.Drawing.Color.Gray;
                    rhinoMesh.VertexColors.Add(vertexColor);
                }

                rhinoMesh.Compact();
                rhinoMesh.RebuildNormals();
            }
            catch
            {
            }
            return rhinoMesh;
        }

        public static Rhino.Geometry.Mesh ColoredMesh(Rhino.Geometry.PointCloud pcloud)
        {
            Rhino.Geometry.Mesh rhinoMesh = new Rhino.Geometry.Mesh();
            MR.DotNet.PointCloud pointCloud = new MR.DotNet.PointCloud();
            var colors = pcloud.GetColors();

            try
            {
                foreach (var p in pcloud.GetPoints())
                {
                    pointCloud.AddPoint(new MR.DotNet.Vector3f((float)p.X, (float)p.Y, (float)p.Z));
                }

                var mesh = TriangulatePointCloud(pointCloud, new TriangulationParameters());
                rhinoMesh = MeshLidToRhinoMesh(mesh, colors.ToList());
            }
            catch
            {
            }

            return rhinoMesh;
        }

        public static MR.DotNet.Mesh RhinoMeshToMeshLib(Rhino.Geometry.Mesh rhinoMesh)
        {
            rhinoMesh.Faces.ConvertQuadsToTriangles();

            List<MR.DotNet.Vector3f> points = new List<MR.DotNet.Vector3f>();
            List<ThreeVertIds> triangles = new List<ThreeVertIds>();

            for (int i = 0; i < rhinoMesh.Vertices.Count; i++)
            {
                var vertex = rhinoMesh.Vertices[i];
                points.Add(new MR.DotNet.Vector3f((float)vertex.X, (float)vertex.Y, (float)vertex.Z));
            }

            for (int i = 0; i < rhinoMesh.Faces.Count; i++)
            {
                var face = rhinoMesh.Faces[i];
                triangles.Add(new ThreeVertIds(face.A, face.B, face.C));
            }
            var resmesh = MR.DotNet.Mesh.FromTriangles(points, triangles);
            resmesh.PackOptimally();
            return resmesh;
        }

        public static List<Rhino.Geometry.Point3d> ConvertToRhinoPoints(List<MR.DotNet.Vector3f> mpoints)
        {
            List<Rhino.Geometry.Point3d> points = new List<Point3d>();
            foreach (var pt in mpoints)
            {
                points.Add(MrpointToRhinopoint(pt));
            }
            return points;
        }

        public static Point3d MrpointToRhinopoint(MR.DotNet.Vector3f mrpoint)
        {
            return new Point3d(mrpoint.X, mrpoint.Y, mrpoint.Z);
        }

        public static Vector3d MrvectorToRhinovector(MR.DotNet.Vector3i mrvec)
        {
            return new Vector3d(mrvec.X, mrvec.Y, mrvec.Z);
        }

        public static System.Drawing.Color MrcolorToColor(MR.DotNet.Color mrcol)
        {
            return System.Drawing.Color.FromArgb(mrcol.A, mrcol.R, mrcol.G, mrcol.B);
        }

        public static Rhino.Geometry.Box BoxiToBox(Box3i box)
        {
            var bbox = new BoundingBox((Point3d)MrvectorToRhinovector(box.Min), (Point3d)MrvectorToRhinovector(box.Max));
            var rbox = new Box(bbox);
            return rbox;
        }

        public static Box3i BoxToBoxi(Box box)
        {
            var min = new Vector3i((int)box.BoundingBox.Min.X,
                                   (int)box.BoundingBox.Min.Y,
                                   (int)box.BoundingBox.Min.Z);
            var max = new Vector3i((int)box.BoundingBox.Max.X,
                                   (int)box.BoundingBox.Max.Y,
                                   (int)box.BoundingBox.Max.Z);
            return new Box3i(min, max);
        }
    }
}

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions