コード例 #1
0
ファイル: KDTreeKennell.cs プロジェクト: whigg/PointClouds
        /// <summary>
        /// returns the target (tree) points found for the input (source) points
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public PointCloud FindClosestPointCloud_Parallel(PointCloud source)
        {
            this.source = source;
            this.ResetTaken();


            VertexKDTree[] resultArray = new VertexKDTree[source.Count];

            //shuffle points for the "Taken" algorithm
            PointCloud sourceShuffled;

            if (this.TakenAlgorithm)
            {
                sourceShuffled = source.Clone();
                sourceShuffled.SetDefaultIndices();
                sourceShuffled = PointCloud.Shuffle(sourceShuffled);
            }
            else
            {
                sourceShuffled = source;
            }

            int   nearest_index    = 0;
            float nearest_distance = 0f;

            System.Threading.Tasks.Parallel.For(0, source.Count, i =>
                                                //for (int i = 0; i < sourceShuffled.Count; i++)
            {
                VertexKDTree vSource      = new VertexKDTree(sourceShuffled.Vectors[i], i);
                VertexKDTree vTargetFound = FindClosestPoint(vSource, ref nearest_distance, ref nearest_index);
                //resultArray[i] = vTargetFound.Clone();
                resultArray[i] = vTargetFound;
            });

            List <VertexKDTree> resultList = new List <VertexKDTree>(resultArray);

            result = PointCloud.FromListVertexKDTree(resultList);

            //shuffle back

            PointCloud pcResultShuffledBack;

            if (this.TakenAlgorithm)
            {
                pcResultShuffledBack = result.Clone();
                for (int i = 0; i < pcResultShuffledBack.Count; i++)
                {
                    //pcResultShuffled.Vectors[i] = pcResult.Vectors[Convert.ToInt32(sourceShuffled.Indices[i])];
                    pcResultShuffledBack.Vectors[Convert.ToInt32(sourceShuffled.Indices[i])] = result.Vectors[i];
                }
            }
            else
            {
                pcResultShuffledBack = result;
            }
            //this.MeanDistance = PointCloud.MeanDistance(source, pcResultShuffledBack);
            return(pcResultShuffledBack);
        }