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

@ -2,15 +2,15 @@ cmake_minimum_required(VERSION 3.0)
project(zeus)
if (NOT DEFINED ATHENA_INCLUDE_DIR)
set(ATHENA_INCLUDE_DIR ../Athena/include)
set(ATHENA_INCLUDE_DIR ../athena/include)
endif()
include_directories(include ${ATHENA_INCLUDE_DIR})
if(NOT WIN32)
if(NOT MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4.1 -msse4.2 -std=c++14")
endif()
add_library(Math
add_library(zeus
src/CVector3f.cpp
src/Math.cpp
src/CQuaternion.cpp
@ -25,32 +25,32 @@ add_library(Math
src/CMatrix4f.cpp
src/CAABox.cpp
include/Math.hpp
include/CQuaternion.hpp
include/CMatrix3f.hpp
include/CProjection.hpp
include/CAxisAngle.hpp
include/CRelAngle.hpp
include/CPlane.hpp
include/CTransform.hpp
include/CColor.hpp
include/Global.hpp
include/MathLib.hpp
include/TVectorUnion.hpp
include/CVector2i.hpp
include/CVector2f.hpp
include/CVector3f.hpp
include/CVector3d.hpp
include/CVector4f.hpp
include/CRectangle.hpp
include/CMatrix4f.hpp
include/CFrustum.hpp
include/CAABox.hpp
include/COBBox.hpp
include/CLine.hpp
include/CSphere.hpp
include/CUnitVector.hpp
include/CMRay.hpp)
include/zeus/Math.hpp
include/zeus/CQuaternion.hpp
include/zeus/CMatrix3f.hpp
include/zeus/CProjection.hpp
include/zeus/CAxisAngle.hpp
include/zeus/CRelAngle.hpp
include/zeus/CPlane.hpp
include/zeus/CTransform.hpp
include/zeus/CColor.hpp
include/zeus/Global.hpp
include/zeus/zeus.hpp
include/zeus/TVectorUnion.hpp
include/zeus/CVector2i.hpp
include/zeus/CVector2f.hpp
include/zeus/CVector3f.hpp
include/zeus/CVector3d.hpp
include/zeus/CVector4f.hpp
include/zeus/CRectangle.hpp
include/zeus/CMatrix4f.hpp
include/zeus/CFrustum.hpp
include/zeus/CAABox.hpp
include/zeus/COBBox.hpp
include/zeus/CLine.hpp
include/zeus/CSphere.hpp
include/zeus/CUnitVector.hpp
include/zeus/CMRay.hpp)
add_subdirectory(test)

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

View File

@ -1,2 +1,2 @@
#include "CAABox.hpp"
const Zeus::CAABox Zeus::CAABox::skInvertedBox = CAABox();
#include "zeus/CAABox.hpp"
const zeus::CAABox zeus::CAABox::skInvertedBox = CAABox();

View File

@ -1,7 +1,7 @@
#include "CColor.hpp"
#include "CVector4f.hpp"
#include "zeus/CColor.hpp"
#include "zeus/CVector4f.hpp"
namespace Zeus
namespace zeus
{
const CColor CColor::skRed (Comp32(0xFF0000FFul));
const CColor CColor::skBlack (Comp32(0x000000FFul));
@ -68,8 +68,8 @@ void CColor::fromHSV(float h, float s, float v, float _a)
void CColor::toHSV(float &h, float &s, float &v) const
{
float min = Math::min(r, Math::min(g, b));
float max = Math::max(r, Math::max(g, b));
float min = std::min(r, std::min(g, b));
float max = std::max(r, std::max(g, b));
v = max;
float delta = max - min;
@ -106,8 +106,8 @@ void CColor::fromHSL(float h, float s, float l, float _a)
void CColor::toHSL(float &h, float &s, float &l)
{
const float min = Math::min(r, Math::min(g, b));
const float max = Math::max(r, Math::max(g, b));
const float min = std::min(r, std::min(g, b));
const float max = std::max(r, std::max(g, b));
const float d = max - min;
if (max == min)

View File

@ -1,8 +1,8 @@
#include "CMatrix3f.hpp"
#include "CQuaternion.hpp"
#include "Global.hpp"
#include "zeus/CMatrix3f.hpp"
#include "zeus/CQuaternion.hpp"
#include "zeus/Global.hpp"
namespace Zeus
namespace zeus
{
const CMatrix3f CMatrix3f::skIdentityMatrix3f = CMatrix3f();

View File

@ -1,3 +1,3 @@
#include "CMatrix4f.hpp"
#include "zeus/CMatrix4f.hpp"
const Zeus::CMatrix4f Zeus::CMatrix4f::skIdentityMatrix4f = CMatrix4f();
const zeus::CMatrix4f zeus::CMatrix4f::skIdentityMatrix4f = CMatrix4f();

View File

@ -1,3 +1,3 @@
#include "CPlane.hpp"
#include "zeus/CPlane.hpp"

View File

@ -1,11 +1,12 @@
#include "CProjection.hpp"
#include <math.h>
#include <stdio.h>
#include "zeus/CProjection.hpp"
#include "zeus/Math.hpp"
#include <cassert>
namespace Zeus
namespace zeus
{
void CProjection::_updateCachedMatrix()
{
assert(m_projType == EProjType::Orthographic || m_projType == EProjType::Perspective);
if (m_projType == EProjType::Orthographic)
{
float tmp;
@ -36,7 +37,7 @@ void CProjection::_updateCachedMatrix()
else if (m_projType == EProjType::Perspective)
{
float cot,tmp;
float t_fovy = tanf(m_persp.m_fov / 2.0);
float t_fovy = std::tan(m_persp.m_fov / 2.0f);
cot = 1.0f / t_fovy;
@ -61,11 +62,6 @@ void CProjection::_updateCachedMatrix()
m_mtx.m[2][3] = -1.0f;
m_mtx.m[3][3] = 0.0f;
}
else
{
fprintf(stderr, "attempted to cache invalid projection type");
abort();
}
}
}

View File

@ -1,17 +1,17 @@
#include "CQuaternion.hpp"
#include <math.h>
#include "zeus/CQuaternion.hpp"
#include "zeus/Math.hpp"
namespace Zeus
namespace zeus
{
void CQuaternion::fromVector3f(const CVector3f& vec)
{
float cosX = cosf(0.5 * vec.x);
float cosY = cosf(0.5 * vec.y);
float cosZ = cosf(0.5 * vec.z);
float cosX = std::cos(0.5f * vec.x);
float cosY = std::cos(0.5f * vec.y);
float cosZ = std::cos(0.5f * vec.z);
float sinX = sinf(0.5 * vec.x);
float sinY = sinf(0.5 * vec.y);
float sinZ = sinf(0.5 * vec.z);
float sinX = std::sin(0.5f * vec.x);
float sinY = std::sin(0.5f * vec.y);
float sinZ = std::sin(0.5f * vec.z);
r = cosZ * cosY * cosX + sinZ * sinY * sinX;
v.x = cosZ * cosY * sinX - sinZ * sinY * cosX;
@ -106,7 +106,7 @@ const CQuaternion& CQuaternion::operator/=(float scale)
float CQuaternion::magnitude() const
{
return sqrt(magSquared());
return std::sqrt(magSquared());
}
float CQuaternion::magSquared() const
@ -137,15 +137,15 @@ CQuaternion CQuaternion::inverse() const
CAxisAngle CQuaternion::toAxisAngle()
{
// CAxisAngle ret;
// ret.angle = acosf(r);
// ret.angle = std::acos(r);
// float thetaInv = 1.0f/sinf(ret.angle);
// float thetaInv = 1.0f/std::sin(ret.angle);
// ret.axis.x = v.x * thetaInv;
// ret.axis.y = v.y * thetaInv;
// ret.axis.z = v.z * thetaInv;
// ret.angle *= 2;
// ret.angle *= 2.f;
// return ret;
return CAxisAngle();
@ -153,20 +153,20 @@ CAxisAngle CQuaternion::toAxisAngle()
CQuaternion CQuaternion::log() const
{
float a = acosf(r);
float sina = sinf(a);
float a = std::acos(r);
float sina = std::sin(a);
CQuaternion ret;
ret.r = 0;
ret.r = 0.f;
if (sina > 0)
if (sina > 0.f)
{
ret.v.x = a * v.x / sina;
ret.v.y = a * v.y / sina;
ret.v.z = a * v.z / sina;
}
else
ret.v = CVector3f(0);
ret.v = CVector3f(0.f);
return ret;
}
@ -174,19 +174,19 @@ CQuaternion CQuaternion::log() const
CQuaternion CQuaternion::exp() const
{
float a = (v.magnitude());
float sina = sinf(a);
float cosa = cos(a);
float sina = std::sin(a);
float cosa = std::cos(a);
CQuaternion ret;
ret.r = cosa;
if (a > 0)
if (a > 0.f)
{
ret.v.x = sina * v.x / a;
ret.v.y = sina * v.y / a;
ret.v.z = sina * v.z / a;
}
else
ret.v = CVector3f(0);
ret.v = CVector3f(0.f);
return ret;
}
@ -215,18 +215,18 @@ CQuaternion CQuaternion::slerp(CQuaternion& a, CQuaternion& b, double t)
CQuaternion ret;
float mag = sqrtf(a.dot(a) * b.dot(b));
float mag = std::sqrt(a.dot(a) * b.dot(b));
float prod = a.dot(b) / mag;
if (fabsf(prod) < 1.0)
if (std::fabs(prod) < 1.0f)
{
const double sign = (prod < 0.0) ? -1.0 : 1.0;
const double sign = (prod < 0.0f) ? -1.0f : 1.0f;
const double theta = acos(sign * prod);
const double s1 = sin (sign * t * theta);
const double d = 1.0 / sin(theta);
const double s0 = sin((1.0 - t) * theta);
const double theta = std::acos(sign * prod);
const double s1 = std::sin(sign * t * theta);
const double d = 1.0 / std::sin(theta);
const double s0 = std::sin((1.0 - t) * theta);
ret.v.x = (float)(a.v.x * s0 + b.v.x * s1) * d;
ret.v.y = (float)(a.v.y * s0 + b.v.y * s1) * d;

View File

@ -1 +1 @@
#include "CRectangle.hpp"
#include "zeus/CRectangle.hpp"

View File

@ -1,6 +1,6 @@
#include "CTransform.hpp"
#include "zeus/CTransform.hpp"
namespace Zeus
namespace zeus
{
CTransform CTransformFromEditorEuler(const CVector3f& eulerVec)
{
@ -11,27 +11,27 @@ CTransform CTransformFromEditorEuler(const CVector3f& eulerVec)
tj = eulerVec[1];
th = eulerVec[2];
ci = cos(ti);
cj = cos(tj);
ch = cos(th);
si = sin(ti);
sj = sin(tj);
sh = sin(th);
ci = std::cos(ti);
cj = std::cos(tj);
ch = std::cos(th);
si = std::sin(ti);
sj = std::sin(tj);
sh = std::sin(th);
cc = ci * ch;
cs = ci * sh;
sc = si * ch;
ss = si * sh;
result.m_basis.m[0][0] = (float)(cj * ch);
result.m_basis.m[1][0] = (float)(sj * sc - cs);
result.m_basis.m[2][0] = (float)(sj * cc + ss);
result.m_basis.m[0][1] = (float)(cj * sh);
result.m_basis.m[1][1] = (float)(sj * ss + cc);
result.m_basis.m[2][1] = (float)(sj * cs - sc);
result.m_basis.m[0][2] = (float)(-sj);
result.m_basis.m[1][2] = (float)(cj * si);
result.m_basis.m[2][2] = (float)(cj * ci);
result.m_basis.m[0][0] = float(cj * ch);
result.m_basis.m[1][0] = float(sj * sc - cs);
result.m_basis.m[2][0] = float(sj * cc + ss);
result.m_basis.m[0][1] = float(cj * sh);
result.m_basis.m[1][1] = float(sj * ss + cc);
result.m_basis.m[2][1] = float(sj * cs - sc);
result.m_basis.m[0][2] = float(-sj);
result.m_basis.m[1][2] = float(cj * si);
result.m_basis.m[2][2] = float(cj * ci);
return result;
}
@ -41,9 +41,9 @@ CTransform CTransformFromAxisAngle(const CVector3f& axis, float angle)
CTransform result;
CVector3f axisN = axis.normalized();
float c = cosf(angle);
float s = sinf(angle);
float t = 1 - c;
float c = std::cos(angle);
float s = std::sin(angle);
float t = 1.f - c;
result.m_basis.m[0][0] = t * axisN.v[0] * axisN.v[0] + c;
result.m_basis.m[1][0] = t * axisN.v[0] * axisN.v[1] - axisN.v[2] * s;

View File

@ -1,10 +1,10 @@
#include "CVector2f.hpp"
#include "zeus/CVector2f.hpp"
#include <memory.h>
#include <cmath>
#include <assert.h>
#include "Math.hpp"
#include "zeus/Math.hpp"
namespace Zeus
namespace zeus
{
const CVector2f CVector2f::skOne = CVector2f(1.0);
const CVector2f CVector2f::skNegOne = CVector2f(-1.0);
@ -19,7 +19,7 @@ float CVector2f::getAngleDiff(const CVector2f& a, const CVector2f& b)
return 0;
float dot = a.dot(b);
float theta = Math::arcCosineR(dot / (mag1 * mag2));
float theta = std::acos(dot / (mag1 * mag2));
return theta;
}
@ -32,18 +32,18 @@ CVector2f CVector2f::slerp(const CVector2f& a, const CVector2f& b, float t)
CVector2f ret;
float mag = sqrtf(a.dot(a) * b.dot(b));
float mag = std::sqrt(a.dot(a) * b.dot(b));
float prod = a.dot(b) / mag;
if (fabsf(prod) < 1.0)
if (std::fabs(prod) < 1.0f)
{
const double sign = (prod < 0.0) ? -1.0 : 1.0;
const double sign = (prod < 0.0f) ? -1.0f : 1.0f;
const double theta = acos(sign * prod);
const double s1 = sin (sign * t * theta);
const double d = 1.0 / sin(theta);
const double s0 = sin((1.0 - t) * theta);
const double theta = std::acos(sign * prod);
const double s1 = std::sin(sign * t * theta);
const double d = 1.0 / std::sin(theta);
const double s0 = std::sin((1.0f - t) * theta);
ret = (a * s0 + b * s1) * d;
return ret;

View File

@ -1,25 +1,29 @@
#include "CVector3f.hpp"
#include "zeus/CVector3f.hpp"
#include <memory.h>
#include <cmath>
#include <assert.h>
#include "Math.hpp"
#include "zeus/Math.hpp"
namespace Zeus
namespace zeus
{
const CVector3f CVector3f::skOne = CVector3f(1.0);
const CVector3f CVector3f::skNegOne = CVector3f(-1.0);
const CVector3f CVector3f::skZero;
const CVector3f kUpVec(0.0, 0.0, 1.0);
const CVector3f kRadToDegVec(180.0f / M_PI);
const CVector3f kDegToRadVec(M_PI / 180.0f);
float CVector3f::getAngleDiff(const CVector3f& a, const CVector3f& b)
{
float mag1 = a.magnitude();
float mag2 = b.magnitude();
if (!mag1 || !mag2)
return 0;
return 0.f;
float dot = a.dot(b);
float theta = Math::arcCosineR(dot / (mag1 * mag2));
float theta = std::acos(dot / (mag1 * mag2));
return theta;
}
@ -32,13 +36,13 @@ CVector3f CVector3f::slerp(const CVector3f& a, const CVector3f& b, float t)
CVector3f ret;
float mag = sqrtf(a.dot(a) * b.dot(b));
float mag = std::sqrt(a.dot(a) * b.dot(b));
float prod = a.dot(b) / mag;
if (fabsf(prod) < 1.0)
if (std::fabs(prod) < 1.0f)
{
const double sign = (prod < 0.0) ? -1.0 : 1.0;
const double sign = (prod < 0.0f) ? -1.0f : 1.0f;
const double theta = acos(sign * prod);
const double s1 = sin (sign * t * theta);

View File

@ -1,9 +1,9 @@
#include "CVector4f.hpp"
#include "CColor.hpp"
#include "zeus/CVector4f.hpp"
#include "zeus/CColor.hpp"
namespace Zeus
namespace zeus
{
CVector4f::CVector4f(const Zeus::CColor& other)
CVector4f::CVector4f(const zeus::CColor& other)
: x(other.r), y(other.g), z(other.b), w(other.a)
{
}

View File

@ -1,13 +1,13 @@
#include "Math.hpp"
#include "CTransform.hpp"
#include "CVector3f.hpp"
#include "zeus/Math.hpp"
#include "zeus/CTransform.hpp"
#include "zeus/CVector3f.hpp"
#if _WIN32
#include <intrin.h>
#else
#include <cpuid.h>
#endif
namespace Zeus
namespace zeus
{
static CPUInfo g_cpuFeatures;
@ -69,12 +69,6 @@ void detectCPU()
const CPUInfo& cpuFeatures() { return g_cpuFeatures; }
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 lookAt(const CVector3f& pos, const CVector3f& lookPos, const CVector3f& up)
{
CVector3f vLook,vRight,vUp;
@ -158,18 +152,18 @@ double sqrtD(double val)
return q;
}
float fastArcCosR(float val)
float fastArcCosF(float val)
{
/* If we're not at a low enough value,
* the approximation below won't provide any benefit,
* and we simply fall back to the standard implementation
*/
if (fabs(val) >= 0.925000011920929)
return float(acos(val));
if (std::fabs(val) >= 0.925000011920929)
return std::acos(val);
/* Fast Arc Cosine approximation using Taylor Polynomials
* while this implementation is fast, it's also not as accurate.
* This is a straight reimplementation of Retro's CMath::FastArcCosR
* This is a straight reimplementation of Retro's CFastArcCosR
* and as a result of the polynomials, it returns the inverse value,
* I'm not certain if this was intended originally, but we'll leave it
* in order to be as accurate as possible.
@ -217,9 +211,9 @@ int ceilingPowerOfTwo(int x)
return x;
}
float fastCosR(float val)
float fastCosF(float val)
{
if (fabs(val) > M_PI)
if (std::fabs(val) > M_PI)
{
float rVal = float(uint32_t(val));
val = -((rVal * val) - 6.2831855);
@ -240,9 +234,9 @@ float fastCosR(float val)
return val;
}
float fastSinR(float val)
float fastSinF(float val)
{
if (fabs(val) > M_PI)
if (std::fabs(val) > M_PI)
{
float rVal = float(uint32_t(val));
val = -((rVal * val) - 6.2831855);
@ -319,16 +313,10 @@ CVector3f getRoundCatmullRomSplinePoint(const CVector3f& a, const CVector3f& b,
if (cVelocity.canBeNormalized())
cVelocity.normalize();
const float cbDistance = cb.magnitude();
return getCatmullRomSplinePoint(b, c, bVelocity * cbDistance, cVelocity * cbDistance, t);
return zeus::getCatmullRomSplinePoint(b, c, bVelocity * cbDistance, cVelocity * cbDistance, t);
}
CVector3f baryToWorld(const CVector3f& p0, const CVector3f& p1, const CVector3f& p2, const CVector3f& bary)
{ return bary.x * p0 + bary.y * p1 + bary.z * p2; }
CVector3f radToDeg(const CVector3f& rad) {return rad * kRadToDegVec;}
CVector3f degToRad(const CVector3f& deg) {return deg * kDegToRadVec;}
}
}

View File

@ -3,8 +3,5 @@ project(zeustest)
include_directories(../include)
add_executable(zeustest
main.cpp)
target_link_libraries(zeustest
Math)
add_executable(zeustest main.cpp)
target_link_libraries(zeustest zeus)

View File

@ -1,19 +1,19 @@
#include <iostream>
#include <iomanip>
#include <MathLib.hpp>
#include <zeus/zeus.hpp>
// This is only for testing, do NOT do this normally
using namespace Zeus;
using namespace zeus;
union Color
{
struct { Zeus::Comp8 r, g, b, a; };
Zeus::Comp32 rgba;
struct { zeus::Comp8 r, g, b, a; };
zeus::Comp32 rgba;
};
int main()
{
Zeus::detectCPU();
zeus::detectCPU();
assert(!CAABox({100, 100, 100}, {100, 100, 100}).invalid());
assert(CAABox().invalid());
CVector3f vec{320, 632162.f, 800.f};
@ -23,7 +23,7 @@ int main()
assert(!vec.normalized().canBeNormalized());
float blarg = 5.f;
CVector3f t{100, 100, 200};
blarg = Math::clamp(0.f, blarg, 1.f);
blarg = clamp(0.f, blarg, 1.f);
CAABox test{{-100, -100, -100}, {100, 100, 100}};
CAABox test2{{-100, -100, -100}, {100, 100, 100}};
CAABox test3{{-50, -50, -50}, {50, 50, 50}};
@ -43,15 +43,15 @@ int main()
CSphere s2({1, 0, 0}, 1);
CSphere s3({3, 0, 0}, 1);
std::cout << Math::min(1, 3) << std::endl;
std::cout << Math::min(2, 1) << std::endl;
std::cout << Math::max(1, 3) << std::endl;
std::cout << Math::max(2, 1) << std::endl;
std::cout << Math::clamp(-50, 100, 50) << std::endl;
std::cout << Math::clamp(-50, -100, 50) << std::endl;
std::cout << Math::powF(6.66663489, 2) << std::endl;
std::cout << Math::invSqrtF(1) << std::endl;
std::cout << Math::floorPowerOfTwo(256) << std::endl;
std::cout << min(1, 3) << std::endl;
std::cout << min(2, 1) << std::endl;
std::cout << max(1, 3) << std::endl;
std::cout << max(2, 1) << std::endl;
std::cout << clamp(-50, 100, 50) << std::endl;
std::cout << clamp(-50, -100, 50) << std::endl;
std::cout << powF(6.66663489, 2) << std::endl;
std::cout << invSqrtF(1) << std::endl;
std::cout << floorPowerOfTwo(256) << std::endl;
std::cout << " Test 1 " << ( aabb.intersects(s1) ? "succeeded" : "failed" ) << std::endl;
std::cout << " Test 2 " << ( aabb.intersects(s2) ? "succeeded" : "failed" ) << std::endl;
std::cout << " Test 3 " << ( aabb.intersects(s3) ? "succeeded" : "failed" ) << std::endl;