-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSampler.py
More file actions
88 lines (75 loc) · 3.5 KB
/
Sampler.py
File metadata and controls
88 lines (75 loc) · 3.5 KB
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
import random
import math
from Utility import Point
class Sampler:
def __init__(self, num_samples, num_sets):
self.num_samples = num_samples
self.num_sets = num_sets
self.count = 0
self.jump = 0
self.samples = []
self.hemisphere_samples = []
self.generate_samples()
def setup_shuffled_indices(self):
pass
def map_samples_to_hemisphere(self, exp):
PI = math.pi
for sample in self.samples:
cos_phi = math.cos(2.0 * PI * sample.x)
sin_phi = math.sin(2.0 * PI * sample.x)
cos_theta = pow((1.0 - sample.y), 1.0 / (exp + 1.0))
sin_theta = math.sqrt(1.0 - cos_theta * cos_theta)
pu = sin_theta * cos_phi
pv = sin_theta * sin_phi
pw = cos_theta
self.hemisphere_samples.append(Point(pu, pv, pw))
def sample_unit_square(self):
if self.count % self.num_samples == 0:
self.jump = (random.randint(1, self.num_sets) % self.num_sets) * self.num_samples
index = self.jump + self.count % self.num_samples
sample = self.samples[index]
self.count += 1
return sample
def sample_hemisphere(self):
if self.count % self.num_samples == 0:
self.jump = (random.randint(1, self.num_sets) % self.num_sets) * self.num_samples
index = self.jump + self.count % self.num_samples
sample = self.hemisphere_samples[index]
self.count += 1
return sample
class Regular(Sampler):
def generate_samples(self):
n = int(math.sqrt(self.num_samples))
for i in range(self.num_sets):
for j in range(n):
for k in range(n):
self.samples.append(Point((k + 0.5) / n, (j + 0.5) / n, 0.0))
class MultiJittered(Sampler):
def generate_samples(self):
n = int(math.sqrt(self.num_samples))
subcell_width = 1.0 / self.num_samples
# fill the samples array
for k in range(self.num_samples * self.num_sets):
self.samples.append(Point(0.0, 0.0, 0.0))
# initial patterns
for p in range(self.num_sets):
for i in range(n):
for j in range(n):
self.samples[i * n + j + p * self.num_samples].x = (i * n + j) * subcell_width + random.uniform(0, subcell_width)
self.samples[i * n + j + p * self.num_samples].y = (j * n + i) * subcell_width + random.uniform(0, subcell_width)
# shuffle x coordinates
for p in range(self.num_sets):
for i in range(n):
for j in range(n):
k = random.randint(j, n - 1)
t = self.samples[i * n + j + p * self.num_samples].x
self.samples[i * n + j + p * self.num_samples].x = self.samples[i * n + k + p * self.num_samples].x
self.samples[i * n + k + p * self.num_samples].x = t
# shuffle y coordinates
for p in range(self.num_sets):
for i in range(n):
for j in range(n):
k = random.randint(j, n - 1)
t = self.samples[i * n + j + p * self.num_samples].y
self.samples[i * n + j + p * self.num_samples].y = self.samples[i * n + k + p * self.num_samples].y
self.samples[i * n + k + p * self.num_samples].y = t