Skip to content
Permalink
Browse files

Common: Move Matrix classes out of MathUtil into their own files and …

…make their interface more friendly.
  • Loading branch information...
jordan-woyak committed Jan 29, 2019
1 parent 3b4486b commit b9a00a40a9a61c3df5c774fd1cc9d04f57994a0c
@@ -24,6 +24,7 @@ add_library(common
JitRegister.cpp
Logging/LogManager.cpp
MathUtil.cpp
Matrix.cpp
MD5.cpp
MemArena.cpp
MemoryUtil.cpp
@@ -134,6 +134,7 @@
<ClInclude Include="LdrWatcher.h" />
<ClInclude Include="LinearDiskCache.h" />
<ClInclude Include="MathUtil.h" />
<ClInclude Include="Matrix.h" />
<ClInclude Include="MD5.h" />
<ClInclude Include="MemArena.h" />
<ClInclude Include="MemoryUtil.h" />
@@ -201,6 +202,7 @@
<ClCompile Include="LdrWatcher.cpp" />
<ClCompile Include="Logging\ConsoleListenerWin.cpp" />
<ClCompile Include="MathUtil.cpp" />
<ClCompile Include="Matrix.cpp" />
<ClCompile Include="MD5.cpp" />
<ClCompile Include="MemArena.cpp" />
<ClCompile Include="MemoryUtil.cpp" />
@@ -256,4 +258,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
@@ -56,6 +56,7 @@
<ClInclude Include="IniFile.h" />
<ClInclude Include="LinearDiskCache.h" />
<ClInclude Include="MathUtil.h" />
<ClInclude Include="Matrix.h" />
<ClInclude Include="MemArena.h" />
<ClInclude Include="MemoryUtil.h" />
<ClInclude Include="MsgHandler.h" />
@@ -291,6 +292,7 @@
<ClCompile Include="Image.cpp" />
<ClCompile Include="IniFile.cpp" />
<ClCompile Include="MathUtil.cpp" />
<ClCompile Include="Matrix.cpp" />
<ClCompile Include="MemArena.cpp" />
<ClCompile Include="MemoryUtil.cpp" />
<ClCompile Include="MsgHandler.cpp" />
@@ -362,4 +364,4 @@
<ItemGroup>
<Natvis Include="BitField.natvis" />
</ItemGroup>
</Project>
</Project>
@@ -2,136 +2,12 @@
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include <cmath>
#include <cstring>
#include <limits>
#include <numeric>

#include "Common/CommonTypes.h"
#include "Common/MathUtil.h"

inline void MatrixMul(int n, const float* a, const float* b, float* result)
{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
float temp = 0;
for (int k = 0; k < n; ++k)
{
temp += a[i * n + k] * b[k * n + j];
}
result[i * n + j] = temp;
}
}
}
#include <numeric>

// Calculate sum of a float list
float MathFloatVectorSum(const std::vector<float>& Vec)
{
return std::accumulate(Vec.begin(), Vec.end(), 0.0f);
}

void Matrix33::LoadIdentity(Matrix33& mtx)
{
memset(mtx.data, 0, sizeof(mtx.data));
mtx.data[0] = 1.0f;
mtx.data[4] = 1.0f;
mtx.data[8] = 1.0f;
}

void Matrix33::RotateX(Matrix33& mtx, float rad)
{
float s = sin(rad);
float c = cos(rad);
memset(mtx.data, 0, sizeof(mtx.data));
mtx.data[0] = 1;
mtx.data[4] = c;
mtx.data[5] = -s;
mtx.data[7] = s;
mtx.data[8] = c;
}
void Matrix33::RotateY(Matrix33& mtx, float rad)
{
float s = sin(rad);
float c = cos(rad);
memset(mtx.data, 0, sizeof(mtx.data));
mtx.data[0] = c;
mtx.data[2] = s;
mtx.data[4] = 1;
mtx.data[6] = -s;
mtx.data[8] = c;
}

void Matrix33::Multiply(const Matrix33& a, const Matrix33& b, Matrix33& result)
{
MatrixMul(3, a.data, b.data, result.data);
}

void Matrix33::Multiply(const Matrix33& a, const float vec[3], float result[3])
{
for (int i = 0; i < 3; ++i)
{
result[i] = 0;

for (int k = 0; k < 3; ++k)
{
result[i] += a.data[i * 3 + k] * vec[k];
}
}
}

void Matrix44::LoadIdentity(Matrix44& mtx)
{
memset(mtx.data, 0, sizeof(mtx.data));
mtx.data[0] = 1.0f;
mtx.data[5] = 1.0f;
mtx.data[10] = 1.0f;
mtx.data[15] = 1.0f;
}

void Matrix44::LoadMatrix33(Matrix44& mtx, const Matrix33& m33)
{
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
mtx.data[i * 4 + j] = m33.data[i * 3 + j];
}
}

for (int i = 0; i < 3; ++i)
{
mtx.data[i * 4 + 3] = 0;
mtx.data[i + 12] = 0;
}
mtx.data[15] = 1.0f;
}

void Matrix44::Set(Matrix44& mtx, const float mtxArray[16])
{
for (int i = 0; i < 16; ++i)
{
mtx.data[i] = mtxArray[i];
}
}

void Matrix44::Translate(Matrix44& mtx, const float vec[3])
{
LoadIdentity(mtx);
mtx.data[3] = vec[0];
mtx.data[7] = vec[1];
mtx.data[11] = vec[2];
}

void Matrix44::Shear(Matrix44& mtx, const float a, const float b)
{
LoadIdentity(mtx);
mtx.data[2] = a;
mtx.data[6] = b;
}

void Matrix44::Multiply(const Matrix44& a, const Matrix44& b, Matrix44& result)
{
MatrixMul(4, a.data, b.data, result.data);
}
@@ -5,8 +5,6 @@
#pragma once

#include <algorithm>
#include <array>
#include <cstdlib>
#include <vector>

#include "Common/CommonTypes.h"
@@ -113,38 +111,3 @@ inline int IntLog2(u64 val)
return result;
#endif
}

// Tiny matrix/vector library.
// Used for things like Free-Look in the gfx backend.

class Matrix33
{
public:
static void LoadIdentity(Matrix33& mtx);

// set mtx to be a rotation matrix around the x axis
static void RotateX(Matrix33& mtx, float rad);
// set mtx to be a rotation matrix around the y axis
static void RotateY(Matrix33& mtx, float rad);

// set result = a x b
static void Multiply(const Matrix33& a, const Matrix33& b, Matrix33& result);
static void Multiply(const Matrix33& a, const float vec[3], float result[3]);

float data[9];
};

class Matrix44
{
public:
static void LoadIdentity(Matrix44& mtx);
static void LoadMatrix33(Matrix44& mtx, const Matrix33& m33);
static void Set(Matrix44& mtx, const float mtxArray[16]);

static void Translate(Matrix44& mtx, const float vec[3]);
static void Shear(Matrix44& mtx, const float a, const float b = 0);

static void Multiply(const Matrix44& a, const Matrix44& b, Matrix44& result);

float data[16];
};
@@ -0,0 +1,158 @@
// Copyright 2019 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include "Common/Matrix.h"

#include <cmath>
#include <algorithm>

inline void MatrixMul(int n, const float* a, const float* b, float* result)
{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
float temp = 0;
for (int k = 0; k < n; ++k)
{
temp += a[i * n + k] * b[k * n + j];
}
result[i * n + j] = temp;
}
}
}

Matrix33 Matrix33::Identity()
{
Matrix33 mtx = {};
mtx.data[0] = 1.0f;
mtx.data[4] = 1.0f;
mtx.data[8] = 1.0f;
return mtx;
}

Matrix33 Matrix33::RotateX(float rad)
{
float s = sin(rad);
float c = cos(rad);
Matrix33 mtx = {};
mtx.data[0] = 1;
mtx.data[4] = c;
mtx.data[5] = -s;
mtx.data[7] = s;
mtx.data[8] = c;
return mtx;
}

Matrix33 Matrix33::RotateY(float rad)
{
float s = sin(rad);
float c = cos(rad);
Matrix33 mtx = {};
mtx.data[0] = c;
mtx.data[2] = s;
mtx.data[4] = 1;
mtx.data[6] = -s;
mtx.data[8] = c;
return mtx;
}

Matrix33 Matrix33::RotateZ(float rad)
{
float s = sin(rad);
float c = cos(rad);
Matrix33 mtx = {};
mtx.data[0] = c;
mtx.data[1] = -s;
mtx.data[3] = s;
mtx.data[4] = c;
mtx.data[8] = 1;
return mtx;
}

Matrix33 Matrix33::Scale(const Vec3& vec)
{
Matrix33 mtx = {};
mtx.data[0] = vec.x;
mtx.data[4] = vec.y;
mtx.data[8] = vec.z;
return mtx;
}

void Matrix33::Multiply(const Matrix33& a, const Matrix33& b, Matrix33* result)
{
MatrixMul(3, a.data.data(), b.data.data(), result->data.data());
}

void Matrix33::Multiply(const Matrix33& a, const Vec3& vec, Vec3* result)
{
for (int i = 0; i < 3; ++i)
{
result->data[i] = 0;

for (int k = 0; k < 3; ++k)
{
result->data[i] += a.data[i * 3 + k] * vec.data[k];
}
}
}

Matrix44 Matrix44::Identity()
{
Matrix44 mtx = {};
mtx.data[0] = 1.0f;
mtx.data[5] = 1.0f;
mtx.data[10] = 1.0f;
mtx.data[15] = 1.0f;
return mtx;
}

Matrix44 Matrix44::FromMatrix33(const Matrix33& m33)
{
Matrix44 mtx;
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
mtx.data[i * 4 + j] = m33.data[i * 3 + j];
}
}

for (int i = 0; i < 3; ++i)
{
mtx.data[i * 4 + 3] = 0;
mtx.data[i + 12] = 0;
}
mtx.data[15] = 1.0f;
return mtx;
}

Matrix44 Matrix44::FromArray(const float mtxArray[16])
{
Matrix44 mtx;
std::copy_n(mtxArray, 16, std::begin(mtx.data));
return mtx;
}

Matrix44 Matrix44::Translate(const Vec3& vec)
{
Matrix44 mtx = Matrix44::Identity();
mtx.data[3] = vec.x;
mtx.data[7] = vec.y;
mtx.data[11] = vec.y;
return mtx;
}

Matrix44 Matrix44::Shear(const float a, const float b)
{
Matrix44 mtx = Matrix44::Identity();
mtx.data[2] = a;
mtx.data[6] = b;
return mtx;
}

void Matrix44::Multiply(const Matrix44& a, const Matrix44& b, Matrix44& result)
{
MatrixMul(4, a.data.data(), b.data.data(), result.data.data());
}

0 comments on commit b9a00a4

Please sign in to comment.
You can’t perform that action at this time.