cat.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <[email protected]>
  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 "cat.h"
  9. #include <cstdio>
  10. // Bug in unsupported/Eigen/SparseExtra needs iostream first
  11. #include <iostream>
  12. #include <unsupported/Eigen/SparseExtra>
  13. // Sparse matrices need to be handled carefully. Because C++ does not
  14. // Template:
  15. // Scalar sparse matrix scalar type, e.g. double
  16. template <typename Scalar>
  17. IGL_INLINE void igl::cat(
  18. const int dim,
  19. const Eigen::SparseMatrix<Scalar> & A,
  20. const Eigen::SparseMatrix<Scalar> & B,
  21. Eigen::SparseMatrix<Scalar> & C)
  22. {
  23. assert(dim == 1 || dim == 2);
  24. using namespace Eigen;
  25. // Special case if B or A is empty
  26. if(A.size() == 0)
  27. {
  28. C = B;
  29. return;
  30. }
  31. if(B.size() == 0)
  32. {
  33. C = A;
  34. return;
  35. }
  36. // This is faster than using DynamicSparseMatrix or setFromTriplets
  37. C = SparseMatrix<Scalar>(
  38. dim == 1 ? A.rows()+B.rows() : A.rows(),
  39. dim == 1 ? A.cols() : A.cols()+B.cols());
  40. Eigen::VectorXi per_col = Eigen::VectorXi::Zero(C.cols());
  41. if(dim == 1)
  42. {
  43. assert(A.outerSize() == B.outerSize());
  44. for(int k = 0;k<A.outerSize();++k)
  45. {
  46. for(typename SparseMatrix<Scalar>::InnerIterator it (A,k); it; ++it)
  47. {
  48. per_col(k)++;
  49. }
  50. for(typename SparseMatrix<Scalar>::InnerIterator it (B,k); it; ++it)
  51. {
  52. per_col(k)++;
  53. }
  54. }
  55. }else
  56. {
  57. for(int k = 0;k<A.outerSize();++k)
  58. {
  59. for(typename SparseMatrix<Scalar>::InnerIterator it (A,k); it; ++it)
  60. {
  61. per_col(k)++;
  62. }
  63. }
  64. for(int k = 0;k<B.outerSize();++k)
  65. {
  66. for(typename SparseMatrix<Scalar>::InnerIterator it (B,k); it; ++it)
  67. {
  68. per_col(A.cols() + k)++;
  69. }
  70. }
  71. }
  72. C.reserve(per_col);
  73. if(dim == 1)
  74. {
  75. for(int k = 0;k<A.outerSize();++k)
  76. {
  77. for(typename SparseMatrix<Scalar>::InnerIterator it (A,k); it; ++it)
  78. {
  79. C.insert(it.row(),k) = it.value();
  80. }
  81. for(typename SparseMatrix<Scalar>::InnerIterator it (B,k); it; ++it)
  82. {
  83. C.insert(A.rows()+it.row(),k) = it.value();
  84. }
  85. }
  86. }else
  87. {
  88. for(int k = 0;k<A.outerSize();++k)
  89. {
  90. for(typename SparseMatrix<Scalar>::InnerIterator it (A,k); it; ++it)
  91. {
  92. C.insert(it.row(),k) = it.value();
  93. }
  94. }
  95. for(int k = 0;k<B.outerSize();++k)
  96. {
  97. for(typename SparseMatrix<Scalar>::InnerIterator it (B,k); it; ++it)
  98. {
  99. C.insert(it.row(),A.cols()+k) = it.value();
  100. }
  101. }
  102. }
  103. C.makeCompressed();
  104. }
  105. template <typename Derived, class MatC>
  106. IGL_INLINE void igl::cat(
  107. const int dim,
  108. const Eigen::MatrixBase<Derived> & A,
  109. const Eigen::MatrixBase<Derived> & B,
  110. MatC & C)
  111. {
  112. assert(dim == 1 || dim == 2);
  113. // Special case if B or A is empty
  114. if(A.size() == 0)
  115. {
  116. C = B;
  117. return;
  118. }
  119. if(B.size() == 0)
  120. {
  121. C = A;
  122. return;
  123. }
  124. if(dim == 1)
  125. {
  126. assert(A.cols() == B.cols());
  127. C.resize(A.rows()+B.rows(),A.cols());
  128. C << A,B;
  129. }else if(dim == 2)
  130. {
  131. assert(A.rows() == B.rows());
  132. C.resize(A.rows(),A.cols()+B.cols());
  133. C << A,B;
  134. }else
  135. {
  136. fprintf(stderr,"cat.h: Error: Unsupported dimension %d\n",dim);
  137. }
  138. }
  139. template <class Mat>
  140. IGL_INLINE Mat igl::cat(const int dim, const Mat & A, const Mat & B)
  141. {
  142. assert(dim == 1 || dim == 2);
  143. Mat C;
  144. igl::cat(dim,A,B,C);
  145. return C;
  146. }
  147. template <class Mat>
  148. IGL_INLINE void igl::cat(const std::vector<std::vector< Mat > > & A, Mat & C)
  149. {
  150. using namespace std;
  151. // Start with empty matrix
  152. C.resize(0,0);
  153. for(const auto & row_vec : A)
  154. {
  155. // Concatenate each row horizontally
  156. // Start with empty matrix
  157. Mat row(0,0);
  158. for(const auto & element : row_vec)
  159. {
  160. row = cat(2,row,element);
  161. }
  162. // Concatenate rows vertically
  163. C = cat(1,C,row);
  164. }
  165. }
  166. template <typename T, typename DerivedC>
  167. IGL_INLINE void igl::cat(const int dim, const std::vector<T> & A, Eigen::PlainObjectBase<DerivedC> & C)
  168. {
  169. assert(dim == 1 || dim == 2);
  170. using namespace Eigen;
  171. const int num_mat = A.size();
  172. if(num_mat == 0)
  173. {
  174. C.resize(0,0);
  175. return;
  176. }
  177. if(dim == 1)
  178. {
  179. const int A_cols = A[0].cols();
  180. int tot_rows = 0;
  181. for(const auto & m : A)
  182. {
  183. tot_rows += m.rows();
  184. }
  185. C.resize(tot_rows, A_cols);
  186. int cur_row = 0;
  187. for(int i = 0; i < num_mat; i++)
  188. {
  189. assert(A_cols == A[i].cols());
  190. C.block(cur_row,0,A[i].rows(),A_cols) = A[i];
  191. cur_row += A[i].rows();
  192. }
  193. }
  194. else if(dim == 2)
  195. {
  196. const int A_rows = A[0].rows();
  197. int tot_cols = 0;
  198. for(const auto & m : A)
  199. {
  200. tot_cols += m.cols();
  201. }
  202. C.resize(A_rows,tot_cols);
  203. int cur_col = 0;
  204. for(int i = 0; i < num_mat; i++)
  205. {
  206. assert(A_rows == A[i].rows());
  207. C.block(0,cur_col,A_rows,A[i].cols()) = A[i];
  208. cur_col += A[i].cols();
  209. }
  210. }
  211. else
  212. {
  213. fprintf(stderr,"cat.h: Error: Unsupported dimension %d\n",dim);
  214. }
  215. }
  216. #ifdef IGL_STATIC_LIBRARY
  217. // Explicit template instantiation
  218. // generated by autoexplicit.sh
  219. template void igl::cat<Eigen::Matrix<float, -1, -1, 0, -1, -1>, Eigen::Matrix<float, -1, -1, 0, -1, -1> >(int, std::vector<Eigen::Matrix<float, -1, -1, 0, -1, -1>, std::allocator<Eigen::Matrix<float, -1, -1, 0, -1, -1> > > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> >&);
  220. // generated by autoexplicit.sh
  221. template Eigen::Matrix<double, -1, -1, 0, -1, -1> igl::cat<Eigen::Matrix<double, -1, -1, 0, -1, -1> >(int, Eigen::Matrix<double, -1, -1, 0, -1, -1> const&, Eigen::Matrix<double, -1, -1, 0, -1, -1> const&);
  222. // generated by autoexplicit.sh
  223. template Eigen::SparseMatrix<double, 0, int> igl::cat<Eigen::SparseMatrix<double, 0, int> >(int, Eigen::SparseMatrix<double, 0, int> const&, Eigen::SparseMatrix<double, 0, int> const&);
  224. // generated by autoexplicit.sh
  225. template Eigen::Matrix<int, -1, -1, 0, -1, -1> igl::cat<Eigen::Matrix<int, -1, -1, 0, -1, -1> >(int, Eigen::Matrix<int, -1, -1, 0, -1, -1> const&, Eigen::Matrix<int, -1, -1, 0, -1, -1> const&);
  226. template void igl::cat<Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(int, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::Matrix<double, -1, 1, 0, -1, 1>&);
  227. template Eigen::Matrix<int, -1, 1, 0, -1, 1> igl::cat<Eigen::Matrix<int, -1, 1, 0, -1, 1> >(int, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&);
  228. template Eigen::Matrix<double, -1, 1, 0, -1, 1> igl::cat<Eigen::Matrix<double, -1, 1, 0, -1, 1> >(int, Eigen::Matrix<double, -1, 1, 0, -1, 1> const&, Eigen::Matrix<double, -1, 1, 0, -1, 1> const&);
  229. template void igl::cat<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(int, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<double, -1, -1, 0, -1, -1>&);
  230. template void igl::cat<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(int, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<int, -1, -1, 0, -1, -1>&);
  231. template void igl::cat<Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(int, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::Matrix<int, -1, 1, 0, -1, 1>&);
  232. template void igl::cat<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(int, std::vector<Eigen::Matrix<int, -1, -1, 0, -1, -1>, std::allocator<Eigen::Matrix<int, -1, -1, 0, -1, -1> > > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  233. template void igl::cat<Eigen::Matrix<int, 1, 4, 1, 1, 4>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(int, std::vector<Eigen::Matrix<int, 1, 4, 1, 1, 4>, std::allocator<Eigen::Matrix<int, 1, 4, 1, 1, 4> > > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  234. template void igl::cat<Eigen::Matrix<int, 1, 15, 1, 1, 15>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(int, std::vector<Eigen::Matrix<int, 1, 15, 1, 1, 15>, std::allocator<Eigen::Matrix<int, 1, 15, 1, 1, 15> > > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  235. template void igl::cat<Eigen::Matrix<int, 1, 2, 1, 1, 2>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(int, std::vector<Eigen::Matrix<int, 1, 2, 1, 1, 2>, std::allocator<Eigen::Matrix<int, 1, 2, 1, 1, 2> > > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  236. template void igl::cat<Eigen::Matrix<int, 1, 27, 1, 1, 27>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(int, std::vector<Eigen::Matrix<int, 1, 27, 1, 1, 27>, std::allocator<Eigen::Matrix<int, 1, 27, 1, 1, 27> > > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  237. template void igl::cat<Eigen::Matrix<int, 1, 3, 1, 1, 3>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(int, std::vector<Eigen::Matrix<int, 1, 3, 1, 1, 3>, std::allocator<Eigen::Matrix<int, 1, 3, 1, 1, 3> > > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  238. template void igl::cat<Eigen::Matrix<int, 3, 1, 0, 3, 1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(int, std::vector<Eigen::Matrix<int, 3, 1, 0, 3, 1>, std::allocator<Eigen::Matrix<int, 3, 1, 0, 3, 1> > > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  239. template void igl::cat<Eigen::Matrix<double, 1, 3, 1, 1, 3>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(int, std::vector<Eigen::Matrix<double, 1, 3, 1, 1, 3>, std::allocator<Eigen::Matrix<double, 1, 3, 1, 1, 3> > > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  240. template void igl::cat<Eigen::Matrix<double, 3, 1, 0, 3, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(int, std::vector<Eigen::Matrix<double, 3, 1, 0, 3, 1>, std::allocator<Eigen::Matrix<double, 3, 1, 0, 3, 1> > > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  241. template void igl::cat<Eigen::Matrix<int, 1, -1, 1, 1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(int, std::vector<Eigen::Matrix<int, 1, -1, 1, 1, -1>, std::allocator<Eigen::Matrix<int, 1, -1, 1, 1, -1> > > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  242. template void igl::cat<Eigen::Matrix<double, 1, 2, 1, 1, 2>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(int, std::vector<Eigen::Matrix<double, 1, 2, 1, 1, 2>, std::allocator<Eigen::Matrix<double, 1, 2, 1, 1, 2> > > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  243. #endif