Skip to content
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

add macro to generate aliases #213

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions src/GeometryBasics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ using EarCut_jll

using Base: @propagate_inbounds

include("utils.jl")

include("fixed_arrays.jl")
include("offsetintegers.jl")
include("basic_types.jl")
Expand Down
15 changes: 3 additions & 12 deletions src/fixed_arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,12 @@ abstract type AbstractPoint{Dim,T} <: StaticVector{Dim,T} end

const Mat = SMatrix
const VecTypes{N,T} = Union{StaticVector{N,T},NTuple{N,T}}
const Vecf{N} = Vec{N,Float32}
const Pointf{N} = Point{N,Float32}
Base.isnan(p::Union{AbstractPoint,Vec}) = any(x -> isnan(x), p)

@alias(Point,1:4,Float32)
@alias(Vec,1:4,Float32)

for i in 1:4
for T in [:Point, :Vec]
name = Symbol("$T$i")
namef = Symbol("$T$(i)f")
@eval begin
const $name = $T{$i}
const $namef = $T{$i,Float32}
export $name
export $namef
end
end
name = Symbol("Mat$i")
namef = Symbol("Mat$(i)f")
@eval begin
Expand Down
5 changes: 3 additions & 2 deletions src/primitives/cylinders.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ end
A `Cylinder2` or `Cylinder3` is a 2D/3D cylinder defined by its origin point,
its extremity and a radius. `origin`, `extremity` and `r`, must be specified.
"""
const Cylinder2{T} = Cylinder{2,T}
const Cylinder3{T} = Cylinder{3,T}
Cylinder2, Cylinder3

@alias(Cylinder,2:3)

origin(c::Cylinder{N,T}) where {N,T} = c.origin
extremity(c::Cylinder{N,T}) where {N,T} = c.extremity
Expand Down
11 changes: 1 addition & 10 deletions src/primitives/rectangles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,8 @@ There is an additional unexported alias `RectT` that simply reverses the order o
Rect, Rect2, Rect3, RectT, Rectf, Rect2f, Rect3f, Recti, Rect2i, Rect3i

const Rect{N,T} = HyperRectangle{N,T}
const Rect2{T} = Rect{2,T}
const Rect3{T} = Rect{3,T}
const RectT{T} = Rect{N,T} where {N}

const Rectf{N} = Rect{N,Float32}
const Rect2f = Rect2{Float32}
const Rect3f = Rect3{Float32}

const Recti{N} = HyperRectangle{N,Int}
const Rect2i = Rect2{Int}
const Rect3i = Rect3{Int}
@alias(Rect,2:3,Int,Float32)

Rect() = Rect{2,Float32}()

Expand Down
27 changes: 27 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const typesuffix = Dict(Float32=>"f", Int=>"i")

macro alias(T, dims, types...)
for i in eval(dims)
name = Symbol("$T$i")
@eval begin
const $name = $T{$i}
export $name
end
end
for t in eval(types)
name = Symbol("$T$(typesuffix[eval(t)])")
@eval begin
const $name{N} = $T{N,$t}
export $name
end
end
for i in eval(dims)
for t in eval(types)
name = Symbol("$T$(i)$(typesuffix[eval(t)])")
@eval begin
const $name = $T{$i,$t}
export $name
end
end
end
end
Loading