added keyword "public" for deriving SpatialHashTable from SpatialIndex

This commit is contained in:
ganovelli 2009-01-16 17:01:17 +00:00
parent 4b1be10671
commit af76e5fde8
1 changed files with 309 additions and 309 deletions

View File

@ -1,316 +1,316 @@
/**************************************************************************** /****************************************************************************
* VCGLib o o * * VCGLib o o *
* Visual and Computer Graphics Library o o * * Visual and Computer Graphics Library o o *
* _ O _ * * _ O _ *
* Copyright(C) 2004 \/)\/ * * Copyright(C) 2004 \/)\/ *
* Visual Computing Lab /\/| * * Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | * * ISTI - Italian National Research Council | *
* \ * * \ *
* All rights reserved. * * All rights reserved. *
* * * *
* This program is free software; you can redistribute it and/or modify * * This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by * * it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or * * the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. * * (at your option) any later version. *
* * * *
* This program is distributed in the hope that it will be useful, * * This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of * * but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. * * for more details. *
* * * *
****************************************************************************/ ****************************************************************************/
#ifndef VCGLIB_SPATIAL_HASHING #ifndef VCGLIB_SPATIAL_HASHING
#define VCGLIB_SPATIAL_HASHING #define VCGLIB_SPATIAL_HASHING
#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>
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
#ifdef _WIN32 #ifdef _WIN32
#ifndef __MINGW32__ #ifndef __MINGW32__
#include <hash_map> #include <hash_map>
#define STDEXT stdext #define STDEXT stdext
#else #else
#include <ext/hash_map> #include <ext/hash_map>
#define STDEXT __gnu_cxx #define STDEXT __gnu_cxx
#endif #endif
#else #else
#include <ext/hash_map> #include <ext/hash_map>
#define STDEXT __gnu_cxx #define STDEXT __gnu_cxx
#endif #endif
namespace vcg{ namespace vcg{
// hashing function // hashing function
struct HashFunctor : public std::unary_function<Point3i, size_t> struct HashFunctor : public std::unary_function<Point3i, size_t>
{ {
enum enum
{ // parameters for hash table { // parameters for hash table
bucket_size = 4, // 0 < bucket_size bucket_size = 4, // 0 < bucket_size
min_buckets = 8 min_buckets = 8
}; };
size_t operator()(const Point3i &p) const size_t operator()(const Point3i &p) const
{ {
const size_t _HASH_P0 = 73856093u; const size_t _HASH_P0 = 73856093u;
const size_t _HASH_P1 = 19349663u; const size_t _HASH_P1 = 19349663u;
const size_t _HASH_P2 = 83492791u; 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; return size_t(p.V(0))*_HASH_P0 ^ size_t(p.V(1))*_HASH_P1 ^ size_t(p.V(2))*_HASH_P2;
} }
bool operator()(const Point3i &s1, const Point3i &s2) const bool operator()(const Point3i &s1, const Point3i &s2) const
{ // test if s1 ordered before s2 { // test if s1 ordered before s2
return (s1 < s2); return (s1 < s2);
} }
}; };
/** Spatial Hash Table /** Spatial Hash Table
Spatial Hashing as described in Spatial Hashing as described in
"Optimized Spatial Hashing for Coll ision Detection of Deformable Objects", "Optimized Spatial Hashing for Coll ision Detection of Deformable Objects",
Matthias Teschner and Bruno Heidelberger and Matthias Muller and Danat Pomeranets and Markus Gross Matthias Teschner and Bruno Heidelberger and Matthias Muller and Danat Pomeranets and Markus Gross
*/ */
template < typename ObjType,class FLT=double> template < typename ObjType,class FLT=double>
class SpatialHashTable:public BasicGrid<FLT>, SpatialIndex<ObjType,FLT> class SpatialHashTable:public BasicGrid<FLT>, public SpatialIndex<ObjType,FLT>
{ {
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; // The real HASH TABLE ************************************** HashType hash_table; // The real HASH TABLE **************************************
// This vector is just a handy reference to all the allocated cells, // 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. // becouse hashed multimaps does not expose a direct list of all the different keys.
std::vector<Point3i> AllocatedCells; 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.
struct CellIterator struct CellIterator
{ {
CellIterator(){} CellIterator(){}
HashIterator t; HashIterator t;
ObjPtr &operator *(){return t->second;} ObjPtr &operator *(){return t->second;}
bool operator != (const CellIterator & p) const {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, const 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:
vcg::Box3i Add( ObjType* s) vcg::Box3i Add( ObjType* s)
{ {
Box3<ScalarType> b; Box3<ScalarType> b;
s->GetBBox(b); s->GetBBox(b);
vcg::Box3i bb; vcg::Box3i bb;
BoxToIBox(b,bb); BoxToIBox(b,bb);
//then insert all the cell of bb //then insert all the cell of bb
for (int i=bb.min.X();i<=bb.max.X();i++) for (int i=bb.min.X();i<=bb.max.X();i++)
for (int j=bb.min.Y();j<=bb.max.Y();j++) for (int j=bb.min.Y();j<=bb.max.Y();j++)
for (int k=bb.min.Z();k<=bb.max.Z();k++) for (int k=bb.min.Z();k<=bb.max.Z();k++)
InsertObject(s,vcg::Point3i(i,j,k)); InsertObject(s,vcg::Point3i(i,j,k));
return bb; return bb;
} }
/// set an empty spatial hash table /// set an empty spatial hash table
void InitEmpty(const Box3x &_bbox, vcg::Point3i grid_size) void InitEmpty(const Box3x &_bbox, vcg::Point3i grid_size)
{ {
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;
assert(!_bbox.IsNull()); assert(!_bbox.IsNull());
bbox=_bbox; bbox=_bbox;
dim = bbox.max - bbox.min; dim = bbox.max - bbox.min;
assert((grid_size.V(0)>0)&&(grid_size.V(1)>0)&&(grid_size.V(2)>0)); assert((grid_size.V(0)>0)&&(grid_size.V(1)>0)&&(grid_size.V(2)>0));
siz=grid_size; siz=grid_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];
} }
/// 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;
else else
{ {
for(i = _oBegin; i!= _oEnd; ++i) for(i = _oBegin; i!= _oEnd; ++i)
{ {
(*i).GetBBox(b); (*i).GetBBox(b);
this->bbox.Add(b); this->bbox.Add(b);
} }
///inflate the bb calculated ///inflate the bb calculated
bbox.Offset(bbox.Diag()/100.0) ; bbox.Offset(bbox.Diag()/100.0) ;
} }
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));
} }
///return the simplexes of the cell that contain p ///return the simplexes of the cell that contain p
void Grid( const Point3<ScalarType> & p, CellIterator & first, CellIterator & last ) void Grid( const Point3<ScalarType> & p, CellIterator & first, CellIterator & last )
{ {
vcg::Point3i _c; vcg::Point3i _c;
this->PToIP(p,_c); this->PToIP(p,_c);
Grid(_c,first,last); Grid(_c,first,last);
} }
///return the simplexes on a specified cell ///return the simplexes on a specified cell
void Grid( int x,int y,int z, CellIterator & first, CellIterator & last ) void Grid( int x,int y,int z, CellIterator & first, CellIterator & last )
{ {
Grid(vcg::Point3i(x,y,z),first,last); Grid(vcg::Point3i(x,y,z),first,last);
} }
///return the simplexes on a specified cell ///return the simplexes on a specified cell
void Grid( const Point3i & _c, CellIterator & first, CellIterator & end ) void Grid( const Point3i & _c, CellIterator & first, CellIterator & end )
{ {
std::pair<HashIterator,HashIterator> CellRange = hash_table.equal_range(_c); std::pair<HashIterator,HashIterator> CellRange = hash_table.equal_range(_c);
first.t=CellRange.first; first.t=CellRange.first;
end.t=CellRange.second; end.t=CellRange.second;
} }
void Clear() void Clear()
{ {
hash_table.clear(); hash_table.clear();
AllocatedCells.clear(); AllocatedCells.clear();
} }
template <class OBJPOINTDISTFUNCTOR, class OBJMARKER> template <class OBJPOINTDISTFUNCTOR, class OBJMARKER>
ObjPtr GetClosest(OBJPOINTDISTFUNCTOR & _getPointDistance, OBJMARKER & _marker, ObjPtr GetClosest(OBJPOINTDISTFUNCTOR & _getPointDistance, OBJMARKER & _marker,
const CoordType & _p, const ScalarType & _maxDist,ScalarType & _minDist, CoordType & _closestPt) const CoordType & _p, const ScalarType & _maxDist,ScalarType & _minDist, CoordType & _closestPt)
{ {
return (vcg::GridClosest<SpatialHashType,OBJPOINTDISTFUNCTOR,OBJMARKER>(*this,_getPointDistance,_marker, _p,_maxDist,_minDist,_closestPt)); return (vcg::GridClosest<SpatialHashType,OBJPOINTDISTFUNCTOR,OBJMARKER>(*this,_getPointDistance,_marker, _p,_maxDist,_minDist,_closestPt));
} }
template <class OBJPOINTDISTFUNCTOR, class OBJMARKER, class OBJPTRCONTAINER,class DISTCONTAINER, class POINTCONTAINER> template <class OBJPOINTDISTFUNCTOR, class OBJMARKER, class OBJPTRCONTAINER,class DISTCONTAINER, class POINTCONTAINER>
unsigned int GetKClosest(OBJPOINTDISTFUNCTOR & _getPointDistance,OBJMARKER & _marker, unsigned int GetKClosest(OBJPOINTDISTFUNCTOR & _getPointDistance,OBJMARKER & _marker,
const unsigned int _k, const CoordType & _p, const ScalarType & _maxDist,OBJPTRCONTAINER & _objectPtrs, const unsigned int _k, const CoordType & _p, const ScalarType & _maxDist,OBJPTRCONTAINER & _objectPtrs,
DISTCONTAINER & _distances, POINTCONTAINER & _points) DISTCONTAINER & _distances, POINTCONTAINER & _points)
{ {
return (vcg::GridGetKClosest<SpatialHashType, return (vcg::GridGetKClosest<SpatialHashType,
OBJPOINTDISTFUNCTOR,OBJMARKER,OBJPTRCONTAINER,DISTCONTAINER,POINTCONTAINER> OBJPOINTDISTFUNCTOR,OBJMARKER,OBJPTRCONTAINER,DISTCONTAINER,POINTCONTAINER>
(*this,_getPointDistance,_marker,_k,_p,_maxDist,_objectPtrs,_distances,_points)); (*this,_getPointDistance,_marker,_k,_p,_maxDist,_objectPtrs,_distances,_points));
} }
template <class OBJPOINTDISTFUNCTOR, class OBJMARKER, class OBJPTRCONTAINER, class DISTCONTAINER, class POINTCONTAINER> template <class OBJPOINTDISTFUNCTOR, class OBJMARKER, class OBJPTRCONTAINER, class DISTCONTAINER, class POINTCONTAINER>
unsigned int GetInSphere(OBJPOINTDISTFUNCTOR & _getPointDistance, unsigned int GetInSphere(OBJPOINTDISTFUNCTOR & _getPointDistance,
OBJMARKER & _marker, OBJMARKER & _marker,
const CoordType & _p, const CoordType & _p,
const ScalarType & _r, const ScalarType & _r,
OBJPTRCONTAINER & _objectPtrs, OBJPTRCONTAINER & _objectPtrs,
DISTCONTAINER & _distances, DISTCONTAINER & _distances,
POINTCONTAINER & _points) POINTCONTAINER & _points)
{ {
return(vcg::GridGetInSphere<SpatialHashType, return(vcg::GridGetInSphere<SpatialHashType,
OBJPOINTDISTFUNCTOR,OBJMARKER,OBJPTRCONTAINER,DISTCONTAINER,POINTCONTAINER> OBJPOINTDISTFUNCTOR,OBJMARKER,OBJPTRCONTAINER,DISTCONTAINER,POINTCONTAINER>
(*this,_getPointDistance,_marker,_p,_r,_objectPtrs,_distances,_points)); (*this,_getPointDistance,_marker,_p,_r,_objectPtrs,_distances,_points));
} }
template <class OBJMARKER, class OBJPTRCONTAINER> template <class OBJMARKER, class OBJPTRCONTAINER>
unsigned int GetInBox(OBJMARKER & _marker, unsigned int GetInBox(OBJMARKER & _marker,
const Box3x _bbox, const Box3x _bbox,
OBJPTRCONTAINER & _objectPtrs) OBJPTRCONTAINER & _objectPtrs)
{ {
return(vcg::GridGetInBox<SpatialHashType,OBJMARKER,OBJPTRCONTAINER> return(vcg::GridGetInBox<SpatialHashType,OBJMARKER,OBJPTRCONTAINER>
(*this,_marker,_bbox,_objectPtrs)); (*this,_marker,_bbox,_objectPtrs));
} }
template <class OBJRAYISECTFUNCTOR, class OBJMARKER> template <class OBJRAYISECTFUNCTOR, class OBJMARKER>
ObjPtr DoRay(OBJRAYISECTFUNCTOR & _rayIntersector, OBJMARKER & _marker, const Ray3<ScalarType> & _ray, const ScalarType & _maxDist, ScalarType & _t) ObjPtr DoRay(OBJRAYISECTFUNCTOR & _rayIntersector, OBJMARKER & _marker, const Ray3<ScalarType> & _ray, const ScalarType & _maxDist, ScalarType & _t)
{ {
return(vcg::GridDoRay<SpatialHashType,OBJRAYISECTFUNCTOR,OBJMARKER> return(vcg::GridDoRay<SpatialHashType,OBJRAYISECTFUNCTOR,OBJMARKER>
(*this,_rayIntersector,_marker,_ray,_maxDist,_t)); (*this,_rayIntersector,_marker,_ray,_maxDist,_t));
} }
}; // end class }; // end class
/** Spatial Hash Table Dynamic /** Spatial Hash Table Dynamic
Update the Hmark value on the simplex for dynamic updating of contents of the cell. Update the Hmark value on the simplex for dynamic updating of contents of the cell.
The simplex must have the HMark() function. The simplex must have the HMark() function.
*/ */
template < typename ContainerType,class FLT=double> template < typename ContainerType,class FLT=double>
class DynamicSpatialHashTable: public SpatialHashTable<ContainerType,FLT> class DynamicSpatialHashTable: public SpatialHashTable<ContainerType,FLT>
{ {
public: public:
typedef typename SpatialHashTable<ContainerType,FLT>::CoordType CoordType; typedef typename SpatialHashTable<ContainerType,FLT>::CoordType CoordType;
typedef typename SpatialHashTable<ContainerType,FLT>::ObjType ObjType; typedef typename SpatialHashTable<ContainerType,FLT>::ObjType ObjType;
typedef typename SpatialHashTable<ContainerType,FLT>::ObjPtr ObjPtr; typedef typename SpatialHashTable<ContainerType,FLT>::ObjPtr ObjPtr;
typedef typename SpatialHashTable<ContainerType,FLT>::Box3x Box3x; typedef typename SpatialHashTable<ContainerType,FLT>::Box3x Box3x;
typedef typename SpatialHashTable<ContainerType,FLT>::CellIterator CellIterator; typedef typename SpatialHashTable<ContainerType,FLT>::CellIterator CellIterator;
void _UpdateHMark(ObjType* s){ s->HMark() = this->tempMark;} void _UpdateHMark(ObjType* s){ s->HMark() = this->tempMark;}
void getInCellUpdated(vcg::Point3i cell,std::vector<ObjPtr> &elems) void getInCellUpdated(vcg::Point3i cell,std::vector<ObjPtr> &elems)
{ {
CellIterator first,last,l; CellIterator first,last,l;
Grid(cell,first,last); Grid(cell,first,last);
for (l=first;l!=last;l++) for (l=first;l!=last;l++)
{ {
if ((l->second)>=(**l).HMark()) if ((l->second)>=(**l).HMark())
elems.push_back(&(**l)); elems.push_back(&(**l));
} }
} }
}; };
}// end namespace }// end namespace
#endif #endif