Commit a4f08955 authored by Sylvain Thery's avatar Sylvain Thery
Browse files

Orbit templated attributeHandlerGen + type_code

parent 350e8781
...@@ -80,6 +80,13 @@ void applySmooth(MAP& map, const ATT& att_in, ATT& att_out) ...@@ -80,6 +80,13 @@ void applySmooth(MAP& map, const ATT& att_in, ATT& att_out)
// check at compile if ATT is an AttributeHandler on orbit VERTEX // check at compile if ATT is an AttributeHandler on orbit VERTEX
CHECK_ATTRIBUTEHANDLER_ORBIT(ATT, VERTEX); CHECK_ATTRIBUTEHANDLER_ORBIT(ATT, VERTEX);
// or check at runtime(take care of const!)
if (! checkAttributeHandlerOrbit<VERTEX>(att_in))
{
CGoGNerr << "function applySmooth work only with VertexAttributes !" << CGoGNendl;
return;
}
foreach_cell<VERTEX>(map,[&](Vertex v) // for all vertex v of map do foreach_cell<VERTEX>(map,[&](Vertex v) // for all vertex v of map do
{ {
att_out[v] = smooth(map,v,att_in); att_out[v] = smooth(map,v,att_in);
......
...@@ -125,6 +125,30 @@ void dumpAttribute(const ATTRIB& attr) ...@@ -125,6 +125,30 @@ void dumpAttribute(const ATTRIB& attr)
} }
//function that apply on vertice with templated attribute type
template<typename T>
void VertexTyped(MAP& map, T& va)
{
foreach_cell<VERTEX>(map,[&](Vertex v) // for all vertices
{
va[v] = 1.1 * va[v];
});
}
// version that take a VertexAttribute, check type at runtime and call instancied template version
void VertexGeneric(MAP& map, VertexAttributeGen& vg)
{
auto va3 = dynamic_cast<VertexAttribute<VEC3, MAP>*>(&vg);
if (va3 != NULL)
return VertexTyped(map,*va3);
auto vaf = dynamic_cast<VertexAttribute<float, MAP>*>(&vg);
if (vaf != NULL)
return VertexTyped(map,*vaf);
}
int main() int main()
{ {
// declare a map to handle the mesh // declare a map to handle the mesh
...@@ -142,6 +166,8 @@ int main() ...@@ -142,6 +166,8 @@ int main()
grid.embedIntoGrid(positionAtt, 1.,1.,0.); grid.embedIntoGrid(positionAtt, 1.,1.,0.);
VertexGeneric(myMap,positionAtt);
// ATTRIBUTE DECLARATION // ATTRIBUTE DECLARATION
// add an attribute of type float on orbit EDGE // add an attribute of type float on orbit EDGE
......
...@@ -93,18 +93,71 @@ void quadranguleFaces(typename PFP::MAP& map, EMBV& attributs) ; ...@@ -93,18 +93,71 @@ void quadranguleFaces(typename PFP::MAP& map, EMBV& attributs) ;
template <typename PFP, typename EMBV> template <typename PFP, typename EMBV>
void CatmullClarkSubdivision(typename PFP::MAP& map, EMBV& attributs) ; void CatmullClarkSubdivision(typename PFP::MAP& map, EMBV& attributs) ;
//template <typename PFP>
//void CatmullClarkSubdivision(typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3>& position) ;
/** /**
* Loop subdivision scheme * Loop subdivision scheme
*/ */
//template <typename PFP, typename EMBV, typename EMB>
template <typename PFP, typename EMBV> template <typename PFP, typename EMBV>
void LoopSubdivision(typename PFP::MAP& map, EMBV& attributs) ; void LoopSubdivision(typename PFP::MAP& map, EMBV& attributs) ;
//template <typename PFP> template <typename PFP>
//void LoopSubdivision(typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3>& position) ; void LoopSubdivisionGen(typename PFP::MAP& map, VertexAttributeGen& attrib)
{
auto va3 = dynamic_cast<VertexAttribute<typename PFP::VEC3, typename PFP::MAP>*>(&attrib);
if (va3 != NULL)
return LoopSubdivision<PFP>(map,*va3);
auto va4 = dynamic_cast<VertexAttribute<typename PFP::VEC4, typename PFP::MAP>*>(&attrib);
if (va4 != NULL)
return LoopSubdivision<PFP>(map,*va4);
auto var = dynamic_cast<VertexAttribute<typename PFP::REAL, typename PFP::MAP>*>(&attrib);
if (var != NULL)
return LoopSubdivision<PFP>(map,*var);
CGoGNerr << "LoopSubdivision not supported on attribute of type "<< attrib.typeName() << CGoGNendl;
}
/**
* Loop typed version with attribute name parameter
*/
template <typename PFP, typename T>
inline void LoopSubdivisionAttribNameTyped(typename PFP::MAP& map, const std::string& nameAttrib)
{
VertexAttribute<T,typename PFP::MAP> va = map.template getAttribute<T,VERTEX,typename PFP::MAP>(nameAttrib) ;
}
/**
* Loop genereo version with attribute name parameter
*/
template <typename PFP>
void LoopSubdivisionAttribName(typename PFP::MAP& map, const std::string& nameAttrib)
{
typedef typename PFP::MAP MAP;
switch(map.template getAttributeTypeCode<VERTEX>(nameAttrib))
{
case CGoGNFLOAT:
return LoopSubdivisionAttribNameTyped<PFP,float>(map,nameAttrib);
break;
case CGoGNDOUBLE:
return LoopSubdivisionAttribNameTyped<PFP,double>(map,nameAttrib);
break;
case CGoGNVEC3F:
return LoopSubdivisionAttribNameTyped<PFP,Geom::Vec3f>(map,nameAttrib);
break;
case CGoGNVEC3D:
return LoopSubdivisionAttribNameTyped<PFP,Geom::Vec3d>(map,nameAttrib);
break;
case CGoGNVEC4F:
return LoopSubdivisionAttribNameTyped<PFP,Geom::Vec4f>(map,nameAttrib);
break;
case CGoGNVEC4D:
return LoopSubdivisionAttribNameTyped<PFP,Geom::Vec4d>(map,nameAttrib);
break;
}
CGoGNerr << "LoopSubdivision not supported on attribute "<< nameAttrib << CGoGNendl;
}
/** /**
......
...@@ -287,7 +287,7 @@ public: ...@@ -287,7 +287,7 @@ public:
* @param attribName nom de l'attribut * @param attribName nom de l'attribut
* @return l'indice de l'attribut * @return l'indice de l'attribut
*/ */
unsigned int getAttributeIndex(const std::string& attribName); unsigned int getAttributeIndex(const std::string& attribName) const;
/** /**
* get the name of an attribute, given its index in the container * get the name of an attribute, given its index in the container
...@@ -417,6 +417,8 @@ public: ...@@ -417,6 +417,8 @@ public:
* ATTRIBUTES DATA ACCESS * * ATTRIBUTES DATA ACCESS *
**************************************/ **************************************/
inline CGoGNCodeType getTypeCode(const std::string& attribName) const;
/** /**
* get an AttributeMultiVector * get an AttributeMultiVector
* @param attrIndex index of the attribute * @param attrIndex index of the attribute
......
...@@ -421,6 +421,17 @@ AttributeMultiVector<T>* AttributeContainer::getDataVector(const std::string& at ...@@ -421,6 +421,17 @@ AttributeMultiVector<T>* AttributeContainer::getDataVector(const std::string& at
return atm; return atm;
} }
inline CGoGNCodeType AttributeContainer::getTypeCode(const std::string& attribName) const
{
unsigned int index = getAttributeIndex(attribName) ;
if(index == UNKNOWN)
return CGoGNUNKNOWNTYPE ;
return m_tableAttribs[index]->getTypeCode();
}
inline AttributeMultiVectorGen* AttributeContainer::getVirtualDataVector(const std::string& attribName) inline AttributeMultiVectorGen* AttributeContainer::getVirtualDataVector(const std::string& attribName)
{ {
unsigned int index = getAttributeIndex(attribName) ; unsigned int index = getAttributeIndex(attribName) ;
......
...@@ -38,6 +38,17 @@ ...@@ -38,6 +38,17 @@
namespace CGoGN namespace CGoGN
{ {
enum CGoGNCodeType
{CGoGNUNKNOWNTYPE=0,
CGoGNINT,CGoGNUINT,
CGoGNSHORT,CGoGNUSHORT,
CGoGNCHAR,CGoGNUCHAR,
CGoGNFLOAT,CGoGNDOUBLE,
CGoGNVEC2F,CGoGNVEC2D,
CGoGNVEC3F,CGoGNVEC3D,
CGoGNVEC4F,CGoGNVEC4D
};
class AttributeMultiVectorGen class AttributeMultiVectorGen
{ {
protected: protected:
...@@ -51,6 +62,11 @@ protected: ...@@ -51,6 +62,11 @@ protected:
*/ */
std::string m_typeName; std::string m_typeName;
/**
* Code of type
*/
CGoGNCodeType m_typeCode;
/** /**
* orbit of the attribute * orbit of the attribute
*/ */
...@@ -102,6 +118,8 @@ public: ...@@ -102,6 +118,8 @@ public:
void setTypeName(const std::string& n); void setTypeName(const std::string& n);
CGoGNCodeType getTypeCode() const;
/** /**
* get block size * get block size
*/ */
...@@ -192,6 +210,8 @@ class AttributeMultiVector : public AttributeMultiVectorGen ...@@ -192,6 +210,8 @@ class AttributeMultiVector : public AttributeMultiVectorGen
*/ */
std::vector<T*> m_tableData; std::vector<T*> m_tableData;
inline void setTypeCode();
public: public:
AttributeMultiVector(const std::string& strName, const std::string& strType); AttributeMultiVector(const std::string& strName, const std::string& strType);
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
* Contact information: cgogn@unistra.fr * * Contact information: cgogn@unistra.fr *
* * * *
*******************************************************************************/ *******************************************************************************/
#include "Geometry/vector_gen.h"
namespace CGoGN namespace CGoGN
{ {
...@@ -84,6 +85,10 @@ inline unsigned int AttributeMultiVectorGen::getBlockSize() const ...@@ -84,6 +85,10 @@ inline unsigned int AttributeMultiVectorGen::getBlockSize() const
return _BLOCKSIZE_ ; return _BLOCKSIZE_ ;
} }
inline CGoGNCodeType AttributeMultiVectorGen::getTypeCode() const
{
return m_typeCode;
}
/***************************************************************************************************/ /***************************************************************************************************/
/***************************************************************************************************/ /***************************************************************************************************/
...@@ -413,4 +418,90 @@ void AttributeMultiVector<T>::dump(unsigned int i) const ...@@ -413,4 +418,90 @@ void AttributeMultiVector<T>::dump(unsigned int i) const
CGoGNout << this->operator[](i); CGoGNout << this->operator[](i);
} }
template <typename T>
inline void AttributeMultiVector<T>::setTypeCode()
{
m_typeCode = CGoGNUNKNOWNTYPE;
}
template <>
inline void AttributeMultiVector<int>::setTypeCode()
{
m_typeCode = CGoGNINT;
}
template <>
inline void AttributeMultiVector<unsigned int>::setTypeCode()
{
m_typeCode = CGoGNUINT;
}
template <>
inline void AttributeMultiVector<short>::setTypeCode()
{
m_typeCode = CGoGNSHORT;
}
template <>
inline void AttributeMultiVector<unsigned short>::setTypeCode()
{
m_typeCode = CGoGNUSHORT;
}
template <>
inline void AttributeMultiVector<char>::setTypeCode()
{
m_typeCode = CGoGNCHAR;
}
template <>
inline void AttributeMultiVector<unsigned char>::setTypeCode()
{
m_typeCode = CGoGNUCHAR;
}
template <>
inline void AttributeMultiVector<float>::setTypeCode()
{
m_typeCode = CGoGNFLOAT;
}
template <>
inline void AttributeMultiVector<double>::setTypeCode()
{
m_typeCode = CGoGNDOUBLE;
}
template <>
inline void AttributeMultiVector<Geom::Vec2f>::setTypeCode()
{
m_typeCode = CGoGNVEC2F;
}
template <>
inline void AttributeMultiVector<Geom::Vec2d>::setTypeCode()
{
m_typeCode = CGoGNVEC2D;
}
template <>
inline void AttributeMultiVector<Geom::Vec3f>::setTypeCode()
{
m_typeCode = CGoGNVEC3F;
}
template <>
inline void AttributeMultiVector<Geom::Vec3d>::setTypeCode()
{
m_typeCode = CGoGNVEC3D;
}
template <>
inline void AttributeMultiVector<Geom::Vec4f>::setTypeCode()
{
m_typeCode = CGoGNVEC4F;
}
template <>
inline void AttributeMultiVector<Geom::Vec4d>::setTypeCode()
{
m_typeCode = CGoGNVEC4D;
}
} // namespace CGoGN } // namespace CGoGN
...@@ -72,8 +72,20 @@ public: ...@@ -72,8 +72,20 @@ public:
protected: protected:
void setInvalid() { valid = false ; } void setInvalid() { valid = false ; }
void setValid() { valid = true ; }
} ; } ;
template <unsigned int ORB>
class AttributeHandlerOrbit: public AttributeHandlerGen
{
public:
AttributeHandlerOrbit(bool v) :
AttributeHandlerGen(v)
{}
static const unsigned int ORBIT = ORB;
};
/** /**
* Class that create an access-table to an existing attribute * Class that create an access-table to an existing attribute
* Main available operations are: * Main available operations are:
...@@ -82,7 +94,7 @@ protected: ...@@ -82,7 +94,7 @@ protected:
* - begin / end / next to manage indexing * - begin / end / next to manage indexing
*/ */
template <typename T, unsigned int ORB, typename MAP> template <typename T, unsigned int ORB, typename MAP>
class AttributeHandler : public AttributeHandlerGen class AttributeHandler : public AttributeHandlerOrbit<ORB>
{ {
protected: protected:
// the map that contains the linked attribute // the map that contains the linked attribute
...@@ -95,7 +107,7 @@ protected: ...@@ -95,7 +107,7 @@ protected:
public: public:
typedef T DATA_TYPE ; typedef T DATA_TYPE ;
static const unsigned int ORBIT = ORB; // static const unsigned int ORBIT = ORB;
/** /**
* Default constructor * Default constructor
...@@ -253,30 +265,41 @@ public: ...@@ -253,30 +265,41 @@ public:
template <typename T, typename MAP> template <typename T, typename MAP>
using DartAttribute = AttributeHandler<T, DART, MAP>; using DartAttribute = AttributeHandler<T, DART, MAP>;
typedef AttributeHandlerOrbit<DART> DartAttributeGen;
/** /**
* c++11 shortcut for Vertex Attribute (Handler) * c++11 shortcut for Vertex Attribute (Handler)
*/ */
template <typename T, typename MAP> template <typename T, typename MAP>
using VertexAttribute = AttributeHandler<T, VERTEX, MAP>; using VertexAttribute = AttributeHandler<T, VERTEX, MAP>;
typedef AttributeHandlerOrbit<VERTEX> VertexAttributeGen;
/** /**
* c++11 shortcut for Edge Attribute (Handler) * c++11 shortcut for Edge Attribute (Handler)
*/ */
template <typename T, typename MAP> template <typename T, typename MAP>
using EdgeAttribute = AttributeHandler<T, EDGE, MAP>; using EdgeAttribute = AttributeHandler<T, EDGE, MAP>;
typedef AttributeHandlerOrbit<EDGE> EdgeAttributeGen;
/** /**
* c++11 shortcut for Face Attribute (Handler) * c++11 shortcut for Face Attribute (Handler)
*/ */
template <typename T, typename MAP> template <typename T, typename MAP>
using FaceAttribute = AttributeHandler<T, FACE, MAP>; using FaceAttribute = AttributeHandler<T, FACE, MAP>;
typedef AttributeHandlerOrbit<FACE> FaceAttributeGen;
/** /**
* c++11 shortcut for Volume Attribute (Handler) * c++11 shortcut for Volume Attribute (Handler)
*/ */
template <typename T, typename MAP> template <typename T, typename MAP>
using VolumeAttribute = AttributeHandler<T, VOLUME, MAP>; using VolumeAttribute = AttributeHandler<T, VOLUME, MAP>;
typedef AttributeHandlerOrbit<VOLUME> VolumeAttributeGen;
...@@ -309,6 +332,12 @@ void foreach_attribute(ATTR& attribute, FUNC func, unsigned int nbth = NumberOfT ...@@ -309,6 +332,12 @@ void foreach_attribute(ATTR& attribute, FUNC func, unsigned int nbth = NumberOfT
} }
template<unsigned int ORBIT>
inline bool checkAttributeHandlerOrbit(const AttributeHandlerGen& att)
{
return (dynamic_cast<const AttributeHandlerOrbit<ORBIT>*>(&att)!=NULL) != NULL;
}
} // namespace CGoGN } // namespace CGoGN
#include "Topology/generic/attributeHandler.hpp" #include "Topology/generic/attributeHandler.hpp"
......
...@@ -58,62 +58,62 @@ inline void AttributeHandler<T, ORB, MAP>::unregisterFromMap() ...@@ -58,62 +58,62 @@ inline void AttributeHandler<T, ORB, MAP>::unregisterFromMap()
template <typename T, unsigned int ORB, typename MAP> template <typename T, unsigned int ORB, typename MAP>
AttributeHandler<T, ORB, MAP>::AttributeHandler() : AttributeHandler<T, ORB, MAP>::AttributeHandler() :
AttributeHandlerGen(false), AttributeHandlerOrbit<ORB>(false),
m_map(NULL), m_map(NULL),
m_attrib(NULL) m_attrib(NULL)
{} {}
template <typename T, unsigned int ORB, typename MAP> template <typename T, unsigned int ORB, typename MAP>
AttributeHandler<T, ORB, MAP>::AttributeHandler(MAP* m, AttributeMultiVector<T>* amv) : AttributeHandler<T, ORB, MAP>::AttributeHandler(MAP* m, AttributeMultiVector<T>* amv) :
AttributeHandlerGen(false), AttributeHandlerOrbit<ORB>(false),
m_map(m), m_map(m),
m_attrib(amv) m_attrib(amv)
{ {
if(m != NULL && amv != NULL && amv->getIndex() != AttributeContainer::UNKNOWN) if(m != NULL && amv != NULL && amv->getIndex() != AttributeContainer::UNKNOWN)
{ {
assert(ORB == amv->getOrbit() || !"AttributeHandler: orbit incompatibility") ; assert(ORB == amv->getOrbit() || !"AttributeHandler: orbit incompatibility") ;
valid = true ; this->valid = true ;
registerInMap() ; registerInMap() ;
} }
else else
valid = false ; this->valid = false ;
} }
template <typename T, unsigned int ORB, typename MAP> template <typename T, unsigned int ORB, typename MAP>
AttributeHandler<T, ORB, MAP>::AttributeHandler(const AttributeHandler<T, ORB, MAP>& ta) : AttributeHandler<T, ORB, MAP>::AttributeHandler(const AttributeHandler<T, ORB, MAP>& ta) :
AttributeHandlerGen(ta.valid), AttributeHandlerOrbit<ORB>(ta.valid),
m_map(ta.m_map), m_map(ta.m_map),
m_attrib(ta.m_attrib) m_attrib(ta.m_attrib)
{ {
if(valid) if(this->valid)
registerInMap() ; registerInMap() ;
} }
template <typename T, unsigned int ORB, typename MAP> template <typename T, unsigned int ORB, typename MAP>
template <unsigned int ORBIT2> template <unsigned int ORBIT2>
AttributeHandler<T, ORB, MAP>::AttributeHandler(const AttributeHandler<T, ORBIT2, MAP>& h) : AttributeHandler<T, ORB, MAP>::AttributeHandler(const AttributeHandler<T, ORBIT2, MAP>& h) :
AttributeHandlerGen(h.valid), AttributeHandlerOrbit<ORB>(h.valid),
m_map(h.m_map), m_map(h.m_map),