1 |
root |
1.1 |
/* |
2 |
|
|
* matrix calculations for non-homogenous space.. |
3 |
|
|
*/ |
4 |
|
|
#ifndef MATRIX_H |
5 |
|
|
#define MATRIX_H |
6 |
|
|
|
7 |
|
|
#include <iostream> |
8 |
|
|
|
9 |
|
|
#include "util.h" |
10 |
|
|
|
11 |
|
|
using namespace std; |
12 |
|
|
|
13 |
|
|
class vec { |
14 |
|
|
double data[3]; |
15 |
|
|
public: |
16 |
|
|
vec() t_no {}; |
17 |
|
|
vec(double a, double b, double c) t_no |
18 |
|
|
{ data[0] = a; data[1] = b; data[2] = c; }; |
19 |
|
|
|
20 |
|
|
double &operator [](int idx) t_no { return data[idx]; }; |
21 |
|
|
const double &operator [](int idx) const t_no { return data[idx]; }; |
22 |
|
|
}; |
23 |
|
|
|
24 |
|
|
inline bool operator ==(const vec &a, const vec &b) t_no |
25 |
|
|
{ return a[0] == b[0] && a[1] == b[1] && a[2] == b[2]; } |
26 |
|
|
|
27 |
|
|
inline vec operator +(const vec &a, const vec &b) t_no |
28 |
|
|
{ return vec(a[0] + b[0], a[1] + b[1], a[2] + b[2]); } |
29 |
|
|
|
30 |
|
|
inline vec operator -(const vec &a, const vec &b) t_no |
31 |
|
|
{ return vec(a[0] - b[0], a[1] - b[1], a[2] - b[2]); } |
32 |
|
|
|
33 |
|
|
inline double operator *(const vec &a, const vec &b) t_no |
34 |
|
|
{ return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; } |
35 |
|
|
|
36 |
|
|
inline vec operator *(double s, const vec &a) t_no |
37 |
|
|
{ return vec(s * a[0], s * a[1], s * a[2]); } |
38 |
|
|
|
39 |
|
|
double abs(const vec &a) t_no; |
40 |
|
|
vec normalize(const vec &a) t_no; |
41 |
|
|
vec cross(const vec &a, const vec &b) t_no; |
42 |
|
|
|
43 |
|
|
class mat { |
44 |
|
|
vec data[3]; |
45 |
|
|
public: |
46 |
|
|
mat() t_no {}; |
47 |
|
|
mat(vec a, vec b, vec c) t_no |
48 |
|
|
{ data[0] = a; data[1] = b; data[2] = c; }; |
49 |
|
|
|
50 |
|
|
vec &row(int y) t_no { return data[y]; }; |
51 |
|
|
const vec &row(int y) const t_no { return data[y]; }; |
52 |
|
|
vec &operator [](int y) t_no { return row(y); }; |
53 |
|
|
const vec &operator [](int y) const t_no { return row(y); }; |
54 |
|
|
|
55 |
|
|
vec col(int x) t_no { return vec(data[0][x], data[1][x], data[2][x]); }; |
56 |
|
|
const vec col(int x) const t_no { return vec(data[0][x], data[1][x], data[2][x]); }; |
57 |
|
|
vec operator ()(int x) t_no { return col(x); }; |
58 |
|
|
const vec operator ()(int x) const t_no { return col(x); }; |
59 |
|
|
|
60 |
|
|
}; |
61 |
|
|
|
62 |
|
|
mat operator +(const mat &a, const mat &b) t_no; |
63 |
|
|
mat operator -(const mat &a, const mat &b) t_no; |
64 |
|
|
vec operator *(const mat &a, const vec &b) t_no; |
65 |
|
|
vec operator *(const vec &a, const mat &b) t_no; |
66 |
|
|
mat operator *(const mat &a, const mat &b) t_no; |
67 |
|
|
|
68 |
|
|
ostream &operator <<(ostream &o, const vec &v) t_no; |
69 |
|
|
ostream &operator <<(ostream &o, const mat &m) t_no; |
70 |
|
|
|
71 |
|
|
#endif |