added IPToP and IBoxToBox functions, modified BoxToIBox function in order to use PToIP function

This commit is contained in:
Nico Pietroni 2005-08-02 11:01:05 +00:00
parent a96663b39e
commit 7bc4ef59fd
1 changed files with 116 additions and 110 deletions

View File

@ -21,9 +21,12 @@
* * * *
****************************************************************************/ ****************************************************************************/
/**************************************************************************** /****************************************************************************
History History
$Log: not supported by cvs2svn $ $Log: not supported by cvs2svn $
Revision 1.3 2005/07/28 06:11:12 cignoni
corrected error in GridP (did not compile)
Revision 1.2 2005/07/01 11:33:36 cignoni Revision 1.2 2005/07/01 11:33:36 cignoni
Added a class BasicGrid with some utility function that are scattered among similar classes Added a class BasicGrid with some utility function that are scattered among similar classes
@ -36,35 +39,39 @@ Removed BestDim function from the grid_static_ptr class and moved to a indipende
#define __VCGLIB_GRID_UTIL #define __VCGLIB_GRID_UTIL
namespace vcg { namespace vcg {
// classe di base per tutte le strutture dati di indexing spaziale basate su griglia.
// contiene tutte le funzioni solite per le conversioni tra point3f e point3i
// classe di base per tutte le strutture dati di indexing spaziale basate su griglia.
template <class ScalarType> // contiene tutte le funzioni solite per le conversioni tra point3f e point3i
class BasicGrid {
public:
Box3<ScalarType> bbox; template <class ScalarType>
/// Dimensione spaziale (lunghezza lati) del bbox class BasicGrid {
Point3<ScalarType> dim;
typedef typename Box3<ScalarType> Box3x;
public:
Box3x bbox;
/// Dimensione spaziale (lunghezza lati) del bbox
Point3<ScalarType> dim;
/// Dimensioni griglia in celle /// Dimensioni griglia in celle
Point3i siz; Point3i siz;
/// Dimensioni di una cella /// Dimensioni di una cella
Point3<ScalarType> voxel; Point3<ScalarType> voxel;
// Dato un punto ritorna le coordinate della cella
inline Point3i GridP( const Point3<ScalarType> & p ) const // Dato un punto ritorna le coordinate della cella
inline Point3i GridP( const Point3<ScalarType> & p ) const
{ {
Point3i pi; Point3i pi;
PToIP(p,pi); PToIP(p,pi);
return pi; return pi;
} }
/// Dato un punto 3d ritorna l'indice del box corrispondente /// Dato un punto 3d ritorna l'indice del box corrispondente
inline void PToIP(const Point3<ScalarType> & p, Point3i &pi ) const inline void PToIP(const Point3<ScalarType> & p, Point3i &pi ) const
{ {
Point3<ScalarType> t = p - bbox.min; Point3<ScalarType> t = p - bbox.min;
pi[0] = int( t[0]/voxel[0] ); pi[0] = int( t[0]/voxel[0] );
@ -72,105 +79,104 @@ Removed BestDim function from the grid_static_ptr class and moved to a indipende
pi[2] = int( t[2]/voxel[2] ); pi[2] = int( t[2]/voxel[2] );
} }
/// Dato un box reale ritorna gli indici dei voxel compresi dentro un ibox /// Given a voxel index return the lower corner of the voxel
void BoxToIBox( const Box3d & b, Box3i & ib ) const inline void IPToP(const Point3i & pi, Point3<ScalarType> &p ) const
{ {
Point3d t; p[0] = ((ScalarType)pi[0])*voxel[0];
p[1] = ((ScalarType)pi[1])*voxel[1];
t = (b.min - bbox.min); // Traslo il box b; p[2] = ((ScalarType)pi[2])*voxel[2];
t[0] /= voxel[0]; p +=bbox.min;
t[1] /= voxel[1];
t[2] /= voxel[2];
ib.min[0] = int( t[0] ); // Trasformazione in intero
ib.min[1] = int( t[1] );
ib.min[2] = int( t[2] );
assert(ib.min[0]>=0 && ib.min[1]>=0 && ib.min[2]>=0);
t = (b.max - bbox.min); // Taslo il box b;
t[0] /= voxel[0];
t[1] /= voxel[1];
t[2] /= voxel[2];
ib.max[0] = int( ceil(t[0]) ); // Trasformazione in intero
ib.max[1] = int( ceil(t[1]) );
ib.max[2] = int( ceil(t[2]) );
assert(ib.max[0]>=0 && ib.max[1]>=0 && ib.max[2]>=0);
} }
/// Dato un box reale ritorna gli indici dei voxel compresi dentro un ibox
void BoxToIBox( const Box3x & b, Box3i & ib ) const
{
PToIP(b.min,ib.min);
PToIP(b.max,ib.max);
assert(ib.max[0]>=0 && ib.max[1]>=0 && ib.max[2]>=0);
}
/// Dato un box in voxel ritorna gli estremi del box reale
void IBoxToBox( const Box3i & ib, Box3x & b ) const
{
IPtoP(ib.min,b.min);
IPtoP(ib.max,b.max);
}
}; };
/** Calcolo dimensioni griglia. /** Calcolo dimensioni griglia.
Calcola la dimensione della griglia in funzione Calcola la dimensione della griglia in funzione
della ratio del bounding box e del numero di elementi della ratio del bounding box e del numero di elementi
*/ */
template<class scalar_type> template<class scalar_type>
void BestDim( const int elems, const Point3<scalar_type> & size, Point3i & dim ) void BestDim( const int elems, const Point3<scalar_type> & size, Point3i & dim )
{
const int mincells = 1; // Numero minimo di celle
const double GFactor = 1.0; // GridEntry = NumElem*GFactor
double diag = size.Norm(); // Diagonale del box
double eps = diag*1e-4; // Fattore di tolleranza
assert(elems>0);
assert(size[0]>=0.0);
assert(size[1]>=0.0);
assert(size[2]>=0.0);
int ncell = int(elems*GFactor); // Calcolo numero di voxel
if(ncell<mincells)
ncell = mincells;
dim[0] = 1;
dim[1] = 1;
dim[2] = 1;
if(size[0]>eps)
{ {
if(size[1]>eps) const int mincells = 1; // Numero minimo di celle
{ const double GFactor = 1.0; // GridEntry = NumElem*GFactor
if(size[2]>eps) double diag = size.Norm(); // Diagonale del box
double eps = diag*1e-4; // Fattore di tolleranza
assert(elems>0);
assert(size[0]>=0.0);
assert(size[1]>=0.0);
assert(size[2]>=0.0);
int ncell = int(elems*GFactor); // Calcolo numero di voxel
if(ncell<mincells)
ncell = mincells;
dim[0] = 1;
dim[1] = 1;
dim[2] = 1;
if(size[0]>eps)
{ {
double k = pow((double)(ncell/(size[0]*size[1]*size[2])),double(1.0/3.f)); if(size[1]>eps)
dim[0] = int(size[0] * k); {
dim[1] = int(size[1] * k); if(size[2]>eps)
dim[2] = int(size[2] * k); {
} double k = pow((double)(ncell/(size[0]*size[1]*size[2])),double(1.0/3.f));
else dim[0] = int(size[0] * k);
{ dim[1] = int(size[1] * k);
dim[0] = int(::sqrt(ncell*size[0]/size[1])); dim[2] = int(size[2] * k);
dim[1] = int(::sqrt(ncell*size[1]/size[0])); }
else
{
dim[0] = int(::sqrt(ncell*size[0]/size[1]));
dim[1] = int(::sqrt(ncell*size[1]/size[0]));
}
}
else
{
if(size[2]>eps)
{
dim[0] = int(::sqrt(ncell*size[0]/size[2]));
dim[2] = int(::sqrt(ncell*size[2]/size[0]));
}
else
dim[0] = int(ncell);
}
} }
} else
else
{
if(size[2]>eps)
{ {
dim[0] = int(::sqrt(ncell*size[0]/size[2])); if(size[1]>eps)
dim[2] = int(::sqrt(ncell*size[2]/size[0])); {
if(size[2]>eps)
{
dim[1] = int(::sqrt(ncell*size[1]/size[2]));
dim[2] = int(::sqrt(ncell*size[2]/size[1]));
}
else
dim[1] = int(ncell);
}
else if(size[2]>eps)
dim[2] = int(ncell);
} }
else dim[0] = math::Max(dim[0],1);
dim[0] = int(ncell); dim[1] = math::Max(dim[1],1);
} dim[2] = math::Max(dim[2],1);
} }
else }
{
if(size[1]>eps)
{
if(size[2]>eps)
{
dim[1] = int(::sqrt(ncell*size[1]/size[2]));
dim[2] = int(::sqrt(ncell*size[2]/size[1]));
}
else
dim[1] = int(ncell);
}
else if(size[2]>eps)
dim[2] = int(ncell);
}
dim[0] = math::Max(dim[0],1);
dim[1] = math::Max(dim[1],1);
dim[2] = math::Max(dim[2],1);
}
}
#endif #endif