namespace X3DTK {
namespace MESH {

template<class MData, class VData, class EData, class FData, bool RW>
TemplateMeshSimplifierStateVariables<MData, VData, EData, FData, RW>::TemplateMeshSimplifierStateVariables()
: StateVariables(), _mesh(0)
{
}

template<class MData, class VData, class EData, class FData, bool RW>
void TemplateMeshSimplifierStateVariables<MData, VData, EData, FData, RW>::init()
{
  _mesh = new TemplateMesh<MData, VData, EData, FData, RW>();
  _mesh->setChild(new TemplateVertex<MData, VData, EData, FData, RW>());
  
  #ifdef TEMPLATE_SPECIALIZATION_SUPPORTED
  if (MData::template find<MESH::MeshNormalData>()) 
  {
    _mesh->template ogetData<MESH::MeshNormalData>().setNormal(true);
    _mesh->template ogetData<MESH::MeshNormalData>().setNormalPerVertex(true);
  }
  
  if (MData::template find<MESH::MeshColorData>()) 
  {
    _mesh->template ogetData<MESH::MeshColorData>().setColor(true);
    _mesh->template ogetData<MESH::MeshColorData>().setColorPerVertex(true);
    _mesh->template ogetData<MESH::MeshColorData>().setRGBA(true);
  }
  
  #else
  
  _mesh->data().setNormal(true);
  _mesh->data().setNormalPerVertex(true);
  
  _mesh->data().setColor(true);
  _mesh->data().setColorPerVertex(true);
  _mesh->data().setRGBA(true);
  #endif
}

template<class MData, class VData, class EData, class FData, bool RW>
void TemplateMeshSimplifierStateVariables<MData, VData, EData, FData, RW>::finish()
{
  _nodeList.clear();
}

template<class MData, class VData, class EData, class FData, bool RW>
void TemplateMeshSimplifierStateVariables<MData, VData, EData, FData, RW>::addNode(SFNode N)
{
  _nodeList.push_back(N);
}

template<class MData, class VData, class EData, class FData, bool RW>
bool TemplateMeshSimplifierStateVariables<MData, VData, EData, FData, RW>::getNode(SFNode N) const
{
  std::list<SFNode>::const_iterator it = std::find(_nodeList.begin(), _nodeList.end(), N);
  return (it != _nodeList.end());
}

template<class MData, class VData, class EData, class FData, bool RW>
void TemplateMeshSimplifierStateVariables<MData, VData, EData, FData, RW>::addCoupleVertex(SFTemplateVertex<MData, VData, EData, FData, RW> *A, SFTemplateVertex<MData, VData, EData, FData, RW> *B)
{
  _vertexMap[A] = B;
}

template<class MData, class VData, class EData, class FData, bool RW>
SFTemplateVertex<MData, VData, EData, FData, RW> *TemplateMeshSimplifierStateVariables<MData, VData, EData, FData, RW>::getNode(SFTemplateVertex<MData, VData, EData, FData, RW> *V) const
{
  typename std::map<SFTemplateVertex<MData, VData, EData, FData, RW> *, SFTemplateVertex<MData, VData, EData, FData, RW> *>::const_iterator it = _vertexMap.find(V);
  if (it != _vertexMap.end())
    return ((*it).second);
  
  return 0;  
}

}
}
