Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
random.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#pragma once
9
10#include <momentum/math/types.h>
11
12#include <random>
13
14namespace momentum {
15
32template <typename Generator_ = std::mt19937>
33class Random final {
34 public:
35 using Generator = Generator_;
36
41 // TODO: Document or enforce the seeding policy of the singleton (currently seeded once via
42 // std::random_device on first call, which makes test reproducibility difficult without an
43 // explicit setSeed()).
44 [[nodiscard]] static Random& GetSingleton();
45
50 explicit Random(uint32_t seed = std::random_device{}());
51
86 template <typename T>
87 [[nodiscard]] T uniform(const T& min, const T& max);
88
103 // TODO: The original example claimed `([0, 1], [0, 1))` which is inconsistent — both elements
104 // share the same distribution and same bounds.
105 template <typename FixedSizeT>
106 [[nodiscard]] FixedSizeT uniform(
107 typename FixedSizeT::Scalar min,
108 typename FixedSizeT::Scalar max);
109
127 template <typename DynamicVector>
128 [[nodiscard]] DynamicVector uniform(
129 Eigen::Index size,
130 typename DynamicVector::Scalar min,
131 typename DynamicVector::Scalar max);
132
151 template <typename DynamicMatrix>
152 [[nodiscard]] DynamicMatrix uniform(
153 Eigen::Index rows,
154 Eigen::Index cols,
155 typename DynamicMatrix::Scalar min,
156 typename DynamicMatrix::Scalar max);
157
159 template <typename T>
160 [[nodiscard]] Quaternion<T> uniformQuaternion();
161
163 template <typename T>
164 [[nodiscard]] Matrix3<T> uniformRotationMatrix();
165
173 template <typename T>
174 [[nodiscard]] Isometry3<T> uniformIsometry3(
175 const Vector3<T>& min = Vector3<T>::Zero(),
176 const Vector3<T>& max = Vector3<T>::Ones());
177
190 template <typename T>
191 [[nodiscard]] Affine3<T> uniformAffine3(
192 T scaleMin = T(0.1),
193 T scaleMax = T(10.0),
194 const Vector3<T>& min = Vector3<T>::Zero(),
195 const Vector3<T>& max = Vector3<T>::Ones());
196
207 template <typename T>
208 [[nodiscard]] T normal(const T& mean, const T& sigma);
209
218 template <typename FixedSizeT>
219 [[nodiscard]] FixedSizeT normal(
220 typename FixedSizeT::Scalar mean,
221 typename FixedSizeT::Scalar sigma);
222
231 template <typename DynamicVector>
232 [[nodiscard]] DynamicVector normal(
233 Eigen::Index size,
234 typename DynamicVector::Scalar mean,
235 typename DynamicVector::Scalar sigma);
236
246 template <typename DynamicMatrix>
247 [[nodiscard]] DynamicMatrix normal(
248 Eigen::Index rows,
249 Eigen::Index cols,
250 typename DynamicMatrix::Scalar mean,
251 typename DynamicMatrix::Scalar sigma);
252
258 [[nodiscard]] uint32_t getSeed() const;
259
262 void setSeed(uint32_t seed);
263
264 private:
265 uint32_t seed_;
266
267 Generator generator_;
268};
269
275
277template <typename T>
278[[nodiscard]] T uniform(const T& min, const T& max);
279
282template <typename FixedSizeT>
283[[nodiscard]] FixedSizeT uniform(typename FixedSizeT::Scalar min, typename FixedSizeT::Scalar max);
284
287template <typename DynamicVector>
288[[nodiscard]] DynamicVector
289uniform(Eigen::Index size, typename DynamicVector::Scalar min, typename DynamicVector::Scalar max);
290
293template <typename DynamicMatrix>
294[[nodiscard]] DynamicMatrix uniform(
295 Eigen::Index rows,
296 Eigen::Index cols,
297 typename DynamicMatrix::Scalar min,
298 typename DynamicMatrix::Scalar max);
299
301template <typename T>
302[[nodiscard]] Quaternion<T> uniformQuaternion();
303
305template <typename T>
306[[nodiscard]] Matrix3<T> uniformRotationMatrix();
307
310template <typename T>
311[[nodiscard]] Isometry3<T> uniformIsometry3(
312 const Vector3<T>& min = Vector3<T>::Zero(),
313 const Vector3<T>& max = Vector3<T>::Ones());
314
318template <typename T>
319[[nodiscard]] Affine3<T> uniformAffine3(
320 T scaleMin = T(0.1),
321 T scaleMax = T(10.0),
322 const Vector3<T>& min = Vector3<T>::Zero(),
323 const Vector3<T>& max = Vector3<T>::Ones());
324
327template <typename T>
328[[nodiscard]] T normal(const T& mean, const T& sigma);
329
332template <typename FixedSizeT>
333[[nodiscard]] FixedSizeT normal(
334 typename FixedSizeT::Scalar mean,
335 typename FixedSizeT::Scalar sigma);
336
339template <typename DynamicVector>
340[[nodiscard]] DynamicVector normal(
341 Eigen::Index size,
342 typename DynamicVector::Scalar mean,
343 typename DynamicVector::Scalar sigma);
344
347template <typename DynamicMatrix>
348[[nodiscard]] DynamicMatrix normal(
349 Eigen::Index rows,
350 Eigen::Index cols,
351 typename DynamicMatrix::Scalar mean,
352 typename DynamicMatrix::Scalar sigma);
353
354} // namespace momentum
355
static Random & GetSingleton()
Returns the process-wide singleton instance.
Definition random-inl.h:186
Affine3< T > uniformAffine3(T scaleMin=T(0.1), T scaleMax=T(10.0), const Vector3< T > &min=Vector3< T >::Zero(), const Vector3< T > &max=Vector3< T >::Ones())
Generates a random affine transformation with rotation drawn uniformly from SO(3),...
Definition random-inl.h:255
Quaternion< T > uniformQuaternion()
Generates a random unit quaternion uniformly distributed on SO(3) (Haar measure).
Definition random-inl.h:234
Matrix3< T > uniformRotationMatrix()
Generates a random rotation matrix uniformly distributed on SO(3) (Haar measure).
Definition random-inl.h:240
T normal(const T &mean, const T &sigma)
Generates a value from a normal (Gaussian) distribution N(mean, sigma^2).
Definition random-inl.h:274
Random(uint32_t seed=std::random_device{}())
Constructs a generator seeded with the given value.
Definition random-inl.h:192
void setSeed(uint32_t seed)
Re-seeds the internal engine, resetting its state to the deterministic sequence implied by seed.
Definition random-inl.h:316
uint32_t getSeed() const
Returns the seed last used to initialize the engine.
Definition random-inl.h:311
Generator_ Generator
Definition random.h:35
Isometry3< T > uniformIsometry3(const Vector3< T > &min=Vector3< T >::Zero(), const Vector3< T > &max=Vector3< T >::Ones())
Generates a random isometry (rigid transformation) on SE(3) with rotation drawn uniformly from SO(3) ...
Definition random-inl.h:246
T uniform(const T &min, const T &max)
Generates a random scalar/vector/matrix from the uniform distribution.
Definition random-inl.h:196
T normal(const T &mean, const T &sigma)
Normally distributed value via the global Random singleton.
Definition random-inl.h:382
Matrix3< T > uniformRotationMatrix()
Uniformly distributed rotation matrix on SO(3) via the global Random singleton.
Definition random-inl.h:361
T uniform(const T &min, const T &max)
Convenience wrappers that forward to Random<>::GetSingleton().
Definition random-inl.h:326
Eigen::Matrix< T, 3, 3 > Matrix3
Definition types.h:96
Isometry3< T > uniformIsometry3(const Vector3< T > &min, const Vector3< T > &max)
Random isometry on SE(3) via the global Random singleton (uniform rotation, uniform translation in [m...
Definition random-inl.h:366
Eigen::Quaternion< T > Quaternion
Unit quaternion representing a 3D rotation.
Definition types.h:385
Eigen::Transform< T, 3, Eigen::Isometry > Isometry3
3D rigid transform (rotation + translation, no scale or shear).
Definition types.h:397
Quaternion< T > uniformQuaternion()
Uniformly distributed unit quaternion on SO(3) via the global Random singleton.
Definition random-inl.h:355
Vector< T, 3 > Vector3
Definition types.h:236
Affine3< T > uniformAffine3(T scaleMin, T scaleMax, const Vector3< T > &min, const Vector3< T > &max)
Random affine transformation via the global Random singleton (uniform rotation, uniform scale in [sca...
Definition random-inl.h:372
Eigen::Transform< T, 3, Eigen::Affine > Affine3
3D affine transform (linear part + translation).
Definition types.h:404