refactor functionality. Types can now be registered

This commit is contained in:
Alexandra Heuschling
2020-12-21 15:22:46 +01:00
parent b9054f5e55
commit ef529c8b52
4 changed files with 205 additions and 56 deletions

View File

@@ -59,6 +59,8 @@
#include <OpenMesh/Core/IO/writer/OMWriter.hh>
#include <OpenMesh/Core/Utils/typename.hh>
#include <OpenMesh/Core/Utils/PropertyCreator.hh>
//=== NAMESPACES ==============================================================
@@ -806,50 +808,86 @@ size_t _OMReader_::restore_binary_custom_data(std::istream& _is, BaseProperty* _
//--------------------------------helper
void _OMReader_:: add_generic_property(OMFormat::Chunk::PropertyName& _property_type, BaseImporter& _bi) const
{
if(_property_type == get_string_for_type(bool()))
PropertyCreationManager& manager = PropertyCreationManager::instance();
switch (chunk_header_.entity_)
{
add_generic_property_aux<bool>(_bi);
}
else if(_property_type == get_string_for_type(char()))
case OMFormat::Chunk::Entity_Vertex:
{
add_generic_property_aux<char>(_bi);
manager.create_property<OpenMesh::VertexHandle>(*_bi.kernel(), _property_type, property_name_);
break;
}
else if(_property_type == get_string_for_type(double()))
case OMFormat::Chunk::Entity_Face:
{
add_generic_property_aux<double>(_bi);
manager.create_property<OpenMesh::FaceHandle>(*_bi.kernel(), _property_type, property_name_);
break;
}
else if(_property_type == get_string_for_type(float()))
case OMFormat::Chunk::Entity_Edge:
{
add_generic_property_aux<float>(_bi);
manager.create_property<OpenMesh::EdgeHandle>(*_bi.kernel(), _property_type, property_name_);
break;
}
else if(_property_type == get_string_for_type(int()))
case OMFormat::Chunk::Entity_Halfedge:
{
add_generic_property_aux<int>(_bi);
manager.create_property<OpenMesh::HalfedgeHandle>(*_bi.kernel(), _property_type, property_name_);
break;
}
else if(_property_type == get_string_for_type(long()))
case OMFormat::Chunk::Entity_Mesh:
{
add_generic_property_aux<long>(_bi);
manager.create_property<OpenMesh::MeshHandle>(*_bi.kernel(), _property_type, property_name_);
break;
}
else if(_property_type == get_string_for_type(short()))
{
add_generic_property_aux<short>(_bi);
}
else if(_property_type == get_string_for_type(uchar()))
{
add_generic_property_aux<uchar>(_bi);
}
else if(_property_type == get_string_for_type(uint()))
{
add_generic_property_aux<uint>(_bi);
}
else if(_property_type == get_string_for_type(ulong()))
{
add_generic_property_aux<ulong>(_bi);
}
else
{
check_all_vector_types(_property_type, _bi);
case OMFormat::Chunk::Entity_Sentinel:
;
break;
default:
;
}
// if(_property_type == get_string_for_type(bool()))
// {
// add_generic_property_aux<bool>(_bi);
// }
// else if(_property_type == get_string_for_type(char()))
// {
// add_generic_property_aux<char>(_bi);
// }
// else if(_property_type == get_string_for_type(double()))
// {
// add_generic_property_aux<double>(_bi);
// }
// else if(_property_type == get_string_for_type(float()))
// {
// add_generic_property_aux<float>(_bi);
// }
// else if(_property_type == get_string_for_type(int()))
// {
// add_generic_property_aux<int>(_bi);
// }
// else if(_property_type == get_string_for_type(long()))
// {
// add_generic_property_aux<long>(_bi);
// }
// else if(_property_type == get_string_for_type(short()))
// {
// add_generic_property_aux<short>(_bi);
// }
// else if(_property_type == get_string_for_type(uchar()))
// {
// add_generic_property_aux<uchar>(_bi);
// }
// else if(_property_type == get_string_for_type(uint()))
// {
// add_generic_property_aux<uint>(_bi);
// }
// else if(_property_type == get_string_for_type(ulong()))
// {
// add_generic_property_aux<ulong>(_bi);
// }
// else
// {
// check_all_vector_types(_property_type, _bi);
// }
}
void _OMReader_::check_all_vector_types(OMFormat::Chunk::PropertyName& _property_type, BaseImporter& _bi) const

View File

@@ -50,6 +50,13 @@
#include <string>
#include <memory>
#include <OpenMesh/Core/IO/importer/BaseImporter.hh>
#include <OpenMesh/Core/Mesh/Handles.hh>
#include <OpenMesh/Core/Geometry/VectorT.hh>
#include <OpenMesh/Core/Utils/typename.hh>
#include <vector>
namespace OpenMesh {
#define OM_CONCAT_IMPL(a, b) a##b
@@ -74,24 +81,24 @@ public:
bool can_you_create(const std::string &_type_name);
/// Create a vertex property on _mesh with name _property_name
virtual void create_vertex_property (PolyConnectivity& _mesh, const std::string& _property_name) = 0;
virtual void create_vertex_property (BaseKernel& _mesh, const std::string& _property_name) = 0;
/// Create a halfedge property on _mesh with name _property_name
virtual void create_halfedge_property(PolyConnectivity& _mesh, const std::string& _property_name) = 0;
virtual void create_halfedge_property(BaseKernel& _mesh, const std::string& _property_name) = 0;
/// Create an edge property on _mesh with name _property_name
virtual void create_edge_property (PolyConnectivity& _mesh, const std::string& _property_name) = 0;
virtual void create_edge_property (BaseKernel& _mesh, const std::string& _property_name) = 0;
/// Create a face property on _mesh with name _property_name
virtual void create_face_property (PolyConnectivity& _mesh, const std::string& _property_name) = 0;
virtual void create_face_property (BaseKernel& _mesh, const std::string& _property_name) = 0;
/// Create a mesh property on _mesh with name _property_name
virtual void create_mesh_property (PolyConnectivity& _mesh, const std::string& _property_name) = 0;
virtual void create_mesh_property (BaseKernel& _mesh, const std::string& _property_name) = 0;
/// Create a property for the element of type HandleT on _mesh with name _property_name
template <typename HandleT>
void create_property(PolyConnectivity& _mesh, const std::string& _property_name);
void create_property(BaseKernel& _mesh, const std::string& _property_name);
virtual ~PropertyCreator() {}
@@ -100,11 +107,11 @@ protected:
};
template <> inline void PropertyCreator::create_property<VertexHandle> (PolyConnectivity& _mesh, const std::string& _property_name) { create_vertex_property (_mesh, _property_name); }
template <> inline void PropertyCreator::create_property<HalfedgeHandle>(PolyConnectivity& _mesh, const std::string& _property_name) { create_halfedge_property(_mesh, _property_name); }
template <> inline void PropertyCreator::create_property<EdgeHandle> (PolyConnectivity& _mesh, const std::string& _property_name) { create_edge_property (_mesh, _property_name); }
template <> inline void PropertyCreator::create_property<FaceHandle> (PolyConnectivity& _mesh, const std::string& _property_name) { create_face_property (_mesh, _property_name); }
template <> inline void PropertyCreator::create_property<MeshHandle> (PolyConnectivity& _mesh, const std::string& _property_name) { create_mesh_property (_mesh, _property_name); }
template <> inline void PropertyCreator::create_property<VertexHandle> (BaseKernel& _mesh, const std::string& _property_name) { create_vertex_property (_mesh, _property_name); }
template <> inline void PropertyCreator::create_property<HalfedgeHandle>(BaseKernel& _mesh, const std::string& _property_name) { create_halfedge_property(_mesh, _property_name); }
template <> inline void PropertyCreator::create_property<EdgeHandle> (BaseKernel& _mesh, const std::string& _property_name) { create_edge_property (_mesh, _property_name); }
template <> inline void PropertyCreator::create_property<FaceHandle> (BaseKernel& _mesh, const std::string& _property_name) { create_face_property (_mesh, _property_name); }
template <> inline void PropertyCreator::create_property<MeshHandle> (BaseKernel& _mesh, const std::string& _property_name) { create_mesh_property (_mesh, _property_name); }
/// Helper class that contains the implementation of the create_<HandleT>_property methods.
/// Implementation is injected into PropertyCreatorT.
@@ -113,11 +120,16 @@ class PropertyCreatorImpl : public PropertyCreator
{
public:
std::string type_id_string() override { return get_type_name<typename PropertyCreatorT::type>(); }
void create_vertex_property (PolyConnectivity& _mesh, const std::string& _property_name) override { VProp<typename PropertyCreatorT::type> prop(_mesh, _property_name.c_str()); }
void create_halfedge_property(PolyConnectivity& _mesh, const std::string& _property_name) override { HProp<typename PropertyCreatorT::type> prop(_mesh, _property_name.c_str()); }
void create_edge_property (PolyConnectivity& _mesh, const std::string& _property_name) override { EProp<typename PropertyCreatorT::type> prop(_mesh, _property_name.c_str()); }
void create_face_property (PolyConnectivity& _mesh, const std::string& _property_name) override { FProp<typename PropertyCreatorT::type> prop(_mesh, _property_name.c_str()); }
void create_mesh_property (PolyConnectivity& _mesh, const std::string& _property_name) override { MProp<typename PropertyCreatorT::type> prop(_mesh, _property_name.c_str()); }
void create_vertex_property (BaseKernel& _mesh, const std::string& _property_name) override { VPropHandleT<typename PropertyCreatorT::type> prop;
_mesh.add_property(prop, _property_name); }
void create_halfedge_property(BaseKernel& _mesh, const std::string& _property_name) override { HPropHandleT<typename PropertyCreatorT::type> prop;
_mesh.add_property(prop, _property_name); }
void create_edge_property (BaseKernel& _mesh, const std::string& _property_name) override { EPropHandleT<typename PropertyCreatorT::type> prop;
_mesh.add_property(prop, _property_name); }
void create_face_property (BaseKernel& _mesh, const std::string& _property_name) override { FPropHandleT<typename PropertyCreatorT::type> prop;
_mesh.add_property(prop, _property_name); }
void create_mesh_property (BaseKernel& _mesh, const std::string& _property_name) override { MPropHandleT<typename PropertyCreatorT::type> prop;
_mesh.add_property(prop, _property_name); }
~PropertyCreatorImpl() override {}
protected:
@@ -132,9 +144,12 @@ class PropertyCreatorT : public PropertyCreatorImpl<PropertyCreatorT<T>>
};
}
///used to register custom type, where typename.hh does NOT provide a string for type recognition
#define OM_REGISTER_PROPERTY_TYPE(ClassName, TypeString) \
namespace OpenMesh { \
inline std::string get_string_for_type(ClassName){ return TypeString;} \
\
namespace { /* ensure internal linkage of class */ \
template <> \
class PropertyCreatorT<ClassName> : public PropertyCreatorImpl<PropertyCreatorT<ClassName>> \
@@ -154,6 +169,31 @@ public:
static PropertyCreatorT<ClassName> OM_CONCAT(property_creator_registration_object_, __LINE__); \
} \
///used to register custom type, where typename.hh does provide a string for type recognition
///
#define OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(ClassName) \
namespace OpenMesh { \
namespace { /* ensure internal linkage of class */ \
template <> \
class PropertyCreatorT<ClassName> : public PropertyCreatorImpl<PropertyCreatorT<ClassName>> \
{ \
public: \
using type = ClassName; \
std::string type_string() override { return OpenMesh::get_string_for_type(type()); } \
\
PropertyCreatorT() \
{ \
PropertyCreationManager::instance().register_property_creator(this); \
} \
~PropertyCreatorT() override {} \
}; \
} \
/* static to ensure internal linkage of object */ \
static PropertyCreatorT<ClassName> OM_CONCAT(property_creator_registration_object_, __LINE__); \
}
/** \brief Class for adding properties based on strings
*
* The PropertyCreationManager holds all PropertyCreators and dispatches the property creation
@@ -170,7 +210,7 @@ public:
static PropertyCreationManager& instance();
template <typename HandleT>
void create_property(PolyConnectivity& _mesh, const std::string& _type_name, const std::string& _property_name)
void create_property(BaseKernel& _mesh, const std::string& _type_name, const std::string& _property_name)
{
for (const auto& pc : property_creators_)
if (pc->can_you_create(_type_name))
@@ -214,13 +254,84 @@ private:
* For user defined types you need to register the type using OM_REGISTER_PROPERTY_TYPE(ClassName, TypeString)
* */
template <typename HandleT>
void create_property_from_string(PolyConnectivity& _mesh, const std::string& _type_name, const std::string& _property_name)
void create_property_from_string(BaseKernel& _mesh, const std::string& _type_name, const std::string& _property_name)
{
PropertyCreationManager::instance().create_property<HandleT>(_mesh, _type_name, _property_name);
}
} /* namespace OpenMesh */
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(OpenMesh::FaceHandle)
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(OpenMesh::EdgeHandle)
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(OpenMesh::HalfedgeHandle)
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(OpenMesh::VertexHandle)
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(bool)
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(char)
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(signed char)
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(double)
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(float)
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(int)
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(short)
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(unsigned char);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(unsigned int);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(unsigned short);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(unsigned long);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec1c);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec1uc);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec1s);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec1us);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec1i);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec1ui);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec1f);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec1d);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec2c);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec2uc);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec2s);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec2us);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec2i);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec2ui);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec2f);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec2d);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec3c);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec3uc);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec3s);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec3us);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec3i);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec3ui);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec3f);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec3d);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec4c);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec4uc);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec4s);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec4us);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec4i);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec4ui);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec4f);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec4d);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec5c);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec5uc);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec5s);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec5us);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec5i);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec5ui);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec5f);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec5d);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec6c);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec6uc);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec6s);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec6us);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec6i);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec6ui);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec6f);
OM_REGISTER_PROPERTY_TYPE_USING_DEFAULT_STRING(Vec6d);
OM_REGISTER_PROPERTY_TYPE(int, "int");

View File

@@ -9,7 +9,7 @@
#include <typeinfo>
#include <vector>
#include <OpenMesh/Core/Mesh/Handles.hh>
#include <OpenMesh/Core/Geometry/Vector11T.hh>
#include <OpenMesh/Core/Geometry/VectorT.hh>
namespace OpenMesh {

View File

@@ -1754,5 +1754,5 @@ TEST_F(OpenMeshReadWriteOM, PropertyFromString)
}
OM_REGISTER_PROPERTY_TYPE(double, "double")
//OM_REGISTER_PROPERTY_TYPE(double, "double")
OM_REGISTER_PROPERTY_TYPE(std::vector<float>, "std::vector<float>")