123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- #pragma once
- #include <Mathematics/ApprGaussian3.h>
- #include <Mathematics/Hyperellipsoid.h>
- #include <Mathematics/Matrix3x3.h>
- #include <Mathematics/Projection.h>
- #include <Mathematics/Rotation.h>
- namespace WwiseGTE
- {
-
-
-
-
-
-
-
- template <typename Real>
- bool GetContainer(int numPoints, Vector3<Real> const* points, Ellipsoid3<Real>& ellipsoid)
- {
-
-
-
- ApprGaussian3<Real> fitter;
- if (fitter.Fit(numPoints, points))
- {
- OrientedBox3<Real> box = fitter.GetParameters();
-
-
- for (int j = 0; j < 3; ++j)
- {
- if (box.extent[j] < (Real)0)
- {
- box.extent[j] = -box.extent[j];
- }
- }
-
-
-
-
-
-
-
-
-
- Real maxValue = (Real)0;
- for (int i = 0; i < numPoints; ++i)
- {
- Vector3<Real> diff = points[i] - box.center;
- Real dot[3] =
- {
- Dot(box.axis[0], diff),
- Dot(box.axis[1], diff),
- Dot(box.axis[2], diff)
- };
- Real value =
- box.extent[0] * dot[0] * dot[0] +
- box.extent[1] * dot[1] * dot[1] +
- box.extent[2] * dot[2] * dot[2];
- if (value > maxValue)
- {
- maxValue = value;
- }
- }
-
- ellipsoid.center = box.center;
- for (int j = 0; j < 3; ++j)
- {
- ellipsoid.axis[j] = box.axis[j];
- ellipsoid.extent[j] = std::sqrt(maxValue / box.extent[j]);
- }
- return true;
- }
- return false;
- }
-
- template <typename Real>
- bool InContainer(Vector3<Real> const& point, Ellipsoid3<Real> const& ellipsoid)
- {
- Vector3<Real> diff = point - ellipsoid.center;
- Vector3<Real> standardized{
- Dot(diff, ellipsoid.axis[0]) / ellipsoid.extent[0],
- Dot(diff, ellipsoid.axis[1]) / ellipsoid.extent[1],
- Dot(diff, ellipsoid.axis[2]) / ellipsoid.extent[2] };
- return Length(standardized) <= (Real)1;
- }
-
-
- template <typename Real>
- bool MergeContainers(Ellipsoid3<Real> const& ellipsoid0,
- Ellipsoid3<Real> const& ellipsoid1, Ellipsoid3<Real>& merge)
- {
-
- merge.center = (Real)0.5 * (ellipsoid0.center + ellipsoid1.center);
-
-
- Matrix3x3<Real> rot0, rot1;
- rot0.SetCol(0, ellipsoid0.axis[0]);
- rot0.SetCol(1, ellipsoid0.axis[1]);
- rot0.SetCol(2, ellipsoid0.axis[2]);
- rot1.SetCol(0, ellipsoid1.axis[0]);
- rot1.SetCol(1, ellipsoid1.axis[1]);
- rot1.SetCol(2, ellipsoid1.axis[2]);
- Quaternion<Real> q0 = Rotation<3, Real>(rot0);
- Quaternion<Real> q1 = Rotation<3, Real>(rot1);
- if (Dot(q0, q1) < (Real)0)
- {
- q1 = -q1;
- }
- Quaternion<Real> q = q0 + q1;
- Normalize(q);
- Matrix3x3<Real> rot = Rotation<3, Real>(q);
- for (int j = 0; j < 3; ++j)
- {
- merge.axis[j] = rot.GetCol(j);
- }
-
-
-
- for (int i = 0; i < 3; ++i)
- {
- // Projection axis.
- Line3<Real> line(merge.center, merge.axis[i]);
-
- Real min0, max0, min1, max1;
- Project(ellipsoid0, line, min0, max0);
- Project(ellipsoid1, 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[i] = (Real)0.5 * (maxIntr - minIntr);
- }
- return true;
- }
- }
|