Effective Transformation Matrix Class Implementation

I want to make an effective useful matrix class to use for 3D transformation of points. Not it isn't intended to be used for linear systems or other uses for matrices. This said making the solution as generalized as possible (therefore leaving it open for other uses) is preferable.

My main problems are:

-How to structure the data.
-How to populate the matrix with values easily.

My question is around the population of data into the matrix.

Currently I have a std::vector of position vectors that make up the matrix. Since these vectors all have 4 components (for 3D transformations) it forces the matrix to have 4 columns. This is good for 3D transformation matrices but bad for a more generalized solution. What would be the best way of allowing custom row/column size when wanted but forcing the conditions for 3D transformations at other times. Is two classes a good option? Say the matrix class and then a class that contains a matrix but ensures it is the correct dimensions? Here is my code as it stands.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#ifndef MATRIX_NX4_HPP
#define MATRIX_NX4_HPP

#include "Vector4.hpp"
#include <Vector>

class MatrixNx4
{
    public:
        MatrixNx4(std::vector<Vector4> source);
        void ToString();
        int GetRows() const;
        Vector4 GetRow(int i) const;
    private:
        int rows_;
        std::vector<Vector4> matrix_;
};

MatrixNx4 operator*(const MatrixNx4& lhs, const MatrixNx4& rhs);

#endif // MATRIX_NX4_HPP

//MatrixNx4.cpp

#include "MatrixNx4.hpp"

MatrixNx4::MatrixNx4(std::vector<Vector4> source)
{
    rows_ = source.size();
    matrix_ = source;
}

void MatrixNx4::ToString()
{
    for (unsigned int i = 0; i < matrix_.size(); i++)
    {
        std::cout << matrix_[i].toString() << std::endl;
    }
}
int MatrixNx4::GetRows() const
{
    return rows_;
}
Vector4 MatrixNx4::GetRow(int i) const
{
    return matrix_[i];
}
MatrixNx4 operator*(const MatrixNx4& lhs, const MatrixNx4& rhs)
{
    if (rhs.GetRows() == 4)
    {
        std::vector<Vector4> ret;
        Vector4 lhsRow;
        Vector4 rhsRow;

        for (int i = 0; i < lhs.GetRows(); i++)
        {
            double v[4] = {0};
            lhsRow = lhs.GetRow(i);

            for (int j = 0; j < 4; j++)
            {
                for (int k = 0; k < 4; k++)
                {
                    rhsRow = rhs.GetRow(k);
                    v[j] += lhsRow[k] * rhsRow[j];
                }
            }

            ret.push_back(Vector4(v[0], v[1], v[2], v[3]));
        }

        return MatrixNx4(ret);
    }
}


If i can have a user defined number of rows/columns then I will face problems with matrix initialization. Is there a way around this?
Topic archived. No new replies allowed.