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 <QtGlobal>
26 #include <QSet>
27 #include <QList>
28 
29 #include "RSpatialIndex.h"
30 
31 #include "spatialindexnavel/include/spatialindex/SpatialIndex.h"
32 
33 class RSiDataStream : public SpatialIndex::IDataStream
34 {
35 public:
36  RSiDataStream(const QList<int>& ids, const QList<QList<RBox> >& bbs)
37  : ids(ids), bbs(bbs), index(0), pos(0), done(false) {
38 
39  Q_ASSERT(this->ids.length()==this->bbs.length());
40  }
41 
42  virtual ~RSiDataStream() { }
43 
44  virtual SpatialIndex::IData* getNext() {
45  Q_ASSERT(index<ids.length());
46  Q_ASSERT(index<bbs.length());
47  Q_ASSERT(pos<bbs[index].length());
48 
49  RBox bb = bbs[index][pos];
50  double p1[] = {
51  bb.getMinimum().x, bb.getMinimum().y, bb.getMinimum().z
52  };
53  double p2[] = {
54  bb.getMaximum().x, bb.getMaximum().y, bb.getMaximum().z
55  };
56  SpatialIndex::Region r(p1, p2, 3);
57  qint64 id = RSpatialIndex::getSIId(ids[index], pos);
58 
59  if (pos<bbs[index].length()-1) {
60  pos++;
61  }
62  else {
63  index++;
64  pos = 0;
65  if (index>=ids.length() || index>=bbs.length()) {
66  done = true;
67  }
68  else {
69  // skip empty bbs lists:
70  while (index<bbs.length() && bbs[index].isEmpty()) {
71  index++;
72  }
73  }
74  }
75 
76  return new SpatialIndex::RTree::Data(0, NULL, r, id);
77  }
78 
79  virtual bool hasNext() {
80  return !done;
81  }
82 
83  virtual uint32_t size() {
84  throw Tools::NotSupportedException("Operation not supported.");
85  }
86 
87  virtual void rewind() {
88  index = 0;
89  pos = 0;
90  }
91 
92 private:
93  const QList<int>& ids;
94  const QList<QList<RBox> >& bbs;
95  int index;
96  int pos;
97  bool done;
98 };
99 
100 
105 public:
107  id(id), found(false) {
108  }
110  }
111  virtual void visitData(int id, int pos,
112  double x1, double y1, double z1,
113  double x2, double y2, double z2) {
114 
115  Q_UNUSED(pos);
116 
117  if (this->id == id) {
118  double p1[] = { x1, y1, z1 };
119  double p2[] = { x2, y2, z2 };
120 
121  region = SpatialIndex::Region(p1, p2, 3);
122 
123  found = true;
124  }
125  }
126  virtual void visitNode(
127  double /*x1*/, double /*y1*/, double /*z1*/,
128  double /*x2*/, double /*y2*/, double /*z2*/) {}
129 
130 public:
131  int id;
132  SpatialIndex::Region region;
133  bool found;
134 };
135 
136 
137 
147 protected:
154  class RSiRegion: public SpatialIndex::Region {
155  public:
156  RSiRegion(double x1, double y1, double z1, double x2, double y2, double z2);
157  };
158 
165  class RSiPoint: public SpatialIndex::Point {
166  public:
167  RSiPoint(double x, double y, double z);
168  };
169 
177  class Visitor: public SpatialIndex::IVisitor {
178  public:
179  Visitor(QMap<int, QSet<int> >& ids, RSpatialIndexVisitor* dataVisitor = NULL) :
180  ids(ids), dataVisitor(dataVisitor) {
181  }
182 
183  void visitNode(const SpatialIndex::INode& n) {
184  //printf("Visitor::visitNode: %Ld\n", n.getIdentifier());
185  if (dataVisitor != NULL) {
186  SpatialIndex::IShape* shape;
187  n.getShape(&shape);
188  if (shape == NULL) {
189  fprintf(stderr, "error: node has no shape\n");
190  return;
191  }
192  SpatialIndex::Region* region =
193  dynamic_cast<SpatialIndex::Region*> (shape);
194  if (region == NULL) {
195  fprintf(stderr,
196  "error: node shape in spacial index is not a SpatialIndex::Region\n");
197  return;
198  }
199  dataVisitor->visitNode(region->m_pLow[0],
200  region->m_pLow[1], region->m_pLow[2],
201  region->m_pHigh[0], region->m_pHigh[1],
202  region->m_pHigh[2]);
203  }
204  }
205 
206  void visitData(const SpatialIndex::IData& d) {
207  qint64 siid = d.getIdentifier();
208  int id = RSpatialIndex::getId(siid);
209  int pos = RSpatialIndex::getPos(siid);
210 
211  //if (ids.contains(id)) {
212  ids[id].insert(pos);
213  /*
214  qFatal("Visitor.visitData: double entry found in spatial index, identifier: %d",
215  (int)d.getIdentifier());
216  Q_ASSERT(false);
217  */
218 // }
219 // else {
220 // ids.insert(id, QSet<int>() << pos);
221 // }
222 
223  //ids.insert(d.getIdentifier());
224 
225  if (dataVisitor != NULL) {
226  SpatialIndex::IShape* shape;
227  d.getShape(&shape);
228  if (shape == NULL) {
229  fprintf(stderr, "error: data node has no shape\n");
230  return;
231  }
232  SpatialIndex::Region* region =
233  dynamic_cast<SpatialIndex::Region*> (shape);
234  if (region == NULL) {
235  fprintf(stderr,
236  "error: shape in spacial index is not a SpatialIndex::Region\n");
237  return;
238  }
239 
240  //size_t len;
241  //uint8_t* data;
242  //d.getData(len, &data);
243  dataVisitor->visitData(id, pos, region->m_pLow[0],
244  region->m_pLow[1], region->m_pLow[2],
245  region->m_pHigh[0], region->m_pHigh[1],
246  region->m_pHigh[2]);
247  //region->getX1(), region->getY1(), region->getZ1(),
248  //region->getX2(), region->getY2(), region->getZ2(),
249  //len, data);
250  //delete data;
251  //delete region;
252  delete shape;
253  }
254  }
255 
256  void visitData(std::vector<const SpatialIndex::IData*>& v) {
257  std::vector<const SpatialIndex::IData*>::iterator it;
258  for (it = v.begin(); it != v.end(); it++) {
259  printf("Visitor::visitData[]: %lld\n", (*it)->getIdentifier());
260  }
261  }
262 
263  private:
264  QMap<int, QSet<int> >& ids;
266  };
267 
268 public:
271 
272  //static int dataToInt(const uint8_t* data);
273  //static void intToData(int i, uint8_t* data);
274 
275  virtual RSpatialIndex* create();
276  virtual void clear();
277 
278  virtual void bulkLoad(const QList<int>& ids, const QList<QList<RBox> >& bbs);
279 
280  virtual void addToIndex(int id, int pos,
281  double x1, double y1, double z1,
282  double x2, double y2, double z2);
283 
284  virtual void addToIndex(int id, int pos,
285  const RBox& bb);
286 
287  //void removeFromIndex(int id);
288  virtual bool removeFromIndex(int id, const QList<RBox>& bb);
289  virtual bool removeFromIndex(int id, int pos, const RBox& bb);
290  virtual bool removeFromIndex(
291  int id,
292  int pos,
293  double x1, double y1, double z1,
294  double x2, double y2, double z2);
295 
296  virtual QMap<int, QSet<int> > queryIntersected(
297  double x1, double y1, double z1,
298  double x2, double y2, double z2,
299  RSpatialIndexVisitor* dataVisitor = NULL);
300  QList<int> queryIntersectedSimple(const RBox& b) {
301  return RSpatialIndex::queryIntersected(b).keys();
302  }
303  virtual QMap<int, QSet<int> > queryContained(
304  double x1, double y1, double z1,
305  double x2, double y2, double z2,
306  RSpatialIndexVisitor* dataVisitor = NULL);
307 
308  virtual QMap<int, QSet<int> > queryNearestNeighbor(
309  unsigned int k,
310  double x, double y, double z,
311  RSpatialIndexVisitor* dataVisitor = NULL);
312 
313  virtual QPair<int, int> queryNearestNeighbor(double x, double y, double z);
314 
315 protected:
319  void addToIndex(int id, int pos, const RSiRegion& region, size_t dataLength = 0,
320  const uint8_t* data = NULL);
324  QMap<int, QSet<int> > queryIntersected(const RSiRegion& region,
325  RSpatialIndexVisitor* dataVisitor = NULL);
329  QMap<int, QSet<int> > queryContained(const RSiRegion& region,
330  RSpatialIndexVisitor* dataVisitor = NULL);
334  QMap<int, QSet<int> > queryNearestNeighbor(unsigned int k, const RSiPoint& point,
335  RSpatialIndexVisitor* dataVisitor = NULL);
336 
337 // QSet<int> sidsToIds(const QSet<int> sids);
338 // QSet<int> idToSids(int id);
339 
340 protected:
341  void init();
342  void uninit();
343 
344 protected:
345  SpatialIndex::ISpatialIndex* tree;
346  SpatialIndex::IStorageManager* buff;
347 
348 // QMap<int, int> sidToId;
349 // QMap<int, QList<int> > idToSid;
350 // int sidCounter;
351 };
352 
354 
355 #endif
Represents a box e.g.
Definition: RBox.h:43
RVector getMaximum() const
Definition: RBox.cpp:158
virtual void clear()=0
bool done
Definition: RSpatialIndexNavel.h:97
SpatialIndex::ISpatialIndex * tree
Definition: RSpatialIndexNavel.h:345
void visitData(const SpatialIndex::IData &d)
Definition: RSpatialIndexNavel.h:206
Copyright (c) 2011-2016 by Andrew Mustun.
Definition: RSpatialIndexNavel.h:33
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-2016 by Andrew Mustun.
Definition: RSpatialIndexVisitor.h:35
virtual SpatialIndex::IData * getNext()
Definition: RSpatialIndexNavel.h:44
Internal representation of a point.
Definition: RSpatialIndexNavel.h:165
virtual ~RSiDataStream()
Definition: RSpatialIndexNavel.h:42
virtual bool hasNext()
Definition: RSpatialIndexNavel.h:79
static int getId(qint64 siid)
Definition: RSpatialIndex.cpp:46
double z
Getter function for this property: getZ.
Definition: RVector.h:303
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:297
int index
Definition: RSpatialIndexNavel.h:95
Definition: RSpatialIndexNavel.h:104
void visitData(std::vector< const SpatialIndex::IData * > &v)
Definition: RSpatialIndexNavel.h:256
SpatialIndex::IStorageManager * buff
Definition: RSpatialIndexNavel.h:346
static int getPos(qint64 siid)
Definition: RSpatialIndex.cpp:50
Internal representation of a rectangular region.
Definition: RSpatialIndexNavel.h:154
Internal node and data visitor.
Definition: RSpatialIndexNavel.h:177
virtual void visitNode(double, double, double, double, double, double)
Definition: RSpatialIndexNavel.h:126
virtual uint32_t size()
Definition: RSpatialIndexNavel.h:83
Q_DECLARE_METATYPE(RMath *)
QMap< int, QSet< int > > & ids
Definition: RSpatialIndexNavel.h:264
RSiDataStream(const QList< int > &ids, const QList< QList< RBox > > &bbs)
Definition: RSpatialIndexNavel.h:36
SpatialIndex::Region region
Definition: RSpatialIndexNavel.h:132
const QList< int > & ids
Definition: RSpatialIndexNavel.h:93
RVector getMinimum() const
Definition: RBox.cpp:151
QList< int > queryIntersectedSimple(const RBox &b)
Definition: RSpatialIndexNavel.h:300
int pos
Definition: RSpatialIndexNavel.h:96
virtual ~RSiGetRegionVisitor()
Definition: RSpatialIndexNavel.h:109
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:131
Visitor(QMap< int, QSet< int > > &ids, RSpatialIndexVisitor *dataVisitor=NULL)
Definition: RSpatialIndexNavel.h:179
double x
Getter function for this property: getX.
Definition: RVector.h:291
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:87
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 RSpatialIndex * create()=0
virtual void visitData(int id, int pos, double x1, double y1, double z1, double x2, double y2, double z2)
Definition: RSpatialIndexNavel.h:111
bool found
Definition: RSpatialIndexNavel.h:133
RSpatialIndexVisitor * dataVisitor
Definition: RSpatialIndexNavel.h:265
#define QCADSPATIALINDEX_EXPORT
Definition: spatialindex_global.h:10
void visitNode(const SpatialIndex::INode &n)
Definition: RSpatialIndexNavel.h:183
const QList< QList< RBox > > & bbs
Definition: RSpatialIndexNavel.h:94
Abstract base class for spatial index implementations.
Definition: RSpatialIndex.h:69
RSiGetRegionVisitor(int id)
Definition: RSpatialIndexNavel.h:106
static qint64 getSIId(int id, int pos)
Definition: RSpatialIndex.cpp:42
Spatial index class.
Definition: RSpatialIndexNavel.h:146
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...