face_occurences.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "face_occurences.h"
  2. #include <map>
  3. #include "sort.h"
  4. #include <cassert>
  5. template <typename IntegerF, typename IntegerC>
  6. IGL_INLINE void igl::face_occurences(
  7. const std::vector<std::vector<IntegerF> > & F,
  8. std::vector<IntegerC> & C)
  9. {
  10. using namespace std;
  11. // Get a list of sorted faces
  12. vector<vector<IntegerF> > sortedF = F;
  13. for(int i = 0; i < (int)F.size();i++)
  14. {
  15. sort(sortedF[i].begin(),sortedF[i].end());
  16. }
  17. // Count how many times each sorted face occurs
  18. map<vector<IntegerF>,int> counts;
  19. for(int i = 0; i < (int)sortedF.size();i++)
  20. {
  21. if(counts.find(sortedF[i]) == counts.end())
  22. {
  23. // initialize to count of 1
  24. counts[sortedF[i]] = 1;
  25. }else
  26. {
  27. // increment count
  28. counts[sortedF[i]]++;
  29. assert(counts[sortedF[i]] == 2);
  30. }
  31. }
  32. // Resize output to fit number of ones
  33. C.resize(F.size());
  34. for(int i = 0;i< (int)F.size();i++)
  35. {
  36. // sorted face should definitely be in counts map
  37. assert(counts.find(sortedF[i]) != counts.end());
  38. C[i] = counts[sortedF[i]];
  39. }
  40. }
  41. #ifndef IGL_HEADER_ONLY
  42. // Explicit template specialization
  43. template void igl::face_occurences<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> >&);
  44. #endif