-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.cpp
79 lines (69 loc) · 1.65 KB
/
math.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
/*
this file contains all math helpfer functions
including:
dot,cross product,
normalization vector,
vector minus,
vector add,
calculate refelction vector,
get position of a point given parameter t
*/
namespace Yuzhou_Math {
//self-defined vector container
struct Vec3 {
double x, y, z;
Vec3() {x=y=z=0;}
Vec3(double x, double y, double z) : x(x), y(y), z(z) {}
};
struct Ray {
Vec3 o; //origin of ray
Vec3 d; //direction of ray
};
/*math helper functions*/
//dot product
double dot(Vec3 i, Vec3 j) {
return i.x*j.x + i.y*j.y + i.z*j.z;
}
//cross product
Vec3 cross(Vec3 a, Vec3 b) {
double i = a.y*b.z - b.y*a.z;
double j = b.x*a.z - a.x*b.z;
double k = a.x*b.y - b.x*a.y;
Vec3 v(i, j, k);
return v;
}
//turn a vector into unit vector
Vec3 normalize(Vec3 v) {
double magnitude = sqrt(pow(v.x, 2) + pow(v.y,2) + pow(v.z,2));
Vec3 vec(v.x / magnitude, v.y / magnitude, v.z / magnitude);
return vec;
}
//Vminus operator
Vec3 Vminus(Vec3 a, Vec3 b) {
Vec3 v(a.x - b.x, a.y - b.y, a.z - b.z);
return v;
}
//add operator
Vec3 add(Vec3 a, Vec3 b) {
Vec3 v(a.x + b.x, a.y + b.y, a.z + b.z);
return v;
}
//return distance between two vectors
double distance(Vec3 a, Vec3 b) {
return sqrt(pow(a.x-b.x, 2) + pow(a.y-b.y,2) + pow(a.z-b.z,2));
}
//get position vector of ray given parameter t
Vec3 getPos(Ray r, double t) {
Vec3 v(r.o.x + t*r.d.x, r.o.y + t*r.d.y, r.o.z + t*r.d.z); //postion vector
return v;
}
//given incoming and normal vector, get reflection vector
Vec3 getReflection(Vec3 i, Vec3 n) {
double dotScalar = dot(i,n);
Vec3 r;
r.x = 2 * dotScalar * n.x - i.x;
r.y = 2 * dotScalar * n.y - i.y;
r.z = 2 * dotScalar * n.z - i.z;
return r;
}
}