Skip to content

Commit 7805c93

Browse files
authored
Tweaks in primitive geometries (#1248)
1 parent fb1c11e commit 7805c93

File tree

8 files changed

+42
-43
lines changed

8 files changed

+42
-43
lines changed

src/geometries/primitives/ball.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ center(b::Ball) = b.center
3131

3232
radius(b::Ball) = b.radius
3333

34-
==(b₁::Ball, b₂::Ball) = b₁.center == b₂.center && b₁.radius == b₂.radius
34+
==(b₁::Ball, b₂::Ball) = center(b₁) == center(b₂) && radius(b₁) == radius(b₂)
3535

3636
Base.isapprox(b₁::Ball, b₂::Ball; atol=atol(lentype(b₁)), kwargs...) =
37-
isapprox(b₁.center, b₂.center; atol, kwargs...) && isapprox(b₁.radius, b₂.radius; atol, kwargs...)
37+
isapprox(center(b₁), center(b₂); atol, kwargs...) && isapprox(radius(b₁), radius(b₂); atol, kwargs...)
3838

3939
function (b::Ball{𝔼{2}})(ρ, φ)
4040
if< 0 || ρ > 1) ||< 0 || φ > 1)

src/geometries/primitives/bezier.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ ncontrols(b::BezierCurve) = length(b.controls)
3535

3636
degree(b::BezierCurve) = ncontrols(b) - 1
3737

38-
==(b₁::BezierCurve, b₂::BezierCurve) = b₁.controls == b₂.controls
38+
==(b₁::BezierCurve, b₂::BezierCurve) = controls(b₁) == controls(b₂)
3939

4040
Base.isapprox(b₁::BezierCurve, b₂::BezierCurve; atol=atol(lentype(b₁)), kwargs...) =
41-
length(b₁.controls) == length(b₂.controls) &&
42-
all(isapprox(p₁, p₂; atol, kwargs...) for (p₁, p₂) in zip(b₁.controls, b₂.controls))
41+
ncontrols(b₁) == ncontrols(b₂) &&
42+
all(isapprox(p₁, p₂; atol, kwargs...) for (p₁, p₂) in zip(controls(b₁), controls(b₂)))
4343

4444
"""
4545
Evaluation method used to obtain a point along
@@ -64,15 +64,15 @@ See <https://en.wikipedia.org/wiki/Horner%27s_method>.
6464
"""
6565
struct Horner <: BezierEvalMethod end
6666

67-
(curve::BezierCurve)(t) = curve(t, DeCasteljau())
67+
(b::BezierCurve)(t) = b(t, DeCasteljau())
6868

6969
# Apply DeCasteljau's method
70-
function (curve::BezierCurve)(t, ::DeCasteljau)
70+
function (b::BezierCurve)(t, ::DeCasteljau)
7171
if t < 0 || t > 1
7272
throw(DomainError(t, "b(t) is not defined for t outside [0, 1]."))
7373
end
74-
ss = segments(Rope(curve.controls))
75-
points = [s(t) for s in ss]
74+
rope = Rope(controls(b))
75+
points = [s(t) for s in segments(rope)]
7676
if length(points) == 1
7777
points[1]
7878
else
@@ -85,14 +85,14 @@ end
8585
# curve, aᵢ = binomial(n, i) * pᵢ * t̄ⁿ⁻ⁱ and t̄ = (1 - t).
8686
# Horner's rule recursively reconstructs B from a sequence bᵢ
8787
# with bₙ = aₙ and bᵢ₋₁ = aᵢ₋₁ + bᵢ * t until b₀ = B.
88-
function (curve::BezierCurve)(t, ::Horner)
88+
function (b::BezierCurve)(t, ::Horner)
8989
if t < 0 || t > 1
9090
throw(DomainError(t, "b(t) is not defined for t outside [0, 1]."))
9191
end
92-
T = numtype(lentype(curve))
93-
cs = curve.controls
92+
T = numtype(lentype(b))
93+
cs = controls(b)
9494
= one(T) - t
95-
n = degree(curve)
95+
n = degree(b)
9696
pₙ = to(last(cs))
9797
aₙ = pₙ
9898

@@ -109,7 +109,7 @@ function (curve::BezierCurve)(t, ::Horner)
109109
end
110110

111111
b₀ = bᵢ₋₁
112-
withcrs(curve, b₀)
112+
withcrs(b, b₀)
113113
end
114114

115115
# -----------
@@ -119,14 +119,14 @@ end
119119
function Base.show(io::IO, b::BezierCurve)
120120
ioctx = IOContext(io, :compact => true)
121121
print(io, "BezierCurve(controls: [")
122-
join(ioctx, b.controls, ", ")
122+
join(ioctx, controls(b), ", ")
123123
print(io, "])")
124124
end
125125

126126
function Base.show(io::IO, ::MIME"text/plain", b::BezierCurve)
127127
summary(io, b)
128128
println(io)
129129
print(io, "└─ controls: [")
130-
join(io, b.controls, ", ")
130+
join(io, controls(b), ", ")
131131
print(io, "]")
132132
end

src/geometries/primitives/box.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,20 @@ Base.minimum(b::Box) = b.min
4949

5050
Base.maximum(b::Box) = b.max
5151

52-
Base.extrema(b::Box) = b.min, b.max
52+
Base.extrema(b::Box) = minimum(b), maximum(b)
5353

54-
diagonal(b::Box{<:𝔼}) = norm(b.max - b.min)
54+
diagonal(b::Box{<:𝔼}) = norm(maximum(b) - minimum(b))
5555

56-
sides(b::Box{<:𝔼}) = Tuple(b.max - b.min)
56+
sides(b::Box{<:𝔼}) = Tuple(maximum(b) - minimum(b))
5757

58-
==(b₁::Box, b₂::Box) = b₁.min == b₂.min && b₁.max == b₂.max
58+
==(b₁::Box, b₂::Box) = minimum(b₁) == minimum(b₂) && maximum(b₁) == maximum(b₂)
5959

6060
Base.isapprox(b₁::Box, b₂::Box; atol=atol(lentype(b₁)), kwargs...) =
61-
isapprox(b₁.min, b₂.min; atol, kwargs...) && isapprox(b₁.max, b₂.max; atol, kwargs...)
61+
isapprox(minimum(b₁), minimum(b₂); atol, kwargs...) && isapprox(maximum(b₁), maximum(b₂); atol, kwargs...)
6262

6363
function (b::Box{<:𝔼})(uv...)
6464
if !all(x -> 0 x 1, uv)
6565
throw(DomainError(uv, "b(u, v, ...) is not defined for u, v, ... outside [0, 1]ⁿ."))
6666
end
67-
b.min + uv .* (b.max - b.min)
67+
minimum(b) + uv .* (maximum(b) - minimum(b))
6868
end

src/geometries/primitives/disk.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ function (d::Disk)(ρ, φ)
3838
throw(DomainError((ρ, φ), "d(ρ, φ) is not defined for ρ, φ outside [0, 1]²."))
3939
end
4040
T = numtype(lentype(d))
41-
r = d.radius
42-
l = T(ρ) * r
41+
l = T(ρ) * radius(d)
4342
sφ, cφ = sincospi(2 * T(φ))
4443
u = ustrip(l * cφ)
4544
v = ustrip(l * sφ)
46-
d.plane(u, v)
45+
plane(d)(u, v)
4746
end

src/geometries/primitives/ellipsoid.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,23 @@ center(e::Ellipsoid) = e.center
3232

3333
rotation(e::Ellipsoid) = e.rotation
3434

35-
==(e₁::Ellipsoid, e₂::Ellipsoid) = e₁.radii == e₂.radii && e₁.center == e₂.center && e₁.rotation == e₂.rotation
35+
==(e₁::Ellipsoid, e₂::Ellipsoid) = radii(e₁) == radii(e₂) && center(e₁) == center(e₂) && rotation(e₁) == rotation(e₂)
3636

3737
function Base.isapprox(e₁::Ellipsoid, e₂::Ellipsoid; atol=atol(lentype(e₁)), kwargs...)
3838
u = Unitful.promote_unit(unit(lentype(e₁)), unit(lentype(e₂)))
39-
all(isapprox(r₁, r₂; atol, kwargs...) for (r₁, r₂) in zip(e₁.radii, e₂.radii)) &&
40-
isapprox(e₁.center, e₂.center; atol, kwargs...) &&
41-
isapprox(e₁.rotation, e₂.rotation; atol=ustrip(u, atol), kwargs...)
39+
all(isapprox(r₁, r₂; atol, kwargs...) for (r₁, r₂) in zip(radii(e₁), radii(e₂))) &&
40+
isapprox(center(e₁), center(e₂); atol, kwargs...) &&
41+
isapprox(rotation(e₁), rotation(e₂); atol=ustrip(u, atol), kwargs...)
4242
end
4343

4444
function (e::Ellipsoid)(θ, φ)
4545
if< 0 || θ > 1) ||< 0 || φ > 1)
4646
throw(DomainError((θ, φ), "e(θ, φ) is not defined for θ, φ outside [0, 1]²."))
4747
end
4848
T = numtype(lentype(e))
49-
r = e.radii
50-
c = e.center
51-
R = e.rotation
49+
r = radii(e)
50+
c = center(e)
51+
R = rotation(e)
5252
sθ, cθ = sincospi(T(θ))
5353
sφ, cφ = sincospi(2 * T(φ))
5454
x = r[1] **

src/geometries/primitives/paraboloidsurface.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,18 @@ Return the focal axis, connecting the focus with the apex of the paraboloid.
8787
The axis is always aligned with the z direction.
8888
"""
8989
function axis(p::ParaboloidSurface)
90-
f = focallength(p)
9190
a = apex(p)
91+
f = focallength(p)
9292
Line(a, a + Vec(zero(f), zero(f), f))
9393
end
9494

9595
==(p₁::ParaboloidSurface, p₂::ParaboloidSurface) =
96-
p₁.apex == p₂.apex && p₁.radius == p₂.radius && p₁.focallength == p₂.focallength
96+
apex(p₁) == apex(p₂) && radius(p₁) == radius(p₂) && focallength(p₁) == focallength(p₂)
9797

9898
Base.isapprox(p₁::ParaboloidSurface, p₂::ParaboloidSurface; atol=atol(lentype(p₁)), kwargs...) =
9999
isapprox(apex(p₁), apex(p₂); atol, kwargs...) &&
100-
isapprox(focallength(p₁), focallength(p₂); atol, kwargs...) &&
101-
isapprox(radius(p₁), radius(p₂); atol, kwargs...)
100+
isapprox(radius(p₁), radius(p₂); atol, kwargs...) &&
101+
isapprox(focallength(p₁), focallength(p₂); atol, kwargs...)
102102

103103
function (p::ParaboloidSurface)(ρ, θ)
104104
if< 0 || ρ > 1)

src/geometries/primitives/sphere.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ center(s::Sphere) = s.center
6868

6969
radius(s::Sphere) = s.radius
7070

71-
==(s₁::Sphere, s₂::Sphere) = s₁.center == s₂.center && s₁.radius == s₂.radius
71+
==(s₁::Sphere, s₂::Sphere) = center(s₁) == center(s₂) && radius(s₁) == radius(s₂)
7272

7373
Base.isapprox(s₁::Sphere, s₂::Sphere; atol=atol(lentype(s₁)), kwargs...) =
74-
isapprox(s₁.center, s₂.center; atol, kwargs...) && isapprox(s₁.radius, s₂.radius; atol, kwargs...)
74+
isapprox(center(s₁), center(s₂); atol, kwargs...) && isapprox(radius(s₁), radius(s₂); atol, kwargs...)
7575

7676
(s::Sphere)(uv...) = Ball(center(s), radius(s))(1, uv...)

src/geometries/primitives/torus.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ direction(t::Torus) = t.direction
4949

5050
radii(t::Torus) = (t.major, t.minor)
5151

52-
axis(t::Torus) = Line(t.center, t.center + t.direction)
52+
axis(t::Torus) = Line(center(t), center(t) + direction(t))
5353

5454
==(t₁::Torus, t₂::Torus) =
55-
t₁.center == t₂.center && t₁.direction == t₂.direction && t₁.major == t₂.major && t₁.minor == t₂.minor
55+
center(t₁) == center(t₂) && direction(t₁) == direction(t₂) && t₁.major == t₂.major && t₁.minor == t₂.minor
5656

5757
Base.isapprox(t₁::Torus, t₂::Torus; atol=atol(lentype(t₁)), kwargs...) =
58-
isapprox(t₁.center, t₂.center; atol, kwargs...) &&
59-
isapprox(t₁.direction, t₂.direction; atol, kwargs...) &&
58+
isapprox(center(t₁), center(t₂); atol, kwargs...) &&
59+
isapprox(direction(t₁), direction(t₂); atol, kwargs...) &&
6060
isapprox(t₁.major, t₂.major; atol, kwargs...) &&
6161
isapprox(t₁.minor, t₂.minor; atol, kwargs...)
6262

@@ -66,7 +66,7 @@ function (t::Torus)(θ, φ)
6666
if< 0 || θ > 1) ||< 0 || φ > 1)
6767
throw(DomainError((θ, φ), "t(θ, φ) is not defined for θ, φ outside [0, 1]²."))
6868
end
69-
c, n⃗ = t.center, t.direction
69+
c, n⃗ = center(t), direction(t)
7070
R, r = t.major, t.minor
7171

7272
Q = urotbetween(Vec(zero(ℒ), zero(ℒ), oneunit(ℒ)), n⃗)

0 commit comments

Comments
 (0)