Skip to content

Commit caf7d41

Browse files
committed
examples: use math.vec in path_tracing.v and in vyper.v
1 parent ebf629d commit caf7d41

File tree

3 files changed

+75
-116
lines changed

3 files changed

+75
-116
lines changed

examples/path_tracing.v

Lines changed: 41 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -29,48 +29,13 @@ import math
2929
import rand
3030
import time
3131
import term
32+
import math.vec
3233

3334
const inf = 1e+10
3435
const eps = 1e-4
3536
const f_0 = 0.0
3637

37-
//**************************** 3D Vector utility struct *********************
38-
struct Vec {
39-
mut:
40-
x f64 = 0.0
41-
y f64 = 0.0
42-
z f64 = 0.0
43-
}
44-
45-
@[inline]
46-
fn (v Vec) + (b Vec) Vec {
47-
return Vec{v.x + b.x, v.y + b.y, v.z + b.z}
48-
}
49-
50-
@[inline]
51-
fn (v Vec) - (b Vec) Vec {
52-
return Vec{v.x - b.x, v.y - b.y, v.z - b.z}
53-
}
54-
55-
@[inline]
56-
fn (v Vec) * (b Vec) Vec {
57-
return Vec{v.x * b.x, v.y * b.y, v.z * b.z}
58-
}
59-
60-
@[inline]
61-
fn (v Vec) dot(b Vec) f64 {
62-
return v.x * b.x + v.y * b.y + v.z * b.z
63-
}
64-
65-
@[inline]
66-
fn (v Vec) mult_s(b f64) Vec {
67-
return Vec{v.x * b, v.y * b, v.z * b}
68-
}
69-
70-
@[inline]
71-
fn (v Vec) cross(b Vec) Vec {
72-
return Vec{v.y * b.z - v.z * b.y, v.z * b.x - v.x * b.z, v.x * b.y - v.y * b.x}
73-
}
38+
type Vec = vec.Vec3[f64]
7439

7540
@[inline]
7641
fn (v Vec) norm() Vec {
@@ -211,14 +176,14 @@ const spheres = [
211176
rad: 16.5
212177
p: Vec{27, 16.5, 47}
213178
e: Vec{}
214-
c: Vec{1, 1, 1}.mult_s(.999)
179+
c: Vec{1, 1, 1}.mul_scalar(.999)
215180
refl: .spec
216181
}, // Mirr
217182
Sphere{
218183
rad: 16.5
219184
p: Vec{73, 16.5, 78}
220185
e: Vec{}
221-
c: Vec{1, 1, 1}.mult_s(.999)
186+
c: Vec{1, 1, 1}.mul_scalar(.999)
222187
refl: .refr
223188
}, // Glas
224189
Sphere{
@@ -232,23 +197,23 @@ const spheres = [
232197
[// scene 1 sunset
233198
Sphere{
234199
rad: 1600
235-
p: Vec{1.0, 0.0, 2.0}.mult_s(3000)
236-
e: Vec{1.0, .9, .8}.mult_s(1.2e+1 * 1.56 * 2)
200+
p: Vec{1.0, 0.0, 2.0}.mul_scalar(3000)
201+
e: Vec{1.0, .9, .8}.mul_scalar(1.2e+1 * 1.56 * 2)
237202
c: Vec{}
238203
refl: .diff
239204
}, // sun
240205
Sphere{
241206
rad: 1560
242-
p: Vec{1, 0, 2}.mult_s(3500)
243-
e: Vec{1.0, .5, .05}.mult_s(4.8e+1 * 1.56 * 2)
207+
p: Vec{1, 0, 2}.mul_scalar(3500)
208+
e: Vec{1.0, .5, .05}.mul_scalar(4.8e+1 * 1.56 * 2)
244209
c: Vec{}
245210
refl: .diff
246211
}, // horizon sun2
247212
Sphere{
248213
rad: 10000
249214
p: cen + Vec{0, 0, -200}
250-
e: Vec{0.00063842, 0.02001478, 0.28923243}.mult_s(6e-2 * 8)
251-
c: Vec{.7, .7, 1}.mult_s(.25)
215+
e: Vec{0.00063842, 0.02001478, 0.28923243}.mul_scalar(6e-2 * 8)
216+
c: Vec{.7, .7, 1}.mul_scalar(.25)
252217
refl: .diff
253218
}, // sky
254219
Sphere{
@@ -261,7 +226,7 @@ const spheres = [
261226
Sphere{
262227
rad: 110000
263228
p: Vec{50, -110048.5, 0}
264-
e: Vec{.9, .5, .05}.mult_s(4)
229+
e: Vec{.9, .5, .05}.mul_scalar(4)
265230
c: Vec{}
266231
refl: .diff
267232
}, // horizon brightener
@@ -276,44 +241,44 @@ const spheres = [
276241
rad: 26.5
277242
p: Vec{22, 26.5, 42}
278243
e: Vec{}
279-
c: Vec{1, 1, 1}.mult_s(.596)
244+
c: Vec{1, 1, 1}.mul_scalar(.596)
280245
refl: .spec
281246
}, // white Mirr
282247
Sphere{
283248
rad: 13
284249
p: Vec{75, 13, 82}
285250
e: Vec{}
286-
c: Vec{.96, .96, .96}.mult_s(.96)
251+
c: Vec{.96, .96, .96}.mul_scalar(.96)
287252
refl: .refr
288253
}, // Glas
289254
Sphere{
290255
rad: 22
291256
p: Vec{87, 22, 24}
292257
e: Vec{}
293-
c: Vec{.6, .6, .6}.mult_s(.696)
258+
c: Vec{.6, .6, .6}.mul_scalar(.696)
294259
refl: .refr
295260
}, // Glas2
296261
],
297262
[// scene 3 Psychedelic
298263
Sphere{
299264
rad: 150
300265
p: Vec{50 + 75, 28, 62}
301-
e: Vec{1, 1, 1}.mult_s(0e-3)
302-
c: Vec{1, .9, .8}.mult_s(.93)
266+
e: Vec{1, 1, 1}.mul_scalar(0e-3)
267+
c: Vec{1, .9, .8}.mul_scalar(.93)
303268
refl: .refr
304269
},
305270
Sphere{
306271
rad: 28
307272
p: Vec{50 + 5, -28, 62}
308-
e: Vec{1, 1, 1}.mult_s(1e+1)
309-
c: Vec{1, 1, 1}.mult_s(0)
273+
e: Vec{1, 1, 1}.mul_scalar(1e+1)
274+
c: Vec{1, 1, 1}.mul_scalar(0)
310275
refl: .diff
311276
},
312277
Sphere{
313278
rad: 300
314279
p: Vec{50, 28, 62}
315-
e: Vec{1, 1, 1}.mult_s(0e-3)
316-
c: Vec{1, 1, 1}.mult_s(.93)
280+
e: Vec{1, 1, 1}.mul_scalar(0e-3)
281+
c: Vec{1, 1, 1}.mul_scalar(.93)
317282
refl: .spec
318283
},
319284
],
@@ -406,10 +371,10 @@ fn radiance(r Ray, depthi int, scene_id int) Vec {
406371

407372
obj := scene[id] // the hit object
408373

409-
x := r.o + r.d.mult_s(t)
410-
n := (x - obj.p).norm()
374+
x := r.o + r.d.mul_scalar(t)
375+
n := Vec(x - obj.p).norm()
411376

412-
nl := if n.dot(r.d) < 0.0 { n } else { n.mult_s(-1) }
377+
nl := if n.dot(r.d) < 0.0 { n } else { n.mul_scalar(-1) }
413378

414379
mut f := obj.c
415380

@@ -426,7 +391,7 @@ fn radiance(r Ray, depthi int, scene_id int) Vec {
426391
depth++
427392
if depth > 5 {
428393
if rand_f64() < p {
429-
f = f.mult_s(f64(1.0) / p)
394+
f = f.mul_scalar(f64(1.0) / p)
430395
} else {
431396
return obj.e // R.R.
432397
}
@@ -445,25 +410,26 @@ fn radiance(r Ray, depthi int, scene_id int) Vec {
445410
w := nl
446411

447412
mut u := if math.abs(w.x) > f64(0.1) { Vec{0, 1, 0} } else { Vec{1, 0, 0} }
448-
u = u.cross(w).norm()
413+
u = Vec(u.cross(w)).norm()
449414

450415
v := w.cross(u)
451416

452417
// **Full Precision**
453-
// d := (u.mult_s(math.cos(r1) * r2s) + v.mult_s(math.sin(r1) * r2s) + w.mult_s(1.0 - r2)).norm()
418+
// d := (u.mul_scalar(math.cos(r1) * r2s) + v.mul_scalar(math.sin(r1) * r2s) + w.mul_scalar(1.0 - r2)).norm()
454419

455420
// tabbed speed-up
456-
d := (u.mult_s(tabs.cos_tab[r1] * r2s) + v.mult_s(tabs.sin_tab[r1] * r2s) +
457-
w.mult_s(math.sqrt(f64(1.0) - r2))).norm()
421+
d := Vec(u.mul_scalar(tabs.cos_tab[r1] * r2s) + v.mul_scalar(tabs.sin_tab[r1] * r2s) +
422+
(w.mul_scalar(math.sqrt(f64(1.0) - r2)))).norm()
458423

459424
return obj.e + f * radiance(Ray{x, d}, depth, scene_id)
460425
} else {
461426
if obj.refl == .spec { // Ideal SPECULAR reflection
462-
return obj.e + f * radiance(Ray{x, r.d - n.mult_s(2.0 * n.dot(r.d))}, depth, scene_id)
427+
return obj.e +
428+
f * radiance(Ray{x, r.d - n.mul_scalar(2.0 * n.dot(r.d))}, depth, scene_id)
463429
}
464430
}
465431

466-
refl_ray := Ray{x, r.d - n.mult_s(2.0 * n.dot(r.d))} // Ideal dielectric REFRACTION
432+
refl_ray := Ray{x, r.d - n.mul_scalar(2.0 * n.dot(r.d))} // Ideal dielectric REFRACTION
467433
into := n.dot(nl) > 0 // Ray from outside going in?
468434

469435
nc := f64(1.0)
@@ -478,7 +444,7 @@ fn radiance(r Ray, depthi int, scene_id int) Vec {
478444
}
479445

480446
dirc := if into { f64(1) } else { f64(-1) }
481-
tdir := (r.d.mult_s(nnt) - n.mult_s(dirc * (ddn * nnt + math.sqrt(cos2t)))).norm()
447+
tdir := Vec(r.d.mul_scalar(nnt) - n.mul_scalar(dirc * (ddn * nnt + math.sqrt(cos2t)))).norm()
482448

483449
a := nt - nc
484450
b := nt + nc
@@ -495,13 +461,13 @@ fn radiance(r Ray, depthi int, scene_id int) Vec {
495461
if depth > 2 {
496462
// Russian roulette
497463
tmp = if rand_f64() < pp {
498-
radiance(refl_ray, depth, scene_id).mult_s(rp)
464+
radiance(refl_ray, depth, scene_id).mul_scalar(rp)
499465
} else {
500-
radiance(Ray{x, tdir}, depth, scene_id).mult_s(tp)
466+
radiance(Ray{x, tdir}, depth, scene_id).mul_scalar(tp)
501467
}
502468
} else {
503-
tmp = (radiance(refl_ray, depth, scene_id).mult_s(re)) +
504-
(radiance(Ray{x, tdir}, depth, scene_id).mult_s(tr))
469+
tmp = (radiance(refl_ray, depth, scene_id).mul_scalar(re)) +
470+
(radiance(Ray{x, tdir}, depth, scene_id).mul_scalar(tr))
505471
}
506472
return obj.e + (f * tmp)
507473
}
@@ -517,7 +483,7 @@ fn ray_trace(w int, h int, samps int, file_name string, scene_id int) Image {
517483

518484
cam := Ray{Vec{50, 52, 295.6}, Vec{0, -0.042612, -1}.norm()} // cam position, direction
519485
cx := Vec{f64(w) * 0.5135 / f64(h), 0, 0}
520-
cy := cx.cross(cam.d).norm().mult_s(0.5135)
486+
cy := Vec(cx.cross(cam.d)).norm().mul_scalar(0.5135)
521487
mut r := Vec{}
522488

523489
// speed-up constants
@@ -544,12 +510,12 @@ fn ray_trace(w int, h int, samps int, file_name string, scene_id int) Image {
544510
r2 := v_2 * rand_f64()
545511
dy := if r2 < v_1 { math.sqrt(r2) - v_1 } else { v_1 - math.sqrt(v_2 - r2) }
546512

547-
d := cx.mult_s(((f64(sx) + 0.5 + dx) * 0.5 + f64(x)) * w1 - .5) +
548-
cy.mult_s(((f64(sy) + 0.5 + dy) * 0.5 + f64(y)) * h1 - .5) + cam.d
513+
d := cx.mul_scalar(((f64(sx) + 0.5 + dx) * 0.5 + f64(x)) * w1 - .5) +
514+
cy.mul_scalar(((f64(sy) + 0.5 + dy) * 0.5 + f64(y)) * h1 - .5) + cam.d
549515
r = r + radiance(Ray{cam.o +
550-
d.mult_s(140.0), d.norm()}, 0, scene_id).mult_s(samps1)
516+
d.mul_scalar(140.0), Vec(d).norm()}, 0, scene_id).mul_scalar(samps1)
551517
}
552-
tmp_vec := Vec{clamp(r.x), clamp(r.y), clamp(r.z)}.mult_s(.25)
518+
tmp_vec := Vec{clamp(r.x), clamp(r.y), clamp(r.z)}.mul_scalar(.25)
553519
unsafe {
554520
(*ivec) = *ivec + tmp_vec
555521
}

examples/term.ui/vyper.v

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// import modules for use in app
22
import term.ui as termui
33
import rand
4+
import math.vec
45

56
// define some global constants
67
const block_size = 1
@@ -29,11 +30,7 @@ enum GameState {
2930
}
3031

3132
// simple 2d vector representation
32-
struct Vec {
33-
mut:
34-
x int
35-
y int
36-
}
33+
type Vec = vec.Vec2[int]
3734

3835
// determine orientation from vector (hacky way to set facing from velocity)
3936
fn (v Vec) facing() Orientation {
@@ -58,10 +55,7 @@ fn (mut v Vec) randomize(min_x int, min_y int, max_x int, max_y int) {
5855
// part of snake's body representation
5956
struct BodyPart {
6057
mut:
61-
pos Vec = Vec{
62-
x: block_size
63-
y: block_size
64-
}
58+
pos Vec = Vec{block_size, block_size}
6559
color termui.Color = green
6660
facing Orientation = .top
6761
}
@@ -72,10 +66,7 @@ mut:
7266
app &App = unsafe { nil }
7367
direction Orientation
7468
body []BodyPart
75-
velocity Vec = Vec{
76-
x: 0
77-
y: 0
78-
}
69+
velocity Vec
7970
}
8071

8172
// length returns the snake's current length
@@ -85,27 +76,27 @@ fn (s Snake) length() int {
8576

8677
// impulse provides a impulse to change the snake's direction
8778
fn (mut s Snake) impulse(direction Orientation) {
88-
mut vec := Vec{}
79+
mut vel := Vec{}
8980
match direction {
9081
.top {
91-
vec.x = 0
92-
vec.y = -1 * block_size
82+
vel.x = 0
83+
vel.y = -1 * block_size
9384
}
9485
.right {
95-
vec.x = 2 * block_size
96-
vec.y = 0
86+
vel.x = 2 * block_size
87+
vel.y = 0
9788
}
9889
.bottom {
99-
vec.x = 0
100-
vec.y = block_size
90+
vel.x = 0
91+
vel.y = block_size
10192
}
10293
.left {
103-
vec.x = -2 * block_size
104-
vec.y = 0
94+
vel.x = -2 * block_size
95+
vel.y = 0
10596
}
10697
}
10798
s.direction = direction
108-
s.velocity = vec
99+
s.velocity = vel
109100
}
110101

111102
// move performs the calculations for the snake's movements
@@ -239,10 +230,7 @@ fn (s Snake) draw() {
239230
// rat representation
240231
struct Rat {
241232
mut:
242-
pos Vec = Vec{
243-
x: block_size
244-
y: block_size
245-
}
233+
pos Vec = Vec{block_size, block_size}
246234
captured bool
247235
color termui.Color = grey
248236
app &App = unsafe { nil }
@@ -441,10 +429,7 @@ fn (mut a App) draw_debug() {
441429
fn (mut a App) draw_gameover() {
442430
a.termui.set_bg_color(white)
443431
a.termui.set_color(red)
444-
a.rat.pos = Vec{
445-
x: -1
446-
y: -1
447-
}
432+
a.rat.pos = Vec{-1, -1}
448433
x_offset := ' ##### '.len // take half of a line from the game over text and store the length
449434
start_x := (a.width / 2) - x_offset
450435
a.termui.draw_text(start_x, (a.height / 2) - 3 * block_size, ' ##### ####### ')

0 commit comments

Comments
 (0)