unique.cpp 8.9 KB

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