Skip to content

Commit

Permalink
Added more functionality to the Matrices (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoEPRodrigues committed Oct 12, 2017
1 parent faec8cc commit 37b57c4
Show file tree
Hide file tree
Showing 16 changed files with 1,060 additions and 326 deletions.
713 changes: 399 additions & 314 deletions .idea/workspace.xml

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions src/Math/Maths.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace ThreeEngine {


typedef float number;

/// Epsilon is the difference between 1.0 and the next value representable by the floating-point.
Expand All @@ -22,9 +23,10 @@ namespace ThreeEngine {
/// Units in the Last Place to take into account in a floating-point type.
const float ULP = 3.0f;

struct Maths
{
static number GetRandom(const number& lowLimit, const number& HighLimit);
struct Maths {
enum Axis { X, Y, Z, W };

static number GetRandom(const number& lowLimit, const number& HighLimit);

private:
Maths() = default;
Expand Down
88 changes: 88 additions & 0 deletions src/Math/Matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "Matrix.h"
#include "../Debug.h"

using namespace std;

namespace ThreeEngine {
Matrix::Matrix() {
Reset(0.0f);
Expand All @@ -26,6 +28,11 @@ namespace ThreeEngine {
in12, in13, in14, in15);
}

Matrix::Matrix(Matrix3 m) : Matrix(m.M[0][0], m.M[0][1], m.M[0][2], 0,
m.M[1][0], m.M[1][1], m.M[1][2], 0,
m.M[2][0], m.M[2][1], m.M[2][2], 0,
0, 0, 0, 1) {}

Matrix::Matrix(const Matrix& other) {
operator=(other);
}
Expand Down Expand Up @@ -79,6 +86,14 @@ namespace ThreeEngine {
0.0f, 0.0f, 0.0f, 1.0f);
}

void Matrix::SetMatrix3(const Matrix3& m) {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
M[i][j] = m.M[i][j];
}
}
}

void Matrix::Reset(const float& value) {
for (int i = 0; i < 4; ++i) {
std::fill(std::begin(M[i]), std::end(M[i]), value);
Expand Down Expand Up @@ -302,6 +317,28 @@ namespace ThreeEngine {
return M[idx];
}

Vector4 Matrix::operator*(const Vector4& v) {
Vector4 vector;
for (int i = 0; i < 4; ++i) {
vector.X += M[0][i] * v[i];
vector.Y += M[1][i] * v[i];
vector.Z += M[2][i] * v[i];
vector.W += M[3][i] * v[i];
}
return vector;
}

Vector4 operator*(const Vector4& v, const Matrix& m) {
Vector4 vector;
for (int i = 0; i < 4; ++i) {
vector.X += m.M[i][0] * v[i];
vector.Y += m.M[i][1] * v[i];
vector.Z += m.M[i][2] * v[i];
vector.W += m.M[i][3] * v[i];
}
return vector;
}

Matrix Matrix::GetTransposed() {
Matrix m;

Expand All @@ -327,4 +364,55 @@ namespace ThreeEngine {

return m;
}

Matrix Matrix::ScaleMatrix(const number& inX, const number& inY, const number& inZ,
const number& inW) {
return {inX, 0, 0, 0,
0, inY, 0, 0,
0, 0, inZ, 0,
0, 0, 0, inW};
}

Matrix Matrix::ScaleMatrixInverted(const number& inX, const number& inY, const number& inZ,
const number& inW) {
return {1.0f / inX, 0, 0, 0,
0, 1.0f / inY, 0, 0,
0, 0, 1.0f / inZ, 0,
0, 0, 0, 1.0f / inW};
}

Matrix Matrix::RotationMatrix(Maths::Axis axis, const number& angle) {
switch (axis) {
case Maths::Axis::X:
return {1, 0, 0, 0,
0, cos(angle), -sin(angle), 0,
0, -sin(angle), cos(angle), 0,
0, 0, 0, 1};
case Maths::Axis::Y:
return {cos(angle), 0, -sin(angle), 0,
0, 1, 0, 0,
-sin(angle), 0, cos(angle), 0,
0, 0, 0, 1};
case Maths::Axis::Z:
default:
return {cos(angle), -sin(angle), 0, 0,
-sin(angle), cos(angle), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1};
}
}

Matrix Matrix::TranslationMatrix(const Vector& vector) {
return Matrix(1, 0, 0, vector.X,
0, 1, 0, vector.Y,
0, 0, 1, vector.Z,
0, 0, 0, 1);
}

Matrix Matrix::TranslationMatrix(const Vector4& vector) {
return Matrix(1, 0, 0, vector.X,
0, 1, 0, vector.Y,
0, 0, 1, vector.Z,
0, 0, 0, 1);
}
} /* namespace Divisaction */
45 changes: 45 additions & 0 deletions src/Math/Matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <ostream>
#include <string>
#include "Maths.h"
#include "Vector4.h"
#include "Matrix3.h"

namespace ThreeEngine {

Expand All @@ -23,6 +25,8 @@ namespace ThreeEngine {

explicit Matrix(number in);

explicit Matrix(Matrix3 m);

Matrix(number in0, number in1, number in2, number in3,
number in4, number in5, number in6, number in7,
number in8, number in9, number in10, number in11,
Expand All @@ -47,6 +51,11 @@ namespace ThreeEngine {
*/
void SetIdentity();

/**
* Set this to the identity matrix
*/
void SetMatrix3(const Matrix3& m);

/**
* Set all values in this to given value
*
Expand Down Expand Up @@ -116,6 +125,20 @@ namespace ThreeEngine {
*/
Matrix operator*(const Matrix& V) const;

/**
* Results in the multiplication of this matrix with a Vector.
* @param v
* @return multiplication vector
*/
Vector4 operator*(const Vector4& v);

/**
* Results in the multiplication of a vector with this matrix.
* @param v
* @return multiplication vector
*/
friend Vector4 operator*(const Vector4& v, const Matrix& m);

/**
* Check against another matrix for equality.
*
Expand Down Expand Up @@ -280,6 +303,28 @@ namespace ThreeEngine {
}
return message;
}

operator Matrix3() const {
Matrix3 m;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
m.M[i][j] = M[i][j];
}
}
return m;
}

static Matrix
ScaleMatrix(const number& inX, const number& inY, const number& inZ, const number& inW);

static Matrix ScaleMatrixInverted(const number& inX, const number& inY, const number& inZ,
const number& inW);

static Matrix RotationMatrix(Maths::Axis axis, const number& angle);

static Matrix TranslationMatrix(const Vector& vector);

static Matrix TranslationMatrix(const Vector4& vector);
};

} /* namespace Divisaction */
Expand Down
71 changes: 71 additions & 0 deletions src/Math/Matrix2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,77 @@
*/
#include "Matrix2.h"

using namespace std;

namespace ThreeEngine {

Matrix2::Matrix2() : TMatrix() {}

Matrix2::Matrix2(TMatrix<2, 2> m) {
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
M[i][j] = m.M[i][j];
}
}
}

Matrix2::Matrix2(number in0, number in1,
number in2, number in3) {

M[0][0] = in0;
M[0][1] = in1;
M[1][0] = in2;
M[1][1] = in3;
}

Matrix2 Matrix2::ScaleMatrix(const number& inX, const number& inY) {
return {inX, 0,
0, inY};
}

Matrix2 Matrix2::ScaleMatrixInverted(const number& inX, const number& inY) {
return {1.0f / inX, 0,
0, 1.0f / inY};
}

Matrix2 Matrix2::RotationMatrix(const number& angle) {
return Matrix2(cos(angle), sin(angle),
-sin(angle), cos(angle));
}

Matrix2 Matrix2::RotationMatrixInverted(const number& angle) {
return Matrix2(cos(angle), -sin(angle),
sin(angle), cos(angle));
}

Vector2 Matrix2::operator*(const Vector2& v) {
Vector2 vector;
for (int i = 0; i < 2; ++i) {
vector.X += M[0][i] * v[i];
vector.Y += M[1][i] * v[i];
}
return vector;
}

Vector2 operator*(const Vector2& v, const Matrix2& m) {
Vector2 vector;
for (int i = 0; i < 2; ++i) {
vector.X += m.M[i][0] * v[i];
vector.Y += m.M[i][1] * v[i];
}
return vector;
}

float Matrix2::Determinant() const {
return (M[0][0] * M[1][1]) - (M[0][1] * M[1][0]);
}

Matrix2 Matrix2::Inverse() const {
Matrix2 inverse = {
M[1][1], -M[0][1],
-M[1][0], M[0][0]
};
return {(1.0f / Determinant()) * inverse};
}

} /* namespace Divisaction */
35 changes: 34 additions & 1 deletion src/Math/Matrix2.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,43 @@
#define THREEENGINE_MATRIX2_H

#include "TMatrix.h"
#include "Vector2.h"

namespace ThreeEngine {

class Matrix2 : public TMatrix<2, 2> {
struct Matrix2 : public TMatrix<2, 2> {

Matrix2();

Matrix2(TMatrix<2, 2>);

Matrix2(number in0, number in1,
number in2, number in3);

static Matrix2 ScaleMatrix(const number& inX, const number& inY);

static Matrix2 ScaleMatrixInverted(const number& inX, const number& inY);

static Matrix2 RotationMatrix(const number& angle);

static Matrix2 RotationMatrixInverted(const number& angle);

/**
* Results in the multiplication of this matrix with a Vector.
* @param v
* @return multiplication vector
*/
Vector2 operator*(const Vector2& v);

/**
* Results in the multiplication of a vector with this matrix.
* @param v
* @return multiplication vector
*/
friend Vector2 operator*(const Vector2& v, const Matrix2& m);

float Determinant() const;
Matrix2 Inverse() const;
};

} /* namespace Divisaction */
Expand Down
Loading

0 comments on commit 37b57c4

Please sign in to comment.