lexicographic_triangulation.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 Alec Jacobson <alecjacobson@gmail.com>
  4. // Qingan Zhou <qnzhou@gmail.com>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla Public License
  7. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  8. // obtain one at http://mozilla.org/MPL/2.0/.
  9. #include "lexicographic_triangulation.h"
  10. #include "sortrows.h"
  11. #include <vector>
  12. #include <list>
  13. template<
  14. typename DerivedP,
  15. typename Orient2D,
  16. typename DerivedF
  17. >
  18. IGL_INLINE void igl::lexicographic_triangulation(
  19. const Eigen::MatrixBase<DerivedP>& P,
  20. Orient2D orient2D,
  21. Eigen::PlainObjectBase<DerivedF>& F)
  22. {
  23. typedef typename DerivedP::Scalar Scalar;
  24. const size_t num_pts = P.rows();
  25. if (num_pts < 3) {
  26. throw "At least 3 points are required for triangulation!";
  27. }
  28. // Sort points in lexicographic order.
  29. DerivedP ordered_P;
  30. Eigen::VectorXi order;
  31. igl::sortrows(P, true, ordered_P, order);
  32. std::vector<Eigen::Vector3i> faces;
  33. std::list<int> boundary;
  34. const Scalar p0[] = {ordered_P(0, 0), ordered_P(0, 1)};
  35. const Scalar p1[] = {ordered_P(1, 0), ordered_P(1, 1)};
  36. for (size_t i=2; i<num_pts; i++) {
  37. const Scalar curr_p[] = {ordered_P(i, 0), ordered_P(i, 1)};
  38. if (faces.size() == 0) {
  39. // All points processed so far are collinear.
  40. // Check if the current point is collinear with every points before it.
  41. auto orientation = orient2D(p0, p1, curr_p);
  42. if (orientation != 0) {
  43. // Add a fan of triangles eminating from curr_p.
  44. if (orientation > 0) {
  45. for (size_t j=0; j<=i-2; j++) {
  46. faces.push_back({order[j], order[j+1], order[i]});
  47. }
  48. } else if (orientation < 0) {
  49. for (size_t j=0; j<=i-2; j++) {
  50. faces.push_back({order[j+1], order[j], order[i]});
  51. }
  52. }
  53. // Initialize current boundary.
  54. boundary.insert(boundary.end(), order.data(), order.data()+i+1);
  55. if (orientation < 0) {
  56. boundary.reverse();
  57. }
  58. }
  59. } else {
  60. const size_t bd_size = boundary.size();
  61. assert(bd_size >= 3);
  62. std::vector<short> orientations;
  63. for (auto itr=boundary.begin(); itr!=boundary.end(); itr++) {
  64. auto next_itr = std::next(itr, 1);
  65. if (next_itr == boundary.end()) {
  66. next_itr = boundary.begin();
  67. }
  68. const Scalar bd_p0[] = {P(*itr, 0), P(*itr, 1)};
  69. const Scalar bd_p1[] = {P(*next_itr, 0), P(*next_itr, 1)};
  70. auto orientation = orient2D(bd_p0, bd_p1, curr_p);
  71. if (orientation < 0) {
  72. faces.push_back({*next_itr, *itr, order[i]});
  73. }
  74. orientations.push_back(orientation);
  75. }
  76. auto left_itr = boundary.begin();
  77. auto right_itr = boundary.begin();
  78. auto curr_itr = boundary.begin();
  79. for (size_t j=0; j<bd_size; j++, curr_itr++) {
  80. size_t prev = (j+bd_size-1) % bd_size;
  81. if (orientations[j] >= 0 && orientations[prev] < 0) {
  82. right_itr = curr_itr;
  83. } else if (orientations[j] < 0 && orientations[prev] >= 0) {
  84. left_itr = curr_itr;
  85. }
  86. }
  87. assert(left_itr != right_itr);
  88. for (auto itr=left_itr; itr!=right_itr; itr++) {
  89. if (itr == boundary.end()) itr = boundary.begin();
  90. if (itr == right_itr) break;
  91. if (itr == left_itr || itr == right_itr) continue;
  92. itr = boundary.erase(itr);
  93. if (itr == boundary.begin()) {
  94. itr = boundary.end();
  95. } else {
  96. itr--;
  97. }
  98. }
  99. if (right_itr == boundary.begin()) {
  100. assert(std::next(left_itr, 1) == boundary.end());
  101. boundary.insert(boundary.end(), order[i]);
  102. } else {
  103. assert(std::next(left_itr, 1) == right_itr);
  104. boundary.insert(right_itr, order[i]);
  105. }
  106. }
  107. }
  108. const size_t num_faces = faces.size();
  109. if (num_faces == 0) {
  110. // All input points are collinear.
  111. // Do nothing here.
  112. } else {
  113. F.resize(num_faces, 3);
  114. for (size_t i=0; i<num_faces; i++) {
  115. F.row(i) = faces[i];
  116. }
  117. }
  118. }
  119. #ifdef IGL_STATIC_LIBRARY
  120. template void igl::lexicographic_triangulation<Eigen::Matrix<double, -1, -1, 0, -1, -1>, short (*)(double const*, double const*, double const*), Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, short (*)(double const*, double const*, double const*), Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  121. #endif