Better commnts and other minor beautifications

This commit is contained in:
Paolo Cignoni 2009-01-13 06:27:01 +00:00
parent a546c88a7c
commit 017d27dc36
1 changed files with 50 additions and 54 deletions

View File

@ -24,11 +24,6 @@
#ifndef VCGLIB_SPATIAL_HASHING #ifndef VCGLIB_SPATIAL_HASHING
#define VCGLIB_SPATIAL_HASHING #define VCGLIB_SPATIAL_HASHING
#define HASH_P0 73856093u
#define HASH_P1 19349663u
#define HASH_P2 83492791u
#include <vcg/space/index/grid_util.h> #include <vcg/space/index/grid_util.h>
#include <vcg/space/index/grid_closest.h> #include <vcg/space/index/grid_closest.h>
//#include <map> //#include <map>
@ -50,14 +45,20 @@
namespace vcg{ namespace vcg{
// hashing function
struct HashFunctor : public unary_function<Point3i, size_t>
{ // hashing function
size_t operator()(const Point3i &p) const struct HashFunctor : public unary_function<Point3i, size_t>
{ {
return size_t(p.V(0))*HASH_P0 ^ size_t(p.V(1))*HASH_P1 ^ size_t(p.V(2))*HASH_P2; 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 /** Spatial Hash Table
@ -70,20 +71,23 @@ namespace vcg{
{ {
public: public:
typedef SpatialHashTable SpatialHashType; typedef SpatialHashTable SpatialHashType;
typedef ObjType* ObjPtr; typedef ObjType* ObjPtr;
typedef typename ObjType::ScalarType ScalarType; typedef typename ObjType::ScalarType ScalarType;
typedef Point3<ScalarType> CoordType; typedef Point3<ScalarType> CoordType;
typedef typename BasicGrid<FLT>::Box3x Box3x; typedef typename BasicGrid<FLT>::Box3x Box3x;
// Hash table definition // Hash table definition
// the hash index directly the grid structure. // 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. // We use a MultiMap because we need to store many object (faces) inside each cell of the grid.
typedef typename STDEXT::hash_multimap<Point3i, ObjType *, HashFunctor> HashType; typedef typename STDEXT::hash_multimap<Point3i, ObjType *, HashFunctor> HashType;
typedef typename HashType::iterator HashIterator; typedef typename HashType::iterator HashIterator;
HashType hash_table; HashType hash_table; // The real HASH TABLE **************************************
std::vector<Point3i> AllocatedCells;
// 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<Point3i> AllocatedCells;
// Class to abstract a HashIterator (that stores also the key, // Class to abstract a HashIterator (that stores also the key,
// while the interface of the generic spatial indexing need only simple object (face) pointers. // while the interface of the generic spatial indexing need only simple object (face) pointers.
@ -93,18 +97,18 @@ namespace vcg{
CellIterator(){} CellIterator(){}
HashIterator t; HashIterator t;
ObjPtr &operator *(){return t->second;} 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++;} void operator ++() {t++;}
}; };
protected: protected:
///insert a new cell ///insert a new cell
void InsertObject(ObjType* s,Point3i 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)); hash_table.insert(typename HashType::value_type(cell, s));
} }
public: public:
@ -147,15 +151,14 @@ namespace vcg{
/// Insert a mesh in the grid. /// Insert a mesh in the grid.
template <class OBJITER> template <class OBJITER>
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; OBJITER i;
Box3x b; Box3x b;
Box3x &bbox = this->bbox; Box3x &bbox = this->bbox;
CoordType &dim = this->dim; CoordType &dim = this->dim;
Point3i &siz = this->siz; Point3i &siz = this->siz;
CoordType &voxel = this->voxel; CoordType &voxel = this->voxel;
int _size=(int)std::distance<OBJITER>(_oBegin,_oEnd); int _size=(int)std::distance<OBJITER>(_oBegin,_oEnd);
if(!_bbox.IsNull()) this->bbox=_bbox; if(!_bbox.IsNull()) this->bbox=_bbox;
@ -167,20 +170,18 @@ namespace vcg{
this->bbox.Add(b); this->bbox.Add(b);
} }
///inflate the bb calculated ///inflate the bb calculated
ScalarType infl=bbox.Diag()/_size; bbox.Offset(bbox.Diag()/100.0) ;
bbox.min -= CoordType(infl,infl,infl);
bbox.max += CoordType(infl,infl,infl);
} }
dim = bbox.max - bbox.min; dim = bbox.max - bbox.min;
BestDim( _size, dim, siz ); BestDim( _size, dim, siz );
// find voxel size // find voxel size
voxel[0] = dim[0]/siz[0]; voxel[0] = dim[0]/siz[0];
voxel[1] = dim[1]/siz[1]; voxel[1] = dim[1]/siz[1];
voxel[2] = dim[2]/siz[2]; voxel[2] = dim[2]/siz[2];
for(i = _oBegin; i!= _oEnd; ++i) for(i = _oBegin; i!= _oEnd; ++i)
Add(&(*i)); Add(&(*i));
} }
@ -206,11 +207,6 @@ namespace vcg{
end.t=CellRange.second; end.t=CellRange.second;
} }
///return the number of cell created
int CellNumber()
{return (hash_table.size());}
void Clear() void Clear()
{ {
hash_table.clear(); hash_table.clear();