Skip to content

Instantly share code, notes, and snippets.

@KristofferC
Created March 23, 2015 14:39
Show Gist options
  • Save KristofferC/cf8b21f61f2b33d4b8f2 to your computer and use it in GitHub Desktop.
Save KristofferC/cf8b21f61f2b33d4b8f2 to your computer and use it in GitHub Desktop.
# Hyper spheres are used to bound points in space.
# For a node all it's children are bounded by the
# node's hyper sphere.
immutable HyperSphere{T <: FloatingPoint}
center::Vector{T}
r::T
end
# Checks if two spheres overlap.
# They do if the distance between their centers
# is smaller than the sum of the radiuses
function intersects{T <: FloatingPoint}(s1::HyperSphere{T},
s2::HyperSphere{T},
m::MinkowskiMetric)
d_12 = distance(m, s1.center, s2.center)
d_12 <= s1.r + s2.r
end
# Checks if one shpere completely encloses the other
# It does if the distance between the centers plus the inner
# spheres r is less than the r of the outer sphere
function encloses{T <: FloatingPoint}(s1::HyperSphere{T},
s2::HyperSphere{T},
m::MinkowskiMetric)
d_12 = distance(m, s1.center, s2.center)
d_12 + s1.r <= s2.r
end
# Creates a bounding sphere from two other spheres
function create_bsphere{T <: FloatingPoint}(s1::HyperSphere{T},
s2::HyperSphere{T},
l::Vector{T},
r::Vector{T},
v_12::Vector{T},
m::MinkowskiMetric)
# Unitvector from s1 to s2
@devec v_12[:] = s2.center - s1.center
norm_v_12 = norm(v_12)
@devec v_12[:] = v_12 ./ norm_v_12
# The two points furthest away from the center
@devec l[:] = s1.center - v_12 .* s1.r
@devec r[:] = s2.center + v_12 .* s2.r
# r is half distance between edges
r = distance(m, l, r) / 2.0
center = Array(T, length(v_12))
@devec center[:] = (l + r) ./ 2.0
HyperSphere(center, r)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment