Added expiremental operator*
This commit is contained in:
parent
7b7df5d93d
commit
f38e01d80a
43
Matrix.cpp
43
Matrix.cpp
@ -1,7 +1,6 @@
|
|||||||
#include "Matrix.h"
|
#include "Matrix.h"
|
||||||
|
|
||||||
template <>
|
template <> void MatrixOf<float>::Transpose(MatrixOf<float> *r) {
|
||||||
void MatrixOf<float>::Transpose(MatrixOf<float>* r) {
|
|
||||||
// Check dimensions first
|
// Check dimensions first
|
||||||
// We dont care about the rows and cols (we overwrite them)
|
// We dont care about the rows and cols (we overwrite them)
|
||||||
// but the data size should be equal to avoid problems
|
// but the data size should be equal to avoid problems
|
||||||
@ -23,9 +22,8 @@ void MatrixOf<float>::Transpose(MatrixOf<float>* r) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void MatrixOf<float>::Multiply(MatrixOf<float>* m1,
|
void MatrixOf<float>::Multiply(const MatrixOf<float> *m1, MatrixOf<float> *m2,
|
||||||
MatrixOf<float>* m2,
|
MatrixOf<float> *r) {
|
||||||
MatrixOf<float>* r) {
|
|
||||||
for (unsigned int rowIx1 = 0; rowIx1 < m1->rows; rowIx1++) {
|
for (unsigned int rowIx1 = 0; rowIx1 < m1->rows; rowIx1++) {
|
||||||
for (unsigned int colIx2 = 0; colIx2 < m2->cols; colIx2++) {
|
for (unsigned int colIx2 = 0; colIx2 < m2->cols; colIx2++) {
|
||||||
unsigned int rDataIx = colIx2 * m2->cols + rowIx1;
|
unsigned int rDataIx = colIx2 * m2->cols + rowIx1;
|
||||||
@ -40,15 +38,15 @@ void MatrixOf<float>::Multiply(MatrixOf<float>* m1,
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void MatrixOf<float>::Multiply(MatrixOf<float>* m2, MatrixOf<float>* r) {
|
void MatrixOf<float>::Multiply(MatrixOf<float> *m2, MatrixOf<float> *r) {
|
||||||
Multiply(this, m2, r);
|
Multiply(this, m2, r);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
Vector3 MatrixOf<float>::Multiply(MatrixOf<float>* m, Vector3 v) {
|
Vector3 MatrixOf<float>::Multiply(const MatrixOf<float> *m, Vector3 v) {
|
||||||
float* vData = new float[3]{v.x, v.y, v.z};
|
float *vData = new float[3]{v.x, v.y, v.z};
|
||||||
MatrixOf<float> v_m = MatrixOf<float>(3, 1, vData);
|
MatrixOf<float> v_m = MatrixOf<float>(3, 1, vData);
|
||||||
float* rData = new float[3]{};
|
float *rData = new float[3]{};
|
||||||
MatrixOf<float> r_m = MatrixOf<float>(3, 1, rData);
|
MatrixOf<float> r_m = MatrixOf<float>(3, 1, rData);
|
||||||
|
|
||||||
Multiply(m, &v_m, &r_m);
|
Multiply(m, &v_m, &r_m);
|
||||||
@ -59,18 +57,25 @@ Vector3 MatrixOf<float>::Multiply(MatrixOf<float>* m, Vector3 v) {
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <>
|
template <typename T> Vector3 MatrixOf<T>::operator*(const Vector3 v) const {
|
||||||
float MatrixOf<float>::Get(unsigned int rowIx, unsigned int colIx) {
|
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);
|
||||||
|
|
||||||
|
Multiply(this, &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;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <> float MatrixOf<float>::Get(unsigned int rowIx, unsigned int colIx) {
|
||||||
unsigned int dataIx = rowIx * this->cols + colIx;
|
unsigned int dataIx = rowIx * this->cols + colIx;
|
||||||
return this->data[dataIx];
|
return this->data[dataIx];
|
||||||
}
|
}
|
||||||
|
|
||||||
template <>
|
template <> unsigned int MatrixOf<float>::RowCount() { return rows; }
|
||||||
unsigned int MatrixOf<float>::RowCount() {
|
|
||||||
return rows;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
template <> unsigned int MatrixOf<float>::ColCount() { return cols; }
|
||||||
unsigned int MatrixOf<float>::ColCount() {
|
|
||||||
return cols;
|
|
||||||
}
|
|
22
Matrix.h
22
Matrix.h
@ -4,30 +4,30 @@
|
|||||||
#include "Vector3.h"
|
#include "Vector3.h"
|
||||||
|
|
||||||
/// @brief Single precision float matrix
|
/// @brief Single precision float matrix
|
||||||
template <typename T>
|
template <typename T> class MatrixOf {
|
||||||
class MatrixOf {
|
public:
|
||||||
public:
|
MatrixOf(unsigned int rows, unsigned int cols, T *data)
|
||||||
MatrixOf(unsigned int rows, unsigned int cols, T* data)
|
|
||||||
: rows(rows), cols(cols), data(data) {}
|
: rows(rows), cols(cols), data(data) {}
|
||||||
MatrixOf(Vector3 v); // creates a 3,1 matrix
|
// MatrixOf(Vector3 v); // creates a 3,1 matrix
|
||||||
|
|
||||||
/// @brief Transpose with result in matrix m
|
/// @brief Transpose with result in matrix m
|
||||||
/// @param r The matrix in which the transposed matrix is stored
|
/// @param r The matrix in which the transposed matrix is stored
|
||||||
void Transpose(MatrixOf<T>* r);
|
void Transpose(MatrixOf<T> *r);
|
||||||
|
|
||||||
static void Multiply(MatrixOf<T>* m1, MatrixOf<T>* m2, MatrixOf<T>* r);
|
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);
|
||||||
|
|
||||||
static Vector3 Multiply(MatrixOf<T>* m, Vector3 v);
|
static Vector3 Multiply(const MatrixOf<T> *m, Vector3 v);
|
||||||
|
Vector3 operator*(const Vector3 v) const;
|
||||||
|
|
||||||
T Get(unsigned int rowIx, unsigned int colIx);
|
T Get(unsigned int rowIx, unsigned int colIx);
|
||||||
unsigned int RowCount();
|
unsigned int RowCount();
|
||||||
unsigned int ColCount();
|
unsigned int ColCount();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned int rows;
|
unsigned int rows;
|
||||||
unsigned int cols;
|
unsigned int cols;
|
||||||
T* data;
|
T *data;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
x
Reference in New Issue
Block a user