123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- #pragma once
- #include <Mathematics/ConvexHull2.h>
- namespace WwiseGTE
- {
- template <typename Real, typename ComputeType>
- class SeparatePoints2
- {
- public:
-
-
-
- bool operator()(int numPoints0, Vector2<Real> const* points0,
- int numPoints1, Vector2<Real> const* points1,
- Line2<Real>& separatingLine) const
- {
-
- ConvexHull2<Real, ComputeType> ch0;
- ch0(numPoints0, points0, (Real)0);
- if (ch0.GetDimension() != 2)
- {
- return false;
- }
-
- ConvexHull2<Real, ComputeType> ch1;
- ch1(numPoints1, points1, (Real)0);
- if (ch1.GetDimension() != 2)
- {
- return false;
- }
- int numEdges0 = static_cast<int>(ch0.GetHull().size());
- int const* edges0 = &ch0.GetHull()[0];
- int numEdges1 = static_cast<int>(ch1.GetHull().size());
- int const* edges1 = &ch1.GetHull()[0];
-
- int j0, j1, i0, i1, side0, side1;
- Vector2<Real> lineNormal;
- Real lineConstant;
- for (j1 = 0, j0 = numEdges0 - 1; j1 < numEdges0; j0 = j1++)
- {
-
- i0 = edges0[j0];
- i1 = edges0[j1];
-
-
- separatingLine.origin = points0[i0];
- separatingLine.direction = points0[i1] - points0[i0];
- Normalize(separatingLine.direction);
- lineNormal = Perp(separatingLine.direction);
- lineConstant = Dot(lineNormal, separatingLine.origin);
-
- side1 = OnSameSide(lineNormal, lineConstant, numEdges1, edges1,
- points1);
- if (side1)
- {
-
- side0 = WhichSide(lineNormal, lineConstant, numEdges0,
- edges0, points0);
- if (side0 * side1 <= 0)
- {
- return true;
- }
- }
- }
-
- for (j1 = 0, j0 = numEdges1 - 1; j1 < numEdges1; j0 = j1++)
- {
-
- i0 = edges1[j0];
- i1 = edges1[j1];
-
-
- separatingLine.origin = points1[i0];
- separatingLine.direction = points1[i1] - points1[i0];
- Normalize(separatingLine.direction);
- lineNormal = Perp(separatingLine.direction);
- lineConstant = Dot(lineNormal, separatingLine.origin);
-
- side0 = OnSameSide(lineNormal, lineConstant, numEdges0, edges0,
- points0);
- if (side0)
- {
-
- side1 = WhichSide(lineNormal, lineConstant, numEdges1,
- edges1, points1);
- if (side0 * side1 <= 0)
- {
- return true;
- }
- }
- }
- return false;
- }
- private:
- int OnSameSide(Vector2<Real> const& lineNormal, Real lineConstant,
- int numEdges, int const* edges, Vector2<Real> const* points) const
- {
-
- Real c0;
- int posSide = 0, negSide = 0;
- for (int i1 = 0, i0 = numEdges - 1; i1 < numEdges; i0 = i1++)
- {
- c0 = Dot(lineNormal, points[edges[i0]]);
- if (c0 > lineConstant)
- {
- ++posSide;
- }
- else if (c0 < lineConstant)
- {
- ++negSide;
- }
- if (posSide && negSide)
- {
-
- return 0;
- }
- c0 = Dot(lineNormal, points[edges[i1]]);
- if (c0 > lineConstant)
- {
- ++posSide;
- }
- else if (c0 < lineConstant)
- {
- ++negSide;
- }
- if (posSide && negSide)
- {
-
- return 0;
- }
- }
- return (posSide ? +1 : -1);
- }
- int WhichSide(Vector2<Real> const& lineNormal, Real lineConstant,
- int numEdges, int const* edges, Vector2<Real> const* points) const
- {
-
- Real c0;
- for (int i1 = 0, i0 = numEdges - 1; i1 < numEdges; i0 = i1++)
- {
- c0 = Dot(lineNormal, points[edges[i0]]);
- if (c0 > lineConstant)
- {
-
- return +1;
- }
- if (c0 < lineConstant)
- {
-
- return -1;
- }
- c0 = Dot(lineNormal, points[edges[i1]]);
- if (c0 > lineConstant)
- {
-
- return +1;
- }
- if (c0 < lineConstant)
- {
-
- return -1;
- }
- }
-
- return 0;
- }
- };
- }
|