Skip to content

Commit

Permalink
Rename Sphere to Ball (sphere is surface; ball is volume)
Browse files Browse the repository at this point in the history
  • Loading branch information
wsshin committed Sep 6, 2021
1 parent ab081a3 commit e038881
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 43 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![CI](https://github.com/stevengj/GeometryPrimitives.jl/workflows/CI/badge.svg)](https://github.com/stevengj/GeometryPrimitives.jl/actions)
[![Codecov](http://codecov.io/github/stevengj/GeometryPrimitives.jl/coverage.svg?branch=master)](http://codecov.io/github/stevengj/GeometryPrimitives.jl?branch=master)

This package provides a set of geometric primitive types (spheres, cuboids, cylinders, and
This package provides a set of geometric primitive types (balls, cuboids, cylinders, and
so on) and operations on them designed to enable piecewise definition of functions,
especially for finite-difference and finite-element simulations, in the Julia language.

Expand Down
2 changes: 1 addition & 1 deletion src/GeometryPrimitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ end

include("cuboid.jl")
include("ellipsoid.jl")
include("sphere.jl")
include("ball.jl")
include("prism/prism.jl")
include("periodize.jl")
include("kdtree.jl")
Expand Down
27 changes: 27 additions & 0 deletions src/ball.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export Ball

mutable struct Ball{N,N²,D} <: Shape{N,N²,D}
c::SVector{N,Float64} # center of ball
r::Float64 # radius
data::D # auxiliary data
Ball{N,N²,D}(c,r,data) where {N,N²,D} = new(c,r,data) # suppress default outer constructor
end

Ball(c::SVector{N,<:Real}, r::Real, data::D=nothing) where {N,D} = Ball{N,N*N,D}(c, r, data)
Ball(c::AbstractVector{<:Real}, r::Real, data=nothing) = (N = length(c); Ball(SVector{N}(c), r, data))

Base.:(==)(s1::Ball, s2::Ball) = s1.c==s2.c && s1.r==s2.r && s1.data==s2.data
Base.isapprox(s1::Ball, s2::Ball) = s1.cs2.c && s1.rs2.r && s1.data==s2.data
Base.hash(s::Ball, h::UInt) = hash(s.c, hash(s.r, hash(s.data, hash(:Ball, h))))

Base.in(x::SVector{N,<:Real}, s::Ball{N}) where {N} = sum(abs2, x - s.c) s.r^2

function surfpt_nearby(x::SVector{N,<:Real}, s::Ball{N}) where {N}
nout = x==s.c ? SVector(ntuple(k -> k==1 ? 1.0 : 0.0, Val(N))) : # nout = e₁ for x == s.c
normalize(x-s.c)
return s.c+s.r*nout, nout
end

translate(s::Ball{N,N²,D}, ∆::SVector{N,<:Real}) where {N,N²,D} = Ball{N,N²,D}(s.c+∆, s.r, s.data)

bounds(s::Ball) = (s.c.-s.r, s.c.+s.r)
6 changes: 3 additions & 3 deletions src/prism/cylinder.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
export Cylinder

const Cylinder = Prism{Sphere{2,4,Nothing}}
const Cylinder = Prism{Ball{2,4,Nothing}}

# Below, if we called Cylinder(c, ...) in the function body, it would call the inner
# constructor Prism{Sphere{2,4,Nothing}}(c, ...) because Cylinder = Prism{Sphere{2,4,Nothing}},
# constructor Prism{Ball{2,4,Nothing}}(c, ...) because Cylinder = Prism{Ball{2,4,Nothing}},
# which is not what we want.
# To call the outer constructor of Prism, we should call Prism(c, ...) instead of Cylinder(c, ...).
Cylinder(c::SVector{3,<:Real},
r::Real,
h::Real=Inf,
a::SVector{3,<:Real}=SVector(0.0,0.0,1.0),
data=nothing) where {D} =
(â = normalize(a); Prism(c, Sphere(SVector(0.0,0.0),r), h, [orthoaxes(â)... â], data))
(â = normalize(a); Prism(c, Ball(SVector(0.0,0.0),r), h, [orthoaxes(â)... â], data))

Cylinder(c::AbstractVector{<:Real}, # center of cylinder
r::Real, # radius of base
Expand Down
2 changes: 1 addition & 1 deletion src/prism/prism.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# ∆x, because the surface point x + ∆x can be easily calculated. Let's consider fixing this
# in the future.
#
# Hmmm... Because the surface point is not calculated by adding ∆x to x for Sphere and
# Hmmm... Because the surface point is not calculated by adding ∆x to x for Ball and
# Ellipse, maybe implemeting a function returning ∆x is not a very good idea. Then, it may
# not be a bad idea to calculate ∆x by subtracting x from the surface point. Let's try to
# implement the general prism.
Expand Down
27 changes: 0 additions & 27 deletions src/sphere.jl

This file was deleted.

8 changes: 4 additions & 4 deletions test/sphere.jl → test/ball.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@testset "Sphere" begin
s = Sphere([3,4], 5)
@testset "Ball" begin
s = Ball([3,4], 5)
@test s == deepcopy(s)
@test hash(s) == hash(deepcopy(s))
@test ndims(s) == 2
Expand All @@ -17,7 +17,7 @@
@test normal([-1,2],s) == normalize([-1,2] - [3,4])
@test bounds(s) == ([-2,-1],[8,9])
@test checkbounds(s)
@test checkbounds(Sphere([1,2,3], 2))
@test checkbounds(Ball([1,2,3], 2))

@test (∆ = rand(2); translate(s,∆) Sphere([3,4]+∆, 5))
@test (∆ = rand(2); translate(s,∆) Ball([3,4]+∆, 5))
end
10 changes: 5 additions & 5 deletions test/kdtree.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
@testset "KDTree" begin
s = [Sphere([i,0], 1) for i in 2:4]
s0 = Sphere([0,0], 1)
s = [Ball([i,0], 1) for i in 2:4]
s0 = Ball([0,0], 1)
s = [s0, s0, s0, s0, s...]
@test_nowarn KDTree(s) # must not generate StackOverflowError

s = Shape{2,4}[Sphere([i,0], 1, i) for i in 0:20]
s = Shape{2,4}[Ball([i,0], 1, i) for i in 0:20]
kd = KDTree(s)
@test GeometryPrimitives.depth(kd) == 3
@test findfirst([10.1,0], kd).data == 10
@test findfirst([10.1,1], kd) == nothing
@test checktree(kd, s)

s = Shape{3,9}[Sphere(SVector(randn(rng),randn(rng),randn(rng)), 0.01) for i=1:100]
s = Shape{3,9}[Ball(SVector(randn(rng),randn(rng),randn(rng)), 0.01) for i=1:100]
@test checktree(KDTree(s), s)
s = Shape{3,9}[Sphere(SVector(randn(rng),randn(rng),randn(rng)), 0.1) for i=1:100]
s = Shape{3,9}[Ball(SVector(randn(rng),randn(rng),randn(rng)), 0.1) for i=1:100]
@test checktree(KDTree(s), s)
end
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ end

@testset "GeometryPrimitives" begin

include("sphere.jl")
include("ball.jl")
include("cuboid.jl")
include("ellipsoid.jl")
include("cylinder.jl")
Expand Down

0 comments on commit e038881

Please sign in to comment.