unique.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "unique.h"
  9. #include "sort.h"
  10. #include "IndexComparison.h"
  11. #include "SortableRow.h"
  12. #include "sortrows.h"
  13. #include "list_to_matrix.h"
  14. #include <algorithm>
  15. #include <iostream>
  16. #include <map>
  17. template <typename T>
  18. IGL_INLINE void igl::unique(
  19. const std::vector<T> & A,
  20. std::vector<T> & C,
  21. std::vector<size_t> & IA,
  22. std::vector<size_t> & IC)
  23. {
  24. using namespace std;
  25. std::vector<size_t> IM;
  26. std::vector<T> sortA;
  27. igl::sort(A,true,sortA,IM);
  28. // Original unsorted index map
  29. IA.resize(sortA.size());
  30. for(int i=0;i<(int)sortA.size();i++)
  31. {
  32. IA[i] = i;
  33. }
  34. IA.erase(
  35. std::unique(
  36. IA.begin(),
  37. IA.end(),
  38. igl::IndexEquals<const std::vector<T>& >(sortA)),IA.end());
  39. IC.resize(A.size());
  40. {
  41. int j = 0;
  42. for(int i = 0;i<(int)sortA.size();i++)
  43. {
  44. if(sortA[IA[j]] != sortA[i])
  45. {
  46. j++;
  47. }
  48. IC[IM[i]] = j;
  49. }
  50. }
  51. C.resize(IA.size());
  52. // Reindex IA according to IM
  53. for(int i = 0;i<(int)IA.size();i++)
  54. {
  55. IA[i] = IM[IA[i]];
  56. C[i] = A[IA[i]];
  57. }
  58. }
  59. // Obsolete slow version converting to vectors
  60. // template <typename DerivedA, typename DerivedIA, typename DerivedIC>
  61. // IGL_INLINE void igl::unique_rows(
  62. // const Eigen::PlainObjectBase<DerivedA>& A,
  63. // Eigen::PlainObjectBase<DerivedA>& C,
  64. // Eigen::PlainObjectBase<DerivedIA>& IA,
  65. // Eigen::PlainObjectBase<DerivedIC>& IC)
  66. // {
  67. // using namespace std;
  68. //
  69. // typedef Eigen::Matrix<typename DerivedA::Scalar, Eigen::Dynamic, 1> RowVector;
  70. // vector<SortableRow<RowVector> > rows;
  71. // rows.resize(A.rows());
  72. // // Loop over rows
  73. // for(int i = 0;i<A.rows();i++)
  74. // {
  75. // RowVector ri = A.row(i);
  76. // rows[i] = SortableRow<RowVector>(ri);
  77. // }
  78. // vector<SortableRow<RowVector> > vC;
  79. //
  80. // // unique on rows
  81. // vector<size_t> vIA;
  82. // vector<size_t> vIC;
  83. // unique(rows,vC,vIA,vIC);
  84. //
  85. // // Convert to eigen
  86. // C.resize(vC.size(),A.cols());
  87. // IA.resize(vIA.size(),1);
  88. // IC.resize(vIC.size(),1);
  89. // for(int i = 0;i<C.rows();i++)
  90. // {
  91. // C.row(i) = vC[i].data;
  92. // IA(i) = vIA[i];
  93. // }
  94. // for(int i = 0;i<A.rows();i++)
  95. // {
  96. // IC(i) = vIC[i];
  97. // }
  98. // }
  99. // Obsolete
  100. // template <typename DerivedA, typename DerivedIA, typename DerivedIC>
  101. // IGL_INLINE void igl::unique_rows_many(
  102. // const Eigen::PlainObjectBase<DerivedA>& A,
  103. // Eigen::PlainObjectBase<DerivedA>& C,
  104. // Eigen::PlainObjectBase<DerivedIA>& IA,
  105. // Eigen::PlainObjectBase<DerivedIC>& IC)
  106. // {
  107. // using namespace std;
  108. // // frequency map
  109. // typedef Eigen::Matrix<typename DerivedA::Scalar, Eigen::Dynamic, 1> RowVector;
  110. // IC.resize(A.rows(),1);
  111. // map<SortableRow<RowVector>, int> fm;
  112. // const int m = A.rows();
  113. // for(int i = 0;i<m;i++)
  114. // {
  115. // RowVector ri = A.row(i);
  116. // if(fm.count(SortableRow<RowVector>(ri)) == 0)
  117. // {
  118. // fm[SortableRow<RowVector>(ri)] = i;
  119. // }
  120. // IC(i) = fm[SortableRow<RowVector>(ri)];
  121. // }
  122. // IA.resize(fm.size(),1);
  123. // Eigen::VectorXi RIA(m);
  124. // C.resize(fm.size(),A.cols());
  125. // {
  126. // int i = 0;
  127. // for(typename map<SortableRow<RowVector > , int >::const_iterator fit = fm.begin();
  128. // fit != fm.end();
  129. // fit++)
  130. // {
  131. // IA(i) = fit->second;
  132. // RIA(fit->second) = i;
  133. // C.row(i) = fit->first.data;
  134. // i++;
  135. // }
  136. // }
  137. // // IC should index C
  138. // for(int i = 0;i<m;i++)
  139. // {
  140. // IC(i) = RIA(IC(i));
  141. // }
  142. // }
  143. template <typename DerivedA, typename DerivedIA, typename DerivedIC>
  144. IGL_INLINE void igl::unique_rows(
  145. const Eigen::PlainObjectBase<DerivedA>& A,
  146. Eigen::PlainObjectBase<DerivedA>& C,
  147. Eigen::PlainObjectBase<DerivedIA>& IA,
  148. Eigen::PlainObjectBase<DerivedIC>& IC)
  149. {
  150. using namespace std;
  151. using namespace igl;
  152. using namespace Eigen;
  153. VectorXi IM;
  154. Eigen::PlainObjectBase<DerivedA> sortA;
  155. sortrows(A,true,sortA,IM);
  156. vector<int> vIA(sortA.rows());
  157. for(int i=0;i<(int)sortA.rows();i++)
  158. {
  159. vIA[i] = i;
  160. }
  161. vIA.erase(
  162. std::unique(
  163. vIA.begin(),
  164. vIA.end(),
  165. igl::IndexRowEquals<const Eigen::PlainObjectBase<DerivedA> &>(sortA)),vIA.end());
  166. IC.resize(A.rows(),1);
  167. {
  168. int j = 0;
  169. for(int i = 0;i<(int)sortA.rows();i++)
  170. {
  171. if(sortA.row(vIA[j]) != sortA.row(i))
  172. {
  173. j++;
  174. }
  175. IC(IM(i,0),0) = j;
  176. }
  177. }
  178. C.resize(vIA.size(),A.cols());
  179. IA.resize(vIA.size(),1);
  180. // Reindex IA according to IM
  181. for(int i = 0;i<(int)vIA.size();i++)
  182. {
  183. IA(i,0) = IM(vIA[i],0);
  184. C.row(i) = A.row(IA(i,0));
  185. }
  186. }
  187. #ifndef IGL_HEADER_ONLY
  188. template void igl::unique<int>(std::vector<int, std::allocator<int> > const&, std::vector<int, std::allocator<int> >&, std::vector<size_t, std::allocator<size_t> >&, std::vector<size_t, std::allocator<size_t> >&);
  189. template void igl::unique_rows<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  190. template void igl::unique_rows<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  191. template void igl::unique_rows<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  192. template void igl::unique_rows<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  193. template void igl::unique_rows<Eigen::Matrix<int, -1, 2, 0, -1, 2>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  194. #endif