Example #1
0
        /// <summary>
        /// Parallel Computing Setup for Code to Execute.
        /// </summary>
        /// <param name="pointCloud">ByRef PointCloud to Execute Code on.</param>
        /// <returns></returns>
        public override bool Execute(ref PointCloud pointCloud)
        {
            //Set Global Variables
            LastPercentReported = 0;
            PointCounter        = 0;
            NewCloud            = new PointCloud();
            CloudPieces         = null;
            CloudPieces         = new PointCloud[ProcCount];
            DictPieces          = new object[ProcCount];
            GlobalCloud         = pointCloud;

            // Setup the cancellation mechanism.
            po.CancellationToken = cts.Token;

            //Create Partitions for multithreading.
            var rangePartitioner = System.Collections.Concurrent.Partitioner.Create(0, GlobalCloud.Count, (int)Math.Ceiling((double)GlobalCloud.Count / ProcCount));

            Dict_Utils.CastDictionary_ToArrayDouble(ref GlobalCloud);

            //Run MultiThreaded Loop.
            Parallel.ForEach(rangePartitioner, po, (rng, loopState) =>
            {
                //Initialize Local Variables.
                /// Get Index for Processor to be able to merge clouds in ProcesserIndex order in the end.
                int MyIndex = (int)(rng.Item1 / Math.Ceiling(((double)GlobalCloud.Count / ProcCount)));
                /// Initialize Partial PointCloud
                PointCloud MyCloud = new PointCloud();
                /// Get Total Count Fraction to calculate Operation Percentage.
                double totc = (double)1 / GlobalCloud.Count;

                /// Initialize Partial Dictionary Lists
                List <double>[] MyDict = new List <double> [GlobalCloud.UserDictionary.Count];
                //Get Dictionary Values from PointCloud
                List <double[]> GlobalDict = new List <double[]>();
                GlobalDict.Clear();

                if (GlobalCloud.UserDictionary.Count > 0)
                {
                    Dict_Utils.Initialize_Dictionary(ref GlobalDict, ref MyDict, GlobalCloud);
                    //Loop over individual RangePartitions per processor.
                    for (int i = rng.Item1; i < rng.Item2; i++)
                    {
                        //Operation Percentage Report
                        ///Safe Counter Increment.
                        Interlocked.Increment(ref PointCounter);
                        ///Calculate and Report Percentage.
                        if (LastPercentReported < ((PointCounter * totc) * 100))
                        {
                            LastPercentReported = (int)(5 * Math.Ceiling((double)(PointCounter * totc) * 20));
                            this.ReportPercent  = LastPercentReported;
                        }

                        //
                        if (insV_InsideBool)
                        {
                            for (int j = 0; j <= insV_Box.Count - 1; j += 1)
                            {
                                PointCloudItem GlobalCloudItem = GlobalCloud[i];
                                if (Math_Utils.IsInBox(GlobalCloudItem.Location, insV_Box[j]))
                                {
                                    Cloud_Utils.AddItem_FromOtherCloud(ref MyCloud, GlobalCloud, i);
                                    Dict_Utils.AddItem_FromGlobalDict(ref MyDict, GlobalDict, i);
                                    break; // TODO: might not be correct. Was : Exit For
                                }
                            }
                        }
                        else
                        {
                            List <Boolean> CropBools       = new List <Boolean>();
                            PointCloudItem GlobalCloudItem = GlobalCloud[i];
                            for (int j = 0; j <= insV_Box.Count - 1; j += 1)
                            {
                                CropBools.Add(Math_Utils.IsInBox(GlobalCloudItem.Location, insV_Box[j]));
                            }
                            bool Crop = !CropBools.Any(x => x == true);

                            if (Crop)
                            {
                                Cloud_Utils.AddItem_FromOtherCloud(ref MyCloud, GlobalCloud, i);
                                Dict_Utils.AddItem_FromGlobalDict(ref MyDict, GlobalDict, i);
                            }
                        }
                    }
                }
                else
                {
                    //Loop over individual RangePartitions per processor.
                    for (int i = rng.Item1; i < rng.Item2; i++)
                    {
                        //Operation Percentage Report
                        ///Safe Counter Increment.
                        Interlocked.Increment(ref PointCounter);
                        ///Calculate and Report Percentage.
                        if (LastPercentReported < ((PointCounter * totc) * 100))
                        {
                            LastPercentReported = (int)(5 * Math.Ceiling((double)(PointCounter * totc) * 20));
                            this.ReportPercent  = LastPercentReported;
                        }

                        //
                        if (insV_InsideBool)
                        {
                            for (int j = 0; j <= insV_Box.Count - 1; j += 1)
                            {
                                PointCloudItem GlobalCloudItem = GlobalCloud[i];
                                if (Math_Utils.IsInBox(GlobalCloudItem.Location, insV_Box[j]))
                                {
                                    Cloud_Utils.AddItem_FromOtherCloud(ref MyCloud, GlobalCloud, i);
                                    break; // TODO: might not be correct. Was : Exit For
                                }
                            }
                        }
                        else
                        {
                            List <Boolean> CropBools       = new List <Boolean>();
                            PointCloudItem GlobalCloudItem = GlobalCloud[i];
                            for (int j = 0; j <= insV_Box.Count - 1; j += 1)
                            {
                                CropBools.Add(Math_Utils.IsInBox(GlobalCloudItem.Location, insV_Box[j]));
                            }
                            bool Crop = !CropBools.Any(x => x == true);

                            if (Crop)
                            {
                                Cloud_Utils.AddItem_FromOtherCloud(ref MyCloud, GlobalCloud, i);
                            }
                        }
                    }
                }
                //Set DictPieces.
                if (GlobalCloud.UserDictionary.Count > 0)
                {
                    DictPieces[(int)MyIndex] = MyDict;
                }

                //Add MyCloud to CloudPieces at ProcesserIndex.
                CloudPieces[(int)MyIndex] = MyCloud;

                //Enable Parrallel Computing Cancellation
                po.CancellationToken.ThrowIfCancellationRequested();
            }
                             );



            //Merge PointCloud Pieces into One PointCloud.
            Cloud_Utils.MergeClouds(ref NewCloud, CloudPieces);

            if (GlobalCloud.UserDictionary.Count > 0)
            {
                List <double>[] NewDict = new List <double> [GlobalCloud.UserDictionary.Count];
                Dict_Utils.Merge_DictPieces(ref NewDict, DictPieces);
                Dict_Utils.SetUserDict_FromDictLists(ref NewCloud, NewDict, GlobalCloud.UserDictionary.Keys);
            }

            //Dispose of Global Clouds.
            GlobalCloud.Dispose();
            pointCloud.Dispose();

            //Set OutputCloud
            pointCloud = (PointCloud)NewCloud.Duplicate();

            //Dispose of PointCloud Pieces and NewCloud.
            CloudPieces = null;
            DictPieces  = null;
            NewCloud.Dispose();


            //Return True on Finish
            return(true);
        }
        /// <summary>
        /// Parallel Computing Setup for Code to Execute.
        /// </summary>
        /// <param name="pointCloud">ByRef PointCloud to Execute Code on.</param>
        /// <returns></returns>
        public override bool Execute(ref PointCloud pointCloud)
        {
            //Set Global Variables
            LastPercentReported = 0;
            PointCounter        = 0;

            if (insV_KDTree == null)
            {
                tree = KDTreeLib.ConstructKDTree(pointCloud);
            }
            else
            {
                tree = insV_KDTree;
            }

            Dict_Utils.CastDictionary_ToArrayDouble(ref pointCloud);
            GlobalCloud = pointCloud;

            NewCloud    = new PointCloud();
            CloudPieces = null;
            CloudPieces = new PointCloud[ProcCount];
            DictPieces  = new object[ProcCount];

            pts = pointCloud.GetPoints().ToList();

            // Setup the cancellation mechanism.
            po.CancellationToken = cts.Token;

            //Create Partitions for multithreading.
            var rangePartitioner = System.Collections.Concurrent.Partitioner.Create(0, GlobalCloud.Count, (int)Math.Ceiling((double)GlobalCloud.Count / ProcCount));

            Dict_Utils.CastDictionary_ToArrayDouble(ref GlobalCloud);

            //Search for NearestNeighbors per point in PointCloud. Gets the indices in the Cloud for the neighbors to each Point.
            List <double> NRange = new List <double> {
                double.MaxValue
            };

            List <int>[] idc = KDTreeLib.NearestNeighborSearch(tree, pts, NRange, insV_NumN);

            //Run MultiThreaded Loop.
            Parallel.ForEach(rangePartitioner, po, (rng, loopState) =>
            {
                //Initialize Local Variables.
                /// Get Index for Processor to be able to merge clouds in ProcesserIndex order in the end.
                int MyIndex = (int)(rng.Item1 / Math.Ceiling(((double)GlobalCloud.Count / ProcCount)));
                /// Initialize Partial PointCloud
                PointCloud MyCloud = new PointCloud();



                /// Get Total Count Fraction to calculate Operation Percentage.
                double totc = (double)1 / GlobalCloud.Count;
                int count   = 0;
                if (GlobalCloud.UserDictionary.Count > 0)
                {
                    /// Initialize Partial Dictionary Lists
                    List <double>[] MyDict = new List <double> [GlobalCloud.UserDictionary.Count];

                    //Get Dictionary Values from PointCloud
                    List <double[]> GlobalDict = new List <double[]>();
                    GlobalDict.Clear();

                    Dict_Utils.Initialize_Dictionary(ref GlobalDict, ref MyDict, GlobalCloud);

                    //Loop over individual RangePartitions per processor.
                    for (int i = rng.Item1; i < rng.Item2; i++)
                    {
                        //Operation Percentage Report
                        ///Safe Counter Increment.
                        Interlocked.Increment(ref PointCounter);
                        ///Calculate and Report Percentage.
                        if (LastPercentReported < ((PointCounter * totc) * 100))
                        {
                            LastPercentReported = (int)(5 * Math.Ceiling((double)(PointCounter * totc) * 20));
                            this.ReportPercent  = LastPercentReported;
                        }

                        calcNormal(idc, ref MyCloud, ref count, i);
                        Dict_Utils.AddItem_FromGlobalDict(ref MyDict, GlobalDict, i);
                    }

                    //Add MyCloud to CloudPieces at ProcesserIndex.
                    CloudPieces[(int)MyIndex] = MyCloud;
                    //Set DictPieces.
                    DictPieces[(int)MyIndex] = MyDict;
                }
                else
                {
                    //Loop over individual RangePartitions per processor.
                    for (int i = rng.Item1; i < rng.Item2; i++)
                    {
                        //Operation Percentage Report
                        ///Safe Counter Increment.
                        Interlocked.Increment(ref PointCounter);
                        ///Calculate and Report Percentage.
                        if (LastPercentReported < ((PointCounter * totc) * 100))
                        {
                            LastPercentReported = (int)(5 * Math.Ceiling((double)(PointCounter * totc) * 20));
                            this.ReportPercent  = LastPercentReported;
                        }

                        calcNormal(idc, ref MyCloud, ref count, i);
                    }
                    //Add MyCloud to CloudPieces at ProcesserIndex.
                    CloudPieces[(int)MyIndex] = MyCloud;
                }
                //Enable Parrallel Computing Cancellation
                po.CancellationToken.ThrowIfCancellationRequested();
            }
                             );



            //Merge PointCloud Pieces into One PointCloud.
            Cloud_Utils.MergeClouds(ref NewCloud, CloudPieces);
            if (GlobalCloud.UserDictionary.Count > 0)
            {
                List <double>[] NewDict = new List <double> [GlobalCloud.UserDictionary.Count];
                Dict_Utils.Merge_DictPieces(ref NewDict, DictPieces);
                Dict_Utils.SetUserDict_FromDictLists(ref NewCloud, NewDict, GlobalCloud.UserDictionary.Keys);
            }


            //Dispose of Global Clouds.
            GlobalCloud.Dispose();
            pointCloud.Dispose();

            /*
             * //Colorize
             * if (insV_Colorize)
             * {
             *  List<double> colorValues = Color_Utils.ColorValues_Std_pos(NewCloud, insV_Key);
             *  List<Color> Colors = Color_Utils.ColorGradient_Std_GtoR();
             *
             *  Instruction.Instr_Dict_Color col = new Instruction.Instr_Dict_Color(insV_Key, colorValues, Colors, -1, 0.00);
             *  Boolean ColResult = col.Execute(ref NewCloud);
             *
             *  //Set ColorGradient UserData
             *  Color_Utils.Set_ColorGradient_Dict(ref NewCloud, Colors, colorValues);
             * }
             */

            pointCloud = (PointCloud)NewCloud.Duplicate();

            //Dispose of PointCloud Pieces and NewCloud.
            GlobalCloud.Dispose();
            NewCloud.Dispose();

            //Return True on Finish
            return(true);
        }