round.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. #include "round.h"
  2. #include <cmath>
  3. // http://stackoverflow.com/a/485549
  4. template <typename DerivedX >
  5. IGL_INLINE DerivedX igl::round(const DerivedX r)
  6. {
  7. return (r > 0.0) ? floor(r + 0.5) : ceil(r - 0.5);
  8. }
  9. template < typename DerivedX, typename DerivedY>
  10. IGL_INLINE void igl::round(
  11. const Eigen::PlainObjectBase<DerivedX>& X,
  12. Eigen::PlainObjectBase<DerivedY>& Y)
  13. {
  14. Y.resize(X.rows(),X.cols());
  15. // loop over rows
  16. for(int i = 0;i<X.rows();i++)
  17. {
  18. // loop over cols
  19. for(int j = 0;j<X.cols();j++)
  20. {
  21. Y(i,j) = igl::round(X(i,j));
  22. }
  23. }
  24. }
  25. #ifndef IGL_HEADER_ONLY
  26. // Explicit instanciation
  27. template void igl::round<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  28. template void igl::round<Eigen::Matrix<double, -1, 2, 0, -1, 2>, Eigen::Matrix<double, -1, 2, 0, -1, 2> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 2, 0, -1, 2> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 2, 0, -1, 2> >&);
  29. #endif