Blame c++/freetype/src/vector.h

b8976b
#ifndef VECTOR_H
b8976b
#define VECTOR_H
b8976b
b8976b
b8976b
#include <cassert></cassert>
b8976b
b8976b
#include <string></string>
b8976b
#include <sstream></sstream>
b8976b
#include <utility></utility>
b8976b
b8976b
#include "common.h"
b8976b
b8976b
b8976b
template<typename t=""></typename>
b8976b
inline bool coord_less(const T &a, const T &b)
b8976b
	{ return a < b; }
b8976b
b8976b
template<>
b8976b
inline bool coord_less(const Real &a, const Real &b)
b8976b
	{ return real_less(a, b); }
b8976b
b8976b
b8976b
b8976b
template<typename lt="" st,="" typename=""></typename>
b8976b
class VectorBase2T {
b8976b
public:
b8976b
	typedef VectorBase2T<st, lt=""> SelfTypeArg;</st,>
b8976b
	typedef ST SelfType;
b8976b
	typedef LT LowerType;
b8976b
b8976b
	typedef SelfType Vector2;
b8976b
	typedef LowerType Coord;
b8976b
b8976b
	enum { Count = 2 };
b8976b
	
b8976b
	union {
b8976b
		struct { Coord coords[Count]; };
b8976b
		struct { Coord x, y; };
b8976b
	};
b8976b
	
b8976b
	inline explicit VectorBase2T(const Coord &x = Coord(), const Coord &y = Coord()):
b8976b
		x(x), y(y) { }
b8976b
	
b8976b
	inline SelfType perp() const
b8976b
		{ return SelfType(-y, x); }
b8976b
	inline SelfType yx() const
b8976b
		{ return SelfType(y, x); }
b8976b
};
b8976b
b8976b
b8976b
template<typename lt="" st,="" typename=""></typename>
b8976b
class VectorBase3T {
b8976b
public:
b8976b
	typedef VectorBase3T<st, lt=""> SelfTypeArg;</st,>
b8976b
	typedef ST SelfType;
b8976b
	typedef LT LowerType;
b8976b
b8976b
	typedef SelfType Vector3;
b8976b
	typedef LowerType Vector2;
b8976b
	typedef typename LowerType::Coord Coord;
b8976b
b8976b
	enum { Count = 3 };
b8976b
b8976b
	union {
b8976b
		struct { Coord coords[Count]; };
b8976b
		struct { Coord x, y, z; };
b8976b
	};
b8976b
	
b8976b
	inline explicit VectorBase3T(const Coord &x = Coord(), const Coord &y = Coord(), const Coord &z = Coord()):
b8976b
		x(x), y(y), z(z) { }
b8976b
	inline VectorBase3T(const Vector2 &v, const Coord &z):
b8976b
		x(v.x), y(v.y), z(z) { }
b8976b
	
b8976b
	inline SelfType cross(const SelfTypeArg &other) const
b8976b
		{ return SelfType(y*other.z - z*other.y, z*other.x - x*other.z, x*other.y - y*other.x); }
b8976b
b8976b
	inline Vector2& vec2()
b8976b
		{ return *(Vector2*)this; };
b8976b
	inline const Vector2& vec2() const
b8976b
		{ return *(const Vector2*)this; };
b8976b
	
b8976b
	inline SelfType xzy() const
b8976b
		{ return SelfType(x, z, y); }
b8976b
	inline SelfType zxy() const
b8976b
		{ return SelfType(z, x, y); }
b8976b
	inline SelfType zyx() const
b8976b
		{ return SelfType(z, y, x); }
b8976b
	inline SelfType yxz() const
b8976b
		{ return SelfType(y, x, z); }
b8976b
	inline SelfType yzx() const
b8976b
		{ return SelfType(y, z, x); }
b8976b
};
b8976b
b8976b
b8976b
template<typename lt="" st,="" typename=""></typename>
b8976b
class VectorBase4T {
b8976b
public:
b8976b
	typedef VectorBase4T<st, lt=""> SelfTypeArg;</st,>
b8976b
	typedef ST SelfType;
b8976b
	typedef LT LowerType;
b8976b
b8976b
	typedef SelfType Vector4;
b8976b
	typedef LowerType Vector3;
b8976b
	typedef typename LowerType::Vector2 Vector2;
b8976b
	typedef typename LowerType::Coord Coord;
b8976b
b8976b
	enum { Count = 4 };
b8976b
b8976b
	union {
b8976b
		struct { Coord coords[Count]; };
b8976b
		struct { Coord x, y, z, w; };
b8976b
	};
b8976b
	
b8976b
	inline explicit VectorBase4T(const Coord &x = Coord(), const Coord &y = Coord(), const Coord &z = Coord(), const Coord &w = Coord()):
b8976b
		x(x), y(y), z(z), w(w) { }
b8976b
	inline VectorBase4T(const Vector2 &v, const Coord &z, const Coord &w = Coord()):
b8976b
		x(v.x), y(v.y), z(z), w(w) { }
b8976b
	inline VectorBase4T(const Vector3 &v, const Coord &w):
b8976b
		x(v.x), y(v.y), z(v.z), w(w) { }
b8976b
	
b8976b
	inline Vector3& vec3()
b8976b
		{ return *(Vector3*)this; };
b8976b
	inline const Vector3& vec3() const
b8976b
		{ return *(const Vector3*)this; };
b8976b
b8976b
	inline Vector2& vec2()
b8976b
		{ return *(Vector2*)this; };
b8976b
	inline const Vector2& vec2() const
b8976b
		{ return *(const Vector2*)this; };
b8976b
};
b8976b
b8976b
b8976b
template<typename t=""></typename>
b8976b
class VectorT: public T {
b8976b
public:
b8976b
	typedef VectorT<t> SelfTypeArg;</t>
b8976b
	typedef T ParentType;
b8976b
	using ParentType::Count;
b8976b
	using typename ParentType::Coord;
b8976b
	using typename ParentType::SelfType;
b8976b
	using ParentType::coords;
b8976b
	using ParentType::ParentType; // contructors
b8976b
	
b8976b
	
b8976b
	static inline bool coord_less(const Coord &a, const Coord &b)
b8976b
		{ return ::coord_less(a, b); }
b8976b
	
b8976b
	
b8976b
	inline VectorT() { }
b8976b
	
b8976b
	inline explicit VectorT(const Coord *c) {
b8976b
		assert(c);
b8976b
		for(int i = 0; i < Count; ++i) coords[i] = c[i];
b8976b
	}
b8976b
	
b8976b
	template<typename tt=""></typename>
b8976b
	inline explicit VectorT(const VectorT<tt> &v) {</tt>
b8976b
		const int cnt = (int)TT::Count < (int)Count ? (int)TT::Count : (int)Count;
b8976b
		for(int i = 0; i < cnt; ++i) coords[i] = Coord(v.coords[i]);
b8976b
		for(int i = cnt; i < Count; ++i) coords[i] = Coord();
b8976b
	}
b8976b
	
b8976b
	Coord& operator[] (int i)
b8976b
		{ assert(i >= 0 && i < Count); return coords[i]; }
b8976b
	const Coord& operator[] (int i) const
b8976b
		{ assert(i >= 0 && i < Count); return coords[i]; }
b8976b
b8976b
	inline bool operator< (const SelfTypeArg &other) const {
b8976b
		for(int i = 0; i < Count; ++i)
b8976b
			if (coord_less(coords[i], other.coords[i])) return true; else
b8976b
				if (coord_less(other.coords[i], coords[i])) return false;
b8976b
		return false;
b8976b
	}
b8976b
b8976b
	inline bool operator== (const SelfTypeArg &other) const {
b8976b
		for(int i = 0; i < Count; ++i)
b8976b
			if ( coord_less(coords[i], other.coords[i])
b8976b
			  || coord_less(other.coords[i], coords[i]) ) return false;
b8976b
		return true;
b8976b
	}
b8976b
b8976b
	inline bool operator!= (const SelfTypeArg &other) const
b8976b
		{ return !(*(const SelfType*)this == other); }
b8976b
b8976b
	inline SelfType& operator+= (const SelfTypeArg &other) {
b8976b
		for(int i = 0; i < Count; ++i) coords[i] += other.coords[i];
b8976b
		return *(SelfType*)this;
b8976b
	}
b8976b
	inline SelfType& operator-= (const SelfTypeArg &other) {
b8976b
		for(int i = 0; i < Count; ++i) coords[i] -= other.coords[i];
b8976b
		return *(SelfType*)this;
b8976b
	}
b8976b
	inline SelfType& operator*= (const Coord &c) {
b8976b
		for(int i = 0; i < Count; ++i) coords[i] *= c;
b8976b
		return *(SelfType*)this;
b8976b
	}
b8976b
	inline SelfType operator- () const {
b8976b
		SelfType v;
b8976b
		for(int i = 0; i < Count; ++i) v.coords[i] = -coords[i];
b8976b
		return v;
b8976b
	}
b8976b
	inline Coord operator* (const SelfTypeArg &other) const {
b8976b
		Coord r = Coord();
b8976b
		for(int i = 0; i < Count; ++i) r += coords[i]*other.coords[i];
b8976b
		return r;
b8976b
	}
b8976b
b8976b
	inline SelfType operator+ (const SelfTypeArg &other) const
b8976b
		{ return SelfType(*this) += other; }
b8976b
	inline SelfType operator- (const SelfTypeArg &other) const
b8976b
		{ return SelfType(*this) -= other; }
b8976b
	inline SelfType operator* (const Coord &c) const
b8976b
		{ return SelfType(*this) *= c; }
b8976b
	inline Coord square() const
b8976b
		{ return *this * *this; }
b8976b
		
b8976b
	inline static SelfType& cast(Coord *c)
b8976b
		{ return *(SelfType*)c; }
b8976b
	inline static const SelfType& cast(const Coord *c)
b8976b
		{ return *(const SelfType*)c; }
b8976b
	
b8976b
	std::string to_string() const {
b8976b
		std::stringstream stream;
b8976b
		stream << "(" << coords[0];
b8976b
		for(int i = 1; i < Count; ++i) stream << ", " << coords[i];
b8976b
		stream << ")";
b8976b
		return stream.str();
b8976b
	}
b8976b
};
b8976b
b8976b
b8976b
template<typename t=""></typename>
b8976b
class VectorFT: public VectorT<t> {</t>
b8976b
public:
b8976b
	typedef VectorT<t> ParentType;</t>
b8976b
	using typename ParentType::Coord;
b8976b
	using typename ParentType::SelfType;
b8976b
	using ParentType::Count;
b8976b
	using ParentType::coords;
b8976b
	using ParentType::ParentType; // contructors
b8976b
	
b8976b
b8976b
	inline SelfType& operator/= (const Coord &c)
b8976b
		{ return *this *= Coord(1)/c; }
b8976b
	inline SelfType operator/ (const Coord &c) const
b8976b
		{ return SelfType(*this) /= c; }
b8976b
	inline Coord length() const
b8976b
		{ return Coord(sqrt(ParentType::square())); }
b8976b
	
b8976b
	inline SelfType normalized() const {
b8976b
		Coord len = length();
b8976b
		return ParentType::coord_less(Coord(), len)
b8976b
		     ? *this / len : SelfType();
b8976b
	}
b8976b
	inline SelfType normalize() const
b8976b
		{ *this = normalized(); return *(SelfType)this; }
b8976b
b8976b
	inline SelfType persp_divide() const
b8976b
		{ return *this / coords[Count - 1]; }
b8976b
};
b8976b
b8976b
template<typename t=""></typename>
b8976b
class PairT {
b8976b
public:
b8976b
	typedef T Vector;
b8976b
	Vector p0;
b8976b
	Vector p1;
b8976b
	
b8976b
	explicit inline PairT(const Vector &p = Vector()):
b8976b
		p0(p), p1(p) { }
b8976b
	inline PairT(const Vector &p0, const Vector &p1):
b8976b
		p0(p0), p1(p1) { }
b8976b
b8976b
	template<typename tt=""></typename>
b8976b
	inline explicit PairT(const VectorT<tt> &p):</tt>
b8976b
		p0(p), p1(p) { }
b8976b
	template<typename tt=""></typename>
b8976b
	inline explicit PairT(const VectorT<tt> &p0, const VectorT<tt> &p1):</tt></tt>
b8976b
		p0(p0), p1(p1) { }
b8976b
b8976b
	template<typename tt=""></typename>
b8976b
	inline explicit PairT(const PairT<tt> &other):</tt>
b8976b
		PairT(other.p0, other.p1) { }
b8976b
		
b8976b
	inline bool operator< (const PairT &other) const {
b8976b
		return p0 < other.p0 ? true
b8976b
			 : other.p0 < p0 ? false
b8976b
			 : p1 < other.p1;
b8976b
	}
b8976b
	inline bool operator== (const PairT &other) const
b8976b
		{ return !(*this < other) && !(other < *this); }
b8976b
	inline bool operator!= (const PairT &other) const
b8976b
		{ return *this < other || other < *this; }
b8976b
	
b8976b
	
b8976b
	inline Vector distance() const
b8976b
		{ return p1 - p0; }
b8976b
	inline Vector size() const
b8976b
		{ return distance(); }
b8976b
b8976b
	inline bool empty() const {
b8976b
		for(int i = 0; i < Vector::Count; ++i)
b8976b
			if (!Vector::coord_less(p0[i], p1[i]))
b8976b
				return true;
b8976b
		return false;
b8976b
	}
b8976b
	inline PairT& sort() {
b8976b
		for(int i = 0; i < Vector::Count; ++i)
b8976b
			if (p0[i] < p1[i])
b8976b
				std::swap(p0[i], p1[i]);
b8976b
		return *this;
b8976b
	}
b8976b
	inline PairT sorted() const
b8976b
		{ return PairT(*this).sort(); }
b8976b
b8976b
	inline PairT& expand(const Vector &p) {
b8976b
		for(int i = 0; i < Vector::Count; ++i) {
b8976b
			if (p[i] < p0[i]) p0[i] = p[i];
b8976b
			if (p1[i] < p[i]) p1[i] = p[i];
b8976b
		}
b8976b
		return *this;
b8976b
	}
b8976b
	inline PairT expanded(const Vector &p) const
b8976b
		{ return PairT(*this).expand(p); }
b8976b
b8976b
	inline PairT& inflate(const Vector &p) {
b8976b
		if (empty()) return *this;
b8976b
		p0 -= p; p1 += p;
b8976b
		return *this;
b8976b
	}
b8976b
	inline PairT inflated(const Vector &p) const
b8976b
		{ return PairT(*this).inflate(p); }
b8976b
};
b8976b
b8976b
b8976b
b8976b
template<typename t=""></typename>
b8976b
class Vectors {
b8976b
public:
b8976b
	typedef T Type;
b8976b
b8976b
	class Vector2: public VectorT< VectorBase2T<vector2, type=""> > {</vector2,>
b8976b
	public:
b8976b
		typedef VectorT< VectorBase2T<vector2, type=""> > ParentType;</vector2,>
b8976b
		using ParentType::ParentType; // constructors
b8976b
	};
b8976b
	class Vector3: public VectorT< VectorBase3T<vector3, vector2=""> > {</vector3,>
b8976b
	public:
b8976b
		typedef VectorT< VectorBase3T<vector3, vector2=""> > ParentType;</vector3,>
b8976b
		using ParentType::ParentType; // constructors
b8976b
	};
b8976b
	class Vector4: public VectorT< VectorBase4T<vector4, vector3=""> > {</vector4,>
b8976b
	public:
b8976b
		typedef VectorT< VectorBase4T<vector4, vector3=""> > ParentType;</vector4,>
b8976b
		using ParentType::ParentType; // constructors
b8976b
	};
b8976b
};
b8976b
b8976b
b8976b
template<typename t=""></typename>
b8976b
class VectorsFloat {
b8976b
public:
b8976b
	typedef T Type;
b8976b
b8976b
	class Vector2: public VectorFT< VectorBase2T<vector2, type=""> > {</vector2,>
b8976b
	public:
b8976b
		typedef VectorFT< VectorBase2T<vector2, type=""> > ParentType;</vector2,>
b8976b
		using ParentType::ParentType; // constructors
b8976b
	};
b8976b
	class Vector3: public VectorFT< VectorBase3T<vector3, vector2=""> > {</vector3,>
b8976b
	public:
b8976b
		typedef VectorFT< VectorBase3T<vector3, vector2=""> > ParentType;</vector3,>
b8976b
		using ParentType::ParentType; // constructors
b8976b
	};
b8976b
	class Vector4: public VectorFT< VectorBase4T<vector4, vector3=""> > {</vector4,>
b8976b
	public:
b8976b
		typedef VectorFT< VectorBase4T<vector4, vector3=""> > ParentType;</vector4,>
b8976b
		using ParentType::ParentType; // constructors
b8976b
	};
b8976b
};
b8976b
b8976b
b8976b
typedef VectorsFloat<real>::Vector2 Vector2;</real>
b8976b
typedef VectorsFloat<real>::Vector3 Vector3;</real>
b8976b
typedef VectorsFloat<real>::Vector4 Vector4;</real>
b8976b
b8976b
typedef VectorsFloat<int>::Vector2 IntVector2;</int>
b8976b
typedef VectorsFloat<int>::Vector3 IntVector3;</int>
b8976b
typedef VectorsFloat<int>::Vector4 IntVector4;</int>
b8976b
b8976b
typedef VectorsFloat<longint>::Vector2 LongIntVector2;</longint>
b8976b
typedef VectorsFloat<longint>::Vector3 LongIntVector3;</longint>
b8976b
typedef VectorsFloat<longint>::Vector4 LongIntVector4;</longint>
b8976b
b8976b
typedef VectorsFloat<uint>::Vector2 UIntVector2;</uint>
b8976b
typedef VectorsFloat<uint>::Vector3 UIntVector3;</uint>
b8976b
typedef VectorsFloat<uint>::Vector4 UIntVector4;</uint>
b8976b
b8976b
typedef VectorsFloat<ulongint>::Vector2 ULongIntVector2;</ulongint>
b8976b
typedef VectorsFloat<ulongint>::Vector3 ULongIntVector3;</ulongint>
b8976b
typedef VectorsFloat<ulongint>::Vector4 ULongIntVector4;</ulongint>
b8976b
b8976b
b8976b
typedef PairT<vector2> Pair2;</vector2>
b8976b
typedef PairT<vector3> Pair3;</vector3>
b8976b
typedef PairT<vector4> Pair4;</vector4>
b8976b
b8976b
typedef PairT<intvector2> IntPair2;</intvector2>
b8976b
typedef PairT<intvector3> IntPair3;</intvector3>
b8976b
typedef PairT<intvector4> IntPair4;</intvector4>
b8976b
b8976b
typedef PairT<longintvector2> LongIntPair2;</longintvector2>
b8976b
typedef PairT<longintvector3> LongIntPair3;</longintvector3>
b8976b
typedef PairT<longintvector4> LongIntPair4;</longintvector4>
b8976b
b8976b
typedef PairT<ulongintvector2> ULongIntPair2;</ulongintvector2>
b8976b
typedef PairT<ulongintvector3> ULongIntPair3;</ulongintvector3>
b8976b
typedef PairT<ulongintvector4> ULongIntPair4;</ulongintvector4>
b8976b
b8976b
b8976b
#endif