Skip to content

Adjust transforms of Grid #1081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/transforms/affine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ applycoord(t::Affine, v::Vec) = t.A * v

applycoord(t::Affine, b::Box) = TransformedGeometry(b, t)

applycoord(t::Affine, g::CartesianGrid) = TransformedGrid(g, t)
applycoord(t::Affine, g::RegularGrid) = TransformedGrid(g, t)

applycoord(t::Affine, g::RectilinearGrid) = TransformedGrid(g, t)

Expand Down
8 changes: 8 additions & 0 deletions src/transforms/lengthunit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ applycoord(t::LengthUnit, len::Len) = uconvert(t.unit, len)

applycoord(t::LengthUnit, lens::NTuple{Dim,Len}) where {Dim} = uconvert.(t.unit, lens)

function applycoord(t::LengthUnit, g::RegularGrid)
dims = size(g)
orig = applycoord(t, minimum(g))
spac = map(s -> applycoord(t, s), spacing(g))
offs = offset(g)
RegularGrid(dims, orig, spac, offs)
end

applycoord(t::LengthUnit, g::RectilinearGrid) = TransformedGrid(g, t)

applycoord(t::LengthUnit, g::StructuredGrid) = TransformedGrid(g, t)
Expand Down
6 changes: 2 additions & 4 deletions src/transforms/scale.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,14 @@ applycoord(t::Scale, p::ParaboloidSurface) = TransformedGeometry(p, t)

applycoord(t::Scale, tr::Torus) = TransformedGeometry(tr, t)

function applycoord(t::Scale, g::CartesianGrid)
function applycoord(t::Scale, g::RegularGrid)
dims = size(g)
orig = applycoord(t, minimum(g))
spac = t.factors .* spacing(g)
offs = offset(g)
CartesianGrid(dims, orig, spac, offs)
RegularGrid(dims, orig, spac, offs)
end

applycoord(t::Scale, g::RegularGrid) = TransformedGrid(g, t)

applycoord(t::Scale, g::RectilinearGrid) =
RectilinearGrid{manifold(g),crs(g)}(ntuple(i -> t.factors[i] * xyz(g)[i], paramdim(g)))

Expand Down
4 changes: 4 additions & 0 deletions src/transforms/shadow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ apply(t::Shadow, tr::Torus) = TransformedGeometry(tr, t), nothing

apply(t::Shadow, ct::CylindricalTrajectory) = apply(t, GeometrySet(collect(ct))), nothing

apply(t::Shadow, g::CartesianGrid) = _shadow(g, _sort(t.dims)), nothing

apply(t::Shadow, g::RegularGrid) = TransformedGrid(g, t), nothing

apply(t::Shadow, g::RectilinearGrid) = TransformedGrid(g, t), nothing

apply(t::Shadow, g::StructuredGrid) = TransformedGrid(g, t), nothing
Expand Down
4 changes: 2 additions & 2 deletions src/transforms/slice.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ _slice(d::Domain, inds) = view(d, inds)
_slice(g::Grid, inds::CartesianIndices) = getindex(g, inds)

_sliceinds(d::Domain, b) = indices(d, b)
_sliceinds(g::CartesianGrid, b) = cartesianrange(g, b)
_sliceinds(g::RectilinearGrid, b) = cartesianrange(g, b)
_sliceinds(g::OrthoRegularGrid, b) = cartesianrange(g, b)
_sliceinds(g::OrthoRectilinearGrid, b) = cartesianrange(g, b)
_sliceinds(g::Grid{🌐}, b::Box{🌐}) = cartesianrange(g, b)

function _slicebox(box::Box{𝔼{2}}, limits)
Expand Down
16 changes: 10 additions & 6 deletions src/transforms/translate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ applycoord(::Translate, v::Vec) = v
# SPECIAL CASES
# --------------

apply(t::Translate, g::RectilinearGrid) =
RectilinearGrid{manifold(g),crs(g)}(ntuple(i -> xyz(g)[i] .+ t.offsets[i], paramdim(g))), nothing
applycoord(t::Translate, g::RegularGrid) = TransformedGrid(g, t)

revert(t::Translate, g::RectilinearGrid, c) = first(apply(inverse(t), g))
applycoord(t::Translate, g::OrthoRegularGrid) = RegularGrid(size(g), applycoord(t, minimum(g)), spacing(g), offset(g))

apply(t::Translate, g::StructuredGrid) =
StructuredGrid{manifold(g),crs(g)}(ntuple(i -> XYZ(g)[i] .+ t.offsets[i], paramdim(g))), nothing
applycoord(t::Translate, g::RectilinearGrid) = TransformedGrid(g, t)

revert(t::Translate, g::StructuredGrid, c) = first(apply(inverse(t), g))
applycoord(t::Translate, g::OrthoRectilinearGrid) =
RectilinearGrid{manifold(g),crs(g)}(ntuple(i -> xyz(g)[i] .+ t.offsets[i], paramdim(g)))

applycoord(t::Translate, g::StructuredGrid) = TransformedGrid(g, t)

applycoord(t::Translate, g::OrthoStructuredGrid) =
StructuredGrid{manifold(g),crs(g)}(ntuple(i -> XYZ(g)[i] .+ t.offsets[i], paramdim(g)))
42 changes: 42 additions & 0 deletions test/transforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,16 @@ end
@test all(r .≈ [f(t), f(t)])
@test all(TB.revert(f, r, c) .≈ d)

# ------------
# REGULARGRID
# ------------

f = Translate(T(1), T(1))
d = RegularGrid((8, 8), Point(Polar(T(0), T(0))), (T(1), T(π / 4)))
r, c = TB.apply(f, d)
@test r ≈ SimpleMesh(f.(vertices(d)), topology(d))
@test TB.revert(f, r, c) ≈ d

# --------------
# CARTESIANGRID
# --------------
Expand All @@ -384,6 +394,13 @@ end
@test r ≈ RectilinearGrid(T.(1:11), T.(1:11))
@test TB.revert(f, r, c) ≈ d

f = Translate(T(1), T(1))
g = RegularGrid((8, 8), Point(Polar(T(0), T(0))), (T(1), T(π / 4)))
d = convert(RectilinearGrid, g)
r, c = TB.apply(f, d)
@test r ≈ SimpleMesh(f.(vertices(d)), topology(d))
@test TB.revert(f, r, c) ≈ d

# ---------------
# STRUCTUREDGRID
# ---------------
Expand All @@ -395,6 +412,13 @@ end
@test r ≈ StructuredGrid(repeat(T.(1:11), 1, 11), repeat(T.(1:11)', 11, 1))
@test TB.revert(f, r, c) ≈ d

f = Translate(T(1), T(1))
g = RegularGrid((8, 8), Point(Polar(T(0), T(0))), (T(1), T(π / 4)))
d = convert(StructuredGrid, g)
r, c = TB.apply(f, d)
@test r ≈ SimpleMesh(f.(vertices(d)), topology(d))
@test TB.revert(f, r, c) ≈ d

# -----------
# SIMPLEMESH
# -----------
Expand Down Expand Up @@ -1403,6 +1427,15 @@ end
r, c = TB.apply(f, d)
@test all(r .≈ [f(t), f(t)])

# ------------
# REGULARGRID
# ------------

f = LengthUnit(u"cm")
d = RegularGrid((8, 8), Point(Polar(T(1), T(0))), (T(1), T(π / 4)))
r, c = TB.apply(f, d)
@test r ≈ RegularGrid((8, 8), Point(Polar(T(100) * u"cm", T(0) * u"rad")), (T(100) * u"cm", T(π / 4) * u"rad"))

# --------------
# CARTESIANGRID
# --------------
Expand Down Expand Up @@ -1622,6 +1655,15 @@ end
r, c = TB.apply(f, d)
@test all(r .== [f(t), f(t)])

# ------------
# REGULARGRID
# ------------

f = Shadow(:yz)
d = RegularGrid((8, 8, 8), Point(Cylindrical(T(0), T(0), T(0))), (T(1), T(π / 4), T(1)))
r, c = TB.apply(f, d)
@test r == SimpleMesh(f.(vertices(d)), topology(d))

# --------------
# CARTESIANGRID
# --------------
Expand Down
Loading