123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- #pragma once
- #include <Mathematics/FIQuery.h>
- #include <Mathematics/TIQuery.h>
- #include <Mathematics/Vector3.h>
- #include <Mathematics/Hypersphere.h>
- #include <Mathematics/Line.h>
- namespace WwiseGTE
- {
- template <typename Real>
- class TIQuery<Real, Line3<Real>, Sphere3<Real>>
- {
- public:
- struct Result
- {
- bool intersect;
- };
- Result operator()(Line3<Real> const& line, Sphere3<Real> const& sphere)
- {
-
-
-
-
- Result result;
- Vector3<Real> diff = line.origin - sphere.center;
- Real a0 = Dot(diff, diff) - sphere.radius * sphere.radius;
- Real a1 = Dot(line.direction, diff);
-
- Real discr = a1 * a1 - a0;
- result.intersect = (discr >= (Real)0);
- return result;
- }
- };
- template <typename Real>
- class FIQuery<Real, Line3<Real>, Sphere3<Real>>
- {
- public:
- struct Result
- {
- bool intersect;
- int numIntersections;
- std::array<Real, 2> parameter;
- std::array<Vector3<Real>, 2> point;
- };
- Result operator()(Line3<Real> const& line, Sphere3<Real> const& sphere)
- {
- Result result;
- DoQuery(line.origin, line.direction, sphere, result);
- for (int i = 0; i < result.numIntersections; ++i)
- {
- result.point[i] = line.origin + result.parameter[i] * line.direction;
- }
- return result;
- }
- protected:
- void DoQuery(Vector3<Real> const& lineOrigin,
- Vector3<Real> const& lineDirection, Sphere3<Real> const& sphere,
- Result& result)
- {
-
-
-
-
- Vector3<Real> diff = lineOrigin - sphere.center;
- Real a0 = Dot(diff, diff) - sphere.radius * sphere.radius;
- Real a1 = Dot(lineDirection, diff);
-
- Real discr = a1 * a1 - a0;
- if (discr > (Real)0)
- {
- result.intersect = true;
- result.numIntersections = 2;
- Real root = std::sqrt(discr);
- result.parameter[0] = -a1 - root;
- result.parameter[1] = -a1 + root;
- }
- else if (discr < (Real)0)
- {
- result.intersect = false;
- result.numIntersections = 0;
- }
- else
- {
- result.intersect = true;
- result.numIntersections = 1;
- result.parameter[0] = -a1;
- }
- }
- };
- }
|