IndexedTriangle.h 3.6 KB

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