Loading [MathJax]/extensions/TeX/AMSsymbols.js
Open3D (C++ API)  0.15.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Helper.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018-2021 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include <cmath>
30 #include <cstdlib>
31 #include <functional>
32 #include <random>
33 #include <string>
34 #include <tuple>
35 #include <vector>
36 
37 namespace open3d {
38 namespace utility {
39 
47 
48 template <typename TT>
49 struct hash_tuple {
50  size_t operator()(TT const& tt) const { return std::hash<TT>()(tt); }
51 };
52 
53 namespace {
54 
55 template <class T>
56 inline void hash_combine(std::size_t& seed, T const& v) {
57  seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
58 }
59 
60 template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1>
61 struct HashValueImpl {
62  static void apply(size_t& seed, Tuple const& tuple) {
63  HashValueImpl<Tuple, Index - 1>::apply(seed, tuple);
64  hash_combine(seed, std::get<Index>(tuple));
65  }
66 };
67 
68 template <class Tuple>
69 struct HashValueImpl<Tuple, 0> {
70  static void apply(size_t& seed, Tuple const& tuple) {
71  hash_combine(seed, std::get<0>(tuple));
72  }
73 };
74 
75 } // unnamed namespace
76 
77 template <typename... TT>
78 struct hash_tuple<std::tuple<TT...>> {
79  size_t operator()(std::tuple<TT...> const& tt) const {
80  size_t seed = 0;
81  HashValueImpl<std::tuple<TT...>>::apply(seed, tt);
82  return seed;
83  }
84 };
85 
86 template <typename T>
87 struct hash_eigen {
88  std::size_t operator()(T const& matrix) const {
89  size_t seed = 0;
90  for (int i = 0; i < (int)matrix.size(); i++) {
91  auto elem = *(matrix.data() + i);
92  seed ^= std::hash<typename T::Scalar>()(elem) + 0x9e3779b9 +
93  (seed << 6) + (seed >> 2);
94  }
95  return seed;
96  }
97 };
98 
99 // Hash function for enum class for C++ standard less than C++14
100 // https://stackoverflow.com/a/24847480/1255535
102  template <typename T>
103  std::size_t operator()(T t) const {
104  return static_cast<std::size_t>(t);
105  }
106 };
107 
110 std::vector<std::string> SplitString(const std::string& str,
111  const std::string& delimiters = " ",
112  bool trim_empty_str = true);
113 
117 bool ContainsString(const std::string& src, const std::string& dst);
118 
122 bool StringStartsWith(const std::string& src, const std::string& tar);
123 
127 bool StringEndsWith(const std::string& src, const std::string& tar);
128 
129 std::string JoinStrings(const std::vector<std::string>& strs,
130  const std::string& delimiter = ", ");
131 
135 size_t WordLength(const std::string& doc,
136  size_t start_pos,
137  const std::string& valid_chars = "_");
138 
139 std::string& LeftStripString(std::string& str,
140  const std::string& chars = "\t\n\v\f\r ");
141 
142 std::string& RightStripString(std::string& str,
143  const std::string& chars = "\t\n\v\f\r ");
144 
147 std::string& StripString(std::string& str,
148  const std::string& chars = "\t\n\v\f\r ");
149 
151 std::string ToLower(const std::string& s);
152 
154 std::string ToUpper(const std::string& s);
155 
156 void Sleep(int milliseconds);
157 
159 inline int DivUp(int x, int y) {
160  div_t tmp = std::div(x, y);
161  return tmp.quot + (tmp.rem != 0 ? 1 : 0);
162 }
163 
169 public:
171  const int min,
172  const int max,
173  std::mt19937::result_type seed = std::random_device{}())
174  : distribution_(min, max), generator_(seed) {}
175  int operator()() { return distribution_(generator_); }
176 
177 protected:
178  std::uniform_int_distribution<int> distribution_;
179  std::mt19937 generator_;
180 };
181 
183 std::string GetCurrentTimeStamp();
184 
185 } // namespace utility
186 } // namespace open3d
std::size_t operator()(T t) const
Definition: Helper.h:103
int operator()()
Definition: Helper.h:175
Definition: Helper.h:49
UniformRandIntGenerator(const int min, const int max, std::mt19937::result_type seed=std::random_device{}())
Definition: Helper.h:170
int DivUp(int x, int y)
Computes the quotient of x/y with rounding up.
Definition: Helper.h:159
std::vector< std::string > SplitString(const std::string &str, const std::string &delimiters, bool trim_empty_str)
Definition: Helper.cpp:45
Definition: Device.h:138
Definition: Helper.h:87
std::string JoinStrings(const std::vector< std::string > &strs, const std::string &delimiter)
Definition: Helper.cpp:76
size_t WordLength(const std::string &doc, size_t start_pos, const std::string &valid_chars)
Definition: Helper.cpp:117
std::string GetCurrentTimeStamp()
Returns current time stamp.
Definition: Helper.cpp:146
Definition: Helper.h:101
Draw pseudo-random integers bounded by min and max (inclusive) from a uniform distribution.
Definition: Helper.h:168
std::mt19937 generator_
Definition: Helper.h:179
size_t operator()(TT const &tt) const
Definition: Helper.h:50
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 int
Definition: K4aPlugin.cpp:489
std::size_t operator()(T const &matrix) const
Definition: Helper.h:88
std::uniform_int_distribution< int > distribution_
Definition: Helper.h:178
Definition: PinholeCameraIntrinsic.cpp:35
std::string & RightStripString(std::string &str, const std::string &chars)
Definition: Helper.cpp:93
bool StringEndsWith(const std::string &src, const std::string &tar)
Definition: Helper.cpp:70
bool ContainsString(const std::string &src, const std::string &dst)
Definition: Helper.cpp:61
bool StringStartsWith(const std::string &src, const std::string &tar)
Definition: Helper.cpp:65
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 image_handle timestamp_usec white_balance image_handle k4a_device_configuration_t config device_handle char size_t serial_number_size bool int32_t int32_t int32_t int32_t k4a_color_control_mode_t default_mode value const const k4a_calibration_t calibration char size_t
Definition: K4aPlugin.cpp:734
std::string & StripString(std::string &str, const std::string &chars)
Definition: Helper.cpp:98
std::string ToUpper(const std::string &str)
Convert string to the upper case.
Definition: Helper.cpp:109
void Sleep(int milliseconds)
Definition: Helper.cpp:138
std::string ToLower(const std::string &str)
Convert string to the lower case.
Definition: Helper.cpp:102
std::string & LeftStripString(std::string &str, const std::string &chars)
Definition: Helper.cpp:88
size_t operator()(std::tuple< TT... > const &tt) const
Definition: Helper.h:79