Transforming `GeometryBasics.Ngon` using `CoordinateTransformations`

I am trying to transform some GeometryBasics.Line objects using CoordinateTransformations but I can’t find a convenient way to transform anything other than GeometryBasics.Point out of the box.

For example, if I have

using GeometryBasics, CoordinateTransformations
using LinearAlgebra
l = Line(Point(0.0, 1.0), Point(1.0, 1.0))
mirror_transform = LinearMap(diagm(-1, 1))
lm = mirror_transform(l)

I would hope to get lm == Line(Point(0.0, 1.0), Point(-1.0, 1.0)) (i.e. transformation of each point).
Are these transformations implemented for GeomtryBasics types somewhere?


If this kind of transformation is not supported, what is a good strategy to extend CoordinateTransformations to achieve something like this? (It seems like various packages might already do something like this and there might be a best practice).
A naive solution would be to decompose everything into Points, apply the transformation on each of them, and then compose the polygon from these transformed points (but that seems very tedious and suboptimal).

1 Like

The question is very specific to a Julia package, so this answer is only tangentially related.

Using LazySets.jl,

using LazySets, LinearAlgebra

l = Line(from=[0.0, 1.0], to=[1.0, 1.0]);
mirror_transform = diagm([-1., 1.]);

# take the concrete linear map of the line by the transfomation
lm = linear_map(mirror_transform, l)
  Line{Float64,Array{Float64,1}}([0.0, 1.0], [1.0, 0.0])

# check equivalence
(lm ⊆ l) && (l ⊆ lm)
  true

# or just
isequivalent(l, lm)
 true

# alternative using lazy set computations
X = mirror_transform * l
  LinearMap{Float64,Line{Float64,Array{Float64,1}},Float64,Array{Float64,2}}([-1.0 0.0; 0.0 1.0], Line{Float64,Array{Float64,1}}([0.0, 1.0], [-1.0, 0.0]))

isequivalent(X, l)
  true

# other example
p = rand(HPolygon, num_constraints=5)
mp = linear_map(mirror_transform, p)

using Plots

plot(p)
plot!(mp)

Thank you @mforets for your help but I actually need to use the packages mentioned above so my question is rather package specific.

I ended up filling an issue over at the CoordinateTransformation.jl repository so that this discussion is documented near the code.

1 Like