-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector3.hpp
96 lines (72 loc) · 2.45 KB
/
vector3.hpp
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
#ifndef VECTOR3_HPP
#define VECTOR3_HPP
#include <iostream>
#include <cmath>
namespace strangeloop {
class Vector3
{
public:
Vector3();
Vector3(double x, double y, double z);
Vector3(Vector3 &vec);
Vector3(const Vector3 &vec);
double operator[](int i) const;
double& operator[](int i);
//void operator=(const Vector3& vec);
Vector3& operator+=(const Vector3& vec);
Vector3& operator*=(const Vector3& vec);
Vector3& operator-=(const Vector3& vec);
Vector3& operator/=(const double t);
static Vector3 random();
double length() const;
double length_squared() const;
bool nearZero() const;
private:
double _buffer[3];
};
inline std::ostream& operator<<(std::ostream& out, const Vector3& vec) {
return out << vec[0] << " " << vec[1] << " " << vec[2] << std::endl;
}
inline Vector3 operator+(const Vector3& vec1, const Vector3& vec2) {
return Vector3(vec1[0] + vec2[0], vec1[1] + vec2[1], vec1[2] + vec2[2]);
}
inline Vector3 operator-(const Vector3& vec1,const Vector3& vec2) {
return Vector3(vec1[0] - vec2[0], vec1[1] - vec2[1], vec1[2] - vec2[2]);
}
inline Vector3 operator*(const Vector3& vec1, const Vector3& vec2) {
return Vector3(vec1[0] * vec2[0], vec1[1] * vec2[1], vec1[2] * vec2[2]);
}
inline Vector3 operator*(const Vector3& vec, double t) {
return Vector3(vec[0] * t, vec[1] * t, vec[2] * t);
}
inline Vector3 operator*(double t, const Vector3& vec) {
return vec * t;
}
inline Vector3 operator/(const Vector3& vec, double t) {
return vec * (1/t);
}
inline double dot(const Vector3& vec1, const Vector3& vec2) {
return (vec1[0] * vec2[0] + vec1[1] * vec2[1] + vec1[2] * vec2[2]);
}
inline Vector3 cross(Vector3& vec1, Vector3& vec2) {
return Vector3(vec1[1]*vec2[2] - vec1[2]*vec2[1],
vec1[2]*vec2[0] - vec1[0]*vec2[2],
vec1[0]*vec2[1] - vec1[1]*vec2[0]);
}
inline Vector3 unit_vector(const Vector3& vec) {
return vec / vec.length();
}
inline Vector3 floor(Vector3& vec) {
return Vector3(std::floor(vec[0]),std::floor(vec[1]),std::floor(vec[2]));
}
inline Vector3 reflect(const Vector3& v, const Vector3& n) {
return v - 2*dot(v,n)*n;
}
inline Vector3 refract(const Vector3& uv, const Vector3& n, double etaiOverEta) {
auto cosTheta = fmin(dot(-1 * uv,n), 1.0);
Vector3 rPerp = etaiOverEta * (uv + cosTheta * n);
Vector3 rPara = -sqrt(fabs(1.0 - rPerp.length_squared())) * n;
return rPerp + rPara;
}
}
#endif // VECTOR3_HPP