max.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "max.h"
  2. #include "for_each.h"
  3. #include "find_zero.h"
  4. template <typename AType, typename DerivedB, typename DerivedI>
  5. IGL_INLINE void igl::max(
  6. const Eigen::SparseMatrix<AType> & A,
  7. const int dim,
  8. Eigen::PlainObjectBase<DerivedB> & B,
  9. Eigen::PlainObjectBase<DerivedI> & I)
  10. {
  11. const int n = A.cols();
  12. const int m = A.rows();
  13. B.resize(dim==1?n:m);
  14. B.setConstant(std::numeric_limits<typename DerivedB::Scalar>::lowest());
  15. I.resize(dim==1?n:m);
  16. for_each(A,[&B,&I,&dim](int i, int j,const typename DerivedB::Scalar v)
  17. {
  18. if(dim == 2)
  19. {
  20. std::swap(i,j);
  21. }
  22. // Coded as if dim == 1, assuming swap for dim == 2
  23. if(v > B(j))
  24. {
  25. B(j) = v;
  26. I(j) = i;
  27. }
  28. });
  29. Eigen::VectorXi Z;
  30. find_zero(A,dim,Z);
  31. for(int j = 0;j<I.size();j++)
  32. {
  33. if(Z(j) != (dim==1?m:n) && 0 > B(j))
  34. {
  35. B(j) = 0;
  36. I(j) = Z(j);
  37. }
  38. }
  39. }
  40. template <typename DerivedX, typename DerivedY, typename DerivedI>
  41. IGL_INLINE void igl::max(
  42. const Eigen::DenseBase<DerivedX> & X,
  43. const int dim,
  44. Eigen::PlainObjectBase<DerivedY> & Y,
  45. Eigen::PlainObjectBase<DerivedI> & I)
  46. {
  47. assert(dim==1||dim==2);
  48. // output size
  49. int n = (dim==1?X.cols():X.rows());
  50. // resize output
  51. Y.resize(n);
  52. I.resize(n);
  53. // loop over dimension opposite of dim
  54. for(int j = 0;j<n;j++)
  55. {
  56. typename DerivedX::Index PHONY,i;
  57. typename DerivedX::Scalar m;
  58. if(dim==1)
  59. {
  60. m = X.col(j).maxCoeff(&i,&PHONY);
  61. }else
  62. {
  63. m = X.row(j).maxCoeff(&PHONY,&i);
  64. }
  65. Y(j) = m;
  66. I(j) = i;
  67. }
  68. }
  69. #ifdef IGL_STATIC_LIBRARY
  70. // Explicit template instantiation
  71. template void igl::max<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::DenseBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  72. template void igl::max<bool, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::SparseMatrix<bool, 0, int> const&, int, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  73. #endif