-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeometry.cpp
More file actions
133 lines (110 loc) · 3.26 KB
/
geometry.cpp
File metadata and controls
133 lines (110 loc) · 3.26 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <vector>
#include <cassert>
#include "geometry.h"
template <> template<>
Vec2<int>::Vec2(const Vec2<float>& v): x(int(v.x + 0.5)), y(int(v.y + 0.5)) {}
template<>template<>
Vec2<float>::Vec2(const Vec2<int>& v):x(v.x), y(v.y){}
template <> template<>
Vec3<int>::Vec3(const Vec3<float>& v): x(int(v.x + 0.5)), y(int(v.y + 0.5)), z(int(v.z + 0.5)) {}
template<>template<>
Vec3<float>::Vec3(const Vec3<int>& v):x(v.x), y(v.y), z(v.z) {}
Matrix::Matrix(int r, int c): rows(r), cols(c), m(std::vector<std::vector<float>>(r, std::vector<float>(c, 0.f))){}
std::vector<float>& Matrix::operator[](const int i){
assert(i >= 0 && i < rows);
return m[i];
}
Matrix Matrix::identity(int dimensions){
Matrix m (dimensions, dimensions);
for (int i =0; i < dimensions; i++) {
for (int j = 0; j < dimensions; j++) {
m[i][j] = i==j ? 1.f : 0.f;
}
}
return m;
}
Matrix Matrix::operator* (const Matrix& a){
assert(cols == a.rows);
Matrix res(rows, a.cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < a.cols; j++) {
res.m[i][j] = 0.f;
for (int k = 0; k < cols; k++) {
res.m[i][j] += m[i][k] * a.m[k][j];
}
}
}
return res;
}
Matrix Matrix::transpose(){
Matrix res(cols, rows);
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
res[i][j] = m[j][i];
}
}
return res;
}
Matrix Matrix::inverse(){
assert(rows == cols);
Matrix result(rows, cols * 2);
// get augmented matrix
// Following is to copy the orginal matrix
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = m[i][j];
}
}
// Following is to augment the identity matrix
for (int i = 0; i < rows; i++) {
result[i][cols + i] = 1;
}
// forward elimination
for (int i=0; i<rows-1; i++) {
// normalize the first row
for(int j=result.cols-1; j>=0; j--)
result[i][j] /= result[i][i];
for (int k=i+1; k<rows; k++) {
float coeff = result[k][i];
for (int j=0; j<result.cols; j++) {
result[k][j] -= result[i][j]*coeff;
}
}
}
//Normalize the last row
float factor = result[rows-1][cols-1];
for (int j = cols-1; j < result.cols; j++) {
result[rows-1][j] /= factor;
}
//Backward elimination
for (int i = rows-1; i>0; i--) {
for (int k = i - 1; k >= 0; k--) {
float coefficient = result[k][i];
for (int j = 0; j < result.cols; j++) {
result[k][j] -= coefficient * result[i][j];
}
}
}
//Truncate to get the inverse matrix
Matrix truncate(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
truncate[i][j] = result[i][j+cols];
}
}
return truncate;
}
Matrix Matrix::inverse_transpose(){
return inverse().transpose();
}
std::ostream& operator<<(std::ostream& s, Matrix& m){
for (int i = 0; i < m.rows; i++) {
for (int j = 0; j < m.cols; j++) {
s<<m[i][j];
if (j == m.cols-1)
s<<'\t';
}
s<<'\n';
}
return s;
}