Skip to content

Commit

Permalink
RectilinearGrid: Implement multidimensional indexing (#669)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliascarv authored Dec 26, 2023
1 parent a2ca0f2 commit 9e99530
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/mesh/rectilineargrid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,11 @@ function centroid(g::RectilinearGrid, ind::Int)
p2 = vertex(g, ijk .+ 1)
Point((coordinates(p1) + coordinates(p2)) / 2)
end

function Base.getindex(g::RectilinearGrid{Dim}, I::CartesianIndices{Dim}) where {Dim}
dims = size(I)
start = Tuple(first(I))
stop = Tuple(last(I)) .+ 1
xyz = ntuple(i -> g.xyz[i][start[i]:stop[i]], Dim)
RectilinearGrid(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 @@ -238,6 +238,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)
@test Meshes.XYZ(grid) == (repeat(x, 1, 6), repeat(y', 6, 1))

Expand Down

0 comments on commit 9e99530

Please sign in to comment.