Skip to content

Commit

Permalink
SimpleMesh: Implement multidimensional indexing (#671)
Browse files Browse the repository at this point in the history
* SimpleMesh: Implement multidimensional indexing

* Fix typo

* Apply suggestions
  • Loading branch information
eliascarv authored Dec 26, 2023
1 parent a26b24a commit 0b92a01
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/mesh/simplemesh.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,12 @@ vertex(m::SimpleMesh, ind::Int) = m.vertices[ind]
vertices(m::SimpleMesh) = m.vertices

nvertices(m::SimpleMesh) = length(m.vertices)

function Base.getindex(m::SimpleMesh{Dim,T,V,GridTopology{Dim}}, I::CartesianIndices{Dim}) where {Dim,T,V}
dims = size(I)
odims = size(m)
cinds = first(I):CartesianIndex(Tuple(last(I)) .+ 1)
inds = vec(LinearIndices(odims .+ 1)[cinds])
periodic = isperiodic(topology(m)) .&& dims .== odims
SimpleMesh(m.vertices[inds], GridTopology(dims, periodic))
end
4 changes: 2 additions & 2 deletions src/mesh/structuredgrid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ 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)
cinds = first(I):CartesianIndex(Tuple(last(I)) .+ 1)
XYZ = ntuple(i -> g.XYZ[i][cinds], 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 @@ -432,6 +432,14 @@
@test vertex(mesh, nvertices(mesh)) == vertex(mesh, size(mesh) .+ 1)
@test mesh[1, 1] == mesh[1]
@test mesh[10, 10] == mesh[100]
sub = mesh[2:4, 3:7]
@test size(sub) == (3, 5)
@test minimum(sub) == P2(1, 2)
@test maximum(sub) == P2(4, 7)
sub = mesh[2, 3:7]
@test size(sub) == (1, 5)
@test minimum(sub) == P2(1, 2)
@test maximum(sub) == P2(2, 7)

# test for https://github.com/JuliaGeometry/Meshes.jl/issues/261
points = rand(P2, 5)
Expand Down

0 comments on commit 0b92a01

Please sign in to comment.