From 017d27dc367346449969966743fc110ca8e764ad Mon Sep 17 00:00:00 2001 From: cignoni Date: Tue, 13 Jan 2009 06:27:01 +0000 Subject: [PATCH] Better commnts and other minor beautifications --- vcg/space/index/spatial_hashing.h | 104 ++++++++++++++---------------- 1 file changed, 50 insertions(+), 54 deletions(-) diff --git a/vcg/space/index/spatial_hashing.h b/vcg/space/index/spatial_hashing.h index b2802a56..8b1e7a37 100644 --- a/vcg/space/index/spatial_hashing.h +++ b/vcg/space/index/spatial_hashing.h @@ -24,11 +24,6 @@ #ifndef VCGLIB_SPATIAL_HASHING #define VCGLIB_SPATIAL_HASHING - -#define HASH_P0 73856093u -#define HASH_P1 19349663u -#define HASH_P2 83492791u - #include #include //#include @@ -50,14 +45,20 @@ namespace vcg{ - // hashing function - struct HashFunctor : public unary_function - { - size_t operator()(const Point3i &p) const - { - return size_t(p.V(0))*HASH_P0 ^ size_t(p.V(1))*HASH_P1 ^ size_t(p.V(2))*HASH_P2; - } - }; + + + // hashing function + struct HashFunctor : public unary_function + { + size_t operator()(const Point3i &p) const + { + static const size_t HASH_P0=73856093u; + static const size_t HASH_P1=19349663u; + static const size_t HASH_P2=83492791u; + + return size_t(p.V(0))*HASH_P0 ^ size_t(p.V(1))*HASH_P1 ^ size_t(p.V(2))*HASH_P2; + } + }; /** Spatial Hash Table @@ -70,20 +71,23 @@ namespace vcg{ { public: - typedef SpatialHashTable SpatialHashType; - typedef ObjType* ObjPtr; - typedef typename ObjType::ScalarType ScalarType; - typedef Point3 CoordType; - typedef typename BasicGrid::Box3x Box3x; + typedef SpatialHashTable SpatialHashType; + typedef ObjType* ObjPtr; + typedef typename ObjType::ScalarType ScalarType; + typedef Point3 CoordType; + typedef typename BasicGrid::Box3x Box3x; - // Hash table definition - // the hash index directly the grid structure. - // We use a MultiMap because we need to store many object (faces) inside each cell of the grid. + // Hash table definition + // the hash index directly the grid structure. + // We use a MultiMap because we need to store many object (faces) inside each cell of the grid. - typedef typename STDEXT::hash_multimap HashType; - typedef typename HashType::iterator HashIterator; - HashType hash_table; - std::vector AllocatedCells; + typedef typename STDEXT::hash_multimap HashType; + typedef typename HashType::iterator HashIterator; + HashType hash_table; // The real HASH TABLE ************************************** + + // This vector is just a handy reference to all the allocated cells, + // becouse hashed multimaps does not expose a direct list of all the different keys. + std::vector AllocatedCells; // Class to abstract a HashIterator (that stores also the key, // while the interface of the generic spatial indexing need only simple object (face) pointers. @@ -93,18 +97,18 @@ namespace vcg{ CellIterator(){} HashIterator t; ObjPtr &operator *(){return t->second;} - bool operator != (const CellIterator & p) {return t!=p.t;} + bool operator != (const CellIterator & p) const {return t!=p.t;} void operator ++() {t++;} }; protected: - ///insert a new cell - void InsertObject(ObjType* s,Point3i cell) - { - if(hash_table.count(cell)==0) AllocatedCells.push_back(cell); - hash_table.insert(typename HashType::value_type(cell, s)); - } + ///insert a new cell + void InsertObject(ObjType* s, const Point3i &cell) + { + if(hash_table.count(cell)==0) AllocatedCells.push_back(cell); + hash_table.insert(typename HashType::value_type(cell, s)); + } public: @@ -147,15 +151,14 @@ namespace vcg{ /// Insert a mesh in the grid. template - void Set(const OBJITER & _oBegin, const OBJITER & _oEnd,const Box3x &_bbox=Box3x() ) + void Set(const OBJITER & _oBegin, const OBJITER & _oEnd, const Box3x &_bbox=Box3x() ) { - OBJITER i; Box3x b; - Box3x &bbox = this->bbox; - CoordType &dim = this->dim; - Point3i &siz = this->siz; - CoordType &voxel = this->voxel; + Box3x &bbox = this->bbox; + CoordType &dim = this->dim; + Point3i &siz = this->siz; + CoordType &voxel = this->voxel; int _size=(int)std::distance(_oBegin,_oEnd); if(!_bbox.IsNull()) this->bbox=_bbox; @@ -167,20 +170,18 @@ namespace vcg{ this->bbox.Add(b); } ///inflate the bb calculated - ScalarType infl=bbox.Diag()/_size; - bbox.min -= CoordType(infl,infl,infl); - bbox.max += CoordType(infl,infl,infl); + bbox.Offset(bbox.Diag()/100.0) ; } - dim = bbox.max - bbox.min; - BestDim( _size, dim, siz ); - // find voxel size - voxel[0] = dim[0]/siz[0]; - voxel[1] = dim[1]/siz[1]; - voxel[2] = dim[2]/siz[2]; + dim = bbox.max - bbox.min; + BestDim( _size, dim, siz ); + // find voxel size + voxel[0] = dim[0]/siz[0]; + voxel[1] = dim[1]/siz[1]; + voxel[2] = dim[2]/siz[2]; - for(i = _oBegin; i!= _oEnd; ++i) - Add(&(*i)); + for(i = _oBegin; i!= _oEnd; ++i) + Add(&(*i)); } @@ -206,11 +207,6 @@ namespace vcg{ end.t=CellRange.second; } - - ///return the number of cell created - int CellNumber() - {return (hash_table.size());} - void Clear() { hash_table.clear();