private void RDeletionTest() { Console.WriteLine("/////Deletion/////"); RTreeRectangle searchArea = new RTreeRectangle(); searchArea.X1 = -5; searchArea.Y1 = -1; searchArea.X2 = 0; searchArea.Y2 = 3; RTree <int> testTree = new RTree <int>(); // Outside testTree.Insert(99, 1, 0); testTree.Insert(100, 1, 6); var newleaf = new LeafRecord <int> { Data = 1, BBox = new RTreeRectangle() }; // Inside testTree.Insert(newleaf); testTree.Insert(2, -5, -1); testTree.Insert(3, 0, 3); testTree.Insert(4, -1, 2); testTree.Delete(newleaf); foreach (var leaf in testTree.Search(searchArea)) { Console.WriteLine(leaf.Data); } Console.WriteLine("/////Deletion Done/////"); }
/// <summary> /// Searches for all data points below the input root for points that /// fall within the searchbox /// </summary> /// <param name="SearchBox">Area to search</param> /// <param name="aRoot">Node to start looking</param> /// <returns>List of all leafRecords that reside within the searchbox</returns> private List <LeafRecord <T> > search(RTreeRectangle SearchBox, NodeRecord <T> aRoot) { var matches = new List <LeafRecord <T> >(); if (!SearchBox.Overlaps(aRoot.BBox)) { // If there is no overlap, return nothing. return(matches); } else if (!aRoot.Node.IsLeaf()) { // If the node is not a leaf, recursively call this function // for each of its children. foreach (var childNode in aRoot.Node.GetRecords()) { NodeRecord <T> node = (NodeRecord <T>)childNode; matches = matches.Concat(search(SearchBox, node)).ToList(); } return(matches); } else { // If this node is a leaf, add each data point that resides in the // search box to the match list. foreach (var childNode in aRoot.Node.GetRecords()) { LeafRecord <T> node = (LeafRecord <T>)childNode; if (SearchBox.Overlaps(node.BBox)) { matches.Add(node); } } return(matches); } }
private void RInsertionTest() { Console.WriteLine("/////Insertion/////"); // TODO: Actually write a test. RTreeRectangle searchArea = new RTreeRectangle(); searchArea.X1 = -5; searchArea.Y1 = -1; searchArea.X2 = 0; searchArea.Y2 = 3; RTree <int> testTree = new RTree <int>(); // Outside testTree.Insert(99, 1, 0); testTree.Insert(100, 1, 6); // Inside testTree.Insert(1, 0, 0); testTree.Insert(2, -5, -1); testTree.Insert(3, 0, 3); testTree.Insert(4, -1, 2); foreach (var leaf in testTree.Search(searchArea)) { Console.WriteLine(leaf.Data); } Console.WriteLine("/////Insertion Done/////"); }
/// <summary> /// Returns whether two rectangles intersect. /// </summary> /// <param name="b"></param> /// <returns></returns> public bool Overlaps(RTreeRectangle b) { return(b.X1 <= X2 && b.Y1 <= Y2 && b.X2 >= X1 && b.Y2 >= Y1); }
/// <summary> /// Returns true if both points equal. /// </summary> /// <param name="b"></param> /// <returns></returns> public bool IsEqual(RTreeRectangle b) { return(X1 == b.X1 && X2 == b.X2 && Y1 == b.Y1 && Y2 == b.Y2); }
/// <summary> /// Extends the size of this rectangle to include b. /// </summary> /// <param name="b"></param> public void Extend(RTreeRectangle b) { X1 = Math.Min(X1, b.X1); Y1 = Math.Min(Y1, b.Y1); X2 = Math.Max(X2, b.X2); Y2 = Math.Max(Y2, b.Y2); }
/// <summary> /// Cacluates the smallest rectangle that encloses both a and b. /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns>Size of the smallest rectangle</returns> public static int EnclosingArea(RTreeRectangle a, RTreeRectangle b) { int minX1 = Math.Min(a.X1, b.X1); int minY1 = Math.Min(a.Y1, b.Y1); int maxX2 = Math.Max(a.X2, b.X2); int maxY2 = Math.Max(a.Y2, b.Y2); return((maxX2 - minX1) * (maxY2 - minY1)); }
/// <summary> /// Calculates the minimum bounding box required for this node. /// </summary> public void ResizeBBox() { RTreeRectangle newBBox = new RTreeRectangle(); // Setting the values like this will guarantee the first // extend picks real values. newBBox.X1 = int.MaxValue; newBBox.Y1 = int.MaxValue; newBBox.X2 = int.MinValue; newBBox.Y2 = int.MinValue; foreach (var childNode in Node.GetRecords()) { newBBox.Extend(childNode.BBox); } BBox = newBBox; }
/// <summary> /// Wraps the search function. Eliminates the need to specify a node. /// </summary> /// <param name="SearchBox">Area to retrieve data from</param> /// <returns>List of all LeafRecords within that area.</returns> public List <LeafRecord <T> > Search(RTreeRectangle SearchBox) { return(search(SearchBox, root)); }