Commit 8a4c5639 authored by Aaron Grabowy's avatar Aaron Grabowy
Browse files

Added minmax; Added ray - inf_cylinder2_boundary intersection

parent 85411dd3
......@@ -144,6 +144,14 @@ template <class A, class B>
return a < b ? b : a;
}
template <class T>
[[nodiscard]] constexpr pair<T, T> minmax(T const& a, T const& b)
{
if (b < a)
return {b, a};
return {a, b};
}
template <class A, class B, class C>
[[nodiscard]] constexpr auto clamp(A const& a, B const& min_value, C const& max_value) -> decltype(min(max(a, min_value), max_value))
{
......
......@@ -587,8 +587,6 @@ template <class ScalarT, class TraitsT>
return detail::intersects_any(r, caps[0], caps[1], boundary_no_caps_of(c));
}
}
// ray - tube
template <class ScalarT>
[[nodiscard]] constexpr ray_hits<2, ScalarT> intersection_parameter(ray<3, ScalarT> const& r, cylinder_boundary_no_caps<3, ScalarT> const& c)
{
......@@ -638,6 +636,27 @@ template <class ScalarT>
return {hits, hit_cnt};
}
// ray - inf_cylinder
template <class ScalarT>
[[nodiscard]] constexpr ray_hits<2, ScalarT> intersection_parameter(ray<2, ScalarT> const& r, inf_cylinder_boundary<2, ScalarT> const& c)
{
const auto n = perpendicular(c.axis.dir);
const auto d = dot(r.dir, n);
if (d == ScalarT(0)) // ray parallel to inf_cylinder
return {};
const auto dist = dot(c.axis.pos - r.origin, n);
const auto [tMin, tMax] = minmax((dist - c.radius) / d, (dist + c.radius) / d);
if (tMin >= ScalarT(0))
return {tMin, tMax};
if (tMax >= ScalarT(0))
return tMax;
return {};
}
// ray - triangle2
template <class ScalarT>
[[nodiscard]] constexpr optional<ray_interval<ScalarT>> intersection_parameter(ray<2, ScalarT> const& r, triangle<2, ScalarT> const& t)
......@@ -725,8 +744,7 @@ template <class ScalarT>
const auto t1 = (-b - sqrtD) / (ScalarT(2) * a);
const auto t2 = (-b + sqrtD) / (ScalarT(2) * a);
auto tMin = min(t1, t2);
auto tMax = max(t1, t2);
const auto [tMin, tMax] = minmax(t1, t2);
if (tMin >= ScalarT(0))
return {tMin, tMax};
......
......@@ -29,6 +29,7 @@ using inf_cylinder_boundary = inf_cylinder<D, ScalarT, boundary_tag>;
template <int D, class ScalarT, class TraitsT>
struct inf_cylinder
{
using scalar_t = ScalarT;
using pos_t = pos<D, ScalarT>;
using dir_t = dir<D, ScalarT>;
using line_t = line<D, ScalarT>;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment