Skip to content

slightly less method invalidation & run formatter #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 23, 2020
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
10 changes: 10 additions & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
always_for_in = true
always_use_return = true
import_to_using = true
pipe_to_function_call = true
remove_extra_newlines = true
short_to_long_function_def = true
style = "yas"
whitespace_in_kwargs = false
whitespace_ops_in_indices = true
whitespace_typedefs = false
25 changes: 5 additions & 20 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,9 @@ using GeometryBasics
# Copy the README to serve as the homepage
cp(joinpath(@__DIR__, "..", "README.md"), joinpath(@__DIR__, "src", "index.md"))

makedocs(
format = Documenter.HTML(),
sitename = "GeometryBasics.jl",
pages = [
"index.md",
"primitives.md",
"rectangles.md",
"polygons.md",
"meshes.md",
"decomposition.md",
"distancefields.md",
"metadata.md",
"api.md",
],
modules = [GeometryBasics]
)
makedocs(format=Documenter.HTML(), sitename="GeometryBasics.jl",
pages=["index.md", "primitives.md", "rectangles.md", "polygons.md", "meshes.md",
"decomposition.md", "distancefields.md", "metadata.md", "api.md"],
modules=[GeometryBasics])

deploydocs(
repo = "github.com/JuliaGeometry/GeometryBasics.jl.git",
push_preview = true
)
deploydocs(repo="github.com/JuliaGeometry/GeometryBasics.jl.git", push_preview=true)
22 changes: 13 additions & 9 deletions src/basic_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ function LineString(points::AbstractVector{<:AbstractPoint}, skip=1)
return LineString(connect(points, LineP, skip))
end

function LineString(points::AbstractVector{<:Pair{P,P}}) where {P<:AbstractPoint{N,T}} where {N, T}
function LineString(points::AbstractVector{<:Pair{P,P}}) where {P<:AbstractPoint{N,T}} where {N,
T}
return LineString(reinterpret(LineP{N,T,P}, points))
end

Expand Down Expand Up @@ -261,8 +262,9 @@ function Base.:(==)(a::Polygon, b::Polygon)
end

function Polygon(exterior::E,
interiors::AbstractVector{E}) where
{E<:AbstractVector{LineP{Dim,T,P}}} where {Dim, T, P}
interiors::AbstractVector{E}) where {E<:AbstractVector{LineP{Dim,T,P}}} where {Dim,
T,
P}
return Polygon{Dim,T,P,typeof(exterior),typeof(interiors)}(exterior, interiors)
end

Expand All @@ -279,12 +281,15 @@ function Polygon(exterior::AbstractVector{P}, faces::AbstractVector{<:Integer},
end

function Polygon(exterior::AbstractVector{P},
faces::AbstractVector{<:LineFace}) where {P<:AbstractPoint{Dim,T}} where {Dim, T}
faces::AbstractVector{<:LineFace}) where {P<:AbstractPoint{Dim,T}} where {Dim,
T}
return Polygon(LineString(exterior, faces))
end

function Polygon(exterior::AbstractVector{P},
interior::AbstractVector{<:AbstractVector{P}}) where {P<:AbstractPoint{Dim, T}} where {Dim, T}
interior::AbstractVector{<:AbstractVector{P}}) where {P<:AbstractPoint{Dim,
T}} where {Dim,
T}
ext = LineString(exterior)
# We need to take extra care for empty interiors, since
# if we just map over it, it won't infer the element type correctly!
Expand All @@ -295,7 +300,7 @@ end

function coordinates(polygon::Polygon{N,T,PointType}) where {N,T,PointType}
exterior = coordinates(polygon.exterior)
return if isempty(polygon.interiors)
if isempty(polygon.interiors)
return exterior
else
result = PointType[]
Expand All @@ -310,7 +315,6 @@ end
"""
struct MultiPolygon{Dim,T<:Real,Element<:AbstractPolygon{Dim,T},
A<:AbstractVector{Element}} <: AbstractVector{Element}

polygons::A
end

Expand Down Expand Up @@ -376,7 +380,7 @@ end
Tables.schema(mesh::Mesh) = Tables.schema(getfield(mesh, :simplices))

function Base.getproperty(mesh::Mesh, name::Symbol)
return if name === :position
if name === :position
# a mesh always has position defined by coordinates...
return metafree(coordinates(mesh))
else
Expand All @@ -386,7 +390,7 @@ end

function Base.propertynames(mesh::Mesh)
names = propertynames(getfield(mesh, :simplices))
return if :position in names
if :position in names
return names
else
# a mesh always has positions!
Expand Down
210 changes: 103 additions & 107 deletions src/fixed_arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,113 +8,109 @@ end

macro fixed_vector(name, parent)
expr = quote
struct $(name){S,T} <: $(parent){S,T}
data::NTuple{S,T}

function $(name){S,T}(x::NTuple{S,T}) where {S,T}
return new{S,T}(x)
end

function $(name){S,T}(x::NTuple{S,Any}) where {S,T}
return new{S,T}(StaticArrays.convert_ntuple(T, x))
end
end

size_or(::Type{$(name)}, or) = or
eltype_or(::Type{$(name)}, or) = or
eltype_or(::Type{$(name){S,T} where S}, or) where {T} = T
eltype_or(::Type{$(name){S,T} where T}, or) where {S} = or
eltype_or(::Type{$(name){S,T}}, or) where {S,T} = T

size_or(::Type{$(name){S,T} where S}, or) where {T} = or
size_or(::Type{$(name){S,T} where T}, or) where {S} = Size{(S,)}()
size_or(::Type{$(name){S,T}}, or) where {S,T} = (S,)

# Array constructor
function $(name){S}(x::AbstractVector{T}) where {S,T}
@assert S <= length(x)
return $(name){S,T}(ntuple(i -> x[i], Val(S)))
end

function $(name){S,T1}(x::AbstractVector{T2}) where {S,T1,T2}
@assert S <= length(x)
return $(name){S,T1}(ntuple(i -> T1(x[i]), Val(S)))
end

function $(name){S,T}(x) where {S,T}
return $(name){S,T}(ntuple(i -> T(x), Val(S)))
end

$(name){S}(x::T) where {S,T} = $(name){S,T}(ntuple(i -> x, Val(S)))
$(name){1,T}(x::T) where {T} = $(name){1,T}((x,))
$(name)(x::NTuple{S}) where {S} = $(name){S}(x)
function $(name)(x::T) where {S,T<:Tuple{Vararg{Any,S}}}
return $(name){S,StaticArrays.promote_tuple_eltype(T)}(x)
end

function $(name){S}(x::T) where {S,T<:Tuple}
return $(name){S,StaticArrays.promote_tuple_eltype(T)}(x)
end
$(name){S,T}(x::StaticVector) where {S,T} = $(name){S,T}(Tuple(x))

@generated function (::Type{$(name){S,T}})(x::$(name)) where {S,T}
idx = [:(x[$i]) for i in 1:S]
return quote
$($(name)){S,T}($(idx...))
end
end

@generated function Base.convert(::Type{$(name){S,T}},
x::$(name)) where {S,T}
idx = [:(x[$i]) for i in 1:S]
return quote
$($(name)){S,T}($(idx...))
end
end

@generated function (::Type{SV})(x::StaticVector) where {SV<:$(name)}
len = size_or(SV, size(x))[1]
return if length(x) == len
:(SV(Tuple(x)))
elseif length(x) > len
elems = [:(x[$i]) for i in 1:len]
:(SV($(Expr(:tuple, elems...))))
else
error("Static Vector too short: $x, target type: $SV")
end
end

Base.@pure StaticArrays.Size(::Type{$(name){S,Any}}) where {S} = Size(S)
Base.@pure StaticArrays.Size(::Type{$(name){S,T}}) where {S,T} = Size(S)

Base.@propagate_inbounds function Base.getindex(v::$(name){S,T},
i::Int) where {S,T}
return v.data[i]
end

Base.Tuple(v::$(name)) = v.data
function Base.convert(::Type{$(name){S,T}}, x::NTuple{S,T}) where {S,T}
return $(name){S,T}(x)
end
function Base.convert(::Type{$(name){S,T}}, x::Tuple) where {S,T}
return $(name){S,T}(convert(NTuple{S,T}, x))
end

@generated function StaticArrays.similar_type(::Type{SV}, ::Type{T},
s::Size{S}) where {SV<:$(name),
T,S}
return if length(S) === 1
$(name){S[1],T}
else
StaticArrays.default_similar_type(T, s(), Val{length(S)})
end
end

Base.:(*)(a::$name, b::$name) = a .* b
function Base.broadcasted(f, a::AbstractArray{T},
b::$name) where {T<:$name}
return Base.broadcasted(f, a, (b,))
end
struct $(name){S,T} <: $(parent){S,T}
data::NTuple{S,T}

function $(name){S,T}(x::NTuple{S,T}) where {S,T}
return new{S,T}(x)
end

function $(name){S,T}(x::NTuple{S,Any}) where {S,T}
return new{S,T}(StaticArrays.convert_ntuple(T, x))
end
end

size_or(::Type{$(name)}, or) = or
eltype_or(::Type{$(name)}, or) = or
eltype_or(::Type{$(name){S,T} where S}, or) where {T} = T
eltype_or(::Type{$(name){S,T} where T}, or) where {S} = or
eltype_or(::Type{$(name){S,T}}, or) where {S,T} = T

size_or(::Type{$(name){S,T} where S}, or) where {T} = or
size_or(::Type{$(name){S,T} where T}, or) where {S} = Size{(S,)}()
size_or(::Type{$(name){S,T}}, or) where {S,T} = (S,)

# Array constructor
function $(name){S}(x::AbstractVector{T}) where {S,T}
@assert S <= length(x)
return $(name){S,T}(ntuple(i -> x[i], Val(S)))
end

function $(name){S,T1}(x::AbstractVector{T2}) where {S,T1,T2}
@assert S <= length(x)
return $(name){S,T1}(ntuple(i -> T1(x[i]), Val(S)))
end

function $(name){S,T}(x) where {S,T}
return $(name){S,T}(ntuple(i -> T(x), Val(S)))
end

$(name){S}(x::T) where {S,T} = $(name){S,T}(ntuple(i -> x, Val(S)))
$(name){1,T}(x::T) where {T} = $(name){1,T}((x,))
$(name)(x::NTuple{S}) where {S} = $(name){S}(x)
function $(name)(x::T) where {S,T<:Tuple{Vararg{Any,S}}}
return $(name){S,StaticArrays.promote_tuple_eltype(T)}(x)
end

function $(name){S}(x::T) where {S,T<:Tuple}
return $(name){S,StaticArrays.promote_tuple_eltype(T)}(x)
end
$(name){S,T}(x::StaticVector) where {S,T} = $(name){S,T}(Tuple(x))

@generated function (::Type{$(name){S,T}})(x::$(name)) where {S,T}
idx = [:(x[$i]) for i in 1:S]
return quote
$($(name)){S,T}($(idx...))
end
end

@generated function Base.convert(::Type{$(name){S,T}}, x::$(name)) where {S,T}
idx = [:(x[$i]) for i in 1:S]
return quote
$($(name)){S,T}($(idx...))
end
end

@generated function (::Type{SV})(x::StaticVector) where {SV<:$(name)}
len = size_or(SV, size(x))[1]
return if length(x) == len
:(SV(Tuple(x)))
elseif length(x) > len
elems = [:(x[$i]) for i in 1:len]
:(SV($(Expr(:tuple, elems...))))
else
error("Static Vector too short: $x, target type: $SV")
end
end

Base.@pure StaticArrays.Size(::Type{$(name){S,Any}}) where {S} = Size(S)
Base.@pure StaticArrays.Size(::Type{$(name){S,T}}) where {S,T} = Size(S)

Base.@propagate_inbounds function Base.getindex(v::$(name){S,T}, i::Int) where {S,T}
return v.data[i]
end

Base.Tuple(v::$(name)) = v.data
function Base.convert(::Type{$(name){S,T}}, x::NTuple{S,T}) where {S,T}
return $(name){S,T}(x)
end
function Base.convert(::Type{$(name){S,T}}, x::Tuple) where {S,T}
return $(name){S,T}(convert(NTuple{S,T}, x))
end

@generated function StaticArrays.similar_type(::Type{SV}, ::Type{T},
s::Size{S}) where {SV<:$(name),T,S}
return if length(S) === 1
$(name){S[1],T}
else
StaticArrays.default_similar_type(T, s(), Val{length(S)})
end
end

Base.:(*)(a::$name, b::$name) = a .* b
function Base.broadcasted(f, a::AbstractArray{T}, b::$name) where {T<:$name}
return Base.broadcasted(f, a, (b,))
end
end
return esc(expr)
end
Expand Down
18 changes: 11 additions & 7 deletions src/offsetintegers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ raw(x::Integer) = x
value(x::OffsetInteger{O,T}) where {O,T} = raw(x) - O
value(x::Integer) = x

function show(io::IO, oi::OffsetInteger{O,T}) where {O,T}
function show(io::IO, oi::OffsetInteger)
return print(io, "|$(raw(oi)) (indexes as $(value(oi))|")
end

Expand All @@ -29,23 +29,27 @@ Base.eltype(oi::OffsetInteger) = eltype(typeof(oi))

# constructors and conversion
function OffsetInteger{O1,T1}(x::OffsetInteger{O2,T2}) where {O1,O2,T1<:Integer,T2<:Integer}
return OffsetInteger{O1,T1}(T2(x))
return OffsetInteger{O1,T1}(convert(T2, x))
end

OffsetInteger{O}(x::Integer) where {O} = OffsetInteger{O,eltype(x)}(x)
OffsetInteger{O}(x::OffsetInteger) where {O} = OffsetInteger{O,eltype(x)}(x)
# This constructor has a massive method invalidation as a consequence,
# and doesn't seem to be needed, so let's remove it!
(::Type{IT})(x::OffsetInteger) where {IT<:Integer} = IT(value(x))

Base.@pure pure_max(x1, x2) = x1 > x2 ? x1 : x2
Base.promote_rule(::Type{T1}, ::Type{OffsetInteger{O,T2}}) where {T1<:Integer,O,T2} = T1
Base.convert(::Type{IT}, x::OffsetInteger) where {IT<:Integer} = IT(value(x))

Base.promote_rule(::Type{IT}, ::Type{<:OffsetInteger}) where {IT<:Integer} = IT

function Base.promote_rule(::Type{OffsetInteger{O1,T1}},
::Type{OffsetInteger{O2,T2}}) where {O1,O2,T1,T2}
::Type{OffsetInteger{O2,T2}}) where {O1,O2,T1<:Integer,
T2<:Integer}
return OffsetInteger{pure_max(O1, O2),promote_type(T1, T2)}
end

#Need to convert to Int here because of: https://github.com/JuliaLang/julia/issues/35038
Base.@pure pure_max(x1, x2) = x1 > x2 ? x1 : x2

# Need to convert to Int here because of: https://github.com/JuliaLang/julia/issues/35038
Base.to_index(I::OffsetInteger) = convert(Int, raw(OneIndex(I)))
Base.to_index(I::OffsetInteger{0}) = convert(Int, raw(I))

Expand Down
4 changes: 2 additions & 2 deletions src/primitives/pyramids.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ struct Pyramid{T} <: GeometryPrimitive{3,T}
width::T
end

function coordinates(p::Pyramid{T}, nvertices=nothing) where {T}
function coordinates(p::Pyramid{T}) where {T}
leftup = Point{3,T}(-p.width, p.width, 0) / 2
leftdown = Point(-p.width, -p.width, 0) / 2
tip = Point{3,T}(p.middle + Point{3,T}(0, 0, p.length))
Expand All @@ -16,6 +16,6 @@ function coordinates(p::Pyramid{T}, nvertices=nothing) where {T}
ld, rd]
end

function faces(r::Pyramid, nvertices=nothing) where {FT}
function faces(::Pyramid)
return (TriangleFace(triangle) for triangle in TupleView{3}(1:18))
end
Loading