00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #ifndef __VCGLIB_UGRID_OBJ
00045 #define __VCGLIB_UGRID_OBJ
00046
00047 #include <vector>
00048 #include <algorithm>
00049 #include <stdio.h>
00050
00051 #include <vcg/space/box3.h>
00052 #include <vcg/space/line3.h>
00053 #include <vcg/space/index/grid_util.h>
00054 namespace vcg {
00061 template < class ObjType, class FLT=float >
00062 class GridStaticObj : public BasicGrid<FLT>
00063 {
00064 public:
00065
00067 ObjType *grid;
00068
00069 int size() const { return this->siz[0]*this->siz[1]*this->siz[2];}
00070
00071 inline GridStaticObj() { grid = 0; }
00072 inline ~GridStaticObj() { if(grid) delete[] grid; }
00073 inline void Init(const ObjType &val)
00074 {
00075 fill(grid,grid+size(),val);
00076 }
00077
00078
00080 inline ObjType & Grid( const int x, const int y, const int z ) {return grid[GridIndI(Point3i(x,y,z))]; }
00081
00082
00083 inline ObjType & Grid( const Point3<FLT> & p ) { return grid[GridIndF(p)]; }
00084
00085 inline int GridIndI( const Point3i & pi ) const
00086 {
00087 #ifndef NDEBUG
00088 if ( pi[0]<0 || pi[0]>=this->siz[0] || pi[1]<0 || pi[1]>=this->siz[1] || pi[2]<0 || pi[2]>=this->siz[2] )
00089 { assert(0);
00090 return 0;
00091 }
00092 #endif
00093 return pi[0]+this->siz[0]*(pi[1]+this->siz[1]*pi[2]);
00094 }
00095
00096
00097 inline int GridIndF( const Point3<FLT> & p ) const { return GridIndI(GridP(p)); }
00098
00099 void Create( const Point3i &_siz, const ObjType & init )
00100 {
00101 this->siz=_siz;
00102 this->voxel[0] = this->dim[0]/this->siz[0];
00103 this->voxel[1] = this->dim[1]/this->siz[1];
00104 this->voxel[2] = this->dim[2]/this->siz[2];
00105
00106 if(grid) delete[] grid;
00107 int n = this->siz[0]*this->siz[1]*this->siz[2];
00108 grid = new ObjType[n];
00109 fill(grid,grid+n,init);
00110 }
00111
00114
00115 template<class FLT2>
00116 void Create(const Box3<FLT2> & b, int ncell, const ObjType & init, bool Inflate = true )
00117 {
00118 this->bbox.Import(b);
00119 if(Inflate) this->bbox.Offset(0.01*this->bbox.Diag());
00120 this->dim = this->bbox.max - this->bbox.min;
00121
00122
00123 Point3i _siz;
00124 BestDim( ncell, this->dim, _siz );
00125 Create(_siz, init );
00126 }
00127 };
00128
00129
00130
00131 }
00132 #endif