-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec.go
200 lines (171 loc) · 4.3 KB
/
vec.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package gotrace
import (
"image/color"
"math"
"math/rand"
"github.com/teobouvard/gotrace/util"
)
// Vec3 defines a 3-dimensional vector
type Vec3 struct {
X, Y, Z float64
}
// Colors
var (
BLACK = Vec3{0, 0, 0}
WHITE = Vec3{1, 1, 1}
RED = Vec3{1, 0, 0}
GREEN = Vec3{0, 1, 0}
BLUE = Vec3{0, 0, 1}
)
// Add returns u + v
func (u Vec3) Add(v Vec3) Vec3 {
return Vec3{
X: u.X + v.X,
Y: u.Y + v.Y,
Z: u.Z + v.Z,
}
}
// Sub returns u-v
func (u Vec3) Sub(v Vec3) Vec3 {
return Vec3{
X: u.X - v.X,
Y: u.Y - v.Y,
Z: u.Z - v.Z,
}
}
// Neg returns -v
func (u Vec3) Neg() Vec3 {
return u.Scale(-1.0)
}
// Scale returns v scaled by t
func (u Vec3) Scale(t float64) Vec3 {
return Vec3{
X: t * u.X,
Y: t * u.Y,
Z: t * u.Z,
}
}
// Div returns the scaling of v by 1/t
func (u Vec3) Div(t float64) Vec3 {
if t == 0 {
panic("division by zero")
}
return Vec3{
X: u.X / t,
Y: u.Y / t,
Z: u.Z / t,
}
}
// Dot returns the dot (inner) product between u and v
func (u Vec3) Dot(v Vec3) float64 {
return u.X*v.X + u.Y*v.Y + u.Z*v.Z
}
// Cross returns the cross product between u and v
func (u Vec3) Cross(v Vec3) Vec3 {
return Vec3{
X: u.Y*v.Z - u.Z*v.Y,
Y: u.Z*v.X - u.X*v.Z,
Z: u.X*v.Y - u.Y*v.X,
}
}
// Mul returns the *termwise* product betweenu and v
func (u Vec3) Mul(v Vec3) Vec3 {
return Vec3{
X: u.X * v.X,
Y: u.Y * v.Y,
Z: u.Z * v.Z,
}
}
// Reflect computes the reflection of v if it hits a surface of normal n
func (u Vec3) Reflect(n Vec3) Vec3 {
proj := n.Scale(2 * u.Dot(n))
return u.Sub(proj)
}
// Refract returns the refraction of u at an interface
func (u Vec3) Refract(n Vec3, nRatio float64) (bool, Vec3) {
dot := u.Dot(n)
discriminant := 1.0 - nRatio*nRatio*(1-dot*dot)
if discriminant > 0 {
refracted := u.Sub(n.Scale(dot)).Scale(nRatio).Sub(n.Scale(math.Sqrt(discriminant)))
return true, refracted
}
return false, Vec3{}
}
// Unit returns a unit vector from u
func (u Vec3) Unit() Vec3 {
return u.Div(u.Norm())
}
// MinCoord returns a new vector corresponding to the element-wise minimum of the two vectors
func MinCoord(u Vec3, v Vec3) Vec3 {
return Vec3{math.Min(u.X, v.X), math.Min(u.Y, v.Y), math.Min(u.Z, v.Z)}
}
// MaxCoord returns a new vector corresponding to the element-wise maximum of the two vectors
func MaxCoord(u Vec3, v Vec3) Vec3 {
return Vec3{math.Max(u.X, v.X), math.Max(u.Y, v.Y), math.Max(u.Z, v.Z)}
}
// Norm returns the euclidean norm of u
func (u Vec3) Norm() float64 {
return math.Sqrt(u.SquareNorm())
}
// SquareNorm returns the square of the euclidean norm of u
func (u Vec3) SquareNorm() float64 {
return u.Dot(u)
}
// RandSphere returns vector drawn from a lambertian distribution inside the unit sphere
func RandSphere(rnd *rand.Rand) Vec3 {
if rnd == nil {
panic("No random source")
}
a := 2.0 * rnd.Float64() * math.Pi
z := 2.0 * (rnd.Float64() - 0.5)
r := math.Sqrt(1 - z*z)
return Vec3{
r * math.Cos(a),
r * math.Sin(a),
z,
}
}
// RandDisk returns a random vector in the unit disk
func RandDisk(rnd *rand.Rand) Vec3 {
if rnd == nil {
panic("No random source")
}
theta := 2 * math.Pi * rnd.Float64()
r := rnd.Float64()
return Vec3{X: r * math.Cos(theta), Y: r * math.Sin(theta)}
}
// RandVec returns a random vector with coordinates in [0, 1)
func RandVec(rnd *rand.Rand) Vec3 {
if rnd == nil {
panic("No random source")
}
return Vec3{rnd.Float64(), rnd.Float64(), rnd.Float64()}
}
// RandVecInterval returns a random vector with coordinates in [low, high)
func RandVecInterval(low float64, high float64, rnd *rand.Rand) Vec3 {
if rnd == nil {
panic("No random source")
}
x := rnd.Float64()*(high-low) + low
y := rnd.Float64()*(high-low) + low
z := rnd.Float64()*(high-low) + low
return Vec3{x, y, z}
}
// AsArray returns the coordinates of the vector as an array of size 3
func (u Vec3) AsArray() [3]float64 {
return [3]float64{u.X, u.Y, u.Z}
}
// GetColor retruns the RGBA color of the vector
func (u Vec3) GetColor(samples int) color.RGBA {
// normalization + alpha correction
scale := 1.0 / float64(samples)
r := math.Sqrt(scale * u.X)
g := math.Sqrt(scale * u.Y)
b := math.Sqrt(scale * u.Z)
// cast to pixel color value
maxColor := 255.0
ir := uint8(util.Map(r, 0, 1, 0, maxColor))
ig := uint8(util.Map(g, 0, 1, 0, maxColor))
ib := uint8(util.Map(b, 0, 1, 0, maxColor))
return color.RGBA{ir, ig, ib, 255}
}