// David Eberly, Geometric Tools, Redmond WA 98052 // Copyright (c) 1998-2020 // Distributed under the Boost Software License, Version 1.0. // https://www.boost.org/LICENSE_1_0.txt // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt // Version: 4.0.2019.08.13 #pragma once #include #include #include #include // The queries consider the box to be a solid. // // The test-intersection queries use the method of separating axes. // https://www.geometrictools.com/Documentation/MethodOfSeparatingAxes.pdf // The find-intersection queries use parametric clipping against the four // edges of the box. namespace WwiseGTE { template class TIQuery, AlignedBox2> : public TIQuery, AlignedBox2> { public: struct Result : public TIQuery, AlignedBox2>::Result { // No additional information to compute. }; Result operator()(Ray2 const& ray, AlignedBox2 const& box) { // Get the centered form of the aligned box. The axes are // implicitly Axis[d] = Vector2::Unit(d). Vector2 boxCenter, boxExtent; box.GetCenteredForm(boxCenter, boxExtent); // Transform the ray to the aligned-box coordinate system. Vector2 rayOrigin = ray.origin - boxCenter; Result result; DoQuery(rayOrigin, ray.direction, boxExtent, result); return result; } protected: void DoQuery(Vector2 const& rayOrigin, Vector2 const& rayDirection, Vector2 const& boxExtent, Result& result) { for (int i = 0; i < 2; ++i) { if (std::fabs(rayOrigin[i]) > boxExtent[i] && rayOrigin[i] * rayDirection[i] >= (Real)0) { result.intersect = false; return; } } TIQuery, AlignedBox2>::DoQuery(rayOrigin, rayDirection, boxExtent, result); } }; template class FIQuery, AlignedBox2> : public FIQuery, AlignedBox2> { public: struct Result : public FIQuery, AlignedBox2>::Result { // No additional information to compute. }; Result operator()(Ray2 const& ray, AlignedBox2 const& box) { // Get the centered form of the aligned box. The axes are // implicitly Axis[d] = Vector2::Unit(d). Vector2 boxCenter, boxExtent; box.GetCenteredForm(boxCenter, boxExtent); // Transform the ray to the aligned-box coordinate system. Vector2 rayOrigin = ray.origin - boxCenter; Result result; DoQuery(rayOrigin, ray.direction, boxExtent, result); for (int i = 0; i < result.numIntersections; ++i) { result.point[i] = ray.origin + result.parameter[i] * ray.direction; } return result; } protected: void DoQuery(Vector2 const& rayOrigin, Vector2 const& rayDirection, Vector2 const& boxExtent, Result& result) { FIQuery, AlignedBox2>::DoQuery(rayOrigin, rayDirection, boxExtent, result); if (result.intersect) { // The line containing the ray intersects the box; the // t-interval is [t0,t1]. The ray intersects the box as long // as [t0,t1] overlaps the ray t-interval [0,+infinity). std::array rayInterval = { (Real)0, std::numeric_limits::max() }; FIQuery, std::array> iiQuery; auto iiResult = iiQuery(result.parameter, rayInterval); result.intersect = iiResult.intersect; result.numIntersections = iiResult.numIntersections; result.parameter = iiResult.overlap; } } }; }