randperm.cpp 1.0 KB

123456789101112131415161718192021222324252627282930
  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 "randperm.h"
  9. #include "colon.h"
  10. #include <algorithm>
  11. #include <random>
  12. template <typename DerivedI>
  13. IGL_INLINE void igl::randperm(
  14. const int n,
  15. Eigen::PlainObjectBase<DerivedI> & I)
  16. {
  17. Eigen::VectorXi II;
  18. igl::colon(0,1,n-1,II);
  19. I = II;
  20. std::random_device rd;
  21. std::mt19937 mt(rd());
  22. std::random(I.data(),I.data()+n, mt);
  23. }
  24. #ifdef IGL_STATIC_LIBRARY
  25. // Explicit template instantiation
  26. template void igl::randperm<Eigen::Matrix<int, -1, 1, 0, -1, 1> >(int, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  27. template void igl::randperm<Eigen::Matrix<int, -1, -1, 0, -1, -1> >(int, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  28. #endif