libspatialindex API Reference  (git-trunk)
tprtree/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 #pragma GCC diagnostic push
30 #pragma GCC diagnostic ignored "-Wcast-qual"
31 
32 namespace SpatialIndex
33 {
34  namespace TPRTree
35  {
36  class TPRTree;
37  class Leaf;
38  class Index;
39  class Node;
40 
42 
43  class Node : public SpatialIndex::INode
44  {
45  public:
46  ~Node() override;
47 
48  //
49  // Tools::IObject interface
50  //
51  Tools::IObject* clone() override;
52 
53  //
54  // Tools::ISerializable interface
55  //
56  uint32_t getByteArraySize() override;
57  void loadFromByteArray(const uint8_t* data) override;
58  void storeToByteArray(uint8_t** data, uint32_t& len) override;
59 
60  //
61  // SpatialIndex::IEntry interface
62  //
63  id_type getIdentifier() const override;
64  void getShape(IShape** out) const override;
65 
66  //
67  // SpatialIndex::INode interface
68  //
69  uint32_t getChildrenCount() const override;
70  id_type getChildIdentifier(uint32_t index) const override;
71  void getChildShape(uint32_t index, IShape** out) const override;
72  void getChildData(uint32_t index, uint32_t& length, uint8_t** data) const override;
73  uint32_t getLevel() const override;
74  bool isIndex() const override;
75  bool isLeaf() const override;
76 
77  private:
78  Node();
79  Node(TPRTree* pTree, id_type id, uint32_t level, uint32_t capacity);
80 
81  virtual Node& operator=(const Node&);
82 
83  virtual bool insertEntry(uint32_t dataLength, uint8_t* pData, MovingRegion& mbr, id_type id);
84  virtual void deleteEntry(uint32_t index);
85 
86  virtual bool insertData(uint32_t dataLength, uint8_t* pData, MovingRegion& mbr, id_type id, std::stack<id_type>& pathBuffer, uint8_t* overflowTable);
87  virtual void reinsertData(uint32_t dataLength, uint8_t* pData, MovingRegion& mbr, id_type id, std::vector<uint32_t>& reinsert, std::vector<uint32_t>& keep);
88 
89  virtual void rstarSplit(uint32_t dataLength, uint8_t* pData, MovingRegion& mbr, id_type id, std::vector<uint32_t>& group1, std::vector<uint32_t>& group2);
90 
91  virtual void condenseTree(std::stack<NodePtr>& toReinsert, std::stack<id_type>& pathBuffer, NodePtr& ptrThis);
92 
93  virtual NodePtr chooseSubtree(const MovingRegion& mbr, uint32_t level, std::stack<id_type>& pathBuffer) = 0;
94  virtual NodePtr findLeaf(const MovingRegion& mbr, id_type id, std::stack<id_type>& pathBuffer) = 0;
95 
96  virtual void split(uint32_t dataLength, uint8_t* pData, MovingRegion& mbr, id_type id, NodePtr& left, NodePtr& right) = 0;
97 
98  TPRTree* m_pTree{nullptr};
99  // Parent of all nodes.
100 
101  uint32_t m_level{0};
102  // The level of the node in the tree.
103  // Leaves are always at level 0.
104 
105  id_type m_identifier{-1};
106  // The unique ID of this node.
107 
108  uint32_t m_children{0};
109  // The number of children pointed by this node.
110 
111  uint32_t m_capacity{0};
112  // Specifies the node capacity.
113 
114  MovingRegion m_nodeMBR;
115  // The minimum bounding region enclosing all data contained in the node.
116 
117  uint8_t** m_pData{nullptr};
118  // The data stored in the node.
119 
120  MovingRegionPtr* m_ptrMBR{nullptr};
121  // The corresponding data MBRs.
122 
123  id_type* m_pIdentifier{nullptr};
124  // The corresponding data identifiers.
125 
126  uint32_t* m_pDataLength{nullptr};
127 
128  uint32_t m_totalDataLength{0};
129 
130  class RstarSplitEntry
131  {
132  public:
133  MovingRegion* m_pRegion;
134  uint32_t m_index;
135  uint32_t m_sortDim;
136 
137  RstarSplitEntry(MovingRegion* pr, uint32_t index, uint32_t dimension)
138  : m_pRegion(pr), m_index(index), m_sortDim(dimension) {}
139 
140  static int compareLow(const void* pv1, const void* pv2)
141  {
142  RstarSplitEntry* pe1 = * (RstarSplitEntry**) pv1;
143  RstarSplitEntry* pe2 = * (RstarSplitEntry**) pv2;
144 
145  if (pe1->m_pRegion->m_pLow[pe1->m_sortDim] < pe2->m_pRegion->m_pLow[pe1->m_sortDim]) return -1;
146  if (pe1->m_pRegion->m_pLow[pe1->m_sortDim] > pe2->m_pRegion->m_pLow[pe1->m_sortDim]) return 1;
147  return 0;
148  }
149 
150  static int compareHigh(const void* pv1, const void* pv2)
151  {
152  RstarSplitEntry* pe1 = * (RstarSplitEntry**) pv1;
153  RstarSplitEntry* pe2 = * (RstarSplitEntry**) pv2;
154 
155  if (pe1->m_pRegion->m_pHigh[pe1->m_sortDim] < pe2->m_pRegion->m_pHigh[pe1->m_sortDim]) return -1;
156  if (pe1->m_pRegion->m_pHigh[pe1->m_sortDim] > pe2->m_pRegion->m_pHigh[pe1->m_sortDim]) return 1;
157  return 0;
158  }
159 
160  static int compareVLow(const void* pv1, const void* pv2)
161  {
162  RstarSplitEntry* pe1 = * (RstarSplitEntry**) pv1;
163  RstarSplitEntry* pe2 = * (RstarSplitEntry**) pv2;
164 
165  if (pe1->m_pRegion->m_pVLow[pe1->m_sortDim] < pe2->m_pRegion->m_pVLow[pe1->m_sortDim]) return -1;
166  if (pe1->m_pRegion->m_pVLow[pe1->m_sortDim] > pe2->m_pRegion->m_pVLow[pe1->m_sortDim]) return 1;
167  return 0;
168  }
169 
170  static int compareVHigh(const void* pv1, const void* pv2)
171  {
172  RstarSplitEntry* pe1 = * (RstarSplitEntry**) pv1;
173  RstarSplitEntry* pe2 = * (RstarSplitEntry**) pv2;
174 
175  if (pe1->m_pRegion->m_pVHigh[pe1->m_sortDim] < pe2->m_pRegion->m_pVHigh[pe1->m_sortDim]) return -1;
176  if (pe1->m_pRegion->m_pVHigh[pe1->m_sortDim] > pe2->m_pRegion->m_pVHigh[pe1->m_sortDim]) return 1;
177  return 0;
178  }
179  }; // RstarSplitEntry
180 
181  class ReinsertEntry
182  {
183  public:
184  uint32_t m_index;
185  double m_dist;
186 
187  ReinsertEntry(uint32_t index, double dist) : m_index(index), m_dist(dist) {}
188 
189  static int compareReinsertEntry(const void* pv1, const void* pv2)
190  {
191  ReinsertEntry* pe1 = * (ReinsertEntry**) pv1;
192  ReinsertEntry* pe2 = * (ReinsertEntry**) pv2;
193 
194  if (pe1->m_dist < pe2->m_dist) return -1;
195  if (pe1->m_dist > pe2->m_dist) return 1;
196  return 0;
197  }
198  }; // ReinsertEntry
199 
200  // Needed to access protected members without having to cast from Node.
201  // It is more efficient than using member functions to access protected members.
202  friend class TPRTree;
203  friend class Leaf;
204  friend class Index;
205  friend class Tools::PointerPool<Node>;
206  }; // Node
207  }
208 }
209 #pragma GCC diagnostic pop
bool isLeaf() const override
id_type getChildIdentifier(uint32_t index) const override
void storeToByteArray(uint8_t **data, uint32_t &len) override
void loadFromByteArray(const uint8_t *data) override
Definition: tprtree/Node.cc:64
void getChildShape(uint32_t index, IShape **out) const override
Tools::PoolPointer< Node > NodePtr
Definition: tprtree/Node.h:39
bool isIndex() const override
uint32_t getLevel() const override
Tools::IObject * clone() override
Definition: tprtree/Node.cc:44
void getChildData(uint32_t index, uint32_t &length, uint8_t **data) const override
int64_t id_type
Definition: SpatialIndex.h:41
id_type getIdentifier() const override
uint32_t getByteArraySize() override
Definition: tprtree/Node.cc:52
uint32_t getChildrenCount() const override
void getShape(IShape **out) const override