Skip to content

Commit da435e1

Browse files
authored
Miscellaneous improvements (#1230)
* Refactor primitives * More refactoring
1 parent f10a253 commit da435e1

File tree

8 files changed

+39
-84
lines changed

8 files changed

+39
-84
lines changed

src/geometries/primitives/ball.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ function (b::Ball{𝔼{2}})(ρ, φ)
5050
end
5151

5252
function (b::Ball{𝔼{3}})(ρ, θ, φ)
53-
T = numtype(lentype(b))
5453
if< 0 || ρ > 1) ||< 0 || θ > 1) ||< 0 || φ > 1)
5554
throw(DomainError((ρ, θ, φ), "b(ρ, θ, φ) is not defined for ρ, θ, φ outside [0, 1]³."))
5655
end
56+
T = numtype(lentype(b))
5757
c = b.center
5858
r = b.radius
5959
ρ′ = T(ρ) * r

src/geometries/primitives/circle.jl

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,13 @@ paramdim(::Type{<:Circle}) = 1
4343

4444
plane(c::Circle) = c.plane
4545

46-
center(c::Circle) = c.plane(0, 0)
47-
4846
radius(c::Circle) = c.radius
4947

50-
==(c₁::Circle, c₂::Circle) = c₁.plane == c₂.plane && c₁.radius == c₂.radius
48+
center(c::Circle) = plane(c)(0, 0)
49+
50+
==(c₁::Circle, c₂::Circle) = plane(c₁) == plane(c₂) && radius(c₁) == radius(c₂)
5151

5252
Base.isapprox(c₁::Circle, c₂::Circle; atol=atol(lentype(c₁)), kwargs...) =
53-
isapprox(c₁.plane, c₂.plane; atol, kwargs...) && isapprox(c₁.radius, c₂.radius; atol, kwargs...)
53+
isapprox(plane(c₁), plane(c₂); atol, kwargs...) && isapprox(radius(c₁), radius(c₂); atol, kwargs...)
5454

55-
function (c::Circle)(φ)
56-
if< 0 || φ > 1)
57-
throw(DomainError(φ, "c(φ) is not defined for φ outside [0, 1]."))
58-
end
59-
T = numtype(lentype(c))
60-
r = c.radius
61-
l = r
62-
sφ, cφ = sincospi(2 * T(φ))
63-
u = ustrip(l * cφ)
64-
v = ustrip(l * sφ)
65-
c.plane(u, v)
66-
end
55+
(c::Circle)(φ) = Disk(plane(c), radius(c))(1, φ)

src/geometries/primitives/cone.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ base(c::Cone) = c.base
2626

2727
apex(c::Cone) = c.apex
2828

29-
height(c::Cone) = norm(center(base(c)) - apex(c))
29+
height(c::Cone) = height(boundary(c))
3030

31-
halfangle(c::Cone) = atan(radius(base(c)), height(c))
31+
halfangle(c::Cone) = halfangle(boundary(c))
3232

3333
==(c₁::Cone, c₂::Cone) = boundary(c₁) == boundary(c₂)
3434

@@ -39,7 +39,7 @@ function (c::Cone)(r, φ, h)
3939
if (r < 0 || r > 1) ||< 0 || φ > 1) || (h < 0 || h > 1)
4040
throw(DomainError((r, φ, h), "c(r, φ, h) is not defined for r, φ, h outside [0, 1]³."))
4141
end
42-
a = c.apex
43-
b = c.base(r, φ)
42+
b = base(c)(r, φ)
43+
a = apex(c)
4444
Segment(b, a)(h)
4545
end

src/geometries/primitives/conesurface.jl

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,13 @@ base(c::ConeSurface) = c.base
2626

2727
apex(c::ConeSurface) = c.apex
2828

29-
==(c₁::ConeSurface, c₂::ConeSurface) = c₁.base == c₂.base && c₁.apex == c₂.apex
29+
height(c::ConeSurface) = norm(center(base(c)) - apex(c))
30+
31+
halfangle(c::ConeSurface) = atan(radius(base(c)), height(c))
32+
33+
==(c₁::ConeSurface, c₂::ConeSurface) = base(c₁) == base(c₂) && apex(c₁) == apex(c₂)
3034

3135
Base.isapprox(c₁::ConeSurface, c₂::ConeSurface; atol=atol(lentype(c₁)), kwargs...) =
32-
isapprox(c₁.base, c₂.base; atol, kwargs...) && isapprox(c₁.apex, c₂.apex; atol, kwargs...)
33-
34-
function (c::ConeSurface)(φ, h)
35-
if< 0 || φ > 1) || (h < 0 || h > 1)
36-
throw(DomainError((φ, h), "c(φ, h) is not defined for φ, h outside [0, 1]²."))
37-
end
38-
T = numtype(lentype(c))
39-
a = c.apex
40-
b = c.base(one(T), φ)
41-
Segment(b, a)(h)
42-
end
36+
isapprox(base(c₁), base(c₂); atol, kwargs...) && isapprox(apex(c₁), apex(c₂); atol, kwargs...)
37+
38+
(c::ConeSurface)(φ, h) = Cone(base(c), apex(c))(1, φ, h)

src/geometries/primitives/cylinder.jl

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ top(c::Cylinder) = c.top
6262

6363
radius(c::Cylinder) = c.radius
6464

65+
axis(c::Cylinder) = axis(boundary(c))
66+
67+
isright(c::Cylinder) = isright(boundary(c))
68+
69+
hasintersectingplanes(c::Cylinder) = hasintersectingplanes(boundary(c))
70+
71+
==(c₁::Cylinder, c₂::Cylinder) = boundary(c₁) == boundary(c₂)
72+
73+
Base.isapprox(c₁::Cylinder, c₂::Cylinder; atol=atol(lentype(c₁)), kwargs...) =
74+
isapprox(boundary(c₁), boundary(c₂); atol, kwargs...)
75+
6576
function (c::Cylinder)(ρ, φ, z)
6677
if< 0 || ρ > 1) ||< 0 || φ > 1) || (z < 0 || z > 1)
6778
throw(DomainError((ρ, φ, z), "c(ρ, φ, z) is not defined for ρ, φ, z outside [0, 1]³."))
@@ -89,18 +100,3 @@ function (c::Cylinder)(ρ, φ, z)
89100
s = Segment(l b, l t)
90101
s(T(z))
91102
end
92-
93-
# ----------------------------------------------
94-
# forward methods to boundary (CylinderSurface)
95-
# ----------------------------------------------
96-
97-
axis(c::Cylinder) = axis(boundary(c))
98-
99-
isright(c::Cylinder) = isright(boundary(c))
100-
101-
hasintersectingplanes(c::Cylinder) = hasintersectingplanes(boundary(c))
102-
103-
==(c₁::Cylinder, c₂::Cylinder) = boundary(c₁) == boundary(c₂)
104-
105-
Base.isapprox(c₁::Cylinder, c₂::Cylinder; atol=atol(lentype(c₁)), kwargs...) =
106-
isapprox(boundary(c₁), boundary(c₂); atol, kwargs...)

src/geometries/primitives/disk.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@ paramdim(::Type{<:Disk}) = 2
2222

2323
plane(d::Disk) = d.plane
2424

25-
center(d::Disk) = d.plane(0, 0)
26-
2725
radius(d::Disk) = d.radius
2826

29-
normal(d::Disk) = normal(d.plane)
27+
center(d::Disk) = center(boundary(d))
28+
29+
normal(d::Disk) = normal(plane(d))
3030

31-
==(d₁::Disk, d₂::Disk) = d₁.plane == d₂.plane && d₁.radius == d₂.radius
31+
==(d₁::Disk, d₂::Disk) = boundary(d₁) == boundary(d₂)
3232

3333
Base.isapprox(d₁::Disk, d₂::Disk; atol=atol(lentype(d₁)), kwargs...) =
34-
isapprox(d₁.plane, d₂.plane; atol, kwargs...) && isapprox(d₁.radius, d₂.radius; atol, kwargs...)
34+
isapprox(boundary(d₁), boundary(d₂); atol, kwargs...)
3535

3636
function (d::Disk)(ρ, φ)
37-
T = numtype(lentype(d))
3837
if< 0 || ρ > 1) ||< 0 || φ > 1)
3938
throw(DomainError((ρ, φ), "d(ρ, φ) is not defined for ρ, φ outside [0, 1]²."))
4039
end
40+
T = numtype(lentype(d))
4141
r = d.radius
4242
l = T(ρ) * r
4343
sφ, cφ = sincospi(2 * T(φ))

src/geometries/primitives/ellipsoid.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ function Base.isapprox(e₁::Ellipsoid, e₂::Ellipsoid; atol=atol(lentype(e₁)
4242
end
4343

4444
function (e::Ellipsoid)(θ, φ)
45-
T = numtype(lentype(e))
4645
if< 0 || θ > 1) ||< 0 || φ > 1)
4746
throw(DomainError((θ, φ), "e(θ, φ) is not defined for θ, φ outside [0, 1]²."))
4847
end
48+
T = numtype(lentype(e))
4949
r = e.radii
5050
c = e.center
5151
R = e.rotation

src/geometries/primitives/frustumsurface.jl

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,9 @@ height(f::FrustumSurface) = norm(center(bottom(f)) - center(top(f)))
3636

3737
axis(f::FrustumSurface) = Line(center(bottom(f)), center(top(f)))
3838

39-
==(f₁::FrustumSurface, f₂::FrustumSurface) = f₁.bot == f₂.bot && f₁.top == f₂.top
39+
==(f₁::FrustumSurface, f₂::FrustumSurface) = bottom(f₁) == bottom(f₂) && top(f₁) == top(f₂)
4040

4141
Base.isapprox(f₁::FrustumSurface, f₂::FrustumSurface; atol=atol(lentype(f₁)), kwargs...) =
42-
isapprox(f₁.bot, f₂.bot; atol, kwargs...) && isapprox(f₁.top, f₂.top; atol, kwargs...)
42+
isapprox(bottom(f₁), bottom(f₂); atol, kwargs...) && isapprox(top(f₁), top(f₂); atol, kwargs...)
4343

44-
function (f::FrustumSurface)(φ, z)
45-
= lentype(f)
46-
T = numtype(ℒ)
47-
if< 0 || φ > 1) || (z < 0 || z > 1)
48-
throw(DomainError((φ, z), "f(φ, z) is not defined for φ, z outside [0, 1]²."))
49-
end
50-
rb = radius(bottom(f))
51-
rt = radius(top(f))
52-
a = axis(f)
53-
d = a(1) - a(0)
54-
l = norm(d)
55-
56-
# rotation to align z axis with cylinder axis
57-
Q = urotbetween(d, Vec(zero(ℒ), zero(ℒ), oneunit(ℒ)))
58-
59-
# scale coordinates
60-
φₛ = 2T(π) * φ
61-
zₛ = z * l
62-
63-
# local coordinates, that will be transformed with rotation and position of the FrustumSurface
64-
x = cos(φₛ) * (rb * (l - zₛ) + rt * zₛ) / l
65-
y = sin(φₛ) * (rb * (l - zₛ) + rt * zₛ) / l
66-
z = zₛ
67-
p = Vec(x, y, z)
68-
69-
center(bottom(f)) + Q' * p
70-
end
44+
(f::FrustumSurface)(φ, z) = Frustum(bottom(f), top(f))(1, φ, z)

0 commit comments

Comments
 (0)