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
12 changes: 11 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,16 @@ function angles(c::Chain)
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]."))
end
n = nvertices(c) - !isclosed(c)
k = max(1, ceil(Int, n * t))
juliohm marked this conversation as resolved.
Show resolved Hide resolved
s, _ = iterate(segments(c), k)
s(n * t - k + 1)
end

# implementations of Chain
include("polytopes/segment.jl")
include("polytopes/rope.jl")
Expand Down
15 changes: 15 additions & 0 deletions test/polytopes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,21 @@ 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
ri = Ring(latlon(45, 0), latlon(45, 90), latlon(90, 90))
@test ri(T(0)) == latlon(45, 0)
@test ri(T(0.25)) == latlon(45, 67.5)
@test ri(T(0.5)) == latlon(67.5, 90)
@test ri(T(0.75)) == latlon(78.75, 67.5)
@test ri(T(1)) == latlon(45, 0)

ro = Rope(latlon(45, 0), latlon(45, 90), latlon(90, 90))
@test ro(T(0)) == latlon(45, 0)
@test ro(T(0.25)) == latlon(45, 45)
@test ro(T(0.5)) == latlon(45, 90)
@test ro(T(0.75)) == latlon(67.5, 90)
@test ro(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