QCAD Application Framework
CAD Application Development and Automation.
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
RSpatialIndexNavel.h
Go to the documentation of this file.
1 #ifndef RSPATIALINDEXNAVEL_H
2 #define RSPATIALINDEXNAVEL_H
3 
4 #include <QSet>
5 #include <QList>
6 
7 #include "RSpatialIndex.h"
8 
9 #include "spatialindex/include/SpatialIndex.h"
10 
15 public:
17  id(id), found(false) {
18  }
20  }
21  virtual void visitData(int id, int pos,
22  double x1, double y1, double z1,
23  double x2, double y2, double z2) {
24 
25  Q_UNUSED(pos);
26 
27  if (this->id == id) {
28  double p1[] = { x1, y1, z1 };
29  double p2[] = { x2, y2, z2 };
30 
31  region = SpatialIndex::Region(p1, p2, 3);
32 
33  found = true;
34  }
35  }
36  virtual void visitNode(
37  double /*x1*/, double /*y1*/, double /*z1*/,
38  double /*x2*/, double /*y2*/, double /*z2*/) {}
39 
40 public:
41  int id;
42  SpatialIndex::Region region;
43  bool found;
44 };
45 
46 
47 
57 protected:
64  class RSiRegion: public SpatialIndex::Region {
65  public:
66  RSiRegion(double x1, double y1, double z1, double x2, double y2,
67  double z2);
68  };
69 
76  class RSiPoint: public SpatialIndex::Point {
77  public:
78  RSiPoint(double x, double y, double z);
79  };
80 
88  class Visitor: public SpatialIndex::IVisitor {
89  public:
90  Visitor(QMap<int, QSet<int> >& ids, RSpatialIndexVisitor* dataVisitor = NULL) :
92  }
93 
94  void visitNode(const SpatialIndex::INode& n) {
95  //printf("Visitor::visitNode: %Ld\n", n.getIdentifier());
96  if (dataVisitor != NULL) {
97  SpatialIndex::IShape* shape;
98  n.getShape(&shape);
99  if (shape == NULL) {
100  fprintf(stderr, "error: node has no shape\n");
101  return;
102  }
103  SpatialIndex::Region* region =
104  dynamic_cast<SpatialIndex::Region*> (shape);
105  if (region == NULL) {
106  fprintf(stderr,
107  "error: node shape in spacial index is not a SpatialIndex::Region\n");
108  return;
109  }
110  dataVisitor->visitNode(region->m_pLow[0],
111  region->m_pLow[1], region->m_pLow[2],
112  region->m_pHigh[0], region->m_pHigh[1],
113  region->m_pHigh[2]);
114  }
115  }
116 
117  void visitData(const SpatialIndex::IData& d) {
118  qint64 siid = d.getIdentifier();
119  int id = RSpatialIndex::getId(siid);
120  int pos = RSpatialIndex::getPos(siid);
121 
122  //if (ids.contains(id)) {
123  ids[id].insert(pos);
124  /*
125  qFatal("Visitor.visitData: double entry found in spatial index, identifier: %d",
126  (int)d.getIdentifier());
127  Q_ASSERT(false);
128  */
129 // }
130 // else {
131 // ids.insert(id, QSet<int>() << pos);
132 // }
133 
134  //ids.insert(d.getIdentifier());
135 
136  if (dataVisitor != NULL) {
137  SpatialIndex::IShape* shape;
138  d.getShape(&shape);
139  if (shape == NULL) {
140  fprintf(stderr, "error: data node has no shape\n");
141  return;
142  }
143  SpatialIndex::Region* region =
144  dynamic_cast<SpatialIndex::Region*> (shape);
145  if (region == NULL) {
146  fprintf(stderr,
147  "error: shape in spacial index is not a SpatialIndex::Region\n");
148  return;
149  }
150 
151  //size_t len;
152  //uint8_t* data;
153  //d.getData(len, &data);
154  dataVisitor->visitData(id, pos, region->m_pLow[0],
155  region->m_pLow[1], region->m_pLow[2],
156  region->m_pHigh[0], region->m_pHigh[1],
157  region->m_pHigh[2]);
158  //region->getX1(), region->getY1(), region->getZ1(),
159  //region->getX2(), region->getY2(), region->getZ2(),
160  //len, data);
161  //delete data;
162  //delete region;
163  delete shape;
164  }
165  }
166 
167  void visitData(std::vector<const SpatialIndex::IData*>& v) {
168  std::vector<const SpatialIndex::IData*>::iterator it;
169  for (it = v.begin(); it != v.end(); it++) {
170  printf("Visitor::visitData[]: %Ld\n", (*it)->getIdentifier());
171  }
172  }
173 
174  private:
175  QMap<int, QSet<int> >& ids;
177  };
178 
179 public:
182 
183  //static int dataToInt(const uint8_t* data);
184  //static void intToData(int i, uint8_t* data);
185 
186  virtual void clear();
187 
188  virtual void addToIndex(int id, int pos,
189  double x1, double y1, double z1,
190  double x2, double y2, double z2);
191 
192  virtual void addToIndex(int id, int pos,
193  const RBox& bb);
194 
195  //void removeFromIndex(int id);
196  virtual bool removeFromIndex(int id, const QList<RBox>& bb);
197  virtual bool removeFromIndex(int id, int pos, const RBox& bb);
198  virtual bool removeFromIndex(
199  int id,
200  int pos,
201  double x1, double y1, double z1,
202  double x2, double y2, double z2);
203 
204  virtual QMap<int, QSet<int> > queryIntersected(
205  double x1, double y1, double z1,
206  double x2, double y2, double z2,
207  RSpatialIndexVisitor* dataVisitor = NULL);
208  virtual QMap<int, QSet<int> > queryContained(
209  double x1, double y1, double z1,
210  double x2, double y2, double z2,
211  RSpatialIndexVisitor* dataVisitor = NULL);
212 
213  virtual QMap<int, QSet<int> > queryNearestNeighbor(
214  uint k,
215  double x, double y, double z,
216  RSpatialIndexVisitor* dataVisitor = NULL);
217 
218  virtual QPair<int, int> queryNearestNeighbor(double x, double y, double z);
219 
220 protected:
224  void addToIndex(int id, int pos, const RSiRegion& region, size_t dataLength = 0,
225  const uint8_t* data = NULL);
229  QMap<int, QSet<int> > queryIntersected(const RSiRegion& region,
230  RSpatialIndexVisitor* dataVisitor = NULL);
234  QMap<int, QSet<int> > queryContained(const RSiRegion& region,
235  RSpatialIndexVisitor* dataVisitor = NULL);
239  QMap<int, QSet<int> > queryNearestNeighbor(uint k, const RSiPoint& point,
240  RSpatialIndexVisitor* dataVisitor = NULL);
241 
242 // QSet<int> sidsToIds(const QSet<int> sids);
243 // QSet<int> idToSids(int id);
244 
245 protected:
246  void init();
247  void uninit();
248 
249 protected:
250  SpatialIndex::ISpatialIndex* tree;
251  SpatialIndex::IStorageManager* buff;
252 
253 // QMap<int, int> sidToId;
254 // QMap<int, QList<int> > idToSid;
255 // int sidCounter;
256 };
257 
259 
260 #endif