Ocean
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#ifndef META_OCEAN_MATH_RANDOM_H
9#define META_OCEAN_MATH_RANDOM_H
10
11#include "ocean/math/Math.h"
12
13#include "ocean/math/Euler.h"
15#include "ocean/math/Numeric.h"
17#include "ocean/math/Rotation.h"
19#include "ocean/math/Vector2.h"
20#include "ocean/math/Vector3.h"
21#include "ocean/math/Vector4.h"
22
24#include "ocean/base/RandomI.h"
25
26namespace Ocean
27{
28
29template <typename T> class RandomT;
30
31/**
32 * Definition of the Random object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with single or double precision float data type.
33 * @see RandomT
34 * @ingroup math
35 */
37
38/**
39 * Instantiation of the RandomT template class using a double precision float data type.
40 * @see RandomT
41 * @ingroup math
42 */
44
45/**
46 * Instantiation of the RandomT template class using a single precision float data type.
47 * @see RandomT
48 * @ingroup math
49 */
51
52/**
53 * This class provides several random functions for different data types using a floating point type for its elements that is specified by T.
54 * Beware: All function must not be used without a initialize() call for each thread!
55 * @tparam T Data type of passed and generated values
56 * @see Random, RandomF, RandomD.
57 * @ingroup math
58 */
59 template <typename T>
60class RandomT : public RandomI
61{
62 public:
63
64 /**
65 * Returns a random sign (either +1 or -1).
66 * @return The random sign
67 */
68 static inline T sign();
69
70 /**
71 * Returns a random sign (either +1 or -1).
72 * @param randomGenerator The random generator to be used
73 * @return The random sign
74 */
75 static inline T sign(RandomGenerator& randomGenerator);
76
77 /**
78 * Returns a random number between two borders.
79 * @param lower The lower border, with range (-infinity, infinity)
80 * @param upper The upper border, with range [lower, infinity)
81 * @return Random number, with range [lower, upper]
82 */
83 static T scalar(const T lower, const T upper);
84
85 /**
86 * Returns a random number between two borders using an explicit random generator.
87 * @param randomGenerator The random generator to be used
88 * @param lower The lower border, with range (-infinity, infinity)
89 * @param upper The upper border, with range [lower, infinity)
90 * @return Random number, with range [lower, upper]
91 */
92 static T scalar(RandomGenerator& randomGenerator, const T lower, const T upper);
93
94 /**
95 * Returns a random number with Gaussian distribution.
96 * The returned value lies inside the range [-5 * sigma, 5 * sigma].
97 * @param sigma The sigma parameter defining the standard deviation of the Gaussian distribution, with range (0, infinity)
98 * @return Gaussian distributed random value
99 */
100 static T gaussianNoise(const T sigma);
101
102 /**
103 * Returns a random number with Gaussian distribution using an explicit random generator.
104 * The returned value lies inside the range [-5 * sigma, 5 * sigma].
105 * @param randomGenerator The random generator to be used
106 * @param sigma The sigma parameter defining the standard deviation of the Gaussian distribution, with range (0, infinity)
107 * @return Gaussian distributed random value
108 */
109 static T gaussianNoise(RandomGenerator& randomGenerator, const T sigma);
110
111 /**
112 * Returns a random 2D vector with Gaussian distribution.
113 * The returned value lies inside the range [-5 * sigma, 5 * sigma]x[-5 * sigma, 5 * sigma].
114 * @param sigmaX The sigma parameter defining the standard deviation of the Gaussian distribution for the x value, with range (0, infinity)
115 * @param sigmaY The sigma parameter defining the standard deviation of the Gaussian distribution for the y value, with range (0, infinity)
116 * @return Gaussian distributed random 2D vector
117 */
118 static VectorT2<T> gaussianNoiseVector2(const T sigmaX, const T sigmaY);
119
120 /**
121 * Returns a random 2D vector with Gaussian distribution using an explicit random generator.
122 * The returned value lies inside the range [-5 * sigma, 5 * sigma]x[-5 * sigma, 5 * sigma].
123 * @param randomGenerator The random generator to be used
124 * @param sigmaX The sigma parameter defining the standard deviation of the Gaussian distribution for the x value, with range (0, infinity)
125 * @param sigmaY The sigma parameter defining the standard deviation of the Gaussian distribution for the y value, with range (0, infinity)
126 * @return Gaussian distributed random 2D vector
127 */
128 static VectorT2<T> gaussianNoiseVector2(RandomGenerator& randomGenerator, const T sigmaX, const T sigmaY);
129
130 /**
131 * Returns a random 2D vector with length 1 which is equal distributed within a circle.
132 * @return The resulting random 2D vector, with vector2().length() == 1
133 */
134 static VectorT2<T> vector2();
135
136 /**
137 * Returns a random 2D vector with length 1 which is equal distributed within a circle using an explicit random generator.
138 * @param randomGenerator The random generator to be used
139 * @return The resulting random 2D vector, with vector2().length() == 1
140 */
141 static VectorT2<T> vector2(RandomGenerator& randomGenerator);
142
143 /**
144 * Returns a random 2D vector with coordinates in a given range.
145 * @param min Minimum coordinate value for each axis, with range (-infinity, infinity)
146 * @param max Maximum coordinate value for each axis, with range [min, infinity)
147 * @return Random 2D vector with random length
148 */
149 static VectorT2<T> vector2(const T min, const T max);
150
151 /**
152 * Returns a random 2D vector with coordinates in a given range using an explicit random generator.
153 * @param randomGenerator The random generator to be used
154 * @param min Minimum coordinate value for each axis, with range (-infinity, infinity)
155 * @param max Maximum coordinate value for each axis, with range [min, infinity)
156 * @return Random 2D vector with random length
157 */
158 static VectorT2<T> vector2(RandomGenerator& randomGenerator, const T min, const T max);
159
160 /**
161 * Returns a random 2D vector with coordinates in a given range.
162 * @param xMin Minimum x coordinate value for each axis, with range (-infinity, infinity)
163 * @param xMax Maximum x coordinate value for each axis, with range [xMin, infinity)
164 * @param yMin Minimum y coordinate value for each axis, with range (-infinity, infinity)
165 * @param yMax Maximum x coordinate value for each axis, with range [yMin, infinity)
166 * @return Random 2D vector with random length
167 */
168 static VectorT2<T> vector2(const T xMin, const T xMax, const T yMin, const T yMax);
169
170 /**
171 * Returns a random 2D vector with coordinates in a given range using an explicit random generator.
172 * @param randomGenerator The random generator to be used
173 * @param xMin Minimum x coordinate value for each axis, with range (-infinity, infinity)
174 * @param xMax Maximum x coordinate value for each axis, with range [xMin, infinity)
175 * @param yMin Minimum y coordinate value for each axis, with range (-infinity, infinity)
176 * @param yMax Maximum x coordinate value for each axis, with range [yMin, infinity)
177 * @return Random 2D vector with random length
178 */
179 static VectorT2<T> vector2(RandomGenerator& randomGenerator, const T xMin, const T xMax, const T yMin, const T yMax);
180
181 /**
182 * Returns a random 3D vector with length 1 which is equal distributed within a sphere.
183 * @return The resulting random 3D vector, with vector3().length() == 1
184 */
185 static VectorT3<T> vector3();
186
187 /**
188 * Returns a random 3D vector with length 1 which is equal distributed within a sphere using an explicit random generator.
189 * @param randomGenerator The random generator to be used
190 * @return The resulting random 3D vector, with vector3().length() == 1
191 */
192 static VectorT3<T> vector3(RandomGenerator& randomGenerator);
193
194 /**
195 * Returns a random 3D vector with coordinates in a given range.
196 * @param min Minimum coordinate value for each axis, with range (-infinity, infinity)
197 * @param max Maximum coordinate value for each axis, with range [min, infinity)
198 * @return Random 3D vector with random length
199 */
200 static VectorT3<T> vector3(const T min, const T max);
201
202 /**
203 * Returns a random 3D vector with coordinates in a given range.
204 * @param range 3D vector defining the +/- ranges separately for each axis, with range [0, infinity] x [0, infinity] x [0, infinity]
205 * @return Random 3D vector with random length
206 */
207 static VectorT3<T> vector3(const VectorT3<T>& range);
208
209 /**
210 * Returns a random 3D vector with coordinates in a given range using an explicit random generator.
211 * @param randomGenerator The random generator to be used
212 * @param min Minimum coordinate value for each axis, with range (-infinity, infinity)
213 * @param max Maximum coordinate value for each axis, with range [min, infinity)
214 * @return Random 3D vector with random length
215 */
216 static VectorT3<T> vector3(RandomGenerator& randomGenerator, const T min, const T max);
217
218 /**
219 * Returns a random 3D vector with coordinates in a given range using an explicit random generator.
220 * @param randomGenerator The random generator to be used
221 * @param range 3D vector defining the +/- ranges separately for each axis, with range [0, infinity] x [0, infinity] x [0, infinity]
222 * @return Random 3D vector with random length
223 */
224 static VectorT3<T> vector3(RandomGenerator& randomGenerator, const VectorT3<T>& range);
225
226 /**
227 * Returns a random 3D vector with coordinates in a given range using an explicit random generator.
228 * @param randomGenerator The random generator to be used
229 * @param xMin Minimum coordinate value for x-axis, with range (-infinity, infinity)
230 * @param xMax Maximum coordinate value for x-axis, with range [xMin, infinity)
231 * @param yMin Minimum coordinate value for y-axis, with range (-infinity, infinity)
232 * @param yMax Maximum coordinate value for y-axis, with range [yMin, infinity)
233 * @param zMin Minimum coordinate value for z-axis, with range (-infinity, infinity)
234 * @param zMax Maximum coordinate value for z-axis, with range [zMin, infinity)
235 * @return Random 3D vector with random length
236 */
237 static VectorT3<T> vector3(RandomGenerator& randomGenerator, const T xMin, const T xMax, const T yMin, const T yMax, const T zMin, const T zMax);
238
239 /**
240 * Returns a random 4D vector with length 1 which is equal distributed within a hyper sphere.
241 * @return The resulting random 4D vector, with vector4().length() == 1
242 */
243 static VectorT4<T> vector4();
244
245 /**
246 * Returns a random 4D vector with length 1 which is equal distributed within a hyper sphere using an explicit random generator.
247 * @param randomGenerator The random generator to be used
248 * @return The resulting random 4D vector, with vector4().length() == 1
249 */
250 static VectorT4<T> vector4(RandomGenerator& randomGenerator);
251
252 /**
253 * Returns a random 4D vector with coordinates in a given range.
254 * @param min Minimum coordinate value for each axis, with range (-infinity, infinity)
255 * @param max Maximum coordinate value for each axis, with range [min, infinity)
256 * @return Random 4D vector with random length
257 */
258 static VectorT4<T> vector4(const T min, const T max);
259
260 /**
261 * Returns a random 4D vector with coordinates in a given range using an explicit random generator.
262 * @param randomGenerator The random generator to be used
263 * @param min Minimum coordinate value for each axis, with range (-infinity, infinity)
264 * @param max Maximum coordinate value for each axis, with range [min, infinity)
265 * @return Random 4D vector with random length
266 */
267 static VectorT4<T> vector4(RandomGenerator& randomGenerator, const T min, const T max);
268
269 /**
270 * Returns a random rotation as unit quaternion.
271 * @return Random rotation
272 */
273 static QuaternionT<T> quaternion();
274
275 /**
276 * Returns a random rotation as unit quaternion using an explicit random generator.
277 * @param randomGenerator The random generator to be used
278 * @return Random rotation
279 */
280 static QuaternionT<T> quaternion(RandomGenerator& randomGenerator);
281
282 /**
283 * Returns a random rotation.
284 * @return Random rotation
285 */
286 static RotationT<T> rotation();
287
288 /**
289 * Returns a random rotation using an explicit random generator.
290 * @param randomGenerator The random generator to be used
291 * @return Random rotation
292 */
293 static RotationT<T> rotation(RandomGenerator& randomGenerator);
294
295 /**
296 * Returns a random euler.
297 * @return Random euler
298 */
299 static EulerT<T> euler();
300
301 /**
302 * Returns a random euler with angles in a given range.
303 * @param range Scalar defining the +/- range for each angle axis in radian, with range [0, PI/2)
304 * @return Random euler
305 */
306 static EulerT<T> euler(const T range);
307
308 /**
309 * Returns a random euler with angles in a given range.
310 * This function allows to specified an angle range so that a minimal and maximal rotation is guaranteed.<br>
311 * First, three individual random angles are determined lying inside the specified range.<br>
312 * Second, the signs of the three angles are determined randomly (as the range is specified with positive values).<br>
313 * @param minRange Scalar defining the minimal range for each angle axis (in negative and positive direction) in radian, with range [0, PI/2)
314 * @param maxRange Scalar defining the minimal range for each angle axis (in negative and positive direction) in radian, with range [minRange, PI/2)
315 * @return Random euler
316 */
317 static EulerT<T> euler(const T minRange, const T maxRange);
318
319 /**
320 * Returns a random euler using an explicit random generator.
321 * @param randomGenerator The random generator to be used
322 * @return Random euler
323 */
324 static EulerT<T> euler(RandomGenerator& randomGenerator);
325
326 /**
327 * Returns a random euler with angles in a given range using an explicit random generator.
328 * @param randomGenerator The random generator to be used
329 * @param range Scalar defining the +/- range for each angle axis in radian, with range [0, PI/2)
330 * @return Random euler
331 */
332 static EulerT<T> euler(RandomGenerator& randomGenerator, const T range);
333
334 /**
335 * Returns a random euler with angles in a given range using an explicit random generator.
336 * This function allows to specified an angle range so that a minimal and maximal rotation is guaranteed.<br>
337 * First, three individual random angles are determined lying inside the specified range.<br>
338 * Second, the signs of the three angles are determined randomly (as the range is specified with positive values).<br>
339 * @param randomGenerator The random generator to be used
340 * @param minRange Scalar defining the minimal range for each angle axis in radian, with range [0, PI/2)
341 * @param maxRange Scalar defining the maximal range for each angle axis in radian, with range [minRange, PI/2)
342 * @return Random euler
343 */
344 static EulerT<T> euler(RandomGenerator& randomGenerator, const T minRange, const T maxRange);
345
346 protected:
347
348};
349
350template <typename T>
352{
353 if (rand() % 2u == 0u)
354 {
355 return T(-1);
356 }
357 else
358 {
359 return T(1);
360 }
361}
362
363template <typename T>
364inline T RandomT<T>::sign(RandomGenerator& randomGenerator)
365{
366 if (randomGenerator.rand() % 2u == 0u)
367 {
368 return T(-1);
369 }
370 else
371 {
372 return T(1);
373 }
374}
375
376template <typename T>
377T RandomT<T>::scalar(const T lower, const T upper)
378{
379 ocean_assert(std::is_floating_point<T>::value);
380
381 ocean_assert(upper >= lower);
382
383 return lower + (upper - lower) * (T(rand()) / T(RAND_MAX));
384}
385
386template <typename T>
387T RandomT<T>::scalar(RandomGenerator& randomGenerator, const T lower, const T upper)
388{
389 ocean_assert(std::is_floating_point<T>::value);
390
391 ocean_assert(upper >= lower);
392
393 return lower + (upper - lower) * (T(randomGenerator.rand()) / T(RandomGenerator::randMax()));
394}
395
396template <typename T>
398{
399 const T maxValue = NumericT<T>::gaussianDistribution(T(0.0), sigma);
400
401#ifdef OCEAN_DEBUG
402 unsigned int debugN = 0u;
403#endif
404
405 while (true)
406 {
407 const T randomValue = scalar(T(-5.0) * sigma, T(5.0) * sigma);
408 const T check = scalar(T(0.0), maxValue);
409
410 if (check <= NumericT<T>::gaussianDistribution(randomValue, sigma))
411 {
412 return randomValue;
413 }
414
415 ocean_assert(++debugN < 1000);
416 }
417}
418
419template <typename T>
420T RandomT<T>::gaussianNoise(RandomGenerator& randomGenerator, const T sigma)
421{
422 const T maxValue = NumericT<T>::gaussianDistribution(T(0.0), sigma);
423
424#ifdef OCEAN_DEBUG
425 unsigned int debugN = 0u;
426#endif
427
428 while (true)
429 {
430 const T randomValue = scalar(randomGenerator, T(-5.0) * sigma, T(5.0) * sigma);
431 const T check = scalar(randomGenerator, T(0.0), maxValue);
432
433 if (check <= NumericT<T>::gaussianDistribution(randomValue, sigma))
434 {
435 return randomValue;
436 }
437
438 ocean_assert(++debugN < 1000);
439 }
440}
441
442template <typename T>
443VectorT2<T> RandomT<T>::gaussianNoiseVector2(const T sigmaX, const T sigmaY)
444{
445 const T x = gaussianNoise(sigmaX);
446 const T y = gaussianNoise(sigmaY);
447
448 return VectorT2<T>(x, y);
449}
450
451template <typename T>
452VectorT2<T> RandomT<T>::gaussianNoiseVector2(RandomGenerator& randomGenerator, const T sigmaX, const T sigmaY)
453{
454 const T x = gaussianNoise(randomGenerator, sigmaX);
455 const T y = gaussianNoise(randomGenerator, sigmaY);
456
457 return VectorT2<T>(x, y);
458}
459
460template <typename T>
462{
463 while (true)
464 {
465 VectorT2<T> vector(scalar(T(-1.0), T(1.0)), scalar(T(-1.0), T(1.0)));
466
467 if (vector.sqr() <= T(1.0) && vector.normalize())
468 {
469 return vector;
470 }
471 }
472
473 ocean_assert(false && "This should never happen!");
474 return VectorT2<T>(1, 0);
475}
476
477template <typename T>
479{
480 while (true)
481 {
482 const T x = scalar(randomGenerator, T(-1.0), T(1.0));
483 const T y = scalar(randomGenerator, T(-1.0), T(1.0));
484
485 VectorT2<T> vector(x, y);
486
487 if (vector.sqr() <= T(1.0) && vector.normalize())
488 {
489 return vector;
490 }
491 }
492
493 ocean_assert(false && "This should never happen!");
494 return VectorT2<T>(1, 0);
495}
496
497template <typename T>
498VectorT2<T> RandomT<T>::vector2(const T min, const T max)
499{
500 return VectorT2<T>(scalar(min, max), scalar(min, max));
501}
502
503template <typename T>
504VectorT2<T> RandomT<T>::vector2(RandomGenerator& randomGenerator, const T min, const T max)
505{
506 const T x = scalar(randomGenerator, min, max);
507 const T y = scalar(randomGenerator, min, max);
508
509 return VectorT2<T>(x, y);
510}
511
512template <typename T>
513VectorT2<T> RandomT<T>::vector2(const T xMin, const T xMax, const T yMin, const T yMax)
514{
515 return VectorT2<T>(scalar(xMin, xMax), scalar(yMin, yMax));
516}
517
518template <typename T>
519VectorT2<T> RandomT<T>::vector2(RandomGenerator& randomGenerator, const T xMin, const T xMax, const T yMin, const T yMax)
520{
521 const T x = scalar(randomGenerator, xMin, xMax);
522 const T y = scalar(randomGenerator, yMin, yMax);
523
524 return VectorT2<T>(x, y);
525}
526
527template <typename T>
529{
530 ocean_assert(range.x() >= T(0.0) && range.y() >= T(0.0) && range.z() >= T(0.0));
531
532 return VectorT3<T>(scalar(-range.x(), range.x()), scalar(-range.y(), range.y()), scalar(-range.z(), range.z()));
533}
534
535template <typename T>
537{
538 ocean_assert(range.x() >= T(0.0) && range.y() >= T(0.0) && range.z() >= T(0.0));
539
540 const T x = scalar(randomGenerator, -range.x(), range.x());
541 const T y = scalar(randomGenerator, -range.y(), range.y());
542 const T z = scalar(randomGenerator, -range.z(), range.z());
543
544 return VectorT3<T>(x, y, z);
545}
546
547template <typename T>
549{
550 while (true)
551 {
552 VectorT3<T> vector(scalar(T(-1.0), T(1.0)), scalar(T(-1.0), T(1.0)), scalar(T(-1.0), T(1.0)));
553
554 if (vector.sqr() <= T(1.0) && vector.normalize())
555 {
556 return vector;
557 }
558 }
559
560 ocean_assert(false && "This should never happen!");
561 return VectorT3<T>(1, 0, 0);
562}
563
564template <typename T>
566{
567 while (true)
568 {
569 const T x = scalar(randomGenerator, T(-1.0), T(1.0));
570 const T y = scalar(randomGenerator, T(-1.0), T(1.0));
571 const T z = scalar(randomGenerator, T(-1.0), T(1.0));
572
573 VectorT3<T> vector(x, y, z);
574
575 if (vector.sqr() <= T(1.0) && vector.normalize())
576 {
577 return vector;
578 }
579 }
580
581 ocean_assert(false && "This should never happen!");
582 return VectorT3<T>(1, 0, 0);
583}
584
585template <typename T>
586VectorT3<T> RandomT<T>::vector3(const T min, const T max)
587{
588 return VectorT3<T>(scalar(min, max), scalar(min, max), scalar(min, max));
589}
590
591template <typename T>
592VectorT3<T> RandomT<T>::vector3(RandomGenerator& randomGenerator, const T min, const T max)
593{
594 const T x = scalar(randomGenerator, min, max);
595 const T y = scalar(randomGenerator, min, max);
596 const T z = scalar(randomGenerator, min, max);
597
598 return VectorT3<T>(x, y, z);
599}
600
601template <typename T>
602VectorT3<T> RandomT<T>::vector3(RandomGenerator& randomGenerator, const T xMin, const T xMax, const T yMin, const T yMax, const T zMin, const T zMax)
603{
604 const T x = scalar(randomGenerator, xMin, xMax);
605 const T y = scalar(randomGenerator, yMin, yMax);
606 const T z = scalar(randomGenerator, zMin, zMax);
607
608 return VectorT3<T>(x, y, z);
609}
610
611template <typename T>
613{
614 while (true)
615 {
616 VectorT4<T> vector(scalar(T(-1.0), T(1.0)), scalar(T(-1.0), T(1.0)), scalar(T(-1.0), T(1.0)), scalar(T(-1.0), T(1.0)));
617
618 if (vector.sqr() <= T(1.0) && vector.normalize())
619 {
620 return vector;
621 }
622 }
623
624 ocean_assert(false && "This should never happen!");
625 return VectorT4<T>(1, 0, 0, 0);
626}
627
628template <typename T>
630{
631 while (true)
632 {
633 const T x = scalar(randomGenerator, T(-1.0), T(1.0));
634 const T y = scalar(randomGenerator, T(-1.0), T(1.0));
635 const T z = scalar(randomGenerator, T(-1.0), T(1.0));
636 const T w = scalar(randomGenerator, T(-1.0), T(1.0));
637
638 VectorT4<T> vector(x, y, z, w);
639
640 if (vector.sqr() <= T(1.0) && vector.normalize())
641 {
642 return vector;
643 }
644 }
645
646 ocean_assert(false && "This should never happen!");
647 return VectorT4<T>(1, 0, 0, 0);
648}
649
650template <typename T>
651VectorT4<T> RandomT<T>::vector4(const T min, const T max)
652{
653 return VectorT4<T>(scalar(min, max), scalar(min, max), scalar(min, max), scalar(min, max));
654}
655
656template <typename T>
657VectorT4<T> RandomT<T>::vector4(RandomGenerator& randomGenerator, const T min, const T max)
658{
659 const T x = scalar(randomGenerator, min, max);
660 const T y = scalar(randomGenerator, min, max);
661 const T z = scalar(randomGenerator, min, max);
662 const T w = scalar(randomGenerator, min, max);
663
664 return VectorT4<T>(x, y, z, w);
665}
666
667template <typename T>
669{
670 // uniformly distributed random rotation by sampling a point on the 4D unit hypersphere
671
672 return QuaternionT<T>(vector4());
673}
674
675template <typename T>
677{
678 // uniformly distributed random rotation by sampling a point on the 4D unit hypersphere
679
680 return QuaternionT<T>(vector4(randomGenerator));
681}
682
683template <typename T>
685{
686 // uniformly distributed random rotation via quaternion
687
688 return RotationT<T>(quaternion());
689}
690
691template <typename T>
693{
694 // uniformly distributed random rotation via quaternion
695
696 return RotationT<T>(quaternion(randomGenerator));
697}
698
699template <typename T>
704
705template <typename T>
707{
708 ocean_assert(range >= T(0.0) && range < NumericT<T>::pi_2());
709
710 return EulerT<T>(scalar(-range, range), scalar(-range, range), scalar(-range, range));
711}
712
713template <typename T>
714EulerT<T> RandomT<T>::euler(const T minRange, const T maxRange)
715{
716 ocean_assert(minRange >= T(0.0) && minRange < NumericT<T>::pi_2());
717 ocean_assert(maxRange >= T(0.0) && maxRange < NumericT<T>::pi_2());
718 ocean_assert(minRange <= maxRange);
719
720 return EulerT<T>(scalar(minRange, maxRange) * sign(), scalar(minRange, maxRange) * sign(), scalar(minRange, maxRange) * sign());
721}
722
723template <typename T>
725{
726 const T yaw = scalar(randomGenerator, -NumericT<T>::pi(), NumericT<T>::pi());
727 const T pitch = scalar(randomGenerator, -NumericT<T>::pi_2(), NumericT<T>::pi_2());
728 const T roll = scalar(randomGenerator, -NumericT<T>::pi(), NumericT<T>::pi());
729
730 return EulerT<T>(yaw, pitch, roll);
731}
732
733template <typename T>
734EulerT<T> RandomT<T>::euler(RandomGenerator& randomGenerator, const T range)
735{
736 ocean_assert(range >= T(0.0) && range < NumericT<T>::pi_2());
737
738 const T yaw = scalar(randomGenerator, -range, range);
739 const T pitch = scalar(randomGenerator, -range, range);
740 const T roll = scalar(randomGenerator, -range, range);
741
742 return EulerT<T>(yaw, pitch, roll);
743}
744
745template <typename T>
746EulerT<T> RandomT<T>::euler(RandomGenerator& randomGenerator, const T minRange, const T maxRange)
747{
748 ocean_assert(minRange >= T(0.0) && minRange < NumericT<T>::pi_2());
749 ocean_assert(maxRange >= T(0.0) && maxRange < NumericT<T>::pi_2());
750 ocean_assert(minRange <= maxRange);
751
752 const T yawSign = sign(randomGenerator);
753 const T yaw = scalar(randomGenerator, minRange, maxRange) * yawSign;
754
755 const T pitchSign = sign(randomGenerator);
756 const T pitch = scalar(randomGenerator, minRange, maxRange) * pitchSign;
757
758 const T rollSign = sign(randomGenerator);
759 const T roll = scalar(randomGenerator, minRange, maxRange) * rollSign;
760
761 return EulerT<T>(yaw, pitch, roll);
762}
763
764}
765
766#endif // META_OCEAN_MATH_RANDOM_H
This class implements an euler rotation with angles: yaw, pitch and roll.
Definition Euler.h:82
This class provides basic numeric functionalities.
Definition Numeric.h:57
static T gaussianDistribution(const T x, const T sigma)
Returns a value of the univariate Gaussian distribution centered around the origin.
Definition Numeric.h:3458
This class implements a unit quaternion rotation.
Definition Quaternion.h:100
This class implements a generator for random numbers.
Definition RandomGenerator.h:42
unsigned int rand()
Returns the next random number.
Definition RandomGenerator.h:175
static constexpr unsigned int randMax()
Returns the maximal random value of this generator.
Definition RandomGenerator.h:211
This class provides base random functions and several random functions for integer data types.
Definition RandomI.h:29
This class provides several random functions for different data types using a floating point type for...
Definition Random.h:61
static T gaussianNoise(const T sigma)
Returns a random number with Gaussian distribution.
Definition Random.h:397
static QuaternionT< T > quaternion()
Returns a random rotation as unit quaternion.
Definition Random.h:668
static VectorT3< T > vector3()
Returns a random 3D vector with length 1 which is equal distributed within a sphere.
Definition Random.h:548
static VectorT2< T > gaussianNoiseVector2(const T sigmaX, const T sigmaY)
Returns a random 2D vector with Gaussian distribution.
Definition Random.h:443
static RotationT< T > rotation()
Returns a random rotation.
Definition Random.h:684
static T sign()
Returns a random sign (either +1 or -1).
Definition Random.h:351
static VectorT4< T > vector4()
Returns a random 4D vector with length 1 which is equal distributed within a hyper sphere.
Definition Random.h:612
static VectorT2< T > vector2()
Returns a random 2D vector with length 1 which is equal distributed within a circle.
Definition Random.h:461
static EulerT< T > euler()
Returns a random euler.
Definition Random.h:700
static T scalar(const T lower, const T upper)
Returns a random number between two borders.
Definition Random.h:377
This class implements a axis-angle rotation using floating point values.
Definition Rotation.h:73
This class implements a vector with two elements.
Definition Vector2.h:96
T sqr() const
Returns the square of the vector length.
Definition Vector2.h:633
bool normalize()
Normalizes this vector.
Definition Vector2.h:612
This class implements a vector with three elements.
Definition Vector3.h:97
const T & y() const noexcept
Returns the y value.
Definition Vector3.h:824
bool normalize()
Normalizes this vector.
Definition Vector3.h:659
const T & x() const noexcept
Returns the x value.
Definition Vector3.h:812
const T & z() const noexcept
Returns the z value.
Definition Vector3.h:836
T sqr() const
Returns the square of the vector length.
Definition Vector3.h:682
This class implements a vector with four elements.
Definition Vector4.h:97
T sqr() const
Returns the square of the vector length.
Definition Vector4.h:628
bool normalize()
Normalizes this vector.
Definition Vector4.h:605
The namespace covering the entire Ocean framework.
Definition Accessor.h:15