1- function Rect (geometry:: AbstractArray{<:Point{N,T}} ) where {N,T}
2- return Rect {N,T} (geometry)
1+ # Boundingbox-like Rect constructors
2+
3+ Rect (p:: AbstractGeometry{N, T} ) where {N, T} = Rect {N, T} (p)
4+ RectT {T} (p:: AbstractGeometry{N} ) where {N, T} = Rect {N, T} (p)
5+ Rect {N} (p:: AbstractGeometry{_N, T} ) where {N, _N, T} = Rect {N, T} (p)
6+
7+ Rect (p:: AbstractArray{<: VecTypes{N, T}} ) where {N, T} = Rect {N, T} (p)
8+ RectT {T} (p:: AbstractArray{<: VecTypes{N}} ) where {N, T} = Rect {N, T} (p)
9+ Rect {N} (p:: AbstractArray{<: VecTypes{_N, T}} ) where {N, _N, T} = Rect {N, T} (p)
10+
11+ # compat: bounding boxes also defined Rect{T} constructors
12+ # This is not really compatible with Rect{N}...
13+ # How do you even deprecate this?
14+ # @deprecate Rect{T}(x::AbstractGeometry) where {T <: Number} RectT{T}(x) where {T}
15+ # @deprecate Rect{T}(x::AbstractArray) where {T <: Number} RectT{T}(x) where {T}
16+
17+ # Implementations
18+ # Specialize fully typed Rect constructors
19+ Rect {N, T} (p:: AbstractGeometry ) where {N, T} = Rect {N, T} (coordinates (p))
20+
21+ function bbox_dim_check (trg, src:: Integer )
22+ @assert trg isa Integer " Rect{$trg , $T1 } is invalid. This may have happened due to calling Rect{$N1 }(obj) to get a bounding box."
23+ if trg < src
24+ throw (ArgumentError (" Cannot construct a $trg dimensional bounding box from $src dimensional Points. ($trg must be ≥ $src )" ))
25+ end
326end
427
528"""
6- Rect(points::AbstractArray{<: Point })
29+ Rect(points::AbstractArray{<: VecTypes })
730
831Construct a bounding box containing all the given points.
932"""
10- function Rect {N1,T1} (geometry:: AbstractArray{PT} ) where {N1,T1,PT<: Point }
11- N2, T2 = length (PT), eltype (PT)
12- @assert N1 >= N2
33+ function Rect {N1, T1} (points:: AbstractArray{<: VecTypes{N2, T2}} ) where {N1, T1, N2, T2}
34+ bbox_dim_check (N1, N2)
1335 vmin = Point {N2,T2} (typemax (T2))
1436 vmax = Point {N2,T2} (typemin (T2))
15- for p in geometry
37+ for p in points
1638 vmin, vmax = _minmax (p, vmin, vmax)
1739 end
1840 o = vmin
@@ -25,29 +47,25 @@ function Rect{N1,T1}(geometry::AbstractArray{PT}) where {N1,T1,PT<:Point}
2547 end
2648end
2749
50+
2851"""
2952 Rect(primitive::GeometryPrimitive)
3053
3154Construct a bounding box for the given primitive.
3255"""
33- function Rect (primitive:: GeometryPrimitive{N,T} ) where {N,T}
34- return Rect {N,T} (primitive)
35- end
36-
37- function Rect {T} (primitive:: GeometryPrimitive{N,T} ) where {N,T}
38- return Rect {N,T} (primitive)
39- end
40-
41- function Rect {T} (a:: Pyramid ) where {T}
42- w, h = a. width / T (2 ), a. length
56+ function Rect {N, T} (a:: Pyramid ) where {N, T}
57+ bbox_dim_check (N, 3 )
58+ w, h = a. width, a. length
4359 m = Vec {3,T} (a. middle)
44- return Rect {T} (m .- Vec {3,T} (w, w, 0 ), m .+ Vec {3,T} (w, w, h))
60+ return Rect {N, T} (m .- Vec {3,T} (w / T ( 2 ) , w / T ( 2 ) , 0 ), Vec {3,T} (w, w, h))
4561end
4662
47- function Rect {T} (a:: Sphere ) where {T}
63+ function Rect {N, T} (a:: HyperSphere ) where {N, T}
4864 mini, maxi = extrema (a)
49- return Rect {T} (mini, maxi .- mini)
65+ return Rect {N, T} (mini, maxi .- mini)
5066end
5167
52- Rect {T} (a) where {T} = Rect {T} (coordinates (a))
53- Rect {N,T} (a) where {N,T} = Rect {N,T} (coordinates (a))
68+ # TODO : exact implementation that doesn't rely on coordinates
69+ # function Rect{N, T}(a::Cylinder) where {N, T}
70+ # return Rect{N, T}(...)
71+ # end
0 commit comments