Open3D (C++ API)  0.18.0+5c982c7
Random.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <mutex>
11 #include <random>
12 
13 #include "open3d/utility/Logging.h"
14 
15 namespace open3d {
16 namespace utility {
17 namespace random {
18 
20 void Seed(const int seed);
21 
35 std::mt19937* GetEngine();
36 
39 std::mutex* GetMutex();
40 
45 
63 template <typename T>
65 public:
71  UniformIntGenerator(const T low, const T high) : distribution_(low, high) {
72  if (low < 0) {
73  utility::LogError("low must be > 0, but got {}.", low);
74  }
75  if (low >= high) {
76  utility::LogError("low must be < high, but got low={} and high={}.",
77  low, high);
78  }
79  }
80 
82  T operator()() {
83  std::lock_guard<std::mutex> lock(*GetMutex());
84  return distribution_(*GetEngine());
85  }
86 
87 protected:
88  std::uniform_int_distribution<T> distribution_;
89 };
90 
108 template <typename T>
110 public:
115  UniformRealGenerator(const T low = 0.0, const T high = 1.0)
116  : distribution_(low, high) {
117  if (low >= high) {
118  utility::LogError("low must be < high, but got low={} and high={}.",
119  low, high);
120  }
121  }
122 
125  std::lock_guard<std::mutex> lock(*GetMutex());
126  return distribution_(*GetEngine());
127  }
128 
129 protected:
130  std::uniform_real_distribution<T> distribution_;
131 };
132 
150 template <typename T>
152 public:
157  NormalGenerator(const T mean = 0.0, const T stddev = 1.0)
158  : distribution_(mean, stddev) {
159  if (stddev <= 0) {
160  utility::LogError("stddev must be > 0, but got {}.", stddev);
161  }
162  }
163 
166  std::lock_guard<std::mutex> lock(*GetMutex());
167  return distribution_(*GetEngine());
168  }
169 
170 protected:
171  std::normal_distribution<T> distribution_;
172 };
173 
193 template <typename T>
195 public:
202  template <typename InputIt>
203  DiscreteGenerator(InputIt first, InputIt last)
204  : distribution_(first, last) {
205  if (first > last) {
206  utility::LogError("first must be <= last.");
207  }
208  }
209 
212  std::lock_guard<std::mutex> lock(*GetMutex());
213  return distribution_(*GetEngine());
214  }
215 
216 protected:
217  std::discrete_distribution<T> distribution_;
218 };
219 
220 } // namespace random
221 } // namespace utility
222 } // namespace open3d
#define LogError(...)
Definition: Logging.h:48
std::discrete_distribution< T > distribution_
Definition: Random.h:217
T operator()()
Call this to generate a discretely distributed integer value.
Definition: Random.h:211
DiscreteGenerator(InputIt first, InputIt last)
Definition: Random.h:203
T operator()()
Call this to generate a normally distributed floating point value.
Definition: Random.h:165
NormalGenerator(const T mean=0.0, const T stddev=1.0)
Definition: Random.h:157
std::normal_distribution< T > distribution_
Definition: Random.h:171
std::uniform_int_distribution< T > distribution_
Definition: Random.h:88
UniformIntGenerator(const T low, const T high)
Definition: Random.h:71
T operator()()
Call this to generate a uniformly distributed integer.
Definition: Random.h:82
T operator()()
Call this to generate a uniformly distributed floating point value.
Definition: Random.h:124
std::uniform_real_distribution< T > distribution_
Definition: Random.h:130
UniformRealGenerator(const T low=0.0, const T high=1.0)
Definition: Random.h:115
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t timeout_in_ms capture_handle capture_handle capture_handle image_handle temperature_c k4a_image_t image_handle uint8_t image_handle image_handle image_handle image_handle uint32_t
Definition: K4aPlugin.cpp:548
std::mt19937 * GetEngine()
Definition: Random.cpp:55
uint32_t RandUint32()
Definition: Random.cpp:59
void Seed(const int seed)
Set Open3D global random seed.
Definition: Random.cpp:53
std::mutex * GetMutex()
Definition: Random.cpp:57
Definition: PinholeCameraIntrinsic.cpp:16