This repository was archived by the owner on Aug 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVec3.hpp
executable file
·165 lines (150 loc) · 4.65 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
#pragma once
#include <cmath>
#include <tuple>
#include <algorithm>
class Vec3
{
private:
double _x, _y, _z;
public:
Vec3(double v = 0.) : _x(v), _y(v), _z(v) {}
Vec3(double x_, double y_, double z_) : _x(x_), _y(y_), _z(z_) {}
~Vec3() {}
double &x() { return _x; }
double &y() { return _y; }
double &z() { return _z; }
double &operator[](const size_t &index)
{
switch (index)
{
case 0:
return _x;
break;
case 1:
return _y;
break;
case 2:
return _z;
break;
default:
throw "Vec3 has only 3 dimentions";
break;
}
}
double x() const { return _x; }
double y() const { return _y; }
double z() const { return _z; }
double operator[](const size_t &index) const
{
switch (index)
{
case 0:
return _x;
break;
case 1:
return _y;
break;
case 2:
return _z;
break;
default:
throw "Vec3 has only 3 dimentions";
break;
}
}
Vec3 operator-() const { return Vec3(-_x, -_y, -_z); }
Vec3 operator+(const Vec3 &r) const { return Vec3(_x + r._x, _y + r._y, _z + r._z); }
Vec3 operator-(const Vec3 &r) const { return Vec3(_x - r._x, _y - r._y, _z - r._z); }
Vec3 operator*(const Vec3 &r) const { return Vec3(_x * r._x, _y * r._y, _z * r._z); }
Vec3 operator*(const double &r) const { return Vec3(_x * r, _y * r, _z * r); }
Vec3 operator/(const Vec3 &r) const { return Vec3(_x / r._x, _y / r._y, _z / r._z); }
Vec3 operator/(const double &r) const { return Vec3(_x / r, _y / r, _z / r); }
friend Vec3 operator*(const double &l, const Vec3 &r) { return (r * l); }
bool operator==(const Vec3 &r) const { return (_x == r._x && _y == r._y && _z == r._z); }
bool operator!=(const Vec3 &r) const { return !(*this == r); }
double dot(const Vec3 &r) const { return (_x * r._x + _y * r._y + _z * r._z); }
Vec3 cross(const Vec3 &r) const { return Vec3(_y * r._z - _z * r._y, _z * r._x - _x * r._z, _x * r._y - _y * r._x); }
static double dot(const Vec3 &l, const Vec3 &r) { return l.dot(r); }
static Vec3 cross(const Vec3 &l, const Vec3 &r) { return l.cross(r); }
double len() const { return sqrt(_x * _x + _y * _y + _z * _z); }
double squaredLen() const { return (_x * _x + _y * _y + _z * _z); }
void normalize()
{
double norm = len();
_x /= norm;
_y /= norm;
_z /= norm;
}
Vec3 normalized() const
{
double norm = len();
return Vec3(_x / norm, _y / norm, _z / norm);
}
Vec3 &operator+=(const Vec3 &r)
{
_x += r._x;
_y += r._y;
_z += r._z;
return *this;
}
Vec3 &operator-=(const Vec3 &r)
{
_x -= r._x;
_y -= r._y;
_z -= r._z;
return *this;
}
Vec3 &operator*=(const Vec3 &r)
{
_x *= r._x;
_y *= r._y;
_z *= r._z;
return *this;
}
Vec3 &operator*=(const double &r)
{
_x *= r;
_y *= r;
_z *= r;
return *this;
}
Vec3 &operator/=(const Vec3 &r)
{
_x /= r._x;
_y /= r._y;
_z /= r._z;
return *this;
}
Vec3 &operator/=(const double &r)
{
_x /= r;
_y /= r;
_z /= r;
return *this;
}
Vec3 reflect(const Vec3 &normal) const { return ((*this) - normal * 2. * normal.dot(*this)); }
Vec3 refract(const Vec3 &normal, double ni, double nr) const
{
double cosi = this->normalized().dot(normal);
double nir = ni / nr;
double cosr2 = 1. - nir * nir * (1 - cosi * cosi);
if (cosr2 <= 0)
return Vec3();
double cosr = sqrt(cosr2) * (cosi > 0 ? -1 : 1);
return ((*this) * nir - normal * (nir * cosi + cosr)).normalized();
}
static Vec3 lerp(const Vec3 &v0, const Vec3 &v1, double t)
{
return ((v1 - v0) * t + v0);
}
static Vec3 mergeMin(const Vec3 &v0, const Vec3 &v1)
{
return Vec3(std::min(v0._x, v1._x), std::min(v0._y, v1._y), std::min(v0._z, v1._z));
}
static Vec3 mergeMax(const Vec3 &v0, const Vec3 &v1)
{
return Vec3(std::max(v0._x, v1._x), std::max(v0._y, v1._y), std::max(v0._z, v1._z));
}
double L_inf() const { return (_x > _y && _x > _z ? _x : (_y > _z ? _y : _z)); }
};
using AABBcord = std::tuple<Vec3, Vec3, Vec3>;