piecewise_constant_winding_number.cpp 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Alec Jacobson
  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 "piecewise_constant_winding_number.h"
  9. #include "unique_edge_map.h"
  10. #include "PI.h"
  11. template <
  12. typename DerivedF,
  13. typename DeriveduE,
  14. typename uE2EType>
  15. IGL_INLINE bool igl::piecewise_constant_winding_number(
  16. const Eigen::MatrixBase<DerivedF>& F,
  17. const Eigen::MatrixBase<DeriveduE>& uE,
  18. const std::vector<std::vector<uE2EType> >& uE2E)
  19. {
  20. const size_t num_faces = F.rows();
  21. const size_t num_edges = uE.rows();
  22. const auto edge_index_to_face_index = [&](size_t ei)
  23. {
  24. return ei % num_faces;
  25. };
  26. const auto is_consistent = [&](size_t fid, size_t s, size_t d)
  27. {
  28. if ((size_t)F(fid, 0) == s && (size_t)F(fid, 1) == d) return true;
  29. if ((size_t)F(fid, 1) == s && (size_t)F(fid, 2) == d) return true;
  30. if ((size_t)F(fid, 2) == s && (size_t)F(fid, 0) == d) return true;
  31. if ((size_t)F(fid, 0) == d && (size_t)F(fid, 1) == s) return false;
  32. if ((size_t)F(fid, 1) == d && (size_t)F(fid, 2) == s) return false;
  33. if ((size_t)F(fid, 2) == d && (size_t)F(fid, 0) == s) return false;
  34. throw "Invalid face!!";
  35. };
  36. for (size_t i=0; i<num_edges; i++)
  37. {
  38. const size_t s = uE(i,0);
  39. const size_t d = uE(i,1);
  40. int count=0;
  41. for (const auto& ei : uE2E[i])
  42. {
  43. const size_t fid = edge_index_to_face_index(ei);
  44. if (is_consistent(fid, s, d))
  45. {
  46. count++;
  47. }
  48. else
  49. {
  50. count--;
  51. }
  52. }
  53. if (count != 0)
  54. {
  55. return false;
  56. }
  57. }
  58. return true;
  59. }
  60. template <typename DerivedF>
  61. IGL_INLINE bool igl::piecewise_constant_winding_number(
  62. const Eigen::MatrixBase<DerivedF>& F)
  63. {
  64. Eigen::Matrix<typename DerivedF::Scalar,Eigen::Dynamic,2> E, uE;
  65. Eigen::Matrix<typename DerivedF::Scalar,Eigen::Dynamic,1> EMAP;
  66. std::vector<std::vector<size_t> > uE2E;
  67. unique_edge_map(F, E, uE, EMAP, uE2E);
  68. return piecewise_constant_winding_number(F,uE,uE2E);
  69. }
  70. #ifdef IGL_STATIC_LIBRARY
  71. // Explicit template instantiation
  72. // generated by autoexplicit.sh
  73. template bool igl::piecewise_constant_winding_number<Eigen::Matrix<int, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, unsigned long>(Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, std::vector<std::vector<unsigned long, std::allocator<unsigned long> >, std::allocator<std::vector<unsigned long, std::allocator<unsigned long> > > > const&);
  74. template bool igl::piecewise_constant_winding_number<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, unsigned long>(Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, std::vector<std::vector<unsigned long, std::allocator<unsigned long> >, std::allocator<std::vector<unsigned long, std::allocator<unsigned long> > > > const&);
  75. #ifdef WIN32
  76. template bool igl::piecewise_constant_winding_number<class Eigen::Matrix<int, -1, -1, 0, -1, -1>, class Eigen::Matrix<int, -1, -1, 0, -1, -1>, unsigned __int64>(class Eigen::MatrixBase<class Eigen::Matrix<int, -1, -1, 0, -1, -1>> const &, class Eigen::MatrixBase<class Eigen::Matrix<int, -1, -1, 0, -1, -1>> const &, class std::vector<class std::vector<unsigned __int64, class std::allocator<unsigned __int64>>, class std::allocator<class std::vector<unsigned __int64, class std::allocator<unsigned __int64>>>> const &);
  77. template bool igl::piecewise_constant_winding_number<class Eigen::Matrix<int, -1, 3, 1, -1, 3>, class Eigen::Matrix<int, -1, -1, 0, -1, -1>, unsigned __int64>(class Eigen::MatrixBase<class Eigen::Matrix<int, -1, 3, 1, -1, 3>> const &, class Eigen::MatrixBase<class Eigen::Matrix<int, -1, -1, 0, -1, -1>> const &, class std::vector<class std::vector<unsigned __int64, class std::allocator<unsigned __int64>>, class std::allocator<class std::vector<unsigned __int64, class std::allocator<unsigned __int64>>>> const &);
  78. #endif
  79. #endif