Skip to content

Commit

Permalink
Delete more useless math code
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Sep 29, 2020
1 parent db9cc41 commit ec7458b
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 270 deletions.
158 changes: 0 additions & 158 deletions ext/native/math/lin/matrix4x4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,6 @@

namespace Lin {

Matrix4x4 Matrix4x4::simpleInverse() const {
Matrix4x4 out;
out.xx = xx;
out.xy = yx;
out.xz = zx;

out.yx = xy;
out.yy = yy;
out.yz = zy;

out.zx = xz;
out.zy = yz;
out.zz = zz;

out.wx = -(xx * wx + xy * wy + xz * wz);
out.wy = -(yx * wx + yy * wy + yz * wz);
out.wz = -(zx * wx + zy * wy + zz * wz);

out.xw = 0.0f;
out.yw = 0.0f;
out.zw = 0.0f;
out.ww = 1.0f;

return out;
}

Matrix4x4 Matrix4x4::transpose() const
{
Matrix4x4 out;
Expand All @@ -58,100 +32,6 @@ Matrix4x4 Matrix4x4::operator * (const Matrix4x4 &other) const
return temp;
}

Matrix4x4 Matrix4x4::inverse() const {
Matrix4x4 temp;
float dW = 1.0f / (xx*(yy*zz - yz*zy) - xy*(yx*zz - yz*zx) - xz*(yy*zx - yx*zy));

temp.xx = (yy*zz - yz*zy) * dW;
temp.xy = (xz*zy - xy*zz) * dW;
temp.xz = (xy*yz - xz*yy) * dW;
temp.xw = xw;

temp.yx = (yz*zx - yx*zz) * dW;
temp.yy = (xx*zz - xz*zx) * dW;
temp.yz = (xz*yx - xx*zx) * dW;
temp.yw = yw;

temp.zx = (yx*zy - yy*zx) * dW;
temp.zy = (xy*zx - xx*zy) * dW;
temp.zz = (xx*yy - xy*yx) * dW;
temp.zw = zw;

temp.wx = (yy*(zx*wz - zz*wx) + yz*(zy*wx - zx*wy) - yx*(zy*wz - zz*wy)) * dW;
temp.wy = (xx*(zy*wz - zz*wy) + xy*(zz*wx - zx*wz) + xz*(zx*wy - zy*wx)) * dW;
temp.wz = (xy*(yx*wz - yz*wx) + xz*(yy*wx - yx*wy) - xx*(yy*wz - yz*wy)) * dW;
temp.ww = ww;

return temp;
}

void Matrix4x4::setViewLookAt(const Vec3 &vFrom, const Vec3 &vAt, const Vec3 &vWorldUp) {
Vec3 vView = vFrom - vAt; // OpenGL, sigh...
vView.normalize();
float DotProduct = vWorldUp * vView;
Vec3 vUp = vWorldUp - vView * DotProduct;
float Length = vUp.length();

if (1e-6f > Length) {
// EMERGENCY
vUp = Vec3(0.0f, 1.0f, 0.0f) - vView * vView.y;
// If we still have near-zero length, resort to a different axis.
Length = vUp.length();
if (1e-6f > Length)
{
vUp = Vec3(0.0f, 0.0f, 1.0f) - vView * vView.z;
Length = vUp.length();
if (1e-6f > Length)
return;
}
}
vUp.normalize();
Vec3 vRight = vUp % vView;
empty();

xx = vRight.x; xy = vUp.x; xz=vView.x;
yx = vRight.y; yy = vUp.y; yz=vView.y;
zx = vRight.z; zy = vUp.z; zz=vView.z;

wx = -vFrom * vRight;
wy = -vFrom * vUp;
wz = -vFrom * vView;
ww = 1.0f;
}

void Matrix4x4::setViewLookAtD3D(const Vec3 &vFrom, const Vec3 &vAt, const Vec3 &vWorldUp) {
Vec3 vView = vAt - vFrom;
vView.normalize();
float DotProduct = vWorldUp * vView;
Vec3 vUp = vWorldUp - vView * DotProduct;
float Length = vUp.length();

if (1e-6f > Length) {
vUp = Vec3(0.0f, 1.0f, 0.0f) - vView * vView.y;
// If we still have near-zero length, resort to a different axis.
Length = vUp.length();
if (1e-6f > Length)
{
vUp = Vec3(0.0f, 0.0f, 1.0f) - vView * vView.z;
Length = vUp.length();
if (1e-6f > Length)
return;
}
}
vUp.normalize();
Vec3 vRight = vUp % vView;
empty();

xx = vRight.x; xy = vUp.x; xz=vView.x;
yx = vRight.y; yy = vUp.y; yz=vView.y;
zx = vRight.z; zy = vUp.z; zz=vView.z;

wx = -vFrom * vRight;
wy = -vFrom * vUp;
wz = -vFrom * vView;
ww = 1.0f;
}

void Matrix4x4::setViewFrame(const Vec3 &pos, const Vec3 &vRight, const Vec3 &vView, const Vec3 &vUp) {
xx = vRight.x; xy = vUp.x; xz=vView.x; xw = 0.0f;
yx = vRight.y; yy = vUp.y; yz=vView.y; yw = 0.0f;
Expand All @@ -163,44 +43,6 @@ void Matrix4x4::setViewFrame(const Vec3 &pos, const Vec3 &vRight, const Vec3 &vV
ww = 1.0f;
}

//YXZ euler angles
void Matrix4x4::setRotation(float x,float y, float z)
{
setRotationY(y);
Matrix4x4 temp;
temp.setRotationX(x);
*this *= temp;
temp.setRotationZ(z);
*this *= temp;
}

void Matrix4x4::setProjection(float near, float far, float fov_horiz, float aspect) {
// Now OpenGL style.
empty();

float xFac = tanf(fov_horiz * 3.14f/360);
float yFac = xFac * aspect;
xx = 1.0f / xFac;
yy = 1.0f / yFac;
zz = -(far+near)/(far-near);
zw = -1.0f;
wz = -(2*far*near)/(far-near);
}

void Matrix4x4::setProjectionD3D(float near_plane, float far_plane, float fov_horiz, float aspect) {
empty();
float Q, f;

f = fov_horiz*0.5f;
Q = far_plane / (far_plane - near_plane);

xx = (float)(1.0f / tanf(f));;
yy = (float)(1.0f / tanf(f*aspect));
zz = Q;
wz = -Q * near_plane;
zw = 1.0f;
}

void Matrix4x4::setOrtho(float left, float right, float bottom, float top, float near, float far) {
empty();
xx = 2.0f / (right - left);
Expand Down
54 changes: 2 additions & 52 deletions ext/native/math/lin/matrix4x4.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,6 @@ class Matrix4x4 {
const Vec3 front() const {return Vec3(zx, zy, zz);}
const Vec3 move() const {return Vec3(wx, wy, wz);}

void setRight(const Vec3 &v) {
xx = v.x; xy = v.y; xz = v.z;
}
void setUp(const Vec3 &v) {
yx = v.x; yy = v.y; yz = v.z;
}
void setFront(const Vec3 &v) {
zx = v.x; zy = v.y; zz = v.z;
}
void setMove(const Vec3 &v) {
wx = v.x; wy = v.y; wz = v.z;
}

const float &operator[](int i) const {
return *(((const float *)this) + i);
}
Expand All @@ -53,17 +40,14 @@ class Matrix4x4 {
void empty() {
memset(this, 0, 16 * sizeof(float));
}
void setScaling(const float f) {
empty();
xx=yy=zz=f; ww=1.0f;
}
static Matrix4x4 identity() {
Matrix4x4 id;
id.setIdentity();
return id;
}
void setIdentity() {
setScaling(1.0f);
empty();
xx = yy = zz = ww = 1.0f;
}
void setTranslation(const Vec3 &trans) {
setIdentity();
Expand All @@ -72,37 +56,8 @@ class Matrix4x4 {
wz = trans.z;
}

Matrix4x4 inverse() const;
Matrix4x4 simpleInverse() const;
Matrix4x4 transpose() const;

void setRotationX(const float a) {
empty();
float c = cosf(a);
float s = sinf(a);
xx = 1.0f;
yy = c; yz = s;
zy = -s; zz = c;
ww = 1.0f;
}
void setRotationY(const float a) {
empty();
float c = cosf(a);
float s = sinf(a);
xx = c; xz = -s;
yy = 1.0f;
zx = s; zz = c;
ww = 1.0f;
}
void setRotationZ(const float a) {
empty();
float c = cosf(a);
float s = sinf(a);
xx = c; xy = s;
yx = -s; yy = c;
zz = 1.0f;
ww = 1.0f;
}
// Exact angles to avoid any artifacts.
void setRotationZ90() {
empty();
Expand Down Expand Up @@ -132,15 +87,10 @@ class Matrix4x4 {
ww = 1.0f;
}

void setRotation(float x,float y, float z);
void setProjection(float near_plane, float far_plane, float fov_horiz, float aspect = 0.75f);
void setProjectionD3D(float near_plane, float far_plane, float fov_horiz, float aspect = 0.75f);
void setOrtho(float left, float right, float bottom, float top, float near, float far);
void setOrthoD3D(float left, float right, float bottom, float top, float near, float far);
void setOrthoVulkan(float left, float right, float top, float bottom, float near, float far);

void setViewLookAt(const Vec3 &from, const Vec3 &at, const Vec3 &worldup);
void setViewLookAtD3D(const Vec3 &from, const Vec3 &at, const Vec3 &worldup);
void setViewFrame(const Vec3 &pos, const Vec3 &right, const Vec3 &forward, const Vec3 &up);
void toText(char *buffer, int len) const;
void print() const;
Expand Down
12 changes: 0 additions & 12 deletions ext/native/math/lin/vec3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@ Vec3 Vec3::operator *(const Matrix4x4 &m) const {
x*m.xy + y*m.yy + z*m.zy + m.wy,
x*m.xz + y*m.yz + z*m.zz + m.wz);
}
Vec4 Vec3::multiply4D(const Matrix4x4 &m) const {
return Vec4(x*m.xx + y*m.yx + z*m.zx + m.wx,
x*m.xy + y*m.yy + z*m.zy + m.wy,
x*m.xz + y*m.yz + z*m.zz + m.wz,
x*m.xw + y*m.yw + z*m.zw + m.ww);
}
Vec4 Vec4::multiply4D(Matrix4x4 &m) const {
return Vec4(x*m.xx + y*m.yx + z*m.zx + w*m.wx,
x*m.xy + y*m.yy + z*m.zy + w*m.wy,
x*m.xz + y*m.yz + z*m.zz + w*m.wz,
x*m.xw + y*m.yw + z*m.zw + w*m.ww);
}

Vec3 Vec3::rotatedBy(const Matrix4x4 &m) const {
return Vec3(x*m.xx + y*m.yx + z*m.zx,
Expand Down
6 changes: 0 additions & 6 deletions ext/native/math/lin/vec3.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class Vec4 {
float x,y,z,w;
Vec4(){}
Vec4(float a, float b, float c, float d) {x=a;y=b;z=c;w=d;}
Vec4 multiply4D(Matrix4x4 &m) const;
};

class Vec3 {
Expand Down Expand Up @@ -80,7 +79,6 @@ class Vec3 {
void operator *=(const Matrix4x4 &m) {
*this = *this * m;
}
Vec4 multiply4D(const Matrix4x4 &m) const;
Vec3 rotatedBy(const Matrix4x4 &m) const;
Vec3 operator %(const Vec3 &v) const {
return Vec3(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x);
Expand Down Expand Up @@ -134,10 +132,6 @@ inline Vec3 cross(const Vec3 &a, const Vec3 &b) {
return a % b;
}

inline float sqr(const Vec3 &v) {
return dot(v, v);
}

class AABBox {
public:
Vec3 min;
Expand Down
42 changes: 0 additions & 42 deletions ext/native/math/math_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

#include "base/basictypes.h"

inline float sqr(float f) {return f*f;}
inline float sqr_signed(float f) {return f<0 ? -f*f : f*f;}

typedef unsigned short float16;

// This ain't a 1.5.10 float16, it's a stupid hack format where we chop 16 bits off a float.
Expand Down Expand Up @@ -56,33 +53,6 @@ inline uint32_t log2i(uint32_t val) {
#define M_PI 3.141592653589793f
#endif

// Calculate pseudo-random 32 bit number based on linear congruential method.
void SetSeed(unsigned int seed);
unsigned int GenerateRandomNumber();
inline float GenerateRandomFloat01() {
return (float)((double)GenerateRandomNumber() / 0xFFFFFFFF);
}
inline float GenerateRandomSignedFloat() {
return (float)((double)GenerateRandomNumber() / 0x80000000) - 1.0f;
}


inline float GaussRand()
{
float R1 = GenerateRandomFloat01();
float R2 = GenerateRandomFloat01();

float X = sqrtf(-2.0f * logf(R1)) * cosf(2.0f * PI * R2);
if (X > 4.0f) X = 4.0f;
if (X < -4.0f) X = -4.0f;
return X;
}

// Accuracy unknown
inline double atan_fast(double x) {
return (x / (1.0 + 0.28 * (x * x)));
}

template<class T>
inline T clamp_value(T val, T floor, T cap) {
if (val > cap)
Expand All @@ -93,18 +63,6 @@ inline T clamp_value(T val, T floor, T cap) {
return val;
}

// linear -> dB conversion
inline float lin2dB(float lin) {
const float LOG_2_DB = 8.6858896380650365530225783783321f; // 20 / ln( 10 )
return logf(lin) * LOG_2_DB;
}

// dB -> linear conversion
inline float dB2lin(float dB) {
const float DB_2_LOG = 0.11512925464970228420089957273422f; // ln( 10 ) / 20
return expf(dB * DB_2_LOG);
}

union FP32 {
uint32_t u;
float f;
Expand Down

0 comments on commit ec7458b

Please sign in to comment.