grid_util2d.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 * VCGLib                                                            o o     *
00003 * Visual and Computer Graphics Library                            o     o   *
00004 *                                                                _   O  _   *
00005 * Copyright(C) 2005                                                \/)\/    *
00006 * Visual Computing Lab                                            /\/|      *
00007 * ISTI - Italian National Research Council                           |      *
00008 *                                                                    \      *
00009 * All rights reserved.                                                      *
00010 *                                                                           *
00011 * This program is free software; you can redistribute it and/or modify      *   
00012 * it under the terms of the GNU General Public License as published by      *
00013 * the Free Software Foundation; either version 2 of the License, or         *
00014 * (at your option) any later version.                                       *
00015 *                                                                           *
00016 * This program is distributed in the hope that it will be useful,           *
00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
00019 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
00020 * for more details.                                                         *
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                 Derives the right values of Dim and voxel starting
00072                 from the current values of siz and bbox
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                 /* Given a 3D point, returns the coordinates of the cell where the point is
00082                 * @param p is a 3D point
00083                 * @return integer coordinates of the cell
00084                 */
00085                 inline Point2i GridP( const Point2<ScalarType> & p ) const 
00086                 {
00087                         Point2i pi; 
00088                         PToIP(p, pi);
00089                         return pi;
00090                 }
00091 
00092                 /* Given a 3D point p, returns the index of the corresponding cell
00093                 * @param p is a 3D point in the space
00094                 * @return integer coordinates pi of the cell
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                 /* Given a cell index return the lower corner of the cell
00104                 * @param integer coordinates pi of the cell
00105                 * @return p is a 3D point representing the lower corner of the cell
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                 /* Given a cell index return the corresponding box
00115                 * @param integer coordinates pi of the cell
00116                 * @return b is the corresponding box in <ScalarType> coordinates
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                 /* Given a cell index return the center of the cell itself
00129                 * @param integer coordinates pi of the cell
00130                 * @return b is the corresponding box in <ScalarType> coordinates
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                 // Same of IPiToPf but for the case that you just want to transform 
00139                 // from a space to the other.
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                 /* Given a cell in <ScalarType> coordinates, compute the corresponding cell in integer coordinates
00148                 * @param b is the cell in <ScalarType> coordinates
00149                 * @return ib is the correspondent box in integer coordinates
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                         //assert(ib.max[0]>=0 && ib.max[1]>=0 && ib.max[2]>=0); 
00156                 }
00157 
00158                 /* Given a cell in integer coordinates, compute the corresponding cell in <ScalarType> coordinates
00159                 * @param ib is the cell in integer coordinates
00160                 * @return b is the correspondent box in <ScalarType> coordinates
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;           // Numero minimo di celle
00185                 const double GFactor = 1;       // GridEntry = NumElem*GFactor
00186                 double diag = size.Norm();      // Diagonale del box
00187                 double eps  = diag*1e-4;                // Fattore di tolleranza
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);       // Calcolo numero di voxel
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


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:31:31