From 27c319a79c1dfb87c381abd71ce6be320a2aac2a Mon Sep 17 00:00:00 2001 From: cignoni Date: Mon, 30 Nov 2009 10:36:49 +0000 Subject: [PATCH] added a removeInSphere specialization and a removePunctual specialization. --- vcg/space/index/spatial_hashing.h | 62 ++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/vcg/space/index/spatial_hashing.h b/vcg/space/index/spatial_hashing.h index b7c30e7d..4ca9c6cd 100644 --- a/vcg/space/index/spatial_hashing.h +++ b/vcg/space/index/spatial_hashing.h @@ -120,12 +120,28 @@ namespace vcg{ { return hash_table.count(cell); } + + inline bool EmptyCell(const Point3i &cell) const + { + return hash_table.find(cell) == hash_table.end(); + } + + void UpdateAllocatedCells() + { + AllocatedCells.clear(); + if(hash_table.empty()) return; + AllocatedCells.push_back(hash_table.begin()->first); + for(HashIterator fi=hash_table.begin();fi!=hash_table.end();++fi) + { + if(AllocatedCells.back()!=fi->first) AllocatedCells.push_back(fi->first); + } + } protected: ///insert a new cell void InsertObject(ObjType* s, const Point3i &cell) { - if(hash_table.count(cell)==0) AllocatedCells.push_back(cell); + //if(hash_table.count(cell)==0) AllocatedCells.push_back(cell); hash_table.insert(typename HashType::value_type(cell, s)); } @@ -168,6 +184,50 @@ protected: return bb; } + +int RemoveInSphere(const Point3 &p, const ScalarType radius) +{ + Box3x b(p-Point3f(radius,radius,radius),p+Point3f(radius,radius,radius)); + vcg::Box3i bb; + BoxToIBox(b,bb); + ScalarType r2=radius*radius; + int cnt=0; + + for (int i=bb.min.X();i<=bb.max.X();i++) + for (int j=bb.min.Y();j<=bb.max.Y();j++) + for (int k=bb.min.Z();k<=bb.max.Z();k++) + { + std::pair CellRange = hash_table.equal_range(Point3i(i,j,k)); + for(HashIterator hi = CellRange.first; hi!=CellRange.second;++hi) + { + if(SquaredDistance(p,hi->second->cP()) <= r2) + { + cnt++; + hash_table.erase(hi); + } + } + } + return cnt; +} + + // Thsi version of the removal is specialized for the case where + // an object has a pointshaped box and using the generic bbox interface is just a waste of time. + + void RemovePunctual( ObjType *s) + { + Point3i pi; + PToIP(s->cP(),pi); + std::pair CellRange = hash_table.equal_range(pi); + for(HashIterator hi = CellRange.first; hi!=CellRange.second;++hi) + { + if (hi->second == s) + { + hash_table.erase(hi); + return; + } + } + } + void Remove( ObjType* s) { Box3 b;