Commit ef1727f2 authored by Hans-Christian Ebke's avatar Hans-Christian Ebke

Added mesh cast for meshes with different but identical traits.

git-svn-id: http://www.openmesh.org/svnrepo/OpenMesh/trunk@920 fdac6126-5c0c-442c-9429-916003d36597
parent b9d27321
......@@ -73,6 +73,8 @@ public:
//---------------------------------------------------------------- item types
typedef MeshItems MeshItemsT;
typedef Connectivity ConnectivityT;
typedef typename Connectivity::Vertex Vertex;
typedef typename Connectivity::Halfedge Halfedge;
typedef typename Connectivity::Edge Edge;
......
......@@ -124,6 +124,94 @@ struct FinalMeshItemsT
};
#ifndef DOXY_IGNORE_THIS
namespace {
namespace TM {
template<typename Lhs, typename Rhs> struct TypeEquality;
template<typename Lhs> struct TypeEquality<Lhs, Lhs> {};
template<typename LhsTraits, typename RhsTraits> struct ItemsEquality {
TypeEquality<typename LhsTraits::Point, typename RhsTraits::Point> te1;
TypeEquality<typename LhsTraits::Scalar, typename RhsTraits::Scalar> te2;
TypeEquality<typename LhsTraits::Normal, typename RhsTraits::Normal> te3;
TypeEquality<typename LhsTraits::Color, typename RhsTraits::Color> te4;
TypeEquality<typename LhsTraits::TexCoord1D, typename RhsTraits::TexCoord1D> te5;
TypeEquality<typename LhsTraits::TexCoord2D, typename RhsTraits::TexCoord2D> te6;
TypeEquality<typename LhsTraits::TexCoord3D, typename RhsTraits::TexCoord3D> te7;
TypeEquality<typename LhsTraits::TextureIndex, typename RhsTraits::TextureIndex> te8;
};
} /* namespace TM */
} /* anonymous namespace */
#endif
/**
* @brief Cast a mesh with different but identical traits into each other.
*
* Note that there exists a syntactically more convenient global method
* mesh_cast().
*
* Example:
* @code{.cpp}
* struct Traits1 : public OpenMesh::DefaultTraits {
* typedef Vec3d Point;
* }
* struct Traits2 : public OpenMesh::DefaultTraits {
* typedef Vec3d Point;
* }
* struct Traits3 : public OpenMesh::DefaultTraits {
* typedef Vec3f Point;
* }
*
* TriMesh_ArrayKernelT<TriTraits1> a;
* TriMesh_ArrayKernelT<TriTraits2> &b = MeshCast<TriMesh_ArrayKernelT<TriTraits2>&, TriMesh_ArrayKernelT<TriTraits1>&>::cast(a); // OK
* TriMesh_ArrayKernelT<TriTraits3> &c = MeshCast<TriMesh_ArrayKernelT<TriTraits3>&, TriMesh_ArrayKernelT<TriTraits1>&>::cast(a); // ERROR
* @endcode
*
* @see mesh_cast()
*
* @param rhs
* @return
*/
template<typename LhsMeshT, typename RhsMeshT> struct MeshCast;
template<typename LhsMeshT, typename RhsMeshT>
struct MeshCast<LhsMeshT&, RhsMeshT&> {
static LhsMeshT &cast(RhsMeshT &rhs) {
(void)sizeof(TM::ItemsEquality<typename LhsMeshT::MeshItemsT, typename RhsMeshT::MeshItemsT>);
(void)sizeof(TM::TypeEquality<typename LhsMeshT::ConnectivityT, typename RhsMeshT::ConnectivityT>);
return reinterpret_cast<LhsMeshT&>(rhs);
}
};
template<typename LhsMeshT, typename RhsMeshT>
struct MeshCast<const LhsMeshT&, const RhsMeshT&> {
static const LhsMeshT &cast(const RhsMeshT &rhs) {
(void)sizeof(TM::ItemsEquality<typename LhsMeshT::MeshItemsT, typename RhsMeshT::MeshItemsT>);
(void)sizeof(TM::TypeEquality<typename LhsMeshT::ConnectivityT, typename RhsMeshT::ConnectivityT>);
return reinterpret_cast<const LhsMeshT&>(rhs);
}
};
template<typename LhsMeshT, typename RhsMeshT>
struct MeshCast<LhsMeshT*, RhsMeshT*> {
static LhsMeshT *cast(RhsMeshT *rhs) {
(void)sizeof(TM::ItemsEquality<typename LhsMeshT::MeshItemsT, typename RhsMeshT::MeshItemsT>);
(void)sizeof(TM::TypeEquality<typename LhsMeshT::ConnectivityT, typename RhsMeshT::ConnectivityT>);
return reinterpret_cast<LhsMeshT*>(rhs);
}
};
template<typename LhsMeshT, typename RhsMeshT>
struct MeshCast<const LhsMeshT*, const RhsMeshT*> {
static const LhsMeshT *cast(const RhsMeshT *rhs) {
(void)sizeof(TM::ItemsEquality<typename LhsMeshT::MeshItemsT, typename RhsMeshT::MeshItemsT>);
(void)sizeof(TM::TypeEquality<typename LhsMeshT::ConnectivityT, typename RhsMeshT::ConnectivityT>);
return reinterpret_cast<const LhsMeshT*>(rhs);
}
};
//=============================================================================
} // namespace OpenMesh
//=============================================================================
......
......@@ -492,6 +492,55 @@ public:
inline void split(EdgeHandle _eh, VertexHandle _vh)
{ Kernel::split_edge(_eh, _vh); }
/**
* @brief Cast a mesh with different but identical traits into each other.
*
* Example:
* @code{.cpp}
* struct Traits1 : public OpenMesh::DefaultTraits {
* typedef Vec3d Point;
* }
* struct Traits2 : public OpenMesh::DefaultTraits {
* typedef Vec3d Point;
* }
* struct Traits3 : public OpenMesh::DefaultTraits {
* typedef Vec3f Point;
* }
*
* TriMesh_ArrayKernelT<TriTraits1> a;
* TriMesh_ArrayKernelT<TriTraits2> &b = mesh_cast<TriMesh_ArrayKernelT<TriTraits2>&>(a); // OK
* TriMesh_ArrayKernelT<TriTraits3> &c = mesh_cast<TriMesh_ArrayKernelT<TriTraits3>&>(a); // ERROR
* @endcode
*
* @see MeshCast
*
* @param rhs
* @return
*/
template<typename LHS>
friend
LHS mesh_cast(PolyMeshT &rhs) {
return MeshCast<LHS, PolyMeshT&>::cast(rhs);
}
template<typename LHS>
friend
LHS mesh_cast(PolyMeshT *rhs) {
return MeshCast<LHS, PolyMeshT*>::cast(rhs);
}
template<typename LHS>
friend
const LHS mesh_cast(const PolyMeshT &rhs) {
return MeshCast<LHS, const PolyMeshT&>::cast(rhs);
}
template<typename LHS>
friend
const LHS mesh_cast(const PolyMeshT *rhs) {
return MeshCast<LHS, const PolyMeshT*>::cast(rhs);
}
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment