round.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 "round.h"
  9. #include <cmath>
  10. // http://stackoverflow.com/a/485549
  11. template <typename DerivedX >
  12. IGL_INLINE DerivedX igl::round(const DerivedX r)
  13. {
  14. return (r > 0.0) ? std::floor(r + 0.5) : std::ceil(r - 0.5);
  15. }
  16. template < typename DerivedX, typename DerivedY>
  17. IGL_INLINE void igl::round(
  18. const Eigen::PlainObjectBase<DerivedX>& X,
  19. Eigen::PlainObjectBase<DerivedY>& Y)
  20. {
  21. Y.resize(X.rows(),X.cols());
  22. // loop over rows
  23. for(int i = 0;i<X.rows();i++)
  24. {
  25. // loop over cols
  26. for(int j = 0;j<X.cols();j++)
  27. {
  28. Y(i,j) = igl::round(X(i,j));
  29. }
  30. }
  31. }
  32. #ifdef IGL_STATIC_LIBRARY
  33. // Explicit instanciation
  34. 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> >&);
  35. 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> >&);
  36. template void igl::round<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, 3, 0, -1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&);
  37. #endif