-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtweekend.h
92 lines (70 loc) · 2.05 KB
/
rtweekend.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef RTWEEKEND_H
#define RTWEEKEND_H
#include <cmath>
#include <limits>
#include <memory>
#include <random>
#include <thread>
#include <vector>
#include <future>
#include <cstring>
// Usings
using std::shared_ptr;
using std::make_shared;
using std::sqrt;
using std::vector;
using std::future;
// Constants
constexpr double infinity = std::numeric_limits<double>::infinity();
constexpr double pi = 3.1415926535897932385;
constexpr double epsilon = 1e-6;
// Utility functions
inline double degrees_to_radians(double degrees) {
return degrees * pi / 180.0;
}
static std::hash<std::thread::id> hasher;
static std::uniform_real_distribution<double> distribution(0.0, 1.0);
static std::exponential_distribution<double> exp_distribution(1);
inline double random_double() {
static thread_local std::mt19937 generator(
static_cast<unsigned>(hasher(std::this_thread::get_id()))
);
return distribution(generator);
}
inline double random_double_exp() {
static thread_local std::mt19937 generator(
static_cast<unsigned>(hasher(std::this_thread::get_id()) * 10)
);
return exp_distribution(generator);
}
inline double random_double(double min, double max) {
// Returns a random real in [min,max).
return min + (max-min)*random_double();
}
inline int random_int(int min, int max) {
// Returns a random integer in [min, max].
return static_cast<int>(random_double(min, max+1));
}
template<typename T>
vector<T> wait_for_all(vector<future<T>>& vf) {
vector<T> res;
for(auto& fu : vf)
res.push_back(fu.get());
return res;
}
inline int ends_with(const char *str, const char *suffix) {
size_t str_len = strlen(str);
size_t suffix_len = strlen(suffix);
return (str_len >= suffix_len) &&
(!memcmp(str + str_len - suffix_len, suffix, suffix_len));
}
// Common Headeres
#include "interval.h"
#include "ray.h"
#include "vec4.h"
#include "vec3.h"
vec4f::vec4f(const vec3f& other, float a) : e{other.x(), other.y(), other.z(), a} {}
vec3f::vec3f(const vec4f& other) : e{other.x(), other.y(), other.z()} {}
#include "vec2.h"
#include "mat4.h"
#endif