Shinya Kitaoka 810553
#pragma once
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef TCG_EDGE_H
Toshihiro Shimizu 890ddd
#define TCG_EDGE_H
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// STD includes
Toshihiro Shimizu 890ddd
#include "assert.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace tcg
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename e=""></typename>
Toshihiro Shimizu 890ddd
struct edge_traits {
Toshihiro Shimizu 890ddd
	typedef typename E::vertices_const_iterator vertices_const_iterator;
Toshihiro Shimizu 890ddd
	typedef typename E::vertices_iterator vertices_iterator;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	typedef typename E::faces_const_iterator faces_const_iterator;
Toshihiro Shimizu 890ddd
	typedef typename E::faces_iterator faces_iterator;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class Edge
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	typedef const int *vertices_const_iterator;
Toshihiro Shimizu 890ddd
	typedef int *vertices_iterator;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	typedef const int *faces_const_iterator;
Toshihiro Shimizu 890ddd
	typedef int *faces_iterator;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
protected:
Toshihiro Shimizu 890ddd
	int m_v[2];
Toshihiro Shimizu 890ddd
	int m_f[2];
Toshihiro Shimizu 890ddd
	int m_index;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	Edge() : m_index(-1)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_v[0] = -1, m_v[1] = -1;
Toshihiro Shimizu 890ddd
		m_f[0] = -1, m_f[1] = -1;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	Edge(int v1, int v2) : m_index(-1)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_v[0] = v1, m_v[1] = v2;
Toshihiro Shimizu 890ddd
		m_f[0] = -1, m_f[1] = -1;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	~Edge() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void setIndex(int idx) { m_index = idx; }
Toshihiro Shimizu 890ddd
	int getIndex() const { return m_index; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int vertex(int i) const { return m_v[i]; }
Toshihiro Shimizu 890ddd
	int verticesCount() const { return m_v[0] < 0 ? 0 : m_v[1] < 0 ? 1 : 2; }
Toshihiro Shimizu 890ddd
	int otherVertex(int v) const { return m_v[0] == v ? m_v[1] : m_v[0]; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void addVertex(int v)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		assert(verticesCount() < 2);
Toshihiro Shimizu 890ddd
		m_v[verticesCount()] = v;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void setVertex(int i, int v) { assert(i < verticesCount()), m_v[i] = v; }
Toshihiro Shimizu 890ddd
	vertices_iterator eraseVertex(vertices_iterator it)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		*std::copy(it + 1, verticesEnd(), it) = -1;
Toshihiro Shimizu 890ddd
		return it;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	vertices_const_iterator verticesBegin() const { return m_v; }
Toshihiro Shimizu 890ddd
	vertices_const_iterator verticesEnd() const { return m_v + verticesCount(); }
Toshihiro Shimizu 890ddd
	vertices_iterator verticesBegin() { return m_v; }
Toshihiro Shimizu 890ddd
	vertices_iterator verticesEnd() { return m_v + verticesCount(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int face(int i) const { return m_f[i]; }
Toshihiro Shimizu 890ddd
	int facesCount() const { return m_f[0] < 0 ? 0 : m_f[1] < 0 ? 1 : 2; }
Toshihiro Shimizu 890ddd
	int otherFace(int f) const { return m_f[0] == f ? m_f[1] : m_f[0]; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void addFace(int f)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		assert(facesCount() < 2);
Toshihiro Shimizu 890ddd
		m_f[facesCount()] = f;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void setFace(int i, int f) { assert(i < facesCount()), m_f[i] = f; }
Toshihiro Shimizu 890ddd
	faces_iterator eraseFace(faces_iterator it)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		*std::copy(it + 1, facesEnd(), it) = -1;
Toshihiro Shimizu 890ddd
		return it;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	faces_const_iterator facesBegin() const { return m_f; }
Toshihiro Shimizu 890ddd
	faces_const_iterator facesEnd() const { return m_f + facesCount(); }
Toshihiro Shimizu 890ddd
	faces_iterator facesBegin() { return m_f; }
Toshihiro Shimizu 890ddd
	faces_iterator facesEnd() { return m_f + facesCount(); }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // namespace tcg
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif // TCG_EDGE_H