cat.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 "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. SparseMatrix<Scalar, RowMajor> dyn_C;
  37. if(dim == 1)
  38. {
  39. assert(A.cols() == B.cols());
  40. dyn_C.resize(A.rows()+B.rows(),A.cols());
  41. }else if(dim == 2)
  42. {
  43. assert(A.rows() == B.rows());
  44. dyn_C.resize(A.rows(),A.cols()+B.cols());
  45. }else
  46. {
  47. fprintf(stderr,"cat.h: Error: Unsupported dimension %d\n",dim);
  48. }
  49. dyn_C.reserve(A.nonZeros()+B.nonZeros());
  50. // Iterate over outside of A
  51. for(int k=0; k<A.outerSize(); ++k)
  52. {
  53. // Iterate over inside
  54. for(typename SparseMatrix<Scalar>::InnerIterator it (A,k); it; ++it)
  55. {
  56. dyn_C.coeffRef(it.row(),it.col()) += it.value();
  57. }
  58. }
  59. // Iterate over outside of B
  60. for(int k=0; k<B.outerSize(); ++k)
  61. {
  62. // Iterate over inside
  63. for(typename SparseMatrix<Scalar>::InnerIterator it (B,k); it; ++it)
  64. {
  65. int r = (dim == 1 ? A.rows()+it.row() : it.row());
  66. int c = (dim == 2 ? A.cols()+it.col() : it.col());
  67. dyn_C.coeffRef(r,c) += it.value();
  68. }
  69. }
  70. C = SparseMatrix<Scalar>(dyn_C);
  71. }
  72. template <typename Derived, class MatC>
  73. IGL_INLINE void igl::cat(
  74. const int dim,
  75. const Eigen::MatrixBase<Derived> & A,
  76. const Eigen::MatrixBase<Derived> & B,
  77. MatC & C)
  78. {
  79. assert(dim == 1 || dim == 2);
  80. // Special case if B or A is empty
  81. if(A.size() == 0)
  82. {
  83. C = B;
  84. return;
  85. }
  86. if(B.size() == 0)
  87. {
  88. C = A;
  89. return;
  90. }
  91. if(dim == 1)
  92. {
  93. assert(A.cols() == B.cols());
  94. C.resize(A.rows()+B.rows(),A.cols());
  95. C << A,B;
  96. }else if(dim == 2)
  97. {
  98. assert(A.rows() == B.rows());
  99. C.resize(A.rows(),A.cols()+B.cols());
  100. C << A,B;
  101. }else
  102. {
  103. fprintf(stderr,"cat.h: Error: Unsupported dimension %d\n",dim);
  104. }
  105. }
  106. template <class Mat>
  107. IGL_INLINE Mat igl::cat(const int dim, const Mat & A, const Mat & B)
  108. {
  109. assert(dim == 1 || dim == 2);
  110. Mat C;
  111. igl::cat(dim,A,B,C);
  112. return C;
  113. }
  114. template <class Mat>
  115. IGL_INLINE void igl::cat(const std::vector<std::vector< Mat > > & A, Mat & C)
  116. {
  117. using namespace std;
  118. // Start with empty matrix
  119. C.resize(0,0);
  120. for(typename vector<vector< Mat > >::const_iterator rit = A.begin(); rit != A.end(); rit++)
  121. {
  122. // Concatenate each row horizontally
  123. // Start with empty matrix
  124. Mat row(0,0);
  125. for(typename vector<vector< Mat > >::iterator cit = A.begin(); rit != A.end(); rit++)
  126. {
  127. row = cat(2,row,*cit);
  128. }
  129. // Concatenate rows vertically
  130. C = cat(1,C,row);
  131. }
  132. }
  133. #ifdef IGL_STATIC_LIBRARY
  134. // Explicit template specialization
  135. // generated by autoexplicit.sh
  136. 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&);
  137. // generated by autoexplicit.sh
  138. 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&);
  139. // generated by autoexplicit.sh
  140. 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&);
  141. 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>&);
  142. 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&);
  143. 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&);
  144. #endif