2
0

IndexedTriangle.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Core/HashCombine.h>
  6. JPH_NAMESPACE_BEGIN
  7. /// Triangle with 32-bit indices
  8. class IndexedTriangleNoMaterial
  9. {
  10. public:
  11. JPH_OVERRIDE_NEW_DELETE
  12. /// Constructor
  13. IndexedTriangleNoMaterial() = default;
  14. IndexedTriangleNoMaterial(uint32 inI1, uint32 inI2, uint32 inI3) { mIdx[0] = inI1; mIdx[1] = inI2; mIdx[2] = inI3; }
  15. /// Check if two triangles are identical
  16. bool operator == (const IndexedTriangleNoMaterial &inRHS) const
  17. {
  18. return mIdx[0] == inRHS.mIdx[0] && mIdx[1] == inRHS.mIdx[1] && mIdx[2] == inRHS.mIdx[2];
  19. }
  20. /// Check if two triangles are equivalent (using the same vertices)
  21. bool IsEquivalent(const IndexedTriangleNoMaterial &inRHS) const
  22. {
  23. return (mIdx[0] == inRHS.mIdx[0] && mIdx[1] == inRHS.mIdx[1] && mIdx[2] == inRHS.mIdx[2])
  24. || (mIdx[0] == inRHS.mIdx[1] && mIdx[1] == inRHS.mIdx[2] && mIdx[2] == inRHS.mIdx[0])
  25. || (mIdx[0] == inRHS.mIdx[2] && mIdx[1] == inRHS.mIdx[0] && mIdx[2] == inRHS.mIdx[1]);
  26. }
  27. /// Check if two triangles are opposite (using the same vertices but in opposing order)
  28. bool IsOpposite(const IndexedTriangleNoMaterial &inRHS) const
  29. {
  30. return (mIdx[0] == inRHS.mIdx[0] && mIdx[1] == inRHS.mIdx[2] && mIdx[2] == inRHS.mIdx[1])
  31. || (mIdx[0] == inRHS.mIdx[1] && mIdx[1] == inRHS.mIdx[0] && mIdx[2] == inRHS.mIdx[2])
  32. || (mIdx[0] == inRHS.mIdx[2] && mIdx[1] == inRHS.mIdx[1] && mIdx[2] == inRHS.mIdx[0]);
  33. }
  34. /// Check if triangle is degenerate
  35. bool IsDegenerate(const VertexList &inVertices) const
  36. {
  37. Vec3 v0(inVertices[mIdx[0]]);
  38. Vec3 v1(inVertices[mIdx[1]]);
  39. Vec3 v2(inVertices[mIdx[2]]);
  40. return (v1 - v0).Cross(v2 - v0).IsNearZero();
  41. }
  42. /// Rotate the vertices so that the second vertex becomes first etc. This does not change the represented triangle.
  43. void Rotate()
  44. {
  45. uint32 tmp = mIdx[0];
  46. mIdx[0] = mIdx[1];
  47. mIdx[1] = mIdx[2];
  48. mIdx[2] = tmp;
  49. }
  50. /// Get center of triangle
  51. Vec3 GetCentroid(const VertexList &inVertices) const
  52. {
  53. return (Vec3(inVertices[mIdx[0]]) + Vec3(inVertices[mIdx[1]]) + Vec3(inVertices[mIdx[2]])) / 3.0f;
  54. }
  55. uint32 mIdx[3];
  56. };
  57. /// Triangle with 32-bit indices and material index
  58. class IndexedTriangle : public IndexedTriangleNoMaterial
  59. {
  60. public:
  61. using IndexedTriangleNoMaterial::IndexedTriangleNoMaterial;
  62. /// Constructor
  63. IndexedTriangle(uint32 inI1, uint32 inI2, uint32 inI3, uint32 inMaterialIndex) : IndexedTriangleNoMaterial(inI1, inI2, inI3), mMaterialIndex(inMaterialIndex) { }
  64. /// Check if two triangles are identical
  65. bool operator == (const IndexedTriangle &inRHS) const
  66. {
  67. return mMaterialIndex == inRHS.mMaterialIndex && IndexedTriangleNoMaterial::operator==(inRHS);
  68. }
  69. /// Rotate the vertices so that the lowest vertex becomes the first. This does not change the represented triangle.
  70. IndexedTriangle GetLowestIndexFirst() const
  71. {
  72. if (mIdx[0] < mIdx[1])
  73. {
  74. if (mIdx[0] < mIdx[2])
  75. return IndexedTriangle(mIdx[0], mIdx[1], mIdx[2], mMaterialIndex); // 0 is smallest
  76. else
  77. return IndexedTriangle(mIdx[2], mIdx[0], mIdx[1], mMaterialIndex); // 2 is smallest
  78. }
  79. else
  80. {
  81. if (mIdx[1] < mIdx[2])
  82. return IndexedTriangle(mIdx[1], mIdx[2], mIdx[0], mMaterialIndex); // 1 is smallest
  83. else
  84. return IndexedTriangle(mIdx[2], mIdx[0], mIdx[1], mMaterialIndex); // 2 is smallest
  85. }
  86. }
  87. uint32 mMaterialIndex = 0;
  88. };
  89. using IndexedTriangleNoMaterialList = Array<IndexedTriangleNoMaterial>;
  90. using IndexedTriangleList = Array<IndexedTriangle>;
  91. JPH_NAMESPACE_END
  92. // Create a std::hash for IndexedTriangleNoMaterial and IndexedTriangle
  93. JPH_MAKE_HASHABLE(JPH::IndexedTriangleNoMaterial, t.mIdx[0], t.mIdx[1], t.mIdx[2])
  94. JPH_MAKE_HASHABLE(JPH::IndexedTriangle, t.mIdx[0], t.mIdx[1], t.mIdx[2], t.mMaterialIndex)