Skip to content

Commit

Permalink
Remove some dependencies with boost; rewrite some parts of Matrix<T, N>
Browse files Browse the repository at this point in the history
  • Loading branch information
DDuarte committed Mar 19, 2013
1 parent 60ccc1d commit a846055
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 47 deletions.
1 change: 0 additions & 1 deletion src/LeEngine/Kernel.h
Expand Up @@ -3,7 +3,6 @@

#include "Singleton.h"
#include "ITask.h"
#include "Shapes.h"

#include <list>
#include <memory>
Expand Down
76 changes: 43 additions & 33 deletions src/LeEngine/Matrix.h
Expand Up @@ -5,29 +5,31 @@
#include "DMath.h"
#include "MathDefines.h"

#include <cstring> // memcpy, memset
#include <cassert>
#include <cstdarg>
#include <vector>

#include <boost/shared_array.hpp>
using boost::shared_array;
#include <algorithm>
#include <array>

template <int Size, typename T = float>
class Matrix
{
static_assert(std::is_scalar<T>::value, "Matrix should use a scalar type.");
private:
T M[Size][Size];
std::array<std::array<T, Size>, Size> M;

public:
typedef typename std::conditional<std::is_floating_point<T>::value, T, double>::type Real;
typedef T Type;
static const int Size = Size;
static const Matrix<Size, T> IDENTITY;
static const Matrix<Size, T> ZERO;

Matrix() { memset(M, 0, Size * Size * sizeof(T)); }
Matrix() { std::fill_n(M, Size * Size, 0); }

Matrix(const T arr[Size][Size]) { memcpy(M, arr, Size * Size * sizeof(T)); }
Matrix(const T arr[Size][Size]) { std::copy_n(arr, Size * Size, M) };

Matrix(const Matrix<Size, T>& matrix) { memcpy(M, matrix.M, Size * Size * sizeof(T)); }
Matrix(const Matrix<Size, T>& matrix) { std::copy_n(matrix.M, Size * Size, M); }

Matrix(const T vals[Size * Size])
{
Expand Down Expand Up @@ -116,8 +118,7 @@ class Matrix
return result;
}

template <typename S>
Matrix<Size, T> operator *(const S& scalar) const
Matrix<Size, T> operator *(const T& scalar) const
{
Matrix<Size, T> result;
for (int row = 0; row < Size; ++row)
Expand All @@ -126,24 +127,28 @@ class Matrix
return result;
}

shared_array<T> GetRow(int row) const
std::vector<T> GetRow(int row) const
{
assert(row < Size);

shared_array<T> r(new T[Size]);
memcpy(r, M[row], Size * sizeof(T));
std::vector<T> r(Size);

for (int i = 0; i < Size; ++i)
r[i] = M[row];

return r;
}

shared_array<T> GetColumn(int column) const
std::vector<T> GetColumn(int column) const
{
assert(column < Size);

shared_array<T> col(new T[Size]);
std::vector<T> r(Size);

for (int i = 0; i < Size; ++i)
col[i] = M[i][column];
return col;
r[i] = M[i][column];

return r;
}

Matrix<Size, T>& SetRow(int row, const T arr[Size])
Expand All @@ -169,15 +174,15 @@ class Matrix
assert(column1 < Size && column2 < Size);
if (column1 != column2)
{
shared_array<T> aux = GetColumn(column1);
SetColumn(column1, GetColumn(column2).get());
std::vector<T> aux = GetColumn(column1);
SetColumn(column1, GetColumn(column2).data());
SetColumn(column2, aux.get());
}

return *this;
}

double Determinant() const
Real Determinant() const
{
if (Size == 1)
return M[0][0];
Expand Down Expand Up @@ -238,11 +243,11 @@ class Matrix
}
}

double det = 1;
Real det = 1;
for (int i = 0; i < Size; ++i)
det *= m[i][i];

return det * pow(-1.0, colEx);
return det * Math<Real>::Pow(-1.0, colEx);
}
}

Expand All @@ -262,18 +267,18 @@ class Matrix
return aux.Transpose();
}

double Cofactor(int l, int c) const
Real Cofactor(int l, int c) const
{
const Matrix<Size,T>& m = *this;
std::vector<double> subMatrixVec;
std::vector<Real> subMatrixVec;

for (int i = 0; i < Size; i++)
if (i != l)
for (int j = 0; j < Size; j++)
if (j != c)
subMatrixVec.push_back(m[i][j]);

Matrix<Size-1, double> subMat(&subMatrixVec[0]);
Matrix<Size-1, Real> subMat(&subMatrixVec[0]);

return pow(-1.0, l+c) * subMat.Determinant();
}
Expand Down Expand Up @@ -317,7 +322,7 @@ class Matrix
//! Returns true if matrix is invertible. Result is stored in first argument, result.
bool Inverse(Matrix<Size, T>& result) const
{
double determinant = Determinant();
Real determinant = Determinant();

if (IsZero(determinant))
return false;
Expand Down Expand Up @@ -347,27 +352,32 @@ const Matrix<Size, T> Matrix<Size, T>::ZERO;
template <int Size, typename T>
const Matrix<Size, T> Matrix<Size, T>::IDENTITY = BuildIdentityMatrix();

typedef Matrix<4, float> mat4;
typedef Matrix<3, float> Matrix3f;
typedef Matrix<3, double> Matrix3d;
typedef Matrix<3, int> Matrix3i;
typedef Matrix<4, float> Matrix4f;
typedef Matrix<4, double> Matrix4d;
typedef Matrix<4, int> Matrix4i;

inline mat4 translate(float x, float y, float z)
inline Matrix4f CreateTranslasteMatrix(float x, float y, float z)
{
float m[] = { 1, 0, 0, x,
0, 1, 0, y,
0, 0, 1, z,
0, 0, 0, 1 };
return mat4(m);
return Matrix4f(m);
}

inline mat4 scale(float x, float y, float z)
inline Matrix4f CreateScaleMatrix(float x, float y, float z)
{
float m[] = { x, 0, 0, 0,
0, y, 0, 0,
0, 0, z, 0,
0, 0, 0, 1 };
return mat4(m);
return Matrix4f(m);
}

inline mat4 rotate(float rotation, Vector3f axis)
inline Matrix4f CreateRotateMatrix(float rotation, Vector3f axis)
{
float a = rotation * Math<float>::ACos(-1.0f) / 180.0f;
float c = Math<float>::Cos(a);
Expand All @@ -391,7 +401,7 @@ inline mat4 rotate(float rotation, Vector3f axis)
0,
0, 0, 0, 1 };

return mat4(m);
return Matrix4f(m);
}

#endif // MATRIX_H
18 changes: 12 additions & 6 deletions src/LeEngine/Random.cpp
@@ -1,12 +1,18 @@
#include "Random.h"
#include <boost/random.hpp>

Random::Random(int seed /*= clock()*/)
Random::Random(int seed)
{
_gen = GeneratorType(seed);
_uniformDist01 = NULL;
}

Random::Random()
{
std::random_device rd;
_gen = GeneratorType(rd());
_uniformDist01 = NULL;
}

Random::~Random()
{
if (_uniformDist01)
Expand All @@ -16,18 +22,18 @@ Random::~Random()
float Random::Unit()
{
if (!_uniformDist01)
_uniformDist01 = new boost::random::uniform_01<GeneratorType, float>(_gen);
return (*_uniformDist01)();
_uniformDist01 = new std::uniform_real_distribution<float>(0.0f, 1.0f);
return (*_uniformDist01)(_gen);
}

float Random::Interval(float min, float max)
{
boost::random::uniform_real_distribution<float> dist(min, max);
std::uniform_real_distribution<float> dist(min, max);
return dist(_gen);
}

int Random::Interval(int min, int max)
{
boost::random::uniform_int_distribution<> dist(min, max - 1); // uniform_int is [min, max]
std::uniform_int_distribution<> dist(min, max - 1);
return dist(_gen);
}
11 changes: 8 additions & 3 deletions src/LeEngine/Random.h
@@ -1,6 +1,7 @@
#ifndef RANDOM_H
#define RANDOM_H

#include <random>
#include <boost/random.hpp>
#include <boost/concept_check.hpp>
#include <ctime>
Expand All @@ -9,14 +10,17 @@
class Random
{
private:
typedef boost::random::mt19937 GeneratorType;
typedef std::mt19937 GeneratorType;

GeneratorType _gen;
boost::random::uniform_01<GeneratorType, float>* _uniformDist01;
std::uniform_real_distribution<float>* _uniformDist01;

public:
//! Constructor
Random(int seed = clock());
Random();

//! Constructor
Random(int seed);

//! Destructor
~Random();
Expand All @@ -39,6 +43,7 @@ class Random
T Distribution(T* values, float* probabilities, int size)
{
boost::random::discrete_distribution<int, float> dist(probabilities, probabilities+size);
//std::discrete_distribution<float> dist(probabilities, probabilities + size);
int rand = dist(_gen);
return values[rand];
}
Expand Down
8 changes: 4 additions & 4 deletions src/LeEngine/Window.cpp
Expand Up @@ -15,15 +15,15 @@ bool Window::Create()
{
if(!glfwOpenWindow(_width, _height, _redBits, _greenBits, _blueBits, _alphaBits, _depthBits, _stencilBits, FullScreenMode(_fullScreen)))
{
LeLog.WriteP("Window: Could not open glfw window. Width: %i, height: %i, r: %i, g: %i, b: %i, a: %i, d: %i, s: %i, mode: %b",
_width, _height, _redBits, _greenBits, _blueBits, _alphaBits, _depthBits, _stencilBits, _fullScreen);
LeLog.WriteP("Window: Could not open glfw window. Width: %i, height: %i, r: %i, g: %i, b: %i, a: %i, d: %i, s: %i, fullscreen: %s",
_width, _height, _redBits, _greenBits, _blueBits, _alphaBits, _depthBits, _stencilBits, _fullScreen ? "yes" : "no");
return false;
}

glfwSetWindowTitle(_title.c_str());

LeLog.WriteP("Window: Opened glfw window. Width: %i, height: %i, r: %i, g: %i, b: %i, a: %i, d: %i, s: %i, mode: %b",
_width, _height, _redBits, _greenBits, _blueBits, _alphaBits, _depthBits, _stencilBits, _fullScreen);
LeLog.WriteP("Window: Opened glfw window. Width: %i, height: %i, r: %i, g: %i, b: %i, a: %i, d: %i, s: %i, fullscreen: %s",
_width, _height, _redBits, _greenBits, _blueBits, _alphaBits, _depthBits, _stencilBits, _fullScreen ? "yes" : "no");

return true;
}
Expand Down

0 comments on commit a846055

Please sign in to comment.