Exemplo n.º 1
0
 public KdSplitNode(int cuttingDimension, float cuttingValue, IKdNode leftChild, IKdNode rightChild)
 {
     this.cuttingDimension = cuttingDimension;
     this.cuttingValue     = cuttingValue;
     this.leftChild        = leftChild;
     this.rightChild       = rightChild;
 }
Exemplo n.º 2
0
        private IEnumerable <Vector2> GetAllNodesWithinDistance(IKdNode <float> root, Vector2 startPosition, float distance)
        {
            List <Vector2> foundNodes = new List <Vector2>();

            Queue <IKdNode <float> > queue = new Queue <IKdNode <float> >();

            queue.Enqueue(root);

            while (queue.Count > 0)
            {
                IKdNode <float> currentNode = queue.Dequeue();
                if (currentNode.isEmpty)
                {
                    continue;
                }
                if (Vector2.Distance(startPosition, new Vector2(currentNode.key.Item1, currentNode.key.Item2)) <= distance)
                {
                    foundNodes.Add(new Vector2(currentNode.key.Item1, currentNode.key.Item2));
                }
                queue.Enqueue(currentNode.left);
                queue.Enqueue(currentNode.right);
            }

            return(foundNodes);
        }
Exemplo n.º 3
0
        private IKdNode <float> InsertRec(IKdNode <float> root, Tuple <float, float> key, int dimension)
        {
            dimension++;

            if (root.isEmpty)
            {
                return(new KdNode <float>(key, new EmptyKdNode <float>(), new EmptyKdNode <float>()));
            }

            if (dimension % 2 == 0)
            {
                if (root.key.Item1 > key.Item1)
                {
                    return(new KdNode <float>(root.key, InsertRec(root.left, key, dimension), root.right));
                }
                else
                {
                    return(new KdNode <float>(root.key, root.left, InsertRec(root.right, key, dimension)));
                }
            }
            else
            {
                if (root.key.Item2 > key.Item2)
                {
                    return(new KdNode <float>(root.key, InsertRec(root.left, key, dimension), root.right));
                }
                else
                {
                    return(new KdNode <float>(root.key, root.left, InsertRec(root.right, key, dimension)));
                }
            }
        }
Exemplo n.º 4
0
        private static IKdNode CreateKdNode(IPoint[] points, int dimension, int maxPointCountPerLeafNode, int rekursionDeep)
        {
            if (points.Length <= maxPointCountPerLeafNode || rekursionDeep < 0)   // n small, make a leaf node
            {
                return(new KdLeafNode(points, dimension));
            }
            else
            {
                var splitResult = MidpointSplitFromLongestEdge.Split(points, dimension);

                IKdNode leftNode  = CreateKdNode(splitResult.PoingsOnLeftSide, dimension, maxPointCountPerLeafNode, rekursionDeep - 1);
                IKdNode rightNode = CreateKdNode(splitResult.PoingsOnRightSide, dimension, maxPointCountPerLeafNode, rekursionDeep - 1);

                return(new KdSplitNode(splitResult.CuttingDimension, splitResult.CuttingValue, leftNode, rightNode));
            }
        }
Exemplo n.º 5
0
 public KdTree(IPoint[] points, int dimension = 3, int maxPointCountPerLeafNode = 10)
 {
     this.rootNode = CreateKdNode(points, dimension, maxPointCountPerLeafNode, 15);
 }
Exemplo n.º 6
0
 public KdNode(Tuple <T, T> key, IKdNode <T> left, IKdNode <T> right)
 {
     this.key   = key;
     this.left  = left;
     this.right = right;
 }
Exemplo n.º 7
0
 public void Insert(Tuple <float, float> key)
 {
     root = InsertRec(root, key, -1);
 }
Exemplo n.º 8
0
 public KdTree()
 {
     root = new EmptyKdNode <float>() as IKdNode <float>;
 }