Extended matrix functions
This commit is contained in:
parent
f38e01d80a
commit
af892bc943
49
Matrix.cpp
49
Matrix.cpp
@ -1,24 +1,17 @@
|
||||
#include "Matrix.h"
|
||||
|
||||
template <> void MatrixOf<float>::Transpose(MatrixOf<float> *r) {
|
||||
// Check dimensions first
|
||||
// We dont care about the rows and cols (we overwrite them)
|
||||
// but the data size should be equal to avoid problems
|
||||
// We cannot check the data size directly, but the row*col should be equal
|
||||
template <> MatrixOf<float>::MatrixOf(unsigned int rows, unsigned int cols) {
|
||||
this->rows = rows;
|
||||
this->cols = cols;
|
||||
|
||||
unsigned int matrixSize = this->cols * this->rows;
|
||||
if (matrixSize != r->rows * r->cols)
|
||||
// Exception??? For now we don't do anything
|
||||
return;
|
||||
this->data = new float[matrixSize]{0.0f};
|
||||
}
|
||||
|
||||
r->cols = this->rows;
|
||||
r->rows = this->cols;
|
||||
|
||||
for (int rDataIx = 0; rDataIx < matrixSize; rDataIx++) {
|
||||
unsigned int rowIx = rDataIx / this->rows;
|
||||
unsigned int colIx = rDataIx % this->rows;
|
||||
unsigned int mDataIx = this->cols * colIx + rowIx;
|
||||
r->data[rDataIx] = this->data[mDataIx];
|
||||
}
|
||||
template <> MatrixOf<float>::MatrixOf(Vector3 v) : MatrixOf(3, 1) {
|
||||
Set(0, 0, v.x);
|
||||
Set(1, 0, v.y);
|
||||
Set(2, 0, v.z);
|
||||
}
|
||||
|
||||
template <>
|
||||
@ -37,23 +30,14 @@ void MatrixOf<float>::Multiply(const MatrixOf<float> *m1, MatrixOf<float> *m2,
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void MatrixOf<float>::Multiply(MatrixOf<float> *m2, MatrixOf<float> *r) {
|
||||
Multiply(this, m2, r);
|
||||
}
|
||||
|
||||
template <>
|
||||
Vector3 MatrixOf<float>::Multiply(const MatrixOf<float> *m, Vector3 v) {
|
||||
float *vData = new float[3]{v.x, v.y, v.z};
|
||||
MatrixOf<float> v_m = MatrixOf<float>(3, 1, vData);
|
||||
float *rData = new float[3]{};
|
||||
MatrixOf<float> r_m = MatrixOf<float>(3, 1, rData);
|
||||
MatrixOf<float> v_m = MatrixOf<float>(v);
|
||||
MatrixOf<float> r_m = MatrixOf<float>(3, 1);
|
||||
|
||||
Multiply(m, &v_m, &r_m);
|
||||
|
||||
Vector3 r = Vector3(r_m.data[0], r_m.data[1], r_m.data[2]);
|
||||
delete[] vData;
|
||||
delete[] rData;
|
||||
return r;
|
||||
}
|
||||
|
||||
@ -70,12 +54,3 @@ template <typename T> Vector3 MatrixOf<T>::operator*(const Vector3 v) const {
|
||||
delete[] rData;
|
||||
return r;
|
||||
}
|
||||
|
||||
template <> float MatrixOf<float>::Get(unsigned int rowIx, unsigned int colIx) {
|
||||
unsigned int dataIx = rowIx * this->cols + colIx;
|
||||
return this->data[dataIx];
|
||||
}
|
||||
|
||||
template <> unsigned int MatrixOf<float>::RowCount() { return rows; }
|
||||
|
||||
template <> unsigned int MatrixOf<float>::ColCount() { return cols; }
|
76
Matrix.h
76
Matrix.h
@ -6,23 +6,83 @@
|
||||
/// @brief Single precision float matrix
|
||||
template <typename T> class MatrixOf {
|
||||
public:
|
||||
MatrixOf(unsigned int rows, unsigned int cols, T *data)
|
||||
: rows(rows), cols(cols), data(data) {}
|
||||
// MatrixOf(Vector3 v); // creates a 3,1 matrix
|
||||
MatrixOf(unsigned int rows, unsigned int cols);
|
||||
MatrixOf(unsigned int rows, unsigned int cols, T *source)
|
||||
: MatrixOf(rows, cols) {
|
||||
Set(source);
|
||||
}
|
||||
MatrixOf(Vector3 v); // creates a 3,1 matrix
|
||||
|
||||
~MatrixOf() {
|
||||
if (this->data == nullptr)
|
||||
return;
|
||||
|
||||
delete[] this->data;
|
||||
}
|
||||
|
||||
/// @brief Transpose with result in matrix m
|
||||
/// @param r The matrix in which the transposed matrix is stored
|
||||
void Transpose(MatrixOf<T> *r);
|
||||
void Transpose(MatrixOf<T> *r) const {
|
||||
// Check dimensions first
|
||||
// We dont care about the rows and cols (we overwrite them)
|
||||
// but the data size should be equal to avoid problems
|
||||
// We cannot check the data size directly, but the row*col should be equal
|
||||
unsigned int matrixSize = this->cols * this->rows;
|
||||
if (matrixSize != r->rows * r->cols)
|
||||
// Exception??? For now we don't do anything
|
||||
return;
|
||||
|
||||
r->cols = this->rows;
|
||||
r->rows = this->cols;
|
||||
|
||||
for (int rDataIx = 0; rDataIx < matrixSize; rDataIx++) {
|
||||
unsigned int rowIx = rDataIx / this->rows;
|
||||
unsigned int colIx = rDataIx % this->rows;
|
||||
unsigned int mDataIx = this->cols * colIx + rowIx;
|
||||
r->data[rDataIx] = this->data[mDataIx];
|
||||
}
|
||||
}
|
||||
|
||||
static void Multiply(const MatrixOf<T> *m1, MatrixOf<T> *m2, MatrixOf<T> *r);
|
||||
void Multiply(MatrixOf<T> *m, MatrixOf<T> *r);
|
||||
void Multiply(MatrixOf<T> *m, MatrixOf<T> *r) { Multiply(this, m, r); }
|
||||
|
||||
static Vector3 Multiply(const MatrixOf<T> *m, Vector3 v);
|
||||
Vector3 operator*(const Vector3 v) const;
|
||||
|
||||
T Get(unsigned int rowIx, unsigned int colIx);
|
||||
unsigned int RowCount();
|
||||
unsigned int ColCount();
|
||||
T Get(unsigned int rowIx, unsigned int colIx) {
|
||||
unsigned int dataIx = rowIx * this->cols + colIx;
|
||||
return this->data[dataIx];
|
||||
}
|
||||
|
||||
void Set(unsigned int rowIx, unsigned int colIx, T value) {
|
||||
unsigned int dataIx = rowIx * this->cols + colIx;
|
||||
this->data[dataIx] = value;
|
||||
}
|
||||
|
||||
// This function does not check on source size!
|
||||
void Set(T *source) {
|
||||
unsigned int matrixSize = this->cols * this->rows;
|
||||
for (unsigned int dataIx = 0; dataIx < matrixSize; dataIx++)
|
||||
this->data[dataIx] = source[dataIx];
|
||||
}
|
||||
|
||||
// This function does not check on source size!
|
||||
void SetRow(unsigned int rowIx, const T *source) {
|
||||
unsigned int dataIx = rowIx * this->cols;
|
||||
for (unsigned int sourceIx = 0; sourceIx < this->cols; dataIx++, sourceIx++)
|
||||
this->data[dataIx] = source[sourceIx];
|
||||
}
|
||||
|
||||
// This function does not check on source size!
|
||||
void SetCol(unsigned int colIx, const T *source) {
|
||||
unsigned int dataIx = colIx;
|
||||
for (unsigned int sourceIx = 0; sourceIx < this->cols;
|
||||
dataIx += this->cols, sourceIx++)
|
||||
this->data[dataIx] = source[sourceIx];
|
||||
}
|
||||
|
||||
unsigned int RowCount() { return rows; }
|
||||
unsigned int ColCount() { return cols; }
|
||||
|
||||
private:
|
||||
unsigned int rows;
|
||||
|
Loading…
x
Reference in New Issue
Block a user