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

531f67
#ifndef VECTOR_H
531f67
#define VECTOR_H
531f67
531f67
531f67
#include <cassert></cassert>
531f67
531f67
#include "common.h"
531f67
531f67
531f67
template<typename lt="" st,="" typename=""></typename>
531f67
class VectorBase2T {
531f67
public:
fdabb2
	typedef VectorBase2T<st, lt=""> SelfTypeArg;</st,>
531f67
	typedef ST SelfType;
531f67
	typedef LT LowerType;
531f67
531f67
	typedef SelfType Vector2;
531f67
	typedef LowerType Coord;
531f67
531f67
	enum { Count = 2 };
531f67
	
531f67
	union {
531f67
		struct { Coord coords[Count]; };
531f67
		struct { Coord x, y; };
531f67
	};
531f67
	
531f67
	inline explicit VectorBase2T(const Coord &x = Coord(), const Coord &y = Coord()):
531f67
		x(x), y(y) { }
531f67
	
531f67
	inline SelfType perp() const
531f67
		{ return SelfType(-y, x); }
531f67
	inline SelfType yx() const
531f67
		{ return SelfType(y, x); }
531f67
};
531f67
531f67
531f67
template<typename lt="" st,="" typename=""></typename>
531f67
class VectorBase3T {
531f67
public:
fdabb2
	typedef VectorBase3T<st, lt=""> SelfTypeArg;</st,>
531f67
	typedef ST SelfType;
531f67
	typedef LT LowerType;
531f67
531f67
	typedef SelfType Vector3;
531f67
	typedef LowerType Vector2;
531f67
	typedef typename LowerType::Coord Coord;
531f67
531f67
	enum { Count = 3 };
531f67
531f67
	union {
531f67
		struct { Coord coords[Count]; };
531f67
		struct { Coord x, y, z; };
531f67
	};
531f67
	
531f67
	inline explicit VectorBase3T(const Coord &x = Coord(), const Coord &y = Coord(), const Coord &z = Coord()):
531f67
		x(x), y(y), z(z) { }
531f67
	inline VectorBase3T(const Vector2 &v, const Coord &z):
531f67
		x(v.x), y(v.y), z(z) { }
531f67
	
fdabb2
	inline SelfType cross(const SelfTypeArg &other) const
531f67
		{ return SelfType(y*other.z - z*other.y, z*other.x - x*other.z, x*other.y - y*other.x); }
531f67
531f67
	inline Vector2& vec2()
531f67
		{ return *(Vector2*)this; };
531f67
	inline const Vector2& vec2() const
531f67
		{ return *(const Vector2*)this; };
531f67
	
531f67
	inline SelfType xzy() const
531f67
		{ return SelfType(x, z, y); }
531f67
	inline SelfType zxy() const
531f67
		{ return SelfType(z, x, y); }
531f67
	inline SelfType zyx() const
531f67
		{ return SelfType(z, y, x); }
531f67
	inline SelfType yxz() const
531f67
		{ return SelfType(y, x, z); }
531f67
	inline SelfType yzx() const
531f67
		{ return SelfType(y, z, x); }
531f67
};
531f67
531f67
531f67
template<typename lt="" st,="" typename=""></typename>
531f67
class VectorBase4T {
531f67
public:
fdabb2
	typedef VectorBase4T<st, lt=""> SelfTypeArg;</st,>
531f67
	typedef ST SelfType;
531f67
	typedef LT LowerType;
531f67
531f67
	typedef SelfType Vector4;
531f67
	typedef LowerType Vector3;
531f67
	typedef typename LowerType::Vector2 Vector2;
531f67
	typedef typename LowerType::Coord Coord;
531f67
531f67
	enum { Count = 4 };
531f67
531f67
	union {
531f67
		struct { Coord coords[Count]; };
531f67
		struct { Coord x, y, z, w; };
531f67
	};
531f67
	
531f67
	inline explicit VectorBase4T(const Coord &x = Coord(), const Coord &y = Coord(), const Coord &z = Coord(), const Coord &w = Coord()):
531f67
		x(x), y(y), z(z), w(w) { }
531f67
	inline VectorBase4T(const Vector2 &v, const Coord &z, const Coord &w = Coord()):
531f67
		x(v.x), y(v.y), z(z), w(w) { }
531f67
	inline VectorBase4T(const Vector3 &v, const Coord &w):
531f67
		x(v.x), y(v.y), z(v.z), w(w) { }
531f67
	
531f67
	inline Vector3& vec3()
531f67
		{ return *(Vector3*)this; };
531f67
	inline const Vector3& vec3() const
531f67
		{ return *(const Vector3*)this; };
531f67
531f67
		inline Vector2& vec2()
531f67
		{ return *(Vector2*)this; };
531f67
	inline const Vector2& vec2() const
531f67
		{ return *(const Vector2*)this; };
531f67
	
fdabb2
	inline SelfType cross(const SelfTypeArg &other) const
531f67
		{ return SelfType(y*other.z - z*other.y, z*other.x - x*other.z, x*other.y - y*other.x, w); }
531f67
};
531f67
531f67
531f67
template<typename t=""></typename>
531f67
class VectorT: public T {
531f67
public:
fdabb2
	typedef VectorT<t> SelfTypeArg;</t>
531f67
	typedef T ParentType;
531f67
	using ParentType::Count;
531f67
	using typename ParentType::Coord;
531f67
	using typename ParentType::SelfType;
531f67
	using ParentType::coords;
531f67
	using ParentType::ParentType; // contructors
531f67
531f67
	inline VectorT() { }
531f67
	
531f67
	inline explicit VectorT(const Coord *c) {
531f67
		assert(c);
531f67
		for(int i = 0; i < Count; ++i) coords[i] = c[i];
531f67
	}
531f67
	
531f67
	template<typename tt=""></typename>
531f67
	inline explicit VectorT(const VectorT<tt> &v) {</tt>
fdabb2
		const int cnt = (int)TT::Count < (int)Count ? (int)TT::Count : (int)Count;
531f67
		for(int i = 0; i < cnt; ++i) coords[i] = Coord(v.coords[i]);
531f67
		for(int i = cnt; i < Count; ++i) coords[i] = Coord();
531f67
	}
531f67
	
531f67
	Coord& operator[] (int i)
531f67
		{ assert(i >= 0 && i < Count); return coords[i]; }
531f67
	const Coord& operator[] (int i) const
531f67
		{ assert(i >= 0 && i < Count); return coords[i]; }
531f67
fdabb2
	inline bool operator< (const SelfTypeArg &other) const {
531f67
		for(int i = 0; i < Count; ++i)
531f67
			if (coords[i] < other.coords[i]) return true;
531f67
			else if (other.coords[i] < coords[i]) return false;
531f67
		return false;
531f67
	}
531f67
fdabb2
	inline bool operator== (const SelfTypeArg &other) const {
531f67
		for(int i = 0; i < Count; ++i)
531f67
			if (coords[i] != other.coords[i]) return false;
531f67
		return true;
531f67
	}
531f67
fdabb2
	inline bool operator!= (const SelfTypeArg &other) const
531f67
		{ return !(*(const SelfType*)this == other); }
531f67
fdabb2
	inline SelfType& operator+= (const SelfTypeArg &other) {
531f67
		for(int i = 0; i < Count; ++i) coords[i] += other.coords[i];
531f67
		return *(SelfType*)this;
531f67
	}
fdabb2
	inline SelfType& operator-= (const SelfTypeArg &other) {
531f67
		for(int i = 0; i < Count; ++i) coords[i] -= other.coords[i];
531f67
		return *(SelfType*)this;
531f67
	}
531f67
	inline SelfType& operator*= (const Coord &c) {
531f67
		for(int i = 0; i < Count; ++i) coords[i] *= c;
531f67
		return *(SelfType*)this;
531f67
	}
531f67
	inline SelfType operator- () const {
531f67
		SelfType v;
531f67
		for(int i = 0; i < Count; ++i) v.coords[i] = -coords[i];
531f67
		return v;
531f67
	}
fdabb2
	inline Coord operator* (const SelfTypeArg &other) const {
531f67
		Coord r = Coord();
531f67
		for(int i = 0; i < Count; ++i) r += coords[i]*other.coords[i];
531f67
		return r;
531f67
	}
531f67
fdabb2
	inline SelfType operator+ (const SelfTypeArg &other) const
531f67
		{ return SelfType(*this) += other; }
fdabb2
	inline SelfType operator- (const SelfTypeArg &other) const
531f67
		{ return SelfType(*this) -= other; }
531f67
	inline SelfType operator* (const Coord &c) const
531f67
		{ return SelfType(*this) *= c; }
531f67
	inline Coord square() const
531f67
		{ return *this * *this; }
14f971
		
14f971
	inline static SelfType& cast(Coord *c)
14f971
		{ return *(SelfType*)c; }
14f971
	inline static const SelfType& cast(const Coord *c)
14f971
		{ return *(const SelfType*)c; }
531f67
};
531f67
531f67
531f67
template<typename t=""></typename>
531f67
class VectorFT: public VectorT<t> {</t>
531f67
public:
fdabb2
	typedef VectorFT<t> SelfTypeArg;</t>
531f67
	typedef VectorT<t> ParentType;</t>
531f67
	using ParentType::Count;
531f67
	using typename ParentType::Coord;
531f67
	using typename ParentType::SelfType;
531f67
	using ParentType::coords;
531f67
	using ParentType::ParentType; // contructors
531f67
	
531f67
531f67
	inline SelfType& operator/= (const Coord &c)
531f67
		{ return *this *= Coord(1)/c; }
531f67
	inline SelfType operator/ (const Coord &c)
531f67
		{ return SelfType(*this) /= c; }
531f67
	inline Coord length() const
531f67
		{ return Coord(sqrt(ParentType::square())); }
531f67
fdabb2
	inline bool operator< (const SelfTypeArg &other) const {
531f67
		for(int i = 0; i < Count; ++i)
fdabb2
			if (coords[i] < other.coords[i] - real_precision) return true;
fdabb2
			else if (other.coords[i] < coords[i] - real_precision) return false;
531f67
		return false;
531f67
	}
531f67
fdabb2
	inline bool operator== (const SelfTypeArg &other) const {
531f67
		for(int i = 0; i < Count; ++i)
fdabb2
			if ( coords[i] < other.coords[i] - real_precision
fdabb2
			  || other.coords[i] < coords[i] - real_precision ) return false;
531f67
		return true;
531f67
	}
531f67
fdabb2
	inline bool operator!= (const SelfTypeArg &other) const
531f67
		{ return !(*(const SelfType*)this == other); }
531f67
};
531f67
531f67
531f67
template<typename t=""></typename>
531f67
class Vectors {
531f67
public:
531f67
	typedef T Type;
531f67
531f67
	class Vector2: public VectorT< VectorBase2T<vector2, type=""> > {</vector2,>
531f67
	public:
531f67
		typedef VectorT< VectorBase2T<vector2, type=""> > ParentType;</vector2,>
531f67
		using ParentType::ParentType; // constructors
531f67
	};
531f67
	class Vector3: public VectorT< VectorBase3T<vector3, vector2=""> > {</vector3,>
531f67
	public:
531f67
		typedef VectorT< VectorBase3T<vector3, vector2=""> > ParentType;</vector3,>
531f67
		using ParentType::ParentType; // constructors
531f67
	};
531f67
	class Vector4: public VectorT< VectorBase4T<vector4, vector3=""> > {</vector4,>
531f67
	public:
531f67
		typedef VectorT< VectorBase4T<vector4, vector3=""> > ParentType;</vector4,>
531f67
		using ParentType::ParentType; // constructors
531f67
	};
531f67
};
531f67
531f67
531f67
template<typename t=""></typename>
531f67
class VectorsFloat {
531f67
public:
531f67
	typedef T Type;
531f67
531f67
	class Vector2: public VectorFT< VectorBase2T<vector2, type=""> > {</vector2,>
531f67
	public:
531f67
		typedef VectorFT< VectorBase2T<vector2, type=""> > ParentType;</vector2,>
531f67
		using ParentType::ParentType; // constructors
531f67
	};
531f67
	class Vector3: public VectorFT< VectorBase3T<vector3, vector2=""> > {</vector3,>
531f67
	public:
531f67
		typedef VectorFT< VectorBase3T<vector3, vector2=""> > ParentType;</vector3,>
531f67
		using ParentType::ParentType; // constructors
531f67
	};
531f67
	class Vector4: public VectorFT< VectorBase4T<vector4, vector3=""> > {</vector4,>
531f67
	public:
531f67
		typedef VectorFT< VectorBase4T<vector4, vector3=""> > ParentType;</vector4,>
531f67
		using ParentType::ParentType; // constructors
531f67
	};
531f67
};
531f67
531f67
531f67
typedef VectorsFloat<real>::Vector2 Vector2;</real>
531f67
typedef VectorsFloat<real>::Vector3 Vector3;</real>
531f67
typedef VectorsFloat<real>::Vector4 Vector4;</real>
531f67
531f67
typedef VectorsFloat<int>::Vector2 IntVector2;</int>
531f67
typedef VectorsFloat<int>::Vector3 IntVector3;</int>
531f67
typedef VectorsFloat<int>::Vector4 IntVector4;</int>
531f67
531f67
typedef VectorsFloat<longint>::Vector2 LongIntVector2;</longint>
531f67
typedef VectorsFloat<longint>::Vector3 LongIntVector3;</longint>
531f67
typedef VectorsFloat<longint>::Vector4 LongIntVector4;</longint>
531f67
531f67
typedef VectorsFloat<uint>::Vector2 UIntVector2;</uint>
531f67
typedef VectorsFloat<uint>::Vector3 UIntVector3;</uint>
531f67
typedef VectorsFloat<uint>::Vector4 UIntVector4;</uint>
531f67
531f67
typedef VectorsFloat<ulongint>::Vector2 ULongIntVector2;</ulongint>
531f67
typedef VectorsFloat<ulongint>::Vector3 ULongIntVector3;</ulongint>
531f67
typedef VectorsFloat<ulongint>::Vector4 ULongIntVector4;</ulongint>
531f67
531f67
531f67
#endif