Delaunay2Mesh.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/Delaunay2.h>
  9. namespace WwiseGTE
  10. {
  11. template <typename InputType, typename ComputeType, typename RationalType>
  12. class Delaunay2Mesh
  13. {
  14. public:
  15. // Construction.
  16. Delaunay2Mesh(Delaunay2<InputType, ComputeType> const& delaunay)
  17. :
  18. mDelaunay(&delaunay)
  19. {
  20. }
  21. // Mesh information.
  22. inline int GetNumVertices() const
  23. {
  24. return mDelaunay->GetNumVertices();
  25. }
  26. inline int GetNumTriangles() const
  27. {
  28. return mDelaunay->GetNumTriangles();
  29. }
  30. inline Vector2<InputType> const* GetVertices() const
  31. {
  32. return mDelaunay->GetVertices();
  33. }
  34. inline int const* GetIndices() const
  35. {
  36. return &mDelaunay->GetIndices()[0];
  37. }
  38. inline int const* GetAdjacencies() const
  39. {
  40. return &mDelaunay->GetAdjacencies()[0];
  41. }
  42. // Containment queries.
  43. int GetContainingTriangle(Vector2<InputType> const& P) const
  44. {
  45. typename Delaunay2<InputType, ComputeType>::SearchInfo info;
  46. return mDelaunay->GetContainingTriangle(P, info);
  47. }
  48. bool GetVertices(int t, std::array<Vector2<InputType>, 3>& vertices) const
  49. {
  50. if (mDelaunay->GetDimension() == 2)
  51. {
  52. std::array<int, 3> indices;
  53. if (mDelaunay->GetIndices(t, indices))
  54. {
  55. PrimalQuery2<ComputeType> const& query = mDelaunay->GetQuery();
  56. Vector2<ComputeType> const* ctVertices = query.GetVertices();
  57. for (int i = 0; i < 3; ++i)
  58. {
  59. Vector2<ComputeType> const& V = ctVertices[indices[i]];
  60. for (int j = 0; j < 2; ++j)
  61. {
  62. vertices[i][j] = (InputType)V[j];
  63. }
  64. }
  65. return true;
  66. }
  67. }
  68. return false;
  69. }
  70. bool GetIndices(int t, std::array<int, 3>& indices) const
  71. {
  72. return mDelaunay->GetIndices(t, indices);
  73. }
  74. bool GetAdjacencies(int t, std::array<int, 3>& adjacencies) const
  75. {
  76. return mDelaunay->GetAdjacencies(t, adjacencies);
  77. }
  78. bool GetBarycentrics(int t, Vector2<InputType> const& P, std::array<InputType, 3>& bary) const
  79. {
  80. std::array<int, 3> indices;
  81. if (mDelaunay->GetIndices(t, indices))
  82. {
  83. PrimalQuery2<ComputeType> const& query = mDelaunay->GetQuery();
  84. Vector2<ComputeType> const* vertices = query.GetVertices();
  85. Vector2<RationalType> rtP{ P[0], P[1] };
  86. std::array<Vector2<RationalType>, 3> rtV;
  87. for (int i = 0; i < 3; ++i)
  88. {
  89. Vector2<ComputeType> const& V = vertices[indices[i]];
  90. for (int j = 0; j < 2; ++j)
  91. {
  92. rtV[i][j] = (RationalType)V[j];
  93. }
  94. };
  95. RationalType rtBary[3];
  96. if (ComputeBarycentrics(rtP, rtV[0], rtV[1], rtV[2], rtBary))
  97. {
  98. for (int i = 0; i < 3; ++i)
  99. {
  100. bary[i] = (InputType)rtBary[i];
  101. }
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. private:
  108. Delaunay2<InputType, ComputeType> const* mDelaunay;
  109. };
  110. }