Open3D (C++ API)  0.19.0
Random.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 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) {
77  "low must be <= high, but got low={} and high={}.", low,
78  high);
79  }
80  }
81 
83  T operator()() {
84  std::lock_guard<std::mutex> lock(*GetMutex());
85  return distribution_(*GetEngine());
86  }
87 
88 protected:
89  std::uniform_int_distribution<T> distribution_;
90 };
91 
109 template <typename T>
111 public:
116  UniformRealGenerator(const T low = 0.0, const T high = 1.0)
117  : distribution_(low, high) {
118  if (low >= high) {
119  utility::LogError("low must be < high, but got low={} and high={}.",
120  low, high);
121  }
122  }
123 
126  std::lock_guard<std::mutex> lock(*GetMutex());
127  return distribution_(*GetEngine());
128  }
129 
130 protected:
131  std::uniform_real_distribution<T> distribution_;
132 };
133 
151 template <typename T>
153 public:
158  NormalGenerator(const T mean = 0.0, const T stddev = 1.0)
159  : distribution_(mean, stddev) {
160  if (stddev <= 0) {
161  utility::LogError("stddev must be > 0, but got {}.", stddev);
162  }
163  }
164 
167  std::lock_guard<std::mutex> lock(*GetMutex());
168  return distribution_(*GetEngine());
169  }
170 
171 protected:
172  std::normal_distribution<T> distribution_;
173 };
174 
194 template <typename T>
196 public:
203  template <typename InputIt>
204  DiscreteGenerator(InputIt first, InputIt last)
205  : distribution_(first, last) {
206  if (first > last) {
207  utility::LogError("first must be <= last.");
208  }
209  }
210 
213  std::lock_guard<std::mutex> lock(*GetMutex());
214  return distribution_(*GetEngine());
215  }
216 
217 protected:
218  std::discrete_distribution<T> distribution_;
219 };
220 
221 } // namespace random
222 } // namespace utility
223 } // namespace open3d
#define LogError(...)
Definition: Logging.h:51
std::discrete_distribution< T > distribution_
Definition: Random.h:218
T operator()()
Call this to generate a discretely distributed integer value.
Definition: Random.h:212
DiscreteGenerator(InputIt first, InputIt last)
Definition: Random.h:204
T operator()()
Call this to generate a normally distributed floating point value.
Definition: Random.h:166
NormalGenerator(const T mean=0.0, const T stddev=1.0)
Definition: Random.h:158
std::normal_distribution< T > distribution_
Definition: Random.h:172
std::uniform_int_distribution< T > distribution_
Definition: Random.h:89
UniformIntGenerator(const T low, const T high)
Definition: Random.h:71
T operator()()
Call this to generate a uniformly distributed integer.
Definition: Random.h:83
T operator()()
Call this to generate a uniformly distributed floating point value.
Definition: Random.h:125
std::uniform_real_distribution< T > distribution_
Definition: Random.h:131
UniformRealGenerator(const T low=0.0, const T high=1.0)
Definition: Random.h:116
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