Commit 185322a7 authored by Isaak Lim's avatar Isaak Lim
Browse files

- updated the OpenMesh tutorials to be OM 3 compliant

- added all the tutorials except "Using IO::Options" as unittests

git-svn-id: http://www.openmesh.org/svnrepo/OpenMesh/trunk@968 fdac6126-5c0c-442c-9429-916003d36597
parent f134e74a
......@@ -46,20 +46,20 @@ int main(int argc, char **argv)
{
for (v_it=mesh.vertices_begin(); v_it!=v_end; ++v_it)
{
mesh.property(cogs,v_it).vectorize(0.0f);
valence = 0;
mesh.property(cogs,*v_it).vectorize(0.0f);
valence = 0.0;
for (vv_it=mesh.vv_iter( v_it ); vv_it; ++vv_it)
for (vv_it=mesh.vv_iter( *v_it ); vv_it; ++vv_it)
{
mesh.property(cogs,v_it) += mesh.point( vv_it );
mesh.property(cogs,*v_it) += mesh.point( *vv_it );
++valence;
}
mesh.property(cogs,v_it) /= valence;
mesh.property(cogs,*v_it) /= valence;
}
for (v_it=mesh.vertices_begin(); v_it!=v_end; ++v_it)
if ( !mesh.is_boundary( v_it ) )
mesh.set_point( v_it, mesh.property(cogs,v_it) );
if ( !mesh.is_boundary( *v_it ) )
mesh.set_point( *v_it, mesh.property(cogs,*v_it) );
}
......
......@@ -52,21 +52,20 @@ private:
: mesh_(_mesh), cog_(_cog)
{}
void operator()(typename Mesh::Vertex& _v)
void operator()(const typename Mesh::VertexHandle& _vh)
{
typename Mesh::VertexHandle vh( mesh_.handle(_v) );
typename Mesh::VertexVertexIter vv_it;
typename Mesh::Scalar valence(0.0);
mesh_.property(cog_, vh) = typename Mesh::Point(0.0, 0.0, 0.0);
mesh_.property(cog_, _vh) = typename Mesh::Point(0.0, 0.0, 0.0);
for (vv_it=mesh_.vv_iter(vh); vv_it; ++vv_it)
for (vv_it=mesh_.vv_iter(_vh); vv_it.is_valid(); ++vv_it)
{
mesh_.property(cog_, vh) += mesh_.point( vv_it );
mesh_.property(cog_, _vh) += mesh_.point( *vv_it );
++valence;
}
mesh_.property(cog_, mesh_.handle(_v) ) /= valence;
mesh_.property(cog_, _vh ) /= valence;
}
private:
......@@ -82,12 +81,11 @@ private:
: mesh_(_mesh), cog_(_cog)
{}
void operator()(typename Mesh::Vertex& _v)
void operator()(const typename Mesh::VertexHandle& _vh)
{
typename Mesh::VertexHandle vh(mesh_.handle(_v));
if (!mesh_.is_boundary(vh))
mesh_.set_point( vh, mesh_.property(cog_, vh) );
if (!mesh_.is_boundary(_vh))
mesh_.set_point( _vh, mesh_.property(cog_, _vh) );
}
private:
......
......@@ -52,9 +52,9 @@ int main(int argc, char **argv)
for (MyMesh::VertexIter v_it = mesh.vertices_begin();
v_it != mesh.vertices_end(); ++v_it)
{
std::cout << "Vertex #" << v_it << ": " << mesh.point( v_it );
mesh.set_point( v_it, mesh.point(v_it)+mesh.normal(v_it) );
std::cout << " moved to " << mesh.point( v_it ) << std::endl;
std::cout << "Vertex #" << *v_it << ": " << mesh.point( *v_it );
mesh.set_point( *v_it, mesh.point(*v_it)+mesh.normal(*v_it) );
std::cout << " moved to " << mesh.point( *v_it ) << std::endl;
}
// don't need the normals anymore? Remove them!
......
......@@ -85,9 +85,9 @@ int main(int argc, char **argv)
for (MyMesh::VertexIter v_it = mesh.vertices_begin();
v_it != mesh.vertices_end(); ++v_it)
{
std::cout << "Vertex #" << v_it << ": " << mesh.point( v_it );
mesh.set_point( v_it, mesh.point(v_it)+mesh.normal(v_it) );
std::cout << " moved to " << mesh.point( v_it ) << std::endl;
std::cout << "Vertex #" << *v_it << ": " << mesh.point( *v_it );
mesh.set_point( *v_it, mesh.point(*v_it)+mesh.normal(*v_it) );
std::cout << " moved to " << mesh.point( *v_it ) << std::endl;
}
return 0;
......
......@@ -100,13 +100,13 @@ int main(int argc, char **argv)
{
cog[0] = cog[1] = cog[2] = valence = 0.0;
for (vv_it=mesh.vv_iter(*v_it); vv_it; ++vv_it)
for (vv_it=mesh.vv_iter(*v_it); vv_it.is_valid(); ++vv_it)
{
cog += mesh.point( *vv_it );
++valence;
}
mesh.data(v_it).set_cog(cog / valence);
mesh.data(*v_it).set_cog(cog / valence);
}
for (v_it=mesh.vertices_begin(); v_it!=v_end; ++v_it)
......
......@@ -15,10 +15,10 @@ fill_props( Mesh& _m, OpenMesh::VPropHandleT<float> _ph, bool _check=false)
it != _m.vertices_end(); ++it)
{
const float v = a[it->idx()%9];
if ( _check && !(_m.property( _ph, it ) == v) )
if ( _check && !(_m.property( _ph, *it ) == v) )
return false;
else
_m.property( _ph, it ) = v;
_m.property( _ph, *it ) = v;
}
return true;
}
......@@ -35,15 +35,15 @@ fill_props( Mesh& _m, OpenMesh::EPropHandleT<bool> _ph, bool _check=false )
const size_t n = it->idx();
const bool v = ((n&(n-1))==0); // true for 0,1,2,4,8,..
if (_check && _m.property( _ph, it ) != v)
if (_check && _m.property( _ph, *it ) != v)
{
std::cout << " eprop_bool: " << n << " -> "
<< _m.property(_ph, it ) << " != " << v << std::endl;
<< _m.property(_ph, *it ) << " != " << v << std::endl;
return false;
}
else
{
_m.property( _ph, it ) = v;
_m.property( _ph, *it ) = v;
std::cout << " eprop_bool: " << n << " -> " << v << std::endl;
}
}
......@@ -60,8 +60,8 @@ fill_props(Mesh& _m, OpenMesh::FPropHandleT<std::string> _ph, bool _check=false)
for( typename Mesh::FaceIter it=_m.faces_begin();
it != _m.faces_end(); ++it)
{
const int n = ++(it->idx());
_m.property( _ph, it ) = int2roman(n);
const int n = (it->idx()) + 1;
_m.property( _ph, *it ) = int2roman(n);
}
return true;
}
......@@ -92,10 +92,10 @@ fill_props( Mesh& _m, OpenMesh::HPropHandleT<T> _ph, bool _check=false)
v.vec4fval[2] = c[n%9];
v.vec4fval[3] = d[n%9];
if ( _check && _m.property( _ph, it ) != v )
if ( _check && _m.property( _ph, *it ) != v )
return false;
else
_m.property( _ph, it ) = v;
_m.property( _ph, *it ) = v;
}
return true;
}
......
#ifndef FILL_PROPS_HH
#define FILL_PROPS_HH
#include <OpenMesh/Core/Utils/Property.hh>
#include "int2roman.hh"
template <typename Mesh>
bool
fill_props( Mesh& _m, OpenMesh::VPropHandleT<float> _ph, bool _check=false)
{
static float a[9] = { 1.1f, 2.2f, 3.3f, 4.4f, 5.5f, 6.6f, 7.7f, 8.8f, 9.9f };
for(typename Mesh::VertexIter it=_m.vertices_begin();
it != _m.vertices_end(); ++it)
{
const float v = a[it->idx()%9];
if ( _check && !(_m.property( _ph, *it ) == v) )
return false;
else
_m.property( _ph, *it ) = v;
}
return true;
}
template <typename Mesh>
bool
fill_props( Mesh& _m, OpenMesh::EPropHandleT<bool> _ph, bool _check=false )
{
for( typename Mesh::EdgeIter it=_m.edges_begin();
it != _m.edges_end(); ++it)
{
const size_t n = it->idx();
const bool v = ((n&(n-1))==0); // true for 0,1,2,4,8,..
if (_check && _m.property( _ph, *it ) != v)
{
return false;
}
else
{
_m.property( _ph, *it ) = v;
}
}
return true;
}
template <typename Mesh>
bool
fill_props(Mesh& _m, OpenMesh::FPropHandleT<std::string> _ph, bool _check=false)
{
for( typename Mesh::FaceIter it=_m.faces_begin();
it != _m.faces_end(); ++it)
{
const int n = (it->idx()) + 1;
_m.property( _ph, *it ) = int2roman(n);
}
return true;
}
template <typename Mesh, typename T>
bool
fill_props( Mesh& _m, OpenMesh::HPropHandleT<T> _ph, bool _check=false)
{
T v;
static float a[9] = { 1.1f, 2.2f, 3.3f, 4.4f, 5.5f, 6.6f, 7.7f, 8.8f, 9.9f };
static float b[9] = { 2.2f, 3.3f, 4.4f, 5.5f, 6.6f, 7.7f, 8.8f, 9.9f, 1.1f };
static float c[9] = { 3.3f, 4.4f, 5.5f, 6.6f, 7.7f, 8.8f, 9.9f, 1.1f, 2.2f };
static float d[9] = { 4.4f, 5.5f, 6.6f, 7.7f, 8.8f, 9.9f, 1.1f, 2.2f, 3.3f };
static double values[9] = { 0.1, 0.02, 0.003, 0.0004, 0.00005, 0.000006,
0.0000007, 0.00000008, 0.000000009 };
for( typename Mesh::HalfedgeIter it=_m.halfedges_begin();
it != _m.halfedges_end(); ++it)
{
const int n = it->idx();
v = it->idx()+1; // ival
v = values[n%9]; // dval
v = ((n&(n-1))==0); // bval
v.vec4fval[0] = a[n%9];
v.vec4fval[1] = b[n%9];
v.vec4fval[2] = c[n%9];
v.vec4fval[3] = d[n%9];
if ( _check && _m.property( _ph, *it ) != v )
return false;
else
_m.property( _ph, *it ) = v;
}
return true;
}
template <typename Mesh, typename T>
bool
fill_props( Mesh& _m, OpenMesh::MPropHandleT<T> _ph, bool _check=false)
{
for( typename Mesh::FaceIter it=_m.faces_begin(); it != _m.faces_end(); ++it)
{
const size_t idx = it->idx();
if ( _check && _m.property( _ph )[int2roman(idx+1)] != idx )
return false;
else
_m.property( _ph )[int2roman(idx+1)] = idx;
}
return true;
}
#endif
#ifndef GENERATE_CUBE_HH
#define GENERATE_CUBE_HH
template <typename MeshType>
size_t generate_cube( MeshType& mesh )
{
typedef typename MeshType::VertexHandle VertexHandle;
typedef typename MeshType::Point Point;
typename MeshType::VertexHandle vhandle[8];
vhandle[0] = mesh.add_vertex(Point(-1, -1, 1));
vhandle[1] = mesh.add_vertex(Point( 1, -1, 1));
vhandle[2] = mesh.add_vertex(Point( 1, 1, 1));
vhandle[3] = mesh.add_vertex(Point(-1, 1, 1));
vhandle[4] = mesh.add_vertex(Point(-1, -1, -1));
vhandle[5] = mesh.add_vertex(Point( 1, -1, -1));
vhandle[6] = mesh.add_vertex(Point( 1, 1, -1));
vhandle[7] = mesh.add_vertex(Point(-1, 1, -1));
// generate (quadrilateral) faces
std::vector< VertexHandle > face_vhandles;
face_vhandles.clear();
face_vhandles.push_back(vhandle[0]);
face_vhandles.push_back(vhandle[1]);
face_vhandles.push_back(vhandle[2]);
face_vhandles.push_back(vhandle[3]);
mesh.add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[7]);
face_vhandles.push_back(vhandle[6]);
face_vhandles.push_back(vhandle[5]);
face_vhandles.push_back(vhandle[4]);
mesh.add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[1]);
face_vhandles.push_back(vhandle[0]);
face_vhandles.push_back(vhandle[4]);
face_vhandles.push_back(vhandle[5]);
mesh.add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[2]);
face_vhandles.push_back(vhandle[1]);
face_vhandles.push_back(vhandle[5]);
face_vhandles.push_back(vhandle[6]);
mesh.add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[3]);
face_vhandles.push_back(vhandle[2]);
face_vhandles.push_back(vhandle[6]);
face_vhandles.push_back(vhandle[7]);
mesh.add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[0]);
face_vhandles.push_back(vhandle[3]);
face_vhandles.push_back(vhandle[7]);
face_vhandles.push_back(vhandle[4]);
mesh.add_face(face_vhandles);
return mesh.n_vertices();
}
#endif
#ifndef INT2ROMAN_HH
#define INT2ROMAN_HH
#include <string>
std::string int2roman( size_t decimal, size_t length = 30 )
{
assert( decimal > 0 && decimal < 1000 );
const size_t nrows = 4;
const size_t ncols = 4;
static size_t table_arabs[ nrows ][ ncols ] = { { 1000, 1000, 1000, 1000 },
{ 900, 500, 400, 100 },
{ 90, 50, 40, 10 },
{ 9, 5, 4, 1 } };
static const char *table_romans[ nrows ][ ncols ] = { { "M", "M", "M", "M" },
{ "CM", "D", "CD", "C" },
{ "XC", "L", "XL", "X" },
{ "IX", "V", "IV", "I" } };
size_t power; // power of ten
size_t index; // Indexes thru values to subtract
std::string roman;
roman.reserve(length);
roman[ 0 ] = '\0';
for ( power = 0; power < nrows; power++ )
for ( index = 0; index < ncols; index++ )
while ( decimal >= table_arabs[ power ][ index ] )
{
roman += table_romans[ power ][ index ];
decimal -= table_arabs[ power ][ index ];
}
return roman;
}
#endif
#include <gtest/gtest.h>
#include <Unittests/unittests_common.hh>
#include <string>
#include <map>
#include "generate_cube.hh"
#include "fill_props.hh"
/*
* ====================================================================
* Definition of custom properties related classes
* ====================================================================
*/
struct MyData
{
int ival;
double dval;
bool bval;
OpenMesh::Vec4f vec4fval;
MyData()
: ival(0), dval(0.0), bval(false)
{ }
MyData( const MyData& _cpy )
: ival(_cpy.ival), dval(_cpy.dval), bval(_cpy.bval),
vec4fval(_cpy.vec4fval)
{ }
// ---------- assignment
MyData& operator = (const MyData& _rhs)
{
ival = _rhs.ival;
dval = _rhs.dval;
bval = _rhs.bval;
vec4fval = _rhs.vec4fval;
return *this;
}
MyData& operator = (int _rhs) { ival = _rhs; return *this; }
MyData& operator = (double _rhs) { dval = _rhs; return *this; }
MyData& operator = (bool _rhs) { bval = _rhs; return *this; }
MyData& operator = (const OpenMesh::Vec4f& _rhs)
{ vec4fval = _rhs; return *this; }
// ---------- comparison
bool operator == (const MyData& _rhs) const
{
return ival == _rhs.ival
&& dval == _rhs.dval
&& bval == _rhs.bval
&& vec4fval == _rhs.vec4fval;
}
bool operator != (const MyData& _rhs) const { return !(*this == _rhs); }
};
typedef std::map< std::string, unsigned int > MyMap;
namespace OpenMesh {
namespace IO {
// support persistence for struct MyData
template <> struct binary<MyData>
{
typedef MyData value_type;
static const bool is_streamable = true;
// return binary size of the value
static size_t size_of(void)
{
return sizeof(int)+sizeof(double)+sizeof(bool)+sizeof(OpenMesh::Vec4f);
}
static size_t size_of(const value_type&)
{
return size_of();
}
static size_t store(std::ostream& _os, const value_type& _v, bool _swap=false)
{
size_t bytes;
bytes = IO::store( _os, _v.ival, _swap );
bytes += IO::store( _os, _v.dval, _swap );
bytes += IO::store( _os, _v.bval, _swap );
bytes += IO::store( _os, _v.vec4fval, _swap );
return _os.good() ? bytes : 0;
}
static size_t restore( std::istream& _is, value_type& _v, bool _swap=false)
{
size_t bytes;
bytes = IO::restore( _is, _v.ival, _swap );
bytes += IO::restore( _is, _v.dval, _swap );
bytes += IO::restore( _is, _v.bval, _swap );
bytes += IO::restore( _is, _v.vec4fval, _swap );
return _is.good() ? bytes : 0;
}
};
template <> struct binary< MyMap >
{
typedef MyMap value_type;
static const bool is_streamable = true;
// return generic binary size of self, if known
static size_t size_of(void) { return UnknownSize; }
// return binary size of the value
static size_t size_of(const value_type& _v)
{
if (_v.empty())
return sizeof(unsigned int);
value_type::const_iterator it = _v.begin();
unsigned int N = _v.size();
size_t bytes = IO::size_of(N);
for(;it!=_v.end(); ++it)
{
bytes += IO::size_of( it->first );
bytes += IO::size_of( it->second );
}
return bytes;
}
static
size_t store(std::ostream& _os, const value_type& _v, bool _swap=false)
{
size_t bytes = 0;
unsigned int N = _v.size();
value_type::const_iterator it = _v.begin();
bytes += IO::store( _os, N, _swap );
for (; it != _v.end() && _os.good(); ++it)
{
bytes += IO::store( _os, it->first, _swap );
bytes += IO::store( _os, it->second, _swap );
}
return _os.good() ? bytes : 0;
}
static
size_t restore( std::istream& _is, value_type& _v, bool _swap=false)
{
size_t bytes = 0;
unsigned int N = 0;
_v.clear();
bytes += IO::restore( _is, N, _swap );
std::string key;
size_t val;
for (size_t i=0; i<N && _is.good(); ++i)
{
bytes += IO::restore( _is, key, _swap );
bytes += IO::restore( _is, val, _swap );
_v[key] = val;
}
return _is.good() ? bytes : 0;
}
};
}
}
namespace {
class OpenMeshTutorials: public OpenMeshBase {
protected:
// This function is called before each test is run
virtual void SetUp() {
// Do some initial stuff with the member data here...
}
// This function is called after all tests are through
virtual void TearDown() {
// Do some final stuff with the member data here...
}
// Member already defined in OpenMeshBase
//Mesh mesh_;
};