Commit a4f08955 authored by Sylvain Thery's avatar Sylvain Thery

Orbit templated attributeHandlerGen + type_code

parent 350e8781
......@@ -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_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
{
att_out[v] = smooth(map,v,att_in);
......
......@@ -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()
{
// declare a map to handle the mesh
......@@ -142,6 +166,8 @@ int main()
grid.embedIntoGrid(positionAtt, 1.,1.,0.);
VertexGeneric(myMap,positionAtt);
// ATTRIBUTE DECLARATION
// add an attribute of type float on orbit EDGE
......
......@@ -93,18 +93,71 @@ void quadranguleFaces(typename PFP::MAP& map, EMBV& attributs) ;
template <typename PFP, typename EMBV>
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
*/
//template <typename PFP, typename EMBV, typename EMB>
template <typename PFP, typename EMBV>
void LoopSubdivision(typename PFP::MAP& map, EMBV& attributs) ;
//template <typename PFP>
//void LoopSubdivision(typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3>& position) ;
template <typename PFP>
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:
* @param attribName nom 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
......@@ -417,6 +417,8 @@ public:
* ATTRIBUTES DATA ACCESS *
**************************************/
inline CGoGNCodeType getTypeCode(const std::string& attribName) const;
/**
* get an AttributeMultiVector
* @param attrIndex index of the attribute
......
......@@ -421,6 +421,17 @@ AttributeMultiVector<T>* AttributeContainer::getDataVector(const std::string& at
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)
{
unsigned int index = getAttributeIndex(attribName) ;
......
......@@ -38,6 +38,17 @@
namespace CGoGN
{
enum CGoGNCodeType
{CGoGNUNKNOWNTYPE=0,
CGoGNINT,CGoGNUINT,
CGoGNSHORT,CGoGNUSHORT,
CGoGNCHAR,CGoGNUCHAR,
CGoGNFLOAT,CGoGNDOUBLE,
CGoGNVEC2F,CGoGNVEC2D,
CGoGNVEC3F,CGoGNVEC3D,
CGoGNVEC4F,CGoGNVEC4D
};
class AttributeMultiVectorGen
{
protected:
......@@ -51,6 +62,11 @@ protected:
*/
std::string m_typeName;
/**
* Code of type
*/
CGoGNCodeType m_typeCode;
/**
* orbit of the attribute
*/
......@@ -102,6 +118,8 @@ public:
void setTypeName(const std::string& n);
CGoGNCodeType getTypeCode() const;
/**
* get block size
*/
......@@ -192,6 +210,8 @@ class AttributeMultiVector : public AttributeMultiVectorGen
*/
std::vector<T*> m_tableData;
inline void setTypeCode();
public:
AttributeMultiVector(const std::string& strName, const std::string& strType);
......
......@@ -21,6 +21,7 @@
* Contact information: cgogn@unistra.fr *
* *
*******************************************************************************/
#include "Geometry/vector_gen.h"
namespace CGoGN
{
......@@ -84,6 +85,10 @@ inline unsigned int AttributeMultiVectorGen::getBlockSize() const
return _BLOCKSIZE_ ;
}
inline CGoGNCodeType AttributeMultiVectorGen::getTypeCode() const
{
return m_typeCode;
}
/***************************************************************************************************/
/***************************************************************************************************/
......@@ -413,4 +418,90 @@ void AttributeMultiVector<T>::dump(unsigned int i) const
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
......@@ -72,8 +72,20 @@ public:
protected:
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
* Main available operations are:
......@@ -82,7 +94,7 @@ protected:
* - begin / end / next to manage indexing
*/
template <typename T, unsigned int ORB, typename MAP>
class AttributeHandler : public AttributeHandlerGen
class AttributeHandler : public AttributeHandlerOrbit<ORB>
{
protected:
// the map that contains the linked attribute
......@@ -95,7 +107,7 @@ protected:
public:
typedef T DATA_TYPE ;
static const unsigned int ORBIT = ORB;
// static const unsigned int ORBIT = ORB;
/**
* Default constructor
......@@ -253,30 +265,41 @@ public:
template <typename T, typename MAP>
using DartAttribute = AttributeHandler<T, DART, MAP>;
typedef AttributeHandlerOrbit<DART> DartAttributeGen;
/**
* c++11 shortcut for Vertex Attribute (Handler)
*/
template <typename T, typename MAP>
using VertexAttribute = AttributeHandler<T, VERTEX, MAP>;
typedef AttributeHandlerOrbit<VERTEX> VertexAttributeGen;
/**
* c++11 shortcut for Edge Attribute (Handler)
*/
template <typename T, typename MAP>
using EdgeAttribute = AttributeHandler<T, EDGE, MAP>;
typedef AttributeHandlerOrbit<EDGE> EdgeAttributeGen;
/**
* c++11 shortcut for Face Attribute (Handler)
*/
template <typename T, typename MAP>
using FaceAttribute = AttributeHandler<T, FACE, MAP>;
typedef AttributeHandlerOrbit<FACE> FaceAttributeGen;
/**
* c++11 shortcut for Volume Attribute (Handler)
*/
template <typename T, typename 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
}
template<unsigned int ORBIT>
inline bool checkAttributeHandlerOrbit(const AttributeHandlerGen& att)
{
return (dynamic_cast<const AttributeHandlerOrbit<ORBIT>*>(&att)!=NULL) != NULL;
}
} // namespace CGoGN
#include "Topology/generic/attributeHandler.hpp"
......
......@@ -58,62 +58,62 @@ inline void AttributeHandler<T, ORB, MAP>::unregisterFromMap()
template <typename T, unsigned int ORB, typename MAP>
AttributeHandler<T, ORB, MAP>::AttributeHandler() :
AttributeHandlerGen(false),
AttributeHandlerOrbit<ORB>(false),
m_map(NULL),
m_attrib(NULL)
{}
template <typename T, unsigned int ORB, typename MAP>
AttributeHandler<T, ORB, MAP>::AttributeHandler(MAP* m, AttributeMultiVector<T>* amv) :
AttributeHandlerGen(false),
AttributeHandlerOrbit<ORB>(false),
m_map(m),
m_attrib(amv)
{
if(m != NULL && amv != NULL && amv->getIndex() != AttributeContainer::UNKNOWN)
{
assert(ORB == amv->getOrbit() || !"AttributeHandler: orbit incompatibility") ;
valid = true ;
this->valid = true ;
registerInMap() ;
}
else
valid = false ;
this->valid = false ;
}
template <typename T, unsigned int ORB, typename MAP>
AttributeHandler<T, ORB, MAP>::AttributeHandler(const AttributeHandler<T, ORB, MAP>& ta) :
AttributeHandlerGen(ta.valid),
AttributeHandlerOrbit<ORB>(ta.valid),
m_map(ta.m_map),
m_attrib(ta.m_attrib)
{
if(valid)
if(this->valid)
registerInMap() ;
}
template <typename T, unsigned int ORB, typename MAP>
template <unsigned int ORBIT2>
AttributeHandler<T, ORB, MAP>::AttributeHandler(const AttributeHandler<T, ORBIT2, MAP>& h) :
AttributeHandlerGen(h.valid),
AttributeHandlerOrbit<ORB>(h.valid),
m_map(h.m_map),
m_attrib(h.m_attrib)
{
if(m_attrib->getOrbit() == ORBIT2)
{
if(valid)
if(this->valid)
registerInMap() ;
}
else
valid = false;
this->valid = false;
}
template <typename T, unsigned int ORB, typename MAP>
inline AttributeHandler<T, ORB, MAP>& AttributeHandler<T, ORB, MAP>::operator=(const AttributeHandler<T, ORB, MAP>& ta)
{
if(valid)
if(this->valid)
unregisterFromMap() ;
m_map = ta.m_map ;
m_attrib = ta.m_attrib ;
valid = ta.valid ;
if(valid)
this->valid = ta.valid ;
if(this->valid)
registerInMap() ;
return *this ;
}
......@@ -122,12 +122,12 @@ template <typename T, unsigned int ORB, typename MAP>
template <unsigned int ORBIT2>
inline AttributeHandler<T, ORB, MAP>& AttributeHandler<T, ORB, MAP>::operator=(const AttributeHandler<T, ORBIT2, MAP>& ta)
{
if(valid)
if(this->valid)
unregisterFromMap() ;
m_map = ta.map() ;
m_attrib = ta.getDataVector() ;
valid = ta.isValid() ;
if(valid)
this->valid = ta.isValid() ;
if(this->valid)
registerInMap() ;
return *this ;
}
......@@ -135,7 +135,7 @@ inline AttributeHandler<T, ORB, MAP>& AttributeHandler<T, ORB, MAP>::operator=(c
template <typename T, unsigned int ORB, typename MAP>
AttributeHandler<T, ORB, MAP>::~AttributeHandler()
{
if(valid)
if(this->valid)
unregisterFromMap() ;
}
......@@ -191,7 +191,7 @@ inline unsigned int AttributeHandler<T, ORB, MAP>::nbElements() const
template <typename T, unsigned int ORB, typename MAP>
inline T& AttributeHandler<T, ORB, MAP>::operator[](Cell<ORB> c)
{
assert(valid || !"Invalid AttributeHandler") ;
assert(this->valid || !"Invalid AttributeHandler") ;
unsigned int a = m_map->getEmbedding(c) ;
if (a == EMBNULL)
......@@ -203,7 +203,7 @@ inline T& AttributeHandler<T, ORB, MAP>::operator[](Cell<ORB> c)
template <typename T, unsigned int ORB, typename MAP>
inline const T& AttributeHandler<T, ORB, MAP>::operator[](Cell<ORB> c) const
{
assert(valid || !"Invalid AttributeHandler") ;
assert(this->valid || !"Invalid AttributeHandler") ;
unsigned int a = m_map->getEmbedding(c) ;
return m_attrib->operator[](a) ;
}
......@@ -211,21 +211,21 @@ inline const T& AttributeHandler<T, ORB, MAP>::operator[](Cell<ORB> c) const
template <typename T, unsigned int ORB, typename MAP>
inline T& AttributeHandler<T, ORB, MAP>::operator[](unsigned int a)
{
assert(valid || !"Invalid AttributeHandler") ;
assert(this->valid || !"Invalid AttributeHandler") ;
return m_attrib->operator[](a) ;
}
template <typename T, unsigned int ORB, typename MAP>
inline const T& AttributeHandler<T, ORB, MAP>::operator[](unsigned int a) const
{
assert(valid || !"Invalid AttributeHandler") ;
assert(this->valid || !"Invalid AttributeHandler") ;
return m_attrib->operator[](a) ;
}
template <typename T, unsigned int ORB, typename MAP>
inline unsigned int AttributeHandler<T, ORB, MAP>::insert(const T& elt)
{
assert(valid || !"Invalid AttributeHandler") ;
assert(this->valid || !"Invalid AttributeHandler") ;
unsigned int idx = m_map->template getAttributeContainer<ORB>().insertLine() ;
m_attrib->operator[](idx) = elt ;
return idx ;
......@@ -234,7 +234,7 @@ inline unsigned int AttributeHandler<T, ORB, MAP>::insert(const T& elt)
template <typename T, unsigned int ORB, typename MAP>
inline unsigned int AttributeHandler<T, ORB, MAP>::newElt()
{
assert(valid || !"Invalid AttributeHandler") ;
assert(this->valid || !"Invalid AttributeHandler") ;
unsigned int idx = m_map->template getAttributeContainer<ORB>().insertLine() ;
return idx ;
}
......@@ -249,21 +249,21 @@ inline void AttributeHandler<T, ORB, MAP>::setAllValues(const T& v)
template <typename T, unsigned int ORB, typename MAP>
inline unsigned int AttributeHandler<T, ORB, MAP>::begin() const
{
assert(valid || !"Invalid AttributeHandler") ;
assert(this->valid || !"Invalid AttributeHandler") ;
return m_map->template getAttributeContainer<ORB>().begin() ;
}
template <typename T, unsigned int ORB, typename MAP>
inline unsigned int AttributeHandler<T, ORB, MAP>::end() const
{
assert(valid || !"Invalid AttributeHandler") ;
assert(this->valid || !"Invalid AttributeHandler") ;
return m_map->template getAttributeContainer<ORB>().end() ;
}
template <typename T, unsigned int ORB, typename MAP>
inline void AttributeHandler<T, ORB, MAP>::next(unsigned int& iter) const
{
assert(valid || !"Invalid AttributeHandler") ;
assert(this->valid || !"Invalid AttributeHandler") ;
m_map->template getAttributeContainer<ORB>().next(iter) ;
}
......
......@@ -149,6 +149,15 @@ public:
template <typename T, unsigned int ORBIT, typename MAP>
inline AttributeHandler<T, ORBIT, MAP> getAttribute(const std::string& nameAttr) ;
/**
* @brief get attribute type code
* @param nameAttr name of attribute
* @return code enum
*/
template <unsigned int ORBIT>
inline CGoGNCodeType getAttributeTypeCode(const std::string& nameAttr);
/**
* check if an attribute exist ( get, test if valid and add if necessary)
* @param nameAttr attribute name
......
......@@ -202,6 +202,14 @@ inline AttributeHandler<T ,ORBIT, MAP> MapCommon<MAP_IMPL>::getAttribute(const s
return AttributeHandler<T, ORBIT, MAP>(static_cast<MAP*>(this), amv) ;
}
template <typename MAP_IMPL>
template < unsigned int ORBIT>
inline CGoGNCodeType MapCommon<MAP_IMPL>::getAttributeTypeCode(const std::string& nameAttr)
{
return this->m_attribs[ORBIT].getTypeCode(nameAttr);
}
template <typename MAP_IMPL>
template <typename T, unsigned int ORBIT, typename MAP>
inline AttributeHandler<T ,ORBIT, MAP> MapCommon<MAP_IMPL>::checkAttribute(const std::string& nameAttr)
......
......@@ -88,7 +88,7 @@ struct RefCompo3Type
template <typename T1, typename T2, typename T3, unsigned int ORB, typename MAP>
class Cell3Attributes: public AttributeHandlerGen
class Cell3Attributes: public AttributeHandlerOrbit<ORB>
{
AttributeHandler<T1, ORB, MAP>& m_h1;
......@@ -101,7 +101,7 @@ public:
Cell3Attributes(AttributeHandler<T1, ORB, MAP>& h1,
AttributeHandler<T2, ORB, MAP>& h2,
AttributeHandler<T3, ORB, MAP>& h3):
AttributeHandlerGen(true),
AttributeHandlerOrbit<ORB>(true),
m_h1(h1), m_h2(h2), m_h3(h3) {}
static const unsigned int ORBIT = ORB;
......
......@@ -91,7 +91,7 @@ struct RefCompo4Type
//template <typename T1, typename T2, typename T3, typename T4>
//class Vertex4Attributes
template <typename T1, typename T2, typename T3, typename T4, unsigned int ORB, typename MAP>
class Cell4Attributes: public AttributeHandlerGen
class Cell4Attributes: public AttributeHandlerOrbit<ORB>
{
AttributeHandler<T1, ORB, MAP>& m_h1;
AttributeHandler<T2, ORB, MAP>& m_h2;
......@@ -106,7 +106,7 @@ public:
AttributeHandler<T2, ORB, MAP>& h2,
AttributeHandler<T3, ORB, MAP>& h3,
AttributeHandler<T4, ORB, MAP>& h4) :
AttributeHandlerGen(true),
AttributeHandlerOrbit<ORB>(true),
m_h1(h1), m_h2(h2), m_h3(h3), m_h4(h4) {}
static const unsigned int ORBIT = ORB;
......
......@@ -276,7 +276,7 @@ double length(const T& v)
template <typename T1, typename T2, unsigned int ORB, typename MAP>
class Cell2Attributes: public AttributeHandlerGen
class Cell2Attributes: public AttributeHandlerOrbit<ORB>
{
AttributeHandler<T1, ORB, MAP>& m_h1;
AttributeHandler<T2, ORB, MAP>& m_h2;
......@@ -285,7 +285,7 @@ public:
typedef RefCompo2Type<T1,T2> REF_DATA_TYPE;
Cell2Attributes(AttributeHandler<T1, ORB, MAP>& h1, AttributeHandler<T2, ORB, MAP>& h2):
AttributeHandlerGen(true),
AttributeHandlerOrbit<ORB>(true),
m_h1(h1), m_h2(h2) {}
static const unsigned int ORBIT = ORB;
......
......@@ -68,7 +68,7 @@ AttributeContainer::~AttributeContainer()
* INFO ABOUT ATTRIBUTES *
**************************************/
unsigned int AttributeContainer::getAttributeIndex(const std::string& attribName)
unsigned int AttributeContainer::getAttributeIndex(const std::string& attribName) const
{
unsigned int index ;
bool found = false ;
......
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