You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

156 lines
7.6 KiB

// This file is part of libigl, a simple c++ geometry processing library.
//
// Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
//
// 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 "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();
assert(R.size() == xm);
assert(C.size() == xn);
const int ym = Y.rows();
const int yn = Y.cols();
assert(R.minCoeff() >= 0);
assert(R.maxCoeff() < ym);
assert(C.minCoeff() >= 0);
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();
assert(R.size() == xm);
assert(C.size() == xn);
const int ym = Y.rows();
const int yn = Y.cols();
assert(R.minCoeff() >= 0);
assert(R.maxCoeff() < ym);
assert(C.minCoeff() >= 0);
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:
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:
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:
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