Skip to content

Commit 1babc12

Browse files
eliascarvsouma4
authored andcommitted
Adjust transforms of Grid (JuliaGeometry#1081)
* Adjust transforms of 'Grid' * Add more tests * Apply suggestions
1 parent c9cc721 commit 1babc12

File tree

7 files changed

+69
-13
lines changed

7 files changed

+69
-13
lines changed

src/transforms/affine.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ applycoord(t::Affine, v::Vec) = t.A * v
6363

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

66-
applycoord(t::Affine, g::CartesianGrid) = TransformedGrid(g, t)
66+
applycoord(t::Affine, g::RegularGrid) = TransformedGrid(g, t)
6767

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

src/transforms/lengthunit.jl

+8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ applycoord(t::LengthUnit, len::Len) = uconvert(t.unit, len)
3232

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

35+
function applycoord(t::LengthUnit, g::RegularGrid)
36+
dims = size(g)
37+
orig = applycoord(t, minimum(g))
38+
spac = map(s -> applycoord(t, s), spacing(g))
39+
offs = offset(g)
40+
RegularGrid(dims, orig, spac, offs)
41+
end
42+
3543
applycoord(t::LengthUnit, g::RectilinearGrid) = TransformedGrid(g, t)
3644

3745
applycoord(t::LengthUnit, g::StructuredGrid) = TransformedGrid(g, t)

src/transforms/scale.jl

+2-4
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,14 @@ applycoord(t::Scale, p::ParaboloidSurface) = TransformedGeometry(p, t)
6868

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

71-
function applycoord(t::Scale, g::CartesianGrid)
71+
function applycoord(t::Scale, g::RegularGrid)
7272
dims = size(g)
7373
orig = applycoord(t, minimum(g))
7474
spac = t.factors .* spacing(g)
7575
offs = offset(g)
76-
CartesianGrid(dims, orig, spac, offs)
76+
RegularGrid(dims, orig, spac, offs)
7777
end
7878

79-
applycoord(t::Scale, g::RegularGrid) = TransformedGrid(g, t)
80-
8179
applycoord(t::Scale, g::RectilinearGrid) =
8280
RectilinearGrid{manifold(g),crs(g)}(ntuple(i -> t.factors[i] * xyz(g)[i], paramdim(g)))
8381

src/transforms/shadow.jl

+4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ apply(t::Shadow, tr::Torus) = TransformedGeometry(tr, t), nothing
6363

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

66+
apply(t::Shadow, g::CartesianGrid) = _shadow(g, _sort(t.dims)), nothing
67+
68+
apply(t::Shadow, g::RegularGrid) = TransformedGrid(g, t), nothing
69+
6670
apply(t::Shadow, g::RectilinearGrid) = TransformedGrid(g, t), nothing
6771

6872
apply(t::Shadow, g::StructuredGrid) = TransformedGrid(g, t), nothing

src/transforms/slice.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ _slice(d::Domain, inds) = view(d, inds)
4141
_slice(g::Grid, inds::CartesianIndices) = getindex(g, inds)
4242

4343
_sliceinds(d::Domain, b) = indices(d, b)
44-
_sliceinds(g::CartesianGrid, b) = cartesianrange(g, b)
45-
_sliceinds(g::RectilinearGrid, b) = cartesianrange(g, b)
44+
_sliceinds(g::OrthoRegularGrid, b) = cartesianrange(g, b)
45+
_sliceinds(g::OrthoRectilinearGrid, b) = cartesianrange(g, b)
4646
_sliceinds(g::Grid{🌐}, b::Box{🌐}) = cartesianrange(g, b)
4747

4848
function _slicebox(box::Box{𝔼{2}}, limits)

src/transforms/translate.jl

+10-6
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ applycoord(::Translate, v::Vec) = v
3737
# SPECIAL CASES
3838
# --------------
3939

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

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

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

48-
revert(t::Translate, g::StructuredGrid, c) = first(apply(inverse(t), g))
46+
applycoord(t::Translate, g::OrthoRectilinearGrid) =
47+
RectilinearGrid{manifold(g),crs(g)}(ntuple(i -> xyz(g)[i] .+ t.offsets[i], paramdim(g)))
48+
49+
applycoord(t::Translate, g::StructuredGrid) = TransformedGrid(g, t)
50+
51+
applycoord(t::Translate, g::OrthoStructuredGrid) =
52+
StructuredGrid{manifold(g),crs(g)}(ntuple(i -> XYZ(g)[i] .+ t.offsets[i], paramdim(g)))

test/transforms.jl

+42
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,16 @@ end
362362
@test all(r .≈ [f(t), f(t)])
363363
@test all(TB.revert(f, r, c) .≈ d)
364364

365+
# ------------
366+
# REGULARGRID
367+
# ------------
368+
369+
f = Translate(T(1), T(1))
370+
d = RegularGrid((8, 8), Point(Polar(T(0), T(0))), (T(1), T/ 4)))
371+
r, c = TB.apply(f, d)
372+
@test r SimpleMesh(f.(vertices(d)), topology(d))
373+
@test TB.revert(f, r, c) d
374+
365375
# --------------
366376
# CARTESIANGRID
367377
# --------------
@@ -384,6 +394,13 @@ end
384394
@test r RectilinearGrid(T.(1:11), T.(1:11))
385395
@test TB.revert(f, r, c) d
386396

397+
f = Translate(T(1), T(1))
398+
g = RegularGrid((8, 8), Point(Polar(T(0), T(0))), (T(1), T/ 4)))
399+
d = convert(RectilinearGrid, g)
400+
r, c = TB.apply(f, d)
401+
@test r SimpleMesh(f.(vertices(d)), topology(d))
402+
@test TB.revert(f, r, c) d
403+
387404
# ---------------
388405
# STRUCTUREDGRID
389406
# ---------------
@@ -395,6 +412,13 @@ end
395412
@test r StructuredGrid(repeat(T.(1:11), 1, 11), repeat(T.(1:11)', 11, 1))
396413
@test TB.revert(f, r, c) d
397414

415+
f = Translate(T(1), T(1))
416+
g = RegularGrid((8, 8), Point(Polar(T(0), T(0))), (T(1), T/ 4)))
417+
d = convert(StructuredGrid, g)
418+
r, c = TB.apply(f, d)
419+
@test r SimpleMesh(f.(vertices(d)), topology(d))
420+
@test TB.revert(f, r, c) d
421+
398422
# -----------
399423
# SIMPLEMESH
400424
# -----------
@@ -1403,6 +1427,15 @@ end
14031427
r, c = TB.apply(f, d)
14041428
@test all(r .≈ [f(t), f(t)])
14051429

1430+
# ------------
1431+
# REGULARGRID
1432+
# ------------
1433+
1434+
f = LengthUnit(u"cm")
1435+
d = RegularGrid((8, 8), Point(Polar(T(1), T(0))), (T(1), T/ 4)))
1436+
r, c = TB.apply(f, d)
1437+
@test r RegularGrid((8, 8), Point(Polar(T(100) * u"cm", T(0) * u"rad")), (T(100) * u"cm", T/ 4) * u"rad"))
1438+
14061439
# --------------
14071440
# CARTESIANGRID
14081441
# --------------
@@ -1622,6 +1655,15 @@ end
16221655
r, c = TB.apply(f, d)
16231656
@test all(r .== [f(t), f(t)])
16241657

1658+
# ------------
1659+
# REGULARGRID
1660+
# ------------
1661+
1662+
f = Shadow(:yz)
1663+
d = RegularGrid((8, 8, 8), Point(Cylindrical(T(0), T(0), T(0))), (T(1), T/ 4), T(1)))
1664+
r, c = TB.apply(f, d)
1665+
@test r == SimpleMesh(f.(vertices(d)), topology(d))
1666+
16251667
# --------------
16261668
# CARTESIANGRID
16271669
# --------------

0 commit comments

Comments
 (0)