Skip to content

Commit 397a114

Browse files
committed
Add missing function, continue tests
1 parent da3088b commit 397a114

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

src/RayTracingWeekend.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module RayTracingWeekend
33
using Images, LinearAlgebra, Random, RandomNumbers.Xorshifts, StaticArrays
44

55
export color_vec3_in_rgb, default_camera, get_ray, hit, near_zero, point, random_between, random_vec2,
6-
random_vec2_in_disk, random_vec3, random_vec3_on_sphere, ray_color, ray_to_HitRecord, reflect,
6+
random_vec2_in_disk, random_vec3, random_vec3_in_sphere, random_vec3_on_sphere, ray_color, ray_to_HitRecord, reflect,
77
reflectance, refract, render, reseed!, rgb, rgb_gamma2, skycolor, squared_length, trand
88
export Camera, Dielectric, Hittable, HittableList, HitRecord, Lambertian, Material, Metal, Ray, Scatter, Sphere, Vec3
99
export scene_2_spheres, scene_4_spheres, scene_blue_red_spheres, scene_diel_spheres, scene_random_spheres
@@ -58,6 +58,15 @@ end
5858
rand(rng, T)
5959
end
6060

61+
@inline function random_vec3_in_sphere(::Type{T}) where T # equiv to random_in_unit_sphere()
62+
while true
63+
p = random_vec3(T(-1), T(1))
64+
if pp <= 1
65+
return p
66+
end
67+
end
68+
end
69+
6170
@inline random_between(min::T=0, max::T=1) where T = trand(T)*(max-min) + min # equiv to random_double()
6271
@inline random_vec3(min::T, max::T) where T = @inbounds @SVector[random_between(min, max) for i 1:3]
6372
@inline random_vec2(min::T, max::T) where T = @inbounds @SVector[random_between(min, max) for i 1:2]

test/runtests.jl

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,50 +33,41 @@ end
3333

3434
@testset "RayTracingWeekend.jl" begin
3535
t_col = SA[0.4, 0.5, 0.1] # test color
36+
37+
# Verify that no heap allocations occur for low-level functions
3638
@test (@ballocated squared_length($t_col)) == 0 # 1.162 ns
3739

3840
@test !near_zero(t_col)
3941
@test (@ballocated near_zero($t_col)) == 0 # 1.382 ns
4042

43+
#@show @btime rgb($t_col) # 1.382 ns
44+
@test (@ballocated rgb($t_col)) == 0
4145

42-
@show rgb(t_col)
43-
# #rgb($t_col) # 1.172 ns (0 allocations: 0 bytes)
44-
45-
# #rgb_gamma2($t_col) # 3.927 ns (0 allocations: 0 bytes)
46-
47-
# _origin = SA[0.0,0.0,0.0]
48-
# _v3_minusY = SA[0.0,-1.0,0.0]
49-
# _t_ray1 = Ray(_origin, _v3_minusY)
50-
# # Float32: 6.161 ns (0 allocations: 0 bytes)
51-
# # Float64: 6.900 ns (0 allocations: 0 bytes)
52-
# #@btime Ray($_origin, $_v3_minusY)
53-
54-
# #@btime point($_t_ray1, 0.5) # 1.412 ns (0 allocations: 0 bytes)
55-
56-
# # 1.402 ns (0 allocations: 0 bytes)
57-
# #@btime skycolor($_t_ray1)
58-
59-
# # 1.412 ns (0 allocations: 0 bytes)
60-
# #@btime rgb(skycolor($_t_ray1)) # 291.492 ns (4 allocations: 80 bytes)
61-
62-
# #@btime trand()
63-
64-
# # 2.695 ns (0 allocations: 0 bytes)
65-
# #@btime random_between(50.0, 100.0)
46+
_origin = SA[0.0,0.0,0.0]
47+
_v3_minusY = SA[0.0,-1.0,0.0]
48+
_t_ray1 = Ray(_origin, _v3_minusY)
49+
50+
# Float32: 6.161 ns (0 allocations: 0 bytes)
51+
# Float64: 6.900 ns (0 allocations: 0 bytes)
52+
@test (@ballocated Ray($_origin, $_v3_minusY)) == 0
53+
54+
@test (@ballocated point($_t_ray1, 0.5)) == 0 # 1.412 ns
6655

67-
# #@btime random_vec3(-1.0,1.0)
56+
@test (@ballocated skycolor($_t_ray1)) == 0 # 1.402 ns
6857

69-
# #@btime random_vec2(-1.0f0,1.0f0) # 3.677 ns (0 allocations: 0 bytes)
58+
@test (@ballocated rgb(skycolor($_t_ray1))) == 0 # 1.412 ns
59+
60+
@test (@ballocated trand()) == 0
61+
62+
@test (@ballocated random_between(50.0, 100.0)) == 0 # 2.695 ns
7063

64+
@test (@ballocated random_vec3(-1.0,1.0)) == 0
65+
66+
@test (@ballocated random_vec2(-1.0f0,1.0f0)) == 0
67+
7168
# # REMEMBER: the times are somewhat random! Use best timing of 5!
72-
# # Float32: 34.690 ns (0 allocations: 0 bytes)
73-
# # Float64: 34.065 ns (0 allocations: 0 bytes)
74-
# # rand() using MersenneTwister _rng w/ Float64:
75-
# # 21.333 ns (0 allocations: 0 bytes)
76-
# # rand() using Xoroshiro128Plus _rng w/ Float64:
77-
# # 19.716 ns (0 allocations: 0 bytes)
78-
# random_vec3_in_sphere(Float64)
79-
69+
@test (@ballocated random_vec3_in_sphere(Float64)) == 0 # 19.716 ns
70+
8071
# "Random unit vector. Equivalent to C++'s `unit_vector(random_in_unit_sphere())`"
8172
# @inline random_vec3_on_sphere(::Type{T}) where T = normalize(random_vec3_in_sphere(T))
8273

0 commit comments

Comments
 (0)