From b4bf13aa4f004a7819e262679a295d8ca886557b Mon Sep 17 00:00:00 2001 From: andrew Date: Tue, 11 Mar 2014 16:01:17 +0200 Subject: Unified Vector classes --- src/Vector3.h | 264 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 264 insertions(+) create mode 100644 src/Vector3.h (limited to 'src/Vector3.h') diff --git a/src/Vector3.h b/src/Vector3.h new file mode 100644 index 000000000..0be52248d --- /dev/null +++ b/src/Vector3.h @@ -0,0 +1,264 @@ + +#pragma once + + + + +#include + + + + + + + + + +template +// tolua_begin +class Vector3 +{ + + TOLUA_TEMPLATE_BIND((T, int, float, double)) + +public: + + T x, y, z; + + + inline Vector3() : x(0), y(0), z(0) {} + inline Vector3(T a_x, T a_y, T a_z) : x(a_x), y(a_y), z(a_z) {} + + + // tolua_end + template + Vector3(const Vector3<_T> & a_Rhs) : x(a_Rhs.x), y(a_Rhs.y), z(a_Rhs.z) {} + + template + Vector3(const Vector3<_T> * a_Rhs) : x(a_Rhs->x), y(a_Rhs->y), z(a_Rhs->z) {} + // tolua_begin + + + inline void Set(T a_x, T a_y, T a_z) + { + x = a_x; + y = a_y; + z = a_z; + } + + inline void Normalize(void) + { + T Len = 1.0 / Length(); + + x *= Len; + y *= Len; + z *= Len; + } + + inline Vector3 NormalizeCopy(void) const + { + T Len = 1.0 / Length(); + + return Vector3( + x * Len, + y * Len, + z * Len + ); + } + + inline void NormalizeCopy(Vector3 & a_Rhs) const + { + T Len = 1.0 / Length(); + + a_Rhs.Set( + x * Len, + y * Len, + z * Len + ); + } + + inline T Length(void) const + { + return sqrt(x * x + y * y + z * z); + } + + inline T SqrLength(void) const + { + return x * x + y * y + z * z; + } + + inline T Dot(const Vector3 & a_Rhs) const + { + return x * a_Rhs.x + y * a_Rhs.y + z * a_Rhs.z; + } + + inline Vector3 Cross(const Vector3 & a_Rhs) const + { + return Vector3( + y * a_Rhs.z - z * a_Rhs.y, + z * a_Rhs.x - x * a_Rhs.z, + x * a_Rhs.y - y * a_Rhs.x + ); + } + + inline bool Equals(const Vector3 & a_Rhs) const + { + return x == a_Rhs.x && y == a_Rhs.y && z == a_Rhs.z; + } + + inline bool operator < (const Vector3 & a_Rhs) + { + // return (x < a_Rhs.x) && (y < a_Rhs.y) && (z < a_Rhs.z); ? + return (x < a_Rhs.x) || (x == a_Rhs.x && y < a_Rhs.y) || (x == a_Rhs.x && y == a_Rhs.y && z < a_Rhs.z); + } + + inline void Move(T a_X, T a_Y, T a_Z) + { + x += a_X; + y += a_Y; + z += a_Z; + } + + // tolua_end + + inline void operator += (const Vector3 & a_Rhs) + { + x += a_Rhs.x; + y += a_Rhs.y; + z += a_Rhs.z; + } + + inline void operator -= (const Vector3 & a_Rhs) + { + x -= a_Rhs.x; + y -= a_Rhs.y; + z -= a_Rhs.z; + } + + inline void operator *= (const Vector3 & a_Rhs) + { + x *= a_Rhs.x; + y *= a_Rhs.y; + z *= a_Rhs.z; + } + + inline void operator *= (T a_v) + { + x *= a_v; + y *= a_v; + z *= a_v; + } + + // tolua_begin + + inline Vector3 operator + (const Vector3& a_Rhs) const + { + return Vector3( + x + a_Rhs.x, + y + a_Rhs.y, + z + a_Rhs.z + ); + } + + inline Vector3 operator - (const Vector3& a_Rhs) const + { + return Vector3( + x - a_Rhs.x, + y - a_Rhs.y, + z - a_Rhs.z + ); + } + + inline Vector3 operator * (const Vector3& a_Rhs) const + { + return Vector3( + x * a_Rhs.x, + y * a_Rhs.y, + z * a_Rhs.z + ); + } + + inline Vector3 operator * (T a_v) const + { + return Vector3( + x * a_v, + y * a_v, + z * a_v + ); + } + + inline Vector3 operator / (T a_v) const + { + return Vector3( + x / a_v, + y / a_v, + z / a_v + ); + } + + inline double LineCoeffToXYPlane(const Vector3 & a_OtherEnd, T a_Z) const + { + if (abs(z - a_OtherEnd.z) < EPS) + { + return NO_INTERSECTION; + } + + return (a_Z - z) / (a_OtherEnd.z - z); + } + + inline double LineCoeffToXZPlane(const Vector3 & a_OtherEnd, T a_Y) const + { + if (abs(y - a_OtherEnd.y) < EPS) + { + return NO_INTERSECTION; + } + + return (a_Y - y) / (a_OtherEnd.y - y); + } + + inline double LineCoeffToYZPlane(const Vector3 & a_OtherEnd, T a_X) const + { + if (abs(x - a_OtherEnd.x) < EPS) + { + return NO_INTERSECTION; + } + + return (a_X - x) / (a_OtherEnd.x - x); + } + + /** The max difference between two coords for which the coords are assumed equal. */ + static const double EPS; + + /** Return value of LineCoeffToPlane() if the line is parallel to the plane. */ + static const double NO_INTERSECTION; +}; +// tolua_end + +template +const double Vector3::EPS = 0.000001; + +template +const double Vector3::NO_INTERSECTION = 1e70; + + + + + +// tolua_begin +typedef Vector3 Vector3d; +typedef Vector3 Vector3f; +typedef Vector3 Vector3i; +// tolua_end + + + + + +typedef std::list cVector3iList; +typedef std::vector cVector3iArray; + + + + + + -- cgit v1.2.3 From d64db443c2d0f094c4ee098fa1ffacb2c92e5d88 Mon Sep 17 00:00:00 2001 From: andrew Date: Tue, 11 Mar 2014 18:10:15 +0200 Subject: LineCoeff Doc --- src/Vector3.h | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'src/Vector3.h') diff --git a/src/Vector3.h b/src/Vector3.h index 0be52248d..8ee7a87b0 100644 --- a/src/Vector3.h +++ b/src/Vector3.h @@ -10,10 +10,6 @@ - - - - template // tolua_begin class Vector3 @@ -196,6 +192,11 @@ public: ); } + /** Returns the coefficient for the (a_OtherEnd - this) line to reach the specified Z coord. + The result satisfies the following equation: + (*this + Result * (a_OtherEnd - *this)).z = a_Z + If the line is too close to being parallel, this function returns NO_INTERSECTION + */ inline double LineCoeffToXYPlane(const Vector3 & a_OtherEnd, T a_Z) const { if (abs(z - a_OtherEnd.z) < EPS) @@ -206,6 +207,11 @@ public: return (a_Z - z) / (a_OtherEnd.z - z); } + /** Returns the coefficient for the (a_OtherEnd - this) line to reach the specified Y coord. + The result satisfies the following equation: + (*this + Result * (a_OtherEnd - *this)).y = a_Y + If the line is too close to being parallel, this function returns NO_INTERSECTION + */ inline double LineCoeffToXZPlane(const Vector3 & a_OtherEnd, T a_Y) const { if (abs(y - a_OtherEnd.y) < EPS) @@ -216,6 +222,11 @@ public: return (a_Y - y) / (a_OtherEnd.y - y); } + /** Returns the coefficient for the (a_OtherEnd - this) line to reach the specified X coord. + The result satisfies the following equation: + (*this + Result * (a_OtherEnd - *this)).x = a_X + If the line is too close to being parallel, this function returns NO_INTERSECTION + */ inline double LineCoeffToYZPlane(const Vector3 & a_OtherEnd, T a_X) const { if (abs(x - a_OtherEnd.x) < EPS) @@ -231,9 +242,14 @@ public: /** Return value of LineCoeffToPlane() if the line is parallel to the plane. */ static const double NO_INTERSECTION; + }; // tolua_end + + + + template const double Vector3::EPS = 0.000001; -- cgit v1.2.3 From 9810d57a394b2bb56fbcfddb05d34dcd504a7b35 Mon Sep 17 00:00:00 2001 From: andrew Date: Tue, 11 Mar 2014 18:32:33 +0200 Subject: Unified Matrix4 code --- src/Vector3.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Vector3.h') diff --git a/src/Vector3.h b/src/Vector3.h index 8ee7a87b0..841c3e7d1 100644 --- a/src/Vector3.h +++ b/src/Vector3.h @@ -22,7 +22,7 @@ public: T x, y, z; - inline Vector3() : x(0), y(0), z(0) {} + inline Vector3(void) : x(0), y(0), z(0) {} inline Vector3(T a_x, T a_y, T a_z) : x(a_x), y(a_y), z(a_z) {} -- cgit v1.2.3 From abf4effaaf0eff1a98e005512f805211c2fad9a7 Mon Sep 17 00:00:00 2001 From: andrew Date: Tue, 11 Mar 2014 21:58:50 +0200 Subject: Matrix4: Removed enum --- src/Vector3.h | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/Vector3.h') diff --git a/src/Vector3.h b/src/Vector3.h index 841c3e7d1..79c47ae77 100644 --- a/src/Vector3.h +++ b/src/Vector3.h @@ -26,6 +26,12 @@ public: inline Vector3(T a_x, T a_y, T a_z) : x(a_x), y(a_y), z(a_z) {} + // Hardcoded copy constructors (tolua++ does not support function templates .. yet) + Vector3(const Vector3 & a_Rhs) : x(a_Rhs.x), y(a_Rhs.y), z(a_Rhs.z) {} + Vector3(const Vector3 & a_Rhs) : x(a_Rhs.x), y(a_Rhs.y), z(a_Rhs.z) {} + Vector3(const Vector3 & a_Rhs) : x(a_Rhs.x), y(a_Rhs.y), z(a_Rhs.z) {} + + // tolua_end template Vector3(const Vector3<_T> & a_Rhs) : x(a_Rhs.x), y(a_Rhs.y), z(a_Rhs.z) {} -- cgit v1.2.3 From a3a94436dcc1cc95c1476c8a88400b16ac279e71 Mon Sep 17 00:00:00 2001 From: andrew Date: Wed, 12 Mar 2014 15:13:19 +0200 Subject: Vector3: Length() should always return a float --- src/Vector3.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src/Vector3.h') diff --git a/src/Vector3.h b/src/Vector3.h index 79c47ae77..80583879a 100644 --- a/src/Vector3.h +++ b/src/Vector3.h @@ -27,9 +27,9 @@ public: // Hardcoded copy constructors (tolua++ does not support function templates .. yet) - Vector3(const Vector3 & a_Rhs) : x(a_Rhs.x), y(a_Rhs.y), z(a_Rhs.z) {} - Vector3(const Vector3 & a_Rhs) : x(a_Rhs.x), y(a_Rhs.y), z(a_Rhs.z) {} - Vector3(const Vector3 & a_Rhs) : x(a_Rhs.x), y(a_Rhs.y), z(a_Rhs.z) {} + Vector3(const Vector3 & a_Rhs) : x((T) a_Rhs.x), y((T) a_Rhs.y), z((T) a_Rhs.z) {} + Vector3(const Vector3 & a_Rhs) : x((T) a_Rhs.x), y((T) a_Rhs.y), z((T) a_Rhs.z) {} + Vector3(const Vector3 & a_Rhs) : x((T) a_Rhs.x), y((T) a_Rhs.y), z((T) a_Rhs.z) {} // tolua_end @@ -50,7 +50,7 @@ public: inline void Normalize(void) { - T Len = 1.0 / Length(); + double Len = 1.0 / Length(); x *= Len; y *= Len; @@ -59,7 +59,7 @@ public: inline Vector3 NormalizeCopy(void) const { - T Len = 1.0 / Length(); + double Len = 1.0 / Length(); return Vector3( x * Len, @@ -70,7 +70,7 @@ public: inline void NormalizeCopy(Vector3 & a_Rhs) const { - T Len = 1.0 / Length(); + double Len = 1.0 / Length(); a_Rhs.Set( x * Len, @@ -79,12 +79,12 @@ public: ); } - inline T Length(void) const + inline double Length(void) const { - return sqrt(x * x + y * y + z * z); + return sqrt((double)(x * x + y * y + z * z)); } - inline T SqrLength(void) const + inline double SqrLength(void) const { return x * x + y * y + z * z; } -- cgit v1.2.3