random_quaternion.cpp 738 B

123456789101112131415161718192021222324
  1. #include "random_quaternion.h"
  2. template <typename Scalar>
  3. IGL_INLINE Eigen::Quaternion<Scalar> igl::random_quaternion()
  4. {
  5. // http://mathproofs.blogspot.com/2005/05/uniformly-distributed-random-unit.html
  6. const auto & unit_rand = []()->Scalar
  7. {
  8. return ((Scalar)rand() / (Scalar)RAND_MAX);
  9. };
  10. const Scalar t0 = 2.*M_PI*unit_rand();
  11. const Scalar t1 = acos(1.-2.*unit_rand());
  12. const Scalar t2 = 0.5*(M_PI*unit_rand() + acos(unit_rand()));
  13. return Eigen::Quaternion<Scalar>(
  14. 1.*sin(t0)*sin(t1)*sin(t2),
  15. 1.*cos(t0)*sin(t1)*sin(t2),
  16. 1.*cos(t1)*sin(t2),
  17. 1.*cos(t2));
  18. }
  19. #ifdef IGL_STATIC_LIBRARY
  20. // Explicit template specialization
  21. template Eigen::Quaternion<double, 0> igl::random_quaternion<double>();
  22. #endif