libspatialindex API Reference  (git-trunk)
rtree/Node.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Project: libspatialindex - A C++ library for spatial indexing
3  * Author: Marios Hadjieleftheriou, mhadji@gmail.com
4  ******************************************************************************
5  * Copyright (c) 2002, Marios Hadjieleftheriou
6  *
7  * All rights reserved.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included
17  * in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  * DEALINGS IN THE SOFTWARE.
26 ******************************************************************************/
27 
28 #pragma once
29 
30 #pragma GCC diagnostic push
31 #pragma GCC diagnostic ignored "-Wcast-qual"
32 
33 namespace SpatialIndex
34 {
35  namespace RTree
36  {
37  class RTree;
38  class Leaf;
39  class Index;
40  class Node;
41 
43 
44  class Node : public SpatialIndex::INode
45  {
46  public:
47  ~Node() override;
48 
49  //
50  // Tools::IObject interface
51  //
52  Tools::IObject* clone() override;
53 
54  //
55  // Tools::ISerializable interface
56  //
57  uint32_t getByteArraySize() override;
58  void loadFromByteArray(const uint8_t* data) override;
59  void storeToByteArray(uint8_t** data, uint32_t& len) override;
60 
61  //
62  // SpatialIndex::IEntry interface
63  //
64  id_type getIdentifier() const override;
65  void getShape(IShape** out) const override;
66 
67  //
68  // SpatialIndex::INode interface
69  //
70  uint32_t getChildrenCount() const override;
71  id_type getChildIdentifier(uint32_t index) const override;
72  void getChildShape(uint32_t index, IShape** out) const override;
73  void getChildData(uint32_t index, uint32_t& length, uint8_t** data) const override;
74  uint32_t getLevel() const override;
75  bool isIndex() const override;
76  bool isLeaf() const override;
77 
78  private:
79  Node();
80  Node(RTree* pTree, id_type id, uint32_t level, uint32_t capacity);
81 
82  virtual Node& operator=(const Node&);
83 
84  virtual void insertEntry(uint32_t dataLength, uint8_t* pData, Region& mbr, id_type id);
85  virtual void deleteEntry(uint32_t index);
86 
87  virtual bool insertData(uint32_t dataLength, uint8_t* pData, Region& mbr, id_type id, std::stack<id_type>& pathBuffer, uint8_t* overflowTable);
88  virtual void reinsertData(uint32_t dataLength, uint8_t* pData, Region& mbr, id_type id, std::vector<uint32_t>& reinsert, std::vector<uint32_t>& keep);
89 
90  virtual void rtreeSplit(uint32_t dataLength, uint8_t* pData, Region& mbr, id_type id, std::vector<uint32_t>& group1, std::vector<uint32_t>& group2);
91  virtual void rstarSplit(uint32_t dataLength, uint8_t* pData, Region& mbr, id_type id, std::vector<uint32_t>& group1, std::vector<uint32_t>& group2);
92 
93  virtual void pickSeeds(uint32_t& index1, uint32_t& index2);
94 
95  virtual void condenseTree(std::stack<NodePtr>& toReinsert, std::stack<id_type>& pathBuffer, NodePtr& ptrThis);
96 
97  virtual NodePtr chooseSubtree(const Region& mbr, uint32_t level, std::stack<id_type>& pathBuffer) = 0;
98  virtual NodePtr findLeaf(const Region& mbr, id_type id, std::stack<id_type>& pathBuffer) = 0;
99 
100  virtual void split(uint32_t dataLength, uint8_t* pData, Region& mbr, id_type id, NodePtr& left, NodePtr& right) = 0;
101 
102  RTree* m_pTree{nullptr};
103  // Parent of all nodes.
104 
105  uint32_t m_level{0};
106  // The level of the node in the tree.
107  // Leaves are always at level 0.
108 
109  id_type m_identifier{-1};
110  // The unique ID of this node.
111 
112  uint32_t m_children{0};
113  // The number of children pointed by this node.
114 
115  uint32_t m_capacity{0};
116  // Specifies the node capacity.
117 
118  Region m_nodeMBR;
119  // The minimum bounding region enclosing all data contained in the node.
120 
121  uint8_t** m_pData{nullptr};
122  // The data stored in the node.
123 
124  RegionPtr* m_ptrMBR{nullptr};
125  // The corresponding data MBRs.
126 
127  id_type* m_pIdentifier{nullptr};
128  // The corresponding data identifiers.
129 
130  uint32_t* m_pDataLength{nullptr};
131 
132  uint32_t m_totalDataLength{0};
133 
134  class RstarSplitEntry
135  {
136  public:
137  Region* m_pRegion;
138  uint32_t m_index;
139  uint32_t m_sortDim;
140 
141  RstarSplitEntry(Region* pr, uint32_t index, uint32_t dimension) :
142  m_pRegion(pr), m_index(index), m_sortDim(dimension) {}
143 
144  static int compareLow(const void* pv1, const void* pv2)
145  {
146  RstarSplitEntry* pe1 = * (RstarSplitEntry**) pv1;
147  RstarSplitEntry* pe2 = * (RstarSplitEntry**) pv2;
148 
149  assert(pe1->m_sortDim == pe2->m_sortDim);
150 
151  if (pe1->m_pRegion->m_pLow[pe1->m_sortDim] < pe2->m_pRegion->m_pLow[pe2->m_sortDim]) return -1;
152  if (pe1->m_pRegion->m_pLow[pe1->m_sortDim] > pe2->m_pRegion->m_pLow[pe2->m_sortDim]) return 1;
153  return 0;
154  }
155 
156  static int compareHigh(const void* pv1, const void* pv2)
157  {
158  RstarSplitEntry* pe1 = * (RstarSplitEntry**) pv1;
159  RstarSplitEntry* pe2 = * (RstarSplitEntry**) pv2;
160 
161  assert(pe1->m_sortDim == pe2->m_sortDim);
162 
163  if (pe1->m_pRegion->m_pHigh[pe1->m_sortDim] < pe2->m_pRegion->m_pHigh[pe2->m_sortDim]) return -1;
164  if (pe1->m_pRegion->m_pHigh[pe1->m_sortDim] > pe2->m_pRegion->m_pHigh[pe2->m_sortDim]) return 1;
165  return 0;
166  }
167  }; // RstarSplitEntry
168 
169  class ReinsertEntry
170  {
171  public:
172  uint32_t m_index;
173  double m_dist;
174 
175  ReinsertEntry(uint32_t index, double dist) : m_index(index), m_dist(dist) {}
176 
177  static int compareReinsertEntry(const void* pv1, const void* pv2)
178  {
179  ReinsertEntry* pe1 = * (ReinsertEntry**) pv1;
180  ReinsertEntry* pe2 = * (ReinsertEntry**) pv2;
181 
182  if (pe1->m_dist < pe2->m_dist) return -1;
183  if (pe1->m_dist > pe2->m_dist) return 1;
184  return 0;
185  }
186  }; // ReinsertEntry
187 
188  // Needed to access protected members without having to cast from Node.
189  // It is more efficient than using member functions to access protected members.
190  friend class RTree;
191  friend class Leaf;
192  friend class Index;
193  friend class Tools::PointerPool<Node>;
194  friend class BulkLoader;
195  }; // Node
196  }
197 }
198 #pragma GCC diagnostic pop
uint32_t getLevel() const override
Definition: rtree/Node.cc:211
void getChildShape(uint32_t index, IShape **out) const override
Definition: rtree/Node.cc:189
void loadFromByteArray(const uint8_t *data) override
Definition: rtree/Node.cc:63
void storeToByteArray(uint8_t **data, uint32_t &len) override
Definition: rtree/Node.cc:112
void getChildData(uint32_t index, uint32_t &length, uint8_t **data) const override
Definition: rtree/Node.cc:196
bool isIndex() const override
Definition: rtree/Node.cc:221
Tools::IObject * clone() override
Definition: rtree/Node.cc:44
uint32_t getChildrenCount() const override
Definition: rtree/Node.cc:177
uint32_t getByteArraySize() override
Definition: rtree/Node.cc:52
void getShape(IShape **out) const override
Definition: rtree/Node.cc:169
id_type getChildIdentifier(uint32_t index) const override
Definition: rtree/Node.cc:182
bool isLeaf() const override
Definition: rtree/Node.cc:216
id_type getIdentifier() const override
Definition: rtree/Node.cc:164
Tools::PoolPointer< Node > NodePtr
Definition: rtree/Node.h:40
int64_t id_type
Definition: SpatialIndex.h:41