#ifndef UTIL_H
#define UTIL_H

#include "my_mesh.h"

#include <QVector3D>
#include <QtMath>


template <typename Mesh>
class HalfedgeLoopRange {
	Mesh &mesh;
	const typename Mesh::HalfedgeHandle &start;

  public:
	HalfedgeLoopRange(Mesh &mesh, const typename Mesh::HalfedgeHandle &start)
		:mesh(mesh), start(start) {}
	typename Mesh::HalfedgeLoopIter begin() {
		return mesh.hl_begin(start);
	}
	typename Mesh::HalfedgeLoopIter end() {
		return mesh.hl_end(start);
	}
};


template <typename Mesh>
class ConstHalfedgeLoopRange {
	Mesh &mesh;
	const typename Mesh::HalfedgeHandle &start;

  public:
	ConstHalfedgeLoopRange(Mesh &mesh, const typename Mesh::HalfedgeHandle &start)
		:mesh(mesh), start(start) {}
	typename Mesh::HalfedgeLoopIter begin() {
		return mesh.chl_begin(start);
	}
	typename Mesh::HalfedgeLoopIter end() {
		return mesh.chl_end(start);
	}
};


QDebug operator<<(QDebug dbg, const Point &p);


/* Returns the angle between the vector from p2 to p1 and the vector
 * from p2 to p3. */
template <typename Point>
qreal angle_between(Point p1,
					Point p2,
					Point p3) {
	Point vec_a = p1 - p2;
	Point vec_b = p3 - p2;
	QVector3D a(vec_a[0], vec_a[1], vec_a[2]);
	QVector3D b(vec_b[0], vec_b[1], vec_b[2]);
	return qAcos(QVector3D::dotProduct(a.normalized(), b.normalized()));
}


template <typename Point>
qreal triangle_area(Point p1,
					Point p2,
					Point p3) {
	Point vec_a = p1 - p2;
	Point vec_b = p3 - p2;
	QVector3D a(vec_a[0], vec_a[1], vec_a[2]);
	QVector3D b(vec_b[0], vec_b[1], vec_b[2]);
	return QVector3D::crossProduct(a, b).length() / 2.;
}


#endif