Beispiel #1
0
        public void SetOutsideShouldFail()
        {
            var qt       = new RegionQuadtree <int>(3);
            var success1 = qt.Set(new Point2i(8, 8), 1);
            var success2 = qt.Set(new Point2i(-1, -1), 1);

            Assert.False(success1);
            Assert.False(success2);
        }
Beispiel #2
0
        public void SetInsideShouldSuccess()
        {
            var qt       = new RegionQuadtree <int>(3);
            var success1 = qt.Set(new Point2i(0, 0), 1);
            var success2 = qt.Set(new Point2i(7, 7), 1);

            Assert.True(success1);
            Assert.True(success2);
        }
Beispiel #3
0
        public void FindConnectedComponentsSmallerNeighborBotLeft()
        {
            var qt = new RegionQuadtree <int>(3);

            qt.Set(1);
            qt.Set(new Point2i(0, 0), 2);
            qt.Set(new Point2i(5, 0), 2);
            qt.Set(new Point2i(5, 5), 2);
            //qt.Set(new Point2i(1, 2), 1);
            var r = qt.FindConnectedComponents();

            Assert.Equal(1, r.Count);
        }
Beispiel #4
0
        public void BlackQuadEnumeration()
        {
            var qt = new RegionQuadtree <int>(3);

            qt.Set(1);
            Assert.Equal(1, qt.Count());
            Assert.Equal(1, qt.ElementAt(0));

            qt.Set(new Point2i(0, 0), 2);

            Assert.True(qt.Contains(2));
            Assert.Equal(10, qt.Count());
            Assert.Equal(9, qt.Count((i) => i == 1));
        }
Beispiel #5
0
        public void WhiteQuadEnumeration()
        {
            var qt = new RegionQuadtree <int>(3);

            qt.Set(new Point2i(0, 0), 1);

            Assert.Equal(1, qt.Count());
            Assert.Equal(1, qt.ElementAt(0));

            qt.Set(new Point2i(4, 4), 2);

            Assert.Equal(2, qt.Count());
            Assert.Equal(1, qt.ElementAt(0));
            Assert.Equal(2, qt.ElementAt(1));
        }
Beispiel #6
0
        public void SetInitialValueOnConstruct()
        {
            var qt = new RegionQuadtree <int>(3);

            qt.Set(1);
            Assert.Equal(QuadType.Black, qt.Type);
        }
Beispiel #7
0
        public void QuadTypeShouldBeGrey()
        {
            var qt = new RegionQuadtree <int>(3);

            qt.Set(new Point2i(0, 0), 1);

            Assert.Equal(QuadType.Grey, qt.Type);
        }
Beispiel #8
0
        public void QuadTypeShouldBeBlack()
        {
            var qt = new RegionQuadtree <int>(0);

            qt.Set(new Point2i(0, 0), 1);

            Assert.Equal(QuadType.Black, qt.Type);
        }
        public void WhiteQuadRemoveEventNotShouldBeCalled()
        {
            var qt    = new RegionQuadtree <int>(3);
            var times = 0;

            qt.OnQuadRemoving += (s, a) =>
            {
                times++;
            };

            qt.Set(new Point2i(0, 0), 1);

            Assert.Equal(0, times);

            qt.Set(new Point2i(1, 0), 1);

            Assert.Equal(0, times);
        }
Beispiel #10
0
        public void WhiteQuadAddEventShouldBeCalledTwice()
        {
            var qt    = new RegionQuadtree <int>(3);
            var times = 0;

            qt.OnQuadAdded += (s, a) =>
            {
                times++;
            };

            qt.Set(new Point2i(0, 0), 1);

            Assert.Equal(1, times);

            qt.Set(new Point2i(1, 0), 1);

            Assert.Equal(2, times);
        }
Beispiel #11
0
        public void TraverseTest()
        {
            var qt = new RegionQuadtree <int>(2);

            qt.Set(new Point2i(0, 0), 1);
            qt.Set(new Point2i(1, 0), 2);
            var t = qt.Traverse().ToList();

            Assert.Equal(2, t.Count);

            qt = new RegionQuadtree <int>(3);
            qt.Set(new Point2i(0, 0), 1);
            qt.Set(new Point2i(7, 7), 2);
            qt.Set(new Point2i(1, 0), 1);
            qt.Set(new Point2i(0, 5), 2);
            qt.Set(new Point2i(3, 6), 2);
            t = qt.Traverse().ToList();
            Assert.Equal(5, t.Count);
        }
Beispiel #12
0
        public void WhiteQuadAddEventShouldBeCalledOnce()
        {
            var qt    = new RegionQuadtree <int>(3);
            var times = 0;

            qt.OnQuadAdded += (s, a) =>
            {
                times++;
            };

            qt.Set(new Point2i(0, 0), 1);

            Assert.Equal(1, times);


            // Don't call event if value is same
            qt.Set(new Point2i(0, 0), 1);

            Assert.Equal(1, times);
        }
Beispiel #13
0
        public void FindConnectedComponentsTest()
        {
            var qt = new RegionQuadtree <int>(3);

            qt.Set(new Point2i(0, 0), 1);
            qt.Set(new Point2i(1, 0), 1);
            qt.Set(new Point2i(0, 2), 1);
            qt.Set(new Point2i(1, 2), 1);
            var r = qt.FindConnectedComponents();

            Assert.Equal(2, r.Count);
            Assert.Equal(2, r[0].Count);
            Assert.Equal(2, r[1].Count);

            qt = new RegionQuadtree <int>(3);
            qt.Set(new Point2i(0, 0), 1);
            qt.Set(new Point2i(1, 0), 1);
            qt.Set(new Point2i(2, 0), 1);
            qt.Set(new Point2i(2, 1), 1);
            qt.Set(new Point2i(2, 2), 1);
            qt.Set(new Point2i(2, 3), 1);
            r = qt.FindConnectedComponents();
            Assert.Equal(1, r.Count);
            Assert.Equal(6, r[0].Count);

            qt = new RegionQuadtree <int>(3);
            qt.Set(new Point2i(0, 0), 1);
            qt.Set(new Point2i(2, 0), 1);
            qt.Set(new Point2i(4, 0), 1);
            qt.Set(new Point2i(0, 2), 1);
            qt.Set(new Point2i(2, 2), 1);
            qt.Set(new Point2i(4, 2), 1);
            r = qt.FindConnectedComponents();
            Assert.Equal(6, r.Count);
            for (int i = 0; i < 6; i++)
            {
                Assert.Equal(1, r[i].Count);
            }
        }
Beispiel #14
0
        public void BlackQuadAddEventShouldBeCalledTwentyTimes()
        {
            var qt = new RegionQuadtree <int>(3);

            qt.Set(1);
            var times = 0;

            qt.OnQuadAdded += (s, a) =>
            {
                times++;
            };

            qt.Unset(new Point2i(0, 0));
            qt.Unset(new Point2i(4, 4));

            Assert.Equal(20, times);
        }
Beispiel #15
0
        public void BlackQuadRemoveEventShouldBeCalledFiveTimes()
        {
            var qt = new RegionQuadtree <int>(3);

            qt.Set(1);
            var times = 0;

            qt.OnQuadRemoving += (s, a) =>
            {
                times++;
            };

            qt.Unset(new Point2i(0, 0));
            qt.Unset(new Point2i(0, 1));

            Assert.Equal(5, times);
        }
Beispiel #16
0
        public void WhiteQuadRemoveEventShouldBeCalledOnce()
        {
            var qt    = new RegionQuadtree <int>(3);
            var times = 0;

            qt.OnQuadRemoving += (s, a) =>
            {
                times++;
            };

            qt.Set(new Point2i(0, 0), 1);
            qt.Unset(new Point2i(0, 0));

            // Remove event should not be called twice
            qt.Unset(new Point2i(0, 0));

            Assert.Equal(1, times);
        }
Beispiel #17
0
        public void BlackQuadAddEventShouldBeCalledTwelveTimes()
        {
            var qt = new RegionQuadtree <int>(3);

            qt.Set(1);
            var times = 0;

            qt.OnQuadAdded += (s, a) =>
            {
                times++;
            };

            qt.Unset(new Point2i(0, 0));

            Assert.Equal(12, times);

            // Remove event should not be called twice
            qt.Unset(new Point2i(0, 0));

            Assert.Equal(12, times);
        }
Beispiel #18
0
        private void initQuadtree()
        {
            quadtree            = new RegionQuadtree <Color>(qtResolution);
            quadtree.AutoExpand = true;

            rects = new Dictionary <AABB2i, QuadData>();

            Action <object, RegionQuadtree <Color> .QuadEventArgs <Color> > onQuadAdded = (s, a) =>
            {
                var aabb         = a.AABB;
                var quadColor    = a.Value;
                var outlineColor = new Color(0, 0, 0, 100);

                var rectPoints = new List <Vector2f>
                {
                    new Vector2f(aabb.LowerBound.X, aabb.LowerBound.Y) * qtMultiplier,
                    new Vector2f(aabb.LowerBound.X + aabb.Width, aabb.LowerBound.Y) * qtMultiplier,
                    new Vector2f(aabb.LowerBound.X + aabb.Width, aabb.LowerBound.Y + aabb.Height) * qtMultiplier,
                    new Vector2f(aabb.LowerBound.X, aabb.LowerBound.Y + aabb.Height) * qtMultiplier,
                };

                const float outlineIntend = 1f;
                var         outlinePoints = new List <Vector2f>
                {
                    rectPoints[0] + new Vector2f(outlineIntend, outlineIntend),
                    rectPoints[1] + new Vector2f(-outlineIntend, outlineIntend),
                    rectPoints[2] + new Vector2f(-outlineIntend, -outlineIntend),
                    rectPoints[3] + new Vector2f(outlineIntend, -outlineIntend),
                };

                var quadData = new QuadData();

                // quad
                if (freeQuadIndexes.Count > 0)
                {
                    quadData.quadIndex = freeQuadIndexes[freeQuadIndexes.Count - 1];
                    freeQuadIndexes.RemoveAt(freeQuadIndexes.Count - 1);
                }
                else
                {
                    quadData.quadIndex = quadVertexArray.VertexCount;
                    quadVertexArray.Resize(quadVertexArray.VertexCount + 4);
                }

                for (uint i = 0; i < 4; i++)
                {
                    quadVertexArray[quadData.quadIndex + i] = new Vertex(rectPoints[(int)i], quadColor);
                }


                // outline
                if (freeOutlineIndexes.Count > 0)
                {
                    quadData.outlineIndex = freeOutlineIndexes[freeOutlineIndexes.Count - 1];
                    freeOutlineIndexes.RemoveAt(freeOutlineIndexes.Count - 1);
                }
                else
                {
                    quadData.outlineIndex = outlineVertexArray.VertexCount;
                    outlineVertexArray.Resize(outlineVertexArray.VertexCount + 8);
                }

                outlineVertexArray[quadData.outlineIndex]     = new Vertex(outlinePoints[0] + new Vector2f(-outlineIntend, 0f), outlineColor);
                outlineVertexArray[quadData.outlineIndex + 1] = new Vertex(outlinePoints[1] + new Vector2f(-outlineIntend, 0f), outlineColor);
                outlineVertexArray[quadData.outlineIndex + 2] = new Vertex(outlinePoints[1] + new Vector2f(0f, -outlineIntend), outlineColor);
                outlineVertexArray[quadData.outlineIndex + 3] = new Vertex(outlinePoints[2] + new Vector2f(0f, -outlineIntend), outlineColor);
                outlineVertexArray[quadData.outlineIndex + 4] = new Vertex(outlinePoints[2] + new Vector2f(outlineIntend, 0f), outlineColor);
                outlineVertexArray[quadData.outlineIndex + 5] = new Vertex(outlinePoints[3] + new Vector2f(outlineIntend, 0f), outlineColor);
                outlineVertexArray[quadData.outlineIndex + 6] = new Vertex(outlinePoints[3] + new Vector2f(0f, outlineIntend), outlineColor);
                outlineVertexArray[quadData.outlineIndex + 7] = new Vertex(outlinePoints[0] + new Vector2f(0f, outlineIntend), outlineColor);

                rects.Add(aabb, quadData);
            };

            quadtree.OnQuadAdded += new EventHandler <RegionQuadtree <Color> .QuadEventArgs <Color> >(onQuadAdded);

            Action <object, RegionQuadtree <Color> .QuadEventArgs <Color> > onQuadRemoving = (s, a) =>
            {
                var quadData = rects[a.AABB];

                // quad
                freeQuadIndexes.Add(quadData.quadIndex);
                for (uint i = 0; i < 4; i++)
                {
                    quadVertexArray[quadData.quadIndex + i] = new Vertex(new Vector2f(), Color.Transparent);
                }

                // outline
                freeOutlineIndexes.Add(quadData.outlineIndex);
                for (uint i = 0; i < 8; i++)
                {
                    outlineVertexArray[quadData.outlineIndex + i] = new Vertex(new Vector2f(), Color.Transparent);
                }

                rects.Remove(a.AABB);
            };

            quadtree.OnQuadRemoving += new EventHandler <RegionQuadtree <Color> .QuadEventArgs <Color> >(onQuadRemoving);

            Action <object, RegionQuadtree <Color> .QuadChangedEventArgs <Color> > onQuadChanged = (s, a) =>
            {
                var quadData     = rects[a.AABB];
                var quadColor    = a.Value;
                var outlineColor = new Color(0, 0, 0, 100);

                for (uint i = 0; i < 4; i++)
                {
                    quadVertexArray[quadData.quadIndex + i] = new Vertex(quadVertexArray[quadData.quadIndex + i].Position, quadColor);
                }

                for (uint i = 0; i < 8; i++)
                {
                    outlineVertexArray[quadData.outlineIndex + i] = new Vertex(outlineVertexArray[quadData.outlineIndex + i].Position, outlineColor);
                }
            };

            quadtree.OnQuadChanged += new EventHandler <RegionQuadtree <Color> .QuadChangedEventArgs <Color> >(onQuadChanged);

            Action <object, RegionQuadtree <Color> .QuadExpandEventArgs <Color> > onExpand = (s, a) =>
            {
                var oldRoot = quadtree;
                var newRoot = a.NewRoot;

                this.quadtree = newRoot;

                qtResolution++;

                position -= new Vector2f(a.Offset.X, a.Offset.Y) * qtMultiplier;

                resolutionText.DisplayedString = "Resolution " + quadtree.AABB.Width * qtMultiplier + "x" + quadtree.AABB.Height * qtMultiplier;
            };

            quadtree.OnExpand += new EventHandler <RegionQuadtree <Color> .QuadExpandEventArgs <Color> >(onExpand);

            quadtree.Set(Color.White);
        }