You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.8 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #ifndef PCBNEW_CONNECTIVITY_RTREE_H_
  24. #define PCBNEW_CONNECTIVITY_RTREE_H_
  25. #include <math/box2.h>
  26. #include <router/pns_layerset.h>
  27. #include <geometry/rtree.h>
  28. /**
  29. * Class CN_RTREE -
  30. * Implements an R-tree for fast spatial indexing of connectivity items.
  31. * Non-owning.
  32. */
  33. template< class T >
  34. class CN_RTREE
  35. {
  36. public:
  37. CN_RTREE()
  38. {
  39. this->m_tree = new RTree<T, int, 3, double>();
  40. }
  41. ~CN_RTREE()
  42. {
  43. delete this->m_tree;
  44. }
  45. /**
  46. * Function Insert()
  47. * Inserts an item into the tree. Item's bounding box is taken via its BBox() method.
  48. */
  49. void Insert( T aItem )
  50. {
  51. const BOX2I& bbox = aItem->BBox();
  52. const LAYER_RANGE layers = aItem->Layers();
  53. const int mmin[3] = { layers.Start(), bbox.GetX(), bbox.GetY() };
  54. const int mmax[3] = { layers.End(), bbox.GetRight(), bbox.GetBottom() };
  55. m_tree->Insert( mmin, mmax, aItem );
  56. }
  57. /**
  58. * Function Remove()
  59. * Removes an item from the tree. Removal is done by comparing pointers, attempting
  60. * to remove a copy of the item will fail.
  61. */
  62. void Remove( T aItem )
  63. {
  64. // First, attempt to remove the item using its given BBox
  65. const BOX2I& bbox = aItem->BBox();
  66. const LAYER_RANGE layers = aItem->Layers();
  67. const int mmin[3] = { layers.Start(), bbox.GetX(), bbox.GetY() };
  68. const int mmax[3] = { layers.End(), bbox.GetRight(), bbox.GetBottom() };
  69. // If we are not successful ( 1 == not found ), then we expand
  70. // the search to the full tree
  71. if( m_tree->Remove( mmin, mmax, aItem ) )
  72. {
  73. // N.B. We must search the whole tree for the pointer to remove
  74. // because the item may have been moved before we have the chance to
  75. // delete it from the tree
  76. const int mmin2[3] = { INT_MIN, INT_MIN, INT_MIN };
  77. const int mmax2[3] = { INT_MAX, INT_MAX, INT_MAX };
  78. m_tree->Remove( mmin2, mmax2, aItem );
  79. }
  80. }
  81. /**
  82. * Function RemoveAll()
  83. * Removes all items from the RTree
  84. */
  85. void RemoveAll( )
  86. {
  87. m_tree->RemoveAll();
  88. }
  89. /**
  90. * Function Query()
  91. * Executes a function object aVisitor for each item whose bounding box intersects
  92. * with aBounds.
  93. */
  94. template <class Visitor>
  95. void Query( const BOX2I& aBounds, const LAYER_RANGE& aRange, Visitor& aVisitor )
  96. {
  97. const int mmin[3] = { aRange.Start(), aBounds.GetX(), aBounds.GetY() };
  98. const int mmax[3] = { aRange.End(), aBounds.GetRight(), aBounds.GetBottom() };
  99. m_tree->Search( mmin, mmax, aVisitor );
  100. }
  101. private:
  102. RTree<T, int, 3, double>* m_tree;
  103. };
  104. #endif /* PCBNEW_CONNECTIVITY_RTREE_H_ */