Exemple #1
0
        /// <summary>
        /// Recursively split node across its pivot axis.
        /// </summary>
        /// <param name="node">KDTreeNode to split</param>
        /// <param name="depth">Current recursion depth, set lower if you get stack overflow</param>
        /// <returns>True if split was a success or max_depth/max_node_size criterion met</returns>
        bool BuildNode(KDNodeJeremyC node, int depth)
        {
            if (depth >= MaxNodeDepth)
            {
                return(true);
            }
            if (node.Indices.Count <= MaxNodeSize)
            {
                return(true);
            }

            foreach (var index in node.Indices)
            {
                VertexKDTree vertex = TreeVectors[index];

                if (!node.IsBuilt)
                {
                    node.Build();
                }
                KDNodeJeremyC child = node.GetSplitNode(vertex);
                child.AddVertex(index, vertex);
            }

            // TODO:  Do we need to check if either child is empty?  Since we're calculating the split axis
            //		  using raw data it's unlikely.
            node.Clear();
            node.ChildLeft.Build();
            node.ChildRight.Build();

            BuildNode(node.ChildLeft, depth + 1);
            BuildNode(node.ChildRight, depth + 1);
            return(true);
        }
Exemple #2
0
        ///// <summary>
        ///// Add a new vertex/point to the KDTree build process.  Points must be added before calling KDTree.Build()
        ///// </summary>
        ///// <param name="vertex">3D vertex</param>
        //public void AddPoint(VectorWithIndex vertex)
        //{
        //    Vectors.Add(vertex);
        //}

        ///// <summary>
        ///// Add a new vertex/point to the KDTree build process.  Points must be added before calling KDTree.Build()
        ///// </summary>
        ///// <param name="x">VectorWithIndex.x</param>
        ///// <param name="y">VectorWithIndex.y</param>
        ///// <param name="z">VectorWithIndex.z</param>
        //public void AddPoint(float x, float y, float z)
        //{
        //    Vectors.Add(new VectorWithIndex(new Vector3(x,y,z), -1));
        //}

        /// <summary>
        /// Add a new list of vertices/points to the KDTree build process.  Points must be added before calling KDTree.Build()
        /// </summary>
        /// <param name="vertex">List of 3D vertices</param>
        //public void AddPoints(List<VectorWithIndex> vertices)
        //{
        //    Vectors = Vectors.Concat(vertices).ToList();
        //}

        ///// <summary>
        ///// Add a new list of vertices/points to the KDTree build process.  Points must be added before calling KDTree.Build()
        ///// </summary>
        ///// <param name="vertex">List of 3D vertices stored as a 1D array w/ every 3 elements defining a vertex.</param>
        //public void AddPoints(List<float> vertices)
        //{
        //    int element_size = 3;
        //    if (vertices.Count % element_size != 0) return;	//vertex count must be a multiple of element_size
        //    for(int i = 0; i < vertices.Count; i+=element_size){
        //        float x = vertices[i];
        //        float y = vertices[i+1];
        //        float z = vertices[i+2];
        //        Vectors.Add(new VectorWithIndex(new Vector3(x, y, z), -1));
        //    }

        //}
        /// <summary>
        /// Build KDTree
        /// </summary>
        /// <notes>
        /// Points are added via KDTree.AddPoint(s) methods, the Build process uses
        /// those points to create the final tree.  If new points are added after a
        /// tree has been built, a new tree must be created.
        /// </notes>
        /// <returns></returns>
        public bool Build(PointCloud pcTarget)
        {
            this.targetTreee = pcTarget;


            this.treePointCloud = pcTarget;
            this.TreeVectors    = treePointCloud.VectorsWithIndex;


            if (VertexCount <= 0)
            {
                Console.WriteLine("[Warning] - No vertices added to KDTree, aborting build.");
                return(false);
            }
            Root = new KDNodeJeremyC();            // { SplitAxis = largest_split_axis, AABB = new BoundingBoxAxisAligned(min,max), MidPoint = center, Parent = null};



            {                   // Fill the Root
                int index = 0;
                foreach (var vertex in TreeVectors)
                {
                    root.AddVertex(index, vertex);
                    index++;
                }
            }

            BuildNode(Root, 0);
            return(true);
        }