123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- #pragma once
- #include <Mathematics/ApprGaussian3.h>
- #include <Mathematics/Matrix3x3.h>
- #include <Mathematics/Rotation.h>
- namespace WwiseGTE
- {
-
-
-
- template <typename Real>
- bool GetContainer(int numPoints, Vector3<Real> const* points, OrientedBox3<Real>& box)
- {
-
- ApprGaussian3<Real> fitter;
- if (fitter.Fit(numPoints, points))
- {
- box = fitter.GetParameters();
-
-
-
-
-
-
- Vector3<Real> diff = points[0] - box.center;
- Vector3<Real> pmin{ Dot(diff, box.axis[0]), Dot(diff, box.axis[1]),
- Dot(diff, box.axis[2]) };
- Vector3<Real> pmax = pmin;
- for (int i = 1; i < numPoints; ++i)
- {
- diff = points[i] - box.center;
- for (int j = 0; j < 3; ++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 < 3; ++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<Vector3<Real>> const& points, OrientedBox3<Real>& box)
- {
- return GetContainer(static_cast<int>(points.size()), points.data(), box);
- }
-
-
-
- template <typename Real>
- bool InContainer(Vector3<Real> const& point, OrientedBox3<Real> const& box)
- {
- Vector3<Real> diff = point - box.center;
- for (int i = 0; i < 3; ++i)
- {
- Real coeff = Dot(diff, box.axis[i]);
- if (std::fabs(coeff) > box.extent[i])
- {
- return false;
- }
- }
- return true;
- }
-
-
-
- template <typename Real>
- bool MergeContainers(OrientedBox3<Real> const& box0,
- OrientedBox3<Real> const& box1, OrientedBox3<Real>& merge)
- {
-
-
-
- merge.center = (Real)0.5 * (box0.center + box1.center);
-
-
-
-
-
-
-
-
-
-
- Matrix3x3<Real> rot0, rot1;
- rot0.SetCol(0, box0.axis[0]);
- rot0.SetCol(1, box0.axis[1]);
- rot0.SetCol(2, box0.axis[2]);
- rot1.SetCol(0, box1.axis[0]);
- rot1.SetCol(1, box1.axis[1]);
- rot1.SetCol(2, box1.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);
- }
-
-
-
-
-
-
-
-
-
-
- std::array<Vector3<Real>, 8> vertex;
- Vector3<Real> pmin{ (Real)0, (Real)0, (Real)0 };
- Vector3<Real> pmax{ (Real)0, (Real)0, (Real)0 };
- box0.GetVertices(vertex);
- for (int i = 0; i < 8; ++i)
- {
- Vector3<Real> diff = vertex[i] - merge.center;
- for (int j = 0; j < 3; ++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 < 8; ++i)
- {
- Vector3<Real> diff = vertex[i] - merge.center;
- for (int j = 0; j < 3; ++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 < 3; ++j)
- {
- merge.center += half * (pmax[j] + pmin[j]) * merge.axis[j];
- merge.extent[j] = half * (pmax[j] - pmin[j]);
- }
- return true;
- }
- }
|