IndexComparison.h 2.7 KB

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