diff --git a/vcg/space/index/index2D/spatial_hashing_2D.h b/vcg/space/index/index2D/spatial_hashing_2D.h index b054b566..e6091bde 100644 --- a/vcg/space/index/index2D/spatial_hashing_2D.h +++ b/vcg/space/index/index2D/spatial_hashing_2D.h @@ -109,6 +109,9 @@ namespace vcg{ // becouse hashed multimaps does not expose a direct list of all the different keys. std::vector AllocatedCells; + ///the size of the diagonal of each cell + ScalarType cell_size; + // Class to abstract a HashIterator (that stores also the key, // while the interface of the generic spatial indexing need only simple object (face) pointers. @@ -177,22 +180,57 @@ namespace vcg{ return false; } + void AddBox(ObjType* s, + const Box2 &b) + { + vcg::Box2i bb; + this->BoxToIBox(b,bb); + //then insert all the cell of bb + for (int i=bb.min.X();i<=bb.max.X();i++) + for (int j=bb.min.Y();j<=bb.max.Y();j++) + InsertObject(s,vcg::Point2i(i,j)); + } + + void AddBoxes(ObjType* s, + const std::vector > &boxes) + { + std::vector Indexes; + for (unsigned int i=0;iBoxToIBox(boxes[i],bb); + //then insert all the cell of bb + for (int i=bb.min.X();i<=bb.max.X();i++) + for (int j=bb.min.Y();j<=bb.max.Y();j++) + Indexes.push_back(vcg::Point2i(i,j)); + } + std::sort(Indexes.begin(),Indexes.end()); + std::vector::iterator it=std::unique(Indexes.begin(),Indexes.end()); + Indexes.resize( it - Indexes.begin() ); + + for (int i=0;i b; - s->GetBBox(b); - vcg::Box2i bb; - this->BoxToIBox(b,bb); - //then insert all the cell of bb - for (int i=bb.min.X();i<=bb.max.X();i++) - for (int j=bb.min.Y();j<=bb.max.Y();j++) - InsertObject(s,vcg::Point2i(i,j)); - //for (int k=bb.min.Z();k<=bb.max.Z();k++) - //InsertObject(s,vcg::Point3i(i,j,k)); - return bb; + if (!subdivideBox) + { + Box2 b; + s->GetBBox(b); + AddBox(s,b); + } + else + { + std::vector > Boxes; + s->GetSubBBox(cell_size,Boxes); + //for (unsigned int i=0;i - void Set(const OBJITER & _oBegin, const OBJITER & _oEnd, const Box2x &_bbox=Box2x() ) + /*template + void Set(const OBJITER & _oBegin, const OBJITER & _oEnd, const Box2x &_bbox=Box2x() ) { OBJITER i; Box2x b; @@ -349,9 +388,47 @@ namespace vcg{ for(i = _oBegin; i!= _oEnd; ++i) Add(&(*i)); - } + }*/ + /// Insert a mesh in the grid. + template + void Set(const OBJITER & _oBegin, const OBJITER & _oEnd, + bool subdivideBox=false,const Box2x &_bbox=Box2x() ) + { + OBJITER i; + Box2x b; + Box2x &bbox = this->bbox; + CoordType &dim = this->dim; + Point2i &siz = this->siz; + CoordType &voxel = this->voxel; + + int _size=(int)std::distance(_oBegin,_oEnd); + if(!_bbox.IsNull()) this->bbox=_bbox; + else + { + for(i = _oBegin; i!= _oEnd; ++i) + { + (*i).GetBBox(b); + this->bbox.Add(b); + } + ///inflate the bb calculated + bbox.Offset(bbox.Diag()/100.0) ; + } + + dim = bbox.max - bbox.min; + BestDim2D( _size, dim, siz ); + // find voxel size + voxel[0] = dim[0]/siz[0]; + voxel[1] = dim[1]/siz[1]; + cell_size=voxel.Norm(); + + for(i = _oBegin; i!= _oEnd; ++i) + { + Add(&(*i),subdivideBox); + } + } + ///return the simplexes of the cell that contain p void GridReal( const Point2 & p, CellIterator & first, CellIterator & last ) { @@ -418,10 +495,20 @@ namespace vcg{ const Box2x _bbox, OBJPTRCONTAINER & _objectPtrs) { + _objectPtrs.clear(); return(vcg::GridGetInBox2D (*this,_marker,_bbox,_objectPtrs)); } + template + unsigned int GetInBoxes(OBJMARKER & _marker, + const std::vector &_bbox, + OBJPTRCONTAINER &_objectPtrs) + { + _objectPtrs.clear(); + return(vcg::GridGetInBoxes2D + (*this,_marker,_bbox,_objectPtrs)); + } }; // end class