unique.cpp 8.9 KB

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