Skip to content

Commit

Permalink
Specializing boundingbox for Ray primitives (#641)
Browse files Browse the repository at this point in the history
* Specializing `boundingbox` for `Ray` primitives
as discussed in #639

* Update boundingbox(::Ray)

using `coordinates(p)` from public API instead of directly accessing internal fields of structs

Co-authored-by: Júlio Hoffimann <[email protected]>

* Fix boundingbox; only call `coordinates` for Point

---------

Co-authored-by: Júlio Hoffimann <[email protected]>
  • Loading branch information
MachSilva and juliohm authored Nov 29, 2023
1 parent 5e74146 commit 872e99b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/boundingboxes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ boundingbox(p::Point) = Box(p, p)

boundingbox(b::Box) = b

function boundingbox(r::Ray{Dim,T}) where {Dim,T}
lower(p, v) = v < 0 ? typemin(T) : p
upper(p, v) = v > 0 ? typemax(T) : p
p = r(0)
v = r(1) - r(0)
l = lower.(coordinates(p), v)
u = upper.(coordinates(p), v)
Box(Point(l), Point(u))
end

function boundingbox(s::Sphere{Dim,T}) where {Dim,T}
c = center(s)
r = radius(s)
Expand Down
13 changes: 13 additions & 0 deletions test/boundingboxes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@
@test boundingbox(b) == b
@test @allocated(boundingbox(b)) < 50

r = Ray(P2(0, 0), V2(1, 0))
@test boundingbox(r) == Box(P2(0, 0), P2(T(Inf), 0))
@test @allocated(boundingbox(r)) < 50
r = Ray(P2(1, 1), V2(0, 1))
@test boundingbox(r) == Box(P2(1, 1), P2(1, T(Inf)))
@test @allocated(boundingbox(r)) < 50
r = Ray(P2(1, 1), V2(-1, -1))
@test boundingbox(r) == Box(P2(T(-Inf), T(-Inf)), P2(1, 1))
@test @allocated(boundingbox(r)) < 50
r = Ray(P2(-1, 1), V2(1, -1))
@test boundingbox(r) == Box(P2(-1, T(-Inf)), P2(T(Inf), 1))
@test @allocated(boundingbox(r)) < 50

s = Sphere(P2(0, 0), T(1))
@test boundingbox(s) == Box(P2(-1, -1), P2(1, 1))
@test @allocated(boundingbox(s)) < 50
Expand Down

0 comments on commit 872e99b

Please sign in to comment.