unique.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 <typename T>
  62. IGL_INLINE void igl::unique(
  63. const std::vector<T> & A,
  64. std::vector<T> & C)
  65. {
  66. std::vector<size_t> IA,IC;
  67. return igl::unique(A,C,IA,IC);
  68. }
  69. template <
  70. typename DerivedA,
  71. typename DerivedC,
  72. typename DerivedIA,
  73. typename DerivedIC>
  74. IGL_INLINE void igl::unique(
  75. const Eigen::PlainObjectBase<DerivedA> & A,
  76. Eigen::PlainObjectBase<DerivedC> & C,
  77. Eigen::PlainObjectBase<DerivedIA> & IA,
  78. Eigen::PlainObjectBase<DerivedIC> & IC)
  79. {
  80. using namespace std;
  81. using namespace Eigen;
  82. vector<typename DerivedA::Scalar > vA;
  83. vector<typename DerivedC::Scalar > vC;
  84. vector<size_t> vIA,vIC;
  85. matrix_to_list(A,vA);
  86. unique(vA,vC,vIA,vIC);
  87. list_to_matrix(vC,C);
  88. list_to_matrix(vIA,IA);
  89. list_to_matrix(vIC,IC);
  90. }
  91. template <
  92. typename DerivedA,
  93. typename DerivedC
  94. >
  95. IGL_INLINE void igl::unique(
  96. const Eigen::PlainObjectBase<DerivedA> & A,
  97. Eigen::PlainObjectBase<DerivedC> & C)
  98. {
  99. using namespace std;
  100. using namespace Eigen;
  101. vector<typename DerivedA::Scalar > vA;
  102. vector<typename DerivedC::Scalar > vC;
  103. vector<size_t> vIA,vIC;
  104. matrix_to_list(A,vA);
  105. unique(vA,vC,vIA,vIC);
  106. list_to_matrix(vC,C);
  107. }
  108. // Obsolete slow version converting to vectors
  109. // template <typename DerivedA, typename DerivedIA, typename DerivedIC>
  110. // IGL_INLINE void igl::unique_rows(
  111. // const Eigen::PlainObjectBase<DerivedA>& A,
  112. // Eigen::PlainObjectBase<DerivedA>& C,
  113. // Eigen::PlainObjectBase<DerivedIA>& IA,
  114. // Eigen::PlainObjectBase<DerivedIC>& IC)
  115. // {
  116. // using namespace std;
  117. //
  118. // typedef Eigen::Matrix<typename DerivedA::Scalar, Eigen::Dynamic, 1> RowVector;
  119. // vector<SortableRow<RowVector> > rows;
  120. // rows.resize(A.rows());
  121. // // Loop over rows
  122. // for(int i = 0;i<A.rows();i++)
  123. // {
  124. // RowVector ri = A.row(i);
  125. // rows[i] = SortableRow<RowVector>(ri);
  126. // }
  127. // vector<SortableRow<RowVector> > vC;
  128. //
  129. // // unique on rows
  130. // vector<size_t> vIA;
  131. // vector<size_t> vIC;
  132. // unique(rows,vC,vIA,vIC);
  133. //
  134. // // Convert to eigen
  135. // C.resize(vC.size(),A.cols());
  136. // IA.resize(vIA.size(),1);
  137. // IC.resize(vIC.size(),1);
  138. // for(int i = 0;i<C.rows();i++)
  139. // {
  140. // C.row(i) = vC[i].data;
  141. // IA(i) = vIA[i];
  142. // }
  143. // for(int i = 0;i<A.rows();i++)
  144. // {
  145. // IC(i) = vIC[i];
  146. // }
  147. // }
  148. // Obsolete
  149. // template <typename DerivedA, typename DerivedIA, typename DerivedIC>
  150. // IGL_INLINE void igl::unique_rows_many(
  151. // const Eigen::PlainObjectBase<DerivedA>& A,
  152. // Eigen::PlainObjectBase<DerivedA>& C,
  153. // Eigen::PlainObjectBase<DerivedIA>& IA,
  154. // Eigen::PlainObjectBase<DerivedIC>& IC)
  155. // {
  156. // using namespace std;
  157. // // frequency map
  158. // typedef Eigen::Matrix<typename DerivedA::Scalar, Eigen::Dynamic, 1> RowVector;
  159. // IC.resize(A.rows(),1);
  160. // map<SortableRow<RowVector>, int> fm;
  161. // const int m = A.rows();
  162. // for(int i = 0;i<m;i++)
  163. // {
  164. // RowVector ri = A.row(i);
  165. // if(fm.count(SortableRow<RowVector>(ri)) == 0)
  166. // {
  167. // fm[SortableRow<RowVector>(ri)] = i;
  168. // }
  169. // IC(i) = fm[SortableRow<RowVector>(ri)];
  170. // }
  171. // IA.resize(fm.size(),1);
  172. // Eigen::VectorXi RIA(m);
  173. // C.resize(fm.size(),A.cols());
  174. // {
  175. // int i = 0;
  176. // for(typename map<SortableRow<RowVector > , int >::const_iterator fit = fm.begin();
  177. // fit != fm.end();
  178. // fit++)
  179. // {
  180. // IA(i) = fit->second;
  181. // RIA(fit->second) = i;
  182. // C.row(i) = fit->first.data;
  183. // i++;
  184. // }
  185. // }
  186. // // IC should index C
  187. // for(int i = 0;i<m;i++)
  188. // {
  189. // IC(i) = RIA(IC(i));
  190. // }
  191. // }
  192. template <typename DerivedA, typename DerivedIA, typename DerivedIC>
  193. IGL_INLINE void igl::unique_rows(
  194. const Eigen::PlainObjectBase<DerivedA>& A,
  195. Eigen::PlainObjectBase<DerivedA>& C,
  196. Eigen::PlainObjectBase<DerivedIA>& IA,
  197. Eigen::PlainObjectBase<DerivedIC>& IC)
  198. {
  199. using namespace std;
  200. using namespace Eigen;
  201. VectorXi IM;
  202. DerivedA sortA;
  203. sortrows(A,true,sortA,IM);
  204. vector<int> vIA(sortA.rows());
  205. for(int i=0;i<(int)sortA.rows();i++)
  206. {
  207. vIA[i] = i;
  208. }
  209. vIA.erase(
  210. std::unique(
  211. vIA.begin(),
  212. vIA.end(),
  213. igl::IndexRowEquals<const Eigen::PlainObjectBase<DerivedA> &>(sortA)),vIA.end());
  214. IC.resize(A.rows(),1);
  215. {
  216. int j = 0;
  217. for(int i = 0;i<(int)sortA.rows();i++)
  218. {
  219. if(sortA.row(vIA[j]) != sortA.row(i))
  220. {
  221. j++;
  222. }
  223. IC(IM(i,0),0) = j;
  224. }
  225. }
  226. C.resize(vIA.size(),A.cols());
  227. IA.resize(vIA.size(),1);
  228. // Reindex IA according to IM
  229. for(int i = 0;i<(int)vIA.size();i++)
  230. {
  231. IA(i,0) = IM(vIA[i],0);
  232. C.row(i) = A.row(IA(i,0));
  233. }
  234. }
  235. #ifdef IGL_STATIC_LIBRARY
  236. // Explicit template specialization
  237. 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> >&);
  238. 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> >&);
  239. 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> >&);
  240. 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> >&);
  241. 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> >&);
  242. 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> >&);
  243. 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> >&);
  244. 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> >&);
  245. 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> >&);
  246. template void igl::unique<int>(std::vector<int, std::allocator<int> > const&, std::vector<int, std::allocator<int> >&);
  247. template void igl::unique<long>(std::vector<long, std::allocator<long> > const&, std::vector<long, std::allocator<long> >&, std::vector<unsigned long, std::allocator<unsigned long> >&, std::vector<unsigned long, std::allocator<unsigned long> >&);
  248. template void igl::unique_rows<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  249. #endif