TriangleKey.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/FeatureKey.h>
  9. // An ordered triangle has V[0] = min(v0, v1, v2). Choose
  10. // (V[0], V[1], V[2]) to be a permutation of (v0, v1, v2) so that the final
  11. // is one of (v0, v1, v2), (v1, v2, v0) or (v2,v0,v1). The idea is that if
  12. // v0 corresponds to (1,0,0), v1 corresponds to (0,1,0), and v2 corresponds
  13. // to (0,0,1), the ordering (v0, v1, v2) corresponds to the 3x3 identity
  14. // matrix I; the rows are the specified 3-tuples. The permutation
  15. // (V[0], V[1], V[2]) induces a permutation of the rows of the identity
  16. // matrix to form a permutation matrix P with det(P) = 1 = det(I).
  17. //
  18. // An unordered triangle stores a permutation of (v0, v1, v2) so that
  19. // V[0] < V[1] < V[2].
  20. namespace WwiseGTE
  21. {
  22. template <bool Ordered>
  23. class TriangleKey : public FeatureKey<3, Ordered>
  24. {
  25. public:
  26. TriangleKey()
  27. {
  28. this->V = { -1, -1, -1 };
  29. }
  30. // This constructor is specialized based on Ordered.
  31. explicit TriangleKey(int v0, int v1, int v2)
  32. {
  33. Initialize(v0, v1, v2);
  34. }
  35. private:
  36. template <bool Dummy = Ordered>
  37. typename std::enable_if<Dummy, void>::type
  38. Initialize(int v0, int v1, int v2)
  39. {
  40. if (v0 < v1)
  41. {
  42. if (v0 < v2)
  43. {
  44. // v0 is minimum
  45. this->V[0] = v0;
  46. this->V[1] = v1;
  47. this->V[2] = v2;
  48. }
  49. else
  50. {
  51. // v2 is minimum
  52. this->V[0] = v2;
  53. this->V[1] = v0;
  54. this->V[2] = v1;
  55. }
  56. }
  57. else
  58. {
  59. if (v1 < v2)
  60. {
  61. // v1 is minimum
  62. this->V[0] = v1;
  63. this->V[1] = v2;
  64. this->V[2] = v0;
  65. }
  66. else
  67. {
  68. // v2 is minimum
  69. this->V[0] = v2;
  70. this->V[1] = v0;
  71. this->V[2] = v1;
  72. }
  73. }
  74. }
  75. template <bool Dummy = Ordered>
  76. typename std::enable_if<!Dummy, void>::type
  77. Initialize(int v0, int v1, int v2)
  78. {
  79. if (v0 < v1)
  80. {
  81. if (v0 < v2)
  82. {
  83. // v0 is minimum
  84. this->V[0] = v0;
  85. this->V[1] = std::min(v1, v2);
  86. this->V[2] = std::max(v1, v2);
  87. }
  88. else
  89. {
  90. // v2 is minimum
  91. this->V[0] = v2;
  92. this->V[1] = std::min(v0, v1);
  93. this->V[2] = std::max(v0, v1);
  94. }
  95. }
  96. else
  97. {
  98. if (v1 < v2)
  99. {
  100. // v1 is minimum
  101. this->V[0] = v1;
  102. this->V[1] = std::min(v2, v0);
  103. this->V[2] = std::max(v2, v0);
  104. }
  105. else
  106. {
  107. // v2 is minimum
  108. this->V[0] = v2;
  109. this->V[1] = std::min(v0, v1);
  110. this->V[2] = std::max(v0, v1);
  111. }
  112. }
  113. }
  114. };
  115. }