-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.h
71 lines (62 loc) · 2.47 KB
/
random.h
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
#ifndef RANDOM_H
#define RANDOM_H
#include <random>
inline double random_double() {
static std::uniform_real_distribution<double> distribution(0.0, 1.0);
static std::mt19937 generator;
return distribution(generator);
}
inline double random_double(double min, double max) {
return min + ((max - min) * random_double());
}
inline double random_int(int min, int max) {
return static_cast<int>(random_double(min, max + 1));
}
// a unit sphere is a sphere with radius 1, so basically this creates a random
// unit vector where x, y and z could range between -1 and 1
//
// from what I understand, for the purpose of generating vectors relative to a normal,
// the random values produced by this follow a distribution
// of (cos(a))^3 where a is the angle between the randomly generated vector and the normal
// in other words, this way of generating a random vector is biased towards
// vectors that are closer to the normal.
inline Vec3 random_unit_vec3_in_unit_sphere() {
while (true) {
// the individual dimensions of a unit vector should be in the range [-1, 1]
// though note in this case due to how the random generation works its
// actually [-1, 1)
auto p = Vec3(random_double(-1, 1), random_double(-1, 1), random_double(-1, 1));
if (p.length_squared() >= 1) {
continue;
}
return p;
}
}
// when used for generating vectors relative to a normal, this apparently
// generates unit vectors that follow true Lambertian distribution
// which improves the likelihood of having vectors that have a larger angle away
// from said normal
inline Vec3 random_unit_vec3() {
return random_unit_vec3_in_unit_sphere().unit();
}
// generates a random vector that is in the same direction as the normal
// it does this by generating a random vector in a sphere, and if what we generated
// is in the opposite direction (i.e wrong "hemisphere") then we inverse it and use
// it (rather than throw it away and try again)
inline Vec3 random_in_hemisphere(Vec3 const & normal) {
Vec3 in_unit_sphere = random_unit_vec3_in_unit_sphere();
if (in_unit_sphere.dot(normal) > 0.0) {
return in_unit_sphere;
} else {
return -in_unit_sphere;
}
}
// generates a random point that falls within a disk of radius 1
Point3 random_point_in_unit_disk() {
while (true) {
auto p = Point3(random_double(-1, 1), random_double(-1, 1), 0);
if (p.length_squared() >= 1.0) continue;
return p;
}
}
#endif