Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parametric function for Chain #1155

Merged
merged 15 commits into from
Dec 19, 2024
19 changes: 18 additions & 1 deletion src/geometries/polytopes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
We say that a geometry is a K-polytope when it is a collection of "flat" sides
that constitute a `K`-dimensional subspace. They are called chain, polygon and
polyhedron respectively for 1D (`K=1`), 2D (`K=2`) and 3D (`K=3`) subspaces.
The parameter `K` is also known as the rank or parametric dimension
The parameter `K` is also known as the rank or parametric dimension
of the polytope (<https://en.wikipedia.org/wiki/Abstract_polytope>).

The term polytope expresses a particular combinatorial structure. A polyhedron,
Expand Down Expand Up @@ -156,6 +156,23 @@
map(i -> ∠(vs[i - 1], vs[i], vs[i + 1]), i1:i2)
end

function (c::Chain)(t)
if t < 0 || t > 1
throw(DomainError(t, "c(t) is not defined for t outside [0, 1]."))

Check warning on line 161 in src/geometries/polytopes.jl

View check run for this annotation

Codecov / codecov/patch

src/geometries/polytopes.jl#L161

Added line #L161 was not covered by tests
end
segs = segments(c)
sums = cumsum(measure.(segs))
sums /= last(sums)
# find k such that sums[k] ≤ t < sums[k + 1]
k = searchsortedfirst(sums, t) - 1
# select segment s at index k
s, _ = iterate(segs, k)
# reparametrization of t within s
∑ₖ = iszero(k) ? zero(eltype(sums)) : sums[k]
∑ₖ₊₁ = sums[k + 1]
s((t - ∑ₖ) / (∑ₖ₊₁ - ∑ₖ))
end

# implementations of Chain
include("polytopes/segment.jl")
include("polytopes/rope.jl")
Expand Down
19 changes: 19 additions & 0 deletions test/polytopes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,25 @@ end
r = Ring(merc.([(0, 0), (1, 0), (1, 1), (0, 1)]))
@test crs(centroid(r)) === crs(r)

# parameterization
juliohm marked this conversation as resolved.
Show resolved Hide resolved
r = boundary(Box(cart(0, 0), cart(1, 1)))
@test r(T(0)) == cart(0, 0)
@test r(T(0.25)) == cart(1, 0)
@test r(T(0.5)) == cart(1, 1)
@test r(T(0.75)) == cart(0, 1)
r = Rope(cart(0, 0), cart(3, 0), cart(4, 0))
@test r(T(0)) == cart(0, 0)
@test r(T(0.25)) == cart(1, 0)
@test r(T(0.5)) == cart(2, 0)
@test r(T(0.75)) == cart(3, 0)
@test r(T(1)) == cart(4, 0)
r = Ring(latlon(45, 0), latlon(45, 90), latlon(90, 90))
@test r(T(0)) == latlon(45, 0)
@test r(T(1)) == latlon(45, 0)
r = Rope(latlon(45, 0), latlon(45, 90), latlon(90, 90))
@test r(T(0)) == latlon(45, 0)
@test r(T(1)) == latlon(90, 90)

ri = Ring(cart.([(1, 1), (2, 2), (3, 3)]))
ro = Rope(cart.([(1, 1), (2, 2), (3, 3)]))
@test sprint(show, ri) == "Ring((x: 1.0 m, y: 1.0 m), (x: 2.0 m, y: 2.0 m), (x: 3.0 m, y: 3.0 m))"
Expand Down
Loading