1 #ifndef MATRIX_HXX
2 #define MATRIX_HXX
3
4 class Matrix
5 {
6 private:
7 /**
8 * create simple 4x4 unit matrix
9 * (1 on diagonals, 0 elsewhere)
10 */
11 void UnitMatrix()
12 {
13 for (unsigned int i = 0; i < 4; i++)
14 for (unsigned int j = 0; j < 4; j++)
15 if (i == j)
16 m[i][j] = 1;
17 else m[i][j] = 0;
18 }
19
20 public:
21 float m[4][4];
22
23 enum Axis
24 {
25 xAxis = 0,
26 yAxis = 1,
27 zAxis = 2
28 };
29
30 Matrix()
31 {
32 UnitMatrix();
33 }
34
35 ~Matrix()
36 {};
37
38 /**
39 * simple debug output
40 */
41 void output()
42 {
43 std::cout << "Matrix: \n";
44 for (unsigned int i = 0; i < 3; i++)
45 {
46 for (unsigned int j = 0; j < 3; j++)
47 std::cout << m[i][j] << " ";
48
49 std::cout << std::endl;
50 }
51 }
52
53 /**
54 * transpose matrix
55 * m[i][j] = m[j][i] for i=0..3,j=0..3
56 */
57 void Transpose(Matrix& matrix)
58 {
59 for (unsigned int i = 0; i < 4; i++)
60 for (unsigned int j = 0; j < 4; j++)
61 m[i][j] = matrix.m[j][i];
62 }
63
64 /**
65 * translation matrix
66 * as described in the lecture, change entries of last column of the matrix
67 */
68 void Translation(float dx, float dy, float dz)
69 {
70 Matrix trans;
71 trans.m[0][3] = dx;
72 trans.m[1][3] = dy;
73 trans.m[2][3] = dz;
74
75 *this = trans * (*this);
76 }
77
78 /**
79 * rotation matrix
80 * computation by cos and sin of the given angle
81 */
82 void Rotation(enum Axis axis, float theta)
83 {
84 Matrix rotate;
85 switch (axis)
86 {
87 case 0: rotate.m[1][1] = cos(theta);
88 rotate.m[1][2] = -sin(theta);
89 rotate.m[2][1] = sin(theta);
90 rotate.m[2][2] = cos(theta);
91 break;
92 case 1: rotate.m[0][0] = cos(theta);
93 rotate.m[0][2] = sin(theta);
94 rotate.m[2][0] = -sin(theta);
95 rotate.m[2][2] = cos(theta);
96 break;
97 case 2: rotate.m[0][0] = cos(theta);
98 rotate.m[0][1] = -sin(theta);
99 rotate.m[1][0] = sin(theta);
100 rotate.m[1][1] = cos(theta);
101 break;
102 default: std::cout << "Unknown axis" << std::endl;
103 exit(1);
104 }
105
106 *this = rotate * (*this);
107 }
108
109 /**
110 * scaling matrix
111 * therefore set the entries of the diagonal matrix
112 */
113 void Scaling(float sx, float sy, float sz)
114 {
115 Matrix scale;
116 scale.m[0][0] = sx;
117 scale.m[1][1] = sy;
118 scale.m[2][2] = sz;
119
120 *this = scale * (*this);
121 }
122
123 /**
124 * shear matrix
125 */
126 void Shear(float xy, float xz, float yz, float yx, float zx, float zy)
127 {
128 Matrix sh;
129 sh.m[0][1] = xy;
130 sh.m[0][2] = xz;
131 sh.m[1][0] = yx;
132 sh.m[1][2] = yz;
133 sh.m[2][0] = zx;
134 sh.m[2][1] = zy;
135
136 *this = sh * (*this);
137 }
138
139 /**
140 * multiplication of matrix and vector
141 * multiply row by row with the colum vector
142 */
143 friend Vec3f operator* (const Matrix& matrix, const Vec3f& vec)
144 {
145 Vec3f result = Vec3f(0);
146
147 Vec4f hom = Vec4f( vec[0], vec[1], vec[2], 1.0 );
148 for (unsigned int j = 0; j < 3; j++)
149 for (unsigned int i = 0; i < 4; i++)
150 result[j] += matrix.m[j][i] * hom[i];
151
152 return result;
153 };
154
155 /**
156 * multiplication of matrix and matrix
157 */
158 friend Matrix operator* (const Matrix& a, const Matrix& b)
159 {
160 Matrix c;
161 for (unsigned int i = 0; i < 4; i++)
162 for (unsigned int j = 0; j < 4; j++)
163 {
164 c.m[i][j] = 0.0;
165 for (unsigned int k = 0; k < 4; k++)
166 c.m[i][j] += a.m[i][k] * b.m[k][j];
167 }
168 return c;
169 };
170 };
171
172 #endif
syntax highlighted by Code2HTML, v. 0.9.1