// David Eberly, Geometric Tools, Redmond WA 98052 // Copyright (c) 1998-2026 // 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 // File Version: 8.0.2025.05.10 #pragma once // The queries consider the capsule to be a solid. // // The test-intersection queries are based on distance computations. #include #include #include #include #include namespace gte { template class TIQuery, Capsule3> { public: struct Result { Result() : intersect(false) { } bool intersect; }; Result operator()(Ray3 const& ray, Capsule3 const& capsule) { Result result{}; DCPQuery, Segment3> rsQuery{}; auto rsResult = rsQuery(ray, capsule.segment); result.intersect = (rsResult.distance <= capsule.radius); return result; } }; template class FIQuery, Capsule3> : public FIQuery, Capsule3> { public: struct Result : public FIQuery, Capsule3>::Result { Result() : FIQuery, Capsule3>::Result{} { } // No additional information to compute. }; Result operator()(Ray3 const& ray, Capsule3 const& capsule) { Result result{}; DoQuery(ray.origin, ray.direction, capsule, result); if (result.intersect) { for (size_t i = 0; i < 2; ++i) { result.point[i] = ray.origin + result.parameter[i] * ray.direction; } } return result; } protected: // The caller must ensure that on entry, 'result' is default // constructed as if there is no intersection. If an intersection is // found, the 'result' values will be modified accordingly. void DoQuery(Vector3 const& rayOrigin, Vector3 const& rayDirection, Capsule3 const& capsule, Result& result) { FIQuery, Capsule3>::DoQuery( rayOrigin, rayDirection, capsule, result); if (result.intersect) { // The line containing the ray intersects the capsule; the // t-interval is [t0,t1]. The ray intersects the capsule as // long as [t0,t1] overlaps the ray t-interval [0,+infinity). FIQuery, std::array> iiQuery; auto iiResult = iiQuery(result.parameter, static_cast(0), true); if (iiResult.intersect) { result.numIntersections = iiResult.numIntersections; result.parameter = iiResult.overlap; } else { // The line containing the ray does not intersect the // capsule. result = Result{}; } } } }; }