face_occurrences.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #include "face_occurrences.h"
  9. #include "list_to_matrix.h"
  10. #include "matrix_to_list.h"
  11. #include <map>
  12. #include "sort.h"
  13. #include <cassert>
  14. template <typename IntegerF, typename IntegerC>
  15. IGL_INLINE void igl::face_occurrences(
  16. const std::vector<std::vector<IntegerF> > & F,
  17. std::vector<IntegerC> & C)
  18. {
  19. using namespace std;
  20. // Get a list of sorted faces
  21. vector<vector<IntegerF> > sortedF = F;
  22. for(int i = 0; i < (int)F.size();i++)
  23. {
  24. sort(sortedF[i].begin(),sortedF[i].end());
  25. }
  26. // Count how many times each sorted face occurs
  27. map<vector<IntegerF>,int> counts;
  28. for(int i = 0; i < (int)sortedF.size();i++)
  29. {
  30. if(counts.find(sortedF[i]) == counts.end())
  31. {
  32. // initialize to count of 1
  33. counts[sortedF[i]] = 1;
  34. }else
  35. {
  36. // increment count
  37. counts[sortedF[i]]++;
  38. assert(counts[sortedF[i]] == 2 && "Input should be manifold");
  39. }
  40. }
  41. // Resize output to fit number of ones
  42. C.resize(F.size());
  43. for(int i = 0;i< (int)F.size();i++)
  44. {
  45. // sorted face should definitely be in counts map
  46. assert(counts.find(sortedF[i]) != counts.end());
  47. C[i] = counts[sortedF[i]];
  48. }
  49. }
  50. template <typename DerivedF, typename DerivedC>
  51. IGL_INLINE void igl::face_occurrences(
  52. const Eigen::MatrixBase<DerivedF> & F,
  53. Eigen::PlainObjectBase<DerivedC> & C)
  54. {
  55. // Should really just rewrite using Eigen+libigl ...
  56. std::vector<std::vector<typename DerivedF::Scalar> > vF;
  57. matrix_to_list(F,vF);
  58. std::vector<std::vector<typename DerivedC::Scalar> > vC;
  59. igl::face_occurrences(vF,vC);
  60. list_to_matrix(vC,C);
  61. }
  62. #ifdef IGL_STATIC_LIBRARY
  63. // Explicit template instantiation
  64. // generated by autoexplicit.sh
  65. template void igl::face_occurrences<unsigned int, int>(std::vector<std::vector<unsigned int, std::allocator<unsigned int> >, std::allocator<std::vector<unsigned int, std::allocator<unsigned int> > > > const&, std::vector<int, std::allocator<int> >&);
  66. template void igl::face_occurrences<int, int>(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > const&, std::vector<int, std::allocator<int> >&);
  67. #endif