Skip to content

Commit

Permalink
StructuredGrid: Implement multidimensional indexing (#670)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliascarv authored Dec 26, 2023
1 parent 9e99530 commit 9bc3ba6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/mesh/structuredgrid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,10 @@ StructuredGrid(XYZ...) = StructuredGrid(XYZ)
vertex(g::StructuredGrid{Dim}, ijk::Dims{Dim}) where {Dim} = Point(ntuple(d -> g.XYZ[d][ijk...], Dim))

XYZ(g::StructuredGrid) = g.XYZ

function Base.getindex(g::StructuredGrid{Dim}, I::CartesianIndices{Dim}) where {Dim}
dims = size(I)
range = first(I):CartesianIndex(Tuple(last(I)) .+ 1)
XYZ = ntuple(i -> g.XYZ[i][range], Dim)
StructuredGrid(XYZ, GridTopology(dims))
end
8 changes: 8 additions & 0 deletions test/mesh.jl
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,14 @@
@test vertex(grid, nvertices(grid)) == vertex(grid, size(grid) .+ 1)
@test grid[1, 1] == grid[1]
@test grid[5, 5] == grid[25]
sub = grid[2:4, 3:5]
@test size(sub) == (3, 3)
@test minimum(sub) == P2(0.2, 0.3)
@test maximum(sub) == P2(0.8, 1.0)
sub = grid[2, 3:5]
@test size(sub) == (1, 3)
@test minimum(sub) == P2(0.2, 0.3)
@test maximum(sub) == P2(0.4, 1.0)
@test Meshes.XYZ(grid) == (X, Y)

# conversion
Expand Down

0 comments on commit 9bc3ba6

Please sign in to comment.