lexicographic_triangulation.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "ExactPredicate.h"
  11. #include "../../sortrows.h"
  12. #include <vector>
  13. template<
  14. typename DerivedP,
  15. typename DerivedF
  16. >
  17. IGL_INLINE void igl::copyleft::cgal::lexicographic_triangulation(
  18. const Eigen::PlainObjectBase<DerivedP>& P,
  19. Eigen::PlainObjectBase<DerivedF>& F)
  20. {
  21. typedef typename DerivedP::Scalar Scalar;
  22. typedef typename igl::copyleft::cgal::ExactPredicate<Scalar> Predicate;
  23. const size_t num_pts = P.rows();
  24. if (num_pts < 3) {
  25. throw "At least 3 points are required for triangulation!";
  26. }
  27. // Sort points in lexicographic order.
  28. DerivedP ordered_P;
  29. Eigen::VectorXi order;
  30. igl::sortrows(P, true, ordered_P, order);
  31. std::vector<Eigen::Vector3i> faces;
  32. std::list<size_t> boundary;
  33. const Scalar p0[] = {ordered_P(0, 0), ordered_P(0, 1)};
  34. const Scalar p1[] = {ordered_P(1, 0), ordered_P(1, 1)};
  35. for (size_t i=2; i<num_pts; i++) {
  36. const Scalar curr_p[] = {ordered_P(i, 0), ordered_P(i, 1)};
  37. if (faces.size() == 0) {
  38. // All points processed so far are collinear.
  39. // Check if the current point is collinear with every points before it.
  40. auto orientation = Predicate::orientation(p0, p1, curr_p);
  41. if (orientation != 0) {
  42. // Add a fan of triangles eminating from curr_p.
  43. if (orientation > 0) {
  44. for (size_t j=0; j<=i-2; j++) {
  45. faces.push_back({order[j], order[j+1], order[i]});
  46. }
  47. } else if (orientation < 0) {
  48. for (size_t j=0; j<=i-2; j++) {
  49. faces.push_back({order[j+1], order[j], order[i]});
  50. }
  51. }
  52. // Initialize current boundary.
  53. boundary.insert(boundary.end(), order.data(), order.data()+i+1);
  54. if (orientation < 0) {
  55. boundary.reverse();
  56. }
  57. }
  58. } else {
  59. const size_t bd_size = boundary.size();
  60. assert(bd_size >= 3);
  61. std::vector<short> orientations;
  62. for (auto itr=boundary.begin(); itr!=boundary.end(); itr++) {
  63. auto next_itr = std::next(itr, 1);
  64. if (next_itr == boundary.end()) {
  65. next_itr = boundary.begin();
  66. }
  67. const Scalar bd_p0[] = {P(*itr, 0), P(*itr, 1)};
  68. const Scalar bd_p1[] = {P(*next_itr, 0), P(*next_itr, 1)};
  69. auto orientation = Predicate::orientation(bd_p0, bd_p1, curr_p);
  70. if (orientation < 0) {
  71. faces.push_back({*next_itr, *itr, order[i]});
  72. }
  73. orientations.push_back(orientation);
  74. }
  75. auto left_itr = boundary.begin();
  76. auto right_itr = boundary.begin();
  77. auto curr_itr = boundary.begin();
  78. for (size_t j=0; j<bd_size; j++, curr_itr++) {
  79. size_t prev = (j+bd_size-1) % bd_size;
  80. if (orientations[j] >= 0 && orientations[prev] < 0) {
  81. right_itr = curr_itr;
  82. } else if (orientations[j] < 0 && orientations[prev] >= 0) {
  83. left_itr = curr_itr;
  84. }
  85. }
  86. assert(left_itr != right_itr);
  87. for (auto itr=left_itr; itr!=right_itr; itr++) {
  88. if (itr == boundary.end()) itr = boundary.begin();
  89. if (itr == right_itr) break;
  90. if (itr == left_itr || itr == right_itr) continue;
  91. itr = boundary.erase(itr);
  92. if (itr == boundary.begin()) {
  93. itr = boundary.end();
  94. } else {
  95. itr--;
  96. }
  97. }
  98. if (right_itr == boundary.begin()) {
  99. assert(std::next(left_itr, 1) == boundary.end());
  100. boundary.insert(boundary.end(), order[i]);
  101. } else {
  102. assert(std::next(left_itr, 1) == right_itr);
  103. boundary.insert(right_itr, order[i]);
  104. }
  105. }
  106. }
  107. const size_t num_faces = faces.size();
  108. if (num_faces == 0) {
  109. // All input points are collinear.
  110. // Do nothing here.
  111. } else {
  112. F.resize(num_faces, 3);
  113. for (size_t i=0; i<num_faces; i++) {
  114. F.row(i) = faces[i];
  115. }
  116. }
  117. }