1 /* |
|
2 * C++ RayTracer |
|
3 * file: matrix.h |
|
4 * |
|
5 * Radek Brich, 2006 |
|
6 */ |
|
7 |
|
8 /* not used at this time */ |
|
9 |
|
10 #ifndef MATRIX_H |
|
11 #define MATRIX_H |
|
12 |
|
13 #include "vector.h" |
|
14 |
|
15 using namespace std; |
|
16 |
|
17 class Matrix |
|
18 { |
|
19 public: |
|
20 float data[4][4]; |
|
21 |
|
22 Matrix(): {}; |
|
23 |
|
24 // sum |
|
25 friend Matrix operator+(const Matrix &a, const Matrix &b) |
|
26 { |
|
27 Matrix m = Matrix(); |
|
28 for (int i = 0; i < 4; i++) |
|
29 for (int j = 0; j < 4; j++) |
|
30 m.data[i][j] = a.data[i][j] + b.data[i][j]; |
|
31 return m; |
|
32 } |
|
33 |
|
34 // difference |
|
35 friend Matrix operator-(const Matrix &a, const Matrix &b) |
|
36 { |
|
37 Matrix m = Matrix(); |
|
38 for (int i = 0; i < 4; i++) |
|
39 for (int j = 0; j < 4; j++) |
|
40 m.data[i][j] = a.data[i][j] - b.data[i][j]; |
|
41 return m; |
|
42 } |
|
43 |
|
44 // product |
|
45 friend Matrix operator*(const Matrix &a, const Matrix &b) |
|
46 { |
|
47 Matrix m = Matrix(); |
|
48 for (int i = 0; i < 4; i++) |
|
49 for (int j = 0; j < 4; j++) |
|
50 m.data[i][j] = |
|
51 a.data[i][0] * b.data[0][j] + |
|
52 a.data[i][1] * b.data[1][j] + |
|
53 a.data[i][2] * b.data[2][j] + |
|
54 a.data[i][3] * b.data[3][j]; |
|
55 return m; |
|
56 } |
|
57 |
|
58 // negative |
|
59 Matrix operator-() |
|
60 { |
|
61 Matrix m = Matrix(); |
|
62 for (int i = 0; i < 4; i++) |
|
63 for (int j = 0; j < 4; j++) |
|
64 m.data[i][j] = -data[i][j]; |
|
65 return m; |
|
66 } |
|
67 |
|
68 // product of matrix and scalar |
|
69 Matrix operator*(const float &f) |
|
70 { |
|
71 Matrix m = Matrix(); |
|
72 for (int i = 0; i < 4; i++) |
|
73 for (int j = 0; j < 4; j++) |
|
74 m.data[i][j] = data[i][j] * f; |
|
75 return m; |
|
76 } |
|
77 |
|
78 friend Matrix operator*(const float &f, Matrix &m) { return m * f; }; |
|
79 |
|
80 // product of matrix and vector |
|
81 Vector3 operator*(const Vector3 &v) |
|
82 { |
|
83 Vector3 u = Vector3(); |
|
84 u.x = data[0][0] * v.x + data[0][1] * v.y + data[0][2] * v.z + data[0][3] * v.w; |
|
85 u.y = data[1][0] * v.x + data[1][1] * v.y + data[1][2] * v.z + data[1][3] * v.w; |
|
86 u.z = data[2][0] * v.x + data[2][1] * v.y + data[2][2] * v.z + data[2][3] * v.w; |
|
87 u.w = data[3][0] * v.x + data[3][1] * v.y + data[3][2] * v.z + data[3][3] * v.w; |
|
88 return u; |
|
89 } |
|
90 |
|
91 friend Matrix operator*(const Vector3 &v, Matrix &m) { return m * v; }; |
|
92 }; |
|
93 |
|
94 #endif |
|