Added the new mechanism for run time checking of mesh adequacy to a given algorithm.

While the many RequireXXX functions allow to check the static correctness of a mesh and
have a O(1) complexity, in many cases we need to run more complex checks to be sure that
the subsequent algorithm can run without issues.
Typical cases are the fact that there are no unreferenced vertices (NoUnreferencedVertex)
or a given adjacency is correctly initialized (and not only statically present as a type component).
This commit is contained in:
Paolo Cignoni 2014-11-03 14:50:37 +00:00
parent df6865a6de
commit b328a5f94a
3 changed files with 110 additions and 1 deletions

View File

@ -0,0 +1,93 @@
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2014 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* 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 *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
#ifndef __VCGLIB_MESH_ASSERT
#define __VCGLIB_MESH_ASSERT
#include <vcg/complex/complex.h>
namespace vcg {
namespace tri {
/**
* \brief For checking the adequacy of a mesh to a given algorithm.
*
* While the many RequireXXX functions allow to check the static correctness of a mesh and
* have a O(1) complexity, in many cases we need to run more complex checks to be sure that
* the subsequent algorithm can run without issues.
* Typical cases are the fact that there are no unreferenced vertices (NoUnreferencedVertex)
* or a given adjacency is correctly initialized (and not only statically present as a type component).
*
*/
template <class MeshType>
class MeshAssert
{
public:
typedef typename MeshType::VertexType VertexType;
typedef typename MeshType::VertexIterator VertexIterator;
typedef typename MeshType::FaceType FaceType;
typedef typename MeshType::FaceIterator FaceIterator;
typedef typename MeshType::CoordType CoordType;
typedef typename MeshType::ScalarType ScalarType;
static void FFAdjacencyIsInitialized(MeshType &m)
{
for(FaceIterator fi=m.face.begin();fi!=m.face.end();++fi)
{
if(!fi->IsD())
for(int i=0;i<fi->VN();++i)
{
if(fi->FFp(i)==0)
throw vcg::MissingPreconditionException("FF adjacency is not initialized");
}
}
}
static void VFAdjacencyIsInitialized(MeshType &m)
{
for(VertexIterator vi=m.vert.begin();vi!=m.vert.end();++vi) if(!vi->IsD())
{
if(vi->VFp().IsNull())
throw vcg::MissingPreconditionException("VF adjacency is not initialized");
}
}
static void NoUnreferencedVertex(MeshType &m)
{
tri::UpdateFlags<MeshType>::VertexClearV(m);
for(FaceIterator fi=m.face.begin();fi!=m.face.end();++fi) if(!fi->IsD())
{
for(int i=0;i<fi->VN();++i) fi->V(i)->SetV();
}
for(VertexIterator vi=m.vert.begin();vi!=m.vert.end();++vi)
if(!vi->IsD())
{
if(!vi->IsV())
throw vcg::MissingPreconditionException("There are unreferenced vertices");
}
}
};
} // end namespace tri
} // end namespace vcg
#endif // __VCGLIB_MESH_ASSERT

View File

@ -44,6 +44,7 @@
#include <vcg/complex/allocate.h>
#include <vcg/complex/algorithms/update/flag.h>
#include <vcg/complex/algorithms/update/selection.h>
#include <vcg/complex/algorithms/mesh_assert.h>
#include <vcg/complex/append.h>
#undef __VCG_MESH

View File

@ -82,6 +82,21 @@ public:
return buf;
}
};
}
class MissingPreconditionException : public std::runtime_error
{
public:
MissingPreconditionException(const std::string &err):std::runtime_error(err)
{
std::cout << "Mesh does not satisfy the following precondition:" << err << "- \n";
}
virtual const char *what() const throw ()
{
static char buf[128]="Mesh does not satisfy precondition";
return buf;
}
};
} // end namespace vcg
#endif // EXCEPTION_H