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