IntpLinearNonuniform3.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <Mathematics/Logger.h>
  9. #include <Mathematics/Vector3.h>
  10. // Linear interpolation of a network of triangles whose vertices are of the
  11. // form (x,y,z,f(x,y,z)). The function samples are F[i] and represent
  12. // f(x[i],y[i],z[i]), where i is the index of the input vertex
  13. // (x[i],y[i],z[i]) to Delaunay3.
  14. //
  15. // The TetrahedronMesh interface must support the following:
  16. // int GetContainingTetrahedron(Vector3<Real> const&) const;
  17. // bool GetIndices(int, std::array<int, 4>&) const;
  18. // bool GetBarycentrics(int, Vector3<Real> const&, Real[4]) const;
  19. namespace WwiseGTE
  20. {
  21. template <typename Real, typename TetrahedronMesh>
  22. class IntpLinearNonuniform3
  23. {
  24. public:
  25. // Construction.
  26. IntpLinearNonuniform3(TetrahedronMesh const& mesh, Real const* F)
  27. :
  28. mMesh(&mesh),
  29. mF(F)
  30. {
  31. LogAssert(mF != nullptr, "Invalid input.");
  32. }
  33. // Linear interpolation. The return value is 'true' if and only if
  34. // the input point is in the convex hull of the input vertices, in
  35. // which case the interpolation is valid.
  36. bool operator()(Vector3<Real> const& P, Real& F) const
  37. {
  38. int t = mMesh->GetContainingTetrahedron(P);
  39. if (t == -1)
  40. {
  41. // The point is outside the tetrahedralization.
  42. return false;
  43. }
  44. // Get the barycentric coordinates of P with respect to the tetrahedron,
  45. // P = b0*V0 + b1*V1 + b2*V2 + b3*V3, where b0 + b1 + b2 + b3 = 1.
  46. std::array<Real, 4> bary;
  47. if (!mMesh->GetBarycentrics(t, P, bary))
  48. {
  49. // TODO: Throw an exception or allow this as valid behavior?
  50. // P is in a needle-like, flat, or degenerate tetrahedron.
  51. return false;
  52. }
  53. // The result is a barycentric combination of function values.
  54. std::array<int, 4> indices;
  55. mMesh->GetIndices(t, indices);
  56. F = bary[0] * mF[indices[0]] + bary[1] * mF[indices[1]] +
  57. bary[2] * mF[indices[2]] + bary[3] * mF[indices[4]];
  58. return true;
  59. }
  60. private:
  61. TetrahedronMesh const* mMesh;
  62. Real const* mF;
  63. };
  64. }