Commit d9ffba38 authored by Aaron Grabowy's avatar Aaron Grabowy
Browse files

Added intersects aabb for line2, ray2, and segment2

parent 9c4a0829
......@@ -1154,6 +1154,37 @@ template <int D, class ScalarT>
// ====================================== Checks if Object Intersects aabb ======================================
template <class ScalarT>
[[nodiscard]] constexpr bool intersects(line<2, ScalarT> const& l, aabb<2, ScalarT> const& b)
{
auto const c = centroid_of(b);
auto const shadow = dot(b.max - c, abs(perpendicular(l.dir)));
return pow2(shadow) >= distance_sqr(c, l);
}
template <class ScalarT>
[[nodiscard]] constexpr bool intersects(ray<2, ScalarT> const& r, aabb<2, ScalarT> const& b)
{
auto const p = r.origin;
auto const d = r.dir;
if ((p.x > b.max.x && d.x >= ScalarT(0)) || (p.x < b.min.x && d.x <= ScalarT(0)) ||
(p.y > b.max.y && d.y >= ScalarT(0)) || (p.y < b.min.y && d.y <= ScalarT(0)))
return false;
return intersects(inf_of(r), b);
}
template <class ScalarT>
[[nodiscard]] constexpr bool intersects(segment<2, ScalarT> const& s, aabb<2, ScalarT> const& b)
{
auto pMin = min(s.pos0, s.pos1);
auto pMax = max(s.pos0, s.pos1);
if (pMin.x > b.max.x || pMax.x < b.min.x || pMin.y > b.max.y || pMax.y < b.min.y)
return false;
return intersects(inf_of(s), b);
}
template <int D, class ScalarT>
[[nodiscard]] constexpr bool intersects(sphere<D, ScalarT> const& a, aabb<D, ScalarT> const& b)
{
......
......@@ -31,27 +31,23 @@ template <int D, class ScalarT>
template <class ScalarT, class TraitsT>
[[nodiscard]] constexpr array<pos<2, ScalarT>, 4> vertices_of(aabb<2, ScalarT, TraitsT> const& bb)
{
auto p00 = pos<2, ScalarT>(bb.min.x, bb.min.y);
auto p10 = pos<2, ScalarT>(bb.max.x, bb.min.y);
auto p11 = pos<2, ScalarT>(bb.max.x, bb.max.y);
auto p01 = pos<2, ScalarT>(bb.min.x, bb.max.y);
return {{p00, p10, p11, p01}}; // in ccw order
return {{bb.min, p10, bb.max, p01}}; // in ccw order
}
template <class ScalarT, class TraitsT>
[[nodiscard]] constexpr array<pos<3, ScalarT>, 8> vertices_of(aabb<3, ScalarT, TraitsT> const& bb)
{
auto p000 = pos<3, ScalarT>(bb.min.x, bb.min.y, bb.min.z);
auto p001 = pos<3, ScalarT>(bb.min.x, bb.min.y, bb.max.z);
auto p010 = pos<3, ScalarT>(bb.min.x, bb.max.y, bb.min.z);
auto p011 = pos<3, ScalarT>(bb.min.x, bb.max.y, bb.max.z);
auto p100 = pos<3, ScalarT>(bb.max.x, bb.min.y, bb.min.z);
auto p101 = pos<3, ScalarT>(bb.max.x, bb.min.y, bb.max.z);
auto p110 = pos<3, ScalarT>(bb.max.x, bb.max.y, bb.min.z);
auto p111 = pos<3, ScalarT>(bb.max.x, bb.max.y, bb.max.z);
return {{p000, p001, p010, p011, p100, p101, p110, p111}};
return {{bb.min, p001, p010, p011, p100, p101, p110, bb.max}};
}
template <class ScalarT, int DomainD, class TraitsT>
......
......@@ -19,6 +19,7 @@ namespace tg
// abs
TG_IMPL_DEFINE_COMPWISE_FUNC_UNARY(pos, abs);
TG_IMPL_DEFINE_COMPWISE_FUNC_UNARY(vec, abs);
TG_IMPL_DEFINE_COMPWISE_FUNC_UNARY(dir, abs);
TG_IMPL_DEFINE_COMPWISE_FUNC_UNARY(size, abs);
TG_IMPL_DEFINE_COMPWISE_FUNC_UNARY(comp, abs);
......
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