コード例 #1
0
        public void RegionQueryTest()
        {
            //Checks that both RegionQuery overloaded methods always return the same result
            for (int d = 2; d <= 5; d++)
            {
                var points = new List <IPointIdFloat>();
                for (int i = 0; i < 200; i++)
                {
                    points.Add(RandomPoint(d));
                }
                KdTree kdt = new KdTree(points);

                for (int i = 0; i < 50; i++)
                {
                    var   p       = RandomPoint(d);
                    float epsilon = (float)rnd.NextDouble() * int.MaxValue;
                    var   l1      = DBScan.RegionQuery(points, p, epsilon).ToList();
                    var   l2      = DBScan.RegionQuery(kdt, p, epsilon).ToList();
                    var   b       = SequenceEquivalent(l1, l2, PointIdFloat.PointsComparison);
                    if (!b)
                    {
                        var l3 = DBScan.RegionQuery(kdt, p, epsilon).ToList();
                        Assert.IsTrue(SequenceEquivalent(l3, l2, PointIdFloat.PointsComparison));
                        Assert.IsTrue(false);
                    }
                }
            }
        }
コード例 #2
0
        public void ExpandRegionTest()
        {
            var points = new List <IPointIdFloat>()
            {
                new PointIdFloat(new List <float>()
                {
                    1.5f, 1.5f, 1.5f
                }),
                new PointIdFloat(new List <float>()
                {
                    1, 1, 1
                }),
                new PointIdFloat(new List <float>()
                {
                    2, 1, 1
                }),
                new PointIdFloat(new List <float>()
                {
                    1, 2, 1
                }),
                new PointIdFloat(new List <float>()
                {
                    1, 1, 2
                }),
                new PointIdFloat(new List <float>()
                {
                    3, 3, 3
                }),
                new PointIdFloat(new List <float>()
                {
                    2, 2, 2
                })
            };

            PointIdFloat.SetIds(points);

            var   clusters = new Dictionary <long, int>();
            var   visited  = new HashSet <long>();
            var   p        = points[0];
            float epsilon  = 1f;

            var neighbours = DBScan.RegionQuery(points, p, epsilon);

            Assert.IsTrue(neighbours.Contains(p));

            DBScan.ExpandCluster(clusters, visited, points, p, neighbours.ToList(), 0, epsilon, 3);
            Assert.IsTrue(clusters.ContainsKey(p.id));
            foreach (var q in neighbours)
            {
                Assert.IsTrue(clusters.ContainsKey(q.id));
                Assert.IsTrue(clusters[q.id] == clusters[p.id]);
            }

            Assert.IsFalse(clusters.ContainsKey(points.Count() + 1));
        }
コード例 #3
0
        public void RegionQueryPointsListTest()
        {
            var points = new List <IPointIdFloat>()
            {
                new PointIdFloat(new List <float>()
                {
                    1, 1, 1
                }),
                new PointIdFloat(new List <float>()
                {
                    2, 1, 1
                }),
                new PointIdFloat(new List <float>()
                {
                    1, 2, 1
                }),
                new PointIdFloat(new List <float>()
                {
                    1, 1, 2
                }),
                new PointIdFloat(new List <float>()
                {
                    3, 3, 3
                }),
                new PointIdFloat(new List <float>()
                {
                    2, 2, 2
                })
            };

            PointIdFloat.SetIds(points);
            var p = new PointIdFloat(new List <float>()
            {
                1.5f, 1.5f, 1.5f
            });
            Func <IPointIdFloat, Tuple <float, float, float> > keySelector = t => Tuple.Create(
                KdTree.KdTreeNode.KeyByDepth(t, 0), 0.0f, 0.0f);

            var result = DBScan.RegionQuery(points, p, 0.16f);

            Assert.IsTrue(result.Count() == 0);

            result = DBScan.RegionQuery(points, p, 1);
            var expectedResult = new List <IPointIdFloat>()
            {
                new PointIdFloat(new List <float>()
                {
                    1, 1, 1
                }),
                new PointIdFloat(new List <float>()
                {
                    2, 1, 1
                }),
                new PointIdFloat(new List <float>()
                {
                    1, 2, 1
                }),
                new PointIdFloat(new List <float>()
                {
                    1, 1, 2
                }),
                new PointIdFloat(new List <float>()
                {
                    2, 2, 2
                })
            };

            PointIdFloat.SetIds(expectedResult);

            Assert.IsTrue(SequenceEquivalent(result.ToList(), expectedResult, PointIdFloat.PointsComparison));

            p = new PointIdFloat(new List <float>()
            {
                0.75f, 0.75f, 0.75f
            });
            result         = DBScan.RegionQuery(points, p, (float)Math.Sqrt(2f / 3f));
            expectedResult = new List <IPointIdFloat>()
            {
                new PointIdFloat(new List <float>()
                {
                    1, 1, 1
                }),
                new PointIdFloat(new List <float>()
                {
                    2, 1, 1
                }),
                new PointIdFloat(new List <float>()
                {
                    1, 2, 1
                }),
                new PointIdFloat(new List <float>()
                {
                    1, 1, 2
                }),
            };
            PointIdFloat.SetIds(expectedResult);

            Assert.IsTrue(SequenceEquivalent(result.ToList(), expectedResult, PointIdFloat.PointsComparison));
        }
コード例 #4
0
        public void RegionQueryKdTreeTest()
        {
            var points = new List <IPointIdFloat>()
            {
                new PointIdFloat(new List <float>()
                {
                    1, 1, 1
                }),
                new PointIdFloat(new List <float>()
                {
                    2, 1, 1
                }),
                new PointIdFloat(new List <float>()
                {
                    1, 2, 1
                }),
                new PointIdFloat(new List <float>()
                {
                    1, 1, 2
                }),
                new PointIdFloat(new List <float>()
                {
                    3, 3, 3
                }),
                new PointIdFloat(new List <float>()
                {
                    2, 2, 2
                })
            };

            PointIdFloat.SetIds(points);

            var p = new PointIdFloat(new List <float>()
            {
                1.5f, 1.5f, 1.5f
            });
            KdTree kdt = new KdTree(points);

            var result = kdt.PointsWithinDistance(p, 0.5f);

            Assert.IsTrue(result.Count == 0);

            result = DBScan.RegionQuery(kdt, p, 1);
            var expectedResult = new List <IPointIdFloat>()
            {
                new PointIdFloat(new List <float>()
                {
                    1, 1, 1
                }),
                new PointIdFloat(new List <float>()
                {
                    2, 1, 1
                }),
                new PointIdFloat(new List <float>()
                {
                    1, 2, 1
                }),
                new PointIdFloat(new List <float>()
                {
                    1, 1, 2
                }),
                new PointIdFloat(new List <float>()
                {
                    2, 2, 2
                })
            };

            PointIdFloat.SetIds(expectedResult);

            Assert.IsTrue(SequenceEquivalent(result.ToList(), expectedResult, PointIdFloat.PointsComparison));

            p = new PointIdFloat(new List <float>()
            {
                0.75f, 0.75f, 0.75f
            });
            result         = kdt.PointsWithinDistance(p, (float)Math.Sqrt(2));
            expectedResult = new List <IPointIdFloat>()
            {
                new PointIdFloat(new List <float>()
                {
                    1, 1, 1
                }),
                new PointIdFloat(new List <float>()
                {
                    2, 1, 1
                }),
                new PointIdFloat(new List <float>()
                {
                    1, 2, 1
                }),
                new PointIdFloat(new List <float>()
                {
                    1, 1, 2
                }),
            };
            PointIdFloat.SetIds(expectedResult);

            Assert.IsTrue(SequenceEquivalent(result.ToList(), expectedResult, PointIdFloat.PointsComparison));
        }