123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- #pragma once
- #include <Mathematics/DCPQuery.h>
- #include <Mathematics/Segment.h>
- namespace WwiseGTE
- {
- template <int N, typename Rational>
- class DistanceSegmentSegmentExact
- {
- public:
- struct Result
- {
- Rational sqrDistance;
- Rational parameter[2];
- Vector<N, Rational> closest[2];
- };
- Result operator()(Segment<N, Rational> const& segment0,
- Segment<N, Rational> const& segment1)
- {
- return operator()(segment0.p[0], segment0.p[1], segment1.p[0], segment1.p[1]);
- }
- Result operator()(
- Vector<N, Rational> const& P0, Vector<N, Rational> const& P1,
- Vector<N, Rational> const& Q0, Vector<N, Rational> const& Q1)
- {
- Vector<N, Rational> P1mP0 = P1 - P0;
- Vector<N, Rational> Q1mQ0 = Q1 - Q0;
- Vector<N, Rational> P0mQ0 = P0 - Q0;
- Rational a = Dot(P1mP0, P1mP0);
- Rational b = Dot(P1mP0, Q1mQ0);
- Rational c = Dot(Q1mQ0, Q1mQ0);
- Rational d = Dot(P1mP0, P0mQ0);
- Rational e = Dot(Q1mQ0, P0mQ0);
- Rational const zero = (Rational)0;
- Rational const one = (Rational)1;
- Rational det = a * c - b * b;
- Rational s, t, nd, bmd, bte, ctd, bpe, ate, btd;
- if (det > zero)
- {
- bte = b * e;
- ctd = c * d;
- if (bte <= ctd)
- {
- s = zero;
- if (e <= zero)
- {
-
- t = zero;
- nd = -d;
- if (nd >= a)
- {
- s = one;
- }
- else if (nd > zero)
- {
- s = nd / a;
- }
-
- }
- else if (e < c)
- {
-
- t = e / c;
- }
- else
- {
-
- t = one;
- bmd = b - d;
- if (bmd >= a)
- {
- s = one;
- }
- else if (bmd > zero)
- {
- s = bmd / a;
- }
-
- }
- }
- else
- {
- s = bte - ctd;
- if (s >= det)
- {
-
- s = one;
- bpe = b + e;
- if (bpe <= zero)
- {
-
- t = zero;
- nd = -d;
- if (nd <= zero)
- {
- s = zero;
- }
- else if (nd < a)
- {
- s = nd / a;
- }
-
- }
- else if (bpe < c)
- {
-
- t = bpe / c;
- }
- else
- {
-
- t = one;
- bmd = b - d;
- if (bmd <= zero)
- {
- s = zero;
- }
- else if (bmd < a)
- {
- s = bmd / a;
- }
-
- }
- }
- else
- {
- ate = a * e;
- btd = b * d;
- if (ate <= btd)
- {
-
- t = zero;
- nd = -d;
- if (nd <= zero)
- {
- s = zero;
- }
- else if (nd >= a)
- {
- s = one;
- }
- else
- {
- s = nd / a;
- }
- }
- else
- {
- t = ate - btd;
- if (t >= det)
- {
-
- t = one;
- bmd = b - d;
- if (bmd <= zero)
- {
- s = zero;
- }
- else if (bmd >= a)
- {
- s = one;
- }
- else
- {
- s = bmd / a;
- }
- }
- else
- {
-
- s /= det;
- t /= det;
- }
- }
- }
- }
- }
- else
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if (e <= zero)
- {
-
- t = zero;
- nd = -d;
- if (nd <= zero)
- {
-
- s = zero;
- }
- else if (nd >= a)
- {
-
- s = one;
- }
- else
- {
-
- s = nd / a;
- }
- }
- else if (e >= c)
- {
-
- t = one;
- bmd = b - d;
- if (bmd <= zero)
- {
-
- s = zero;
- }
- else if (bmd >= a)
- {
-
- s = one;
- }
- else
- {
-
- s = bmd / a;
- }
- }
- else
- {
-
-
- s = zero;
- t = e / c;
- }
- }
- Result result;
- result.parameter[0] = s;
- result.parameter[1] = t;
- result.closest[0] = P0 + s * P1mP0;
- result.closest[1] = Q0 + t * Q1mQ0;
- Vector<N, Rational> diff = result.closest[1] - result.closest[0];
- result.sqrDistance = Dot(diff, diff);
- return result;
- }
- };
- }
|