Example #1
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            ///input parameters
            List <Point3d> Rhino_points = new List <Point3d>();


            /// initialise parameters
            double        T_N = 0.0, D = 0.0, M = 0.0, O = 0.0, T_s = 0.0;
            List <double> Rhino_indices = new List <double>();
            List <double> Rhino_xyz     = new List <double>();

            ///import data
            if (!DA.GetDataList("Points", Rhino_points))
            {
                return;
            }
            if (!DA.GetData("ThresValN", ref T_N))
            {
                return;
            }
            if (!DA.GetData("MaxDist", ref D))
            {
                return;
            }
            if (!DA.GetData("Minsize", ref M))
            {
                return;
            }
            if (!DA.GetData("Offset", ref O))
            {
                return;
            }
            if (!DA.GetData("Tilesize", ref T_s))
            {
                return;
            }

            /// 1. decompose points into doubles (rhinocommon => .NET)
            /// 2. create appropriete matlab arrays (.NET => Matlab)
            /// 3. run matlab function (Matlab)
            /// 4. convert function output (list with indices) to .Net array (Matlab => .Net)
            /// 5. convert array to rhinocommon list. (.Net => Rhinocommon)
            /// 6. output data

            /// 1.
            for (int i = 0; i < Rhino_points.Count; i++)
            {
                Rhino_xyz[i * 3]     = Rhino_points[i].X;
                Rhino_xyz[i * 3 + 1] = Rhino_points[i].Y;
                Rhino_xyz[i * 3 + 2] = Rhino_points[i].Z;
            }

            /// 2.
            var Matlab_xyz = new MWNumericArray(Rhino_points.Count, 3, Rhino_xyz.ToArray());
            var Matlab_n   = new MWNumericArray();
            var Matlab_c   = new MWNumericArray();

            /// 3.
            Segmentation.segment segment_mesh = new Segmentation.segment();

            MWArray cluster = new MWNumericArray();

            cluster = segment_mesh.G_RegionGrowingNC2(Matlab_xyz, Matlab_n, Matlab_c, T_N, D, M, O, T_s);

            /// 4.
            MWNumericArray na = (MWNumericArray)cluster;

            double[] dc = (double[])na.ToVector(0);

            /// 5.
            Rhino_indices = new List <double>(dc);

            /// 6.
            DA.SetDataList(0, Rhino_indices);
        }
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            ///input parameters
            Mesh Rhino_mesh = new Mesh();

            /// initialise parameters
            double        T_N = 0.0, T_C = 0.0, D = 0.0, M = 0.0, O = 0.0, T_s = 0.0;
            List <double> Rhino_indices = new List <double>();

            ///import data
            if (!DA.GetData("Mesh", ref Rhino_mesh))
            {
                return;
            }
            if (!DA.GetData("ThresValN", ref T_N))
            {
                return;
            }
            if (!DA.GetData("ThresValC", ref T_C))
            {
                return;
            }
            if (!DA.GetData("MaxDist", ref D))
            {
                return;
            }
            if (!DA.GetData("Minsize", ref M))
            {
                return;
            }
            if (!DA.GetData("Offset", ref O))
            {
                return;
            }
            if (!DA.GetData("Tilesize", ref T_s))
            {
                return;
            }
            ///get mesh data to Matlab
            /// 1. decompose mesh into face centroids/normals (rhinocommon => .NET)
            /// 1a. compute normals if not present
            /// 2. create appropriete matlab arrays (.NET => Matlab)
            /// 3. run matlab function (Matlab)
            /// 4. convert function output (list with indices) to .Net array (Matlab => .Net)
            /// 5. convert array to rhinocommon list. (.Net => Rhinocommon)
            /// 6. output data

            /// 1.
            var      Rhino_c    = new Point3f();
            var      Rhino_n    = new Vector3f();
            MeshFace Rhino_face = new MeshFace();

            var C_xyz = new float[Rhino_mesh.Faces.Count * 3];
            var C_n = new float[Rhino_mesh.Faces.Count * 3];
            var C_c = new float[Rhino_mesh.Faces.Count * 3];
            int r1, r2, r3, g1, g2, g3, b1, b2, b3, r, g, b;

            ///1a. check if normals are present
            if (Rhino_mesh.FaceNormals.Count == 0)
            {
                Rhino_mesh.FaceNormals.ComputeFaceNormals();
            }


            /// 2.
            for (int i = 0; i < Rhino_mesh.Faces.Count; i++)
            {
                Rhino_c          = (Point3f)Rhino_mesh.Faces.GetFaceCenter(i);
                C_xyz[i * 3]     = Rhino_c.X;
                C_xyz[i * 3 + 1] = Rhino_c.Y;
                C_xyz[i * 3 + 2] = Rhino_c.Z;

                Rhino_n        = Rhino_mesh.FaceNormals[i];
                C_n[i * 3]     = Rhino_n.X;
                C_n[i * 3 + 1] = Rhino_n.Y;
                C_n[i * 3 + 2] = Rhino_n.Z;

                Rhino_face = Rhino_mesh.Faces.GetFace(i);

                if (Rhino_mesh.VertexColors.Count > 0)
                {
                    r1             = Rhino_mesh.VertexColors[Rhino_face[0]].R;
                    g1             = Rhino_mesh.VertexColors[Rhino_face[0]].G;
                    b1             = Rhino_mesh.VertexColors[Rhino_face[0]].B;
                    r2             = Rhino_mesh.VertexColors[Rhino_face[1]].R;
                    g2             = Rhino_mesh.VertexColors[Rhino_face[1]].G;
                    b2             = Rhino_mesh.VertexColors[Rhino_face[1]].B;
                    r3             = Rhino_mesh.VertexColors[Rhino_face[2]].R;
                    g3             = Rhino_mesh.VertexColors[Rhino_face[2]].G;
                    b3             = Rhino_mesh.VertexColors[Rhino_face[2]].B;
                    r              = (r1 + r2 + r3) / 3;
                    g              = (g1 + g2 + g3) / 3;
                    b              = (b1 + b2 + b3) / 3;
                    C_c[i * 3]     = r;
                    C_c[i * 3 + 1] = g;
                    C_c[i * 3 + 2] = b;
                }
            }

            var Matlab_xyz = new MWNumericArray(Rhino_mesh.Faces.Count, 3, C_xyz);
            var Matlab_n   = new MWNumericArray(Rhino_mesh.Faces.Count, 3, C_n);
            var Matlab_c   = new MWNumericArray(Rhino_mesh.Faces.Count, 3, C_c);

            /// 3.
            Segmentation.segment segment_mesh = new Segmentation.segment();

            MWArray cluster = new MWNumericArray();

            cluster = segment_mesh.G_RegionGrowingNC2(Matlab_xyz, Matlab_n, Matlab_c, T_N, T_C, D, M, O, T_s);

            /// 4.
            MWNumericArray na = (MWNumericArray)cluster;

            double[] dc = (double[])na.ToVector(0);

            /// 5.
            Rhino_indices = new List <double>(dc);

            /// 6.
            DA.SetDataList(0, Rhino_indices);
        }