From 8bfd6c40e03b748ff8ec38ee98c6587da351062a Mon Sep 17 00:00:00 2001 From: cignoni Date: Mon, 16 Jul 2007 15:13:39 +0000 Subject: [PATCH] Splitted initialiazation functions of grid to add flexibility in the creation --- vcg/space/index/grid_static_ptr.h | 114 ++++++++++++++++++++++-------- 1 file changed, 86 insertions(+), 28 deletions(-) diff --git a/vcg/space/index/grid_static_ptr.h b/vcg/space/index/grid_static_ptr.h index 637b7f27..80e627b2 100644 --- a/vcg/space/index/grid_static_ptr.h +++ b/vcg/space/index/grid_static_ptr.h @@ -24,6 +24,10 @@ History $Log: not supported by cvs2svn $ +Revision 1.36 2005/12/02 00:43:31 cignoni +Forgotten a base deferencing like the previous one +Note also the different possible sintax with this-> instead of the base class name + Revision 1.35 2005/12/02 00:25:13 cignoni Added and removed typenames for gcc compiling. Added base class qualifier for referencing the elemntes of the templated base class (BasicGrid) @@ -335,7 +339,7 @@ namespace vcg { { this->bbox.Import( b ); ScalarType t = this->bbox.Diag()/100.0; - if(t == 0) t = ScalarType(1e20); // <--- Some doubts on this (Cigno 5/1/04) + if(t == 0) t = ScalarType(1e-20); // <--- Some doubts on this (Cigno 5/1/04) this->bbox.Offset(t); this->dim = this->bbox.max - this->bbox.min; } @@ -363,35 +367,89 @@ namespace vcg { //); } - template - inline void Set(const OBJITER & _oBegin, const OBJITER & _oEnd, const Box3x &_bbox=Box3x() ) + template + inline void Set(const OBJITER & _oBegin, const OBJITER & _oEnd) + { + Box3 _bbox; + Box3 b; + OBJITER i; + for(i = _oBegin; i!= _oEnd; ++i) + { + (*i).GetBBox(b); + _bbox.Add(b); + } + ///inflate the bb calculated + int _size=(int)std::distance(_oBegin,_oEnd); + ScalarType infl=_bbox.Diag()/_size; + _bbox.min-=vcg::Point3(infl,infl,infl); + _bbox.max+=vcg::Point3(infl,infl,infl); + + Set(_oBegin,_oEnd,_bbox); + } + + + + // This function automatically compute a reasonable size for the uniform grid providing the side (radius) of the cell + // + // Note that the bbox must be already 'inflated' so to be sure that no object will fall on the border of the grid. + + template + inline void Set(const OBJITER & _oBegin, const OBJITER & _oEnd, const Box3x &_bbox, FLT radius) + { + Point3i _siz; + Point3 _dim = _bbox.max - _bbox.min; + _dim/=radius; + assert(_dim[0]>0 && _dim[1]>0 && _dim[2]>0 ); + _siz[0] = floor(_dim[0]); + _siz[1] = floor(_dim[1]); + _siz[2] = floor(_dim[2]); + + Point3 offset=Point3::Construct(_siz); + offset*=radius; + offset-= (_bbox.max - _bbox.min); + offset /=2; + + assert( offset[0]>=0 && offset[1]>=0 && offset[2]>=0 ); + + _bbox.min -= offset; + _bbox.max += offset; + + Set(_oBegin,_oEnd,_bbox,_siz); + } + + + // This function automatically compute a reasonable size for the uniform grid such that the number of cells is + // the same of the nubmer of elements to be inserted in the grid. + // + // Note that the bbox must be already 'inflated' so to be sure that no object will fall on the border of the grid. + + template + inline void Set(const OBJITER & _oBegin, const OBJITER & _oEnd, const Box3x &_bbox) + { + int _size=(int)std::distance(_oBegin,_oEnd); + Point3 _dim = _bbox.max - _bbox.min; + Point3i _siz; + BestDim( _size, _dim, _siz ); + + Set(_oBegin,_oEnd,_bbox,_siz); + } + + + template + inline void Set(const OBJITER & _oBegin, const OBJITER & _oEnd, const Box3x &_bbox, Point3i _siz) { OBJITER i; - Box3 b; - int _size=(int)std::distance(_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 - ScalarType infl=this->bbox.Diag()/_size; - this->bbox.min-=vcg::Point3(infl,infl,infl); - this->bbox.max+=vcg::Point3(infl,infl,infl); - } - - this->dim = this->bbox.max - this->bbox.min; - BestDim( _size, this->dim, this->siz ); - // find voxel size - this->voxel[0] = this->dim[0]/this->siz[0]; - this->voxel[1] = this->dim[1]/this->siz[1]; - this->voxel[2] = this->dim[2]/this->siz[2]; - + + this->bbox=_bbox; + this->siz=_siz; + + // find voxel size starting from the provided bbox and grid size. + + this->dim = this->bbox.max - this->bbox.min; + this->voxel[0] = this->dim[0]/this->siz[0]; + this->voxel[1] = this->dim[1]/this->siz[1]; + this->voxel[2] = this->dim[2]/this->siz[2]; + // "Alloca" la griglia: +1 per la sentinella grid.resize( this->siz[0]*this->siz[1]*this->siz[2]+1 );