-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathball.jl
More file actions
60 lines (45 loc) · 1.89 KB
/
ball.jl
File metadata and controls
60 lines (45 loc) · 1.89 KB
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
# ------------------------------------------------------------------
# Licensed under the MIT License. See LICENSE in the project root.
# ------------------------------------------------------------------
"""
Ball(center, radius)
A ball with `center` and `radius`.
See also [`Sphere`](@ref).
"""
struct Ball{M<:Manifold,C<:CRS,ℒ<:Len} <: Primitive{M,C}
center::Point{M,C}
radius::ℒ
Ball(center::Point{M,C}, radius::ℒ) where {M<:Manifold,C<:CRS,ℒ<:Len} = new{M,C,float(ℒ)}(center, radius)
end
Ball(center::Point, radius) = Ball(center, aslen(radius))
Ball(center::Tuple, radius) = Ball(Point(center), radius)
Ball(center::Point) = Ball(center, oneunit(lentype(center)))
Ball(center::Tuple) = Ball(Point(center))
paramdim(::Type{<:Ball{𝔼{Dim}}}) where {Dim} = Dim
paramdim(::Type{<:Ball{🌐}}) = 2
center(b::Ball) = b.center
radius(b::Ball) = b.radius
==(b₁::Ball, b₂::Ball) = center(b₁) == center(b₂) && radius(b₁) == radius(b₂)
Base.isapprox(b₁::Ball, b₂::Ball; atol=atol(lentype(b₁)), kwargs...) =
isapprox(center(b₁), center(b₂); atol, kwargs...) && isapprox(radius(b₁), radius(b₂); atol, kwargs...)
function (b::Ball{𝔼{2}})(ρ, φ)
if (ρ < 0 || ρ > 1) || (φ < 0 || φ > 1)
throw(DomainError((ρ, φ), "b(ρ, φ) is not defined for ρ, φ outside [0, 1]²."))
end
T = numtype(lentype(b))
ρ′ = T(ρ) * radius(b)
φ′ = T(φ) * 2 * T(π) * u"rad"
p = Point(convert(crs(b), Polar(ρ′, φ′)))
p + to(center(b))
end
function (b::Ball{𝔼{3}})(ρ, θ, φ)
if (ρ < 0 || ρ > 1) || (θ < 0 || θ > 1) || (φ < 0 || φ > 1)
throw(DomainError((ρ, θ, φ), "b(ρ, θ, φ) is not defined for ρ, θ, φ outside [0, 1]³."))
end
T = numtype(lentype(b))
ρ′ = T(ρ) * radius(b)
θ′ = T(θ) * T(π) * u"rad"
φ′ = T(φ) * 2 * T(π) * u"rad"
p = Point(convert(crs(b), Spherical(ρ′, θ′, φ′)))
p + to(center(b))
end