-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.cpp
More file actions
95 lines (80 loc) · 2.58 KB
/
Matrix.cpp
File metadata and controls
95 lines (80 loc) · 2.58 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "Matrix.hpp"
Matrix::Matrix( void )
{
double tab[4][4];
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
tab[i][j] = 0;
this->_initialize(tab);
return ;
}
Matrix::Matrix( double values[4][4] )
{
this->_initialize(values);
return ;
}
Matrix::Matrix(Matrix const & src)
{
*this = src;
return ;
}
Matrix::~Matrix( void )
{
return ;
}
Matrix Matrix::operator*(Matrix const & rhs) const
{
double tab[4][4];
double tmp;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
{
tmp = 0;
for (int k = 0; k < 4; k++)
tmp += this->_values[i][k] * rhs._values[k][j];
tab[i][j] = tmp;
}
return Matrix(tab);
}
Matrix & Matrix::operator=(Matrix const & rhs)
{
this->_initialize(rhs._values);
return *this;
}
Matrix Matrix::inverse() const
{
double tab[4][4] =
{
{ this->_values[0][0], this->_values[1][0], this->_values[2][0], this->_values[3][0] },
{ this->_values[0][1], this->_values[1][1], this->_values[2][1], this->_values[3][1] },
{ this->_values[0][2], this->_values[1][2], this->_values[2][2], this->_values[3][2] },
{ this->_values[0][3], this->_values[1][3], this->_values[2][3], this->_values[3][3] },
};
return Matrix(tab);
}
void Matrix::_initialize(double const values[4][4])
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
this->_values[i][j] = values[i][j];
}
Vertex Matrix::operator*(Vertex const & vertex) const
{
double x = vertex.getX() * this->_values[0][0] + vertex.getY() * this->_values[0][1] + vertex.getZ() * this->_values[0][2];
double y = vertex.getX() * this->_values[1][0] + vertex.getY() * this->_values[1][1] + vertex.getZ() * this->_values[1][2];
double z = vertex.getX() * this->_values[2][0] + vertex.getY() * this->_values[2][1] + vertex.getZ() * this->_values[2][2];
return Vertex(x, y, z);
}
Triangle Matrix::transformTriangle(Triangle const & triangle) const
{
return Triangle(*this * triangle.getA(), *this * triangle.getB(), *this * triangle.getC());
}
std::string Matrix::toString() const
{
std::stringstream sstr;
sstr << "[Matrix :\t{" << this->_values[0][0] << ", " << this->_values[0][1] << ", " << this->_values[0][2] << " ,"<< this->_values[0][3] << "}] \n"
<< "\t\t{" << this->_values[1][0] << ", " << this->_values[1][1] << ", " << this->_values[1][2] << " ,"<<this->_values[1][2] <<"}] \n"
<< "\t\t{" << this->_values[2][0] << ", " << this->_values[2][1] << ", " << this->_values[2][2] << " ," << this->_values[2][3] << "}] \n"
<< "\t\t{" << this->_values[3][0] << ", " << this->_values[3][1] << ", " << this->_values[3][2] << " ," << this->_values[3][3] << "}] \n";
return sstr.str();
}