PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/misc/nistmeshlab/vcglib/vcg/space/index/spatial_hashing.h

https://github.com/lessandro/nbis
C Header | 430 lines | 305 code | 69 blank | 56 comment | 28 complexity | 96ba6fa0bd95b7f8b8386fa0a42fcc3f MD5 | raw file
  1. /****************************************************************************
  2. * VCGLib o o *
  3. * Visual and Computer Graphics Library o o *
  4. * _ O _ *
  5. * Copyright(C) 2004 \/)\/ *
  6. * Visual Computing Lab /\/| *
  7. * ISTI - Italian National Research Council | *
  8. * \ *
  9. * All rights reserved. *
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. * This program is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  19. * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
  20. * for more details. *
  21. * *
  22. ****************************************************************************/
  23. #ifndef VCGLIB_SPATIAL_HASHING
  24. #define VCGLIB_SPATIAL_HASHING
  25. #include <vcg/space/index/grid_util.h>
  26. #include <vcg/space/index/grid_closest.h>
  27. //#include <map>
  28. #include <vector>
  29. #include <algorithm>
  30. #ifdef _WIN32
  31. #ifndef __MINGW32__
  32. #include <hash_map>
  33. #define STDEXT stdext
  34. #else
  35. #include <ext/hash_map>
  36. #define STDEXT __gnu_cxx
  37. #endif
  38. #else
  39. #include <ext/hash_map>
  40. #define STDEXT __gnu_cxx
  41. #endif
  42. namespace vcg{
  43. // hashing function
  44. struct HashFunctor : public std::unary_function<Point3i, size_t>
  45. {
  46. enum
  47. { // parameters for hash table
  48. bucket_size = 4, // 0 < bucket_size
  49. min_buckets = 8
  50. };
  51. size_t operator()(const Point3i &p) const
  52. {
  53. const size_t _HASH_P0 = 73856093u;
  54. const size_t _HASH_P1 = 19349663u;
  55. const size_t _HASH_P2 = 83492791u;
  56. return size_t(p.V(0))*_HASH_P0 ^ size_t(p.V(1))*_HASH_P1 ^ size_t(p.V(2))*_HASH_P2;
  57. }
  58. bool operator()(const Point3i &s1, const Point3i &s2) const
  59. { // test if s1 ordered before s2
  60. return (s1 < s2);
  61. }
  62. };
  63. /** Spatial Hash Table
  64. Spatial Hashing as described in
  65. "Optimized Spatial Hashing for Coll ision Detection of Deformable Objects",
  66. Matthias Teschner and Bruno Heidelberger and Matthias Muller and Danat Pomeranets and Markus Gross
  67. */
  68. template < typename ObjType,class FLT=double>
  69. class SpatialHashTable:public BasicGrid<FLT>, public SpatialIndex<ObjType,FLT>
  70. {
  71. public:
  72. typedef SpatialHashTable SpatialHashType;
  73. typedef ObjType* ObjPtr;
  74. typedef typename ObjType::ScalarType ScalarType;
  75. typedef Point3<ScalarType> CoordType;
  76. typedef typename BasicGrid<FLT>::Box3x Box3x;
  77. // Hash table definition
  78. // the hash index directly the grid structure.
  79. // We use a MultiMap because we need to store many object (faces) inside each cell of the grid.
  80. typedef typename STDEXT::hash_multimap<Point3i, ObjType *, HashFunctor> HashType;
  81. typedef typename HashType::iterator HashIterator;
  82. HashType hash_table; // The real HASH TABLE **************************************
  83. // This vector is just a handy reference to all the allocated cells,
  84. // becouse hashed multimaps does not expose a direct list of all the different keys.
  85. std::vector<Point3i> AllocatedCells;
  86. // Class to abstract a HashIterator (that stores also the key,
  87. // while the interface of the generic spatial indexing need only simple object (face) pointers.
  88. struct CellIterator
  89. {
  90. CellIterator(){}
  91. HashIterator t;
  92. ObjPtr &operator *(){return (t->second); }
  93. ObjPtr operator *() const {return (t->second); }
  94. bool operator != (const CellIterator & p) const {return t!=p.t;}
  95. void operator ++() {t++;}
  96. };
  97. size_t CellSize(const Point3i &cell)
  98. {
  99. return hash_table.count(cell);
  100. }
  101. inline bool EmptyCell(const Point3i &cell) const
  102. {
  103. return hash_table.find(cell) == hash_table.end();
  104. }
  105. void UpdateAllocatedCells()
  106. {
  107. AllocatedCells.clear();
  108. if(hash_table.empty()) return;
  109. AllocatedCells.push_back(hash_table.begin()->first);
  110. for(HashIterator fi=hash_table.begin();fi!=hash_table.end();++fi)
  111. {
  112. if(AllocatedCells.back()!=fi->first) AllocatedCells.push_back(fi->first);
  113. }
  114. }
  115. protected:
  116. ///insert a new cell
  117. void InsertObject(ObjType* s, const Point3i &cell)
  118. {
  119. //if(hash_table.count(cell)==0) AllocatedCells.push_back(cell);
  120. hash_table.insert(typename HashType::value_type(cell, s));
  121. }
  122. ///remove all the objects in a cell
  123. void RemoveCell(const Point3i &cell)
  124. {
  125. }
  126. bool RemoveObject(ObjType* s, const Point3i &cell)
  127. {
  128. std::pair<HashIterator,HashIterator> CellRange = hash_table.equal_range(cell);
  129. CellIterator first; first.t=CellRange.first;
  130. CellIterator end; end.t=CellRange.second;
  131. for(CellIterator ci = first; ci!=end;++ci)
  132. {
  133. if (*ci == s)
  134. {
  135. hash_table.erase(ci.t);
  136. return true;
  137. }
  138. }
  139. return false;
  140. }
  141. public:
  142. vcg::Box3i Add( ObjType* s)
  143. {
  144. Box3<ScalarType> b;
  145. s->GetBBox(b);
  146. vcg::Box3i bb;
  147. BoxToIBox(b,bb);
  148. //then insert all the cell of bb
  149. for (int i=bb.min.X();i<=bb.max.X();i++)
  150. for (int j=bb.min.Y();j<=bb.max.Y();j++)
  151. for (int k=bb.min.Z();k<=bb.max.Z();k++)
  152. InsertObject(s,vcg::Point3i(i,j,k));
  153. return bb;
  154. }
  155. ///Remove all the objects contained in the cell containing s
  156. // it removes s too.
  157. bool RemoveCell(ObjType* s)
  158. {
  159. Point3i pi;
  160. PToIP(s->cP(),pi);
  161. std::pair<HashIterator,HashIterator> CellRange = hash_table.equal_range(pi);
  162. hash_table.erase(CellRange.first,CellRange.second);
  163. return true;
  164. } ///insert a new cell
  165. int RemoveInSphere(const Point3<ScalarType> &p, const ScalarType radius)
  166. {
  167. Box3x b(p-Point3f(radius,radius,radius),p+Point3f(radius,radius,radius));
  168. vcg::Box3i bb;
  169. BoxToIBox(b,bb);
  170. ScalarType r2=radius*radius;
  171. int cnt=0;
  172. std::vector<HashIterator> toDel;
  173. for (int i=bb.min.X();i<=bb.max.X();i++)
  174. for (int j=bb.min.Y();j<=bb.max.Y();j++)
  175. for (int k=bb.min.Z();k<=bb.max.Z();k++)
  176. {
  177. std::pair<HashIterator,HashIterator> CellRange = hash_table.equal_range(Point3i(i,j,k));
  178. for(HashIterator hi = CellRange.first; hi!=CellRange.second;++hi)
  179. {
  180. if(SquaredDistance(p,hi->second->cP()) <= r2)
  181. {
  182. cnt++;
  183. toDel.push_back(hi);
  184. }
  185. }
  186. }
  187. for(typename std::vector<HashIterator>::iterator vi=toDel.begin(); vi!=toDel.end();++vi)
  188. hash_table.erase(*vi);
  189. return cnt;
  190. }
  191. // Thsi version of the removal is specialized for the case where
  192. // an object has a pointshaped box and using the generic bbox interface is just a waste of time.
  193. void RemovePunctual( ObjType *s)
  194. {
  195. Point3i pi;
  196. PToIP(s->cP(),pi);
  197. std::pair<HashIterator,HashIterator> CellRange = hash_table.equal_range(pi);
  198. for(HashIterator hi = CellRange.first; hi!=CellRange.second;++hi)
  199. {
  200. if (hi->second == s)
  201. {
  202. hash_table.erase(hi);
  203. return;
  204. }
  205. }
  206. }
  207. void Remove( ObjType* s)
  208. {
  209. Box3<ScalarType> b;
  210. s->GetBBox(b);
  211. vcg::Box3i bb;
  212. BoxToIBox(b,bb);
  213. //then remove the obj from all the cell of bb
  214. for (int i=bb.min.X();i<=bb.max.X();i++)
  215. for (int j=bb.min.Y();j<=bb.max.Y();j++)
  216. for (int k=bb.min.Z();k<=bb.max.Z();k++)
  217. RemoveObject(s,vcg::Point3i(i,j,k));
  218. }
  219. /// set an empty spatial hash table
  220. void InitEmpty(const Box3x &_bbox, vcg::Point3i grid_size)
  221. {
  222. Box3x b;
  223. Box3x &bbox = this->bbox;
  224. CoordType &dim = this->dim;
  225. Point3i &siz = this->siz;
  226. CoordType &voxel = this->voxel;
  227. assert(!_bbox.IsNull());
  228. bbox=_bbox;
  229. dim = bbox.max - bbox.min;
  230. assert((grid_size.V(0)>0)&&(grid_size.V(1)>0)&&(grid_size.V(2)>0));
  231. siz=grid_size;
  232. voxel[0] = dim[0]/siz[0];
  233. voxel[1] = dim[1]/siz[1];
  234. voxel[2] = dim[2]/siz[2];
  235. hash_table.clear();
  236. }
  237. /// Insert a mesh in the grid.
  238. template <class OBJITER>
  239. void Set(const OBJITER & _oBegin, const OBJITER & _oEnd, const Box3x &_bbox=Box3x() )
  240. {
  241. OBJITER i;
  242. Box3x b;
  243. Box3x &bbox = this->bbox;
  244. CoordType &dim = this->dim;
  245. Point3i &siz = this->siz;
  246. CoordType &voxel = this->voxel;
  247. int _size=(int)std::distance<OBJITER>(_oBegin,_oEnd);
  248. if(!_bbox.IsNull()) this->bbox=_bbox;
  249. else
  250. {
  251. for(i = _oBegin; i!= _oEnd; ++i)
  252. {
  253. (*i).GetBBox(b);
  254. this->bbox.Add(b);
  255. }
  256. ///inflate the bb calculated
  257. bbox.Offset(bbox.Diag()/100.0) ;
  258. }
  259. dim = bbox.max - bbox.min;
  260. BestDim( _size, dim, siz );
  261. // find voxel size
  262. voxel[0] = dim[0]/siz[0];
  263. voxel[1] = dim[1]/siz[1];
  264. voxel[2] = dim[2]/siz[2];
  265. for(i = _oBegin; i!= _oEnd; ++i)
  266. Add(&(*i));
  267. }
  268. ///return the simplexes of the cell that contain p
  269. void GridReal( const Point3<ScalarType> & p, CellIterator & first, CellIterator & last )
  270. {
  271. vcg::Point3i _c;
  272. this->PToIP(p,_c);
  273. Grid(_c,first,last);
  274. }
  275. ///return the simplexes on a specified cell
  276. void Grid( int x,int y,int z, CellIterator & first, CellIterator & last )
  277. {
  278. this->Grid(vcg::Point3i(x,y,z),first,last);
  279. }
  280. ///return the simplexes on a specified cell
  281. void Grid( const Point3i & _c, CellIterator & first, CellIterator & end )
  282. {
  283. std::pair<HashIterator,HashIterator> CellRange = hash_table.equal_range(_c);
  284. first.t=CellRange.first;
  285. end.t=CellRange.second;
  286. }
  287. void Clear()
  288. {
  289. hash_table.clear();
  290. AllocatedCells.clear();
  291. }
  292. template <class OBJPOINTDISTFUNCTOR, class OBJMARKER>
  293. ObjPtr GetClosest(OBJPOINTDISTFUNCTOR & _getPointDistance, OBJMARKER & _marker,
  294. const CoordType & _p, const ScalarType & _maxDist,ScalarType & _minDist, CoordType & _closestPt)
  295. {
  296. return (vcg::GridClosest<SpatialHashType,OBJPOINTDISTFUNCTOR,OBJMARKER>(*this,_getPointDistance,_marker, _p,_maxDist,_minDist,_closestPt));
  297. }
  298. template <class OBJPOINTDISTFUNCTOR, class OBJMARKER, class OBJPTRCONTAINER,class DISTCONTAINER, class POINTCONTAINER>
  299. unsigned int GetKClosest(OBJPOINTDISTFUNCTOR & _getPointDistance,OBJMARKER & _marker,
  300. const unsigned int _k, const CoordType & _p, const ScalarType & _maxDist,OBJPTRCONTAINER & _objectPtrs,
  301. DISTCONTAINER & _distances, POINTCONTAINER & _points)
  302. {
  303. return (vcg::GridGetKClosest<SpatialHashType,
  304. OBJPOINTDISTFUNCTOR,OBJMARKER,OBJPTRCONTAINER,DISTCONTAINER,POINTCONTAINER>
  305. (*this,_getPointDistance,_marker,_k,_p,_maxDist,_objectPtrs,_distances,_points));
  306. }
  307. template <class OBJPOINTDISTFUNCTOR, class OBJMARKER, class OBJPTRCONTAINER, class DISTCONTAINER, class POINTCONTAINER>
  308. unsigned int GetInSphere(OBJPOINTDISTFUNCTOR & _getPointDistance,
  309. OBJMARKER & _marker,
  310. const CoordType & _p,
  311. const ScalarType & _r,
  312. OBJPTRCONTAINER & _objectPtrs,
  313. DISTCONTAINER & _distances,
  314. POINTCONTAINER & _points)
  315. {
  316. return(vcg::GridGetInSphere<SpatialHashType,
  317. OBJPOINTDISTFUNCTOR,OBJMARKER,OBJPTRCONTAINER,DISTCONTAINER,POINTCONTAINER>
  318. (*this,_getPointDistance,_marker,_p,_r,_objectPtrs,_distances,_points));
  319. }
  320. template <class OBJMARKER, class OBJPTRCONTAINER>
  321. unsigned int GetInBox(OBJMARKER & _marker,
  322. const Box3x _bbox,
  323. OBJPTRCONTAINER & _objectPtrs)
  324. {
  325. return(vcg::GridGetInBox<SpatialHashType,OBJMARKER,OBJPTRCONTAINER>
  326. (*this,_marker,_bbox,_objectPtrs));
  327. }
  328. template <class OBJRAYISECTFUNCTOR, class OBJMARKER>
  329. ObjPtr DoRay(OBJRAYISECTFUNCTOR & _rayIntersector, OBJMARKER & _marker, const Ray3<ScalarType> & _ray, const ScalarType & _maxDist, ScalarType & _t)
  330. {
  331. return(vcg::GridDoRay<SpatialHashType,OBJRAYISECTFUNCTOR,OBJMARKER>
  332. (*this,_rayIntersector,_marker,_ray,_maxDist,_t));
  333. }
  334. }; // end class
  335. /** Spatial Hash Table Dynamic
  336. Update the Hmark value on the simplex for dynamic updating of contents of the cell.
  337. The simplex must have the HMark() function.
  338. */
  339. template < typename ContainerType,class FLT=double>
  340. class DynamicSpatialHashTable: public SpatialHashTable<ContainerType,FLT>
  341. {
  342. public:
  343. typedef typename SpatialHashTable<ContainerType,FLT>::CoordType CoordType;
  344. typedef typename SpatialHashTable<ContainerType,FLT>::ObjType ObjType;
  345. typedef typename SpatialHashTable<ContainerType,FLT>::ObjPtr ObjPtr;
  346. typedef typename SpatialHashTable<ContainerType,FLT>::Box3x Box3x;
  347. typedef typename SpatialHashTable<ContainerType,FLT>::CellIterator CellIterator;
  348. void _UpdateHMark(ObjType* s){ s->HMark() = this->tempMark;}
  349. void getInCellUpdated(vcg::Point3i cell,std::vector<ObjPtr> &elems)
  350. {
  351. CellIterator first,last,l;
  352. Grid(cell,first,last);
  353. for (l=first;l!=last;l++)
  354. {
  355. if ((l->second)>=(**l).HMark())
  356. elems.push_back(&(**l));
  357. }
  358. }
  359. };
  360. }// end namespace
  361. #endif