-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVec3.hpp
196 lines (153 loc) · 3.79 KB
/
Vec3.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#ifndef VEC3_HPP
#define VEC3_HPP
#include <cmath>
#include <iostream>
#include "Utils.hpp"
class Vec3
{
public:
Vec3() :x(0), y(0), z(0){}
Vec3(float x, float y, float z)
:x(x), y(y), z(z)
{}
Vec3(float* coords)
:x(coords[0]), y(coords[1]), z(coords[2])
{}
Vec3 operator+(const Vec3& v) const {
return Vec3(x+v.x, y+v.y, z+v.z);
}
Vec3 operator+(float t) const {
return Vec3(x+t, y+t, z+t);
}
Vec3 operator-(const Vec3& v) const {
return Vec3(x-v.x, y-v.y, z-v.z);
}
Vec3 operator-(float t) const {
return Vec3(x-t, y-t, z-t);
}
Vec3 operator*(const Vec3& v) const {
return Vec3(x*v.x, y*v.y, z*v.z);
}
Vec3 operator*(float t) const {
return Vec3(x*t, y*t, z*t);
}
Vec3 operator/(float t) const {
return Vec3(x/t, y/t, z/t);
}
Vec3 operator-() const { return Vec3(-x, -y, -z); }
Vec3& operator+=(const Vec3& v) {
x += v.x;
y += v.y;
z += v.z;
return *this;
}
Vec3& operator-=(const Vec3& v) {
x -= v.x;
y -= v.y;
z -= v.z;
return *this;
}
Vec3& operator*=(float t) {
x *= t;
y *= t;
z *= t;
return *this;
}
Vec3& operator/=(float t){
return *this *= 1/t;
}
Vec3& negate()
{
x = -x;
y = -y;
z = -z;
return *this;
}
//make a unit vector out of this
void normalize()
{
(*this)*=(1/length());
}
float dot(const Vec3& u) const
{
return x*u.x+
y*u.y+
z*u.z;
}
Vec3 cross(const Vec3& u) const
{
return Vec3(y*u.z - z*u.y,
z*u.x - x*u.z,
x*u.y - y*u.x
);
}
Vec3 reflect(const Vec3& normal) const{
return *this - normal * dot(normal) * 2;
}
Vec3 refract(const Vec3& normal, float etaRatio) const {
auto cosTheta = fmin((-*this).dot(normal), 1.0);
Vec3 rOutPerp = ((*this)+normal*cosTheta) * etaRatio;
Vec3 rOutPar = normal * -std::sqrt(fabs(1-rOutPerp.lengthSquared()));
return rOutPerp + rOutPar;
}
float length() const {
return std::sqrt(lengthSquared());
}
float lengthSquared() const {
return x*x + y*y+ z*z;
}
bool closeToZero(){
const auto eps = 1e-8;
return (fabs(x)<eps) && (fabs(y)<eps) && (fabs(z)<eps);
}
inline static Vec3 random(){
return Vec3(randomFloat(), randomFloat(), randomFloat());
}
inline static Vec3 random(float min, float max){
return Vec3(randomFloat(min,max), randomFloat(min, max), randomFloat(min, max));
}
public:
float x;
float y;
float z;
};
using Color = Vec3;
inline std::ostream& operator<<(std::ostream& out, const Vec3& v){
return out<<static_cast<int>(v.x)<<' '<<static_cast<int>(v.y)<<' '<<static_cast<int>(v.z);
}
inline float dot(const Vec3& v, const Vec3& u)
{
return v.x*u.x+
v.y*u.y+
v.z*u.z;
}
inline Vec3 cross(const Vec3& v, const Vec3& u)
{
return Vec3(v.y*u.z - v.z*u.y,
v.z*u.x - v.x*u.z,
v.x*u.y - v.y*u.x
);
}
//normalizes vector to 0..1 with same direction
inline Vec3 unitVector(const Vec3& v)
{
return v/v.length();
}
inline Vec3 randomInUnitSphere(){
while (1) {
auto p = Vec3::random(-1,1);
if(p.lengthSquared() >= 1) continue;
return p;
}
}
inline Vec3 randomUnit(){
return unitVector(randomInUnitSphere());
}
inline Vec3 randomInUnitDisk() {
while (true) {
auto p = Vec3(randomFloat(-1,1), randomFloat(-1,1), 0);
if (p.lengthSquared() >= 1) continue;
return p;
}
}
#endif // VEC3_HPP