forked from JuliaGeometry/GeometryBasics.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboundingboxes.jl
46 lines (40 loc) · 1.18 KB
/
boundingboxes.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function Rect(geometry::AbstractArray{<:Point{N,T}}) where {N,T}
return Rect{N,T}(geometry)
end
"""
Construct a HyperRectangle enclosing all points.
"""
function Rect{N1,T1}(geometry::AbstractArray{PT}) where {N1,T1,PT<:AbstractPoint}
N2, T2 = length(PT), eltype(PT)
@assert N1 >= N2
vmin = Point{N2,T2}(typemax(T2))
vmax = Point{N2,T2}(typemin(T2))
for p in geometry
vmin, vmax = minmax(p, vmin, vmax)
end
o = vmin
w = vmax - vmin
return if N1 > N2
z = zero(Vec{N1 - N2,T1})
Rect{N1,T1}(vcat(o, z), vcat(w, z))
else
Rect{N1,T1}(o, w)
end
end
function Rect(primitive::GeometryPrimitive{N,T}) where {N,T}
return Rect{N,T}(primitive)
end
function Rect{T}(primitive::GeometryPrimitive{N,T}) where {N,T}
return Rect{N,T}(primitive)
end
function Rect{T}(a::Pyramid) where {T}
w, h = a.width / T(2), a.length
m = Vec{3,T}(a.middle)
return Rect{T}(m .- Vec{3,T}(w, w, 0), m .+ Vec{3,T}(w, w, h))
end
function Rect{T}(a::Sphere) where {T}
mini, maxi = extrema(a)
return Rect{T}(mini, maxi .- mini)
end
Rect{T}(a) where {T} = Rect{T}(coordinates(a))
Rect{N,T}(a) where {N,T} = Rect{N,T}(coordinates(a))