123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- #pragma once
- #include <Mathematics/ApprGaussian2.h>
- namespace WwiseGTE
- {
-
-
-
- template <typename Real>
- bool GetContainer(int numPoints, Vector2<Real> const* points, OrientedBox2<Real>& box)
- {
-
- ApprGaussian2<Real> fitter;
- if (fitter.Fit(numPoints, points))
- {
- box = fitter.GetParameters();
-
-
-
-
-
- Vector2<Real> diff = points[0] - box.center;
- Vector2<Real> pmin{ Dot(diff, box.axis[0]), Dot(diff, box.axis[1]) };
- Vector2<Real> pmax = pmin;
- for (int i = 1; i < numPoints; ++i)
- {
- diff = points[i] - box.center;
- for (int j = 0; j < 2; ++j)
- {
- Real dot = Dot(diff, box.axis[j]);
- if (dot < pmin[j])
- {
- pmin[j] = dot;
- }
- else if (dot > pmax[j])
- {
- pmax[j] = dot;
- }
- }
- }
- for (int j = 0; j < 2; ++j)
- {
- box.center += ((Real)0.5 * (pmin[j] + pmax[j])) * box.axis[j];
- box.extent[j] = (Real)0.5 * (pmax[j] - pmin[j]);
- }
- return true;
- }
- return false;
- }
- template <typename Real>
- bool GetContainer(std::vector<Vector2<Real>> const& points, OrientedBox2<Real>& box)
- {
- return GetContainer(static_cast<int>(points.size()), points.data(), box);
- }
-
-
-
- template <typename Real>
- bool InContainer(Vector2<Real> const& point, OrientedBox2<Real> const& box)
- {
- Vector2<Real> diff = point - box.center;
- for (int i = 0; i < 2; ++i)
- {
- Real coeff = Dot(diff, box.axis[i]);
- if (std::fabs(coeff) > box.extent[i])
- {
- return false;
- }
- }
- return true;
- }
-
-
-
- template <typename Real>
- bool MergeContainers(OrientedBox2<Real> const& box0,
- OrientedBox2<Real> const& box1, OrientedBox2<Real>& merge)
- {
-
-
-
- merge.center = (Real)0.5 * (box0.center + box1.center);
-
-
-
- if (Dot(box0.axis[0], box1.axis[0]) >= (Real)0)
- {
- merge.axis[0] = (Real)0.5 * (box0.axis[0] + box1.axis[0]);
- }
- else
- {
- merge.axis[0] = (Real)0.5 * (box0.axis[0] - box1.axis[0]);
- }
- Normalize(merge.axis[0]);
- merge.axis[1] = -Perp(merge.axis[0]);
-
-
-
-
-
-
-
-
-
-
- std::array<Vector2<Real>, 4> vertex;
- Vector2<Real> pmin{ (Real)0, (Real)0 };
- Vector2<Real> pmax{ (Real)0, (Real)0 };
- box0.GetVertices(vertex);
- for (int i = 0; i < 4; ++i)
- {
- Vector2<Real> diff = vertex[i] - merge.center;
- for (int j = 0; j < 2; ++j)
- {
- Real dot = Dot(diff, merge.axis[j]);
- if (dot > pmax[j])
- {
- pmax[j] = dot;
- }
- else if (dot < pmin[j])
- {
- pmin[j] = dot;
- }
- }
- }
- box1.GetVertices(vertex);
- for (int i = 0; i < 4; ++i)
- {
- Vector2<Real> diff = vertex[i] - merge.center;
- for (int j = 0; j < 2; ++j)
- {
- Real dot = Dot(diff, merge.axis[j]);
- if (dot > pmax[j])
- {
- pmax[j] = dot;
- }
- else if (dot < pmin[j])
- {
- pmin[j] = dot;
- }
- }
- }
-
-
-
- Real const half = (Real)0.5;
- for (int j = 0; j < 2; ++j)
- {
- merge.center += half * (pmax[j] + pmin[j]) * merge.axis[j];
- merge.extent[j] = half * (pmax[j] - pmin[j]);
- }
- return true;
- }
- }
|