Go to the documentation of this file.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 #ifndef __VCGLIB_GRID_UTIL_2D
00027 #define __VCGLIB_GRID_UTIL_2D
00028
00029 #include<vcg/space/index/base2d.h>
00030 #include<vcg/space/box2.h>
00031
00032
00033 #ifndef WIN32
00034 #define __int64 long long
00035 #define __cdecl
00036 #endif
00037
00038 namespace vcg {
00039
00054 template <class SCALARTYPE>
00055 class BasicGrid2D
00056 {
00057 public:
00058
00059 typedef SCALARTYPE ScalarType;
00060 typedef Box2<ScalarType> Box2x;
00061 typedef Point2<ScalarType> CoordType;
00062 typedef BasicGrid2D<SCALARTYPE> GridType;
00063
00064 Box2x bbox;
00065
00066 CoordType dim;
00067 Point2i siz;
00068 CoordType voxel;
00069
00070
00071
00072
00073
00074 void ComputeDimAndVoxel()
00075 {
00076 this->dim = this->bbox.max - this->bbox.min;
00077 this->voxel[0] = this->dim[0]/this->siz[0];
00078 this->voxel[1] = this->dim[1]/this->siz[1];
00079 }
00080
00081
00082
00083
00084
00085 inline Point2i GridP( const Point2<ScalarType> & p ) const
00086 {
00087 Point2i pi;
00088 PToIP(p, pi);
00089 return pi;
00090 }
00091
00092
00093
00094
00095
00096 inline void PToIP(const CoordType & p, Point2i &pi ) const
00097 {
00098 CoordType t = p - bbox.min;
00099 pi[0] = int( t[0] / voxel[0] );
00100 pi[1] = int( t[1] / voxel[1] );
00101 }
00102
00103
00104
00105
00106
00107 inline void IPiToPf(const Point2i & pi, CoordType &p ) const
00108 {
00109 p[0] = ((ScalarType)pi[0])*voxel[0];
00110 p[1] = ((ScalarType)pi[1])*voxel[1];
00111 p += bbox.min;
00112 }
00113
00114
00115
00116
00117
00118 inline void IPiToBox(const Point2i & pi, Box2x & b ) const
00119 {
00120 CoordType p;
00121 p[0] = ((ScalarType)pi[0])*voxel[0];
00122 p[1] = ((ScalarType)pi[1])*voxel[1];
00123 p += bbox.min;
00124 b.min = p;
00125 b.max = (p + voxel);
00126 }
00127
00128
00129
00130
00131 inline void IPiToBoxCenter(const Point2i & pi, CoordType & c ) const
00132 {
00133 CoordType p;
00134 IPiToPf(pi,p);
00135 c = p + voxel/ScalarType(2.0);
00136 }
00137
00138
00139
00140 inline void IPfToPf(const CoordType & pi, CoordType &p ) const
00141 {
00142 p[0] = ((ScalarType)pi[0])*voxel[0];
00143 p[1] = ((ScalarType)pi[1])*voxel[1];
00144 p += bbox.min;
00145 }
00146
00147
00148
00149
00150
00151 inline void BoxToIBox( const Box2x & b, Box2i & ib ) const
00152 {
00153 PToIP(b.min, ib.min);
00154 PToIP(b.max, ib.max);
00155
00156 }
00157
00158
00159
00160
00161
00163 void IBoxToBox( const Box2i & ib, Box2x & b ) const
00164 {
00165 IPiToPf(ib.min,b.min);
00166 IPiToPf(ib.max+Point2i(1,1),b.max);
00167 }
00168 };
00169
00170 template<class scalar_type>
00171 void BestDim2D( const Box2<scalar_type> box, const scalar_type voxel_size, Point2i & dim )
00172 {
00173 Point2<scalar_type> box_size = box.max-box.min;
00174 __int64 elem_num = (__int64)(box_size[0]/voxel_size +0.5) *( __int64)(box_size[1]/voxel_size +0.5);
00175 BestDim2D(elem_num,box_size,dim);
00176 }
00181 template<class scalar_type>
00182 void BestDim2D( const __int64 elems, const Point2<scalar_type> & size, Point2i & dim )
00183 {
00184 const __int64 mincells = 1;
00185 const double GFactor = 1;
00186 double diag = size.Norm();
00187 double eps = diag*1e-4;
00188
00189 assert(elems>0);
00190 assert(size[0]>=0.0);
00191 assert(size[1]>=0.0);
00192
00193
00194 __int64 ncell = (__int64)(elems*GFactor);
00195 if(ncell<mincells)
00196 ncell = mincells;
00197
00198 dim[0] = 1;
00199 dim[1] = 1;
00200
00201 if ((size[0]>eps)&&(size[1]>eps))
00202 {
00203 scalar_type Area=size[0]*size[1];
00204 double k = pow((double)(ncell/Area),double(1.0/2.f));
00205 dim[0] = int(size[0] * k);
00206 dim[1] = int(size[1] * k);
00207 }
00208 else
00209 {
00210 if(size[0]>eps)
00211 dim[0] = int(ncell);
00212 else
00213 dim[1] = int(ncell);
00214 }
00215
00216 dim[0] = std::max(dim[0],1);
00217 dim[1] = std::max(dim[1],1);
00218 }
00219
00220 }
00221 #endif