templates - c++ Templated Matrix class multiplication -
i'm kind of new c++, want understand why following doesn't work or i'm doing wrong. i'm trying make matrix class using templates size , apply maths matrix or matrices.
in main.cpp have following within main method.
mat<4, 4> m = mat<4, 4>(1.0f); // identity matrix mat<2, 3> = mat<2, 3>(2.0f); // matrix 2 rows & 2 columns mat<3, 2> b = mat<3, 2>(3.0f); // matrix 3 rows & 2 columns mat<3, 3> c = * b; // multiplication. std::cout << c << std::endl;
with matrix class stated beneath multiplication works when both matrices have same size both number of rows , columns (ie 3x3, 4x4, etc.). it's legit multiply 2 matrices of different sizes long number of columns matrix same number of rows matrix b (ie a<2, 3> * b<3, 2> = c<3, 3>). matrix class needs able multiply matrices same size both number rows , columns (4x4 matrices 3d maths). know i'm doing wrong or understand why isn't working.
matrix class:
template <int rows, int columns> struct mat { float elements[rows * columns]; // default constructor. mat() { (int = 0; < rows * columns; i++) elements[i] = (float) 0.0f; } // basic constructor mat(float diagonal) : mat() { int min = rows > columns ? columns : rows; (int = 0; < min; i++) elements[i + * columns] = diagonal; } /************************* * helper functionalities * *************************/ // returns identity matrix. // should used when width , height same. static mat<4, 4> identity() { return mat<4, 4>(1.0f); } // returns orthographic matrix. static mat<4, 4> orthographic(float left, float right, float bottom, float top, float near, float far) { mat<4, 4> result(1.0f); result.elements[0 + 0 * 4] = 2.0f / (right - left); result.elements[1 + 1 * 4] = 2.0f / (top - bottom); result.elements[2 + 2 * 4] = 2.0f / (near - far); result.elements[0 + 3 * 4] = (left + right) / (left - right); result.elements[1 + 3 * 4] = (bottom + top) / (bottom - top); result.elements[2 + 3 * 4] = (far + near) / (far - near); return result; } // returns perspective matrix. static mat<4, 4> perspective(float fov, float aspect, float near, float far) { mat<4, 4> result(1.0f); result.elements[0 + 0 * 4] = (1.0f / tan(torad(0.5f * fov))) / aspect; result.elements[1 + 1 * 4] = (1.0f / tan(torad(0.5f * fov))); result.elements[2 + 2 * 4] = (near + far) / (near - far); result.elements[3 + 2 * 4] = -1.0f; result.elements[2 + 3 * 4] = (2.0f * near * far) / (near - far); return result; } // returns translation matrix. static mat<4, 4> translation(const vec<3>& translation) { mat<4, 4> result(1.0f); result.elements[0 + 3 * 4] = translation.x; result.elements[1 + 3 * 4] = translation.y; result.elements[2 + 3 * 4] = translation.z; return result; } // returns rotation matrix. static mat<4, 4> rotation(float angle, const vec<3>& axis) { mat<4, 4> result(1.0f); float radians = torad(angle); float c = cos(radians); float s = sin(radians); result.elements[0 + 0 * 4] = axis.x * (1.0f - c) + c; result.elements[1 + 0 * 4] = axis.y * axis.x * (1.0f - c) + axis.z * s; result.elements[2 + 0 * 4] = axis.x * axis.z * (1.0f - c) - axis.y * s; result.elements[0 + 1 * 4] = axis.x * axis.y * (1.0f - c) - axis.z * s; result.elements[1 + 1 * 4] = axis.y * (1.0f - c) + c; result.elements[2 + 1 * 4] = axis.y * axis.z * (1.0f - c) + axis.y * s; result.elements[0 + 2 * 4] = axis.x * axis.z * (1.0f - c) + axis.y * s; result.elements[1 + 2 * 4] = axis.y * axis.z * (1.0f - c) - axis.x * s; result.elements[2 + 2 * 4] = axis.z * (1.0f - c) + c; return result; } // returns scale matrix. static mat<4, 4> scale(const vec<3>& scale) { mat<4, 4> result(1.0f); result.elements[0 + 0 * 4] = scale.x; result.elements[1 + 1 * 4] = scale.y; result.elements[2 + 2 * 4] = scale.z; return result; } /*********************** * math functionalities * ***********************/ // overloaded addition operator add 2 matrices together. friend mat<rows, columns> operator + (mat<rows, columns> left, const mat<rows, columns>& right) { (int = 0; < rows * columns; i++) left.elements[i] += right.elements[i]; return left; } // overloaded addition operator add scalar matrix. friend mat<rows, columns> operator + (mat<rows, columns> left, const float& scalar) { (int = 0; < rows * columns; i++) left.elements[i] += scalar; return left; } // overloaded add , assign operator add matrix matrix. mat<rows, columns>& operator += (const mat<rows, columns>& other) { (int = 0; < rows * columns; i++) elements[i] += other.elements[i]; return *this; } // overloaded add , assign operator add scalar matrix. mat<rows, columns>& operator += (const float& scalar) { (int = 0; < rows * columns; i++) elements[i] += scalar; return *this; } // overloaded subtraction operator subtract matrix matrix. friend mat<rows, columns> operator - (mat<rows, columns> left, const mat<rows, columns>& right) { (int = 0; < rows * columns; i++) left.elements[i] -= right.elements[i]; return left; } // overloaded subtraction operator subtract scalar matrix. friend mat<rows, columns> operator - (mat<rows, columns> left, const float& scalar) { (int = 0; < rows * columns; i++) left.elements[i] -= scalar; return left; } // overloaded subtract , assign operator subtract matrix matrix. mat<rows, columns>& operator -= (const mat<rows, columns>& other) { (int = 0; < rows * columns; i++) elements[i] -= other.elements[i]; return *this; } // overloaded subtract , assign operator subtract scalar matrix. mat<rows, columns>& operator -= (const float& scalar) { (int = 0; < rows * columns; i++) elements[i] -= scalar; return *this; } template<int rows, int equals, int columns> // overloaded multiplication operator multiply matrix matrix. friend mat<rows, columns> operator * (const mat<rows, equals>& left, const mat<equals, columns>& right) { mat<rows, columns> result; (int y = 0; y < rows; y++) { (int x = 0; x < columns; x++) { float sum = 0.0f; (int = 0; < columns; i++) { sum += left.elements[x + * columns] * right.elements[i + y * columns]; } result.elements[x + y * columns] = sum; } } return result; } /************************* * output functionalities * *************************/ // overloaded output operator print vector output friend std::ostream& operator << (std::ostream& out, mat& matrix) { out << "mat<" << rows << ", " << columns << "> (" << std::endl; (int y = 0; y < rows; y++) { out << "\t"; (int x = 0; x < columns; x++) { out << matrix.elements[x + y * columns]; if (x < columns - 1) out << ", "; else out << std::endl; } } return out << ")"; } };
edit @filmor asked compilation error: error i'm getting error c2995: 'mat operator *(const mat &,const mat &)' : function template has been defined maths\matrix.h 190 1 tests
in piece of code operator* see 2 errors: first of should iterate on equals (not columns) , left matrix has equals
columns, need access elements via left.elements[x + * equals]
for (int = 0; < columns; i++) { sum += left.elements[x + * columns] * right.elements[i + y * columns]; }
Comments
Post a Comment