-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeom.cpp
More file actions
executable file
·78 lines (66 loc) · 1.52 KB
/
geom.cpp
File metadata and controls
executable file
·78 lines (66 loc) · 1.52 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
#include <vector>
#include <cassert>
#include <cmath>
#include <iostream>
#include "geom.h"
template <> v3d<float>::v3d(mtrx m) : x(m[0][0]/m[3][0]), y(m[1][0]/m[3][0]), z(m[2][0]/m[3][0]) {}
template <> template <> v3d<int>::v3d<>(const v3d<float> &v): x(int(v.x+.5)), y(int(v.y+.5)), z(int(v.z+.5)) {}
template <> template <> v3d<float>::v3d<>(const v3d<int> &v): x(v.x), y(v.y), z(v.z) {}
mtrx::mtrx(int r, int c): m(std::vector<std::vector<float>> (r, std::vector<float>(c, 0.f))), rows(r), cols(c) {}
mtrx::mtrx(v3df v) : m(std::vector<std::vector<float> >(4, std::vector<float>(1, 1.f))), rows(4), cols(1) {
m[0][0] = v.x;
m[1][0] = v.y;
m[2][0] = v.z;
}
int mtrx::nrows()
{
return rows;
}
int mtrx::ncols()
{
return cols;
}
mtrx mtrx::ident(int dims)
{
mtrx e(dims, dims);
for (int i=0; i<dims; i++)
for (int j=0; j<dims; j++)
e[i][j] = (i==j ? 1.f : 0.f);
return e;
}
std::vector<float>& mtrx::operator[](const int i)
{
assert(i>=0 && i<rows);
return m[i];
}
mtrx mtrx::operator*(const mtrx& a)
{
assert(cols == a.rows);
mtrx 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;
}
std::ostream& operator<< (std::ostream &s, mtrx &m)
{
for (int i=0; i<m.rows; i++)
{
for (int j=0; j<m.cols; j++)
{
s << m[i][j];
if (j<m.ncols()-1)
s << "\t";
}
s << "\n";
}
return s;
}