Skip to content

Commit 33d2d26

Browse files
authored
Add missing hash and convert methods for ScaledIsometry (#51)
1 parent fa45ede commit 33d2d26

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ The format of this changelog is based on
1010

1111
### Fixed
1212

13-
- Rounding no longer fails when radius is less than `min_side_len` only due to numerical precision issues
13+
- Rounding no longer fails when available length is less than `min_side_len` only due to numerical precision issues
1414
- Circular arcs in rounded polygons will no longer occasionally produce very short edges near the endpoints, and are instead now drawn with equally spaced points including the endpoints
1515
- `Turn` segments with `SimpleTrace` or `SimpleCPW` styles now use `atol` to determine the discretization; this is faster and in some cases more accurate than the fallback method using `adapted_grid`
16+
- Added missing `hash` and `convert` methods for `ScaledIsometry`
1617

1718
## 1.1.1 (2025-04-16)
1819

src/transform.jl

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -380,15 +380,33 @@ function Base.:(==)(t1::ScaledIsometry, t2::ScaledIsometry)
380380
t1.mag == t2.mag
381381
end
382382

383+
function Base.hash(f::ScaledIsometry, h::UInt)
384+
h = hash(ScaledIsometry, h)
385+
# zero origin and nothing are == so should hash to the same thing
386+
orig = (isnothing(f.origin) || iszero(f.origin)) ? nothing : f.origin
387+
h = hash(orig, h)
388+
h = hash(f.rotation % 360°, h)
389+
h = hash(f.xrefl, h)
390+
return hash(f.mag, h)
391+
end
392+
393+
Base.convert(::Type{ScaledIsometry{Nothing}}, f::ScaledIsometry{Nothing}) = f
394+
Base.convert(::Type{ScaledIsometry{T}}, f::ScaledIsometry{T}) where {T <: Point} = f
395+
function Base.convert(
396+
::Type{ScaledIsometry{S}},
397+
f::ScaledIsometry{T}
398+
) where {S <: Point, T <: Point}
399+
return ScaledIsometry(convert(S, f.origin), f.rotation, f.xrefl, f.mag)
400+
end
383401
function Base.convert(
384-
::Type{ScaledIsometry{Point{S}}},
385-
f::ScaledIsometry
386-
) where {S <: Coordinate}
387-
orig = isnothing(f.origin) ? zero(Point{S}) : convert(Point{S}, f.origin)
388-
return ScaledIsometry(orig, f.rotation, f.xrefl, f.mag)
402+
::Type{ScaledIsometry{S}},
403+
f::ScaledIsometry{Nothing}
404+
) where {S <: Point}
405+
return ScaledIsometry(zero(S), f.rotation, f.xrefl, f.mag)
389406
end
390-
Base.convert(::Type{ScaledIsometry{Point{S}}}, f::Transformation) where {S <: Coordinate} =
391-
convert(ScaledIsometry{Point{S}}, ScaledIsometry(f))
407+
Base.convert(::Type{ScaledIsometry{S}}, f::Transformation) where {S <: Point} =
408+
convert(ScaledIsometry{S}, ScaledIsometry(f))
409+
# Cannot convert *to* ScaledIsometry{Nothing} in general
392410

393411
Base.isapprox(t1::Transformation, t2::ScaledIsometry; kwargs...) =
394412
isapprox(t1, affine(t2), kwargs...)

test/tests.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ end
258258
@test !xrefl(trU)
259259
@test xrefl(rotFlt XReflection() rotDeg trU)
260260
@test !xrefl(rotFlt YReflection() trU XReflection() rotDeg)
261+
262+
tr = convert(ScaledIsometry{Nothing}, ScaledIsometry())
263+
@test iszero(@allocated tr2 = convert(ScaledIsometry{Nothing}, tr))
264+
@test convert(ScaledIsometry{Point{Int}}, tr).origin isa Point{Int}
265+
@test hash(ScaledIsometry()) == hash(ScaledIsometry(Point(0, 0)))
261266
end
262267

263268
@testset "Polygon rendering" begin

0 commit comments

Comments
 (0)