Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ The format of this changelog is based on

### Fixed

- `LineSegment`s can be placed in coordinate systems of a different coordinate type and
transformed through references, which previously threw a `MethodError`, and their
`bounds` (and `lowerleft`/`upperright`) now give their extent instead of a zero
rectangle at the origin.
- `flatten` and `flatten!` with finite `max_copy` no longer duplicate geometry when a
reference kept for exceeding `max_copy` has references of its own. The contents of a
kept reference stay inside it instead of also being flattened or kept at the top level.
Expand Down
15 changes: 12 additions & 3 deletions src/polygons.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1155,9 +1155,7 @@ abstract type D1{T} <: GeometryEntity{T} end

ab(p0, p1) = Point(gety(p1) - gety(p0), getx(p0) - getx(p1))

to_polygons(::D1{T}) where {T} = Polygon{T}[]

transform(d1::T, f::Transformation) where {T <: D1} = T(f(d1.p0), f(d1.p1))
to_polygons(::D1{T}; kwargs...) where {T} = Polygon{T}[]

"""
LineSegment{T} <: D1{T}
Expand All @@ -1176,6 +1174,12 @@ struct LineSegment{T} <: D1{T}
end
end
LineSegment(p0::Point{S}, p1::Point{T}) where {S, T} = LineSegment(promote(p0, p1)...)
transform(l::LineSegment, f::Transformation) = LineSegment(f(l.p0), f(l.p1))
convert(::Type{GeometryEntity{T}}, l::LineSegment) where {T} =
LineSegment(convert(Point{T}, l.p0), convert(Point{T}, l.p1))

lowerleft(l::LineSegment) = Point(l.p0.x, min(l.p0.y, l.p1.y))
upperright(l::LineSegment) = Point(l.p1.x, max(l.p0.y, l.p1.y))

struct LineSegmentView{T} <: AbstractVector{T}
v::Vector{Point{T}}
Expand All @@ -1199,13 +1203,18 @@ struct Ray{T} <: D1{T}
p1::Point{T}
end
Ray(p0::Point{S}, p1::Point{T}) where {S, T} = Ray(promote(p0, p1)...)
transform(r::Ray, f::Transformation) = Ray(f(r.p0), f(r.p1))
convert(::Type{GeometryEntity{T}}, r::Ray) where {T} =
Ray(convert(Point{T}, r.p0), convert(Point{T}, r.p1))

struct Line{T} <: D1{T}
p0::Point{T}
p1::Point{T}
end
Line(p0::Point{S}, p1::Point{T}) where {S, T} = Line(promote(p0, p1)...)
Line(seg::LineSegment) = Line(seg.p0, seg.p1)
transform(l::Line, f::Transformation) = Line(f(l.p0), f(l.p1))
convert(::Type{GeometryEntity{T}}, l::Line) where {T} = convert(Line{T}, l)

Base.promote_rule(::Type{Line{S}}, ::Type{Line{T}}) where {S, T} = Line{promote_type(S, T)}
Base.convert(::Type{Line{S}}, L::Line) where {S} = Line{S}(L.p0, L.p1)
Expand Down
25 changes: 25 additions & 0 deletions test/test_entity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,31 @@
@test !isproper(bounds(opt_plus))
end

@testset "1D entities" begin
seg = LineSegment(Point(1.0μm, 5.0μm), Point(1.0μm, 2.0μm))
expected_bounds = Rectangle(Point(1.0μm, 2.0μm), Point(1.0μm, 5.0μm))
@test bounds(seg) == expected_bounds
@test bounds(WithDirection(0°)(seg)) == expected_bounds

# Placing converts to the coordinate system's type, and references transform it
child = CoordinateSystem("segment_child", nm)
place!(child, seg, GDSMeta())
@test only(elements(child)) isa LineSegment{coordinatetype(child)}
parent = CoordinateSystem("segment_parent", nm)
addref!(parent, child, Point(10.0μm, 0.0μm); rot=90°)
flat_seg = only(elements(flatten(parent)))
@test flat_seg.p0 ≈ Point(5.0μm, 1.0μm)
@test flat_seg.p1 ≈ Point(8.0μm, 1.0μm)

for D1Type in (Polygons.Ray, Polygons.Line)
d1 = D1Type(Point(0μm, 0μm), Point(1μm, 0μm))
rotated = rotate(d1, 90°)
@test rotated isa D1Type
@test rotated.p1 ≈ Point(0μm, 1μm)
@test convert(GeometryEntity{typeof(1.0nm)}, d1) isa D1Type{typeof(1.0nm)}
end
end

@testset "Path Nodes" begin
# Create a halo of a path
pa = Path(0nm, 0nm)
Expand Down
Loading