Skip to content

Commit

Permalink
Adjust for changes in StaticArrays.jl, and repair tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajkeller34 committed Feb 22, 2017
1 parent ef4379d commit 6253979
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 33 deletions.
4 changes: 2 additions & 2 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
julia 0.5
PyCall
ForwardDiff
StaticArrays
StaticArrays 0.3.0
CoordinateTransformations
FileIO
Compat
Compat 0.7.15
10 changes: 7 additions & 3 deletions src/Points.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Points
import Devices: Coordinate
import StaticArrays: FieldVector, @SMatrix
import StaticArrays
import StaticArrays: @SMatrix
import CoordinateTransformations: LinearMap, Translation, , compose
import Clipper: IntPoint
import Base: convert, .+, .-, *, summary, promote_rule, show, reinterpret
Expand All @@ -14,19 +15,22 @@ export getx, gety, lowerleft, upperright

"""
```
immutable Point{T} <: FieldVector{T}
immutable Point{T} <: StaticArrays.FieldVector{T}
x::T
y::T
end
```
2D Cartesian coordinate in the plane.
"""
immutable Point{T<:Coordinate} <: FieldVector{T}
immutable Point{T<:Coordinate} <: StaticArrays.FieldVector{T}
x::T
y::T
end

StaticArrays.similar_type{P<:Point, T}(::Type{P}, ::Type{T},
::StaticArrays.Size{(2,)}) = Point{T}

Point(x::Number, y::Number) =
error("Cannot use `Point` with this combination of types.")
Point(x::Length, y::Length) = Point{promote_type(typeof(x),typeof(y))}(x,y)
Expand Down
56 changes: 28 additions & 28 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ForwardDiff

# This is needed in case the user has changed the default length promotion type.
ru = promote_type(typeof(m),typeof(cm))()
p(x,y) = Point(x,y)

@testset "Points" begin
@testset "> Point constructors" begin
Expand Down Expand Up @@ -171,51 +172,51 @@ end
r1 = Rectangle(2,2)
r2 = Rectangle(1,2)
@test clip(Clipper.ClipTypeDifference, r1, r2)[1] ==
Polygon(Point{Int}[(2,2),(1,2),(1,0),(2,0)])
Polygon(Point{Int}[p(2,2),p(1,2),p(1,0),p(2,0)])
@test typeof(clip(Clipper.ClipTypeDifference, r1, r2)[1]) ==
Polygon{Int}

# Rectangle{Int}, Polygon{Int} clipping
p2 = Polygon(Point{Int}[(0,0), (1,0), (1,2), (0,2)])
p2 = Polygon(Point{Int}[p(0,0), p(1,0), p(1,2), p(0,2)])
@test clip(Clipper.ClipTypeDifference, r1, p2)[1] ==
Polygon(Point{Int}[(2,2),(1,2),(1,0),(2,0)])
Polygon(Point{Int}[p(2,2),p(1,2),p(1,0),p(2,0)])
@test typeof(clip(Clipper.ClipTypeDifference, r1, p2)[1]) ==
Polygon{Int}

# Polygon{Int}, Polygon{Int} clipping
p1 = Polygon(Point{Int}[(0,0), (2,0), (2,2), (0,2)])
p1 = Polygon(Point{Int}[p(0,0), p(2,0), p(2,2), p(0,2)])
@test clip(Clipper.ClipTypeDifference, p1, p2)[1] ==
Polygon(Point{Int}[(2,2),(1,2),(1,0),(2,0)])
Polygon(Point{Int}[p(2,2), p(1,2), p(1,0), p(2,0)])
@test typeof(clip(Clipper.ClipTypeDifference, p1, p2)[1]) ==
Polygon{Int}

# Rectangle{Float64}, Rectangle{Float64} clipping
r1 = Rectangle(2.0,2.0)
r2 = Rectangle(1.0,2.0)
@test clip(Clipper.ClipTypeDifference, r1, r2)[1] ==
Polygon(Point{Float64}[(2.0,2.0),(1.0,2.0),(1.0,0.0),(2.0,0.0)])
Polygon(Point{Float64}[p(2.0,2.0), p(1.0,2.0), p(1.0,0.0), p(2.0,0.0)])
@test typeof(clip(Clipper.ClipTypeDifference, r1, r2)[1]) ==
Polygon{Float64}

# Rectangle{Float64}, Polygon{Float64} clipping
p2 = Polygon(Point{Float64}[(0,0), (1,0), (1,2), (0,2)])
p2 = Polygon(Point{Float64}[p(0,0), p(1,0), p(1,2), p(0,2)])
@test clip(Clipper.ClipTypeDifference, r1, p2)[1] ==
Polygon(Point{Float64}[(2,2),(1,2),(1,0),(2,0)])
Polygon(Point{Float64}[p(2,2), p(1,2), p(1,0), p(2,0)])
@test typeof(clip(Clipper.ClipTypeDifference, r1, p2)[1]) ==
Polygon{Float64}

# Polygon{Float64}, Polygon{Float64} clipping
p1 = Polygon(Point{Float64}[(0,0), (2,0), (2,2), (0,2)])
p1 = Polygon(Point{Float64}[p(0,0), p(2,0), p(2,2), p(0,2)])
@test clip(Clipper.ClipTypeDifference, p1, p2)[1] ==
Polygon(Point{Float64}[(2,2),(1,2),(1,0),(2,0)])
Polygon(Point{Float64}[p(2,2), p(1,2), p(1,0), p(2,0)])
@test typeof(clip(Clipper.ClipTypeDifference, p1, p2)[1]) ==
Polygon{Float64}

# Test a case where the AbstractPolygon subtypes and numeric types are mixed
# Rectangle{Int}, Polygon{Float64} clipping
r2 = Rectangle(1,2)
@test clip(Clipper.ClipTypeDifference, p1, r2)[1] ==
Polygon(Point{Float64}[(2,2),(1,2),(1,0),(2,0)])
Polygon(Point{Float64}[p(2,2), p(1,2), p(1,0), p(2,0)])
@test typeof(clip(Clipper.ClipTypeDifference, p1, r2)[1]) ==
Polygon{Float64}
end
Expand All @@ -225,14 +226,14 @@ end
r1 = Rectangle(2μm,2μm)
r2 = Rectangle(1μm,2μm)
@test clip(Clipper.ClipTypeDifference, r1, r2)[1] ==
Polygon(Point{typeof(1μm)}[(2μm,2μm),(1μm,2μm),(1μm,0μm),(2μm,0μm)])
Polygon(Point{typeof(1μm)}[p(2μm,2μm), p(1μm,2μm), p(1μm,0μm), p(2μm,0μm)])
@test typeof(clip(Clipper.ClipTypeDifference, r1, r2)[1]) ==
Polygon{typeof(1μm)}

r1 = Rectangle(2.0μm,2.0μm)
r2 = Rectangle(1.0μm,2.0μm)
@test clip(Clipper.ClipTypeDifference, r1, r2)[1] ==
Polygon(Point{typeof(1.0μm)}[(2μm,2μm),(1μm,2μm),(1μm,0μm),(2μm,0μm)])
Polygon(Point{typeof(1.0μm)}[p(2μm,2μm), p(1μm,2μm), p(1μm,0μm), p(2μm,0μm)])
@test typeof(clip(Clipper.ClipTypeDifference, r1, r2)[1]) ==
Polygon{typeof(1.0μm)}

Expand All @@ -244,36 +245,35 @@ end
s = [r1, r1+Point(0,4), r1+Point(0,8)]
c = [Rectangle(1,10)]
r = clip(Clipper.ClipTypeDifference, s, c)
@test Polygon(Point{Int}[(2,2),(1,2),(1,0),(2,0)]) in r
@test Polygon(Point{Int}[(2,6),(1,6),(1,4),(2,4)]) in r
@test Polygon(Point{Int}[(2,10),(1,10),(1,8),(2,8)]) in r
@test Polygon(Point{Int}[p(2,2),p(1,2),p(1,0),p(2,0)]) in r
@test Polygon(Point{Int}[p(2,6),p(1,6),p(1,4),p(2,4)]) in r
@test Polygon(Point{Int}[p(2,10),p(1,10),p(1,8),p(2,8)]) in r
@test length(r) == 3
end
end

@testset "Polygon offsetting" begin
r = Rectangle(1, 1)
@test offset(r, 1)[1] ==
Polygon([Point(2,2),Point(-1,2),Point(-1,-1),Point(2,-1)])
Polygon([p(2,2), p(-1,2), p(-1,-1), p(2,-1)])
@test_throws DimensionError offset(r,1μm)

r = Rectangle(1.0, 1.0)
@test offset(r, 0.5)[1] ==
Polygon(Point{Float64}[(1.5, 1.5),(-0.5, 1.5),(-0.5, -0.5),(1.5, -0.5)])
Polygon([p(1.5, 1.5), p(-0.5, 1.5), p(-0.5, -0.5), p(1.5, -0.5)])
@test_throws DimensionError offset(r, 0.5μm)

r = Rectangle(1μm, 1μm)
@test_throws DimensionError offset(r, 1)
@test offset(r, 1μm)[1] == Polygon(
Point{typeof(1μm)}[(2μm, 2μm),(-1μm, 2μm),(-1μm, -1μm),(2μm, -1μm)])
[p(2μm, 2μm), p(-1μm, 2μm), p(-1μm, -1μm), p(2μm, -1μm)])
@test offset(r, 5000nm)[1] == Polygon(
Point{typeof(1μm)}[(6μm, 6μm),(-5μm, 6μm),(-5μm, -5μm),(6μm, -5μm)])
[p(6μm, 6μm), p(-5μm, 6μm), p(-5μm, -5μm), p(6μm, -5μm)])

r = Rectangle(1.0μm, 1.0μm)
@test_throws DimensionError offset(r, 1.0)
@test offset(r, 50nm)[1] Polygon(
Point{typeof(0.5μm)}[(1.05μm,1.05μm),(-0.05μm,1.05μm),
(-0.05μm,-0.05μm),(1.05μm,-0.05μm)])
[p(1.05μm,1.05μm), p(-0.05μm,1.05μm), p(-0.05μm,-0.05μm), p(1.05μm,-0.05μm)])

end

Expand Down Expand Up @@ -336,12 +336,12 @@ end
end

@testset "> Path segments" begin
p = Path(Point(0.0μm, 0.0μm))
@test_throws Unitful.DimensionError straight!(p, 10.0)
@test pathlength(p) == 0.0μm
straight!(p, 10μm)
@test pathlength(p) == 10μm
@test ForwardDiff.derivative(p[1].seg.f, 0.0) Point(10.0μm, 0.0μm)
path = Path(Point(0.0μm, 0.0μm))
@test_throws Unitful.DimensionError straight!(path, 10.0)
@test pathlength(path) == 0.0μm
straight!(path, 10μm)
@test pathlength(path) == 10μm
@test ForwardDiff.derivative(path[1].seg.f, 0.0) Point(10.0μm, 0.0μm)
end

# TODO: How to test `CompoundSegment` / `CompoundStyle`?
Expand Down

0 comments on commit 6253979

Please sign in to comment.