Ejemplo n.º 1
0
        public void EnumerateFromPoint()
        {
            Vector3DLeafItem item1 = new Vector3DLeafItem(1, 0, 0);
            Vector3DLeafItem item2 = new Vector3DLeafItem(2, 0, 0);
            Vector3DLeafItem item3 = new Vector3DLeafItem(3, 0, 0);
            IEnumerable <Vector3DLeafItem> enumerable = new Vector3DLeafItem[] { item1, item2, item3 }.AsEnumerable <Vector3DLeafItem>();
            KDTreeNode <Vector3DLeafItem>  rootNode = KDTreeNode <Vector3DLeafItem> .CreateTree(enumerable);

            int index = 0;

            foreach (Vector3DLeafItem item in rootNode.GetDistanceEnumerator(new double[] { 2.1, 0, 0 }))
            {
                switch (index++)
                {
                case 0:
                    Assert.IsTrue(item == item2);
                    break;

                case 1:
                    Assert.IsTrue(item == item3);
                    break;

                case 2:
                    Assert.IsTrue(item == item1);
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        public void CreateFromKDTree()
        {
            Vector3DLeafItem item1 = new Vector3DLeafItem(5, 5, 5);
            Vector3DLeafItem item2 = new Vector3DLeafItem(5, 5, 5);
            Vector3DLeafItem item3 = new Vector3DLeafItem(5, 5, 5);
            IEnumerable <Vector3DLeafItem> enumerable = new Vector3DLeafItem[] { item1, item2, item3 }.AsEnumerable <Vector3DLeafItem>();
            KDTreeNode <Vector3DLeafItem>  rootNode = KDTreeNode <Vector3DLeafItem> .CreateTree(enumerable);

            RunTestOnNode3D(item1, item2, item3, rootNode);

            KDTreeNode <Vector3DLeafItem> fromRootNode = KDTreeNode <Vector3DLeafItem> .CreateTree(rootNode.UnorderedEnumerator());

            KDTreeNode <Vector3DLeafItem> testNode = fromRootNode;

            Assert.IsTrue(testNode.DimensionSplitIsOn == 0);
            Assert.IsTrue(testNode.NodeLessThanSplit == null);
            Assert.IsTrue(testNode.NodeGreaterThanOrEqualToSplit != null);

            testNode = testNode.NodeGreaterThanOrEqualToSplit;
            Assert.IsTrue(testNode.DimensionSplitIsOn == 1);
            Assert.IsTrue(testNode.NodeLessThanSplit == null);
            Assert.IsTrue(testNode.NodeGreaterThanOrEqualToSplit != null);

            testNode = testNode.NodeGreaterThanOrEqualToSplit;
            Assert.IsTrue(testNode.DimensionSplitIsOn == 2);
            Assert.IsTrue(testNode.NodeLessThanSplit == null);
            Assert.IsTrue(testNode.NodeGreaterThanOrEqualToSplit == null);
        }
Ejemplo n.º 3
0
        public void SamePointTest2D()
        {
            Vector2DLeafItem item1 = new Vector2DLeafItem(5, 5);
            Vector2DLeafItem item2 = new Vector2DLeafItem(5, 5);
            Vector2DLeafItem item3 = new Vector2DLeafItem(5, 5);
            IEnumerable <Vector2DLeafItem> enumerable = new Vector2DLeafItem[] { item1, item2, item3 }.AsEnumerable <Vector2DLeafItem>();
            KDTreeNode <Vector2DLeafItem>  rootNode = KDTreeNode <Vector2DLeafItem> .CreateTree(enumerable);

            KDTreeNode <Vector2DLeafItem> testNode = rootNode;

            Assert.IsTrue(testNode.DimensionSplitIsOn == 0);
            Assert.IsTrue(testNode.NodeLessThanSplit == null);
            Assert.IsTrue(testNode.NodeGreaterThanOrEqualToSplit != null);
            Assert.IsTrue(testNode.LeafItem == item1);

            testNode = testNode.NodeGreaterThanOrEqualToSplit;
            Assert.IsTrue(testNode.DimensionSplitIsOn == 1);
            Assert.IsTrue(testNode.NodeLessThanSplit == null);
            Assert.IsTrue(testNode.NodeGreaterThanOrEqualToSplit != null);
            Assert.IsTrue(testNode.LeafItem == item2);

            testNode = testNode.NodeGreaterThanOrEqualToSplit;
            Assert.IsTrue(testNode.DimensionSplitIsOn == 0);
            Assert.IsTrue(testNode.NodeLessThanSplit == null);
            Assert.IsTrue(testNode.NodeGreaterThanOrEqualToSplit == null);
            Assert.IsTrue(testNode.LeafItem == item3);
        }
Ejemplo n.º 4
0
        public void SamePointTest3D()
        {
            Vector3DLeafItem item1 = new Vector3DLeafItem(5, 5, 5);
            Vector3DLeafItem item2 = new Vector3DLeafItem(5, 5, 5);
            Vector3DLeafItem item3 = new Vector3DLeafItem(5, 5, 5);
            IEnumerable <Vector3DLeafItem> enumerable = new Vector3DLeafItem[] { item1, item2, item3 }.AsEnumerable <Vector3DLeafItem>();
            KDTreeNode <Vector3DLeafItem>  rootNode = KDTreeNode <Vector3DLeafItem> .CreateTree(enumerable);

            RunTestOnNode3D(item1, item2, item3, rootNode);
        }
Ejemplo n.º 5
0
        private static void RunTestOnNode3D(Vector3DLeafItem item1, Vector3DLeafItem item2, Vector3DLeafItem item3, KDTreeNode <Vector3DLeafItem> rootNode)
        {
            KDTreeNode <Vector3DLeafItem> testNode = rootNode;

            Assert.IsTrue(testNode.DimensionSplitIsOn == 0);
            Assert.IsTrue(testNode.NodeLessThanSplit == null);
            Assert.IsTrue(testNode.NodeGreaterThanOrEqualToSplit != null);
            Assert.IsTrue(testNode.LeafItem == item1);

            testNode = testNode.NodeGreaterThanOrEqualToSplit;
            Assert.IsTrue(testNode.DimensionSplitIsOn == 1);
            Assert.IsTrue(testNode.NodeLessThanSplit == null);
            Assert.IsTrue(testNode.NodeGreaterThanOrEqualToSplit != null);
            Assert.IsTrue(testNode.LeafItem == item2);

            testNode = testNode.NodeGreaterThanOrEqualToSplit;
            Assert.IsTrue(testNode.DimensionSplitIsOn == 2);
            Assert.IsTrue(testNode.NodeLessThanSplit == null);
            Assert.IsTrue(testNode.NodeGreaterThanOrEqualToSplit == null);
            Assert.IsTrue(testNode.LeafItem == item3);
        }
Ejemplo n.º 6
0
        public static KDTreeNode <StoredType> CreateTree(IEnumerable <StoredType> collectionToCreateFrom, int splitingDimension = 0)
        {
            KDTreeUnitTests.Run();

            int    count;
            double medianOnDimension = FindMedianOnDimension(collectionToCreateFrom, splitingDimension, out count);

            if (count == 0)
            {
                return(null);
            }

            StoredType firstItemFromCollection = default(StoredType); // if StoredType is a class this will set it to null - if struct, a zeroed struct.

            foreach (StoredType item in collectionToCreateFrom)
            {
                firstItemFromCollection = item;
                break;
            }

            KDTreeNode <StoredType> newNode = new KDTreeNode <StoredType>();

            newNode.DimensionSplitIsOn = splitingDimension;

            if (count > 1)
            {
                newNode.SplitPosition = medianOnDimension;
                List <StoredType> lessThanList           = new List <StoredType>();
                List <StoredType> greaterThanOrEqualList = new List <StoredType>();
                foreach (StoredType item in collectionToCreateFrom)
                {
                    double positionOfItemOnDimension = item.GetPositionForDimension(splitingDimension);
                    if (positionOfItemOnDimension < newNode.SplitPosition)
                    {
                        lessThanList.Add(item);
                    }
                    else
                    {
                        if (positionOfItemOnDimension == newNode.SplitPosition && newNode.LeafItem == null)
                        {
                            // if all the points were in exactly the same position we would just get a big linked list.
                            newNode.LeafItem = item;
                        }
                        else
                        {
                            greaterThanOrEqualList.Add(item);
                        }
                    }
                }

                int nextSplitDimension = (splitingDimension + 1) % firstItemFromCollection.Dimensions;
                newNode.NodeLessThanSplit             = CreateTree(lessThanList, nextSplitDimension);
                newNode.NodeGreaterThanOrEqualToSplit = CreateTree(greaterThanOrEqualList, nextSplitDimension);

                return(newNode);
            }

            newNode.LeafItem = firstItemFromCollection;

            return(newNode);
        }