-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec3_test.go
245 lines (165 loc) · 4.1 KB
/
vec3_test.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package raytracer_test
import (
"math"
"testing"
r "github.com/derek-schaefer/raytracer"
"github.com/stretchr/testify/assert"
)
func TestNewVec3(t *testing.T) {
v := r.NewVec3(1, 2, 3)
assert.Equal(t, v.X(), float64(1))
assert.Equal(t, v.Y(), float64(2))
assert.Equal(t, v.Z(), float64(3))
}
func TestRandomVec3(t *testing.T) {
min := 0.0
max := 1.0
v := r.RandomVec3(random)
x := v.X()
y := v.Y()
z := v.Z()
assert.True(t, min <= x && x <= max)
assert.True(t, min <= y && y <= max)
assert.True(t, min <= z && z <= max)
}
func TestRandomUnitSphereVec3(t *testing.T) {
v := r.RandomUnitSphereVec3(random)
assert.Less(t, v.LengthSquared(), float64(1))
}
func TestRandomUnitVec3(t *testing.T) {
v := r.RandomUnitVec3(random)
f := v.Length()
assert.True(t, r.NearlyEqual(f, 1))
for i := 0; i < len(v); i++ {
assert.True(t, -1 <= v[i] && v[i] <= 1)
}
}
func TestRandomHemisphereVec3(t *testing.T) {
s := r.RandomUnitSphereVec3(random)
v := r.RandomHemisphereVec3(random, s)
assert.Greater(t, v.Dot(s), float64(0))
}
func TestRandomUnitDiskVec3(t *testing.T) {
p := r.RandomUnitDiskVec3(random)
assert.Less(t, p.LengthSquared(), 1.0)
for i := 0; i < 2; i++ {
assert.True(t, -1 <= p[i] && p[i] <= 1)
}
}
func TestRandomRangeVec3(t *testing.T) {
min := 2.0
max := 3.0
v := r.RandomRangeVec3(random, min, max)
x := v.X()
y := v.Y()
z := v.Z()
assert.True(t, min <= x && x <= max)
assert.True(t, min <= y && y <= max)
assert.True(t, min <= z && z <= max)
}
func TestVec3X(t *testing.T) {
v := r.Vec3{1, 0, 0}
assert.Equal(t, v.X(), float64(1))
}
func TestVec3Y(t *testing.T) {
v := r.Vec3{0, 1, 0}
assert.Equal(t, v.Y(), float64(1))
}
func TestVec3Z(t *testing.T) {
v := r.Vec3{0, 0, 1}
assert.Equal(t, v.Z(), float64(1))
}
func TestVec3SetY(t *testing.T) {
v := r.Vec3{0, 0, 0}
v.SetY(1)
assert.Equal(t, v.Y(), float64(1))
}
func TestVec3SetZ(t *testing.T) {
v := r.Vec3{0, 0, 0}
v.SetZ(1)
assert.Equal(t, v.Z(), float64(1))
}
func TestVec3SetX(t *testing.T) {
v := r.Vec3{0, 0, 0}
v.SetX(1)
assert.Equal(t, v.X(), float64(1))
}
func TestVec3Add(t *testing.T) {
v1 := r.Vec3{1, 2, 3}
v2 := r.Vec3{1, 1, 1}
v3 := v1.Add(v2)
assert.Equal(t, v3, r.Vec3{2, 3, 4})
}
func TestVec3Subtract(t *testing.T) {
v1 := r.Vec3{1, 2, 3}
v2 := r.Vec3{1, 1, 1}
v3 := v1.Subtract(v2)
assert.Equal(t, v3, r.Vec3{0, 1, 2})
}
func TestVec3Multiply(t *testing.T) {
v1 := r.Vec3{1, 2, 3}
v2 := v1.Multiply(2)
assert.Equal(t, v2, r.Vec3{2, 4, 6})
}
func TestVec3MultiplyV(t *testing.T) {
v1 := r.NewVec3(2, 3, 4)
v2 := r.NewVec3(3, 4, 5)
assert.Equal(t, v1.MultiplyV(v2), r.NewVec3(6, 12, 20))
}
func TestVec3Divide(t *testing.T) {
v1 := r.Vec3{2, 4, 8}
v2 := v1.Divide(2)
assert.Equal(t, v2, r.Vec3{1, 2, 4})
}
func TestVec3Unit(t *testing.T) {
n1 := 3.0
v1 := r.Vec3{n1, n1, n1}
n2 := n1 / math.Sqrt(math.Pow(n1, 3))
v2 := v1.Unit()
assert.Equal(t, v2, r.Vec3{n2, n2, n2})
}
func TestVec3LengthSquared(t *testing.T) {
v := r.Vec3{2, 3, 4}
assert.Equal(t, v.LengthSquared(), float64(29))
}
func TestVec3Length(t *testing.T) {
v := r.Vec3{2, 3, 4}
assert.Equal(t, v.Length(), math.Sqrt(29))
}
func TestVec3Dot(t *testing.T) {
v1 := r.Vec3{1, 2, 3}
v2 := r.Vec3{2, 3, 4}
v3 := v1.Dot(v2)
assert.Equal(t, v3, float64(20))
}
func TestVec3Cross(t *testing.T) {
v1 := r.Vec3{1, 2, 3}
v2 := r.Vec3{2, 3, 4}
v3 := v1.Cross(v2)
assert.Equal(t, v3, r.NewVec3(-1, 2, -1))
}
func TestVec3Reflect(t *testing.T) {
v1 := r.NewVec3(0.1, 0.1, 0.1)
v2 := r.NewVec3(0.2, 0.2, 0.2)
n := 0.076
assert.Equal(t, v1.Reflect(v2), r.NewVec3(n, n, n))
}
func TestVec3NearZero(t *testing.T) {
v1 := r.NewVec3(0, 0, 0)
assert.True(t, v1.NearZero())
for i := 0; i < 3; i++ {
var v2 r.Vec3
v2[i] = math.SmallestNonzeroFloat64
assert.False(t, v2.NearZero())
}
}
func TestVec3Refract(t *testing.T) {
v1 := r.NewVec3(0.1, 0.1, 0.1)
v2 := r.NewVec3(0.2, 0.2, 0.2)
n := -0.06270264507705078
assert.Equal(t, v1.Refract(v2, 1.5), r.NewVec3(n, n, n))
}
func TestVec3String(t *testing.T) {
v := r.Vec3{1, 2, 3}
assert.Equal(t, v.String(), "Vec3(1.000000, 2.000000, 3.000000)")
}