IndexComparison.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <[email protected]>
  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. #ifndef IGL_INDEXCOMPARISON_H
  9. #define IGL_INDEXCOMPARISON_H
  10. namespace igl{
  11. /// Comparison struct used by sort
  12. /// http://bytes.com/topic/c/answers/132045-sort-get-index
  13. template<class T> struct IndexLessThan
  14. {
  15. IndexLessThan(const T arr) : arr(arr) {}
  16. bool operator()(const size_t a, const size_t b) const
  17. {
  18. return arr[a] < arr[b];
  19. }
  20. const T arr;
  21. };
  22. /// Comparison struct used by unique
  23. template<class T> struct IndexEquals
  24. {
  25. IndexEquals(const T arr) : arr(arr) {}
  26. bool operator()(const size_t a, const size_t b) const
  27. {
  28. return arr[a] == arr[b];
  29. }
  30. const T arr;
  31. };
  32. /// Comparison struct for vectors for use with functions like std::sort
  33. template<class T> struct IndexVectorLessThan
  34. {
  35. IndexVectorLessThan(const T & vec) : vec ( vec) {}
  36. bool operator()(const size_t a, const size_t b) const
  37. {
  38. return vec(a) < vec(b);
  39. }
  40. const T & vec;
  41. };
  42. /// Comparison struct for use with functions like std::sort
  43. template<class T> struct IndexDimLessThan
  44. {
  45. IndexDimLessThan(const T & mat,const int & dim, const int & j) :
  46. mat(mat),
  47. dim(dim),
  48. j(j)
  49. {}
  50. bool operator()(const size_t a, const size_t b) const
  51. {
  52. if(dim == 1)
  53. {
  54. return mat(a,j) < mat(b,j);
  55. }else
  56. {
  57. return mat(j,a) < mat(j,b);
  58. }
  59. }
  60. const T & mat;
  61. const int & dim;
  62. const int & j;
  63. };
  64. /// Comparison struct For use with functions like std::sort
  65. template<class T> struct IndexRowLessThan
  66. {
  67. IndexRowLessThan(const T & mat) : mat ( mat) {}
  68. bool operator()(const size_t a, const size_t b) const
  69. {
  70. const int cols = mat.cols();
  71. // Lexicographical order
  72. for(int j = 0;j<cols;j++)
  73. {
  74. if(mat(a,j) > mat(b,j))
  75. {
  76. return false;
  77. } else if(mat(a,j) < mat(b,j))
  78. {
  79. return true;
  80. }
  81. }
  82. // equality is false
  83. return false;
  84. }
  85. const T & mat;
  86. };
  87. /// Comparison struct for use with functions like std::sort
  88. template<class T> struct IndexRowEquals
  89. {
  90. IndexRowEquals(const T & mat) : mat ( mat) {}
  91. bool operator()(const size_t a, const size_t b) const
  92. {
  93. const int cols = mat.cols();
  94. // Lexicographical order
  95. for(int j = 0;j<cols;j++)
  96. {
  97. if(mat(a,j) != mat(b,j))
  98. {
  99. return false;
  100. }
  101. }
  102. return true;
  103. }
  104. const T & mat;
  105. };
  106. }
  107. #endif