QCAD
Open Source 2D CAD
RSpatialIndexNavel.h
Go to the documentation of this file.
1 
20 #ifndef RSPATIALINDEXNAVEL_H
21 #define RSPATIALINDEXNAVEL_H
22 
23 #include "spatialindex_global.h"
24 
25 #include <QSet>
26 #include <QList>
27 
28 #include "RSpatialIndex.h"
29 
30 #include "spatialindexnavel/include/spatialindex/SpatialIndex.h"
31 
32 class RSiDataStream : public SpatialIndex::IDataStream
33 {
34 public:
35  RSiDataStream(const QList<int>& ids, const QList<QList<RBox> >& bbs) : ids(ids), bbs(bbs), index(0), pos(0) {
36  }
37 
38  virtual ~RSiDataStream() { }
39 
40  virtual SpatialIndex::IData* getNext() {
41  if (index>=ids.length() || index>=bbs.length()) {
42  return NULL;
43  }
44 
45  RBox bb = bbs[index][pos];
46  double p1[] = {
47  bb.getMinimum().x, bb.getMinimum().y, bb.getMinimum().z
48  };
49  double p2[] = {
50  bb.getMaximum().x, bb.getMaximum().y, bb.getMaximum().z
51  };
52  SpatialIndex::Region r(p1, p2, 3);
53  qint64 id = RSpatialIndex::getSIId(ids[index], pos);
54 
55  if (pos<bbs[index].length()-1) {
56  pos++;
57  }
58  else {
59  index++;
60  pos = 0;
61  }
62 
63  return new SpatialIndex::RTree::Data(0, NULL, r, id);
64  }
65 
66  virtual bool hasNext() {
67  return index<ids.length() && index<bbs.length() && pos<bbs[index].length();
68  }
69 
70  virtual uint32_t size() {
71  throw Tools::NotSupportedException("Operation not supported.");
72  }
73 
74  virtual void rewind() {
75  index = 0;
76  pos = 0;
77  }
78 
79 private:
80  const QList<int>& ids;
81  const QList<QList<RBox> >& bbs;
82  int index;
83  int pos;
84 };
85 
86 
91 public:
93  id(id), found(false) {
94  }
96  }
97  virtual void visitData(int id, int pos,
98  double x1, double y1, double z1,
99  double x2, double y2, double z2) {
100 
101  Q_UNUSED(pos);
102 
103  if (this->id == id) {
104  double p1[] = { x1, y1, z1 };
105  double p2[] = { x2, y2, z2 };
106 
107  region = SpatialIndex::Region(p1, p2, 3);
108 
109  found = true;
110  }
111  }
112  virtual void visitNode(
113  double /*x1*/, double /*y1*/, double /*z1*/,
114  double /*x2*/, double /*y2*/, double /*z2*/) {}
115 
116 public:
117  int id;
118  SpatialIndex::Region region;
119  bool found;
120 };
121 
122 
123 
133 protected:
140  class RSiRegion: public SpatialIndex::Region {
141  public:
142  RSiRegion(double x1, double y1, double z1, double x2, double y2,
143  double z2);
144  };
145 
152  class RSiPoint: public SpatialIndex::Point {
153  public:
154  RSiPoint(double x, double y, double z);
155  };
156 
164  class Visitor: public SpatialIndex::IVisitor {
165  public:
166  Visitor(QMap<int, QSet<int> >& ids, RSpatialIndexVisitor* dataVisitor = NULL) :
167  ids(ids), dataVisitor(dataVisitor) {
168  }
169 
170  void visitNode(const SpatialIndex::INode& n) {
171  //printf("Visitor::visitNode: %Ld\n", n.getIdentifier());
172  if (dataVisitor != NULL) {
173  SpatialIndex::IShape* shape;
174  n.getShape(&shape);
175  if (shape == NULL) {
176  fprintf(stderr, "error: node has no shape\n");
177  return;
178  }
179  SpatialIndex::Region* region =
180  dynamic_cast<SpatialIndex::Region*> (shape);
181  if (region == NULL) {
182  fprintf(stderr,
183  "error: node shape in spacial index is not a SpatialIndex::Region\n");
184  return;
185  }
186  dataVisitor->visitNode(region->m_pLow[0],
187  region->m_pLow[1], region->m_pLow[2],
188  region->m_pHigh[0], region->m_pHigh[1],
189  region->m_pHigh[2]);
190  }
191  }
192 
193  void visitData(const SpatialIndex::IData& d) {
194  qint64 siid = d.getIdentifier();
195  int id = RSpatialIndex::getId(siid);
196  int pos = RSpatialIndex::getPos(siid);
197 
198  //if (ids.contains(id)) {
199  ids[id].insert(pos);
200  /*
201  qFatal("Visitor.visitData: double entry found in spatial index, identifier: %d",
202  (int)d.getIdentifier());
203  Q_ASSERT(false);
204  */
205 // }
206 // else {
207 // ids.insert(id, QSet<int>() << pos);
208 // }
209 
210  //ids.insert(d.getIdentifier());
211 
212  if (dataVisitor != NULL) {
213  SpatialIndex::IShape* shape;
214  d.getShape(&shape);
215  if (shape == NULL) {
216  fprintf(stderr, "error: data node has no shape\n");
217  return;
218  }
219  SpatialIndex::Region* region =
220  dynamic_cast<SpatialIndex::Region*> (shape);
221  if (region == NULL) {
222  fprintf(stderr,
223  "error: shape in spacial index is not a SpatialIndex::Region\n");
224  return;
225  }
226 
227  //size_t len;
228  //uint8_t* data;
229  //d.getData(len, &data);
230  dataVisitor->visitData(id, pos, region->m_pLow[0],
231  region->m_pLow[1], region->m_pLow[2],
232  region->m_pHigh[0], region->m_pHigh[1],
233  region->m_pHigh[2]);
234  //region->getX1(), region->getY1(), region->getZ1(),
235  //region->getX2(), region->getY2(), region->getZ2(),
236  //len, data);
237  //delete data;
238  //delete region;
239  delete shape;
240  }
241  }
242 
243  void visitData(std::vector<const SpatialIndex::IData*>& v) {
244  std::vector<const SpatialIndex::IData*>::iterator it;
245  for (it = v.begin(); it != v.end(); it++) {
246  printf("Visitor::visitData[]: %Ld\n", (*it)->getIdentifier());
247  }
248  }
249 
250  private:
251  QMap<int, QSet<int> >& ids;
253  };
254 
255 public:
258 
259  //static int dataToInt(const uint8_t* data);
260  //static void intToData(int i, uint8_t* data);
261 
262  virtual void clear();
263 
264  virtual void bulkLoad(const QList<int>& ids, const QList<QList<RBox> >& bbs);
265 
266  virtual void addToIndex(int id, int pos,
267  double x1, double y1, double z1,
268  double x2, double y2, double z2);
269 
270  virtual void addToIndex(int id, int pos,
271  const RBox& bb);
272 
273  //void removeFromIndex(int id);
274  virtual bool removeFromIndex(int id, const QList<RBox>& bb);
275  virtual bool removeFromIndex(int id, int pos, const RBox& bb);
276  virtual bool removeFromIndex(
277  int id,
278  int pos,
279  double x1, double y1, double z1,
280  double x2, double y2, double z2);
281 
282  virtual QMap<int, QSet<int> > queryIntersected(
283  double x1, double y1, double z1,
284  double x2, double y2, double z2,
285  RSpatialIndexVisitor* dataVisitor = NULL);
286  QList<int> queryIntersectedSimple(const RBox& b) {
287  return RSpatialIndex::queryIntersected(b).keys();
288  }
289  virtual QMap<int, QSet<int> > queryContained(
290  double x1, double y1, double z1,
291  double x2, double y2, double z2,
292  RSpatialIndexVisitor* dataVisitor = NULL);
293 
294  virtual QMap<int, QSet<int> > queryNearestNeighbor(
295  unsigned int k,
296  double x, double y, double z,
297  RSpatialIndexVisitor* dataVisitor = NULL);
298 
299  virtual QPair<int, int> queryNearestNeighbor(double x, double y, double z);
300 
301 protected:
305  void addToIndex(int id, int pos, const RSiRegion& region, size_t dataLength = 0,
306  const uint8_t* data = NULL);
310  QMap<int, QSet<int> > queryIntersected(const RSiRegion& region,
311  RSpatialIndexVisitor* dataVisitor = NULL);
315  QMap<int, QSet<int> > queryContained(const RSiRegion& region,
316  RSpatialIndexVisitor* dataVisitor = NULL);
320  QMap<int, QSet<int> > queryNearestNeighbor(unsigned int k, const RSiPoint& point,
321  RSpatialIndexVisitor* dataVisitor = NULL);
322 
323 // QSet<int> sidsToIds(const QSet<int> sids);
324 // QSet<int> idToSids(int id);
325 
326 protected:
327  void init();
328  void uninit();
329 
330 protected:
331  SpatialIndex::ISpatialIndex* tree;
332  SpatialIndex::IStorageManager* buff;
333 
334 // QMap<int, int> sidToId;
335 // QMap<int, QList<int> > idToSid;
336 // int sidCounter;
337 };
338 
340 
341 #endif
Represents a box e.g.
Definition: RBox.h:43
RVector getMaximum() const
Definition: RBox.cpp:117
virtual void clear()=0
SpatialIndex::ISpatialIndex * tree
Definition: RSpatialIndexNavel.h:331
void visitData(const SpatialIndex::IData &d)
Definition: RSpatialIndexNavel.h:193
Copyright (c) 2011-2015 by Andrew Mustun.
Definition: RSpatialIndexNavel.h:32
virtual QMap< int, QSet< int > > queryContained(double x1, double y1, double z1, double x2, double y2, double z2, RSpatialIndexVisitor *dataVisitor=NULL)=0
Queries the index for all items that are completely inside the given box x1,y1,z1,x2,y2,z2.
Copyright (c) 2011-2015 by Andrew Mustun.
Definition: RSpatialIndexVisitor.h:35
virtual SpatialIndex::IData * getNext()
Definition: RSpatialIndexNavel.h:40
Internal representation of a point.
Definition: RSpatialIndexNavel.h:152
virtual ~RSiDataStream()
Definition: RSpatialIndexNavel.h:38
virtual bool hasNext()
Definition: RSpatialIndexNavel.h:66
static int getId(qint64 siid)
Definition: RSpatialIndex.cpp:46
double z
Getter function for this property: getZ.
Definition: RVector.h:269
virtual void addToIndex(int id, int pos, double x1, double y1, double z1, double x2, double y2, double z2)=0
Adds a new entry into this spatial index.
double y
Getter function for this property: getY.
Definition: RVector.h:263
int index
Definition: RSpatialIndexNavel.h:82
Definition: RSpatialIndexNavel.h:90
void visitData(std::vector< const SpatialIndex::IData * > &v)
Definition: RSpatialIndexNavel.h:243
SpatialIndex::IStorageManager * buff
Definition: RSpatialIndexNavel.h:332
static int getPos(qint64 siid)
Definition: RSpatialIndex.cpp:50
Internal representation of a rectangular region.
Definition: RSpatialIndexNavel.h:140
Internal node and data visitor.
Definition: RSpatialIndexNavel.h:164
virtual void visitNode(double, double, double, double, double, double)
Definition: RSpatialIndexNavel.h:112
virtual uint32_t size()
Definition: RSpatialIndexNavel.h:70
Q_DECLARE_METATYPE(RMath *)
QMap< int, QSet< int > > & ids
Definition: RSpatialIndexNavel.h:251
RSiDataStream(const QList< int > &ids, const QList< QList< RBox > > &bbs)
Definition: RSpatialIndexNavel.h:35
SpatialIndex::Region region
Definition: RSpatialIndexNavel.h:118
const QList< int > & ids
Definition: RSpatialIndexNavel.h:80
RVector getMinimum() const
Definition: RBox.cpp:111
QList< int > queryIntersectedSimple(const RBox &b)
Definition: RSpatialIndexNavel.h:286
int pos
Definition: RSpatialIndexNavel.h:83
virtual ~RSiGetRegionVisitor()
Definition: RSpatialIndexNavel.h:95
virtual QMap< int, QSet< int > > queryNearestNeighbor(unsigned int k, double x, double y, double z, RSpatialIndexVisitor *dataVisitor=NULL)=0
Queries the index for closest neighbor items.
int id
Definition: RSpatialIndexNavel.h:117
Visitor(QMap< int, QSet< int > > &ids, RSpatialIndexVisitor *dataVisitor=NULL)
Definition: RSpatialIndexNavel.h:166
double x
Getter function for this property: getX.
Definition: RVector.h:257
virtual void bulkLoad(const QList< int > &ids, const QList< QList< RBox > > &bbs)
Definition: RSpatialIndex.cpp:54
void init(void basePath)
Definition: AddBlockInit.js:2
virtual void rewind()
Definition: RSpatialIndexNavel.h:74
virtual bool removeFromIndex(int id, int pos, double x1, double y1, double z1, double x2, double y2, double z2)=0
Removes the entry with the given ID from this spatial index.
virtual void visitData(int id, int pos, double x1, double y1, double z1, double x2, double y2, double z2)
Definition: RSpatialIndexNavel.h:97
bool found
Definition: RSpatialIndexNavel.h:119
RSpatialIndexVisitor * dataVisitor
Definition: RSpatialIndexNavel.h:252
#define QCADSPATIALINDEX_EXPORT
Definition: spatialindex_global.h:10
void visitNode(const SpatialIndex::INode &n)
Definition: RSpatialIndexNavel.h:170
const QList< QList< RBox > > & bbs
Definition: RSpatialIndexNavel.h:81
Abstract base class for spatial index implementations.
Definition: RSpatialIndex.h:69
RSiGetRegionVisitor(int id)
Definition: RSpatialIndexNavel.h:92
static qint64 getSIId(int id, int pos)
Definition: RSpatialIndex.cpp:42
Spatial index class.
Definition: RSpatialIndexNavel.h:132
virtual QMap< int, QSet< int > > queryIntersected(double x1, double y1, double z1, double x2, double y2, double z2, RSpatialIndexVisitor *dataVisitor=NULL)=0
Queries the index for all items that are completely inside or intersect with the given box x1...