-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* include new methods for clamping points to boxes * avoid internal fields, use patch version, update test for complete coverage --------- Co-authored-by: Mark Baum <>
- Loading branch information
Showing
8 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Clamping | ||
|
||
Meshes adds methods to Julia's built-in `clamp` function. The additional methods clamp points to the edges of a box in any number of dimensions. The target points and boxes must have the same number of dimensions and the same numeric type. | ||
|
||
```@docs | ||
clamp(point::Point{Dim,T}, box::Box{Dim,T}) where {Dim,T} | ||
clamp(points::PointSet{Dim,T}, box::Box{Dim,T}) where {Dim,T} | ||
``` | ||
|
||
```@example clamping | ||
using Meshes # hide | ||
import CairoMakie as Mke # hide | ||
# set of 2D points to clamp | ||
points = PointSet(rand(2, 100)) | ||
# 2D box defining the clamping boundaries | ||
box = Box((0.25, 0.25), (0.75, 0.75)) | ||
# clamp point coordinates to the box edges | ||
clamped = clamp(points, box) | ||
fig = Mke.Figure(size=(800, 400)) | ||
ax = Mke.Axis(fig[1,1], title="unclamped", aspect=1, limits=(0,1,0,1)) | ||
viz!(ax, box) | ||
viz!(ax, points, color=:black, pointsize=6) | ||
ax = Mke.Axis(fig[1,2], title="clamped", aspect=1, limits=(0,1,0,1)) | ||
viz!(ax, box) | ||
viz!(ax, clamped, color=:black, pointsize=6) | ||
fig | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
""" | ||
clamp(point, box) | ||
Clamp the coordinates of a [`Point`](@ref) to the edges of a [`Box`](@ref). For each dimension, coordinates outside of the box are moved to the nearest edge of the box. The point and box must have an equal number of dimensions.""" | ||
function Base.clamp(point::Point{Dim,T}, box::Box{Dim,T})::Point{Dim,T} where {Dim,T} | ||
x = coordinates(point) | ||
lo = coordinates(minimum(box)) | ||
hi = coordinates(maximum(box)) | ||
ntuple(Dim) do i | ||
clamp(x[i], lo[i], hi[i]) | ||
end |> Point | ||
end | ||
|
||
""" | ||
clamp(pointset, box) | ||
Clamp each point in a [`PointSet`](@ref) to the edges of a [`Box`](@ref), returning a new set of points.""" | ||
function Base.clamp(points::PointSet{Dim,T}, box::Box{Dim,T})::PointSet{Dim,T} where {Dim,T} | ||
PointSet(map(point -> clamp(point, box), points)) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
@testset "Clamping" begin | ||
box = Box((zero(T), zero(T)), (one(T), one(T))) | ||
@test clamp(P2(0.5, 0.5), box) == P2(0.5, 0.5) | ||
@test clamp(P2(-1, 0.5), box) == P2(0, 0.5) | ||
@test clamp(P2(0.5, -1), box) == P2(0.5, 0) | ||
@test clamp(P2(2, 0.5), box) == P2(1, 0.5) | ||
@test clamp(P2(0.5, 2), box) == P2(0.5, 1) | ||
@test clamp(P2(2, 2), box) == P2(1, 1) | ||
@test clamp(P2(-1, -1), box) == P2(0, 0) | ||
|
||
points = PointSet(P2(0.5, 0.5), P2(-1, 0.5), P2(0.5, 2)) | ||
@test clamp(points, box) == PointSet(P2(0.5, 0.5), P2(0, 0.5), P2(0.5, 1)) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ed7a69c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
ed7a69c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/99773
Tip: Release Notes
Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.
To add them here just re-invoke and the PR will be updated.
Tagging
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: