123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- #pragma once
- #include <Mathematics/FIQuery.h>
- #include <Mathematics/TIQuery.h>
- #include <Mathematics/Vector3.h>
- #include <Mathematics/Halfspace.h>
- #include <Mathematics/Triangle.h>
- namespace WwiseGTE
- {
- template <typename Real>
- class TIQuery<Real, Halfspace3<Real>, Triangle3<Real>>
- {
- public:
- struct Result
- {
- bool intersect;
- };
- Result operator()(Halfspace3<Real> const& halfspace, Triangle3<Real> const& triangle)
- {
- Result result;
-
-
-
- Real s[3];
- for (int i = 0; i < 3; ++i)
- {
- s[i] = Dot(halfspace.normal, triangle.v[i]) - halfspace.constant;
- }
-
-
- result.intersect = (std::max(std::max(s[0], s[1]), s[2]) >= (Real)0);
- return result;
- }
- };
- template <typename Real>
- class FIQuery<Real, Halfspace3<Real>, Triangle3<Real>>
- {
- public:
- struct Result
- {
- bool intersect;
-
-
-
- int numPoints;
- Vector3<Real> point[4];
- };
- Result operator()(Halfspace3<Real> const& halfspace, Triangle3<Real> const& triangle)
- {
- Result result;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Real s[3];
- int numPositive = 0, numNegative = 0, numZero = 0;
- for (int i = 0; i < 3; ++i)
- {
- s[i] = Dot(halfspace.normal, triangle.v[i]) - halfspace.constant;
- if (s[i] > (Real)0)
- {
- ++numPositive;
- }
- else if (s[i] < (Real)0)
- {
- ++numNegative;
- }
- else
- {
- ++numZero;
- }
- }
- if (numNegative == 0)
- {
-
- result.intersect = true;
- result.numPoints = 3;
- result.point[0] = triangle.v[0];
- result.point[1] = triangle.v[1];
- result.point[2] = triangle.v[2];
- }
- else if (numNegative == 1)
- {
- result.intersect = true;
- if (numPositive == 2)
- {
-
-
- result.numPoints = 4;
- for (int i0 = 0; i0 < 3; ++i0)
- {
- if (s[i0] < (Real)0)
- {
- int i1 = (i0 + 1) % 3, i2 = (i0 + 2) % 3;
- result.point[0] = triangle.v[i1];
- result.point[1] = triangle.v[i2];
- Real t2 = s[i2] / (s[i2] - s[i0]);
- Real t0 = s[i0] / (s[i0] - s[i1]);
- result.point[2] = triangle.v[i2] + t2 *
- (triangle.v[i0] - triangle.v[i2]);
- result.point[3] = triangle.v[i0] + t0 *
- (triangle.v[i1] - triangle.v[i0]);
- break;
- }
- }
- }
- else if (numPositive == 1)
- {
-
-
- result.numPoints = 3;
- for (int i0 = 0; i0 < 3; ++i0)
- {
- if (s[i0] == (Real)0)
- {
- int i1 = (i0 + 1) % 3, i2 = (i0 + 2) % 3;
- result.point[0] = triangle.v[i0];
- Real t1 = s[i1] / (s[i1] - s[i2]);
- Vector3<Real> p = triangle.v[i1] + t1 *
- (triangle.v[i2] - triangle.v[i1]);
- if (s[i1] > (Real)0)
- {
- result.point[1] = triangle.v[i1];
- result.point[2] = p;
- }
- else
- {
- result.point[1] = p;
- result.point[2] = triangle.v[i2];
- }
- break;
- }
- }
- }
- else
- {
-
- result.numPoints = 0;
- for (int i = 0; i < 3; ++i)
- {
- if (s[i] == (Real)0)
- {
- result.point[result.numPoints++] = triangle.v[i];
- }
- }
- }
- }
- else if (numNegative == 2)
- {
- result.intersect = true;
- if (numPositive == 1)
- {
-
-
- result.numPoints = 3;
- for (int i0 = 0; i0 < 3; ++i0)
- {
- if (s[i0] > (Real)0)
- {
- int i1 = (i0 + 1) % 3, i2 = (i0 + 2) % 3;
- result.point[0] = triangle.v[i0];
- Real t0 = s[i0] / (s[i0] - s[i1]);
- Real t2 = s[i2] / (s[i2] - s[i0]);
- result.point[1] = triangle.v[i0] + t0 *
- (triangle.v[i1] - triangle.v[i0]);
- result.point[2] = triangle.v[i2] + t2 *
- (triangle.v[i0] - triangle.v[i2]);
- break;
- }
- }
- }
- else
- {
-
- result.numPoints = 1;
- for (int i = 0; i < 3; ++i)
- {
- if (s[i] == (Real)0)
- {
- result.point[0] = triangle.v[i];
- break;
- }
- }
- }
- }
- else
- {
-
- result.intersect = false;
- result.numPoints = 0;
- }
- return result;
- }
- };
- }
|