Skip to content

Commit 1afc876

Browse files
authored
Add unfold function for point arrays (#83)
* Add unfold function for point arrays * Make unfold interface same as reflect_across_line * Add unfold to docs
1 parent 2c07d48 commit 1afc876

File tree

5 files changed

+68
-1
lines changed

5 files changed

+68
-1
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ To send us a pull request, please:
3535
5. Send us a pull request, answering any default questions in the pull request interface.
3636
6. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation.
3737

38-
GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and
38+
GitHub provides additional documentation on [forking a repository](https://help.github.com/articles/fork-a-repo/) and
3939
[creating a pull request](https://help.github.com/articles/creating-a-pull-request/).
4040

4141
## Finding contributions to work on

docs/src/polygons.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,5 @@ In addition to other generic [entity styles](entitystyles.md) like `NoRender`, `
7777
points
7878
sweep_poly
7979
gridpoints_in_polygon
80+
unfold
8081
```

src/DeviceLayout.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ import .Polygons:
383383
radius,
384384
rounded_corner,
385385
sweep_poly,
386+
unfold,
386387
union2d
387388
export Polygons,
388389
Polygon,
@@ -405,6 +406,7 @@ export Polygons,
405406
radius,
406407
rounded_corner,
407408
sweep_poly,
409+
unfold,
408410
union2d
409411

410412
include("align.jl")

src/polygons.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export circle,
7878
radius,
7979
rounded_corner,
8080
sweep_poly,
81+
unfold,
8182
union2d
8283

8384
const USCALE = 1.0 * Unitful.fm
@@ -1527,6 +1528,36 @@ function uniqueray(v::Vector{Point{T}}) where {T <: Real}
15271528
return Ray(Point(minx, miny), Point(minx, miny - 1)), indv
15281529
end
15291530

1531+
"""
1532+
unfold(v::Vector{Point{T}}, direction; through_pt=nothing) where {T}
1533+
unfold(v::Vector{Point{T}}, p0, p1) where {T}
1534+
1535+
Return a vector of twice the length of `v`, where the first half is `v` and the second half
1536+
is `v` in reverse order and reflected about an axis.
1537+
1538+
This can be used to construct polygons that have a mirror symmetry. The symmetry axis
1539+
can be defined in either of two ways: as a line with a given `direction` passing through
1540+
point `through_pt` (defaults to origin), or by two points `p0`, `p1`. `direction` can
1541+
be passed either as an angle or as a `Point` representing a vector.
1542+
1543+
As a trivial example, to draw a centered square:
1544+
1545+
```julia
1546+
uy = Point(0μm, 1μm) # could also be passed as 90°
1547+
pts = [Point(-1μm, -1μm), Point(-1μm, 1μm)]
1548+
square = Polygon(unfold(pts, uy))
1549+
```
1550+
"""
1551+
function unfold(v::Vector{Point{T}}, direction; through_pt=nothing) where {T}
1552+
N = length(v)
1553+
_reflect = Reflection(direction; through_pt=through_pt)
1554+
v_ref = [_reflect(v[N - i + 1]) for i in eachindex(v)]
1555+
return vcat(v, v_ref)
1556+
end
1557+
function unfold(v::Vector{Point{T}}, p0, p1) where {T}
1558+
return unfold(v, p1 - p0; through_pt=p0)
1559+
end
1560+
15301561
"""
15311562
orientation(p::Polygon)
15321563

test/tests.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,39 @@ end
263263
@test iszero(@allocated tr2 = convert(ScaledIsometry{Nothing}, tr))
264264
@test convert(ScaledIsometry{Point{Int}}, tr).origin isa Point{Int}
265265
@test hash(ScaledIsometry()) == hash(ScaledIsometry(Point(0, 0)))
266+
267+
# Point vector unfolding
268+
ux = Point(1, 0)
269+
uy = Point(0, 1)
270+
# Ints
271+
pts = [Point(-1, 0), Point(-1, 1)]
272+
@test unfold(pts, uy) [Point(-1, 0), Point(-1, 1), Point(1, 1), Point(1, 0)]
273+
# Floats
274+
pts = [Point(-1.0, 0.0), Point(-1.0, 1.0)]
275+
@test unfold(pts, uy)
276+
[Point(-1.0, 0.0), Point(-1.0, 1.0), Point(1.0, 1.0), Point(1.0, 0.0)]
277+
# Mixed Ints/Floats
278+
pts = [Point(-1, 0), Point(-1, 1)]
279+
@test unfold(pts, Point(0.0, 1.0))
280+
[Point(-1.0, 0.0), Point(-1.0, 1.0), Point(1.0, 1.0), Point(1.0, 0.0)]
281+
# x-axis reflection
282+
pts = [Point(-1, -1), Point(1, -1)]
283+
@test unfold(pts, ux) [Point(-1, -1), Point(1, -1), Point(1, 1), Point(-1, 1)]
284+
# With units
285+
pts = [Point(-1μm, 0μm), Point(-1μm, 1μm)]
286+
@test unfold(pts, uy)
287+
[Point(-1μm, 0μm), Point(-1μm, 1μm), Point(1μm, 1μm), Point(1μm, 0μm)]
288+
@test unfold(pts, uy * μm)
289+
[Point(-1μm, 0μm), Point(-1μm, 1μm), Point(1μm, 1μm), Point(1μm, 0μm)]
290+
# With angle instead of vector
291+
@test unfold(pts, 90°)
292+
[Point(-1μm, 0μm), Point(-1μm, 1μm), Point(1μm, 1μm), Point(1μm, 0μm)]
293+
# Axis by two points
294+
@test unfold(pts, Point(0μm, 0μm), Point(0μm, 1μm))
295+
[Point(-1μm, 0μm), Point(-1μm, 1μm), Point(1μm, 1μm), Point(1μm, 0μm)]
296+
# Less trivial case (off-center axis)
297+
@test unfold(pts, Point(1μm, 0μm), Point(1μm, 1μm))
298+
[Point(-1μm, 0μm), Point(-1μm, 1μm), Point(3μm, 1μm), Point(3μm, 0μm)]
266299
end
267300

268301
@testset "Polygon rendering" begin

0 commit comments

Comments
 (0)