123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- #pragma once
- #include <Mathematics/ApprGaussian2.h>
- #include <Mathematics/Hyperellipsoid.h>
- #include <Mathematics/Projection.h>
- namespace WwiseGTE
- {
-
-
-
-
-
-
-
- template <typename Real>
- bool GetContainer(int numPoints, Vector2<Real> const* points, Ellipse2<Real>& ellipse)
- {
-
-
-
- ApprGaussian2<Real> fitter;
- if (fitter.Fit(numPoints, points))
- {
- OrientedBox2<Real> box = fitter.GetParameters();
-
-
- for (int j = 0; j < 2; ++j)
- {
- if (box.extent[j] < (Real)0)
- {
- box.extent[j] = -box.extent[j];
- }
- }
-
-
-
-
-
-
-
-
-
- Real maxValue = (Real)0;
- for (int i = 0; i < numPoints; ++i)
- {
- Vector2<Real> diff = points[i] - box.center;
- Real dot[2] =
- {
- Dot(box.axis[0], diff),
- Dot(box.axis[1], diff)
- };
- Real value =
- box.extent[0] * dot[0] * dot[0] +
- box.extent[1] * dot[1] * dot[1];
- if (value > maxValue)
- {
- maxValue = value;
- }
- }
-
- ellipse.center = box.center;
- for (int j = 0; j < 2; ++j)
- {
- ellipse.axis[j] = box.axis[j];
- ellipse.extent[j] = std::sqrt(maxValue / box.extent[j]);
- }
- return true;
- }
- return false;
- }
-
- template <typename Real>
- bool InContainer(Vector2<Real> const& point, Ellipse2<Real> const& ellipse)
- {
- Vector2<Real> diff = point - ellipse.center;
- Vector2<Real> standardized{
- Dot(diff, ellipse.axis[0]) / ellipse.extent[0],
- Dot(diff, ellipse.axis[1]) / ellipse.extent[1] };
- return Length(standardized) <= (Real)1;
- }
-
-
- template <typename Real>
- bool MergeContainers(Ellipse2<Real> const& ellipse0,
- Ellipse2<Real> const& ellipse1, Ellipse2<Real>& merge)
- {
-
- merge.center = (Real)0.5 * (ellipse0.center + ellipse1.center);
-
-
- if (Dot(ellipse0.axis[0], ellipse1.axis[0]) >= (Real)0)
- {
- merge.axis[0] = (Real)0.5 * (ellipse0.axis[0] + ellipse1.axis[0]);
- }
- else
- {
- merge.axis[0] = (Real)0.5 * (ellipse0.axis[0] - ellipse1.axis[0]);
- }
- Normalize(merge.axis[0]);
- merge.axis[1] = -Perp(merge.axis[0]);
-
-
-
- for (int j = 0; j < 2; ++j)
- {
- // Projection axis.
- Line2<Real> line(merge.center, merge.axis[j]);
-
- Real min0, max0, min1, max1;
- Project(ellipse0, line, min0, max0);
- Project(ellipse1, line, min1, max1);
-
-
- Real maxIntr = (max0 >= max1 ? max0 : max1);
- Real minIntr = (min0 <= min1 ? min0 : min1);
-
-
- merge.center += line.direction * ((Real)0.5 * (minIntr + maxIntr));
-
- merge.extent[j] = (Real)0.5 * (maxIntr - minIntr);
- }
- return true;
- }
- }
|