Skip to content

Commit

Permalink
Add isaffine trait (#708)
Browse files Browse the repository at this point in the history
* Add 'isaffine' trait

* Fix typo

* Apply suggestions
  • Loading branch information
eliascarv authored Jan 24, 2024
1 parent 138541c commit 5dcd83e
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Meshes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ export
LambdaMuSmoothing,
LaplaceSmoothing,
TaubinSmoothing,
isaffine,

# miscellaneous
signarea,
Expand Down
9 changes: 9 additions & 0 deletions src/transforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ See [https://en.wikipedia.org/wiki/Geometric_transformation]
"""
abstract type GeometricTransform <: Transform end

"""
isaffine(transform)
Tells whether or not the geometric `transform` is Affine,
i.e. it can be defined as a muladd operation (`Ax + b`).
"""
isaffine(t::GeometricTransform) = isaffine(typeof(t))
isaffine(::Type{<:GeometricTransform}) = false

# fallback with raw vector of geometries for convenience
function apply(t::GeometricTransform, g::AbstractVector{<:Geometry})
n, c = apply(t, GeometrySet(g))
Expand Down
2 changes: 2 additions & 0 deletions src/transforms/affine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ end

parameters(t::Affine) = (A=t.A, b=t.b)

isaffine(::Type{<:Affine}) = true

isrevertible(t::Affine) = isinvertible(t)

function isinvertible(t::Affine)
Expand Down
2 changes: 2 additions & 0 deletions src/transforms/rotate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Rotate(θ) = Rotate(Angle2d(θ))

parameters(t::Rotate) = (; rot=t.rot)

isaffine(::Type{<:Rotate}) = true

isrevertible(::Type{<:Rotate}) = true

isinvertible(::Type{<:Rotate}) = true
Expand Down
2 changes: 2 additions & 0 deletions src/transforms/stretch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Stretch(factors...) = Stretch(factors)

parameters(t::Stretch) = (; factors=t.factors)

isaffine(::Type{<:Stretch}) = true

isrevertible(::Type{<:Stretch}) = true

isinvertible(::Type{<:Stretch}) = true
Expand Down
2 changes: 2 additions & 0 deletions src/transforms/translate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Translate(offsets...) = Translate(offsets)

parameters(t::Translate) = (; offsets=t.offsets)

isaffine(::Type{<:Translate}) = true

isrevertible(::Type{<:Translate}) = true

isinvertible(::Type{<:Translate}) = true
Expand Down
9 changes: 9 additions & 0 deletions test/transforms.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@testset "Transforms" begin
@testset "Rotate" begin
@test isaffine(Rotate)
@test TB.isrevertible(Rotate)
@test TB.isinvertible(Rotate)
@test TB.inverse(Rotate(Angle2d(T/ 2)))) == Rotate(Angle2d(-T/ 2)))
Expand Down Expand Up @@ -201,6 +202,7 @@
end

@testset "Translate" begin
@test isaffine(Translate)
@test TB.isrevertible(Translate)
@test TB.isinvertible(Translate)
@test TB.inverse(Translate(T(1), T(2))) == Translate(T(-1), T(-2))
Expand Down Expand Up @@ -363,6 +365,7 @@

@testset "Affine" begin
f = Affine(Angle2d(T/ 2)), T[1, 1])
@test isaffine(f)
@test TB.isrevertible(f)
@test TB.isinvertible(f)
@test TB.inverse(f) == Affine(Angle2d(-T/ 2)), Angle2d(-T/ 2)) * -T[1, 1])
Expand Down Expand Up @@ -535,6 +538,7 @@
end

@testset "Stretch" begin
@test isaffine(Stretch)
@test TB.isrevertible(Stretch)
@test TB.isinvertible(Stretch)
@test TB.inverse(Stretch(T(1), T(2))) == Stretch(T(1), T(1 / 2))
Expand Down Expand Up @@ -686,6 +690,7 @@
end

@testset "Expand" begin
@test !isaffine(Expand)
@test TB.isrevertible(Expand)
@test TB.isinvertible(Expand)
@test TB.inverse(Expand(T(1), T(2))) == Expand(T(1), T(1 / 2))
Expand Down Expand Up @@ -837,6 +842,7 @@
end

@testset "StdCoords" begin
@test !isaffine(StdCoords)
@test TB.isrevertible(StdCoords)

# ---------
Expand Down Expand Up @@ -872,6 +878,7 @@
end

@testset "Repair{0}" begin
@test !isaffine(Repair)
poly = PolyArea(P2[(0, 0), (1, 0), (1, 0), (1, 1), (0, 1), (0, 1)])
rpoly = poly |> Repair{0}()
@test nvertices(rpoly) == 4
Expand Down Expand Up @@ -950,6 +957,7 @@
end

@testset "Bridge" begin
@test !isaffine(Bridge)
δ = T(0.01)
f = Bridge(δ)
@test TB.parameters(f) == (; δ)
Expand Down Expand Up @@ -1021,6 +1029,7 @@
end

@testset "Smoothing" begin
@test !isaffine(LambdaMuSmoothing)
n, λ, μ = 30, T(0.5), T(0)
f = LambdaMuSmoothing(n, λ, μ)
@test TB.parameters(f) == (; n, λ, μ)
Expand Down

0 comments on commit 5dcd83e

Please sign in to comment.