示例#1
1
 private MWNumericArray transpose(MWNumericArray array)
 {
     double[] tmp = (double[])array.ToVector(MWArrayComponent.Real);
     return new MWNumericArray(tmp.Length, 1, tmp);
 }
示例#2
0
        public void GrayScaleImageTest(ImageData imageData)
        {
            byte[] a = Convert.FromBase64String(imageData.Base64);
            //MemoryStream memoryStream = new MemoryStream(a);
            //Image image = System.Drawing.Image.FromStream(memoryStream);
            Analyse        z = new Analyse();
            MWNumericArray mWNumericArray = a;
            MWArray        I     = z.analyseImage(mWNumericArray, imageData.CoordinateXY[0], imageData.CoordinateXY[1]);
            MWNumericArray I_num = I.ToArray();

            Byte[] I_bytes = (Byte[])I_num.ToVector(MWArrayComponent.Real);

            int w = I.Dimensions[0];
            int h = I.Dimensions[1];

            Bitmap       bmp = new Bitmap(w, h, PixelFormat.Format8bppIndexed);
            ColorPalette cp  = bmp.Palette;

            for (Int32 i = 0; i < 256; ++i)
            {
                cp.Entries[i] = Color.FromArgb(255, i, i, i);
            }

            bmp.Palette = cp;

            BitmapData data = bmp.LockBits((new Rectangle(0, 0, bmp.Width, bmp.Height)), ImageLockMode.WriteOnly, bmp.PixelFormat);

            Marshal.Copy(I_bytes, 0, data.Scan0, I_bytes.Length);
            bmp.UnlockBits(data);
            bmp.Save("image.png", ImageFormat.Png);
        }
示例#3
0
        public double[,] doFFT(double[] sinal, double freqAm)
        {
            double[] resY = new double[sinal.Length];
            double[] resX = new double[sinal.Length];
            double[,] res = new double[sinal.Length, 2];

            MWNumericArray aux_res2  = new MWNumericArray(new double[sinal.Length]);
            MWNumericArray aux_res2X = new MWNumericArray(new double[sinal.Length]);
            MWNumericArray xdt       = new MWNumericArray(new double[sinal.Length]);
            Array          aux_res3  = null;
            Array          aux_res3X = null;
            fftMatlab2     obj       = new fftMatlab2();

            xdt = sinal;

            MWArray aux_res  = obj.IB_fft(xdt, freqAm);
            MWArray aux_resX = obj.IB_fftx(xdt, freqAm);

            aux_res2X = (MWNumericArray)aux_resX;
            aux_res2  = (MWNumericArray)aux_res;

            aux_res3X = aux_res2X.ToVector(MWArrayComponent.Real);
            aux_res3  = aux_res2.ToVector(MWArrayComponent.Real);


            resY = (double[])aux_res3;
            resX = (double[])aux_res3X;
            for (int i = 0; i < resX.Length; i++)
            {
                res[i, 0] = resY[i]; //linha 0 valores de Y
                res[i, 1] = resX[i]; //linha 1 valores de X
            }

            return(res);
        }
示例#4
0
        public FittingResult Predict(Spectrum specInput, bool needFilter = true, int numOfId = 5, int topK = 1)
        {
            var spec = specInput.Clone();
            var y    = new MWNumericArray(specInput.Data.Lenght, 1, specInput.Data.Y);

            //进行预处理
            if (needFilter && this._filters != null)
            {
                y           = Preprocesser.ProcessForPredict(this._filters, y);
                spec.Data.Y = (double[])y.ToVector(MWArrayComponent.Real);
            }

            var handler = Tools.ModelHandler;


            var rlst    = handler.FitPredictor(5, this._lib.X, y, this._wind, (MWNumericArray)this._region.VarIndex, topK);
            var tq      = (MWNumericArray)rlst[0];
            var sq      = (MWNumericArray)rlst[1];
            var ratio   = (MWNumericArray)rlst[2];
            var idx     = (MWNumericArray)rlst[3];
            var fit     = (MWNumericArray)rlst[4];
            var fitspec = spec.Clone();

            fitspec.Components = this._lib.Components.Clone();
            fitspec.Data.Y     = (double[])fit.ToVector(MWArrayComponent.Real);
            var r = new FittingResult()
            {
                SpecOriginal = spec,
                SQ           = sq.ToScalarDouble(),
                TQ           = tq.ToScalarDouble(),
                FitSpec      = fitspec,
                MinSQ        = this._minSQ,
                MinTQ        = this._TQ,
                Wind         = this._wind,
                VarIndex     = this._region.VarIndex
            };
            var flst = new List <FittingSpec>();

            for (int i = 1; i <= ratio.NumberOfElements; i++)
            {
                if ((double)ratio[i] <= double.Epsilon)
                {
                    break;
                }
                flst.Add(new FittingSpec()
                {
                    Spec = this._lib[(int)idx[i] - 1],
                    Rate = (double)ratio[i]
                });
            }

            r.Specs = flst.ToArray();
            this.getResult(ref r);
            r.Result = r.TQ >= this._TQ && r.SQ >= this._minSQ;
            return(r);
        }
示例#5
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            ///define i/o parameters
            PointCloud Rhino_Cloud = new PointCloud();

            /// read inputs
            if (!DA.GetData("Point_Cloud", ref Rhino_Cloud))
            {
                return;
            }
            if (!DA.GetData("k", ref k))
            {
                return;
            }

            /// interal parameters
            var           Rhino_xyz = Rhino_Cloud.GetPoints();
            List <double> xyz       = new List <double>();

            if (!Rhino_Cloud.ContainsNormals)
            {
                for (int i = 0; i < Rhino_Cloud.Count; i++)
                {
                    xyz.Add(Rhino_xyz[i].X);
                    xyz.Add(Rhino_xyz[i].Y);
                    xyz.Add(Rhino_xyz[i].Z);
                }
            }

            ///2.
            var Matlab_xyz = new MWNumericArray(Rhino_Cloud.Count, 3, xyz.ToArray());



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

            MWArray array = new MWNumericArray();

            array = pointcloudutilities.G_compute_normals(Matlab_xyz, k);

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

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

            /// 5.%£%/:
            Resolution = dc[0];

            /// 6.
            DA.SetData(0, Resolution);
        }
示例#6
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            ///define i/o parameters
            PointCloud Rhino_Cloud = new PointCloud();
            double     Samples     = new double();
            double     Resolution  = new double();

            /// read inputs
            if (!DA.GetData("Point_Cloud", ref Rhino_Cloud))
            {
                return;
            }
            if (!DA.GetData("Samples", ref Samples))
            {
                return;
            }

            /// interal parameters
            var           Rhino_xyz = Rhino_Cloud.GetPoints();
            List <double> xyz       = new List <double>();

            for (int i = 0; i < Rhino_Cloud.Count; i++)
            {
                xyz.Add(Rhino_xyz[i].X);
                xyz.Add(Rhino_xyz[i].Y);
                xyz.Add(Rhino_xyz[i].Z);
            }

            ///2.
            var Matlab_samples = new MWNumericArray(Samples);
            var Matlab_xyz     = new MWNumericArray(Rhino_Cloud.Count, 3, xyz.ToArray());


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

            MWArray cluster = new MWNumericArray();

            cluster = segment_mesh.G_Resolution_PCD(Matlab_xyz, Matlab_samples);

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

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

            /// 5.%£%/:
            Resolution = dc[0];

            /// 6.
            DA.SetData(0, Resolution);
        }
示例#7
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            ///input parameters
            List <double> tform = new List <double>();

            /// initialise parameters

            ///import
            if (!DA.GetDataList("tform", tform))
            {
                return;
            }

            /// 0. error catching
            /// 1. decompose tform into translation
            /// 2. decompose tform in euler XYZ rotations
            /// 4. convert function output (list with indices) to .Net array (Matlab => .Net)
            /// 5. convert array to rhinocommon list. (.Net => Rhinocommon)
            /// 6. output data

            ///0.
            if (tform.Count != 16)
            {
                throw new Size_Exception(string.Format("Need[16, 1] parameters"));
            }
            /// 1.
            Vector3d T = new Vector3d(tform[3], tform[7], tform[11]);

            /// 2.
            var param = new MWNumericArray(16, 1, tform.ToArray());

            Scan2BIM_Matlab.General general = new General();
            MWArray rotXYZ = new MWNumericArray();

            rotXYZ = general.S2B_rotm2eul(param);

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

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

            /// 5.
            var      Rhino_rotXYZ = new List <double>(dc);
            Vector3d R            = new Vector3d(Rhino_rotXYZ[0], Rhino_rotXYZ[1], Rhino_rotXYZ[2]);

            /// 6.
            DA.SetData("R", R);
            DA.SetData("t", T);
        }
示例#8
0
        public double[] passaAlta(double[] sinal, int polos, int fc, int fs)
        {
            double[] res = new double[sinal.Length];

            MWNumericArray aux_res2 = new MWNumericArray(new double[sinal.Length]);
            MWNumericArray xdt      = new MWNumericArray(new double[sinal.Length]);
            Array          aux_res3 = null;
            Fltros         obj      = new Fltros();

            xdt = sinal;
            MWArray aux_res = obj.HighPassFilt(xdt, polos, fc, fs);

            aux_res2 = (MWNumericArray)aux_res;
            aux_res3 = aux_res2.ToVector(MWArrayComponent.Real);
            res      = (double[])aux_res3;
            return(res);
        }
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            ///input parameters
            PointCloud Rhino_Cloud1 = new PointCloud();
            PointCloud Rhino_Cloud2 = new PointCloud();

            /// initialise parameters
            double Metric = 0.0, InlierRatio = 0.2, MaxIterations = 20, Downsampling1 = 1.0, Downsampling2 = 1.0;

            ///import
            if (!DA.GetData("pc_moving", ref Rhino_Cloud1))
            {
                return;
            }
            if (!DA.GetData("pc_fixed", ref Rhino_Cloud2))
            {
                return;
            }
            if (!DA.GetData("Metric", ref Metric))
            {
                return;
            }
            if (!DA.GetData("InlierRatio", ref InlierRatio))
            {
                return;
            }
            if (!DA.GetData("MaxIterations", ref MaxIterations))
            {
                return;
            }
            if (!DA.GetData("Downsample pc_moving", ref Downsampling1))
            {
                return;
            }
            if (!DA.GetData("Downsample pc_fixed", ref Downsampling2))
            {
                return;
            }

            /// 0. error catching
            /// 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

            if (Rhino_Cloud1.Count <= 1 || Rhino_Cloud2.Count <= 1)
            {
                throw new Size_Exception(string.Format("Use point clouds with more than 1 point"));
            }
            if (Downsampling1 == 0 || Downsampling2 == 0)
            {
                throw new Size_Exception(string.Format("Do not use 0 as downsampling"));
            }

            /// 1.
            var Rhino_xyz1 = Rhino_Cloud1.GetPoints();
            var Rhino_xyz2 = Rhino_Cloud2.GetPoints();

            List <double> xyz1 = new List <double>();
            List <double> xyz2 = new List <double>();

            int interval1 = (int)(1 / Downsampling1);
            int interval2 = (int)(1 / Downsampling2);

            var Rhino_xyz11 = Rhino_xyz1
                              .Where((v, index) => index % interval1 == 0)
                              .ToList();
            var Rhino_xyz22 = Rhino_xyz2
                              .Where((v, index) => index % interval2 == 0)
                              .ToList();

            for (int i = 0; i < Rhino_xyz11.Count; i++)
            {
                xyz1.Add(Rhino_xyz11[i].X);
                xyz1.Add(Rhino_xyz11[i].Y);
                xyz1.Add(Rhino_xyz11[i].Z);
            }
            var Matlab_xyz1 = new MWNumericArray(Rhino_xyz11.Count, 3, xyz1.ToArray());

            for (int j = 0; j < Rhino_xyz22.Count; j++)
            {
                xyz2.Add(Rhino_xyz22[j].X);
                xyz2.Add(Rhino_xyz22[j].Y);
                xyz2.Add(Rhino_xyz22[j].Z);
            }
            var Matlab_xyz2 = new MWNumericArray(Rhino_xyz22.Count, 3, xyz2.ToArray());

            /// 3.
            Scan2BIM_Matlab.General general = new General();

            var mwca = (MWCellArray)general.S2B_ICP2(Matlab_xyz1, Matlab_xyz2, Metric, InlierRatio, MaxIterations);

            /// 4.
            MWNumericArray na0 = (MWNumericArray)mwca[1];

            double[] dc0 = (double[])na0.ToVector(0);

            MWNumericArray na1 = (MWNumericArray)mwca[2];

            double[] dc1 = (double[])na1.ToVector(0);


            /// 5.
            var Rhino_param0 = new List <double>(dc0);
            var Rhino_param1 = new List <double>(dc1);

            /// 6.
            DA.SetDataList(0, Rhino_param0);
            DA.SetDataList(1, Rhino_param1);
        }
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            ///define i/o parameters
            PointCloud Rhino_Cloud = new PointCloud();
            Double     k           = 6;

            /// read inputs
            if (!DA.GetData(0, ref Rhino_Cloud))
            {
                return;
            }
            if (!DA.GetData("k", ref k))
            {
                return;
            }


            if (Rhino_Cloud.Count <= k)
            {
                throw new Size_Exception(string.Format("Use point clouds with more than k points"));
            }

            /// interal parameters
            List <Vector3d> normals = new List <Vector3d>();

            if (!Rhino_Cloud.ContainsNormals)
            {
                var X = Rhino_Cloud.Select(x => x.X).ToList();
                var Y = Rhino_Cloud.Select(x => x.Y).ToList();
                var Z = Rhino_Cloud.Select(x => x.Z).ToList();

                ///2.
                var Matlab_X = new MWNumericArray(Rhino_Cloud.Count, 1, X.ToArray());
                var Matlab_Y = new MWNumericArray(Rhino_Cloud.Count, 1, Y.ToArray());
                var Matlab_Z = new MWNumericArray(Rhino_Cloud.Count, 1, Z.ToArray());

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

                var mwca = (MWCellArray)segment_mesh.G_Normals(Matlab_X, Matlab_Y, Matlab_Z, k);

                /// 4.
                MWNumericArray na0 = (MWNumericArray)mwca[1];
                double[]       dc0 = (double[])na0.ToVector(0);

                MWNumericArray na1 = (MWNumericArray)mwca[2];
                double[]       dc1 = (double[])na1.ToVector(0);

                MWNumericArray na2 = (MWNumericArray)mwca[3];
                double[]       dc2 = (double[])na2.ToVector(0);


                /// 5.
                var      Rhino_param0 = new List <double>(dc0);
                var      Rhino_param1 = new List <double>(dc1);
                var      Rhino_param2 = new List <double>(dc2);
                Vector3d R            = new Vector3d();
                for (int i = 0; i < Rhino_param0.Count; i++)
                {
                    R = new Vector3d(Rhino_param0[i], Rhino_param1[i], Rhino_param2[i]);
                    normals.Add(R);
                }
            }

            else
            {
                normals = Rhino_Cloud.GetNormals().ToList();
            }

            PointCloud Rhino_Cloud_out = new PointCloud();

            Rhino_Cloud_out.AddRange(Rhino_Cloud.GetPoints(), normals);
            GH_Cloud Rhino_Cloud_out2 = new GH_Cloud(Rhino_Cloud_out);

            /// 6.
            DA.SetData(0, Rhino_Cloud_out2);
        }
示例#11
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            ///define i/o parameters
            List <Point3d> Rhino_C = new List <Point3d>();
            List <Point3d> Rhino_P = new List <Point3d>();

            /// initialise internal parameters
            int k = 100, n = 4; double t = 0.015, w = 0.7, dw = 0.1;

            if (!DA.GetDataList <Point3d>("C", Rhino_C))
            {
                return;
            }
            if (!DA.GetDataList <Point3d>("P", Rhino_P))
            {
                return;
            }
            if (!DA.GetData("k", ref k))
            {
                return;
            }
            if (!DA.GetData("t", ref t))
            {
                return;
            }
            if (!DA.GetData("dw", ref dw))
            {
                return;
            }
            if (!DA.GetData("w", ref w))
            {
                return;
            }
            if (!DA.GetData("n", ref n))
            {
                return;
            }

            /// 1. decompose P,C into lists of coordinates (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. compute proper geometry based on n=2,3,...n
            /// 7. output data

            ///Point3d test = new Point3d();


            List <double> Cx = new List <double>();
            List <double> Cy = new List <double>();
            List <double> Cz = new List <double>();
            List <double> Px = new List <double>();
            List <double> Py = new List <double>();
            List <double> Pz = new List <double>();

            /// List<double> n = new List<double>();
            ///List<double> c= new List<double>();

            //MWNumericArray Matlab_Cx = new MWNumericArray();
            //MWNumericArray Matlab_Cy = new MWNumericArray();
            //MWNumericArray Matlab_Px = new MWNumericArray();
            //MWNumericArray Matlab_Py = new MWNumericArray();

            for (int i = 0; i < Rhino_C.Count; i++)
            {
                Cx.Add(Rhino_C[i].X);
                Cy.Add(Rhino_C[i].Y);
                Cz.Add(Rhino_C[i].Z);
            }
            for (int i = 0; i < Rhino_P.Count; i++)
            {
                Px.Add(Rhino_P[i].X);
                Py.Add(Rhino_P[i].Y);
                Pz.Add(Rhino_P[i].Z);
            }

            ///2.
            var Matlab_Cx = new MWNumericArray(Rhino_C.Count, 1, Cx.ToArray());
            var Matlab_Cy = new MWNumericArray(Rhino_C.Count, 1, Cy.ToArray());
            var Matlab_Px = new MWNumericArray(Rhino_P.Count, 1, Px.ToArray());
            var Matlab_Py = new MWNumericArray(Rhino_P.Count, 1, Py.ToArray());

            /// 3.
            Reconstruction.fittwall fitt_wall = new Reconstruction.fittwall();

            ///MWArray result = new MWNumericArray(4);
            var result = fitt_wall.G_fitwall(4, Matlab_Cx, Matlab_Cy, Matlab_Px, Matlab_Py, k, t, dw, w, n);

            /// 4.
            MWNumericArray out1 = (MWNumericArray)result[0];

            double[] Mx = (double[])out1.ToVector(0);

            MWNumericArray out2 = (MWNumericArray)result[1];

            double[] My = (double[])out2.ToVector(0);

            MWNumericArray out3 = (MWNumericArray)result[2];

            double[] inlrNum = (double[])out3.ToVector(0);

            MWNumericArray out4 = (MWNumericArray)result[3];

            double[] error = (double[])out4.ToVector(0);


            /// 5.
            var    Rhino_Mx      = new List <double>(Mx);
            var    Rhino_My      = new List <double>(My);
            double Rhino_inlrNum = inlrNum[0];
            double Rhino_error   = error[0];

            /// 6.
            List <Point3d> Rhino_M = new List <Point3d>();

            for (int i = 0; i < Rhino_Mx.Count; i++)
            {
                var temp = new Point3d(Rhino_Mx[i], Rhino_My[i], Cz.Min());
                Rhino_M.Add(temp);
            }

            if (Rhino_M.Count == 2)
            {
                var geometry = new Line(Rhino_M[0], Rhino_M[1]);
                DA.SetData(0, geometry);
            }
            if (Rhino_Mx.Count == 3)
            {
                var geometry = new Arc(Rhino_M[0], Rhino_M[1], Rhino_M[2]);
                DA.SetData(0, geometry);
            }
            else
            {
                var geometry = new Polyline(Rhino_M);
                DA.SetData(0, geometry);
            }


            /// 7.
            DA.SetData(1, Rhino_inlrNum);
            //DA.SetData(2, Rhino_error);
        }
示例#12
0
        public IdentifyResult Predict(Spectrum speciii, bool needFilter = true, int numOfId = 5, int topK = 1)
        {
            if (this._lib == null || speciii == null)
            {
                throw new ArgumentNullException("");
            }
            var spec = speciii.Clone();
            var y    = new MWNumericArray(speciii.Data.Lenght, 1, speciii.Data.Y);

            //进行预处理
            if (needFilter && this._filters != null)
            {
                y           = Preprocesser.ProcessForPredict(this._filters, y);
                spec.Data.Y = (double[])y.ToVector(MWArrayComponent.Real);
            }

            var handler = Tools.ModelHandler;

            //Tools.ToolHandler.Save(this._lib.X, "x", "x");
            //Tools.ToolHandler.Save(y, "y", "y");
            //Tools.ToolHandler.Save(this._baseLib.X, "xa", "xa");
            //this.LibBase.FullPath = "aaaa.lib";
            //this.LibBase.Save();


            this._maxNum = Math.Min(this._maxNum, this._lib.X.Dimensions[1]);
            var rlst  = handler.IdentifyPredictor(3, this._lib.X, y, this._wind, this._maxNum, this._p, this._w, this._t, topK);
            var minSQ = (MWNumericArray)rlst[0];
            var minTQ = (MWNumericArray)rlst[1];
            var ad    = (MWNumericArray)rlst[2];

            var r = new IdentifyResult()
            {
                Spec  = spec,
                MinSQ = this.MinSQ,
                MinTQ = this.TQ
            };
            //设置结果
            var itemlst = new List <IdentifyItem>();

            // r.Items = new IdentifyItem[ad.NumberOfElements];
            for (int i = 0; i < this._maxNum; i++)
            {
                var adidx = (int)ad[i + 1] - 1;
                if (adidx >= this._lib.Count || adidx < 0)
                {
                    break;
                }
                itemlst.Add(new IdentifyItem()
                {
                    Parent       = r,
                    TQ           = (double)minTQ[i + 1],
                    SQ           = (double)minSQ[i + 1],
                    Spec         = this._lib[adidx],
                    Wind         = this._wind,
                    SpecOriginal = r.Spec
                });
            }
            r.Items = itemlst.ToArray();
            r       = GetPredictValue(r, r.Items.Length, numOfId);
            r.IsId  = r.Items.Where(d => d.Result).Count() > 0;
            return(r);
        }
        public void execute()
        {
            double[,] nm;
            int m_b;// = noOfSuspects - 1;

            double[,] Phi_w = new double[height * width, noOfImages];
            int[,] t;
            double[,] one;
            double[,] mc;
            double[,] res;
            double[,] Phi_b;
            int person = 0;

            double[] eigenval;
            double[,] eigenvtr;
            double[] eigenval_sbt;
            double[,] eigenvtr_sbt;
            int[]    index;
            double[] aa;
            double[] bb;
            double[] tmp;
            double[,] D_b;
            double[,] mU_Sw_U;
            double[] evl;
            double[,] evr;
            double[,] A;
            double[] D_w;

            //int m_b;
            //int Sb_t;

            //static int l=height*width;
            //static int b=
            for (int i = 0; i < noOfSuspects; i++)
            {
                breadth = imagePerClass[i];
                t       = new int[height * width, breadth];
                one     = new double[1, breadth];
                for (int j = 0; j < breadth; j++)
                {
                    one[0, j] = 1;
                }

                mc = new double[height * width, 1];
                for (int j = 0; j < height * width; j++)
                {
                    mc[j, 0] = mean_c[j, i];
                    for (int k = 0; k < imagePerClass[i]; k++)
                    {
                        t[j, k] = images[j, person + k];
                    }
                }
                nm = new double[mc.GetLength(0) * one.GetLength(0), mc.GetLength(1) * one.GetLength(1)];
                k_t_p(ref mc, ref one, ref nm);
                res = new double[nm.GetLength(0), nm.GetLength(1)];

                for (int j = 0; j < nm.GetLength(0); j++)
                {
                    for (int k = 0; k < nm.GetLength(1); k++)
                    {
                        res[j, k] = t[j, k] - nm[j, k];
                    }
                }
                for (int j = 0; j < height * width; j++)
                {
                    for (int k = person; k < person + imagePerClass[i]; k++)
                    {
                        Phi_w[j, k] = res[j, k - person];
                    }
                }
                person = person + imagePerClass[i];
            }

            //int oo = 0;
            for (int j = 0; j < Phi_w.GetLength(0); j++)
            {
                for (int k = 0; k < Phi_w.GetLength(1); k++)
                {
                    Phi_w[j, k] = Phi_w[j, k] / Math.Sqrt(noOfImages);
                }
            }
            one = new double[1, noOfSuspects];
            for (int j = 0; j < noOfSuspects; j++)
            {
                one[0, j] = 1;
            }
            nm           = new double[mean_i.GetLength(0) * one.GetLength(0), mean_c.GetLength(1) * one.GetLength(1)];
            double[,] mi = new double[height * width, 1];
            for (int j = 0; j < mi.GetLength(0); j++)
            {
                mi[j, 0] = mean_i[j];
            }
            k_t_p(ref mi, ref one, ref nm);
            Phi_b = new double[mean_c.GetLength(0), mean_c.GetLength(1)];
            for (int j = 0; j < mean_c.GetLength(0); j++)
            {
                for (int k = 0; k < mean_c.GetLength(1); k++)
                {
                    Phi_b[j, k] = mean_c[j, k] - nm[j, k];
                }
            }

            for (int i = 0; i < noOfSuspects; i++)
            {
                for (int j = 0; j < Phi_b.GetLength(0); j++)
                {
                    Phi_b[j, i] = Phi_b[j, i] * Math.Sqrt((double)imagePerClass[i] / (double)noOfImages);
                }
            }
            nm  = new double[Phi_b.GetLength(1), Phi_b.GetLength(0)];
            res = new double[nm.GetLength(0), Phi_b.GetLength(1)];
            transpose(ref Phi_b, ref nm);
            matrix_multiply(ref nm, ref Phi_b, ref res);

            ///calculation of Eigenvector of
            MWArray       c  = null;
            Eutilityclass Ev = new Eutilityclass();

            c = Ev.Eigenvtr1((MWNumericArray)res);
            MWNumericArray D  = (MWNumericArray)c;
            Array          pA = D.ToVector(MWArrayComponent.Real);

            double[] doubleArray1 = (double[])pA;
            int      icount       = 0;

            nm = new double[res.GetLength(0), res.GetLength(1)];//eigenvector
            for (int i = 0; i < res.GetLength(0); i++)
            {
                for (int j = 0; j < res.GetLength(1); j++)
                {
                    nm[j, i] = doubleArray1[icount];
                    icount++;
                }
            }
            c        = Ev.Eigenvlu1((MWNumericArray)res);
            eigenval = new double[res.GetLength(0)];

            D            = (MWNumericArray)c;
            pA           = D.ToVector(MWArrayComponent.Real);
            doubleArray1 = (double[])pA;
            icount       = 0;
            int count = 0;

            for (int i = 0; i < res.GetLength(0); i++)
            {
                for (int j = 0; j < res.GetLength(1); j++)
                {
                    if (j == i)
                    {
                        eigenval[count] = Math.Abs(doubleArray1[icount]);
                        count++;
                    }
                    icount++;
                }
            }
            eigenval_sbt = new double[res.GetLength(1)];
            eigenval_sbt = (double[])eigenval.Clone();
            Array.Sort(eigenval);
            index = new int[res.GetLength(0)];

            for (int i = 0; i < eigenval_sbt.GetLength(0); i++)
            {
                index[i]        = eigenval_sbt.GetLength(0) - 1 - Array.IndexOf(eigenval, eigenval[i]);
                eigenval_sbt[i] = eigenval[eigenval_sbt.GetLength(0) - i - 1];
            }
            aa           = new double[res.GetLength(0)];
            eigenvtr_sbt = new double[res.GetLength(0), res.GetLength(1)];
            count        = 0;

            for (int i = 0; i < res.GetLength(0); i++)
            {
                for (int j = 0; j < res.GetLength(1); j++)
                {
                    eigenvtr_sbt[j, (int)index[i]] = nm[j, i];
                }
                aa[i] = eigenval_sbt[i] / eigenval_sbt[0];
                if (aa[i] < thresh_eigval_sb)
                {
                    count++;
                }
            }
            bb    = new double[count];
            m_b   = (int)((Math.Round(((double)(noOfSuspects - 1)) * remain_eigvec)));
            count = 0;
            //double temp;
            for (int i = 0; i < eigenval_sbt.GetLength(0); i++)
            {
                if (aa[i] < thresh_eigval_sb)
                {
                    bb[count] = i;
                    count++;
                }
                //temp=ei
            }
            //MessageBox.Show(""+m_b);
            eigenvtr = new double[eigenvtr_sbt.GetLength(0), m_b];
            eigenval = new double[m_b];
            D_b      = new double[m_b, m_b];
            for (int j = 0; j < m_b; j++)
            {
                for (int i = 0; i < eigenvtr_sbt.GetLength(0); i++)
                {
                    eigenvtr[i, j] = eigenvtr_sbt[i, j];
                    if (i == j)
                    {
                        D_b[i, j] = Math.Pow(eigenval_sbt[j], -1);
                    }
                }
                eigenval[j] = eigenval_sbt[j];
            }
            eigenvtr_sbt = new double[Phi_b.GetLength(0), eigenvtr.GetLength(1)];
            matrix_multiply(ref Phi_b, ref eigenvtr, ref eigenvtr_sbt);
            nm = new double[eigenvtr_sbt.GetLength(0), D_b.GetLength(0)]; ///Z
            matrix_multiply(ref eigenvtr_sbt, ref D_b, ref (nm));
            res = new double[nm.GetLength(1), nm.GetLength(0)];           //Z'
            transpose(ref nm, ref res);
            mc = new double[res.GetLength(0), Phi_w.GetLength(1)];        //mT
            matrix_multiply(ref res, ref Phi_w, ref mc);

            mU_Sw_U = new double[mc.GetLength(0), mc.GetLength(0)];
            res     = new double[mc.GetLength(1), mc.GetLength(0)];//mT'
            transpose(ref mc, ref res);
            matrix_multiply(ref mc, ref res, ref mU_Sw_U);
            Phi_w        = null;
            Phi_b        = null;
            D_b          = null;
            eigenvtr     = null;
            eigenvtr_sbt = null;


            icount       = 0;
            c            = Ev.Eigenvlu1((MWNumericArray)mU_Sw_U);
            evl          = new double[mU_Sw_U.GetLength(1)];
            D            = (MWNumericArray)c;
            pA           = D.ToVector(MWArrayComponent.Real);
            doubleArray1 = (double[])pA;
            for (int i = 0; i < mU_Sw_U.GetLength(0); i++)
            {
                for (int j = 0; j < mU_Sw_U.GetLength(1); j++)
                {
                    if (i == j)
                    {
                        evl[j] = Math.Abs(doubleArray1[icount]);
                    }
                    icount++;
                }
            }

            c            = Ev.Eigenvtr1((MWNumericArray)mU_Sw_U);
            evr          = new double[mU_Sw_U.GetLength(0), mU_Sw_U.GetLength(1)];
            D            = (MWNumericArray)c;
            pA           = D.ToVector(MWArrayComponent.Real);
            doubleArray1 = (double[])pA;
            count        = 0;
            for (int i = 0; i < (mU_Sw_U.GetLength(0)); i++)
            {
                for (int j = 0; j < (mU_Sw_U.GetLength(1)); j++)
                {
                    evr[j, i] = (doubleArray1[count]);
                    count++;
                }
            }
            tmp = new double[evl.GetLength(0)];
            Array.Sort(evl);
            index = new int[evl.GetLength(0)];

            for (int i = 0; i < evl.GetLength(0); i++)
            {
                index[i] = Array.IndexOf(evl, evl[i]);
                //tmp[i] = evl[evl.GetLength(0) - i-1];
            }
            //evl = (double[])tmp.Clone();
            eigenvtr = new double[evr.GetLength(0), evr.GetLength(1)];//U_Vec
            for (int i = 0; i < evr.GetLength(0); i++)
            {
                for (int j = 0; j < evr.GetLength(1); j++)
                {
                    eigenvtr[j, (int)index[i]] = evr[j, i];
                }
            }
            res = new double[nm.GetLength(0), eigenvtr.GetLength(1)];
            matrix_multiply(ref nm, ref eigenvtr, ref res);
            nm = null;
            A  = new double[res.GetLength(1), res.GetLength(0)];
            transpose(ref res, ref A);
            res      = null;
            evr      = null;
            eigenvtr = null;

            D_w = new double[evl.GetLength(0)];

            for (int i = 0; i < D_w.GetLength(0); i++)
            {
                //D_w[i]=eta_sw+evl[i];
                D_w[i] = Math.Pow(eta_sw + evl[i], -(1.0 / 2.0));
            }
            res = new double[D_w.GetLength(0), D_w.GetLength(0)];
            for (int i = 0; i < D_w.GetLength(0); i++)
            {
                res[i, i] = D_w[i];
            }
            D_b        = null;
            D_w        = null;
            DFLD_Trans = new double[res.GetLength(0), A.GetLength(1)];
            matrix_multiply(ref res, ref A, ref DFLD_Trans);
            res = null;
            A   = null;

            y = new double[DFLD_Trans.GetLength(0), mean_c.GetLength(1)];
            matrix_multiply(ref DFLD_Trans, ref mean_c, ref y);
            //int aaaa;
        }
示例#14
0
        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;
            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;
            }

            ///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();
            var C_xyz   = new float[Rhino_mesh.Faces.Count * 3];
            var C_n     = new float[Rhino_mesh.Faces.Count * 3];

            ///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;
            }

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

            /// 3.
            Scan2BIM_Matlab.Segmentation segmentation = new Scan2BIM_Matlab.Segmentation();

            MWArray cluster = new MWNumericArray();

            cluster = segmentation.S2B_RegionGrowing_2(Matlab_xyz, Matlab_n, T_N, T_C, D, M, O);

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

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

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

            /// 6.
            DA.SetDataList(0, Rhino_indices);
        }
示例#15
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            ///input parameters
            PointCloud Rhino_Cloud1 = new PointCloud();
            PointCloud Rhino_Cloud2 = new PointCloud();

            /// initialise parameters
            double Metric = 0.0, InlierRatio = 1.0, MaxIterations = 60;

            ///import
            if (!DA.GetData("pc_moving", ref Rhino_Cloud1))
            {
                return;
            }
            if (!DA.GetData("pc_fixed", ref Rhino_Cloud2))
            {
                return;
            }
            if (!DA.GetData("Metric", ref Metric))
            {
                return;
            }
            if (!DA.GetData("InlierRatio", ref InlierRatio))
            {
                return;
            }
            if (!DA.GetData("MaxIterations", ref MaxIterations))
            {
                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.
            var Rhino_xyz1 = Rhino_Cloud1.GetPoints();
            var Rhino_xyz2 = Rhino_Cloud2.GetPoints();

            List <double> xyz1 = new List <double>();
            List <double> xyz2 = new List <double>();

            for (int i = 0; i < Rhino_Cloud1.Count; i++)
            {
                xyz1.Add(Rhino_xyz1[i].X);
                xyz1.Add(Rhino_xyz1[i].Y);
                xyz1.Add(Rhino_xyz1[i].Z);
            }
            var Matlab_xyz1 = new MWNumericArray(Rhino_Cloud1.Count, 3, xyz1.ToArray());

            for (int j = 0; j < Rhino_Cloud2.Count; j++)
            {
                xyz2.Add(Rhino_xyz2[j].X);
                xyz2.Add(Rhino_xyz2[j].Y);
                xyz2.Add(Rhino_xyz2[j].Z);
            }
            var Matlab_xyz2 = new MWNumericArray(Rhino_Cloud2.Count, 3, xyz2.ToArray());

            /// 3.
            Scan2BIM_Matlab.General general = new Scan2BIM_Matlab.General();

            //MWArray[] Result = new MWArray[2];
            MWArray cluster = new MWNumericArray();

            cluster = general.S2B_ICP2(Matlab_xyz1, Matlab_xyz2, Metric, InlierRatio, MaxIterations);

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

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

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

            /// 6.
            DA.SetDataList(0, Rhino_param);
        }
        /// <summary>
        /// /////PCA Generation//////////
        /// </summary>
        public void pca()
        {
            for (int i = 0; i < height * width; i++)
            {
                float sum = 0;
                for (int j = 0; j < trainData; j++)
                {
                    sum += C[i, j];
                }
                mean[i] = (float)(sum / trainData);
            }
            //-------------deviation from mean----------------------
            for (int i = 0; i < height * width; i++)
            {
                for (int j = 0; j < trainData; j++)
                {
                    dC[i, j] = (C[i, j] - mean[i]);
                }
            }
            //--------------multiply transpose(dC)*dC---------------

            for (int i = 0; i < height * width; i++)
            {
                for (int j = 0; j < trainData; j++)
                {
                    tdC[j, i] = dC[i, j];
                }
            }

            for (int i = 0; i < trainData; i++)
            {
                for (int j = 0; j < trainData; j++)
                {
                    float sum = 0;
                    for (int k = 0; k < height * width; k++)
                    {
                        sum += tdC[i, k] * dC[k, j];
                    }
                    L[i, j] = sum / (trainData);
                }
            }
            //-------------------calculate Eigenvector of L----------------//
            MWArray       c  = null;
            Eutilityclass Ev = new Eutilityclass();

            c = Ev.Eigenvtr1((MWNumericArray)L);
            MWNumericArray D  = (MWNumericArray)c;
            Array          pA = D.ToVector(MWArrayComponent.Real);

            float[] doubleArray1 = (float[])pA;
            int     icount       = 0;

            for (int i = 0; i < trainData; i++)
            {
                for (int j = 0; j < trainData; j++)
                {
                    V[j, i] = doubleArray1[icount];
                    sV[j, trainData - i - 1] = doubleArray1[icount];
                    icount++;
                }
            }
            savp("realEig.txt", (float[, ])sV.Clone());

            /*
             * //Eigen value
             * c = Ev.Eigenvlu1((MWNumericArray)L);
             * float[] eigenval = new float[L.GetLength(0)];
             * D = (MWNumericArray)c;
             * pA = D.ToVector(MWArrayComponent.Real);
             * doubleArray1 = (float[])pA;
             * icount = 0;
             * int count = 0;
             * for (int i = 0; i < L.GetLength(0); i++)
             * {
             *  for (int j = 0; j < L.GetLength(1); j++)
             *  {
             *      if (j == i)
             *      {
             *          eigenval[count] = Math.Abs(doubleArray1[icount]);
             *          count++;
             *      }
             *      icount++;
             *  }
             * }
             * Vd = (float[])eigenval;
             * //----------------------V=dC*V----------------------------
             */
            float[] sm  = new float[trainData];
            float[] ssm = new float[trainData];



            temp  = matmul(ref dC, ref V);
            stemp = matmul(ref dC, ref sV);
            for (int i = 0; i < height * width; i++)
            {
                for (int j = 0; j < trainData; j++)
                {
                    sm[j]  += temp[i, j] * temp[i, j];
                    ssm[j] += stemp[i, j] * stemp[i, j];
                }
            }
            //Improving classification by dividing by length (i think mormalization)

            /*
             * for (int j = 0; j < trainData; j++)
             * {
             *
             *  sm[j] = (float)Math.Pow(sm[j], .5);
             *  ssm[j] = (float)Math.Pow(ssm[j], .5);
             *
             * }
             * */
            for (int i = 0; i < height * width; i++)
            {
                for (int j = 0; j < trainData; j++)
                {
                    temp[i, j]  = temp[i, j] / (float)Math.Pow(sm[j], .5);
                    stemp[i, j] = stemp[i, j] / (float)Math.Pow(ssm[j], .5);
                }
            }

            // obtaining the value of 'r'=tDc*V

            /*
             * float summ = 0;
             *
             * for (int i = 0; i < trainData; i++)
             * {
             *  for (int j = 0; j < trainData; j++)
             *  {
             *
             *      for (int k = 0; k < height * width; k++)
             *      {
             *          summ += tdC[i, k] * stemp[k, j];
             *
             *      }
             *      rc[i, j] = summ;
             *      summ = 0;
             *  }
             * }
             *
             */
            rc = matmul(ref tdC, ref stemp);
        }
示例#17
0
        /// <summary>
        /// 预测
        /// </summary>
        /// <param name="spec"></param>
        /// <param name="needFilter">是否需要前处理</param>
        /// <returns></returns>
        public PLS1Result Predict(Spectrum spec, bool needFilter = true, int numOfId = 5, int topK = 1)
        {
            if (spec == null)
            {
                throw new ArgumentNullException("");
            }
            var s = spec.Clone();

            if (s.Data == null || s.Data.Y == null || s.Data.Y.Length == 0)
            {
                return(null);
            }
            var x = new MWNumericArray(s.Data.Lenght, 1, s.Data.Y);

            //进行预处理
            if (needFilter && this._filters != null)
            {
                x        = Preprocesser.ProcessForPredict(this._filters, x);
                s.Data.Y = (double[])x.ToVector(MWArrayComponent.Real);
            }
            var c = this._comp.Clone();

            //定义变量
            double[] SR, MD, ND, Ylast;


            var handler = Tools.ModelHandler;

            if (this._annType != PLSAnnEnum.None)
            {
                var r           = handler.PLS1Predictor(5, x, this._Scores, this._Loads, this._Weights, this._Bias, this._Score_Length, this._centerSpecData, this._centerCompValue, (int)this._method);
                var toolHandler = Tools.ToolHandler;
                // SR,MD,nd,XScores
                Ylast = (double[])((MWNumericArray)r[0]).ToVector(MWArrayComponent.Real);
                SR    = (double[])((MWNumericArray)r[1]).ToVector(MWArrayComponent.Real);
                MD    = (double[])((MWNumericArray)r[2]).ToVector(MWArrayComponent.Real);
                ND    = (double[])((MWNumericArray)r[3]).ToVector(MWArrayComponent.Real);
                var mscores = (MWNumericArray)toolHandler.Centring(1, r[4], this._ScoresMean)[0];


                var annp = (MWNumericArray)handler.annp(1,
                                                        mscores,
                                                        this._annModel.w1,
                                                        this._annModel.b1,
                                                        this._annModel.w2,
                                                        this._annModel.b2,
                                                        this._annArgus.F1.GetDescription(),
                                                        this._annArgus.F2.GetDescription())[0];
                Ylast[this._factor - 1] = annp.ToScalarDouble() + this._centerCompValue;
            }
            else
            {
                var r = handler.PLS1Predictor(5, x, this._Scores, this._Loads, this._Weights, this._Bias, this._Score_Length, this._centerSpecData, this._centerCompValue, (int)this._method);
                Ylast = (double[])((MWNumericArray)r[0]).ToVector(MWArrayComponent.Real);
                SR    = (double[])((MWNumericArray)r[1]).ToVector(MWArrayComponent.Real);
                MD    = (double[])((MWNumericArray)r[2]).ToVector(MWArrayComponent.Real);
                ND    = (double[])((MWNumericArray)r[3]).ToVector(MWArrayComponent.Real);
            }
            c.PredictedValue = Ylast[this.Factor - 1];

            // c.PredictedValue = this._Nonnegative && c.PredictedValue < 0 ? 0 : c.PredictedValue;

            c.PredictedValue = this._Nonnegative && c.PredictedValue < 0 ? Math.Pow(10, -c.Eps)
                : c.PredictedValue;

            if (s.Components != null && s.Components.Contains(c.Name))
            {
                c.ActualValue = s.Components[c.Name].ActualValue;
                c.Error       = c.PredictedValue - c.ActualValue;
            }
            //加入判定条件,设置样式
            int errorcount = 0;

            if (MD[this._factor - 1] > this._MDMin)
            {
                errorcount++;
            }
            else if (SR[this._factor - 1] > this._SRMin)
            {
                errorcount++;
            }
            else if (ND[this._factor - 1] > this._NNDMin)
            {
                errorcount++;
            }
            switch (errorcount)
            {
            case 0:
                c.State = ComponentStatu.Pass;
                break;

            case 1:
                c.State = ComponentStatu.Green;
                break;

            case 2:
                c.State = ComponentStatu.Blue;
                break;

            default:
                c.State = ComponentStatu.Red;
                break;
            }
            PLS1Result item = new PLS1Result()
            {
                Spec    = s,
                MDMin   = this._MDMin,
                NDMin   = this._NNDMin,
                SRMin   = this._SRMin,
                Comp    = c,
                MahDist = MD,
                ND      = ND,
                SR      = SR,
                YLast   = Ylast,
                Factor  = this.Factor
            };

            return(item);
        }
示例#18
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            ///input parameters
            List <double> nodeFeature1 = new List <double>();
            List <double> nodeFeature2 = new List <double>();
            List <double> edgeFeature1 = new List <double>();
            List <double> edgeFeature2 = new List <double>();
            List <int>    Rhino_si     = new List <int>();
            List <int>    Rhino_sj     = new List <int>();
            List <double> w            = new List <double>();
            List <int>    Nstates      = new List <int>();

            ///import data
            if (!DA.GetDataList("nodeFeature1", nodeFeature1))
            {
                return;
            }
            if (!DA.GetDataList("nodeFeature2", nodeFeature2))
            {
                return;
            }
            if (!DA.GetDataList("edgeFeature1", edgeFeature1))
            {
                return;
            }
            if (!DA.GetDataList("edgeFeature2", edgeFeature2))
            {
                return;
            }
            if (!DA.GetDataList("Mesh_si", Rhino_si))
            {
                return;
            }
            if (!DA.GetDataList("Mesh_sj", Rhino_sj))
            {
                return;
            }
            if (!DA.GetDataList("Parameters", w))
            {
                return;
            }
            if (!DA.GetDataList("Nstates", Nstates))
            {
                return;
            }



            ///     0.cast list to MWarrays
            ///     1.run decode function Matlab
            ///     2.cast MWaray to list
            ///     3.output list

            ///0.
            var Matlab_fn1    = new MWNumericArray(nodeFeature1.Count, 1, nodeFeature1.ToArray());
            var Matlab_fn2    = new MWNumericArray(nodeFeature2.Count, 1, nodeFeature2.ToArray());
            var Matlab_fe1    = new MWNumericArray(edgeFeature1.Count, 1, edgeFeature1.ToArray());
            var Matlab_fe2    = new MWNumericArray(edgeFeature2.Count, 1, edgeFeature2.ToArray());
            var Matlab_si     = new MWNumericArray(Rhino_si.Count, 1, Rhino_si.ToArray());
            var Matlab_sj     = new MWNumericArray(Rhino_sj.Count, 1, Rhino_sj.ToArray());
            var Matlab_w      = new MWNumericArray(w.Count, 1, w.ToArray());
            var Matlab_states = new MWNumericArray(Nstates.Count, 1, Nstates.ToArray());


            /// 1.
            Clustering.cluster cluster_mesh = new Clustering.cluster();

            MWArray cluster = new MWNumericArray();

            cluster = cluster_mesh.G_CRF_wall_decoding(Matlab_states, Matlab_fn1, Matlab_fn2, Matlab_fe1, Matlab_fe2, Matlab_si, Matlab_sj, Matlab_w);

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

            var dc            = (int[])na.ToVector(0);
            var Rhino_indices = dc.ToList();

            for (int i = 0; i < Rhino_indices.Count; i++)
            {
                Rhino_indices[i] = Rhino_indices[i] - 1;
            }

            /// 3.
            DA.SetDataList(0, Rhino_indices);
        }
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            ///define i/o parameters
            PointCloud Rhino_Cloud = new PointCloud();
            double     Samples     = new double();
            double     Resolution  = new double();

            /// read inputs
            if (!DA.GetData("Point_Cloud", ref Rhino_Cloud))
            {
                return;
            }
            if (!DA.GetData("Samples", ref Samples))
            {
                return;
            }

            /// interal parameters
            var           Rhino_xyz = Rhino_Cloud.GetPoints();
            List <double> xyz       = new List <double>();
            List <double> x         = new List <double>();

            //if (x.Count != 0)
            //{
            //    var temp = x.Max();
            //    if (temp != 0)
            //    {
            //        var result = x.Select(k => k / temp).ToList(); ;
            //    }
            //    else
            //    {
            //        var result = x;
            //    }

            //    A = result;
            //}

            for (int i = 0; i < Rhino_Cloud.Count; i++)
            {
                xyz.Add(Rhino_xyz[i].X);
                xyz.Add(Rhino_xyz[i].Y);
                xyz.Add(Rhino_xyz[i].Z);
            }

            ///2.
            var Matlab_samples = new MWNumericArray(Samples);
            var Matlab_xyz     = new MWNumericArray(Rhino_Cloud.Count, 3, xyz.ToArray());


            /// 3.
            Scan2BIM_Matlab.General general = new General();

            MWArray cluster = new MWNumericArray();

            cluster = general.S2B_Resolution_PCD(Matlab_xyz, Matlab_samples);

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

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

            /// 5.%£%/:
            Resolution = dc[0];

            /// 6.
            DA.SetData(0, Resolution);
        }
示例#20
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            ///define i/o parameters
            PointCloud    Rhino_Cloud   = new PointCloud();
            List <double> Rhino_indices = new List <double>();

            /// initialise internal parameters
            double T_N = 0.0, T_C = 0.0, D = 0.0, M = 0.0, O = 0.0, T_s = 0.0;


            if (!DA.GetData("Point_Cloud", ref Rhino_Cloud))
            {
                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;
            }

            /// 1. decompose PCD into vertices, normals (rhinocommon => .NET)
            /// 1a. retrieve/construct colors
            /// 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

            var Rhino_xyz = Rhino_Cloud.GetPoints();
            var Rhino_n   = Rhino_Cloud.GetNormals();
            var Rhino_c   = Rhino_Cloud.GetColors();

            List <double>  xyz      = new List <double>();
            List <double>  n        = new List <double>();
            List <double>  c        = new List <double>();
            MWNumericArray Matlab_n = new MWNumericArray();
            MWNumericArray Matlab_c = new MWNumericArray();

            for (int i = 0; i < Rhino_Cloud.Count; i++)
            {
                xyz.Add(Rhino_xyz[i].X);
                xyz.Add(Rhino_xyz[i].Y);
                xyz.Add(Rhino_xyz[i].Z);
            }


            if (Rhino_Cloud.ContainsNormals == true)
            {
                for (int i = 0; i < Rhino_Cloud.Count; i++)
                {
                    n.Add(Rhino_n[i].X);
                    n.Add(Rhino_n[i].Y);
                    n.Add(Rhino_n[i].Z);
                }
                Matlab_n = new MWNumericArray(Rhino_Cloud.Count, 3, n.ToArray());
            }


            if (Rhino_Cloud.ContainsColors == true)
            {
                for (int i = 0; i < Rhino_Cloud.Count; i++)
                {
                    c.Add(Rhino_c[i].R);
                    c.Add(Rhino_c[i].G);
                    c.Add(Rhino_c[i].B);
                }
                Matlab_c = new MWNumericArray(Rhino_Cloud.Count, 3, c.ToArray());
            }


            ///2.
            var Matlab_xyz = new MWNumericArray(Rhino_Cloud.Count, 3, xyz.ToArray());


            /// 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);
        }
        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);
        }
示例#22
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);
        }
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            ///define i/o parameters
            List <double> GHArea             = new List <double>();
            List <double> GHNormalsimilarity = new List <double>();
            List <double> GHNormalZ          = new List <double>();
            List <double> GHDiagonalXY       = new List <double>();
            List <double> GHHeight           = new List <double>();
            List <double> GHCoplanarity      = new List <double>();
            List <double> GHProximity        = new List <double>();
            List <double> GHConnections      = new List <double>();
            List <double> GHWallinlier       = new List <double>();
            List <double> GHDvBottom         = new List <double>();
            List <double> GHDvTop            = new List <double>();
            //List<double> GHProxXY = new List<double>();
            List <double> GHColAbove    = new List <double>();
            List <double> GHColBelow    = new List <double>();
            List <double> GHColFarAbove = new List <double>();
            List <double> GHVbot        = new List <double>();
            List <double> GHVtop        = new List <double>();
            List <double> GHRaytrace    = new List <double>();

            if (!DA.GetDataList <double>("Area", GHArea))
            {
                return;
            }
            if (!DA.GetDataList <double>("NormalSimilarity", GHNormalsimilarity))
            {
                return;
            }
            if (!DA.GetDataList <double>("NormalZ", GHNormalZ))
            {
                return;
            }
            if (!DA.GetDataList <double>("DiagonalXY", GHDiagonalXY))
            {
                return;
            }
            if (!DA.GetDataList <double>("Height", GHHeight))
            {
                return;
            }
            if (!DA.GetDataList <double>("Coplanarity", GHCoplanarity))
            {
                return;
            }
            if (!DA.GetDataList <double>("Proximity", GHProximity))
            {
                return;
            }
            if (!DA.GetDataList <double>("Connections", GHConnections))
            {
                return;
            }
            if (!DA.GetDataList <double>("Wallinlier", GHWallinlier))
            {
                return;
            }
            if (!DA.GetDataList <double>("DvBottom", GHDvBottom))
            {
                return;
            }
            if (!DA.GetDataList <double>("DvTop", GHDvTop))
            {
                return;
            }
            if (!DA.GetDataList <double>("ColAbove", GHColAbove))
            {
                return;
            }
            if (!DA.GetDataList <double>("ColBelow", GHColBelow))
            {
                return;
            }
            if (!DA.GetDataList <double>("ColFarAbove", GHColFarAbove))
            {
                return;
            }
            if (!DA.GetDataList <double>("Vbot", GHVbot))
            {
                return;
            }
            if (!DA.GetDataList <double>("Vtop", GHVtop))
            {
                return;
            }
            if (!DA.GetDataList <double>("Raytrace", GHRaytrace))
            {
                return;
            }

            /// initialise internal parameters
            //convert C#List to C#Array and directly to MatlabArray
            var Area             = new MWNumericArray(GHArea.Count, 1, GHArea.ToArray());
            var Normalsimilarity = new MWNumericArray(GHNormalsimilarity.Count, 1, GHNormalsimilarity.ToArray());
            var NormalZ          = new MWNumericArray(GHNormalZ.Count, 1, GHNormalZ.ToArray());
            var DiagonalXY       = new MWNumericArray(GHDiagonalXY.Count, 1, GHDiagonalXY.ToArray());
            var Height           = new MWNumericArray(GHHeight.Count, 1, GHHeight.ToArray());

            var Coplanarity = new MWNumericArray(GHCoplanarity.Count, 1, GHCoplanarity.ToArray());
            var Proximity   = new MWNumericArray(GHProximity.Count, 1, GHProximity.ToArray());
            var Connections = new MWNumericArray(GHConnections.Count, 1, GHConnections.ToArray());
            var Wallinlier  = new MWNumericArray(GHWallinlier.Count, 1, GHWallinlier.ToArray());
            var DvBottom    = new MWNumericArray(GHDvBottom.Count, 1, GHDvBottom.ToArray());

            var DvTop       = new MWNumericArray(GHDvTop.Count, 1, GHDvTop.ToArray());
            var ColAbove    = new MWNumericArray(GHColAbove.Count, 1, GHColAbove.ToArray());
            var ColBelow    = new MWNumericArray(GHColBelow.Count, 1, GHColBelow.ToArray());
            var ColFarAbove = new MWNumericArray(GHColFarAbove.Count, 1, GHColFarAbove.ToArray());
            var Vbot        = new MWNumericArray(GHVbot.Count, 1, GHVbot.ToArray());

            var Vtop     = new MWNumericArray(GHVtop.Count, 1, GHVtop.ToArray());
            var Raytrace = new MWNumericArray(GHRaytrace.Count, 1, GHRaytrace.ToArray());

            Classification.Prediction predict = new Classification.Prediction();

            MWArray c = new MWNumericArray();

            c = predict.G_predictfunction(Area, Normalsimilarity, NormalZ, DiagonalXY, Height, Coplanarity, Proximity, Connections, Wallinlier, DvBottom, DvTop, ColAbove, ColBelow, ColFarAbove, Vbot, Vtop, Raytrace);

            //convert MatlabArray to C#Array to C#List
            MWNumericArray na = (MWNumericArray)c;

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

            List <double> output = new List <double>(dc);

            //// Finally assign the output to the output parameter.

            DA.SetDataList(0, output);
        }