Aikido
RNG-impl.hpp
Go to the documentation of this file.
1 #include <cassert>
2 
3 namespace aikido {
4 namespace common {
5 
6 //==============================================================================
7 constexpr auto RNG::min() -> result_type
8 {
9  return 0u;
10 }
11 
12 //==============================================================================
13 constexpr auto RNG::max() -> result_type
14 {
15  return (static_cast<std::uint64_t>(1) << NUM_BITS) - 1;
16 }
17 
18 //==============================================================================
19 template <class T>
20 RNGWrapper<T>::RNGWrapper(const T& _rng) : mRng(_rng)
21 {
22 }
23 
24 //==============================================================================
25 template <class T>
27 {
28 }
29 
30 //==============================================================================
31 template <class T>
32 auto RNGWrapper<T>::rng() -> T&
33 {
34  // This is potentially dangerous, but is necessary to implement the current
35  // API. We should deprecate this function in the future.
36  return const_cast<T&>(mRng.base());
37 }
38 
39 //==============================================================================
40 template <class T>
41 auto RNGWrapper<T>::rng() const -> const T&
42 {
43  return mRng.base();
44 }
45 
46 //==============================================================================
47 template <class T>
49 {
50  return mRng();
51 }
52 
53 //==============================================================================
54 template <class T>
55 void RNGWrapper<T>::discard(unsigned long long _z)
56 {
57  mRng.discard(_z);
58 }
59 
60 //==============================================================================
61 template <class T>
62 std::unique_ptr<RNG> RNGWrapper<T>::clone() const
63 {
64  return std::unique_ptr<RNGWrapper>(new RNGWrapper(mRng.base()));
65 }
66 
67 //==============================================================================
68 template <class T>
69 std::unique_ptr<RNG> RNGWrapper<T>::clone(result_type _seed) const
70 {
71  return std::unique_ptr<RNGWrapper>(new RNGWrapper(_seed));
72 }
73 
74 //==============================================================================
75 template <class Engine, class Scalar, class Quaternion>
76 Quaternion sampleQuaternion(
77  Engine& _engine, std::uniform_real_distribution<Scalar>& _distribution)
78 {
79  assert(_distribution.a() == 0.);
80  assert(_distribution.b() == 1.);
81 
82  const double u1 = _distribution(_engine);
83  const double u2 = _distribution(_engine);
84  const double u3 = _distribution(_engine);
85 
86  return Quaternion(
87  std::sqrt(1. - u1) * std::sin(2. * M_PI * u2),
88  std::sqrt(1. - u1) * std::cos(2. * M_PI * u2),
89  std::sqrt(u1) * std::sin(2. * M_PI * u3),
90  std::sqrt(u1) * std::cos(2. * M_PI * u3));
91 }
92 
93 } // namespace common
94 } // namespace aikido
aikido::common::RNGWrapper::clone
std::unique_ptr< RNG > clone() const override
Create a copy of this RNG, including its internal state.
Definition: RNG-impl.hpp:62
aikido::common::RNGWrapper::RNGWrapper
RNGWrapper()=default
Constructs a random engine with a default seed.
aikido::common::RNG::max
static constexpr result_type max()
Gets the largest possible value in the output range, 2^NUM_BITS - 1.
Definition: RNG-impl.hpp:13
aikido
Format of serialized trajectory in YAML.
Definition: algorithm.hpp:4
aikido::common::RNGWrapper
Concrete implementation of the RNG type erasure class.
Definition: RNG.hpp:72
aikido::common::RNG::min
static constexpr result_type min()
Gets the smallest possible value in the output range, always zero.
Definition: RNG-impl.hpp:7
aikido::common::RNGWrapper::operator()
result_type operator()() override
Advances the state of the engine and returns the generated value.
Definition: RNG-impl.hpp:48
aikido::common::RNGWrapper::discard
void discard(unsigned long long _z) override
Advances the adaptor's state by a specified amount.
Definition: RNG-impl.hpp:55
aikido::common::RNGWrapper::rng
T & rng()
Gets the internal random engine.
Definition: RNG-impl.hpp:32
aikido::common::sampleQuaternion
Quaternion sampleQuaternion(Engine &_engine, std::uniform_real_distribution< Scalar > &_distribution)
Sample a unit quaternion uniformly at random.
Definition: RNG-impl.hpp:76
aikido::common::RNG::result_type
std::uint32_t result_type
Definition: RNG.hpp:27