-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixVector.h
More file actions
122 lines (96 loc) · 3.21 KB
/
MatrixVector.h
File metadata and controls
122 lines (96 loc) · 3.21 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
#ifndef MATRIX_H_
#define MATRIX_H_
#include <math.h>
class Vec3
{
public: float x,y,z;
public: Vec3(){}
Vec3(float xx, float yy, float zz): x(xx) ,y(yy), z(zz) {}
Vec3 operator + (const Vec3& vec) const {return Vec3(x+vec.x,y+vec.y,z+vec.z); }
Vec3 operator - (const Vec3& vec) const {return Vec3(x-vec.x,y-vec.y,z-vec.z); }
Vec3 operator / (const float& d) const {return Vec3(x/d,y/d,z/d);}
Vec3 operator * (const float& d) const {return Vec3(x*d,y*d,z*d);}
Vec3 crossProduct (const Vec3& v) const {return Vec3(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);}
float dotProduct (const Vec3& v) const {return x * v.x + y * v.y + z * v.z;}
float norm() const {return x*x + y*y + z*z ;}
float magnitude() const {return sqrt(norm());}
float& operator [] (int i){return (&x)[i];}
};
class Vec2
{
public: float x, y,z ; //z for depth
Vec3 c; //c for color
float i; // i for intensity
public: Vec2(){i = 1;}
Vec2(float xx, float yy,float zz=0,float ii=1): x(xx) ,y(yy),z(zz),i(ii){}
Vec2(const Vec2& in):x(in.x),y(in.y),z(in.z),i(in.i){}
void operator = (const Vec2& in){
x = in.x;
y = in.y;
z = in.z;
i = in.i;
}
};
class Matrix
{
private:
float* data;
int row, col;
public:
Matrix(int rows,int column) : row(rows), col(column)
{
data = new float[row*col];
}
Matrix(const Matrix& mat)
{
row = mat.row;
col = mat.col;
data = new float[row*col];
for (int i =0;i<row*col;i++)
(*this)(i) = mat(i);
}
void operator=(const Matrix& mat)
{
delete[] data;
row = mat.row;
col = mat.col;
data = new float[row*col];
for (int i =0;i<row*col;i++)
(*this)(i) = mat(i);
}
float& operator() (int r, int c); //mat(i,j)
float& operator() (int pos) ; //mat(pos)
const float operator() (int r, int c) const; //mat(i,j)
const float operator() (int pos) const; //mat(pos)
Matrix operator * (Matrix& mat); // M = T * M
Matrix operator + (Matrix& mat); // C = A + B
Matrix operator - (Matrix& mat); // C = A - B
Matrix operator / (float val); // C[] = C[] / val
float dot(Matrix& mat); // A.dot(B) == (A = A DOT B)
float magnitude(); //returns the square root of sum of squares of all element
~Matrix(){
delete[] data;
}
};
//
//int main()
//{
// Matrix m(3,3);
// Matrix n(3,3);
//
// m(0) = 1; m(1) = 2; m(2) = 3;
// m(3) = 4; m(4) = 5; m(5) = 6;
// m(6) = 2; m(7) = 4; m(8) = 5;
//
// n(0) = 1; n(1) = 0; n(2) = 0;
// n(3) = 0; n(4) = 1; n(5) = 0;
// n(6) = 0; n(7) = 0; n(8) = 1;
//
// n = m * n;
//
// for (int i =0; i< 3; i++,std::cout << std::endl)
// for (int j =0; j< 3; j++)
// std::cout << n(i,j) << " ";
//
//}
#endif // MATRIX_H_