-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec3.cu
155 lines (128 loc) · 4.42 KB
/
vec3.cu
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
#include "cuda_path_tracer/vec3.cuh"
#include <cmath>
constexpr auto epsilon = 1e-6F;
__host__ __device__ auto Vec3::operator-() const -> Vec3 {
return {-x, -y, -z};
}
__host__ __device__ auto Vec3::operator==(const Vec3 &other) const -> bool {
return x == other.x && y == other.y && z == other.z;
}
__host__ __device__ auto Vec3::operator+=(const Vec3 &other) -> Vec3 & {
x += other.x;
y += other.y;
z += other.z;
return *this;
}
__host__ __device__ auto Vec3::operator*=(const Vec3 &other) -> Vec3 & {
x *= other.x;
y *= other.y;
z *= other.z;
return *this;
}
__host__ __device__ auto Vec3::operator/=(const float t) -> Vec3 & {
const auto k = 1.0F / t;
x *= k;
y *= k;
z *= k;
return *this;
}
__device__ Vec3::operator float4() const { return make_float4(x, y, z, 1.0F); }
__host__ __device__ auto Vec3::getLengthSquared() const -> float {
return x * x + y * y + z * z;
}
__host__ __device__ auto Vec3::getLength() const -> float {
return sqrtf(getLengthSquared());
}
__host__ __device__ auto Vec3::nearZero() const -> bool {
return std::fabs(x) < epsilon && std::fabs(y) < epsilon &&
std::fabs(z) < epsilon;
}
__host__ auto operator<<(std::ostream &os, const Vec3 &v) -> std::ostream & {
os << "(" << v.x << ", " << v.y << ", " << v.z << ")";
return os;
}
__host__ __device__ auto operator+(const Vec3 &v1, const Vec3 &v2) -> Vec3 {
return {v1.x + v2.x, v1.y + v2.y, v1.z + v2.z};
}
__host__ __device__ auto operator-(const Vec3 &v1, const Vec3 &v2) -> Vec3 {
return {v1.x - v2.x, v1.y - v2.y, v1.z - v2.z};
}
__host__ __device__ auto operator*(const Vec3 &v, const float t) -> Vec3 {
return {t * v.x, t * v.y, t * v.z};
}
__host__ __device__ auto operator*(const Vec3 &v1, const Vec3 &v2) -> Vec3 {
return {v1.x * v2.x, v1.y * v2.y, v1.z * v2.z};
}
__host__ __device__ auto operator/(const Vec3 &v, float t) -> Vec3 {
return {v.x / t, v.y / t, v.z / t};
}
__host__ __device__ auto makeUnitVector(const Vec3 &v) -> Vec3 {
return v / v.getLength();
}
__host__ __device__ auto dot(const Vec3 &v1, const Vec3 &v2) -> float {
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
__host__ __device__ auto cross(const Vec3 &v1, const Vec3 &v2) -> Vec3 {
return {v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z,
v1.x * v2.y - v1.y * v2.x};
}
__device__ auto
randomInUnitDiskRejectionSampling(curandState_t &state) -> Vec3 {
while (true) {
const auto p = Vec3{2.0F * curand_uniform(&state) - 1.0F,
2.0F * curand_uniform(&state) - 1.0F, 0};
const auto sqrd = p.getLengthSquared();
if (epsilon < sqrd && sqrd <= 1.0F) {
return p;
}
}
}
__device__ auto
randomInUnitDiskRejectionSampling(curandStatePhilox4_32_10_t &state) -> Vec3 {
while (true) {
const auto values = curand_uniform4(&state);
const auto p = Vec3{2.0F * values.w - 1.0F, 2.0F * values.x - 1.0F, 0};
const auto q = Vec3{2.0F * values.y - 1.0F, 2.0F * values.z - 1.0F, 0};
const auto p_sqrd = p.getLengthSquared();
const auto q_sqrd = q.getLengthSquared();
if (epsilon < p_sqrd && p_sqrd <= 1.0F) {
return p;
}
if (epsilon < q_sqrd && q_sqrd <= 1.0F) {
return q;
}
}
}
__device__ auto randomInUnitDisk(curandStatePhilox4_32_10_t &state)
-> cuda::std::tuple<Vec3, Vec3, Vec3, Vec3> {
const float4 radius = curand_uniform4(&state);
const float4 angle = curand_uniform4(&state) * 2.0F * M_PIf32;
return cuda::std::tuple{
Vec3{sqrtf(radius.x) * cosf(angle.x), sqrtf(radius.x) * sinf(angle.x), 0},
Vec3{sqrtf(radius.y) * cosf(angle.y), sqrtf(radius.y) * sinf(angle.y), 0},
Vec3{sqrtf(radius.z) * cosf(angle.z), sqrtf(radius.z) * sinf(angle.z), 0},
Vec3{sqrtf(radius.w) * cosf(angle.w), sqrtf(radius.w) * sinf(angle.w), 0},
};
}
__device__ auto
randomInUnitSphereRejectionSampling(curandStatePhilox4_32_10_t &state) -> Vec3 {
while (true) {
const auto values = curand_uniform4(&state);
const auto p = Vec3{2.0F * values.w - 1.0F, 2.0F * values.x - 1.0F,
2.0F * values.y - 1.0F};
if (p.getLengthSquared() <= 1.0F) {
return p;
}
}
}
__device__ auto
randomInUnitSphereRejectionSampling(curandState_t &state) -> Vec3 {
while (true) {
const auto p = Vec3{2.0F * curand_uniform(&state) - 1.0F,
2.0F * curand_uniform(&state) - 1.0F,
2.0F * curand_uniform(&state) - 1.0F};
if (p.getLengthSquared() <= 1.0F) {
return p;
}
}
}