IndexedTriangle.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. constexpr IndexedTriangleNoMaterial(uint32 inI1, uint32 inI2, uint32 inI3) : mIdx { inI1, inI2, 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. constexpr IndexedTriangle(uint32 inI1, uint32 inI2, uint32 inI3, uint32 inMaterialIndex, uint inUserData = 0) : IndexedTriangleNoMaterial(inI1, inI2, inI3), mMaterialIndex(inMaterialIndex), mUserData(inUserData) { }
  64. /// Check if two triangles are identical
  65. bool operator == (const IndexedTriangle &inRHS) const
  66. {
  67. return mMaterialIndex == inRHS.mMaterialIndex && mUserData == inRHS.mUserData && 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, mUserData); // 0 is smallest
  76. else
  77. return IndexedTriangle(mIdx[2], mIdx[0], mIdx[1], mMaterialIndex, mUserData); // 2 is smallest
  78. }
  79. else
  80. {
  81. if (mIdx[1] < mIdx[2])
  82. return IndexedTriangle(mIdx[1], mIdx[2], mIdx[0], mMaterialIndex, mUserData); // 1 is smallest
  83. else
  84. return IndexedTriangle(mIdx[2], mIdx[0], mIdx[1], mMaterialIndex, mUserData); // 2 is smallest
  85. }
  86. }
  87. uint32 mMaterialIndex = 0;
  88. uint32 mUserData = 0; ///< User data that can be used for anything by the application, e.g. for tracking the original index of the triangle
  89. };
  90. using IndexedTriangleNoMaterialList = Array<IndexedTriangleNoMaterial>;
  91. using IndexedTriangleList = Array<IndexedTriangle>;
  92. JPH_NAMESPACE_END
  93. // Create a std::hash for IndexedTriangleNoMaterial and IndexedTriangle
  94. JPH_MAKE_HASHABLE(JPH::IndexedTriangleNoMaterial, t.mIdx[0], t.mIdx[1], t.mIdx[2])
  95. JPH_MAKE_HASHABLE(JPH::IndexedTriangle, t.mIdx[0], t.mIdx[1], t.mIdx[2], t.mMaterialIndex, t.mUserData)