pos.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #ifndef IGL_POS_H
  9. #define IGL_POS_H
  10. #include <Eigen/Core>
  11. #include <vector>
  12. namespace igl
  13. {
  14. // Pos - Fake halfedge for fast and easy navigation on triangle meshes with VT and TT adj
  15. template <typename S>
  16. class Pos
  17. {
  18. public:
  19. // Init the pos by specifying Face,Edge Index and Orientation
  20. Pos(const Eigen::Matrix<S, Eigen::Dynamic, Eigen::Dynamic>* F,
  21. Eigen::Matrix<S, Eigen::Dynamic, Eigen::Dynamic>* FF,
  22. Eigen::Matrix<S, Eigen::Dynamic, Eigen::Dynamic>* FFi,
  23. int fi,
  24. int ei,
  25. bool reverse = false
  26. )
  27. : F(F), FF(FF), FFi(FFi), fi(fi), ei(ei), reverse(reverse)
  28. {}
  29. // Change Face
  30. void flipF()
  31. {
  32. if (isBorder())
  33. return;
  34. int fin = (*FF)(fi,ei);
  35. int ein = (*FFi)(fi,ei);
  36. int reversen = !reverse;
  37. fi = fin;
  38. ei = ein;
  39. reverse = reversen;
  40. }
  41. // Change Edge
  42. void flipE()
  43. {
  44. if (!reverse)
  45. ei = (ei+2)%3; // ei-1
  46. else
  47. ei = (ei+1)%3;
  48. reverse = !reverse;
  49. }
  50. // Change Vertex
  51. void flipV()
  52. {
  53. reverse = !reverse;
  54. }
  55. bool isBorder()
  56. {
  57. return (*FF)(fi,ei) == -1;
  58. }
  59. /*!
  60. * Returns the next edge skipping the border
  61. * _________
  62. * /\ c | b /\
  63. * / \ | / \
  64. * / d \ | / a \
  65. * /______\|/______\
  66. * v
  67. * In this example, if a and d are of-border and the pos is iterating counterclockwise, this method iterate through the faces incident on vertex v,
  68. * producing the sequence a, b, c, d, a, b, c, ...
  69. */
  70. bool NextFE()
  71. {
  72. if ( isBorder() ) // we are on a border
  73. {
  74. do
  75. {
  76. flipF();
  77. flipE();
  78. } while (!isBorder());
  79. flipE();
  80. return false;
  81. }
  82. else
  83. {
  84. flipF();
  85. flipE();
  86. return true;
  87. }
  88. }
  89. // Get vertex index
  90. int Vi()
  91. {
  92. assert(fi >= 0);
  93. assert(fi < F->rows());
  94. assert(ei >= 0);
  95. assert(ei <= 2);
  96. if (!reverse)
  97. return (*F)(fi,ei);
  98. else
  99. return (*F)(fi,(ei+1)%3);
  100. }
  101. // Get face index
  102. int Fi()
  103. {
  104. return fi;
  105. }
  106. // Get edge index
  107. int Ei()
  108. {
  109. return ei;
  110. }
  111. bool operator==(Pos& p2)
  112. {
  113. return
  114. (
  115. (fi == p2.fi) &&
  116. (ei == p2.ei) &&
  117. (reverse == p2.reverse) &&
  118. (F == p2.F) &&
  119. (FF == p2.FF) &&
  120. (FFi == p2.FFi)
  121. );
  122. }
  123. private:
  124. int fi;
  125. int ei;
  126. bool reverse;
  127. const Eigen::Matrix<S, Eigen::Dynamic, Eigen::Dynamic>* F;
  128. const Eigen::Matrix<S, Eigen::Dynamic, Eigen::Dynamic>* FF;
  129. const Eigen::Matrix<S, Eigen::Dynamic, Eigen::Dynamic>* FFi;
  130. };
  131. }
  132. #endif