1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
|
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* 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 *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
#ifndef VCGLIB_SPATIAL_HASHING
#define VCGLIB_SPATIAL_HASHING
#include <vcg/space/index/grid_util.h>
#include <vcg/space/index/grid_closest.h>
//#include <map>
#include <vector>
#include <algorithm>
#ifdef _WIN32
#ifndef __MINGW32__
#include <hash_map>
#define STDEXT stdext
#else
#include <ext/hash_map>
#define STDEXT __gnu_cxx
#endif
#else // We are in the *nix gcc branch
#if (__GNUC__ ==4) && (__GNUC_MINOR__ > 3) && (defined(__DEPRECATED))
#undef __DEPRECATED // since gcc 4.4 <ext/hash_map> was deprecated and generate warnings. Relax Deprecation Just for this...
#define ___WE_UNDEFINED_DEPRECATED__
#endif
#include <ext/hash_map>
#define STDEXT __gnu_cxx
#if defined(___WE_UNDEFINED_DEPRECATED__)
#define __DEPRECATED
#endif
#endif
namespace vcg{
// hashing function
struct HashFunctor : public std::unary_function<Point3i, size_t>
{
enum
{ // parameters for hash table
bucket_size = 4, // 0 < bucket_size
min_buckets = 8
};
size_t operator()(const Point3i &p) const
{
const size_t _HASH_P0 = 73856093u;
const size_t _HASH_P1 = 19349663u;
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;
}
bool operator()(const Point3i &s1, const Point3i &s2) const
{ // test if s1 ordered before s2
return (s1 < s2);
}
};
/** Spatial Hash Table
Spatial Hashing as described in
"Optimized Spatial Hashing for Coll ision Detection of Deformable Objects",
Matthias Teschner and Bruno Heidelberger and Matthias Muller and Danat Pomeranets and Markus Gross
*/
template < typename ObjType,class FLT=double>
class SpatialHashTable:public BasicGrid<FLT>, public SpatialIndex<ObjType,FLT>
{
public:
typedef SpatialHashTable SpatialHashType;
typedef ObjType* ObjPtr;
typedef typename ObjType::ScalarType ScalarType;
typedef Point3<ScalarType> CoordType;
typedef typename BasicGrid<FLT>::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.
typedef typename STDEXT::hash_multimap<Point3i, ObjType *, HashFunctor> 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<Point3i> 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.
struct CellIterator
{
CellIterator(){}
HashIterator t;
ObjPtr &operator *(){return (t->second); }
ObjPtr operator *() const {return (t->second); }
bool operator != (const CellIterator & p) const {return t!=p.t;}
void operator ++() {t++;}
};
inline bool Empty() const
{
return hash_table.empty();
}
size_t CellSize(const Point3i &cell)
{
return hash_table.count(cell);
}
inline bool EmptyCell(const Point3i &cell) const
{
return hash_table.find(cell) == hash_table.end();
}
void UpdateAllocatedCells()
{
AllocatedCells.clear();
if(hash_table.empty()) return;
AllocatedCells.push_back(hash_table.begin()->first);
for(HashIterator fi=hash_table.begin();fi!=hash_table.end();++fi)
{
if(AllocatedCells.back()!=fi->first) AllocatedCells.push_back(fi->first);
}
}
protected:
///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));
}
///remove all the objects in a cell
void RemoveCell(const Point3i &/*cell*/)
{
}
bool RemoveObject(ObjType* s, const Point3i &cell)
{
std::pair<HashIterator,HashIterator> CellRange = hash_table.equal_range(cell);
CellIterator first; first.t=CellRange.first;
CellIterator end; end.t=CellRange.second;
for(CellIterator ci = first; ci!=end;++ci)
{
if (*ci == s)
{
hash_table.erase(ci.t);
return true;
}
}
return false;
}
public:
vcg::Box3i Add( ObjType* s)
{
Box3<ScalarType> b;
s->GetBBox(b);
vcg::Box3i 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++)
for (int k=bb.min.Z();k<=bb.max.Z();k++)
InsertObject(s,vcg::Point3i(i,j,k));
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<HashIterator,HashIterator> CellRange = hash_table.equal_range(pi);
hash_table.erase(CellRange.first,CellRange.second);
return true;
} ///insert a new cell
int RemoveInSphere(const Point3<ScalarType> &p, const ScalarType radius)
{
Box3x b(p-Point3f(radius,radius,radius),p+Point3f(radius,radius,radius));
vcg::Box3i bb;
this->BoxToIBox(b,bb);
ScalarType r2=radius*radius;
int cnt=0;
std::vector<HashIterator> 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<HashIterator,HashIterator> 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(typename std::vector<HashIterator>::iterator vi=toDel.begin(); vi!=toDel.end();++vi)
hash_table.erase(*vi);
return cnt;
}
// Specialized version that is able to take in input a
template<class DistanceFunctor>
int RemoveInSphereNormal(const Point3<ScalarType> &p, const Point3<ScalarType> &n, DistanceFunctor &DF, const ScalarType radius)
{
Box3x b(p-Point3f(radius,radius,radius),p+Point3f(radius,radius,radius));
vcg::Box3i bb;
this->BoxToIBox(b,bb);
int cnt=0;
std::vector<HashIterator> 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<HashIterator,HashIterator> CellRange = hash_table.equal_range(Point3i(i,j,k));
for(HashIterator hi = CellRange.first; hi!=CellRange.second;++hi)
{
if(DF(p,n,hi->second->cP(),hi->second->cN()) <= radius)
{
cnt++;
toDel.push_back(hi);
}
}
}
for(typename std::vector<HashIterator>::iterator vi=toDel.begin(); vi!=toDel.end();++vi)
hash_table.erase(*vi);
return cnt;
}
// This 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.
void RemovePunctual( ObjType *s)
{
Point3i pi;
PToIP(s->cP(),pi);
std::pair<HashIterator,HashIterator> CellRange = hash_table.equal_range(pi);
for(HashIterator hi = CellRange.first; hi!=CellRange.second;++hi)
{
if (hi->second == s)
{
hash_table.erase(hi);
return;
}
}
}
void Remove( ObjType* s)
{
Box3<ScalarType> b;
s->GetBBox(b);
vcg::Box3i bb;
BoxToIBox(b,bb);
//then remove the obj from 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++)
for (int k=bb.min.Z();k<=bb.max.Z();k++)
RemoveObject(s,vcg::Point3i(i,j,k));
}
/// set an empty spatial hash table
void InitEmpty(const Box3x &_bbox, vcg::Point3i grid_size)
{
Box3x b;
Box3x &bbox = this->bbox;
CoordType &dim = this->dim;
Point3i &siz = this->siz;
CoordType &voxel = this->voxel;
assert(!_bbox.IsNull());
bbox=_bbox;
dim = bbox.max - bbox.min;
assert((grid_size.V(0)>0)&&(grid_size.V(1)>0)&&(grid_size.V(2)>0));
siz=grid_size;
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.
template <class OBJITER>
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;
int _size=(int)std::distance<OBJITER>(_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;
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));
}
///return the simplexes of the cell that contain p
void GridReal( const Point3<ScalarType> & p, CellIterator & first, CellIterator & last )
{
vcg::Point3i _c;
this->PToIP(p,_c);
Grid(_c,first,last);
}
///return the simplexes on a specified cell
void Grid( int x,int y,int z, CellIterator & first, CellIterator & last )
{
this->Grid(vcg::Point3i(x,y,z),first,last);
}
///return the simplexes on a specified cell
void Grid( const Point3i & _c, CellIterator & first, CellIterator & end )
{
std::pair<HashIterator,HashIterator> CellRange = hash_table.equal_range(_c);
first.t=CellRange.first;
end.t=CellRange.second;
}
void Clear()
{
hash_table.clear();
AllocatedCells.clear();
}
template <class OBJPOINTDISTFUNCTOR, class OBJMARKER>
ObjPtr GetClosest(OBJPOINTDISTFUNCTOR & _getPointDistance, OBJMARKER & _marker,
const CoordType & _p, const ScalarType & _maxDist,ScalarType & _minDist, CoordType & _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>
unsigned int GetKClosest(OBJPOINTDISTFUNCTOR & _getPointDistance,OBJMARKER & _marker,
const unsigned int _k, const CoordType & _p, const ScalarType & _maxDist,OBJPTRCONTAINER & _objectPtrs,
DISTCONTAINER & _distances, POINTCONTAINER & _points)
{
return (vcg::GridGetKClosest<SpatialHashType,
OBJPOINTDISTFUNCTOR,OBJMARKER,OBJPTRCONTAINER,DISTCONTAINER,POINTCONTAINER>
(*this,_getPointDistance,_marker,_k,_p,_maxDist,_objectPtrs,_distances,_points));
}
template <class OBJPOINTDISTFUNCTOR, class OBJMARKER, class OBJPTRCONTAINER, class DISTCONTAINER, class POINTCONTAINER>
unsigned int GetInSphere(OBJPOINTDISTFUNCTOR & _getPointDistance,
OBJMARKER & _marker,
const CoordType & _p,
const ScalarType & _r,
OBJPTRCONTAINER & _objectPtrs,
DISTCONTAINER & _distances,
POINTCONTAINER & _points)
{
return(vcg::GridGetInSphere<SpatialHashType,
OBJPOINTDISTFUNCTOR,OBJMARKER,OBJPTRCONTAINER,DISTCONTAINER,POINTCONTAINER>
(*this,_getPointDistance,_marker,_p,_r,_objectPtrs,_distances,_points));
}
template <class OBJMARKER, class OBJPTRCONTAINER>
unsigned int GetInBox(OBJMARKER & _marker,
const Box3x _bbox,
OBJPTRCONTAINER & _objectPtrs)
{
return(vcg::GridGetInBox<SpatialHashType,OBJMARKER,OBJPTRCONTAINER>
(*this,_marker,_bbox,_objectPtrs));
}
template <class OBJRAYISECTFUNCTOR, class OBJMARKER>
ObjPtr DoRay(OBJRAYISECTFUNCTOR & _rayIntersector, OBJMARKER & _marker, const Ray3<ScalarType> & _ray, const ScalarType & _maxDist, ScalarType & _t)
{
return(vcg::GridDoRay<SpatialHashType,OBJRAYISECTFUNCTOR,OBJMARKER>
(*this,_rayIntersector,_marker,_ray,_maxDist,_t));
}
}; // end class
/** Spatial Hash Table Dynamic
Update the Hmark value on the simplex for dynamic updating of contents of the cell.
The simplex must have the HMark() function.
*/
template < typename ContainerType,class FLT=double>
class DynamicSpatialHashTable: public SpatialHashTable<ContainerType,FLT>
{
public:
typedef typename SpatialHashTable<ContainerType,FLT>::CoordType CoordType;
typedef typename SpatialHashTable<ContainerType,FLT>::ObjType ObjType;
typedef typename SpatialHashTable<ContainerType,FLT>::ObjPtr ObjPtr;
typedef typename SpatialHashTable<ContainerType,FLT>::Box3x Box3x;
typedef typename SpatialHashTable<ContainerType,FLT>::CellIterator CellIterator;
void _UpdateHMark(ObjType* s){ s->HMark() = this->tempMark;}
void getInCellUpdated(vcg::Point3i cell,std::vector<ObjPtr> &elems)
{
CellIterator first,last,l;
Grid(cell,first,last);
for (l=first;l!=last;l++)
{
if ((l->second)>=(**l).HMark())
elems.push_back(&(**l));
}
}
};
}// end namespace
#endif
|