Skip to content

Commit

Permalink
Add Grid utility function bounding_box (#880)
Browse files Browse the repository at this point in the history
  • Loading branch information
lijas authored Feb 6, 2024
1 parent 73c2375 commit fa48d1e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ more discussion).

- VTK export now work with `QuadraticHexahedron` elements. ([#714][github-714])

- The function `bounding_box(::AbstractGrid)` has been added. It computes the bounding box for
a given grid (based on its node coordinates), and returns the minimum and maximum vertices
of the bounding box. ([#880][github-880])

### Changed

- `create_sparsity_pattern` now supports cross-element dof coupling by passing kwarg
Expand Down Expand Up @@ -891,3 +895,5 @@ poking into Ferrite internals:
[github-779]: https://github.com/Ferrite-FEM/Ferrite.jl/pull/779
[github-835]: https://github.com/Ferrite-FEM/Ferrite.jl/pull/835
[github-855]: https://github.com/Ferrite-FEM/Ferrite.jl/pull/855
[github-880]: https://github.com/Ferrite-FEM/Ferrite.jl/pull/880

22 changes: 22 additions & 0 deletions src/Grid/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,25 @@ for (func, entity_f, entity_t, filter_f,
end
end
end



"""
bounding_box(grid::AbstractGrid)
Computes the axis-aligned bounding box for a given grid, based on its node coordinates.
Returns the minimum and maximum vertex coordinates of the bounding box.
"""
function bounding_box(grid::AbstractGrid{dim}) where {dim}
T = get_coordinate_eltype(grid)
min_vertex = Vec{dim}(i->typemax(T))
max_vertex = Vec{dim}(i->typemin(T))
for node in getnodes(grid)
x = get_node_coordinate(node)
_max_tmp = max_vertex # avoid type instability
_min_tmp = min_vertex
max_vertex = Vec{dim}(i -> max(x[i], _max_tmp[i]))
min_vertex = Vec{dim}(i -> min(x[i], _min_tmp[i]))
end
return min_vertex, max_vertex
end
4 changes: 4 additions & 0 deletions test/test_abstractgrid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@
@test Ferrite.ndofs(dhs[1]) == Ferrite.ndofs(dhs[2])
@test isapprox(u1,u2,atol=1e-8)

minv, maxv = Ferrite.bounding_box(subtype_grid)
@test minv Vec((-1.0,-1.0))
@test maxv Vec((+1.0,+1.0))

colors1 = Ferrite.create_coloring(subtype_grid, alg = ColoringAlgorithm.WorkStream)
colors2 = Ferrite.create_coloring(reference_grid, alg = ColoringAlgorithm.WorkStream)
@test all(colors1 .== colors2)
Expand Down
4 changes: 4 additions & 0 deletions test/test_grid_dofhandler_vtk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ end
rm(dofhandlerfilename*".vtu")
end

minv, maxv = Ferrite.bounding_box(grid)
@test minv 2left
@test maxv 2right

end

end # of testset
Expand Down

0 comments on commit fa48d1e

Please sign in to comment.