| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- // This file is part of libigl, a simple c++ geometry processing library.
- //
- // Copyright (C) 2013 Alec Jacobson <[email protected]>
- //
- // This Source Code Form is subject to the terms of the Mozilla Public License
- // v. 2.0. If a copy of the MPL was not distributed with this file, You can
- // obtain one at http://mozilla.org/MPL/2.0/.
- #include "slice_into.h"
- #include "IGL_ASSERT.h"
- #include "colon.h"
- #include "sort.h"
- // Bug in unsupported/Eigen/SparseExtra needs iostream first
- #include <iostream>
- #include <unsupported/Eigen/SparseExtra>
- template <typename T, typename DerivedR, typename DerivedC>
- IGL_INLINE void igl::slice_into(
- const Eigen::SparseMatrix<T>& X,
- const Eigen::MatrixBase<DerivedR> & R,
- const Eigen::MatrixBase<DerivedC> & C,
- Eigen::SparseMatrix<T>& Y)
- {
- const int xm = X.rows();
- const int xn = X.cols();
- IGL_ASSERT(R.size() == xm);
- IGL_ASSERT(C.size() == xn);
- const int ym = Y.rows();
- const int yn = Y.cols();
- IGL_ASSERT(R.minCoeff() >= 0);
- IGL_ASSERT(R.maxCoeff() < ym);
- IGL_ASSERT(C.minCoeff() >= 0);
- IGL_ASSERT(C.maxCoeff() < yn);
- std::vector<bool> in_R(Y.rows());
- for(int r = 0;r<R.size();r++) { in_R[R(r)] = true; }
- // Rebuild each column in C
- for(int c = 0;c<C.size();c++)
- {
- int k = C(c);
- Eigen::SparseVector<T> Yk = Y.col(k);
- // implicit zeros
- for(typename Eigen::SparseMatrix<T>::InnerIterator yit(Y, k);yit;++yit)
- {
- if(in_R[yit.row()]){ Yk.coeffRef(yit.row()) = 0; }
- }
- // explicit values
- for(typename Eigen::SparseMatrix<T>::InnerIterator xit(X, c);xit;++xit)
- {
- // row in X
- int r = xit.row();
- // row in Y
- int s = R(r);
- Yk.coeffRef(s) = xit.value();
- }
- Y.col(k) = Yk;
- }
- }
- template <typename DerivedX, typename DerivedY, typename DerivedR, typename DerivedC>
- IGL_INLINE void igl::slice_into(
- const Eigen::MatrixBase<DerivedX> & X,
- const Eigen::MatrixBase<DerivedR> & R,
- const Eigen::MatrixBase<DerivedC> & C,
- Eigen::PlainObjectBase<DerivedY> & Y)
- {
- int xm = X.rows();
- int xn = X.cols();
- IGL_ASSERT(R.size() == xm);
- IGL_ASSERT(C.size() == xn);
- const int ym = Y.rows();
- const int yn = Y.cols();
- IGL_ASSERT(R.minCoeff() >= 0);
- IGL_ASSERT(R.maxCoeff() < ym);
- IGL_ASSERT(C.minCoeff() >= 0);
- IGL_ASSERT(C.maxCoeff() < yn);
- // Build reindexing maps for columns and rows, -1 means not in map
- Eigen::Matrix<typename DerivedR::Scalar,Eigen::Dynamic,1> RI;
- RI.resize(xm);
- for(int i = 0;i<xm;i++)
- {
- for(int j = 0;j<xn;j++)
- {
- Y(int(R(i)),int(C(j))) = X(i,j);
- }
- }
- }
- template <typename MatX, typename MatY, typename DerivedR>
- IGL_INLINE void igl::slice_into(
- const MatX & X,
- const Eigen::MatrixBase<DerivedR> & R,
- const int dim,
- MatY& Y)
- {
- Eigen::Matrix<typename MatX::Scalar, Eigen::Dynamic, 1> C;
- switch(dim)
- {
- case 1:
- IGL_ASSERT(R.size() == X.rows());
- // boring base case
- if(X.cols() == 0)
- {
- return;
- }
- igl::colon(0,X.cols()-1,C);
- return slice_into(X,R,C,Y);
- case 2:
- IGL_ASSERT(R.size() == X.cols());
- // boring base case
- if(X.rows() == 0)
- {
- return;
- }
- igl::colon(0,X.rows()-1,C);
- return slice_into(X,C,R,Y);
- default:
- IGL_ASSERT(false && "Unsupported dimension");
- return;
- }
- }
- template <typename DerivedX, typename DerivedR, typename DerivedY>
- IGL_INLINE void igl::slice_into(
- const Eigen::MatrixBase<DerivedX> & X,
- const Eigen::MatrixBase<DerivedR> & R,
- Eigen::PlainObjectBase<DerivedY> & Y)
- {
- // phony column indices
- Eigen::Matrix<typename DerivedR::Scalar, Eigen::Dynamic, 1> C;
- C.resize(1);
- C(0) = 0;
- return igl::slice_into(X,R,C,Y);
- }
- #ifdef IGL_STATIC_LIBRARY
- // Explicit template instantiation
- // generated by autoexplicit.sh
- template void igl::slice_into<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> const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, int, Eigen::Matrix<int, -1, 1, 0, -1, 1>&);
- // generated by autoexplicit.sh
- template void igl::slice_into<double, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::SparseMatrix<double, 0, int> const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::SparseMatrix<double, 0, int>&);
- template void igl::slice_into<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::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
- template void igl::slice_into<Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
- template void igl::slice_into<double, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::SparseMatrix<double, 0, int> const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::SparseMatrix<double, 0, int>&);
- template void igl::slice_into<Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::Matrix<double, -1, 1, 0, -1, 1> const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
- template void igl::slice_into<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::Matrix<double, -1, -1, 0, -1, -1> const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
- template void igl::slice_into<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::Matrix<double, -1, -1, 0, -1, -1> const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, int, Eigen::Matrix<double, -1, -1, 0, -1, -1>&);
- template void igl::slice_into<Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::Matrix<double, -1, 1, 0, -1, 1> const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, int, Eigen::Matrix<double, -1, 1, 0, -1, 1>&);
- template void igl::slice_into<Eigen::SparseMatrix<double, 0, int>, Eigen::SparseMatrix<double, 0, int>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::SparseMatrix<double, 0, int> const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, int, Eigen::SparseMatrix<double, 0, int>&);
- template void igl::slice_into<Eigen::Matrix<double, -1, -1, 0, -1, -1>, 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::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
- template void igl::slice_into<Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::MatrixWrapper<Eigen::Array<int, -1, 1, 0, -1, 1> > >(Eigen::Matrix<double, -1, 1, 0, -1, 1> const&, Eigen::MatrixBase<Eigen::MatrixWrapper<Eigen::Array<int, -1, 1, 0, -1, 1> > > const&, int, Eigen::Matrix<double, -1, 1, 0, -1, 1>&);
- #endif
|