face_occurrences.cpp 2.0 KB

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