/// This file is part of the X3DToolKit library
/// Copyright (C) 2002-2004  Yannick Le Goc (legoc@imag.fr)

/// http://artis.imag.fr/Members/Yannick.Legoc/X3D/

/// This library is free software; you can redistribute it and/or
/// modify it under the terms of the GNU Lesser General Public
/// License as published by the Free Software Foundation; either
/// version 2.1 of the License, or (at your option) any later version.

/// This library is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
/// Lesser General Public License for more details.

/// You should have received a copy of the GNU Lesser General Public
/// License along with this library; if not, write to the Free Software
/// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#ifndef X3DTK_MESH_SFEDGE_H
#define X3DTK_MESH_SFEDGE_H

#include "MESH_BaseSFEdge.h"
#include "MESH_SFEdgeContent.h"

namespace X3DTK {
namespace MESH {

template<class MData, class VData, class EData, class FData, bool RW> class TemplateVertex;
template<class MData, class VData, class EData, class FData, bool RW> class SFTemplateVertex;
template<class MData, class VData, class EData, class FData, bool RW> class SFTemplateFace;
template<class MData, class VData, class EData, class FData, bool RW> class EntityMFTemplateFace;

/*! \brief Template class declared in <b><X3DTK/MESH/scenegraph.h></b> 
 *  providing an edge structure for the Mesh class.
 *
 * \ingroup MeshSceneGraph
 */

template<class MData, class VData, class EData, class FData, bool RW>
class SFTemplateEdge : public BaseSFEdge
{
  friend class TemplateVertex<MData, VData, EData, FData, RW>;
  friend class TemplateMesh<MData, VData, EData, FData, RW>;
  friend class SFTemplateDirectEdge<MData, VData, EData, FData, RW>;
  friend class SFTemplateIndirectEdge<MData, VData, EData, FData, RW>;
  friend class SFTemplateFace<MData, VData, EData, FData, RW>;
public:
  typedef EntityMFTemplateFace<MData, VData, EData, FData, RW> MFFace;
  
  /// Returns the symetric edge.
  inline SFTemplateEdge *getSymetric();
  /// Returns the Vertex from.
  virtual SFTemplateVertex<MData, VData, EData, FData, RW> *getFromVertex() const = 0;
  /// Gets the Vertex to.
  virtual SFTemplateVertex<MData, VData, EData, FData, RW> *getToVertex() const = 0;
  /// Gets the left faces.
  virtual const MFFace &getLeftFaces() const = 0;
  /// Gets the right faces.
  virtual const MFFace &getRightFaces() const = 0;
  /// Gets the faces. The method is inefficient because no reference is returned and 
  /// a copy constructor is called. Prefer getting the left faces first and then the
  /// right faces.
  MFFace getFaces() const;
  /// Returns true if is a boundary.
  inline bool isBoundary() const;
  /// Returns true if is non-manifold.
  inline bool isNonManifold() const;
  /// Gets the data.
  inline EData &data();
  /// Gets the data.
  inline const EData &data() const;

#ifdef TEMPLATE_SPECIALIZATION_SUPPORTED
  /// Gets the data F from EData which must be of type clist<tlist<T...> >.
  template<class F>  
  F &getData();
  /// Gets optionally the data F from EData which must be of type clist<tlist<T...> >.
  template<class F>  
  F &ogetData();
#endif
  
protected:    
  /// Constructor.
  SFTemplateEdge(SFTemplateEdgeContent<MData, VData, EData, FData, RW> *const edgeContent);
  /// Destructor.
  virtual ~SFTemplateEdge();
  
  /// Sets the symetric edge.
  void setSymetric(SFTemplateEdge *symetric);
  /// Adds a left face.
  virtual void addLeftFace(SFTemplateFace<MData, VData, EData, FData, RW> *face) = 0;
  /// Adds a right face.
  virtual void addRightFace(SFTemplateFace<MData, VData, EData, FData, RW> *face) = 0;

  /// Removes a left face.
  virtual void removeLeftFace(SFTemplateFace<MData, VData, EData, FData, RW> *face) = 0;
  /// Removes a right face.
  virtual void removeRightFace(SFTemplateFace<MData, VData, EData, FData, RW> *face) = 0;

private:
  SFTemplateEdgeContent<MData, VData, EData, FData, RW> *const _edgeContent;
  SFTemplateEdge *_symetric;
  
  /// Reverses the face. Is called by SFFace::reverse().
  void reverse(SFTemplateFace<MData, VData, EData, FData, RW> *face);
};

/// operator<< overloaded for SFEdge class.
template<class MData, class VData, class EData, class FData, bool RW>
std::ostream& operator<<(std::ostream& o, const SFTemplateEdge<MData, VData, EData, FData, RW> &e);

}
}

#include "MESH_SFEdge.inl"

#endif
