IntpLinearNonuniform2.h 2.5 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/Vector2.h>
  10. // Linear interpolation of a network of triangles whose vertices are of the
  11. // form (x,y,f(x,y)). The function samples are F[i] and represent
  12. // f(x[i],y[i]), where i is the index of the input vertex (x[i],y[i]) to
  13. // Delaunay2.
  14. //
  15. // The TriangleMesh interface must support the following:
  16. // bool GetIndices(int, std::array<int, 3>&) const;
  17. // bool GetBarycentrics(int, Vector2<Real> const&,
  18. // std::array<Real, 3>&) const;
  19. // int GetContainingTriangle(Vector2<Real> const&) const;
  20. namespace WwiseGTE
  21. {
  22. template <typename Real, typename TriangleMesh>
  23. class IntpLinearNonuniform2
  24. {
  25. public:
  26. // Construction.
  27. IntpLinearNonuniform2(TriangleMesh const& mesh, Real const* F)
  28. :
  29. mMesh(&mesh),
  30. mF(F)
  31. {
  32. LogAssert(mF != nullptr, "Invalid input.");
  33. }
  34. // Linear interpolation. The return value is 'true' if and only if
  35. // the input point P is in the convex hull of the input vertices, in
  36. // which case the interpolation is valid.
  37. bool operator()(Vector2<Real> const& P, Real& F) const
  38. {
  39. int t = mMesh->GetContainingTriangle(P);
  40. if (t == -1)
  41. {
  42. // The point is outside the triangulation.
  43. return false;
  44. }
  45. // Get the barycentric coordinates of P with respect to the triangle,
  46. // P = b0*V0 + b1*V1 + b2*V2, where b0 + b1 + b2 = 1.
  47. std::array<Real, 3> bary;
  48. if (!mMesh->GetBarycentrics(t, P, bary))
  49. {
  50. // TODO: Throw an exception or allow this as valid behavior?
  51. // P is in a needle-like or degenerate triangle.
  52. return false;
  53. }
  54. // The result is a barycentric combination of function values.
  55. std::array<int, 3> indices;
  56. mMesh->GetIndices(t, indices);
  57. F = bary[0] * mF[indices[0]] + bary[1] * mF[indices[1]] + bary[2] * mF[indices[2]];
  58. return true;
  59. }
  60. private:
  61. TriangleMesh const* mMesh;
  62. Real const* mF;
  63. };
  64. }