PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/source/library/Interlace/Geo/Indexing/RTree.cs

https://bitbucket.org/VahidN/interlace
C# | 128 lines | 67 code | 20 blank | 41 comment | 5 complexity | 8c23db82a47c454ded70115c3ebe2ace MD5 | raw file
  1. #region Using Directives and Copyright Notice
  2. // Copyright (c) 2007-2010, Computer Consultancy Pty Ltd
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above copyright
  10. // notice, this list of conditions and the following disclaimer in the
  11. // documentation and/or other materials provided with the distribution.
  12. // * Neither the name of the Computer Consultancy Pty Ltd nor the
  13. // names of its contributors may be used to endorse or promote products
  14. // derived from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL COMPUTER CONSULTANCY PTY LTD BE LIABLE
  20. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. // DAMAGE.
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Diagnostics;
  30. using System.Text;
  31. #endregion
  32. namespace Interlace.Geo.Indexing
  33. {
  34. /// <summary>
  35. /// A visitor for RTree nodes.
  36. /// </summary>
  37. public delegate void RTreeVisitorDelegate<T>(T obj);
  38. [Serializable]
  39. public class RTree
  40. {
  41. RTreeNode _root;
  42. int _nodeCapacity;
  43. int _nodeMinimum;
  44. /// <summary>
  45. /// Initializes a new instance of the <see cref="T:RTree"/> class.
  46. /// </summary>
  47. /// <param name="nodeCapacity">The node capacity.</param>
  48. /// <param name="nodeMinimum">The node minimum.</param>
  49. public RTree(int nodeCapacity, int nodeMinimum)
  50. {
  51. if (nodeMinimum > nodeCapacity / 2)
  52. {
  53. throw new ArgumentException("The node minimum must be less than or equal to " +
  54. "half of the node capacity.", "nodeMinimum");
  55. }
  56. _nodeCapacity = nodeCapacity;
  57. _nodeMinimum = nodeMinimum;
  58. _root = new RTreeLeaf(this);
  59. _root.Parent = null;
  60. }
  61. public int NodeCapacity
  62. {
  63. get
  64. {
  65. return _nodeCapacity;
  66. }
  67. }
  68. public int NodeMinimum
  69. {
  70. get
  71. {
  72. return _nodeMinimum;
  73. }
  74. }
  75. /// <summary>
  76. /// Inserts the specified object.
  77. /// </summary>
  78. /// <param name="bounds">The bounding box of the object.</param>
  79. /// <param name="obj">The object.</param>
  80. public void Insert<T>(Box bounds, T obj)
  81. {
  82. _root.Insert(new RTreeObject(bounds, obj));
  83. }
  84. /// <summary>
  85. /// Finds objects intersecting the supplied bounding box.
  86. /// </summary>
  87. public void Find<T>(RTreeVisitorDelegate<T> visitor, Box bounds)
  88. {
  89. _root.Find<T>(visitor, bounds);
  90. }
  91. public bool DoesBoxExist(Box bounds)
  92. {
  93. return _root.DoesBoxExist(bounds);
  94. }
  95. internal void ReplaceRootWithSplitResult(RTreeNode lhsNode, RTreeNode rhsNode)
  96. {
  97. RTreeNonLeaf newRoot = new RTreeNonLeaf(this);
  98. newRoot.ReplaceNodeWithSplitResult(null, lhsNode, rhsNode);
  99. _root = newRoot;
  100. }
  101. public void ThrowOnInvariantsViolated()
  102. {
  103. if (_root == null) throw new InvalidOperationException();
  104. if (_root.Parent != null) throw new InvalidOperationException();
  105. _root.ThrowOnInvariantsViolated(null);
  106. }
  107. }
  108. }