Skip to content

Commit 9550977

Browse files
dont use macro for aliases and introduce more variants (Float64 -> d, UInt -> ui) (#214)
* dont use maccro for aliases * add a section about aliases * add convert for different Rect eltypes * export Float64 Rect types --------- Co-authored-by: ffreyer <[email protected]>
1 parent bc1ce58 commit 9550977

File tree

6 files changed

+207
-33
lines changed

6 files changed

+207
-33
lines changed

docs/src/index.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,40 @@ Use `GeometryBasics.mesh` to get a mesh directly from a geometry:
106106
```@repl quickstart
107107
mesh = GeometryBasics.mesh(rect)
108108
```
109+
110+
111+
## Aliases
112+
113+
GeometryBasics exports common aliases for Point, Vec, Mat and Rect:
114+
115+
### Vec
116+
117+
| |`T`(eltype) |`Float64` |`Float32` |`Int` |`UInt` |
118+
|--------|------------|----------|----------|----------|----------|
119+
|`N`(dim)|`Vec{N,T}` |`Vecd{N}` |`Vecf{N}` |`Veci{N}` |`Vecui{N}`|
120+
|`2` |`Vec2{T}` |`Vec2d` |`Vec2f` |`Vec2i` |`Vec2ui` |
121+
|`3` |`Vec3{T}` |`Vec3d` |`Vec3f` |`Vec3i` |`Vec3ui` |
122+
123+
### Point
124+
125+
| |`T`(eltype) |`Float64` |`Float32` |`Int` |`UInt` |
126+
|--------|------------|----------|----------|----------|----------|
127+
|`N`(dim)|`Point{N,T}`|`Pointd{N}`|`Pointf{N}`|`Pointi{N}`|`Pointui{N}`|
128+
|`2` |`Point2{T}` |`Point2d` |`Point2f` |`Point2i` |`Point2ui`|
129+
|`3` |`Point3{T}` |`Point3d` |`Point3f` |`Point3i` |`Point3ui`|
130+
131+
### Mat
132+
133+
| |`T`(eltype) |`Float64` |`Float32` |`Int` |`UInt` |
134+
|--------|------------|----------|----------|----------|----------|
135+
|`N`(dim)|`Mat{N,T}` |`Matd{N}` |`Matf{N}` |`Mati{N}` |`Matui{N}`|
136+
|`2` |`Mat2{T}` |`Mat2d` |`Mat2f` |`Mat2i` |`Mat2ui` |
137+
|`3` |`Mat3{T}` |`Mat3d` |`Mat3f` |`Mat3i` |`Mat3ui` |
138+
139+
### Rect
140+
141+
| |`T`(eltype) |`Float64` |`Float32` |`Int` |`UInt` |
142+
|--------|------------|----------|----------|----------|----------|
143+
|`N`(dim)|`Rect{N,T}` |`Rectd{N}`|`Rectf{N}`|`Recti{N}`|`Rectui{N}`|
144+
|`2` |`Rect2{T}` |`Rect2d` |`Rect2f` |`Rect2i` |`Rect2ui` |
145+
|`3` |`Rect3{T}` |`Rect3d` |`Rect3f` |`Rect3i` |`Rect3ui` |

src/GeometryBasics.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export uv_mesh, normal_mesh, uv_normal_mesh
6666
export height, origin, radius, width, widths
6767
export HyperSphere, Circle, Sphere
6868
export Cylinder, Cylinder2, Cylinder3, Pyramid, extremity
69-
export HyperRectangle, Rect, Rect2, Rect3, Recti, Rect2i, Rect3i, Rectf, Rect2f, Rect3f
69+
export HyperRectangle, Rect, Rect2, Rect3, Recti, Rect2i, Rect3i, Rectf, Rect2f, Rect3f, Rectd, Rect2d, Rect3d
7070
export before, during, meets, overlaps, intersects, finishes
7171
export centered, direction, area, volume, update
7272
export max_dist_dim, max_euclidean, max_euclideansq, min_dist_dim, min_euclidean

src/basic_types.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ abstract type AbstractSimplex{Dim,N,T} <: StaticVector{Dim,T} end
2424
Face index, connecting points to form a simplex
2525
"""
2626

27-
@fixed_vector SimplexFace AbstractSimplexFace
27+
@fixed_vector SimplexFace = AbstractSimplexFace
2828
const TetrahedronFace{T} = SimplexFace{4,T}
2929
Face(::Type{<:SimplexFace{N}}, ::Type{T}) where {N,T} = SimplexFace{N,T}
3030

3131
"""
3232
Face index, connecting points to form an Ngon
3333
"""
3434

35-
@fixed_vector NgonFace AbstractNgonFace
35+
@fixed_vector NgonFace = AbstractNgonFace
3636
const LineFace{T} = NgonFace{2,T}
3737
const TriangleFace{T} = NgonFace{3,T}
3838
const QuadFace{T} = NgonFace{4,T}

src/fixed_arrays.jl

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ function unit(::Type{T}, i::Integer) where {T <: StaticVector}
66
return T(tup)
77
end
88

9-
macro fixed_vector(name, parent)
9+
macro fixed_vector(name_parent)
10+
@assert name_parent.head == :(=)
11+
name, parent = name_parent.args
1012
expr = quote
1113
struct $(name){S,T} <: $(parent){S,T}
1214
data::NTuple{S,T}
@@ -116,37 +118,46 @@ macro fixed_vector(name, parent)
116118
end
117119

118120
abstract type AbstractPoint{Dim,T} <: StaticVector{Dim,T} end
119-
@fixed_vector Point AbstractPoint
120-
@fixed_vector Vec StaticVector
121+
122+
@fixed_vector Point = AbstractPoint
123+
@fixed_vector Vec = StaticVector
124+
125+
121126

122127
const Mat = SMatrix
123128
const VecTypes{N,T} = Union{StaticVector{N,T},NTuple{N,T}}
124129
const Vecf{N} = Vec{N,Float32}
125130
const Pointf{N} = Point{N,Float32}
131+
126132
Base.isnan(p::Union{AbstractPoint,Vec}) = any(isnan, p)
127133
Base.isinf(p::Union{AbstractPoint,Vec}) = any(isinf, p)
128134
Base.isfinite(p::Union{AbstractPoint,Vec}) = all(isfinite, p)
129135

130-
for i in 1:4
131-
for T in [:Point, :Vec]
132-
name = Symbol("$T$i")
133-
namef = Symbol("$T$(i)f")
134-
@eval begin
135-
const $name = $T{$i}
136-
const $namef = $T{$i,Float32}
137-
export $name
138-
export $namef
136+
## Generate aliases
137+
## As a text file instead of eval/macro, to not confuse code linter
138+
139+
#=
140+
open(joinpath(@__DIR__, "generated-aliases.jl"), "w") do io
141+
for i in 1:4
142+
for T in [:Point, :Vec, :Mat]
143+
namei = "$T$i"
144+
res = T == :Mat ? "Mat{$i,$i,T,$(i * i)}" : "$T{$i,T}"
145+
println(io, "const $(namei){T} = $res")
146+
println(io, "export $namei")
147+
for (postfix, t) in ["d" => Float64, "f" => Float32, "i" => Int, "ui" => UInt]
148+
namep = "$T$i$postfix"
149+
println(io, "const $(namep) = $(namei){$t}")
150+
println(io, "export $namep")
151+
# mnamep = "$(mname)$postfix"
152+
# println(io, "const $mnamep = $mname{$t}")
153+
# println(io, "export $mnamep")
154+
end
139155
end
140156
end
141-
name = Symbol("Mat$i")
142-
namef = Symbol("Mat$(i)f")
143-
@eval begin
144-
const $name{T} = $Mat{$i,$i,T,$(i * i)}
145-
const $namef = $name{Float32}
146-
export $name
147-
export $namef
148-
end
149157
end
158+
=#
159+
160+
include("generated-aliases.jl")
150161

151162
export Mat, Vec, Point, unit
152163
export Vecf, Pointf

src/generated-aliases.jl

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
const Point1{T} = Point{1,T}
2+
export Point1
3+
const Point1d = Point1{Float64}
4+
export Point1d
5+
const Point1f = Point1{Float32}
6+
export Point1f
7+
const Point1i = Point1{Int64}
8+
export Point1i
9+
const Point1ui = Point1{UInt64}
10+
export Point1ui
11+
const Vec1{T} = Vec{1,T}
12+
export Vec1
13+
const Vec1d = Vec1{Float64}
14+
export Vec1d
15+
const Vec1f = Vec1{Float32}
16+
export Vec1f
17+
const Vec1i = Vec1{Int64}
18+
export Vec1i
19+
const Vec1ui = Vec1{UInt64}
20+
export Vec1ui
21+
const Mat1{T} = Mat{1,1,T,1}
22+
export Mat1
23+
const Mat1d = Mat1{Float64}
24+
export Mat1d
25+
const Mat1f = Mat1{Float32}
26+
export Mat1f
27+
const Mat1i = Mat1{Int64}
28+
export Mat1i
29+
const Mat1ui = Mat1{UInt64}
30+
export Mat1ui
31+
const Point2{T} = Point{2,T}
32+
export Point2
33+
const Point2d = Point2{Float64}
34+
export Point2d
35+
const Point2f = Point2{Float32}
36+
export Point2f
37+
const Point2i = Point2{Int64}
38+
export Point2i
39+
const Point2ui = Point2{UInt64}
40+
export Point2ui
41+
const Vec2{T} = Vec{2,T}
42+
export Vec2
43+
const Vec2d = Vec2{Float64}
44+
export Vec2d
45+
const Vec2f = Vec2{Float32}
46+
export Vec2f
47+
const Vec2i = Vec2{Int64}
48+
export Vec2i
49+
const Vec2ui = Vec2{UInt64}
50+
export Vec2ui
51+
const Mat2{T} = Mat{2,2,T,4}
52+
export Mat2
53+
const Mat2d = Mat2{Float64}
54+
export Mat2d
55+
const Mat2f = Mat2{Float32}
56+
export Mat2f
57+
const Mat2i = Mat2{Int64}
58+
export Mat2i
59+
const Mat2ui = Mat2{UInt64}
60+
export Mat2ui
61+
const Point3{T} = Point{3,T}
62+
export Point3
63+
const Point3d = Point3{Float64}
64+
export Point3d
65+
const Point3f = Point3{Float32}
66+
export Point3f
67+
const Point3i = Point3{Int64}
68+
export Point3i
69+
const Point3ui = Point3{UInt64}
70+
export Point3ui
71+
const Vec3{T} = Vec{3,T}
72+
export Vec3
73+
const Vec3d = Vec3{Float64}
74+
export Vec3d
75+
const Vec3f = Vec3{Float32}
76+
export Vec3f
77+
const Vec3i = Vec3{Int64}
78+
export Vec3i
79+
const Vec3ui = Vec3{UInt64}
80+
export Vec3ui
81+
const Mat3{T} = Mat{3,3,T,9}
82+
export Mat3
83+
const Mat3d = Mat3{Float64}
84+
export Mat3d
85+
const Mat3f = Mat3{Float32}
86+
export Mat3f
87+
const Mat3i = Mat3{Int64}
88+
export Mat3i
89+
const Mat3ui = Mat3{UInt64}
90+
export Mat3ui
91+
const Point4{T} = Point{4,T}
92+
export Point4
93+
const Point4d = Point4{Float64}
94+
export Point4d
95+
const Point4f = Point4{Float32}
96+
export Point4f
97+
const Point4i = Point4{Int64}
98+
export Point4i
99+
const Point4ui = Point4{UInt64}
100+
export Point4ui
101+
const Vec4{T} = Vec{4,T}
102+
export Vec4
103+
const Vec4d = Vec4{Float64}
104+
export Vec4d
105+
const Vec4f = Vec4{Float32}
106+
export Vec4f
107+
const Vec4i = Vec4{Int64}
108+
export Vec4i
109+
const Vec4ui = Vec4{UInt64}
110+
export Vec4ui
111+
const Mat4{T} = Mat{4,4,T,16}
112+
export Mat4
113+
const Mat4d = Mat4{Float64}
114+
export Mat4d
115+
const Mat4f = Mat4{Float32}
116+
export Mat4f
117+
const Mat4i = Mat4{Int64}
118+
export Mat4i
119+
const Mat4ui = Mat4{UInt64}
120+
export Mat4ui

src/primitives/rectangles.jl

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,37 @@ end
1919
2020
A rectangle in N dimensions, formally the cartesian product of intervals. See also [`HyperRectangle`](@ref). Its aliases are
2121
22-
| |`T`(eltype)|`Float32` |`Int` |
23-
|--------|-----------|----------|----------|
24-
|`N`(dim)|`Rect{N,T}`|`Rectf{N}`|`Recti{N}`|
25-
|`2` |`Rect2{T}` |`Rect2f` |`Rect2i` |
26-
|`3` |`Rect3{T}` |`Rect3f` |`Rect3i` |
22+
| |`T`(eltype)|`Float64` |`Float32` |`Int` |
23+
|--------|-----------|----------|----------|----------|
24+
|`N`(dim)|`Rect{N,T}`|`Rectd{N}`|`Rectf{N}`|`Recti{N}`|
25+
|`2` |`Rect2{T}` |`Rect2d` |`Rect2f` |`Rect2i` |
26+
|`3` |`Rect3{T}` |`Rect3d` |`Rect3f` |`Rect3i` |
2727
2828
There is an additional unexported alias `RectT` that simply reverses the order of type parameters: `RectT{T,N} == Rect{N,T}`.
2929
3030
"""
31-
Rect, Rect2, Rect3, RectT, Rectf, Rect2f, Rect3f, Recti, Rect2i, Rect3i
31+
Rect, Rect2, Rect3, RectT, Rectd, Rect2d, Rect3d, Rectf, Rect2f, Rect3f, Recti, Rect2i, Rect3i
3232

3333
const Rect{N,T} = HyperRectangle{N,T}
3434
const Rect2{T} = Rect{2,T}
3535
const Rect3{T} = Rect{3,T}
36-
const RectT{T} = Rect{N,T} where {N}
36+
37+
const RectT{T,N} = Rect{N,T}
38+
39+
const Rectd{N} = Rect{N,Float64}
40+
const Rect2d = Rect2{Float64}
41+
const Rect3d = Rect3{Float64}
3742

3843
const Rectf{N} = Rect{N,Float32}
3944
const Rect2f = Rect2{Float32}
4045
const Rect3f = Rect3{Float32}
4146

42-
const Recti{N} = HyperRectangle{N,Int}
47+
const Recti{N} = Rect{N,Int}
4348
const Rect2i = Rect2{Int}
4449
const Rect3i = Rect3{Int}
4550

4651
Rect() = Rect{2,Float32}()
47-
4852
RectT{T}() where {T} = Rect{2,T}()
49-
5053
Rect{N}() where {N} = Rect{N,Float32}()
5154

5255
function Rect{N,T}() where {T,N}
@@ -171,6 +174,9 @@ function Rect3f(x::Tuple{Tuple{<:Number,<:Number,<:Number},
171174
return Rect3f(Vec3f(x[1]...), Vec3f(x[2]...))
172175
end
173176

177+
# allow auto-conversion between different eltypes
178+
Base.convert(::Type{Rect{N, T}}, r::Rect{N}) where {N, T} = Rect{N, T}(r)
179+
174180
origin(prim::Rect) = prim.origin
175181
Base.maximum(prim::Rect) = origin(prim) + widths(prim)
176182
Base.minimum(prim::Rect) = origin(prim)

0 commit comments

Comments
 (0)