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

531f67
#ifndef VECTOR_H
531f67
#define VECTOR_H
531f67
531f67
531f67
#include <cassert></cassert>
531f67
a1942e
#include <string></string>
a1942e
#include <sstream></sstream>
0662a2
#include <utility></utility>
a1942e
531f67
#include "common.h"
531f67
531f67
0662a2
template<typename t=""></typename>
0662a2
inline bool coord_less(const T &a, const T &b)
0662a2
	{ return a < b; }
0662a2
0662a2
template<>
0662a2
inline bool coord_less(const Real &a, const Real &b)
0662a2
	{ return real_less(a, b); }
0662a2
0662a2
0662a2
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
0662a2
	inline Vector2& vec2()
531f67
		{ return *(Vector2*)this; };
531f67
	inline const Vector2& vec2() const
531f67
		{ return *(const Vector2*)this; };
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
0662a2
	
0662a2
	
0662a2
	static inline bool coord_less(const Coord &a, const Coord &b)
0662a2
		{ return ::coord_less(a, b); }
0662a2
	
0662a2
	
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)
0662a2
			if (coord_less(coords[i], other.coords[i])) return true; else
0662a2
				if (coord_less(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)
0662a2
			if ( coord_less(coords[i], other.coords[i])
0662a2
			  || coord_less(other.coords[i], 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; }
a1942e
	
a1942e
	std::string to_string() const {
a1942e
		std::stringstream stream;
a1942e
		stream << "(" << coords[0];
a1942e
		for(int i = 1; i < Count; ++i) stream << ", " << coords[i];
a1942e
		stream << ")";
a1942e
		return stream.str();
a1942e
	}
531f67
};
531f67
531f67
531f67
template<typename t=""></typename>
531f67
class VectorFT: public VectorT<t> {</t>
531f67
public:
531f67
	typedef VectorT<t> ParentType;</t>
531f67
	using typename ParentType::Coord;
531f67
	using typename ParentType::SelfType;
0662a2
	using ParentType::Count;
531f67
	using ParentType::coords;
531f67
	using ParentType::ParentType; // contructors
531f67
	
531f67
531f67
	inline SelfType& operator/= (const Coord &c)
531f67
		{ return *this *= Coord(1)/c; }
0662a2
	inline SelfType operator/ (const Coord &c) const
531f67
		{ return SelfType(*this) /= c; }
531f67
	inline Coord length() const
531f67
		{ return Coord(sqrt(ParentType::square())); }
0662a2
	
0662a2
	inline SelfType normalized() const {
0662a2
		Coord len = length();
0662a2
		return ParentType::coord_less(Coord(), len)
0662a2
		     ? *this / len : SelfType();
0662a2
	}
0662a2
	inline SelfType normalize() const
0662a2
		{ *this = normalized(); return *(SelfType)this; }
531f67
0662a2
	inline SelfType persp_divide() const
0662a2
		{ return *this / coords[Count - 1]; }
0662a2
};
0662a2
0662a2
template<typename t=""></typename>
0662a2
class PairT {
0662a2
public:
0662a2
	typedef T Vector;
0662a2
	Vector p0;
0662a2
	Vector p1;
0662a2
	
0662a2
	explicit inline PairT(const Vector &p = Vector()):
0662a2
		p0(p), p1(p) { }
0662a2
	inline PairT(const Vector &p0, const Vector &p1):
0662a2
		p0(p0), p1(p1) { }
4dc49c
4dc49c
	template<typename tt=""></typename>
4dc49c
	inline explicit PairT(const VectorT<tt> &p):</tt>
4dc49c
		p0(p), p1(p) { }
4dc49c
	template<typename tt=""></typename>
4dc49c
	inline explicit PairT(const VectorT<tt> &p0, const VectorT<tt> &p1):</tt></tt>
4dc49c
		p0(p0), p1(p1) { }
4dc49c
4dc49c
	template<typename tt=""></typename>
4dc49c
	inline explicit PairT(const PairT<tt> &other):</tt>
4dc49c
		PairT(other.p0, other.p1) { }
4dc49c
		
0662a2
	inline bool operator< (const PairT &other) const {
0662a2
		return p0 < other.p0 ? true
0662a2
			 : other.p0 < p0 ? false
0662a2
			 : p1 < other.p1;
0662a2
	}
0662a2
	inline bool operator== (const PairT &other) const
0662a2
		{ return !(*this < other) && !(other < *this); }
0662a2
	inline bool operator!= (const PairT &other) const
0662a2
		{ return *this < other || other < *this; }
0662a2
	
0662a2
	
0662a2
	inline Vector distance() const
0662a2
		{ return p1 - p0; }
0662a2
	inline Vector size() const
0662a2
		{ return distance(); }
0662a2
0662a2
	inline bool empty() const {
0662a2
		for(int i = 0; i < Vector::Count; ++i)
0662a2
			if (!Vector::coord_less(p0[i], p1[i]))
0662a2
				return true;
531f67
		return false;
531f67
	}
4dc49c
	inline PairT& sort() {
0662a2
		for(int i = 0; i < Vector::Count; ++i)
4dc49c
			if (p0[i] < p1[i])
0662a2
				std::swap(p0[i], p1[i]);
4dc49c
		return *this;
531f67
	}
0662a2
	inline PairT sorted() const
4dc49c
		{ return PairT(*this).sort(); }
4dc49c
4dc49c
	inline PairT& expand(const Vector &p) {
4dc49c
		for(int i = 0; i < Vector::Count; ++i) {
3a6f9b
			if (p[i] < p0[i]) p0[i] = p[i];
3a6f9b
			if (p1[i] < p[i]) p1[i] = p[i];
4dc49c
		}
4dc49c
		return *this;
4dc49c
	}
4dc49c
	inline PairT expanded(const Vector &p) const
4dc49c
		{ return PairT(*this).expand(p); }
4dc49c
4dc49c
	inline PairT& inflate(const Vector &p) {
4dc49c
		if (empty()) return *this;
4dc49c
		p0 -= p; p1 += p;
4dc49c
		return *this;
4dc49c
	}
4dc49c
	inline PairT inflated(const Vector &p) const
4dc49c
		{ return PairT(*this).inflate(p); }
531f67
};
531f67
531f67
0662a2
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
0662a2
typedef PairT<vector2> Pair2;</vector2>
0662a2
typedef PairT<vector3> Pair3;</vector3>
0662a2
typedef PairT<vector4> Pair4;</vector4>
0662a2
0662a2
typedef PairT<intvector2> IntPair2;</intvector2>
0662a2
typedef PairT<intvector3> IntPair3;</intvector3>
0662a2
typedef PairT<intvector4> IntPair4;</intvector4>
0662a2
0662a2
typedef PairT<longintvector2> LongIntPair2;</longintvector2>
0662a2
typedef PairT<longintvector3> LongIntPair3;</longintvector3>
0662a2
typedef PairT<longintvector4> LongIntPair4;</longintvector4>
0662a2
5f2fc2
typedef PairT<ulongintvector2> ULongIntPair2;</ulongintvector2>
5f2fc2
typedef PairT<ulongintvector3> ULongIntPair3;</ulongintvector3>
5f2fc2
typedef PairT<ulongintvector4> ULongIntPair4;</ulongintvector4>
5f2fc2
0662a2
531f67
#endif