From 5a96ab4f8d54b8c20b4cea1cadd954d5b7cb60e5 Mon Sep 17 00:00:00 2001 From: cignoni Date: Wed, 2 Dec 2009 15:08:49 +0000 Subject: [PATCH] a few optimization and correction to the hashed grid removal stuff --- vcg/space/index/spatial_hashing.h | 55 ++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/vcg/space/index/spatial_hashing.h b/vcg/space/index/spatial_hashing.h index 4ca9c6cd..63785570 100644 --- a/vcg/space/index/spatial_hashing.h +++ b/vcg/space/index/spatial_hashing.h @@ -150,7 +150,7 @@ protected: { } - ///insert a new cell + bool RemoveObject(ObjType* s, const Point3i &cell) { std::pair CellRange = hash_table.equal_range(cell); @@ -184,31 +184,45 @@ protected: return bb; } + ///Remove all the objects contained in the cell containing s + // it removes s too. + bool RemoveCell(ObjType* s) + { + Point3i pi; + PToIP(s->cP(),pi); + std::pair CellRange = hash_table.equal_range(pi); + hash_table.erase(CellRange.first,CellRange.second); + return true; + } ///insert a new cell -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; + 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; + std::vector toDel; 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; -} + 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++; + toDel.push_back(hi); + } + } + } + for(std::vector::iterator vi=toDel.begin(); vi!=toDel.end();++vi) + hash_table.erase(*vi); + + 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. @@ -259,6 +273,7 @@ int RemoveInSphere(const Point3 &p, const ScalarType radius) voxel[0] = dim[0]/siz[0]; voxel[1] = dim[1]/siz[1]; voxel[2] = dim[2]/siz[2]; + hash_table.clear(); } /// Insert a mesh in the grid.