Add ray-sphere intersection
This commit is contained in:
parent
47e3970deb
commit
4e16f5d9bf
1 changed files with 15 additions and 0 deletions
|
|
@ -5,6 +5,7 @@
|
|||
#include <psemek/geom/simplex.hpp>
|
||||
#include <psemek/geom/ray.hpp>
|
||||
#include <psemek/geom/box.hpp>
|
||||
#include <psemek/geom/sphere.hpp>
|
||||
#include <psemek/geom/orientation.hpp>
|
||||
#include <psemek/geom/contains.hpp>
|
||||
#include <psemek/geom/gauss.hpp>
|
||||
|
|
@ -209,4 +210,18 @@ namespace psemek::geom
|
|||
return static_cast<bool>(intersection(seg, s));
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
std::optional<std::pair<T, T>> intersection(ray<T, N> const & r, sphere<T, N> const & s)
|
||||
{
|
||||
auto d = r.origin - s.center;
|
||||
|
||||
return solve_quadratic(length_sqr(r.direction), T{2} * dot(r.direction, d), length_sqr(d) - sqr(s.radius));
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N, typename = std::enable_if_t<(N > 2)>>
|
||||
bool intersect(ray<T, N> const & r, sphere<T, N> const & s)
|
||||
{
|
||||
return static_cast<bool>(intersection(r, s));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue