-
Notifications
You must be signed in to change notification settings - Fork 1
/
sampling.go
76 lines (67 loc) · 1.62 KB
/
sampling.go
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
package raytracer
import (
"math/rand"
"sort"
)
var sampleCache [][]Vector
func createCache() {
sampleCache = make([][]Vector, 10)
// Create 10 different cache variations
for index := 0; index < 10; index++ {
sampleCache[index] = make([]Vector, 100000)
for i := 0; i < 100000; i++ {
x := rand.Float64() - 0.5
y := rand.Float64() - 0.5
z := rand.Float64() - 0.5
v := normalizeVector(Vector{x, y, z, 0})
sampleCache[index][i] = v
}
}
}
func createSamples(normal Vector, limit int, shifting float64) []Vector {
if sampleCache == nil {
createCache()
}
index := rand.Int() % 10
result := make([]Vector, 0, limit)
result = append(result, normal)
for i := range sampleCache[index] {
if sameSideTest(sampleCache[index][i], normal, shifting) {
result = append(result, sampleCache[index][i])
if len(result) == limit {
break
}
}
}
return result
}
func sampleSphere(radius float64, limit int) []Vector {
result := make([]Vector, limit)
for i := 0; i < limit; i++ {
result[i] = Vector{
(rand.Float64() - 0.5) * radius,
(rand.Float64() - 0.5) * radius,
(rand.Float64() - 0.5) * radius,
1,
}
}
return result
}
func sampleTriangle(triangle Triangle, count int) []Vector {
result := make([]Vector, count)
for i := 0; i < count; i++ {
vl := []float64{
rand.Float64(), rand.Float64(),
}
sort.Float64s(vl)
s := vl[0]
t := vl[1]
result[i] = Vector{
s*triangle.P1[0] + (t-s)*triangle.P2[0] + (1-t)*triangle.P3[0],
s*triangle.P1[1] + (t-s)*triangle.P2[1] + (1-t)*triangle.P3[1],
s*triangle.P1[2] + (t-s)*triangle.P2[2] + (1-t)*triangle.P3[2],
1,
}
}
return result
}