Skip to content

Commit d5c1293

Browse files
committed
Define CrossSection; simplify plotting.jl accordingly
1 parent 85bf897 commit d5c1293

File tree

3 files changed

+48
-50
lines changed

3 files changed

+48
-50
lines changed

Diff for: src/planar/cross_section.jl

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
export CrossSection
2+
3+
mutable struct CrossSection{S<:Shape3} <: Shape2
4+
shp::S # 3D shape
5+
p::S²Float{3,9} # projection matrix to cross-sectional coordinates; must be orthonormal; 3rd entry of projection is along normal axis
6+
c::Float # intercept on axis normal to cross section
7+
end
8+
9+
CrossSection(shp::S,
10+
n::SReal{3},
11+
c::Real
12+
) where {S<:Shape3} =
13+
(n̂ = normalize(n); CrossSection{S}(shp, [orthoaxes(n̂)... n̂]', c))
14+
15+
CrossSection(shp::Shape3, n::AbsVecReal, c::Real) = CrossSection(shp, SVec{3}(n), c)
16+
17+
function (shp::Shape3)(ax::Symbol, c::Real)
18+
ax==:x || ax==:y || ax==:z || @error "ax = $(ax) should be :x or :y or :z."
19+
20+
ind_n̂ = (ax==:x) + 2(ax==:y) + 3(ax==:z) # ind_n̂ = 1, 2, 3 for ax = :x, :y, :z
21+
= SVec(ntuple(identity,Val(3))) .== ind_n̂
22+
23+
return CrossSection(shp, n̂, c)
24+
end
25+
26+
coord3d(x::SReal{2}, s::CrossSection) = (y = SFloat{3}(x.data..., s.c); s.p' * y)
27+
28+
level(x::SReal{2}, s::CrossSection) = level(coord3d(x,s), s.shp)
29+
30+
function surfpt_nearby(x::SReal{2}, s::CrossSection)
31+
pt, nout = surfpt_nearby(coord3d(x,s), s.shp)
32+
33+
uv = SVec(1,2)
34+
pt2 = (s.p * pt)[uv]
35+
nout2 = normalize((s.p * nout)[uv])
36+
37+
return pt2, nout2
38+
end
39+
40+
translate(s::CrossSection, ∆::SReal{2}) = CrossSection(translate(s.shp, coord3d(∆)), s.p, s.c)
41+
42+
# This does not create the tightest bounds. For the tightest bounds, this function should
43+
# be implemented for CrossSection{S} for each S<:Shape3.
44+
function bounds(s::CrossSection)
45+
bₙ, bₚ = bounds(s.shp)
46+
return s.p * bₙ, s.p * bₚ
47+
end

Diff for: src/planar/planar.jl

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
include("cross_section.jl")
12
include("polygon.jl")
23
include("special.jl")
34
include("sector.jl")

Diff for: src/util/plotting.jl

-50
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,6 @@ function Makie.plot!(ds::DrawShape{<:Tuple{Shape2}})
3030
return ds
3131
end
3232

33-
# Implement drawshape!() for 3D shapes.
34-
function Makie.plot!(ds::DrawShape{<:Tuple{Shape3,Tuple{Symbol,Real}}})
35-
res₀ = ds.res.val # fields found by examining typeof(ds) and fieldnames(typeof(ds)), etc
36-
hres = ds.hres.val
37-
vres = ds.vres.val
38-
39-
# If hres and vres are set to valid values, use them; otherwise use res₀.
40-
hres1 && (hres = res₀)
41-
vres1 && (vres = res₀)
42-
43-
hres>1 || @error "hres = $hres should at least 2."
44-
vres>1 || @error "vres = $vres should at least 2."
45-
46-
shp = ds[1]
47-
cs = ds[2]
48-
res = (hres, vres)
49-
50-
# Makie.convert_arguments() defined below handles this new signature.
51-
contour!(ds, shp, cs, res, levels=SVec(0.0); ds.attributes...)
52-
53-
return ds
54-
end
55-
5633
# Define the new signature of contour!() used in drawshape!() for 2D shapes.
5734
function Makie.convert_arguments(P::SurfaceLike, shp::Shape2, res::Tuple2{Integer})
5835
lower, upper = bounds(shp)
@@ -66,30 +43,3 @@ function Makie.convert_arguments(P::SurfaceLike, shp::Shape2, res::Tuple2{Intege
6643

6744
return convert_arguments(P, xs, ys, lvs)
6845
end
69-
70-
# Define the new signature of contour!() used in drawshape!() for 3D shapes.
71-
function Makie.convert_arguments(P::SurfaceLike, shp::Shape3,
72-
cs::Tuple{Symbol,Real}, # (:x or :y or :z, intercept): cross section spec
73-
res::Tuple2{Integer})
74-
ax, cept = cs # axis normal to cross section, intercept
75-
76-
ax==:x || ax==:y || ax==:z || @error "cs[1] = $(cs[1]) should be :x or :y or :z."
77-
nw = (ax==:x) + 2(ax==:y) + 3(ax==:z) # nw = 1, 2, 3 for ax = :x, :y, :z
78-
nu, nv = mod1(nw+1,3), mod1(nw+2,3)
79-
80-
# Set the unit vectors along the u-, v-, w-axes.
81-
= SVec(ntuple(identity,Val(3))) .== nu
82-
= SVec(ntuple(identity,Val(3))) .== nv
83-
= SVec(ntuple(identity,Val(3))) .== nw
84-
85-
lower, upper = bounds(shp)
86-
= upper - lower
87-
88-
ϵrel = EPS_REL
89-
us = range(lower[nu] - ϵrel*∆[nu], upper[nu] + ϵrel*∆[nu], length=res[1])
90-
vs = range(lower[nv] - ϵrel*∆[nv], upper[nv] + ϵrel*∆[nv], length=res[2])
91-
92-
lvs = [level(u*+ v*+ cept*ŵ, shp) for u = us, v = vs]
93-
94-
return convert_arguments(P, us, vs, lvs)
95-
end

0 commit comments

Comments
 (0)