コード例 #1
0
 public PointDataContainer(float[] point,
     ARTreeContainer parent = null)
 {
     if (parent != null)
         Parent = parent;
     Coordinates = point;
 }
コード例 #2
0
ファイル: RTreeLeaf.cs プロジェクト: politician/fallen-8
 public RTreeLeaf(float[] clower, float[] cupper, ARTreeContainer parent = null)
 {
     Lower = clower;
     Upper = cupper;
     if (parent != null)
         Parent = parent;
     Data = new List<IRTreeDataContainer>();
 }
コード例 #3
0
ファイル: RTreeLeaf.cs プロジェクト: politician/fallen-8
 public RTreeLeaf(IMBR mbr, ARTreeContainer parent = null)
 {
     Lower = mbr.Lower;
     Upper = mbr.Upper;
     if (parent != null)
         Parent = parent;
     Data = new List<IRTreeDataContainer>();
 }
コード例 #4
0
 public SpatialDataContainer(IMBR mbr,
     ARTreeContainer parent = null)
 {
     if (parent != null)
         Parent = parent;
     Lower = mbr.Lower;
     Upper = mbr.Upper;
 }
コード例 #5
0
ファイル: RTreeNode.cs プロジェクト: politician/fallen-8
 public RTreeNode(IMBR mbr, IEnumerable<ARTreeContainer> children = null, ARTreeContainer parent = null)
 {
     if (parent != null)
         Parent = parent;
     Lower = mbr.Lower;
     Upper = mbr.Upper;
     Children = children != null ? new List<ARTreeContainer>(children) : new List<ARTreeContainer>();
 }
コード例 #6
0
 public SpatialDataContainer(float[] clower,
     float[] cupper,
     ARTreeContainer parent = null)
 {
     if (parent != null)
         Parent = parent;
     Lower = clower;
     Upper = cupper;
 }
コード例 #7
0
ファイル: RTreeNode.cs プロジェクト: politician/fallen-8
 public RTreeNode(float[] clower,
     float[] cupper, IEnumerable<ARTreeContainer> children = null, ARTreeContainer parent = null)
 {
     if (parent != null)
         Parent = parent;
     Lower = clower;
     Upper = cupper;
     Children = children != null ? new List<ARTreeContainer>(children) : new List<ARTreeContainer>();
 }
コード例 #8
0
ファイル: RTree.cs プロジェクト: politician/fallen-8
        /// <summary>
        /// find best split axis
        /// </summary>
        /// <param name="result">The Result </param>
        /// <param name="container">
        /// container with data
        /// </param>
        /// <returns>
        /// number of split axis
        /// </returns>
        private int ChooseSplitAxis(out List<IRTreeContainer> result, ARTreeContainer container)
        {
            var currentContainers = container.IsLeaf == false
                ? new List<IRTreeContainer>(((RTreeNode)container).Children)
                : new List<IRTreeContainer>(((RTreeLeaf)container).Data);

            result = currentContainers;

            var marginValue = new float[_countOfR];

            for (int i = 0; i < _countOfR; i++)
            {

                currentContainers.Sort((x, y) => x.LowerPoint[i].CompareTo(y.LowerPoint[i]));
                currentContainers.Sort((x, y) => x.UpperPoint[i].CompareTo(y.UpperPoint[i]));

                var firstSeq = new List<IRTreeContainer>();
                var secondSeq = new List<IRTreeContainer>();

                firstSeq.AddRange(currentContainers.GetRange(0, MinCountOfNode));
                secondSeq.AddRange(currentContainers.GetRange(MinCountOfNode, currentContainers.Count - MinCountOfNode));

                var position = 0;
                marginValue[i] = FindMarginValue(firstSeq, i) +
                                       FindMarginValue(secondSeq, i);
                while (position <= currentContainers.Count - 2 * MinCountOfNode)
                {
                    firstSeq.Add(secondSeq.First());
                    secondSeq.RemoveAt(0);
                    marginValue[i] += FindMarginValue(firstSeq, i) +
                                       FindMarginValue(secondSeq, i);
                    position++;
                }
            }

            var splitAxis = 0;
            var sum = marginValue[0];

            for (int i = 1; i < _countOfR; i++)
            {
                if (sum < marginValue[i])
                {
                    sum = marginValue[i];
                    splitAxis = i;

                }
            }
            return splitAxis;
        }
コード例 #9
0
ファイル: RTree.cs プロジェクト: politician/fallen-8
        private void ChooseSplitIndex(List<IRTreeContainer> containers, int axis, ARTreeContainer parent1, ARTreeContainer parent2)
        {
            containers.Sort((x, y) => x.LowerPoint[axis].CompareTo(y.LowerPoint[axis]));
            containers.Sort((x, y) => x.UpperPoint[axis].CompareTo(y.UpperPoint[axis]));

            var firstSeqTest = new List<IRTreeContainer>();
            var secondSeqTest = new List<IRTreeContainer>();

            firstSeqTest.AddRange(containers.GetRange(0, MinCountOfNode - 1));
            secondSeqTest.AddRange(containers.GetRange(MinCountOfNode - 1, containers.Count - MinCountOfNode + 1));

            var position = 0;

            float minOverlapValue = float.PositiveInfinity;
            IMBR mbrParent1 = null;
            IMBR mbrParent2 = null;
            float area1 = float.PositiveInfinity;
            float area2 = float.PositiveInfinity;
            var sequenzPosition = 0;

            while (position <= containers.Count - 2 * MinCountOfNode)
            {
                firstSeqTest.Add(secondSeqTest.First());
                secondSeqTest.RemoveAt(0);
                var mbr1 = FindMBR(firstSeqTest);
                var mbr2 = FindMBR(secondSeqTest);
                var currentOverlap = FindOverlapValue(mbr1, mbr2);
                if (minOverlapValue > currentOverlap)
                {
                    mbrParent1 = mbr1;
                    mbrParent2 = mbr2;
                    sequenzPosition = position;
                    minOverlapValue = currentOverlap;

                }
                else
                {
                    if (minOverlapValue == currentOverlap)
                    {
                        area1 = GetArea(mbrParent1);
                        area2 = GetArea(mbrParent2);
                        var currentArea1 = GetArea(mbr1);
                        var currentArea2 = GetArea(mbr2);

                        if (area1 + area2 > currentArea1 + currentArea2)
                        {
                            mbrParent1 = mbr1;
                            mbrParent2 = mbr2;
                            sequenzPosition = position;
                            minOverlapValue = currentOverlap;
                            area1 = currentArea1;
                            area2 = currentArea2;
                        }
                    }

                }
                position++;

            }

            if (float.IsPositiveInfinity(area1))
            {
                area1 = GetArea(mbrParent1);
                area2 = GetArea(mbrParent2);
            }

            parent1.Area = area1;
            parent1.Lower = mbrParent1.Lower;
            parent1.Upper = mbrParent1.Upper;

            parent2.Area = area2;
            parent2.Lower = mbrParent2.Lower;
            parent2.Upper = mbrParent2.Upper;

            if (!parent1.IsLeaf)
            {
                for (int counter = 0; counter < containers.Count; counter++)
                {
                    if (counter <= MinCountOfNode - 1 + sequenzPosition)
                    {
                        containers[counter].Parent = parent1;
                        ((RTreeNode)parent1).Children.Add((ARTreeContainer)containers[counter]);
                    }
                    else
                    {
                        containers[counter].Parent = parent2;
                        ((RTreeNode)parent2).Children.Add((ARTreeContainer)containers[counter]);
                    }
                }
            }
            else
            {
                for (int counter = 0; counter < containers.Count; counter++)
                {
                    if (counter <= MinCountOfNode - 1 + sequenzPosition)
                    {
                        containers[counter].Parent = parent1;
                        ((RTreeLeaf)parent1).Data.Add((IRTreeDataContainer)containers[counter]);

                    }
                    else
                    {
                        containers[counter].Parent = parent2;
                        ((RTreeLeaf)parent2).Data.Add((IRTreeDataContainer)containers[counter]);

                    }
                }
            }
        }
コード例 #10
0
ファイル: RTree.cs プロジェクト: politician/fallen-8
        public void Initialize(Fallen8 fallen8, IDictionary<string, object> parameter)
        {
            if (parameter == null) throw new ArgumentNullException("parameter");

            if (!parameter.ContainsKey("IMetric")||!(parameter["IMetric"] is IMetric))
                throw new Exception("IMetric is uncorrectly");
            Metric = (IMetric)parameter["IMetric"];

            if (!parameter.ContainsKey("MinCount")||!(parameter["MinCount"] is int))
                throw new Exception("MinCount is uncorrectly");
            MinCountOfNode = (int)parameter["MinCount"];

            if (!parameter.ContainsKey("MaxCount")||!(parameter["MaxCount"] is int))
                throw new Exception("Max is uncorrectly");
            MaxCountOfNode = (int)parameter["MaxCount"];

            if (!parameter.ContainsKey("Space")||!(parameter["Space"] is IEnumerable<IDimension>))
                throw new Exception("Space is uncorrectly");
            Space = new List<IDimension>(parameter["Space"] as IEnumerable<IDimension>);

            foreach (var value in Space)
            {
                _countOfR += value.CountOfR;
            }

            _mapOfContainers = new Dictionary<int, IRTreeDataContainer>();

            if (MinCountOfNode*2 > MaxCountOfNode+1)
                throw new Exception("with this parametrs MinCount and MaxCount is split method inposible");

            _countOfReInsert = (MaxCountOfNode - MinCountOfNode) / 3;
            if (_countOfReInsert < 1) _countOfReInsert = 1;

            //set of root
            var lower = new float[_countOfR];

            var upper = new float[_countOfR];

            for (int i = 0; i < _countOfR; i++)
            {
                lower[i] = float.PositiveInfinity;
                upper[i] = float.NegativeInfinity;
            }
            _root = new RTreeLeaf(lower, upper);

            _levelForOverflowStrategy = new List<bool> {true};
        }
コード例 #11
0
ファイル: RTree.cs プロジェクト: politician/fallen-8
 public void Wipe()
 {
     _mapOfContainers.Clear();
     var lower = new float[_countOfR];
     var upper = new float[_countOfR];
     for (int i = 0; i < _countOfR; i++)
     {
         lower[i] = float.PositiveInfinity;
         upper[i] = float.NegativeInfinity;
     }
     _root = new RTreeLeaf(lower, upper);
     _levelForOverflowStrategy.Clear();
     _levelForOverflowStrategy.Add(true);
 }
コード例 #12
0
ファイル: RTree.cs プロジェクト: politician/fallen-8
 public void Dispose()
 {
     _mapOfContainers.Clear();
     Metric = null;
     _root.Dispose();
     _root = null;
     _levelForOverflowStrategy.Clear();
     Space = null;
 }
コード例 #13
0
ファイル: RTree.cs プロジェクト: politician/fallen-8
        private void Split(ARTreeContainer container, int level)
        {
            List<IRTreeContainer> result;
            var axis = ChooseSplitAxis(out result, container);
            ARTreeContainer container1;
            ARTreeContainer container2;

            if (!container.IsLeaf)
            {
                container1 = new RTreeNode();
                container2 = new RTreeNode();
            }
            else
            {
                container1 = new RTreeLeaf();
                container2 = new RTreeLeaf();

            }
            ChooseSplitIndex(result, axis, container1, container2);

            if (container.Parent == null)
            {
                var lower = _root.Lower;
                var upper = _root.Upper;
                var area = _root.Area;
                _root.Dispose();
                _root = null;
                _root = new RTreeNode(lower, upper) {Area = area};
                container1.Parent = _root;
                container2.Parent = _root;
                ((RTreeNode)_root).Children.Add(container1);
                ((RTreeNode)_root).Children.Add(container2);
                _levelForOverflowStrategy.Add(true);
            }
            else
            {
                container1.Parent = container.Parent;
                container2.Parent = container.Parent;

                ((RTreeNode)container.Parent).Children.Remove(container);
                ((RTreeNode)container.Parent).Children.Add(container1);
                ((RTreeNode)container.Parent).Children.Add(container2);

                container.Dispose();
                if (!container1.Parent.IsLeaf)
                {
                    if (((RTreeNode)container1.Parent).Children.Count > MaxCountOfNode)
                        OverflowTreatment(level - 1, container1.Parent);
                }
                else
                {
                    if (((RTreeLeaf)container2.Parent).Data.Count > MaxCountOfNode)
                        OverflowTreatment(level - 1, container2.Parent);
                }
            }
        }
コード例 #14
0
ファイル: RTree.cs プロジェクト: politician/fallen-8
        private void ReInsert(ARTreeContainer container, int level)
        {
            var center = FindCenterOfContainer(container);
            var distance = new List<Tuple<float, IRTreeContainer>>(MaxCountOfNode + 1);
            var dataContainer = container.IsLeaf
                                     ? new List<ISpatialContainer>(((RTreeLeaf) container).Data)
                                     : new List<ISpatialContainer>(((RTreeNode) container).Children);

            foreach (IRTreeContainer value in dataContainer)
            {
                if (value is APointContainer)
                    distance.Add(Tuple.Create(
                        Metric.Distance(new RTreePoint(((APointContainer) value).Coordinates),
                                        new RTreePoint(center)), value));
                else
                    distance.Add(Tuple.Create(
                        Metric.Distance(new RTreePoint(FindCenterOfContainer((ASpatialContainer) value)),
                                        new RTreePoint(center)), value));
            }
            distance.Sort((x, y) => x.Item1.CompareTo(y.Item1));
            distance.Reverse();

            var reinsertData = new List<Tuple<float, IRTreeContainer>>(_countOfReInsert);
            reinsertData.AddRange(distance.GetRange(0, _countOfReInsert));
            if (container.IsLeaf)
                ((RTreeLeaf) container).Data.RemoveAll(_ => reinsertData.Exists(y => y.Item2 == _));
            else
                ((RTreeNode) container).Children.RemoveAll(_ => reinsertData.Exists(y => y.Item2 == _));

            RecalculationByRemoving(reinsertData.Select(_ => _.Item2));
            var leafLevel = _levelForOverflowStrategy.Count - 1;
            for (int i = 0; i < _countOfReInsert; i++)
            {
                reinsertData[i].Item2.Parent = null;
                var newLeafLeavel = _levelForOverflowStrategy.Count - 1;
                Insert(reinsertData[i].Item2, level - (leafLevel - newLeafLeavel));
            }
        }
コード例 #15
0
ファイル: RTree.cs プロジェクト: politician/fallen-8
        private void OverflowTreatment(int level, ARTreeContainer container)
        {
            if (level != 0 && _levelForOverflowStrategy[level])
            {
                _levelForOverflowStrategy[level] = false;
                ReInsert(container, level);

            }
            else
            {
                Split(container, level);
            }
        }
コード例 #16
0
ファイル: RTree.cs プロジェクト: politician/fallen-8
        private void LocalReorganisationByRemoving(ARTreeContainer rTree, int level)
        {
            if (rTree.Parent != null)
            {
                ((RTreeNode)rTree.Parent).Children.Remove(rTree);
                RecalculationByRemoving(rTree);
                var leafLevel = _levelForOverflowStrategy.Count - 1;
                if (((RTreeNode)rTree.Parent).Children.Count < MinCountOfNode)
                    LocalReorganisationByRemoving(rTree.Parent, level - 1);

                if (rTree is RTreeLeaf)
                {
                    foreach (var value in ((RTreeLeaf)rTree).Data)
                    {
                        var newLeafLeavel = _levelForOverflowStrategy.Count - 1;
                        Insert(value, level - (leafLevel - newLeafLeavel));
                    }

                }
                else
                {
                    foreach (var value in ((RTreeNode)rTree).Children)
                    {
                        var newLeafLeavel = _levelForOverflowStrategy.Count - 1;
                        Insert(value, level - (leafLevel - newLeafLeavel));
                    }

                }

                rTree.Dispose();
            }
            else
            {
                if (rTree is RTreeLeaf)
                {
                    if (((RTreeLeaf)rTree).Data.Count == 0)
                        for (int i = 0; i < _countOfR; i++)
                        {
                            rTree.Upper[i] = float.NegativeInfinity;
                            rTree.Lower[i] = float.PositiveInfinity;
                        }
                }
                else
                {
                    if (((RTreeNode)rTree).Children.Count == 1)
                    {
                        _root = ((RTreeNode)rTree).Children[0];
                        _root.Parent = null;
                        rTree.Dispose();
                        _levelForOverflowStrategy.RemoveAt(_levelForOverflowStrategy.Count - 1);
                    }
                }

            }
        }