Skip to content

Commit 263bce4

Browse files
committed
remove dependency on GeometryTypes
1 parent 87f250e commit 263bce4

File tree

5 files changed

+171
-6
lines changed

5 files changed

+171
-6
lines changed

REQUIRE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
StaticArrays
2-
GeometryTypes
32
Tables
43
StructArrays

src/FixedSizeArrays.jl

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
"""
2+
Compatibility layer for transferring from FixedSizeArrays. This provides
3+
alternative definitions of `Vec`, `Mat`, `Point`, `FixedVectorNoTuple`, `@fsa`,
4+
etc, using StaticArrays as a backend.
5+
The type definitions are not "perfect" matches because the type parameters are
6+
different. However, it should cover common method signatures and constructors.
7+
"""
8+
module FixedSizeArrays
9+
10+
using StaticArrays
11+
12+
export FixedArray
13+
export FixedVector
14+
export FixedMatrix
15+
export Mat
16+
export Vec
17+
export Point
18+
export @fsa
19+
export FixedVectorNoTuple
20+
21+
const FixedArray = StaticArray
22+
const FixedVector = StaticVector
23+
const FixedMatrix = StaticMatrix
24+
const Mat = SMatrix
25+
const FixedVectorNoTuple = FieldVector
26+
27+
function fsa_ast(ex)
28+
@assert isa(ex, Expr)
29+
if ex.head == :vect # Vector
30+
return Expr(:call, SVector{length(ex.args)}, Expr(:tuple, ex.args...))
31+
elseif ex.head == :hcat # 1 x n
32+
s1 = 1
33+
s2 = length(ex.args)
34+
return Expr(:call, SMatrix{s1, s2}, Expr(:tuple, ex.args...))
35+
elseif ex.head == :vcat
36+
if isa(ex.args[1], Expr) && ex.args[1].head == :row # n x m
37+
# Validate
38+
s1 = length(ex.args)
39+
s2s = map(i -> ((isa(ex.args[i], Expr) && ex.args[i].head == :row) ? length(ex.args[i].args) : 1), 1:s1)
40+
s2 = minimum(s2s)
41+
if maximum(s2s) != s2
42+
error("Rows must be of matching lengths")
43+
end
44+
45+
exprs = [ex.args[i].args[j] for i = 1:s1, j = 1:s2]
46+
return Expr(:call, SMatrix{s1, s2}, Expr(:tuple, exprs...))
47+
else # n x 1
48+
return Expr(:call, SMatrix{length(ex.args), 1}, Expr(:tuple, ex.args...))
49+
end
50+
end
51+
end
52+
macro fsa(ex)
53+
expr = fsa_ast(ex)
54+
esc(expr)
55+
end
56+
57+
function unit(::Type{T}, i::Integer) where T <: StaticVector
58+
T(ntuple(Val(length(T))) do j
59+
ifelse(i == j, 1, 0)
60+
end)
61+
end
62+
63+
export unit
64+
65+
macro fixed_vector(name, parent)
66+
esc(quote
67+
struct $(name){S, T} <: $(parent){S, T}
68+
data::NTuple{S, T}
69+
70+
function $(name){S, T}(x::NTuple{S,T}) where {S, T}
71+
new{S, T}(x)
72+
end
73+
74+
function $(name){S, T}(x::NTuple{S,Any}) where {S, T}
75+
new{S, T}(StaticArrays.convert_ntuple(T, x))
76+
end
77+
end
78+
size_or(::Type{$(name)}, or) = or
79+
eltype_or(::Type{$(name)}, or) = or
80+
eltype_or(::Type{$(name){S, T} where S}, or) where {T} = T
81+
eltype_or(::Type{$(name){S, T} where T}, or) where {S} = or
82+
eltype_or(::Type{$(name){S, T}}, or) where {S, T} = T
83+
84+
size_or(::Type{$(name){S, T} where S}, or) where {T} = or
85+
size_or(::Type{$(name){S, T} where T}, or) where {S} = Size{(S,)}()
86+
size_or(::Type{$(name){S, T}}, or) where {S, T} = (S,)
87+
# Array constructor
88+
@inline function $(name){S}(x::AbstractVector{T}) where {S, T}
89+
@assert S <= length(x)
90+
$(name){S, T}(ntuple(i-> x[i], Val(S)))
91+
end
92+
@inline function $(name){S, T1}(x::AbstractVector{T2}) where {S, T1, T2}
93+
@assert S <= length(x)
94+
$(name){S, T1}(ntuple(i-> T1(x[i]), Val(S)))
95+
end
96+
97+
@inline function $(name){S, T}(x) where {S, T}
98+
$(name){S, T}(ntuple(i-> T(x), Val(S)))
99+
end
100+
101+
102+
@inline function $(name){S}(x::T) where {S, T}
103+
$(name){S, T}(ntuple(i-> x, Val(S)))
104+
end
105+
@inline function $(name){1, T}(x::T) where T
106+
$(name){1, T}((x,))
107+
end
108+
@inline $(name)(x::NTuple{S}) where {S} = $(name){S}(x)
109+
@inline $(name)(x::T) where {S, T <: Tuple{Vararg{Any, S}}} = $(name){S, StaticArrays.promote_tuple_eltype(T)}(x)
110+
@inline function $(name){S}(x::T) where {S, T <: Tuple}
111+
$(name){S, StaticArrays.promote_tuple_eltype(T)}(x)
112+
end
113+
$(name){S, T}(x::StaticVector) where {S, T} = $(name){S, T}(Tuple(x))
114+
@generated function (::Type{$(name){S, T}})(x::$(name)) where {S, T}
115+
idx = [:(x[$i]) for i = 1:S]
116+
quote
117+
$($(name)){S, T}($(idx...))
118+
end
119+
end
120+
@generated function convert(::Type{$(name){S, T}}, x::$(name)) where {S, T}
121+
idx = [:(x[$i]) for i = 1:S]
122+
quote
123+
$($(name)){S, T}($(idx...))
124+
end
125+
end
126+
@generated function (::Type{SV})(x::StaticVector) where SV <: $(name)
127+
len = size_or(SV, size(x))[1]
128+
if length(x) == len
129+
:(SV(Tuple(x)))
130+
elseif length(x) > len
131+
elems = [:(x[$i]) for i = 1:len]
132+
:(SV($(Expr(:tuple, elems...))))
133+
else
134+
error("Static Vector too short: $x, target type: $SV")
135+
end
136+
end
137+
138+
Base.@pure StaticArrays.Size(::Type{$(name){S, Any}}) where {S} = Size(S)
139+
Base.@pure StaticArrays.Size(::Type{$(name){S, T}}) where {S,T} = Size(S)
140+
141+
Base.@propagate_inbounds function Base.getindex(v::$(name){S, T}, i::Int) where {S, T}
142+
v.data[i]
143+
end
144+
@inline Base.Tuple(v::$(name)) = v.data
145+
@inline Base.convert(::Type{$(name){S, T}}, x::NTuple{S, T}) where {S, T} = $(name){S, T}(x)
146+
@inline function Base.convert(::Type{$(name){S, T}}, x::Tuple) where {S, T}
147+
$(name){S, T}(convert(NTuple{S, T}, x))
148+
end
149+
150+
@generated function StaticArrays.similar_type(::Type{SV}, ::Type{T}, s::Size{S}) where {SV <: $(name), T, S}
151+
if length(S) === 1
152+
$(name){S[1], T}
153+
else
154+
StaticArrays.default_similar_type(T,s(),Val{length(S)})
155+
end
156+
end
157+
158+
end)
159+
end
160+
161+
export @fixed_vector
162+
163+
end

src/GeometryBasics.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
module GeometryBasics
22

33
using StaticArrays, Tables, StructArrays
4-
import GeometryTypes
4+
55
using Base: @propagate_inbounds
66

7+
include("FixedSizeArrays.jl")
8+
using .FixedSizeArrays
9+
710
include("basic_types.jl")
811
include("metadata.jl")
912
include("viewtypes.jl")

src/basic_types.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ abstract type AbstractNgonFace{Dim, T} <: AbstractFace{Dim, T} end
1818

1919
abstract type AbstractSimplex{Dim, N, T} <: StaticVector{Dim, T} end
2020

21-
GeometryTypes.@fixed_vector Point AbstractPoint
21+
@fixed_vector Point AbstractPoint
2222

2323

2424

@@ -27,7 +27,7 @@ GeometryTypes.@fixed_vector Point AbstractPoint
2727
Face index, connecting points to form a simplex
2828
"""
2929

30-
GeometryTypes.@fixed_vector SimplexFace AbstractSimplexFace
30+
@fixed_vector SimplexFace AbstractSimplexFace
3131
const LineFace{T} = SimplexFace{2, T}
3232
Base.show(io::IO, x::Type{<: LineFace{T}}) where T = print(io, "LineFace{", T, "}")
3333
Face(::Type{<: SimplexFace{N}}, ::Type{T}) where {N, T} = SimplexFace{N, T}
@@ -38,7 +38,7 @@ Face(::Type{<: SimplexFace{N}}, ::Type{T}) where {N, T} = SimplexFace{N, T}
3838
Face index, connecting points to form an Ngon
3939
"""
4040

41-
GeometryTypes.@fixed_vector NgonFace AbstractNgonFace
41+
@fixed_vector NgonFace AbstractNgonFace
4242
const TriangleFace{T} = NgonFace{3, T}
4343
Base.show(io::IO, x::Type{<: TriangleFace{T}}) where T = print(io, "TriangleFace{", T, "}")
4444
Face(::Type{<: NgonFace{N}}, ::Type{T}) where {N, T} = NgonFace{N, T}

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ end
122122
end
123123

124124
@testset "Mesh" begin
125-
125+
126126
numbers = [1, 2, 3, 4, 5, 6]
127127
points = connect(numbers, Point{2})
128128

0 commit comments

Comments
 (0)