-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
155 lines (128 loc) · 2.76 KB
/
main.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
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
package main
import (
"math"
"sync"
"github.com/fmi/go-homework/geom"
)
type Triangle struct {
A, B, C geom.Vector
}
type Quad struct {
A, B, C, D geom.Vector
}
type Sphere struct {
Origin geom.Vector
R float64
}
func NewTriangle(a, b, c geom.Vector) Triangle {
return Triangle{
A: a,
B: b,
C: c,
}
}
func NewQuad(a, b, c, d geom.Vector) Quad {
return Quad{
A: a,
B: b,
C: c,
D: d,
}
}
func NewSphere(origin geom.Vector, r float64) Sphere {
return Sphere{
Origin: origin,
R: r,
}
}
func (tr Triangle) Intersect(ray geom.Ray) bool {
// a.k.a Möller–Trumbore intersection algorithm
const EPSILON float64 = 0.0000001
e1 := Subtract(tr.B, tr.A)
e2 := Subtract(tr.C, tr.A)
p := CrossProduct(ray.Direction, e2)
det := DotProduct(e1, p)
if det > -EPSILON && det < EPSILON {
return false
}
f := 1.0 / det
s := Subtract(ray.Origin, tr.A)
u := f * DotProduct(s, p)
if u < 0.0 || u > 1.0 {
return false
}
q := CrossProduct(s, e1)
v := f * DotProduct(ray.Direction, q)
if v < 0.0 || u+v > 1.0 {
return false
}
t := f * DotProduct(e2, q)
return t > EPSILON
}
func (quad Quad) Intersect(ray geom.Ray) bool {
// Disclaimer: doesn't work for complex quads :( (had no time to implement "Triangulating")
var wg sync.WaitGroup
const cnt int = 2
ch := make(chan bool, cnt)
defer close(ch)
for i := 0; i < cnt; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
var tr Triangle
if i == 0 {
tr = Triangle{quad.A, quad.B, quad.C}
} else {
tr = Triangle{quad.A, quad.C, quad.D}
}
ch <- tr.Intersect(ray)
}(i)
}
wg.Wait()
return <-ch || <-ch
}
func (sph Sphere) Intersect(ray geom.Ray) bool {
L := Subtract(ray.Origin, sph.Origin)
a := DotProduct(ray.Direction, ray.Direction)
b := 2 * DotProduct(ray.Direction, L)
c := DotProduct(L, L) - math.Pow(sph.R, 2)
const scaleNum float64 = 0.01
closeDirectionPoint := Add(ray.Origin, Multiply(ray.Direction, scaleNum))
return b*b-4*a*c >= 0 && Distance(sph.Origin, ray.Origin) > Distance(sph.Origin, closeDirectionPoint)
}
func Distance(a, b geom.Vector) float64 {
return math.Sqrt(math.Pow(a.X-b.X, 2) + math.Pow(a.Y-b.Y, 2) + math.Pow(a.Z-b.Z, 2))
}
func DotProduct(a, b geom.Vector) float64 {
return (a.X * b.X) + (a.Y * b.Y) + (a.Z * b.Z)
}
func CrossProduct(a, b geom.Vector) geom.Vector {
return geom.Vector{
X: a.Y*b.Z - a.Z*b.Y,
Y: a.Z*b.X - a.X*b.Z,
Z: a.X*b.Y - a.Y*b.X,
}
}
func Add(a, b geom.Vector) geom.Vector {
return geom.Vector{
X: a.X + b.X,
Y: a.Y + b.Y,
Z: a.Z + b.Z,
}
}
func Subtract(a, b geom.Vector) geom.Vector {
return geom.Vector{
X: a.X - b.X,
Y: a.Y - b.Y,
Z: a.Z - b.Z,
}
}
func Multiply(a geom.Vector, f float64) geom.Vector {
return geom.Vector{
X: a.X * f,
Y: a.Y * f,
Z: a.Z * f,
}
}
func main() {
}