From 8012694fc7f04f8e1020a6cbf3614035c3717651 Mon Sep 17 00:00:00 2001 From: Phillip Stephens Date: Wed, 7 Oct 2015 17:21:38 -0700 Subject: [PATCH] More reimplementations Move API to Zeus namespace --- CMakeLists.txt | 7 +++-- include/CAABox.hpp | 59 ++++++++++++++++++---------------------- include/CAxisAngle.hpp | 3 ++ include/CColor.hpp | 16 +++++++---- include/CFrustum.hpp | 4 ++- include/CMatrix3f.hpp | 5 +++- include/CMatrix4f.hpp | 3 ++ include/COBBox.hpp | 26 +++++++++++++----- include/CPlane.hpp | 13 ++++++++- include/CProjection.hpp | 4 ++- include/CQuaternion.hpp | 7 +++-- include/CRectangle.hpp | 3 ++ include/CRelAngle.hpp | 3 ++ include/CSphere.hpp | 17 ++++-------- include/CTransform.hpp | 52 ++++++++++++++--------------------- include/CUnitVector.hpp | 4 ++- include/CVector2f.hpp | 16 ++++++----- include/CVector3f.hpp | 47 ++++++++++++-------------------- include/CVector4f.hpp | 15 ++++++---- include/Math.hpp | 19 +++++++------ include/MathLib.hpp | 1 + include/TVectorUnion.hpp | 11 ++++++++ src/CAABox.cpp | 2 +- src/CColor.cpp | 18 ++++++------ src/CMatrix3f.cpp | 4 ++- src/CMatrix4f.cpp | 2 +- src/CProjection.cpp | 3 ++ src/CQuaternion.cpp | 15 ++++++---- src/CTransform.cpp | 3 ++ src/CVector2f.cpp | 8 ++++-- src/CVector3f.cpp | 7 +++-- src/Math.cpp | 29 +++++++++++++++++--- test/main.cpp | 5 ++++ 33 files changed, 253 insertions(+), 178 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9769e6d..4f0d460 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,7 +25,6 @@ add_library(Math src/CMatrix4f.cpp src/CAABox.cpp - include/CVector3f.hpp include/Math.hpp include/CQuaternion.hpp include/CMatrix3f.hpp @@ -37,11 +36,13 @@ add_library(Math include/CColor.hpp include/Global.hpp include/MathLib.hpp + include/TVectorUnion.hpp include/CVector2f.hpp + include/CVector3f.hpp + include/CVector3d.hpp + include/CVector4f.hpp include/CRectangle.hpp include/CMatrix4f.hpp - include/TVectorUnion.hpp - include/CVector4f.hpp include/CFrustum.hpp include/CAABox.hpp include/COBBox.hpp diff --git a/include/CAABox.hpp b/include/CAABox.hpp index 5b56afa..5fa3ffe 100644 --- a/include/CAABox.hpp +++ b/include/CAABox.hpp @@ -7,6 +7,8 @@ #include "Math.hpp" #include +namespace Zeus +{ class ZE_ALIGN(16) CAABox { public: @@ -107,26 +109,29 @@ public: return true; } - CVector3f center() const - { - return (m_min + m_max) * 0.5f; - } - CVector3f volume() const - { - return (m_max - m_min) * 0.5f; - } + CVector3f center() const {return (m_min + m_max) * 0.5f;} + + CVector3f volume() const {return (m_max - m_min) * 0.5f;} inline CAABox getTransformedAABox(const CTransform& xfrm) { CAABox box; - for (int i = 0; i < 8; i+=2) - { - CVector3f point = xfrm * getPoint(i); - box.accumulateBounds(point); - point = xfrm * getPoint(i+1); - box.accumulateBounds(point); - } - + CVector3f point = xfrm * getPoint(0); + box.accumulateBounds(point); + point = xfrm * getPoint(1); + box.accumulateBounds(point); + point = xfrm * getPoint(2); + box.accumulateBounds(point); + point = xfrm * getPoint(3); + box.accumulateBounds(point); + point = xfrm * getPoint(4); + box.accumulateBounds(point); + point = xfrm * getPoint(5); + box.accumulateBounds(point); + point = xfrm * getPoint(6); + box.accumulateBounds(point); + point = xfrm * getPoint(7); + box.accumulateBounds(point); return box; } @@ -138,7 +143,6 @@ public: m_min.y = point.y; if (m_min.z > point.z) m_min.z = point.z; - if (m_max.x < point.x) m_max.x = point.x; if (m_max.y < point.y) @@ -184,9 +188,9 @@ public: inline CVector3f clampToBox(const CVector3f& vec) { CVector3f ret = vec; - Math::clamp(ret.x, m_min.x, m_max.x); - Math::clamp(ret.y, m_min.y, m_max.y); - Math::clamp(ret.z, m_min.z, m_max.z); + Math::clamp(m_min.x, ret.x, m_max.x); + Math::clamp(m_min.y, ret.y, m_max.y); + Math::clamp(m_min.z, ret.z, m_max.z); return ret; } @@ -223,20 +227,11 @@ public: negZ.m_min = m_min; } - inline bool invalid() - { - return (m_max.x < m_min.x || m_max.y < m_min.y || m_max.z < m_min.z); - } + inline bool invalid() {return (m_max.x < m_min.x || m_max.y < m_min.y || m_max.z < m_min.z);} }; -inline bool operator ==(const CAABox& left, const CAABox& right) -{ - return (left.m_min == right.m_min && left.m_max == right.m_max); -} -inline bool operator !=(const CAABox& left, const CAABox& right) -{ - return (left.m_min != right.m_min || left.m_max != right.m_max); - +inline bool operator ==(const CAABox& left, const CAABox& right) {return (left.m_min == right.m_min && left.m_max == right.m_max);} +inline bool operator !=(const CAABox& left, const CAABox& right) {return (left.m_min != right.m_min || left.m_max != right.m_max);} } #endif // CAABOX_HPP diff --git a/include/CAxisAngle.hpp b/include/CAxisAngle.hpp index 53bae55..c9fe56f 100644 --- a/include/CAxisAngle.hpp +++ b/include/CAxisAngle.hpp @@ -4,6 +4,8 @@ #include "Global.hpp" #include "CVector3f.hpp" +namespace Zeus +{ struct ZE_ALIGN(16) CAxisAngle { ZE_DECLARE_ALIGNED_ALLOCATOR(); @@ -20,5 +22,6 @@ struct ZE_ALIGN(16) CAxisAngle CVector3f axis; float angle; }; +} #endif // CAXISANGLE_H diff --git a/include/CColor.hpp b/include/CColor.hpp index edc766c..52de5f4 100644 --- a/include/CColor.hpp +++ b/include/CColor.hpp @@ -11,7 +11,9 @@ #define COLOR(rgba) rgba #endif -typedef union _RGBA32 +namespace Zeus +{ +typedef union { struct { @@ -170,19 +172,19 @@ public: } inline void normalize() { - float mag = length(); + float mag = magnitude(); assert(mag != 0.0); mag = 1.0 / mag; *this *= mag; } inline CColor normalized() { - float mag = length(); + float mag = magnitude(); assert(mag != 0.0); mag = 1.0 / mag; return *this * mag; } - inline float lengthSquared() const + inline float magSquared() const { #if __SSE4_1__ TVectorUnion result; @@ -196,9 +198,9 @@ public: return r * r + g * g + b * b + a * a; #endif } - inline float length() const + inline float magnitude() const { - return sqrtf(lengthSquared()); + return sqrtf(magSquared()); } static inline CColor lerp(const CColor& a, const CColor& b, float t) { @@ -288,4 +290,6 @@ static inline CColor operator/(float lhs, const CColor& rhs) #endif } +} + #endif // CCOLOR_HPP diff --git a/include/CFrustum.hpp b/include/CFrustum.hpp index 00747e7..4cf13f7 100644 --- a/include/CFrustum.hpp +++ b/include/CFrustum.hpp @@ -4,6 +4,8 @@ #include "CPlane.hpp" #include "CAABox.hpp" +namespace Zeus +{ class CFrustum { CPlane m_planes[6]; @@ -123,5 +125,5 @@ public: } }; - +} #endif // CFRUSTUM_HPP diff --git a/include/CMatrix3f.hpp b/include/CMatrix3f.hpp index 7566864..ec55138 100644 --- a/include/CMatrix3f.hpp +++ b/include/CMatrix3f.hpp @@ -6,7 +6,8 @@ #include /* Column-major matrix class */ - +namespace Zeus +{ class CQuaternion; class ZE_ALIGN(16) CMatrix3f { @@ -106,4 +107,6 @@ public: CMatrix3f operator*(const CMatrix3f& lhs, const CMatrix3f& rhs); +} + #endif // CMATRIX3F_HPP diff --git a/include/CMatrix4f.hpp b/include/CMatrix4f.hpp index c3d8b0b..43751a3 100644 --- a/include/CMatrix4f.hpp +++ b/include/CMatrix4f.hpp @@ -4,6 +4,8 @@ #include "CVector4f.hpp" #include "CVector3f.hpp" +namespace Zeus +{ class ZE_ALIGN(16) CMatrix4f { public: @@ -178,6 +180,7 @@ static inline CMatrix4f operator*(const CMatrix4f& lhs, const CMatrix4f& rhs) #endif return ret; } +} #endif // CMATRIX4F diff --git a/include/COBBox.hpp b/include/COBBox.hpp index da81a59..c903074 100644 --- a/include/COBBox.hpp +++ b/include/COBBox.hpp @@ -5,6 +5,8 @@ #include "CVector3f.hpp" #include "CAABox.hpp" +namespace Zeus +{ class ZE_ALIGN(16) COBBox { public: @@ -36,17 +38,27 @@ public: {-1.0, 1.0, -1.0}, {-1.0, 1.0, 1.0} }; + CVector3f p = m_extents * basis[0]; - for (int i = 0; i < 8; i+=2) - { - CVector3f p = (m_extents * basis[i]); - ret.accumulateBounds(trans * p); - p = m_extents * basis[i + 1]; - ret.accumulateBounds(trans * p); - } + ret.accumulateBounds(trans * p); + p = m_extents * basis[1]; + ret.accumulateBounds(trans * p); + p = m_extents * basis[2]; + ret.accumulateBounds(trans * p); + p = m_extents * basis[3]; + ret.accumulateBounds(trans * p); + p = m_extents * basis[4]; + ret.accumulateBounds(trans * p); + p = m_extents * basis[5]; + ret.accumulateBounds(trans * p); + p = m_extents * basis[6]; + ret.accumulateBounds(trans * p); + p = m_extents * basis[7]; + ret.accumulateBounds(trans * p); return ret; } }; +} #endif diff --git a/include/CPlane.hpp b/include/CPlane.hpp index 4e3cbbd..46854b4 100644 --- a/include/CPlane.hpp +++ b/include/CPlane.hpp @@ -3,7 +3,10 @@ #include "Global.hpp" #include "CVector3f.hpp" +#include "Math.hpp" +namespace Zeus +{ class ZE_ALIGN(16) CPlane { public: @@ -28,11 +31,18 @@ public: #endif d = displacement; } + + float clipLineSegment(const CVector3f& a, const CVector3f& b) + { + float mag = ((b.z - a.z) * (((b.x - a.x) * ((b.y - a.y) * vec.y)) + vec.x)) + vec.z; + float dis = (-(vec.y - d)) / mag; + return Math::clamp(0.0f, dis, 1.0f); + } inline void normalize() { float nd = d; - float mag = vec.length(); + float mag = vec.magnitude(); assert(mag != 0.0f); mag = 1.0 / mag; vec *= mag; @@ -52,5 +62,6 @@ public: #endif }; }; +} #endif // CPLANE_HPP diff --git a/include/CProjection.hpp b/include/CProjection.hpp index d4e7ec9..ffa9c7f 100644 --- a/include/CProjection.hpp +++ b/include/CProjection.hpp @@ -7,7 +7,8 @@ #define _USE_MATH_DEFINES 1 #include - +namespace Zeus +{ enum EProjType { PROJ_NONE = 0, @@ -110,5 +111,6 @@ protected: CMatrix4f m_mtx; }; +} #endif // CMATRIX3F_HPP diff --git a/include/CQuaternion.hpp b/include/CQuaternion.hpp index 08b5142..6273dc8 100644 --- a/include/CQuaternion.hpp +++ b/include/CQuaternion.hpp @@ -8,6 +8,8 @@ #include #include +namespace Zeus +{ class ZE_ALIGN(16) CQuaternion { public: @@ -46,8 +48,8 @@ public: const CQuaternion& operator*=(const CQuaternion& q); const CQuaternion& operator*=(float scale); const CQuaternion& operator/=(float scale); - float length() const; - float lengthSquared() const; + float magnitude() const; + float magSquared() const; void normalize(); CQuaternion normalized() const; void invert(); @@ -106,4 +108,5 @@ public: CQuaternion operator+(float lhs, const CQuaternion& rhs); CQuaternion operator-(float lhs, const CQuaternion& rhs); CQuaternion operator*(float lhs, const CQuaternion& rhs); +} #endif // CQUATERNION_HPP diff --git a/include/CRectangle.hpp b/include/CRectangle.hpp index 66f7741..14abc8d 100644 --- a/include/CRectangle.hpp +++ b/include/CRectangle.hpp @@ -2,6 +2,8 @@ #define CRECTANGLE_HPP #include "CVector2f.hpp" +namespace Zeus +{ class CRectangle { public: @@ -28,5 +30,6 @@ public: CVector2f position; CVector2f size; }; +} #endif // CRECTANGLE_HPP diff --git a/include/CRelAngle.hpp b/include/CRelAngle.hpp index 2e61a23..58325f6 100644 --- a/include/CRelAngle.hpp +++ b/include/CRelAngle.hpp @@ -4,6 +4,8 @@ #include "CVector3f.hpp" #include "Math.hpp" +namespace Zeus +{ /** * @brief The CRelAngle class represents relative angles in radians */ @@ -26,5 +28,6 @@ public: { } }; +} #endif // CRELANGLE_HPP diff --git a/include/CSphere.hpp b/include/CSphere.hpp index 15dbdc1..cb68271 100644 --- a/include/CSphere.hpp +++ b/include/CSphere.hpp @@ -3,23 +3,15 @@ #include "CVector3f.hpp" +namespace Zeus +{ class ZE_ALIGN(16) CSphere { public: ZE_DECLARE_ALIGNED_ALLOCATOR(); - CSphere(const CVector3f& position, float radius) - { -#if __SSE__ - mVec128 = position.mVec128; -#endif - r = radius; - } - - inline CVector3f getSurfaceNormal(const CVector3f& coord) - { - return (vec - coord).normalized(); - } + CSphere(const CVector3f& position, float radius) { vec = position; r = radius; } + inline CVector3f getSurfaceNormal(const CVector3f& coord) { return (vec - coord).normalized(); } union { @@ -31,5 +23,6 @@ public: #endif }; }; +} #endif diff --git a/include/CTransform.hpp b/include/CTransform.hpp index 9cc2e47..127c21a 100644 --- a/include/CTransform.hpp +++ b/include/CTransform.hpp @@ -7,6 +7,8 @@ #include "CVector3f.hpp" #include "CQuaternion.hpp" +namespace Zeus +{ class ZE_ALIGN(16) CTransform { public: @@ -31,55 +33,40 @@ public: m_origin = position; } - inline void rotate(const CVector3f& euler) - { - *this = *this * CMatrix3f(CQuaternion(euler)); - } + inline void translate(float x, float y, float z) { translate({x, y, z}); } + + inline void rotate(const CVector3f& euler) { *this = *this * CMatrix3f(CQuaternion(euler)); } inline void scaleBy(float factor) - { - CTransform xfrm(CMatrix3f(CVector3f(factor, factor, factor))); - *this = *this * xfrm; - } + { CTransform xfrm(CMatrix3f(CVector3f(factor, factor, factor))); *this = *this * xfrm; } - inline void scale(const CVector3f& scale) + inline void scale(const CVector3f& factor) { m_basis = CMatrix3f(true); - m_basis[0][0] = scale.x; - m_basis[1][1] = scale.y; - m_basis[2][2] = scale.z; + m_basis[0][0] = factor.x; + m_basis[1][1] = factor.y; + m_basis[2][2] = factor.z; m_origin.zeroOut(); } - inline void scale(float x, float y, float z) - { - scale({x, y, z}); - } - inline void multiplyIgnoreTranslation(const CTransform& xfrm) - { - m_basis = m_basis*xfrm.m_basis; - } + inline void scale(float x, float y, float z) { scale({x, y, z}); } + inline void scale(float factor) { scale({factor, factor, factor}); } - inline CTransform getRotation() - { - CTransform ret = *this; - ret.m_origin.zeroOut(); - return ret; - } + inline void multiplyIgnoreTranslation(const CTransform& xfrm) { m_basis = m_basis*xfrm.m_basis; } + + inline CTransform getRotation() { CTransform ret = *this; ret.m_origin.zeroOut(); return ret; } + void setRotation(const CMatrix3f& mat) { m_basis = mat; } + void setRotation(const CTransform& xfrm) { setRotation(xfrm.m_basis); } /** * @brief buildMatrix3f Returns the stored matrix * buildMatrix3f is here for compliance with Retro's Math API * @return The Matrix (Neo, you are the one) */ - inline CMatrix3f buildMatrix3f() - { - return m_basis; - } + inline CMatrix3f buildMatrix3f() { return m_basis; } - inline CVector3f operator*(const CVector3f& other) const - {return m_origin + m_basis * other;} + inline CVector3f operator*(const CVector3f& other) const {return m_origin + m_basis * other;} inline void toMatrix4f(CMatrix4f& mat) const { @@ -125,5 +112,6 @@ static inline CTransform CTransformFromScaleVector(const CVector3f& scale) CTransform CTransformFromEditorEuler(const CVector3f& eulerVec); CTransform CTransformFromEditorEulers(const CVector3f& eulerVec, const CVector3f& origin); CTransform CTransformFromAxisAngle(const CVector3f& axis, float angle); +} #endif // CTRANSFORM_HPP diff --git a/include/CUnitVector.hpp b/include/CUnitVector.hpp index 94d8b5a..98084ec 100644 --- a/include/CUnitVector.hpp +++ b/include/CUnitVector.hpp @@ -3,6 +3,8 @@ #include "CVector3f.hpp" +namespace Zeus +{ class ZE_ALIGN(16) CUnitVector3f : public CVector3f { public: @@ -15,5 +17,5 @@ public: normalize(); } }; - +} #endif diff --git a/include/CVector2f.hpp b/include/CVector2f.hpp index f53064d..ddebabb 100644 --- a/include/CVector2f.hpp +++ b/include/CVector2f.hpp @@ -8,7 +8,8 @@ #include #include - +namespace Zeus +{ class ZE_ALIGN(16) CVector2f { public: @@ -197,7 +198,7 @@ class ZE_ALIGN(16) CVector2f } inline void normalize() { - float mag = length(); + float mag = magnitude(); if (mag > 1e-6f) { mag = 1.0 / mag; @@ -209,7 +210,7 @@ class ZE_ALIGN(16) CVector2f inline CVector2f normalized() const { - float mag = length(); + float mag = magnitude(); if (mag > 1e-6f) { mag = 1.0 / mag; @@ -236,7 +237,7 @@ class ZE_ALIGN(16) CVector2f return (x * rhs.x) + (y * rhs.y); #endif } - inline float lengthSquared() const + inline float magSquared() const { #if __SSE4_1__ TVectorUnion result; @@ -250,9 +251,9 @@ class ZE_ALIGN(16) CVector2f return x*x + y*y; #endif } - inline float length() const + inline float magnitude() const { - return sqrtf(lengthSquared()); + return sqrtf(magSquared()); } inline void zeroOut() @@ -288,7 +289,7 @@ class ZE_ALIGN(16) CVector2f inline bool isNormalized(float thresh = 1e-5f) const { - return (fabs(1.0f - lengthSquared()) <= thresh); + return (fabs(1.0f - magSquared()) <= thresh); } inline bool canBeNormalized() @@ -357,5 +358,6 @@ static inline CVector2f operator/(float lhs, const CVector2f& rhs) return CVector2f(lhs / rhs.x, lhs / rhs.y); #endif } +} #endif // CVECTOR2F_HPP diff --git a/include/CVector3f.hpp b/include/CVector3f.hpp index cdcee6d..192f45e 100644 --- a/include/CVector3f.hpp +++ b/include/CVector3f.hpp @@ -8,6 +8,8 @@ #include #include +namespace Zeus +{ class ZE_ALIGN(16) CVector3f { public: @@ -162,7 +164,7 @@ public: } inline void normalize() { - float mag = length(); + float mag = magnitude(); if (mag > 1e-6f) { mag = 1.0 / mag; @@ -173,7 +175,7 @@ public: } inline CVector3f normalized() const { - float mag = length(); + float mag = magnitude(); if (mag > 1e-6f) { mag = 1.0 / mag; @@ -182,9 +184,8 @@ public: return {0, 0, 0}; } inline CVector3f cross(const CVector3f& rhs) const - { - return CVector3f(y * rhs.z - z * rhs.y, z * rhs.x - x * rhs.z, x * rhs.y - y * rhs.x); - } + { return CVector3f(y * rhs.z - z * rhs.y, z * rhs.x - x * rhs.z, x * rhs.y - y * rhs.x); } + inline float dot(const CVector3f& rhs) const { #if __SSE4_1__ @@ -199,7 +200,7 @@ public: return (x * rhs.x) + (y * rhs.y) + (z * rhs.z); #endif } - inline float lengthSquared() const + inline float magSquared() const { #if __SSE4_1__ TVectorUnion result; @@ -213,10 +214,8 @@ public: return x*x + y*y + z*z; #endif } - inline float length() const - { - return sqrtf(lengthSquared()); - } + inline float magnitude() const + { return sqrtf(magSquared()); } inline void zeroOut() { @@ -240,34 +239,24 @@ public: static float getAngleDiff(const CVector3f& a, const CVector3f& b); static inline CVector3f lerp(const CVector3f& a, const CVector3f& b, float t) - { - return (a + (b - a) * t); - } + { return (a + (b - a) * t); } static inline CVector3f nlerp(const CVector3f& a, const CVector3f& b, float t) - { - return lerp(a, b, t).normalized(); - } + { return lerp(a, b, t).normalized(); } static CVector3f slerp(const CVector3f& a, const CVector3f& b, float t); //static CVector3f slerp(const CVector3f& a, const CVector3f& b, const CRelAngle& angle); inline bool isNormalized(float thresh = 1e-5f) const - { - return (fabs(1.0f - lengthSquared()) <= thresh); - } + { return (fabs(1.0f - magSquared()) <= thresh); } inline bool canBeNormalized() - { - return !isNormalized(); - } + { return !isNormalized(); } inline bool isZero() const - { - return lengthSquared() <= 1e-7; - } + { return magSquared() <= 1e-7; } inline void scaleToLength(float newLength) { - float length = lengthSquared(); + float length = magSquared(); if (length < 1e-6f) { x = newLength, y = 0.f, z = 0.f; @@ -298,10 +287,7 @@ public: union { - struct - { - float x, y, z; - }; + struct { float x, y, z; }; float v[4]; #if __SSE__ __m128 mVec128; @@ -353,5 +339,6 @@ static inline CVector3f operator/(float lhs, const CVector3f& rhs) return CVector3f(lhs / rhs.x, lhs / rhs.y, lhs / rhs.z); #endif } +} #endif // CVECTOR3F_HPP diff --git a/include/CVector4f.hpp b/include/CVector4f.hpp index d211d3a..1310eeb 100644 --- a/include/CVector4f.hpp +++ b/include/CVector4f.hpp @@ -9,6 +9,8 @@ #include #include +namespace Zeus +{ class ZE_ALIGN(16) CVector4f { public: @@ -221,7 +223,7 @@ class ZE_ALIGN(16) CVector4f } inline void normalize() { - float mag = length(); + float mag = magnitude(); if (mag > 1e-6f) { mag = 1.0 / mag; @@ -232,7 +234,7 @@ class ZE_ALIGN(16) CVector4f } inline CVector4f normalized() const { - float mag = length(); + float mag = magnitude(); if (mag > 1e-6f) { mag = 1.0 / mag; @@ -255,7 +257,7 @@ class ZE_ALIGN(16) CVector4f return (x * rhs.x) + (y * rhs.y) + (z * rhs.z) + (w * rhs.w); #endif } - inline float lengthSquared() const + inline float magSquared() const { #if __SSE4_1__ TVectorUnion result; @@ -269,9 +271,9 @@ class ZE_ALIGN(16) CVector4f return x*x + y*y + z*z + w*w; #endif } - inline float length() const + inline float magnitude() const { - return sqrtf(lengthSquared()); + return sqrtf(magSquared()); } inline void zeroOut() @@ -304,7 +306,7 @@ class ZE_ALIGN(16) CVector4f inline bool isNormalized(float thresh = 1e-5f) const { - return (fabs(1.0f - lengthSquared()) <= thresh); + return (fabs(1.0f - magSquared()) <= thresh); } inline bool canBeNormalized() @@ -373,5 +375,6 @@ static inline CVector4f operator/(float lhs, const CVector4f& rhs) return CVector4f(lhs / rhs.x, lhs / rhs.y, lhs / rhs.z, lhs / rhs.w); #endif } +} #endif // CVECTOR4F_HPP diff --git a/include/Math.hpp b/include/Math.hpp index 3d6ab70..506824d 100644 --- a/include/Math.hpp +++ b/include/Math.hpp @@ -6,18 +6,12 @@ #include "CVector3f.hpp" #include "CTransform.hpp" -/* Macros for min/max. */ -#ifndef MIN -#define MIN(a,b) (((a)<(b))?(a):(b)) -#endif /* MIN */ -#ifndef MAX -#define MAX(a,b) (((a)>(b))?(a):(b)) -#endif /* MAX */ - +namespace Zeus +{ namespace Math { template - inline T clamp(T min, T val, T max) {return MAX(min, MIN(max, val));} + inline T clamp(T min, T val, T max) {return std::max(min, std::min(max, val));} inline float radToDeg(float rad) {return rad * 180.f / M_PI;} inline float degToRad(float deg) {return deg * M_PI / 180;} @@ -26,10 +20,17 @@ namespace Math inline CVector3f radToDeg(CVector3f rad) {return rad * kRadToDegVec;} inline CVector3f degToRad(CVector3f deg) {return deg * kDegToRadVec;} + // Since round(double) doesn't exist in some implementations + // we'll define our own + inline float round(double val) { return (val < 0.0 ? floor(val - 0.5) : floor(val + 0.5)); } + extern const CVector3f kUpVec; CTransform lookAt(const CVector3f& pos, const CVector3f& lookPos, const CVector3f& up=kUpVec); inline CVector3f baryToWorld(const CVector3f& p0, const CVector3f& p1, const CVector3f& p2, const CVector3f& bary) { return bary.x * p0 + bary.y * p1 + bary.z * p2; } + + inline CVector3f getBezierPoint(const CVector3f& a, const CVector3f& b, const CVector3f& c, const CVector3f& d, float t); +} } #endif // MATH_HPP diff --git a/include/MathLib.hpp b/include/MathLib.hpp index 7abb61a..b25cf5c 100644 --- a/include/MathLib.hpp +++ b/include/MathLib.hpp @@ -10,6 +10,7 @@ #include "CQuaternion.hpp" #include "CVector2f.hpp" #include "CVector3f.hpp" +#include "CVector3d.hpp" #include "CVector4f.hpp" #include "CUnitVector.hpp" #include "CRectangle.hpp" diff --git a/include/TVectorUnion.hpp b/include/TVectorUnion.hpp index 3cdee5a..a277083 100644 --- a/include/TVectorUnion.hpp +++ b/include/TVectorUnion.hpp @@ -1,6 +1,8 @@ #ifndef TVECTORUNION #define TVECTORUNION +namespace Zeus +{ typedef union { float v[4]; @@ -9,5 +11,14 @@ typedef union #endif } TVectorUnion; +typedef union +{ + double v[4]; +#if __SSE__ + __m128d mVec128[2]; +#endif +} TDblVectorUnion; +} + #endif // TVECTORUNION diff --git a/src/CAABox.cpp b/src/CAABox.cpp index 779a606..e22eb9a 100644 --- a/src/CAABox.cpp +++ b/src/CAABox.cpp @@ -1,2 +1,2 @@ #include "CAABox.hpp" -const CAABox CAABox::skInvertedBox = CAABox(); +const Zeus::CAABox Zeus::CAABox::skInvertedBox = CAABox(); diff --git a/src/CColor.cpp b/src/CColor.cpp index b986d59..baf8c50 100644 --- a/src/CColor.cpp +++ b/src/CColor.cpp @@ -1,11 +1,11 @@ #include "CColor.hpp" -const CColor CColor::skRed (COLOR(0xFF0000FF)); -const CColor CColor::skBlack (COLOR(0x000000FF)); -const CColor CColor::skBlue (COLOR(0x0000FFFF)); -const CColor CColor::skGreen (COLOR(0x00FF00FF)); -const CColor CColor::skGrey (COLOR(0x808080FF)); -const CColor CColor::skOrange(COLOR(0xFF7000FF)); -const CColor CColor::skPurple(COLOR(0xA000FFFF)); -const CColor CColor::skYellow(COLOR(0xFFFF00FF)); -const CColor CColor::skWhite (COLOR(0xFFFFFFFF)); +const Zeus::CColor Zeus::CColor::skRed (COLOR(0xFF0000FF)); +const Zeus::CColor Zeus::CColor::skBlack (COLOR(0x000000FF)); +const Zeus::CColor Zeus::CColor::skBlue (COLOR(0x0000FFFF)); +const Zeus::CColor Zeus::CColor::skGreen (COLOR(0x00FF00FF)); +const Zeus::CColor Zeus::CColor::skGrey (COLOR(0x808080FF)); +const Zeus::CColor Zeus::CColor::skOrange(COLOR(0xFF7000FF)); +const Zeus::CColor Zeus::CColor::skPurple(COLOR(0xA000FFFF)); +const Zeus::CColor Zeus::CColor::skYellow(COLOR(0xFFFF00FF)); +const Zeus::CColor Zeus::CColor::skWhite (COLOR(0xFFFFFFFF)); diff --git a/src/CMatrix3f.cpp b/src/CMatrix3f.cpp index 0ac3150..e5b179d 100644 --- a/src/CMatrix3f.cpp +++ b/src/CMatrix3f.cpp @@ -2,6 +2,8 @@ #include "CQuaternion.hpp" #include "Global.hpp" +namespace Zeus +{ const CMatrix3f CMatrix3f::skIdentityMatrix3f = CMatrix3f(); CMatrix3f::CMatrix3f(const CQuaternion& quat) @@ -136,4 +138,4 @@ CMatrix3f CMatrix3f::inverted() const -(m[0][0]*m[1][2] - m[0][2]*m[1][0]) * det, (m[0][0]*m[1][1] - m[0][1]*m[1][0]) * det); } - +} diff --git a/src/CMatrix4f.cpp b/src/CMatrix4f.cpp index ef78109..821876d 100644 --- a/src/CMatrix4f.cpp +++ b/src/CMatrix4f.cpp @@ -1,3 +1,3 @@ #include "CMatrix4f.hpp" -const CMatrix4f CMatrix4f::skIdentityMatrix4f = CMatrix4f(); +const Zeus::CMatrix4f Zeus::CMatrix4f::skIdentityMatrix4f = CMatrix4f(); diff --git a/src/CProjection.cpp b/src/CProjection.cpp index cdae359..cf3af9a 100644 --- a/src/CProjection.cpp +++ b/src/CProjection.cpp @@ -2,6 +2,8 @@ #include #include +namespace Zeus +{ void CProjection::_updateCachedMatrix() { if (m_projType == PROJ_ORTHO) @@ -65,4 +67,5 @@ void CProjection::_updateCachedMatrix() abort(); } } +} diff --git a/src/CQuaternion.cpp b/src/CQuaternion.cpp index 419ffa2..c628af1 100644 --- a/src/CQuaternion.cpp +++ b/src/CQuaternion.cpp @@ -1,6 +1,8 @@ #include "CQuaternion.hpp" #include +namespace Zeus +{ void CQuaternion::fromVector3f(const CVector3f& vec) { float cosX = cosf(0.5 * vec.x); @@ -102,24 +104,24 @@ const CQuaternion& CQuaternion::operator/=(float scale) return *this; } -float CQuaternion::length() const +float CQuaternion::magnitude() const { - return sqrt(lengthSquared()); + return sqrt(magSquared()); } -float CQuaternion::lengthSquared() const +float CQuaternion::magSquared() const { return (r*r + (v.dot(v))); } void CQuaternion::normalize() { - *this /= length(); + *this /= magnitude(); } CQuaternion CQuaternion::normalized() const { - return *this/length(); + return *this/magnitude(); } void CQuaternion::invert() @@ -170,7 +172,7 @@ CQuaternion CQuaternion::log() const CQuaternion CQuaternion::exp() const { - float a = (v.length()); + float a = (v.magnitude()); float sina = sinf(a); float cosa = cos(a); CQuaternion ret; @@ -249,3 +251,4 @@ CQuaternion operator*(float lhs, const CQuaternion& rhs) { return CQuaternion(lhs * rhs.r, lhs * rhs.v); } +} diff --git a/src/CTransform.cpp b/src/CTransform.cpp index 2340347..c3bc042 100644 --- a/src/CTransform.cpp +++ b/src/CTransform.cpp @@ -1,5 +1,7 @@ #include "CTransform.hpp" +namespace Zeus +{ CTransform CTransformFromEditorEuler(const CVector3f& eulerVec) { CTransform result; @@ -64,3 +66,4 @@ CTransform CTransformFromEditorEulers(const CVector3f& eulerVec, const CVector3f ret.m_origin = origin; return ret; } +} diff --git a/src/CVector2f.cpp b/src/CVector2f.cpp index 557a8aa..932117e 100644 --- a/src/CVector2f.cpp +++ b/src/CVector2f.cpp @@ -4,14 +4,16 @@ #include #include "Math.hpp" +namespace Zeus +{ const CVector2f CVector2f::skOne = CVector2f(1.0); const CVector2f CVector2f::skNegOne = CVector2f(-1.0); const CVector2f CVector2f::skZero; float CVector2f::getAngleDiff(const CVector2f& a, const CVector2f& b) { - float mag1 = a.length(); - float mag2 = b.length(); + float mag1 = a.magnitude(); + float mag2 = b.magnitude(); if (!mag1 || !mag2) return 0; @@ -47,4 +49,4 @@ CVector2f CVector2f::slerp(const CVector2f& a, const CVector2f& b, float t) } return a; } - +} diff --git a/src/CVector3f.cpp b/src/CVector3f.cpp index 5869dea..9dfb9d5 100644 --- a/src/CVector3f.cpp +++ b/src/CVector3f.cpp @@ -4,14 +4,16 @@ #include #include "Math.hpp" +namespace Zeus +{ const CVector3f CVector3f::skOne = CVector3f(1.0); const CVector3f CVector3f::skNegOne = CVector3f(-1.0); const CVector3f CVector3f::skZero; float CVector3f::getAngleDiff(const CVector3f& a, const CVector3f& b) { - float mag1 = a.length(); - float mag2 = b.length(); + float mag1 = a.magnitude(); + float mag2 = b.magnitude(); if (!mag1 || !mag2) return 0; @@ -50,3 +52,4 @@ CVector3f CVector3f::slerp(const CVector3f& a, const CVector3f& b, float t) } return a; } +} diff --git a/src/Math.cpp b/src/Math.cpp index 15b3608..ed61fcb 100644 --- a/src/Math.cpp +++ b/src/Math.cpp @@ -1,10 +1,14 @@ #include "Math.hpp" -const CVector3f Math::kUpVec(0.0, 0.0, 1.0); -const CVector3f Math::kRadToDegVec(180.0f / M_PI); -const CVector3f Math::kDegToRadVec(M_PI / 180.0f); +namespace Zeus +{ +namespace Math +{ +const CVector3f kUpVec(0.0, 0.0, 1.0); +const CVector3f kRadToDegVec(180.0f / M_PI); +const CVector3f kDegToRadVec(M_PI / 180.0f); -CTransform Math::lookAt(const CVector3f& pos, const CVector3f& lookPos, const CVector3f& up) +CTransform lookAt(const CVector3f& pos, const CVector3f& lookPos, const CVector3f& up) { CVector3f vLook,vRight,vUp; @@ -19,3 +23,20 @@ CTransform Math::lookAt(const CVector3f& pos, const CVector3f& lookPos, const CV CMatrix3f rmBasis(vRight, vUp, vLook); return CTransform(rmBasis.transposed(), CVector3f(-pos.dot(vRight), -pos.dot(vUp), -pos.dot(vLook))); } + +CVector3f getBezierPoint(const CVector3f& r4, const CVector3f& r5, const CVector3f& r6, const CVector3f& r7, float f1) +{ + //TODO: This isn't quite right, figure out what's really going on and reimplement +#if 0 + float inv = 1.0 - f1; + CVector3f r3; + r3.x = ((((((r4.x * (r5.x * f1)) + inv) * (((r5.x * (r6.x * f1)) + inv) * f1)) + inv) * (((((r5.x * (r6.x * f1)) + inv) * (((r6.x * (r7.x * f1)) + inv) * f1)) + inv) * f1)) + inv); + r3.y = ((((((r4.y * (r5.y * f1)) + inv) * (((r5.y * (r6.y * f1)) + inv) * f1)) + inv) * (((((r5.y * (r6.y * f1)) + inv) * (((r6.y * (r7.y * f1)) + inv) * f1)) + inv) * f1)) + inv); + r3.z = ((((((r4.z * (r5.z * f1)) + inv) * (((r5.z * (r6.z * f1)) + inv) * f1)) + inv) * (((((r5.z * (r6.z * f1)) + inv) * (((r6.z * (r7.z * f1)) + inv) * f1)) + inv) * f1)) + inv); + + return r3; +#endif + return CVector3f::skZero; +} +} +} diff --git a/test/main.cpp b/test/main.cpp index d5e8513..97c822d 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -1,6 +1,9 @@ #include #include +// This is only for testing, do NOT do this normally +using namespace Zeus; + int main() { assert(!CAABox({100, 100, 100}, {100, 100, 100}).invalid()); @@ -26,5 +29,7 @@ int main() assert(test3.inside(test)); assert(!test4.inside(test)); + std::cout << Math::round(1.5) << std::endl; + return 0; }