outer_facet.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Qingnan Zhou <qnzhou@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. #include "outer_facet.h"
  9. #include "../outer_element.h"
  10. #include "order_facets_around_edge.h"
  11. #include <algorithm>
  12. template<
  13. typename DerivedV,
  14. typename DerivedF,
  15. typename DerivedI,
  16. typename IndexType
  17. >
  18. IGL_INLINE void igl::cgal::outer_facet(
  19. const Eigen::PlainObjectBase<DerivedV> & V,
  20. const Eigen::PlainObjectBase<DerivedF> & F,
  21. const Eigen::PlainObjectBase<DerivedI> & I,
  22. IndexType & f,
  23. bool & flipped) {
  24. // Algorithm:
  25. //
  26. // 1. Find an outer edge (s, d).
  27. //
  28. // 2. Order adjacent facets around this edge. Because the edge is an
  29. // outer edge, there exists a plane passing through it such that all its
  30. // adjacent facets lie on the same side. The implementation of
  31. // order_facets_around_edge() will find a natural start facet such that
  32. // The first and last facets according to this order are on the outside.
  33. //
  34. // 3. Because the vertex s is an outer vertex by construction (see
  35. // implemnetation of outer_edge()). The first adjacent facet is facing
  36. // outside (i.e. flipped=false) if it contains directed edge (s, d).
  37. //
  38. typedef typename DerivedV::Scalar Scalar; typedef typename DerivedV::Index
  39. Index; const size_t INVALID = std::numeric_limits<size_t>::max();
  40. Index s,d;
  41. Eigen::Matrix<Index,Eigen::Dynamic,1> incident_faces;
  42. outer_edge(V, F, I, s, d, incident_faces);
  43. assert(incident_faces.size() > 0);
  44. auto convert_to_signed_index = [&](size_t fid) -> int{
  45. if ((F(fid, 0) == s && F(fid, 1) == d) ||
  46. (F(fid, 1) == s && F(fid, 2) == d) ||
  47. (F(fid, 2) == s && F(fid, 0) == d) ) {
  48. return int(fid+1) * -1;
  49. } else {
  50. return int(fid+1);
  51. }
  52. };
  53. auto signed_index_to_index = [&](int signed_id) -> size_t {
  54. return size_t(abs(signed_id) - 1);
  55. };
  56. std::vector<int> adj_faces(incident_faces.size());
  57. std::transform(incident_faces.data(),
  58. incident_faces.data() + incident_faces.size(),
  59. adj_faces.begin(),
  60. convert_to_signed_index);
  61. Eigen::VectorXi order;
  62. order_facets_around_edge(V, F, s, d, adj_faces, order);
  63. f = signed_index_to_index(adj_faces[order[0]]);
  64. flipped = adj_faces[order[0]] > 0;
  65. }