Humungous refactor

This commit is contained in:
Jack Andersen
2016-03-04 13:03:26 -10:00
parent cd28979d0a
commit a76d43d5df
44 changed files with 465 additions and 479 deletions

View File

@@ -1,115 +0,0 @@
#ifndef MATH_HPP
#define MATH_HPP
#undef min
#undef max
#ifndef NOMINMAX
#define NOMINMAX 1
#endif
#ifndef _USE_MATH_DEFINES
#define _USE_MATH_DEFINES 1
#endif
#include <math.h>
#include <algorithm>
namespace Zeus
{
struct CPUInfo
{
const char cpuBrand [48] = {0};
const char cpuVendor[32] = {0};
const bool isIntel = false;
const bool SSE1 = false;
const bool SSE2 = false;
const bool SSE3 = false;
const bool SSSE3 = false;
const bool SSE41 = false;
const bool SSE42 = false;
const bool SSE4a = false;
const bool AESNI = false;
};
/**
* Detects CPU capabilities and returns true if SSE4.1 or SSE4.2 is available
*/
void detectCPU();
const CPUInfo& cpuFeatures();
class CVector3f;
class CTransform;
namespace Math
{
template<typename T>
inline T min(T a, T b) { return a < b ? a : b; }
template<typename T>
inline T max(T a, T b) { return a > b ? a : b; }
template<typename T>
inline T clamp(T a, T val, T b) {return max<T>(a, min<T>(b, val));}
inline float radToDeg(float rad) {return rad * 180.f / M_PI;}
inline float degToRad(float deg) {return deg * M_PI / 180;}
extern const CVector3f kRadToDegVec;
extern const CVector3f kDegToRadVec;
CVector3f radToDeg(const CVector3f& rad);
CVector3f degToRad(const CVector3f& deg);
extern const CVector3f kUpVec;
CTransform lookAt(const CVector3f& pos, const CVector3f& lookPos, const CVector3f& up=kUpVec);
CVector3f baryToWorld(const CVector3f& p0, const CVector3f& p1, const CVector3f& p2, const CVector3f& bary);
CVector3f getBezierPoint(const CVector3f& a, const CVector3f& b,
const CVector3f& c, const CVector3f& d, float t);
float getCatmullRomSplinePoint(float a, float b,
float c, float d, float t);
CVector3f getCatmullRomSplinePoint(const CVector3f& a, const CVector3f& b,
const CVector3f& c, const CVector3f& d, float t);
CVector3f getRoundCatmullRomSplinePoint(const CVector3f& a, const CVector3f& b,
const CVector3f& c, const CVector3f& d, float t);
inline float slowCosineR(float val) { return float(cos(val)); }
inline float slowSineR(float val) { return float(sin(val)); }
inline float slowTangentR(float val) { return float(tan(val)); }
inline float arcSineR(float val) { return float(asin(val)); }
inline float arcTangentR(float val) { return float(atan(val)); }
inline float arcCosineR(float val) { return float(acos(val)); }
inline float powF(float a, float b) { return float(exp(b * log(a))); }
inline float floorF(float val) { return float(floor(val)); }
inline float ceilingF(float val)
{
float tmp = floorF(val);
return (tmp == val ? tmp : tmp + 1.0);
}
// Since round(double) doesn't exist in some <cmath> implementations
// we'll define our own
inline double round(double val) { return (val < 0.0 ? ceilingF(val - 0.5) : floorF(val + 0.5)); }
inline double powD(float a, float b) { return exp(b * log(a)); }
double sqrtD(double val);
inline double invSqrtD(double val) { return 1.0 / sqrtD(val); }
inline float invSqrtF(float val) { return float(1.0 / sqrtD(val)); }
inline float sqrtF(float val) { return float(sqrtD(val)); }
float fastArcCosR(float val);
float fastCosR(float val);
float fastSinR(float val);
int floorPowerOfTwo(int x);
int ceilingPowerOfTwo(int x);
template <class T>
inline int PopCount(T x)
{
using U = std::make_unsigned_t<std::conditional_t<std::is_enum<T>::value, std::underlying_type_t<T>, T>>;
U cx = U(x);
const U m1 = U(0x5555555555555555); //binary: 0101...
const U m2 = U(0x3333333333333333); //binary: 00110011..
const U m4 = U(0x0f0f0f0f0f0f0f0f); //binary: 4 zeros, 4 ones ...
const U h01 = U(0x0101010101010101); //the sum of 256 to the power of 0,1,2,3...
cx -= (cx >> 1) & m1; //put count of each 2 bits into those 2 bits
cx = (cx & m2) + ((cx >> 2) & m2); //put count of each 4 bits into those 4 bits
cx = (cx + (cx >> 4)) & m4; //put count of each 8 bits into those 8 bits
return (cx * h01) >> ((sizeof(U)-1)*8); //returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24) + ...
}
}
}
#endif // MATH_HPP

View File

@@ -1,27 +0,0 @@
#ifndef __MATHLIB_HPP
#define __MATHLIB_HPP
#include "CAxisAngle.hpp"
#include "CRelAngle.hpp"
#include "CMatrix3f.hpp"
#include "CMatrix4f.hpp"
#include "CProjection.hpp"
#include "CTransform.hpp"
#include "CQuaternion.hpp"
#include "CVector2f.hpp"
#include "CVector3f.hpp"
#include "CVector3d.hpp"
#include "CVector4f.hpp"
#include "CUnitVector.hpp"
#include "CRectangle.hpp"
#include "CPlane.hpp"
#include "CLine.hpp"
#include "CAABox.hpp"
#include "COBBox.hpp"
#include "CSphere.hpp"
#include "CFrustum.hpp"
#include "CColor.hpp"
#include "Global.hpp"
#include "Math.hpp"
#endif // __MATHLIB_HPP

View File

@@ -1,18 +1,18 @@
#ifndef CAABOX_HPP
#define CAABOX_HPP
#include "CVector3f.hpp"
#include "zeus/CVector3f.hpp"
#include "CUnitVector.hpp"
#include "CTransform.hpp"
#include "CPlane.hpp"
#include "zeus/CTransform.hpp"
#include "zeus/CPlane.hpp"
#include "CLine.hpp"
#include "CSphere.hpp"
#include "Math.hpp"
#include "zeus/Math.hpp"
#if ZE_ATHENA_TYPES
#include <Athena/IStreamReader.hpp>
#include <athena/IStreamReader.hpp>
#endif
namespace Zeus
namespace zeus
{
class alignas(16) CAABox
{
@@ -62,9 +62,9 @@ public:
{
}
#if ZE_ATHENA_TYPES
CAABox(Athena::io::IStreamReader& in) {readBoundingBox(in);}
CAABox(athena::io::IStreamReader& in) {readBoundingBox(in);}
inline void readBoundingBox(Athena::io::IStreamReader& in)
inline void readBoundingBox(athena::io::IStreamReader& in)
{
m_min = CVector3f(in);
m_max = CVector3f(in);
@@ -93,7 +93,7 @@ public:
float distanceFromPoint(const CVector3f &other) const
{
return Math::sqrtF(distanceFromPointSquared(other));
return sqrtF(distanceFromPointSquared(other));
}
inline bool intersects(const CAABox& other) const
@@ -283,9 +283,9 @@ public:
inline CVector3f clampToBox(const CVector3f& vec)
{
CVector3f ret = vec;
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);
clamp(m_min.x, ret.x, m_max.x);
clamp(m_min.y, ret.y, m_max.y);
clamp(m_min.z, ret.z, m_max.z);
return ret;
}

View File

@@ -2,10 +2,10 @@
#define CAXISANGLE_H
#include "Global.hpp"
#include "CVector3f.hpp"
#include "zeus/CVector3f.hpp"
#include "CUnitVector.hpp"
namespace Zeus
namespace zeus
{
struct alignas(16) CAxisAngle : CVector3f
{

View File

@@ -2,10 +2,10 @@
#define CCOLOR_HPP
#include "Global.hpp"
#include "Math.hpp"
#include "zeus/Math.hpp"
#include "TVectorUnion.hpp"
#if ZE_ATHENA_TYPES
#include <Athena/FileReader.hpp>
#include <athena/FileReader.hpp>
#endif
#include <iostream>
@@ -16,7 +16,7 @@
#define COLOR(rgba) rgba
#endif
namespace Zeus
namespace zeus
{
typedef uint8_t Comp8;
typedef uint32_t Comp32;
@@ -57,7 +57,7 @@ public:
CColor(float rgb, float a = 1.0) { splat(rgb, a); }
CColor(float r, float g, float b, float a = 1.0f) {v[0] = r; v[1] = g; v[2] = b; v[3] = a; }
#if ZE_ATHENA_TYPES
CColor(Athena::io::IStreamReader& reader) {readRGBA(reader);}
CColor(athena::io::IStreamReader& reader) {readRGBA(reader);}
CColor(const atVec4f& vec)
#if __SSE__ || __GEKKO_PS__
: mVec128(vec.mVec128){}
@@ -75,14 +75,14 @@ public:
CColor& operator=(const CVector4f& other);
#if ZE_ATHENA_TYPES
inline void readRGBA(Athena::io::IStreamReader& reader)
inline void readRGBA(athena::io::IStreamReader& reader)
{
r = reader.readFloat();
g = reader.readFloat();
b = reader.readFloat();
a = reader.readFloat();
}
inline void readBGRA(Athena::io::IStreamReader& reader)
inline void readBGRA(athena::io::IStreamReader& reader)
{
b = reader.readFloat();
g = reader.readFloat();
@@ -232,7 +232,7 @@ public:
}
inline float magnitude() const
{
return sqrtf(magSquared());
return std::sqrt(magSquared());
}
static inline CColor lerp(const CColor& a, const CColor& b, float t)
{
@@ -320,7 +320,7 @@ public:
void toHSL(float& h, float& s, float& l);
CColor toGrayscale()
{ return {Math::sqrtF((r * r + g * g + b * b) / 3), a}; }
{ return {sqrtF((r * r + g * g + b * b) / 3), a}; }
};
static inline CColor operator+(float lhs, const CColor& rhs)

View File

@@ -1,10 +1,10 @@
#ifndef CFRUSTUM_HPP
#define CFRUSTUM_HPP
#include "CPlane.hpp"
#include "CAABox.hpp"
#include "zeus/CPlane.hpp"
#include "zeus/CAABox.hpp"
namespace Zeus
namespace zeus
{
class CFrustum
{

View File

@@ -2,10 +2,10 @@
#define CLINE_HPP
#include "Global.hpp"
#include "CVector3f.hpp"
#include "zeus/CVector3f.hpp"
#include "CUnitVector.hpp"
namespace Zeus
namespace zeus
{
class alignas(16) CLine
{

View File

@@ -1,10 +1,10 @@
#ifndef CMRAY_HPP
#define CMRAY_HPP
#include "CVector3f.hpp"
#include "CTransform.hpp"
#include "Math.hpp"
#include "zeus/CVector3f.hpp"
#include "zeus/CTransform.hpp"
#include "zeus/Math.hpp"
namespace Zeus
namespace zeus
{
struct alignas(16) CMRay
{

View File

@@ -2,12 +2,12 @@
#define CMATRIX3F_HPP
#include "Global.hpp"
#include "CVector3f.hpp"
#include "zeus/CVector3f.hpp"
#include <assert.h>
#include <string.h>
/* Column-major matrix class */
namespace Zeus
namespace zeus
{
class CQuaternion;
class alignas(16) CMatrix3f

View File

@@ -1,10 +1,10 @@
#ifndef CMATRIX4F
#define CMATRIX4F
#include "CMatrix3f.hpp"
#include "CVector4f.hpp"
#include "CVector3f.hpp"
#include "zeus/CMatrix3f.hpp"
#include "zeus/CVector4f.hpp"
#include "zeus/CVector3f.hpp"
namespace Zeus
namespace zeus
{
class alignas(16) CMatrix4f
{

View File

@@ -1,11 +1,11 @@
#ifndef COBBOX_HPP
#define COBBOX_HPP
#include "CTransform.hpp"
#include "CVector3f.hpp"
#include "CAABox.hpp"
#include "zeus/CTransform.hpp"
#include "zeus/CVector3f.hpp"
#include "zeus/CAABox.hpp"
namespace Zeus
namespace zeus
{
class alignas(16) COBBox
{

View File

@@ -2,10 +2,10 @@
#define CPLANE_HPP
#include "Global.hpp"
#include "CVector3f.hpp"
#include "Math.hpp"
#include "zeus/CVector3f.hpp"
#include "zeus/Math.hpp"
namespace Zeus
namespace zeus
{
class alignas(16) CPlane
{
@@ -36,7 +36,7 @@ public:
{
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);
return clamp(0.0f, dis, 1.0f);
}
inline void normalize()

View File

@@ -2,12 +2,12 @@
#define CPROJECTION_HPP
#include "Global.hpp"
#include "CMatrix4f.hpp"
#include <stdio.h>
#include "zeus/CMatrix4f.hpp"
#include <cstdlib>
#include <cstdio>
#include <cmath>
#define _USE_MATH_DEFINES 1
#include <math.h>
namespace Zeus
namespace zeus
{
enum class EProjType
{
@@ -25,7 +25,7 @@ struct SProjOrtho
struct SProjPersp
{
float m_fov, m_aspect, m_near, m_far;
SProjPersp(float p_fov=55.0f * M_PI / 180.0f, float p_aspect=1.0f, float p_near=0.1f, float p_far=4096.f) :
SProjPersp(float p_fov=degToRad(55.0f), float p_aspect=1.0f, float p_near=0.1f, float p_far=4096.f) :
m_fov(p_fov), m_aspect(p_aspect), m_near(p_near), m_far(p_far) {}
};
extern const SProjOrtho kOrthoIdentity;
@@ -67,8 +67,8 @@ public:
{
if (m_projType != EProjType::Orthographic)
{
fprintf(stderr, "attempted to access orthographic structure of non-ortho projection");
abort();
std::fprintf(stderr, "attempted to access orthographic structure of non-ortho projection");
std::abort();
}
return m_ortho;
}
@@ -76,8 +76,8 @@ public:
{
if (m_projType != EProjType::Perspective)
{
fprintf(stderr, "attempted to access perspective structure of non-persp projection");
abort();
std::fprintf(stderr, "attempted to access perspective structure of non-persp projection");
std::abort();
}
return m_persp;
}

View File

@@ -3,14 +3,14 @@
#include "Global.hpp"
#include "CAxisAngle.hpp"
#include "CVector3f.hpp"
#include "CVector4f.hpp"
#include <math.h>
#include "zeus/CVector3f.hpp"
#include "zeus/CVector4f.hpp"
#include "zeus/Math.hpp"
#if ZE_ATHENA_TYPES
#include <Athena/IStreamReader.hpp>
#include <athena/IStreamReader.hpp>
#endif
namespace Zeus
namespace zeus
{
class alignas(16) CQuaternion
{
@@ -25,7 +25,7 @@ public:
CQuaternion(float x, float y, float z) { fromVector3f(CVector3f(x, y, z)); }
CQuaternion(float r, const CVector3f& vec) : v(vec){ this->r = r;}
#if ZE_ATHENA_TYPES
CQuaternion(Athena::io::IStreamReader& input) { r = input.readFloat(); v = CVector3f(input);}
CQuaternion(athena::io::IStreamReader& input) { r = input.readFloat(); v = CVector3f(input);}
CQuaternion(const atVec4f& vec)
{
#if __SSE__
@@ -99,7 +99,7 @@ public:
*/
static inline CQuaternion fromAxisAngle(const CVector3f& axis, float angle)
{
return CQuaternion(cosf(angle/2), axis*sinf(angle/2));
return CQuaternion(std::cos(angle / 2.f), axis * std::sin(angle / 2.f));
}
void rotateX(float angle) { *this *= fromAxisAngle({1.0f, 0.0f, 0.0f}, angle); }
@@ -129,17 +129,17 @@ public:
inline float roll() const
{
return atan2f(2.0 * (v.x * v.y + r * v.z), r * r + v.x * v.x - v.y * v.y - v.z * v.z);
return std::atan2(2.f * (v.x * v.y + r * v.z), r * r + v.x * v.x - v.y * v.y - v.z * v.z);
}
inline float pitch() const
{
return atan2f(2.0 * (v.y * v.z + r * v.x), r * r - v.x * v.x - v.y * v.y + v.z * v.z);
return std::atan2(2.f * (v.y * v.z + r * v.x), r * r - v.x * v.x - v.y * v.y + v.z * v.z);
}
inline float yaw() const
{
return asinf(-2.0 * (v.x * v.z - r * v.y));
return std::asin(-2.f * (v.x * v.z - r * v.y));
}
union

View File

@@ -1,8 +1,8 @@
#ifndef CRECTANGLE_HPP
#define CRECTANGLE_HPP
#include "CVector2f.hpp"
#include "zeus/CVector2f.hpp"
namespace Zeus
namespace zeus
{
class CRectangle
{

View File

@@ -1,10 +1,10 @@
#ifndef CRELANGLE_HPP
#define CRELANGLE_HPP
#include "CVector3f.hpp"
#include "Math.hpp"
#include "zeus/CVector3f.hpp"
#include "zeus/Math.hpp"
namespace Zeus
namespace zeus
{
/**
* @brief The CRelAngle class represents relative angles in radians
@@ -18,9 +18,9 @@ public:
*/
CRelAngle(const CVector3f& angles)
{
x = Math::degToRad(angles.x);
y = Math::degToRad(angles.y);
z = Math::degToRad(angles.z);
x = degToRad(angles.x);
y = degToRad(angles.y);
z = degToRad(angles.z);
}
CRelAngle(float x, float y, float z)

View File

@@ -1,9 +1,9 @@
#ifndef CSPHERE_HPP
#define CSPHERE_HPP
#include "CVector3f.hpp"
#include "zeus/CVector3f.hpp"
namespace Zeus
namespace zeus
{
class alignas(16) CSphere
{

View File

@@ -2,12 +2,12 @@
#define CTRANSFORM_HPP
#include "Global.hpp"
#include "CMatrix3f.hpp"
#include "CMatrix4f.hpp"
#include "CVector3f.hpp"
#include "CQuaternion.hpp"
#include "zeus/CMatrix3f.hpp"
#include "zeus/CMatrix4f.hpp"
#include "zeus/CVector3f.hpp"
#include "zeus/CQuaternion.hpp"
namespace Zeus
namespace zeus
{
class alignas(16) CTransform
{
@@ -69,8 +69,8 @@ public:
static inline CTransform RotateX(float theta)
{
float sinT = sinf(theta);
float cosT = cosf(theta);
float sinT = std::sin(theta);
float cosT = std::cos(theta);
return CTransform(CMatrix3f(TVectorUnion{1.f, 0.f, 0.f, 0.f},
TVectorUnion{0.f, cosT, sinT, 0.f},
TVectorUnion{0.f, -sinT, cosT, 0.f}));
@@ -78,8 +78,8 @@ public:
static inline CTransform RotateY(float theta)
{
float sinT = sinf(theta);
float cosT = cosf(theta);
float sinT = std::sin(theta);
float cosT = std::cos(theta);
return CTransform(CMatrix3f(TVectorUnion{cosT, 0.f, -sinT, 0.f},
TVectorUnion{0.f, 1.f, 0.f, 0.f},
TVectorUnion{sinT, 0.f, cosT, 0.f}));
@@ -87,8 +87,8 @@ public:
static inline CTransform RotateZ(float theta)
{
float sinT = sinf(theta);
float cosT = cosf(theta);
float sinT = std::sin(theta);
float cosT = std::cos(theta);
return CTransform(CMatrix3f(TVectorUnion{cosT, sinT, 0.f, 0.f},
TVectorUnion{-sinT, cosT, 0.f, 0.f},
TVectorUnion{0.f, 0.f, 1.f, 0.f}));
@@ -96,12 +96,12 @@ public:
inline void rotateLocalX(float theta)
{
float sinT = sinf(theta);
float cosT = cosf(theta);
float sinT = std::sin(theta);
float cosT = std::cos(theta);
Zeus::CVector3f b2 = m_basis[2] * sinT;
Zeus::CVector3f b1 = m_basis[1] * sinT;
Zeus::CVector3f cosV(cosT);
zeus::CVector3f b2 = m_basis[2] * sinT;
zeus::CVector3f b1 = m_basis[1] * sinT;
zeus::CVector3f cosV(cosT);
m_basis[1] *= cosV;
m_basis[2] *= cosV;
@@ -112,12 +112,12 @@ public:
inline void rotateLocalY(float theta)
{
float sinT = sinf(theta);
float cosT = cosf(theta);
float sinT = std::sin(theta);
float cosT = std::cos(theta);
Zeus::CVector3f b0 = m_basis[0] * sinT;
Zeus::CVector3f b2 = m_basis[2] * sinT;
Zeus::CVector3f cosV(cosT);
zeus::CVector3f b0 = m_basis[0] * sinT;
zeus::CVector3f b2 = m_basis[2] * sinT;
zeus::CVector3f cosV(cosT);
m_basis[0] *= cosV;
m_basis[2] *= cosV;
@@ -128,12 +128,12 @@ public:
inline void rotateLocalZ(float theta)
{
float sinT = sinf(theta);
float cosT = cosf(theta);
float sinT = std::sin(theta);
float cosT = std::cos(theta);
Zeus::CVector3f b0 = m_basis[0] * sinT;
Zeus::CVector3f b1 = m_basis[1] * sinT;
Zeus::CVector3f cosV(cosT);
zeus::CVector3f b0 = m_basis[0] * sinT;
zeus::CVector3f b1 = m_basis[1] * sinT;
zeus::CVector3f cosV(cosT);
m_basis[0] *= cosV;
m_basis[1] *= cosV;
@@ -225,6 +225,7 @@ 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);
CTransform lookAt(const CVector3f& pos, const CVector3f& lookPos, const CVector3f& up=kUpVec);
}
#endif // CTRANSFORM_HPP

View File

@@ -1,9 +1,9 @@
#ifndef CUNITVECTOR_HPP
#define CUNITVECTOR_HPP
#include "CVector3f.hpp"
#include "zeus/CVector3f.hpp"
namespace Zeus
namespace zeus
{
class alignas(16) CUnitVector3f : public CVector3f
{

View File

@@ -2,17 +2,17 @@
#define CVECTOR2f_HPP
#include "Global.hpp"
#include "Math.hpp"
#include "zeus/Math.hpp"
#include "TVectorUnion.hpp"
#if ZE_ATHENA_TYPES
#include <Athena/IStreamReader.hpp>
#include <athena/IStreamReader.hpp>
#endif
#include <math.h>
#include <assert.h>
#include "zeus/Math.hpp"
#include <cassert>
namespace Zeus
namespace zeus
{
class alignas(16) CVector2f
{
@@ -68,7 +68,7 @@ public:
return ret;
}
void read(Athena::io::IStreamReader& input)
void read(athena::io::IStreamReader& input)
{
x = input.readFloat();
y = input.readFloat();
@@ -76,12 +76,12 @@ public:
v[3] = 0.0f;
}
CVector2f(Athena::io::IStreamReader& input)
CVector2f(athena::io::IStreamReader& input)
{ read(input); }
#endif
CVector2f(float xy) {splat(xy);}
void assign(float x, float y) {v[0] = x; v[1] = y; v[2] = 0; v[3] = 0.0;}
void assign(float x, float y) {v[0] = x; v[1] = y; v[2] = 0; v[3] = 0.0f;}
CVector2f(float x, float y) {assign(x, y);}
inline bool operator ==(const CVector2f& rhs) const
@@ -172,7 +172,7 @@ public:
inline CVector2f operator+(float val) const
{
#if __SSE__
TVectorUnion splat = {{val, val, 0.0, 0.0}};
TVectorUnion splat = {{val, val, 0.0f, 0.0f}};
return CVector2f(_mm_add_ps(mVec128, splat.mVec128));
#else
return CVector2f(x + val, y + val);
@@ -181,7 +181,7 @@ public:
inline CVector2f operator-(float val) const
{
#if __SSE__
TVectorUnion splat = {{val, val, 0.0, 0.0}};
TVectorUnion splat = {{val, val, 0.0f, 0.0f}};
return CVector2f(_mm_sub_ps(mVec128, splat.mVec128));
#else
return CVector2f(x - val, y - val);
@@ -190,7 +190,7 @@ public:
inline CVector2f operator*(float val) const
{
#if __SSE__
TVectorUnion splat = {{val, val, 0.0, 0.0}};
TVectorUnion splat = {{val, val, 0.0f, 0.0f}};
return CVector2f(_mm_mul_ps(mVec128, splat.mVec128));
#else
return CVector2f(x * val, y * val);
@@ -199,7 +199,7 @@ public:
inline CVector2f operator/(float val) const
{
#if __SSE__
TVectorUnion splat = {{val, val, val, 0.0}};
TVectorUnion splat = {{val, val, val, 0.0f}};
return CVector2f(_mm_div_ps(mVec128, splat.mVec128));
#else
return CVector2f(x / val, y / val);
@@ -251,7 +251,7 @@ public:
inline CVector2f normalized() const
{
float mag = magnitude();
mag = 1.0 / mag;
mag = 1.0f / mag;
return *this * mag;
}
@@ -299,7 +299,7 @@ public:
#endif
}
inline float magnitude() const
{ return Math::sqrtF(magSquared()); }
{ return sqrtF(magSquared()); }
inline void zeroOut()
{
@@ -335,7 +335,7 @@ public:
inline bool canBeNormalized() const
{
const float epsilon = 1.1920929e-7f;
if (fabs(x) >= epsilon || fabs(y) >= epsilon)
if (std::fabs(x) >= epsilon || std::fabs(y) >= epsilon)
return true;
return false;
}
@@ -359,7 +359,7 @@ public:
static inline CVector2f operator+(float lhs, const CVector2f& rhs)
{
#if __SSE__
TVectorUnion splat = {{lhs, lhs, 0.0, 0.0}};
TVectorUnion splat = {{lhs, lhs, 0.0f, 0.0f}};
return CVector2f(_mm_add_ps(splat.mVec128, rhs.mVec128));
#else
return CVector2f(lhs + rhs.x, lhs + rhs.y);
@@ -369,7 +369,7 @@ static inline CVector2f operator+(float lhs, const CVector2f& rhs)
static inline CVector2f operator-(float lhs, const CVector2f& rhs)
{
#if __SSE__
TVectorUnion splat = {{lhs, lhs, 0.0, 0.0}};
TVectorUnion splat = {{lhs, lhs, 0.0f, 0.0f}};
return CVector2f(_mm_sub_ps(splat.mVec128, rhs.mVec128));
#else
return CVector2f(lhs - rhs.x, lhs - rhs.y);
@@ -379,7 +379,7 @@ static inline CVector2f operator-(float lhs, const CVector2f& rhs)
static inline CVector2f operator*(float lhs, const CVector2f& rhs)
{
#if __SSE__
TVectorUnion splat = {{lhs, lhs, 0.0, 0.0}};
TVectorUnion splat = {{lhs, lhs, 0.0f, 0.0f}};
return CVector2f(_mm_mul_ps(splat.mVec128, rhs.mVec128));
#else
return CVector2f(lhs * rhs.x, lhs * rhs.y);
@@ -389,7 +389,7 @@ static inline CVector2f operator*(float lhs, const CVector2f& rhs)
static inline CVector2f operator/(float lhs, const CVector2f& rhs)
{
#if __SSE__
TVectorUnion splat = {{lhs, lhs, 0.0, 0.0}};
TVectorUnion splat = {{lhs, lhs, 0.0f, 0.0f}};
return CVector2f(_mm_div_ps(splat.mVec128, rhs.mVec128));
#else
return CVector2f(lhs / rhs.x, lhs / rhs.y);

View File

@@ -2,16 +2,13 @@
#define CVECTOR2i_HPP
#include "Global.hpp"
#include "Math.hpp"
#include "zeus/Math.hpp"
#if ZE_ATHENA_TYPES
#include <Athena/IStreamReader.hpp>
#include <athena/IStreamReader.hpp>
#endif
#include <math.h>
#include <assert.h>
namespace Zeus
namespace zeus
{
class CVector2i

View File

@@ -2,11 +2,11 @@
#define CVECTOR3D_HPP
#include "Global.hpp"
#include "Math.hpp"
#include "zeus/Math.hpp"
#include "TVectorUnion.hpp"
#include "CVector3f.hpp"
#include "zeus/CVector3f.hpp"
namespace Zeus
namespace zeus
{
class alignas(16) CVector3d
{

View File

@@ -2,17 +2,14 @@
#define CVECTOR3F_HPP
#include "Global.hpp"
#include "Math.hpp"
#include "CVector2f.hpp"
#include "zeus/Math.hpp"
#include "zeus/CVector2f.hpp"
#include "TVectorUnion.hpp"
#if ZE_ATHENA_TYPES
#include <Athena/IStreamReader.hpp>
#include <athena/IStreamReader.hpp>
#endif
#include <math.h>
#include <assert.h>
namespace Zeus
namespace zeus
{
class alignas(16) CVector3f
{
@@ -69,25 +66,25 @@ public:
return ret;
}
void read(Athena::io::IStreamReader& input)
void read(athena::io::IStreamReader& input)
{
x = input.readFloat();
y = input.readFloat();
z = input.readFloat();
v[3] = 0.0f;
}
CVector3f(Athena::io::IStreamReader& input) {read(input);}
CVector3f(athena::io::IStreamReader& input) {read(input);}
#endif
CVector3f(float xyz) {splat(xyz);}
void assign(float x, float y, float z) {v[0] = x; v[1] = y; v[2] = z; v[3] = 0.0;}
void assign(float x, float y, float z) {v[0] = x; v[1] = y; v[2] = z; v[3] = 0.0f;}
CVector3f(float x, float y, float z) {assign(x, y, z);}
CVector3f(const CVector2f& other)
{
x = other.x;
y = other.y;
z = 0.0;
z = 0.0f;
v[3] = 0.0f;
}
@@ -151,7 +148,7 @@ public:
inline CVector3f operator+(float val) const
{
#if __SSE__
TVectorUnion splat = {{val, val, val, 0.0}};
TVectorUnion splat = {{val, val, val, 0.0f}};
return CVector3f(_mm_add_ps(mVec128, splat.mVec128));
#else
return CVector3f(x + val, y + val, z + val);
@@ -160,7 +157,7 @@ public:
inline CVector3f operator-(float val) const
{
#if __SSE__ || __GEKKO_PS__
TVectorUnion splat = {{val, val, val, 0.0}};
TVectorUnion splat = {{val, val, val, 0.0f}};
#endif
#if __SSE__
return CVector3f(_mm_sub_ps(mVec128, splat.mVec128));
@@ -173,7 +170,7 @@ public:
inline CVector3f operator*(float val) const
{
#if __SSE__ || __GEKKO_PS__
TVectorUnion splat = {{val, val, val, 0.0}};
TVectorUnion splat = {{val, val, val, 0.0f}};
#endif
#if __SSE__
return CVector3f(_mm_mul_ps(mVec128, splat.mVec128));
@@ -186,7 +183,7 @@ public:
inline CVector3f operator/(float val) const
{
#if __SSE__ || __GEKKO_PS__
TVectorUnion splat = {{val, val, val, 0.0}};
TVectorUnion splat = {{val, val, val, 0.0f}};
#endif
#if __SSE__
return CVector3f(_mm_div_ps(mVec128, splat.mVec128));
@@ -285,7 +282,7 @@ public:
#endif
}
inline float magnitude() const
{ return Math::sqrtF(magSquared()); }
{ return sqrtF(magSquared()); }
inline void zeroOut()
{
@@ -318,7 +315,7 @@ public:
inline bool canBeNormalized() const
{
const float epsilon = 1.1920929e-7f;
if (fabs(x) >= epsilon || fabs(y) >= epsilon || fabs(z) >= epsilon)
if (std::fabs(x) >= epsilon || std::fabs(y) >= epsilon || std::fabs(z) >= epsilon)
return true;
return false;
}
@@ -338,7 +335,7 @@ public:
return;
}
length = sqrt(length);
length = std::sqrt(length);
float scalar = newLength / length;
*this *= scalar;
}
@@ -368,7 +365,7 @@ public:
static inline CVector3f operator+(float lhs, const CVector3f& rhs)
{
#if __SSE__
TVectorUnion splat = {{lhs, lhs, lhs, 0.0}};
TVectorUnion splat = {{lhs, lhs, lhs, 0.0f}};
return CVector3f(_mm_add_ps(splat.mVec128, rhs.mVec128));
#else
return CVector3f(lhs + rhs.x, lhs + rhs.y, lhs + rhs.z);
@@ -378,7 +375,7 @@ static inline CVector3f operator+(float lhs, const CVector3f& rhs)
static inline CVector3f operator-(float lhs, const CVector3f& rhs)
{
#if __SSE__
TVectorUnion splat = {{lhs, lhs, lhs, 0.0}};
TVectorUnion splat = {{lhs, lhs, lhs, 0.0f}};
return CVector3f(_mm_sub_ps(splat.mVec128, rhs.mVec128));
#else
return CVector3f(lhs - rhs.x, lhs - rhs.y, lhs - rhs.z);
@@ -388,7 +385,7 @@ static inline CVector3f operator-(float lhs, const CVector3f& rhs)
static inline CVector3f operator*(float lhs, const CVector3f& rhs)
{
#if __SSE__
TVectorUnion splat = {{lhs, lhs, lhs, 0.0}};
TVectorUnion splat = {{lhs, lhs, lhs, 0.0f}};
return CVector3f(_mm_mul_ps(splat.mVec128, rhs.mVec128));
#else
return CVector3f(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z);
@@ -398,12 +395,19 @@ static inline CVector3f operator*(float lhs, const CVector3f& rhs)
static inline CVector3f operator/(float lhs, const CVector3f& rhs)
{
#if __SSE__
TVectorUnion splat = {{lhs, lhs, lhs, 0.0}};
TVectorUnion splat = {{lhs, lhs, lhs, 0.0f}};
return CVector3f(_mm_div_ps(splat.mVec128, rhs.mVec128));
#else
return CVector3f(lhs / rhs.x, lhs / rhs.y, lhs / rhs.z);
#endif
}
extern const CVector3f kUpVec;
extern const CVector3f kRadToDegVec;
extern const CVector3f kDegToRadVec;
inline CVector3f radToDeg(const CVector3f& rad) {return rad * kRadToDegVec;}
inline CVector3f degToRad(const CVector3f& deg) {return deg * kDegToRadVec;}
}
#endif // CVECTOR3F_HPP

View File

@@ -3,15 +3,15 @@
#include "Global.hpp"
#include "TVectorUnion.hpp"
#include "CVector3f.hpp"
#include "zeus/CVector3f.hpp"
#if ZE_ATHENA_TYPES
#include <Athena/IStreamReader.hpp>
#include <athena/IStreamReader.hpp>
#endif
#include <math.h>
#include <float.h>
#include <assert.h>
#include "zeus/Math.hpp"
#include <cfloat>
#include <cassert>
namespace Zeus
namespace zeus
{
class CColor;
class alignas(16) CVector4f
@@ -68,7 +68,7 @@ public:
return ret;
}
void read(Athena::io::IStreamReader& input)
void read(athena::io::IStreamReader& input)
{
x = input.readFloat();
y = input.readFloat();
@@ -76,7 +76,7 @@ public:
w = input.readFloat();
}
CVector4f(Athena::io::IStreamReader& input)
CVector4f(athena::io::IStreamReader& input)
{ read(input); }
#endif
@@ -330,7 +330,7 @@ public:
}
inline float magnitude() const
{
return sqrtf(magSquared());
return std::sqrt(magSquared());
}
inline void zeroOut()
@@ -364,7 +364,7 @@ public:
inline bool canBeNormalized() const
{
const float epsilon = 1.1920929e-7f;
if (fabs(x) >= epsilon || fabs(y) >= epsilon || fabs(z) >= epsilon || fabs(w) >= epsilon)
if (std::fabs(x) >= epsilon || std::fabs(y) >= epsilon || std::fabs(z) >= epsilon || std::fabs(w) >= epsilon)
return true;
return false;
}

114
include/zeus/Math.hpp Normal file
View File

@@ -0,0 +1,114 @@
#ifndef MATH_HPP
#define MATH_HPP
#undef min
#undef max
#undef M_PI
#define M_PI 3.14159265358979323846 /* pi */
#undef M_PI_2
#define M_PI_2 1.57079632679489661923 /* pi/2 */
#undef M_PI_4
#define M_PI_4 0.78539816339744830962 /* pi/4 */
#undef M_1_PI
#define M_1_PI 0.31830988618379067154 /* 1/pi */
#undef M_2_PI
#define M_2_PI 0.63661977236758134308 /* 2/pi */
#undef M_2_SQRTPI
#define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */
#undef M_SQRT2
#define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
#undef M_SQRT1_2
#define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
#include <cmath>
#include <algorithm>
namespace zeus
{
struct CPUInfo
{
const char cpuBrand [48] = {0};
const char cpuVendor[32] = {0};
const bool isIntel = false;
const bool SSE1 = false;
const bool SSE2 = false;
const bool SSE3 = false;
const bool SSSE3 = false;
const bool SSE41 = false;
const bool SSE42 = false;
const bool SSE4a = false;
const bool AESNI = false;
};
/**
* Detects CPU capabilities and returns true if SSE4.1 or SSE4.2 is available
*/
void detectCPU();
const CPUInfo& cpuFeatures();
class CVector3f;
class CTransform;
template<typename T>
inline T min(T a, T b) { return a < b ? a : b; }
template<typename T>
inline T max(T a, T b) { return a > b ? a : b; }
template<typename T>
inline T clamp(T a, T val, T b) {return max<T>(a, min<T>(b, val));}
inline float radToDeg(float rad) {return rad * 180.f / M_PI;}
inline float degToRad(float deg) {return deg * M_PI / 180.f;}
inline double radToDeg(double rad) {return rad * 180.0 / M_PI;}
inline double degToRad(double deg) {return deg * M_PI / 180.0;}
CVector3f baryToWorld(const CVector3f& p0, const CVector3f& p1, const CVector3f& p2, const CVector3f& bary);
CVector3f getBezierPoint(const CVector3f& a, const CVector3f& b,
const CVector3f& c, const CVector3f& d, float t);
float getCatmullRomSplinePoint(float a, float b,
float c, float d, float t);
CVector3f getCatmullRomSplinePoint(const CVector3f& a, const CVector3f& b,
const CVector3f& c, const CVector3f& d, float t);
CVector3f getRoundCatmullRomSplinePoint(const CVector3f& a, const CVector3f& b,
const CVector3f& c, const CVector3f& d, float t);
inline float powF(float a, float b) { return std::pow(a, b); }
inline float floorF(float val) { return std::floor(val); }
inline float ceilingF(float val)
{
float tmp = std::floor(val);
return (tmp == val ? tmp : tmp + 1.0);
}
// Since round(double) doesn't exist in some <cmath> implementations
// we'll define our own
inline double round(double val) { return (val < 0.0 ? ceilingF(val - 0.5) : floorF(val + 0.5)); }
inline double powD(float a, float b) { return std::exp(b * std::log(a)); }
double sqrtD(double val);
inline double invSqrtD(double val) { return 1.0 / sqrtD(val); }
inline float invSqrtF(float val) { return float(1.0 / sqrtD(val)); }
inline float sqrtF(float val) { return float(sqrtD(val)); }
float fastArcCosF(float val);
float fastCosF(float val);
float fastSinF(float val);
int floorPowerOfTwo(int x);
int ceilingPowerOfTwo(int x);
template <class T>
inline int PopCount(T x)
{
using U = std::make_unsigned_t<std::conditional_t<std::is_enum<T>::value, std::underlying_type_t<T>, T>>;
U cx = U(x);
const U m1 = U(0x5555555555555555); //binary: 0101...
const U m2 = U(0x3333333333333333); //binary: 00110011..
const U m4 = U(0x0f0f0f0f0f0f0f0f); //binary: 4 zeros, 4 ones ...
const U h01 = U(0x0101010101010101); //the sum of 256 to the power of 0,1,2,3...
cx -= (cx >> 1) & m1; //put count of each 2 bits into those 2 bits
cx = (cx & m2) + ((cx >> 2) & m2); //put count of each 4 bits into those 4 bits
cx = (cx + (cx >> 4)) & m4; //put count of each 8 bits into those 8 bits
return (cx * h01) >> ((sizeof(U)-1)*8); //returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24) + ...
}
}
#endif // MATH_HPP

View File

@@ -1,7 +1,7 @@
#ifndef TVECTORUNION
#define TVECTORUNION
namespace Zeus
namespace zeus
{
typedef union
{

27
include/zeus/zeus.hpp Normal file
View File

@@ -0,0 +1,27 @@
#ifndef __MATHLIB_HPP
#define __MATHLIB_HPP
#include "CAxisAngle.hpp"
#include "CRelAngle.hpp"
#include "zeus/CMatrix3f.hpp"
#include "zeus/CMatrix4f.hpp"
#include "zeus/CProjection.hpp"
#include "zeus/CTransform.hpp"
#include "zeus/CQuaternion.hpp"
#include "zeus/CVector2f.hpp"
#include "zeus/CVector3f.hpp"
#include "CVector3d.hpp"
#include "zeus/CVector4f.hpp"
#include "CUnitVector.hpp"
#include "zeus/CRectangle.hpp"
#include "zeus/CPlane.hpp"
#include "CLine.hpp"
#include "zeus/CAABox.hpp"
#include "COBBox.hpp"
#include "CSphere.hpp"
#include "CFrustum.hpp"
#include "zeus/CColor.hpp"
#include "Global.hpp"
#include "zeus/Math.hpp"
#endif // __MATHLIB_HPP