QCAD
Open Source 2D CAD
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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.h"
31 
36 public:
38  id(id), found(false) {
39  }
41  }
42  virtual void visitData(int id, int pos,
43  double x1, double y1, double z1,
44  double x2, double y2, double z2) {
45 
46  Q_UNUSED(pos);
47 
48  if (this->id == id) {
49  double p1[] = { x1, y1, z1 };
50  double p2[] = { x2, y2, z2 };
51 
52  region = SpatialIndex::Region(p1, p2, 3);
53 
54  found = true;
55  }
56  }
57  virtual void visitNode(
58  double /*x1*/, double /*y1*/, double /*z1*/,
59  double /*x2*/, double /*y2*/, double /*z2*/) {}
60 
61 public:
62  int id;
63  SpatialIndex::Region region;
64  bool found;
65 };
66 
67 
68 
78 protected:
85  class RSiRegion: public SpatialIndex::Region {
86  public:
87  RSiRegion(double x1, double y1, double z1, double x2, double y2,
88  double z2);
89  };
90 
97  class RSiPoint: public SpatialIndex::Point {
98  public:
99  RSiPoint(double x, double y, double z);
100  };
101 
109  class Visitor: public SpatialIndex::IVisitor {
110  public:
111  Visitor(QMap<int, QSet<int> >& ids, RSpatialIndexVisitor* dataVisitor = NULL) :
112  ids(ids), dataVisitor(dataVisitor) {
113  }
114 
115  void visitNode(const SpatialIndex::INode& n) {
116  //printf("Visitor::visitNode: %Ld\n", n.getIdentifier());
117  if (dataVisitor != NULL) {
118  SpatialIndex::IShape* shape;
119  n.getShape(&shape);
120  if (shape == NULL) {
121  fprintf(stderr, "error: node has no shape\n");
122  return;
123  }
124  SpatialIndex::Region* region =
125  dynamic_cast<SpatialIndex::Region*> (shape);
126  if (region == NULL) {
127  fprintf(stderr,
128  "error: node shape in spacial index is not a SpatialIndex::Region\n");
129  return;
130  }
131  dataVisitor->visitNode(region->m_pLow[0],
132  region->m_pLow[1], region->m_pLow[2],
133  region->m_pHigh[0], region->m_pHigh[1],
134  region->m_pHigh[2]);
135  }
136  }
137 
138  void visitData(const SpatialIndex::IData& d) {
139  qint64 siid = d.getIdentifier();
140  int id = RSpatialIndex::getId(siid);
141  int pos = RSpatialIndex::getPos(siid);
142 
143  //if (ids.contains(id)) {
144  ids[id].insert(pos);
145  /*
146  qFatal("Visitor.visitData: double entry found in spatial index, identifier: %d",
147  (int)d.getIdentifier());
148  Q_ASSERT(false);
149  */
150 // }
151 // else {
152 // ids.insert(id, QSet<int>() << pos);
153 // }
154 
155  //ids.insert(d.getIdentifier());
156 
157  if (dataVisitor != NULL) {
158  SpatialIndex::IShape* shape;
159  d.getShape(&shape);
160  if (shape == NULL) {
161  fprintf(stderr, "error: data node has no shape\n");
162  return;
163  }
164  SpatialIndex::Region* region =
165  dynamic_cast<SpatialIndex::Region*> (shape);
166  if (region == NULL) {
167  fprintf(stderr,
168  "error: shape in spacial index is not a SpatialIndex::Region\n");
169  return;
170  }
171 
172  //size_t len;
173  //uint8_t* data;
174  //d.getData(len, &data);
175  dataVisitor->visitData(id, pos, region->m_pLow[0],
176  region->m_pLow[1], region->m_pLow[2],
177  region->m_pHigh[0], region->m_pHigh[1],
178  region->m_pHigh[2]);
179  //region->getX1(), region->getY1(), region->getZ1(),
180  //region->getX2(), region->getY2(), region->getZ2(),
181  //len, data);
182  //delete data;
183  //delete region;
184  delete shape;
185  }
186  }
187 
188  void visitData(std::vector<const SpatialIndex::IData*>& v) {
189  std::vector<const SpatialIndex::IData*>::iterator it;
190  for (it = v.begin(); it != v.end(); it++) {
191  printf("Visitor::visitData[]: %Ld\n", (*it)->getIdentifier());
192  }
193  }
194 
195  private:
196  QMap<int, QSet<int> >& ids;
198  };
199 
200 public:
203 
204  //static int dataToInt(const uint8_t* data);
205  //static void intToData(int i, uint8_t* data);
206 
207  virtual void clear();
208 
209  virtual void addToIndex(int id, int pos,
210  double x1, double y1, double z1,
211  double x2, double y2, double z2);
212 
213  virtual void addToIndex(int id, int pos,
214  const RBox& bb);
215 
216  //void removeFromIndex(int id);
217  virtual bool removeFromIndex(int id, const QList<RBox>& bb);
218  virtual bool removeFromIndex(int id, int pos, const RBox& bb);
219  virtual bool removeFromIndex(
220  int id,
221  int pos,
222  double x1, double y1, double z1,
223  double x2, double y2, double z2);
224 
225  virtual QMap<int, QSet<int> > queryIntersected(
226  double x1, double y1, double z1,
227  double x2, double y2, double z2,
228  RSpatialIndexVisitor* dataVisitor = NULL);
229  virtual QMap<int, QSet<int> > queryContained(
230  double x1, double y1, double z1,
231  double x2, double y2, double z2,
232  RSpatialIndexVisitor* dataVisitor = NULL);
233 
234  virtual QMap<int, QSet<int> > queryNearestNeighbor(
235  unsigned int k,
236  double x, double y, double z,
237  RSpatialIndexVisitor* dataVisitor = NULL);
238 
239  virtual QPair<int, int> queryNearestNeighbor(double x, double y, double z);
240 
241 protected:
245  void addToIndex(int id, int pos, const RSiRegion& region, size_t dataLength = 0,
246  const uint8_t* data = NULL);
250  QMap<int, QSet<int> > queryIntersected(const RSiRegion& region,
251  RSpatialIndexVisitor* dataVisitor = NULL);
255  QMap<int, QSet<int> > queryContained(const RSiRegion& region,
256  RSpatialIndexVisitor* dataVisitor = NULL);
260  QMap<int, QSet<int> > queryNearestNeighbor(unsigned int k, const RSiPoint& point,
261  RSpatialIndexVisitor* dataVisitor = NULL);
262 
263 // QSet<int> sidsToIds(const QSet<int> sids);
264 // QSet<int> idToSids(int id);
265 
266 protected:
267  void init();
268  void uninit();
269 
270 protected:
271  SpatialIndex::ISpatialIndex* tree;
272  SpatialIndex::IStorageManager* buff;
273 
274 // QMap<int, int> sidToId;
275 // QMap<int, QList<int> > idToSid;
276 // int sidCounter;
277 };
278 
280 
281 #endif