-
Notifications
You must be signed in to change notification settings - Fork 3
/
GzMatrix.cpp
112 lines (111 loc) · 2.82 KB
/
GzMatrix.cpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// GzMatrix.cpp
//
//
// Created by Peter Piao on 11/17/16.
//
//
#include "GzMatrix.h"
#include <cmath>
GzMatrix::GzMatrix()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (i == j) M[i][j] = 1.0f;
else M[i][j] = 0.0f;
}
}
}
GzMatrix::GzMatrix(const GzVector3 &v, int type) : GzMatrix()
{
if (type == TRANSLATION)
{
M[0][3] = v.x;
M[1][3] = v.y;
M[2][3] = v.z;
}
else if (type == SCALING)
{
M[0][0] = v.x;
M[1][1] = v.y;
M[2][2] = v.z;
}
}
GzMatrix::GzMatrix(float angle, int axis) : GzMatrix()
{
float rad = angle * PI / 180;
if (axis == X)
{
M[1][1] = std::cos(rad);
M[2][2] = std::cos(rad);
M[2][1] = -std::sin(rad);
M[1][2] = std::sin(rad);
}
if (axis == Y)
{
M[0][0] = std::cos(rad);
M[2][2] = std::cos(rad);
M[2][0] = std::sin(rad);
M[0][2] = -std::sin(rad);
}
if (axis == Z)
{
M[0][0] = std::cos(rad);
M[1][1] = std::cos(rad);
M[1][0] = -std::sin(rad);
M[0][1] = std::sin(rad);
}
}
GzMatrix GzMatrix::inverseTranspose() const
{
GzMatrix temp;
float detA;
detA = M[0][0]*M[1][1]*M[2][2] + M[1][0]*M[2][1]*M[0][2]
+ M[2][0]*M[0][1]*M[1][2] - M[0][0]*M[2][1]*M[1][2]
- M[2][0]*M[1][1]*M[0][2] - M[1][0]*M[0][1]*M[2][2];
if (detA == 0.0f) return temp;
temp.M[0][0] = M[1][1]*M[2][2] - M[1][2]*M[2][1];
temp.M[0][1] = M[1][2]*M[2][0] - M[1][0]*M[2][2];
temp.M[0][2] = M[1][0]*M[2][1] - M[1][1]*M[2][0];
temp.M[1][0] = M[0][2]*M[2][1] - M[0][1]*M[2][2];
temp.M[1][1] = M[0][0]*M[2][2] - M[0][2]*M[2][0];
temp.M[1][2] = M[0][1]*M[2][0] - M[0][0]*M[2][1];
temp.M[2][0] = M[0][1]*M[1][2] - M[0][2]*M[1][1];
temp.M[2][1] = M[0][2]*M[1][0] - M[0][0]*M[1][2];
temp.M[2][2] = M[0][0]*M[1][1] - M[0][1]*M[1][0];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
temp.M[i][j] = temp.M[i][j] / detA;
}
}
return temp;
}
GzVector3 operator*(const GzMatrix &m, const GzVector3 &v)
{
float a, b, c, d;
a = m.M[0][0]*v.x + m.M[0][1]*v.y + m.M[0][2]*v.z + m.M[0][3];
b = m.M[1][0]*v.x + m.M[1][1]*v.y + m.M[1][2]*v.z + m.M[1][3];
c = m.M[2][0]*v.x + m.M[2][1]*v.y + m.M[2][2]*v.z + m.M[2][3];
d = m.M[3][0]*v.x + m.M[3][1]*v.y + m.M[3][2]*v.z + m.M[3][3];
if (d == 0.0f) return GzVector3();
return GzVector3(a/d, b/d, c/d);
}
GzMatrix operator*(const GzMatrix &m1, const GzMatrix &m2)
{
GzMatrix result;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < 3; k++)
{
result.M[i][j] += m1.M[i][k] * m2.M[k][j];
}
}
}
return result;
}