Added Requirements. Refactored some funcs and uniformed naming of functions...

This commit is contained in:
Paolo Cignoni 2013-09-10 10:49:01 +00:00
parent e65be2aa17
commit b849524274
1 changed files with 184 additions and 176 deletions
vcg/complex/algorithms

View File

@ -8,7 +8,7 @@
* \ * * \ *
* All rights reserved. * * All rights reserved. *
* * * *
* This program is free software; you can redistribute it and/or modify * * This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by * * it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or * * the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. * * (at your option) any later version. *
@ -52,60 +52,68 @@ Initial Commit
namespace vcg { namespace vcg {
namespace tri{ namespace tri{
template <class StatMeshType> template <class StatMeshType>
class Stat class Stat
{ {
public: public:
typedef StatMeshType MeshType; typedef StatMeshType MeshType;
typedef typename MeshType::VertexType VertexType; typedef typename MeshType::VertexType VertexType;
typedef typename MeshType::VertexPointer VertexPointer; typedef typename MeshType::VertexPointer VertexPointer;
typedef typename MeshType::VertexIterator VertexIterator; typedef typename MeshType::VertexIterator VertexIterator;
typedef typename MeshType::ScalarType ScalarType; typedef typename MeshType::ScalarType ScalarType;
typedef typename MeshType::FaceType FaceType; typedef typename MeshType::FaceType FaceType;
typedef typename MeshType::FacePointer FacePointer; typedef typename MeshType::FacePointer FacePointer;
typedef typename MeshType::FaceIterator FaceIterator; typedef typename MeshType::FaceIterator FaceIterator;
typedef typename MeshType::EdgeIterator EdgeIterator; typedef typename MeshType::EdgeIterator EdgeIterator;
typedef typename MeshType::FaceContainer FaceContainer; typedef typename MeshType::FaceContainer FaceContainer;
typedef typename vcg::Box3<ScalarType> Box3Type; typedef typename vcg::Box3<ScalarType> Box3Type;
static void ComputePerVertexQualityMinMax( MeshType & m, float &minV, float &maxV)
{
std::pair<float,float> pp=ComputePerVertexQualityMinMax(m);
minV=pp.first; maxV=pp.second;
}
static std::pair<float,float> ComputePerVertexQualityMinMax( MeshType & m)
{
std::pair<float,float> minmax = std::make_pair(std::numeric_limits<float>::max(),-std::numeric_limits<float>::max());
VertexIterator vi;
for(vi = m.vert.begin(); vi != m.vert.end(); ++vi)
if(!(*vi).IsD())
{
if( (*vi).Q() < minmax.first) minmax.first=(*vi).Q();
if( (*vi).Q() > minmax.second) minmax.second=(*vi).Q();
}
return minmax;
}
static void ComputePerFaceQualityMinMax( MeshType & m, float &minV, float &maxV) static void ComputePerVertexQualityMinMax( MeshType & m, float &minV, float &maxV)
{ {
std::pair<float,float> pp=ComputePerFaceQualityMinMax(m); std::pair<float,float> pp=ComputePerVertexQualityMinMax(m);
minV=pp.first; maxV=pp.second; minV=pp.first; maxV=pp.second;
} }
static std::pair<float,float> ComputePerFaceQualityMinMax( MeshType & m) static std::pair<float,float> ComputePerVertexQualityMinMax( MeshType & m)
{ {
std::pair<float,float> minmax = std::make_pair(std::numeric_limits<float>::max(),-std::numeric_limits<float>::max()); // assert(0);
tri::RequirePerVertexQuality(m);
FaceIterator fi; typename MeshType::template PerMeshAttributeHandle < std::pair<float,float> > mmqH;
for(fi = m.face.begin(); fi != m.face.end(); ++fi) mmqH = tri::Allocator<MeshType>::template GetPerMeshAttribute <std::pair<float,float> >(m,"minmaxQ");
if(!(*fi).IsD())
{ std::pair<float,float> minmax = std::make_pair(std::numeric_limits<float>::max(),-std::numeric_limits<float>::max());
if( (*fi).Q() < minmax.first) minmax.first =(*fi).Q();
if( (*fi).Q() > minmax.second) minmax.second=(*fi).Q(); for(VertexIterator vi = m.vert.begin(); vi != m.vert.end(); ++vi)
} if(!(*vi).IsD())
return minmax; {
} if( (*vi).Q() < minmax.first) minmax.first=(*vi).Q();
if( (*vi).Q() > minmax.second) minmax.second=(*vi).Q();
}
mmqH() = minmax;
return minmax;
}
static void ComputePerFaceQualityMinMax( MeshType & m, float &minV, float &maxV)
{
std::pair<float,float> pp=ComputePerFaceQualityMinMax(m);
minV=pp.first; maxV=pp.second;
}
static std::pair<float,float> ComputePerFaceQualityMinMax( MeshType & m)
{
tri::RequirePerFaceQuality(m);
std::pair<float,float> minmax = std::make_pair(std::numeric_limits<float>::max(),-std::numeric_limits<float>::max());
FaceIterator fi;
for(fi = m.face.begin(); fi != m.face.end(); ++fi)
if(!(*fi).IsD())
{
if( (*fi).Q() < minmax.first) minmax.first =(*fi).Q();
if( (*fi).Q() > minmax.second) minmax.second=(*fi).Q();
}
return minmax;
}
/** /**
\short compute the barycenter of the surface thin-shell. \short compute the barycenter of the surface thin-shell.
@ -119,155 +127,155 @@ class Stat
ScalarType areaSum=0; ScalarType areaSum=0;
FaceIterator fi; FaceIterator fi;
for(fi = m.face.begin(); fi != m.face.end(); ++fi) for(fi = m.face.begin(); fi != m.face.end(); ++fi)
if(!(*fi).IsD()) if(!(*fi).IsD())
{ {
ScalarType area=DoubleArea(*fi); ScalarType area=DoubleArea(*fi);
barycenter += Barycenter(*fi)*area; barycenter += Barycenter(*fi)*area;
areaSum+=area; areaSum+=area;
} }
return barycenter/areaSum; return barycenter/areaSum;
} }
static ScalarType ComputeMeshArea(MeshType & m) static ScalarType ComputeMeshArea(MeshType & m)
{ {
ScalarType area=0; ScalarType area=0;
FaceIterator fi; for(FaceIterator fi = m.face.begin(); fi != m.face.end(); ++fi)
for(fi = m.face.begin(); fi != m.face.end(); ++fi) if(!(*fi).IsD())
if(!(*fi).IsD()) area += DoubleArea(*fi);
area += DoubleArea(*fi);
return area/ScalarType(2.0);
return area/ScalarType(2.0); }
}
static void ComputePerVertexQualityDistribution( MeshType & m, Distribution<float> &h, bool selectionOnly = false) // V1.0
static void ComputePerVertexQualityDistribution( MeshType & m, Distribution<float> &h, bool selectionOnly = false) // V1.0 {
tri::RequirePerVertexQuality(m);
for(VertexIterator vi = m.vert.begin(); vi != m.vert.end(); ++vi)
if(!(*vi).IsD() && ((!selectionOnly) || (*vi).IsS()) )
{ {
VertexIterator vi; assert(!math::IsNAN((*vi).Q()) && "You should never try to compute Histogram with Invalid Floating points numbers (NaN)");
for(vi = m.vert.begin(); vi != m.vert.end(); ++vi) h.Add((*vi).Q());
if(!(*vi).IsD() && ((!selectionOnly) || (*vi).IsS()) )
{
assert(!math::IsNAN((*vi).Q()) && "You should never try to compute Histogram with Invalid Floating points numbers (NaN)");
h.Add((*vi).Q());
}
} }
}
static void ComputePerFaceQualityDistribution( MeshType & m, Distribution<float> &h, bool selectionOnly = false) // V1.0 static void ComputePerFaceQualityDistribution( MeshType & m, Distribution<float> &h, bool selectionOnly = false) // V1.0
{
tri::RequirePerFaceQuality(m);
for(FaceIterator fi = m.face.begin(); fi != m.face.end(); ++fi)
if(!(*fi).IsD() && ((!selectionOnly) || (*fi).IsS()) )
{ {
FaceIterator fi; assert(!math::IsNAN((*fi).Q()) && "You should never try to compute Histogram with Invalid Floating points numbers (NaN)");
for(fi = m.face.begin(); fi != m.face.end(); ++fi) h.Add((*fi).Q());
if(!(*fi).IsD() && ((!selectionOnly) || (*fi).IsS()) )
{
assert(!math::IsNAN((*fi).Q()) && "You should never try to compute Histogram with Invalid Floating points numbers (NaN)");
h.Add((*fi).Q());
}
} }
}
static void ComputePerFaceQualityHistogram( MeshType & m, Histogramf &h, bool selectionOnly=false,int HistSize=10000 ) static void ComputePerFaceQualityHistogram( MeshType & m, Histogramf &h, bool selectionOnly=false,int HistSize=10000 )
{ {
std::pair<float,float> minmax = tri::Stat<MeshType>::ComputePerFaceQualityMinMax(m); tri::RequirePerFaceQuality(m);
h.Clear(); std::pair<float,float> minmax = tri::Stat<MeshType>::ComputePerFaceQualityMinMax(m);
h.SetRange( minmax.first,minmax.second, HistSize ); h.Clear();
for(FaceIterator fi = m.face.begin(); fi != m.face.end(); ++fi) h.SetRange( minmax.first,minmax.second, HistSize );
if(!(*fi).IsD() && ((!selectionOnly) || (*fi).IsS()) ){ for(FaceIterator fi = m.face.begin(); fi != m.face.end(); ++fi)
assert(!math::IsNAN((*fi).Q()) && "You should never try to compute Histogram with Invalid Floating points numbers (NaN)"); if(!(*fi).IsD() && ((!selectionOnly) || (*fi).IsS()) ){
h.Add((*fi).Q()); assert(!math::IsNAN((*fi).Q()) && "You should never try to compute Histogram with Invalid Floating points numbers (NaN)");
} h.Add((*fi).Q());
} }
}
static void ComputePerVertexQualityHistogram( MeshType & m, Histogramf &h, bool selectionOnly = false, int HistSize=10000 ) // V1.0 static void ComputePerVertexQualityHistogram( MeshType & m, Histogramf &h, bool selectionOnly = false, int HistSize=10000 ) // V1.0
{
tri::RequirePerVertexQuality(m);
std::pair<float,float> minmax = ComputePerVertexQualityMinMax(m);
h.Clear();
h.SetRange( minmax.first,minmax.second, HistSize);
for(VertexIterator vi = m.vert.begin(); vi != m.vert.end(); ++vi)
if(!(*vi).IsD() && ((!selectionOnly) || (*vi).IsS()) )
{ {
VertexIterator vi; assert(!math::IsNAN((*vi).Q()) && "You should never try to compute Histogram with Invalid Floating points numbers (NaN)");
std::pair<float,float> minmax = ComputePerVertexQualityMinMax(m); h.Add((*vi).Q());
h.Clear();
h.SetRange( minmax.first,minmax.second, HistSize);
for(vi = m.vert.begin(); vi != m.vert.end(); ++vi)
if(!(*vi).IsD() && ((!selectionOnly) || (*vi).IsS()) )
{
assert(!math::IsNAN((*vi).Q()) && "You should never try to compute Histogram with Invalid Floating points numbers (NaN)");
h.Add((*vi).Q());
}
// Sanity check; If some very wrong value has happened in the Q value,
// the histogram is messed. If a significant percentage (20% )of the values are all in a single bin
// we should try to solve the problem. No easy solution here.
// We choose to compute the get the 1percentile and 99 percentile values as new mixmax ranges
// and just to be sure enlarge the Histogram.
if(h.MaxCount() > HistSize/5)
{
std::vector<float> QV;
QV.reserve(m.vn);
for(vi = m.vert.begin(); vi != m.vert.end(); ++vi)
if(!(*vi).IsD()) QV.push_back((*vi).Q());
std::nth_element(QV.begin(),QV.begin()+m.vn/100,QV.end());
float newmin=*(QV.begin()+m.vn/100);
std::nth_element(QV.begin(),QV.begin()+m.vn-m.vn/100,QV.end());
float newmax=*(QV.begin()+m.vn-m.vn/100);
h.Clear();
h.SetRange(newmin, newmax, HistSize*50);
for(vi = m.vert.begin(); vi != m.vert.end(); ++vi)
if(!(*vi).IsD() && ((!selectionOnly) || (*vi).IsS()) )
h.Add((*vi).Q());
}
} }
// Sanity check; If some very wrong value has happened in the Q value,
// the histogram is messed. If a significant percentage (20% )of the values are all in a single bin
// we should try to solve the problem. No easy solution here.
// We choose to compute the get the 1percentile and 99 percentile values as new mixmax ranges
// and just to be sure enlarge the Histogram.
static void ComputeEdgeHistogram( MeshType & m, Histogramf &h) if(h.MaxCount() > HistSize/5)
{
std::vector<float> QV;
QV.reserve(m.vn);
for(VertexIterator vi = m.vert.begin(); vi != m.vert.end(); ++vi)
if(!(*vi).IsD()) QV.push_back((*vi).Q());
std::nth_element(QV.begin(),QV.begin()+m.vn/100,QV.end());
float newmin=*(QV.begin()+m.vn/100);
std::nth_element(QV.begin(),QV.begin()+m.vn-m.vn/100,QV.end());
float newmax=*(QV.begin()+m.vn-m.vn/100);
h.Clear();
h.SetRange(newmin, newmax, HistSize*50);
for(VertexIterator vi = m.vert.begin(); vi != m.vert.end(); ++vi)
if(!(*vi).IsD() && ((!selectionOnly) || (*vi).IsS()) )
h.Add((*vi).Q());
}
}
static void ComputeEdgeLengthHistogram( MeshType & m, Histogramf &h)
{
assert(m.edge.size()>0);
h.Clear();
h.SetRange( 0, m.bbox.Diag(), 10000);
for(EdgeIterator ei = m.edge.begin(); ei != m.edge.end(); ++ei)
{
if(!(*ei).IsD())
{ {
assert(m.edge.size()>0); h.Add(Distance<float>((*ei).V(0)->P(),(*ei).V(1)->P()));
h.Clear(); }
h.SetRange( 0, m.bbox.Diag(), 10000); }
for(EdgeIterator ei = m.edge.begin(); ei != m.edge.end(); ++ei) }
static ScalarType ComputeEdgeLengthAverage(MeshType & m)
{
Histogramf h;
ComputeEdgeLengthHistogram(m,h);
return h.Avg();
}
static void ComputeFaceEdgeLengthDistribution( MeshType & m, Distribution<float> &h)
{
h.Clear();
tri::UpdateFlags<MeshType>::FaceBorderFromNone(m);
for(FaceIterator fi = m.face.begin(); fi != m.face.end(); ++fi)
{
if(!(*fi).IsD())
{
for(int i=0;i<3;++i)
{ {
if(!(*ei).IsD()) h.Add(Distance<float>(fi->P0(i),fi->P1(i)));
{ if(fi->IsB(i)) // to be uniform border edges must be added twice...
h.Add(Distance<float>((*ei).V(0)->P(),(*ei).V(1)->P())); h.Add(Distance<float>(fi->P0(i),fi->P1(i)));
}
} }
} }
}
}
static ScalarType ComputeEdgeAverage(MeshType & m) static ScalarType ComputeFaceEdgeLengthAverage(MeshType & m)
{
double sum=0;
for(FaceIterator fi = m.face.begin(); fi != m.face.end(); ++fi)
if(!(*fi).IsD())
{ {
Histogramf h; for(int i=0;i<3;++i)
ComputeEdgeHistogram(m,h); sum+=double(Distance<float>(fi->P0(i),fi->P1(i)));
return h.Avg();
} }
return sum/(m.fn*3.0);
static void ComputeFaceEdgeDistribution( MeshType & m, Distribution<float> &h) }
{
h.Clear();
tri::UpdateFlags<MeshType>::FaceBorderFromNone(m);
for(FaceIterator fi = m.face.begin(); fi != m.face.end(); ++fi)
{
if(!(*fi).IsD())
{
for(int i=0;i<3;++i)
{
h.Add(Distance<float>(fi->P0(i),fi->P1(i)));
if(fi->IsB(i)) // to be uniform border edges must be added twice...
h.Add(Distance<float>(fi->P0(i),fi->P1(i)));
}
}
}
}
static ScalarType ComputeFaceEdgeAverage(MeshType & m)
{
double sum=0;
for(FaceIterator fi = m.face.begin(); fi != m.face.end(); ++fi)
if(!(*fi).IsD())
{
for(int i=0;i<3;++i)
sum+=double(Distance<float>(fi->P0(i),fi->P1(i)));
}
return sum/(m.fn*3.0);
}
}; // end class }; // end class
} //End Namespace tri } //End Namespace tri
} // End Namespace vcg } // End Namespace vcg
#endif #endif