Open3D (C++ API)  0.11.0
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 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 <string>
33 #include <tuple>
34 #include <vector>
35 
36 namespace open3d {
37 namespace utility {
38 
46 
47 template <typename TT>
48 struct hash_tuple {
49  size_t operator()(TT const& tt) const { return std::hash<TT>()(tt); }
50 };
51 
52 namespace {
53 
54 template <class T>
55 inline void hash_combine(std::size_t& seed, T const& v) {
56  seed ^= hash_tuple<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
57 }
58 
59 template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1>
60 struct HashValueImpl {
61  static void apply(size_t& seed, Tuple const& tuple) {
62  HashValueImpl<Tuple, Index - 1>::apply(seed, tuple);
63  hash_combine(seed, std::get<Index>(tuple));
64  }
65 };
66 
67 template <class Tuple>
68 struct HashValueImpl<Tuple, 0> {
69  static void apply(size_t& seed, Tuple const& tuple) {
70  hash_combine(seed, std::get<0>(tuple));
71  }
72 };
73 
74 } // unnamed namespace
75 
76 template <typename... TT>
77 struct hash_tuple<std::tuple<TT...>> {
78  size_t operator()(std::tuple<TT...> const& tt) const {
79  size_t seed = 0;
80  HashValueImpl<std::tuple<TT...>>::apply(seed, tt);
81  return seed;
82  }
83 };
84 
85 template <typename T>
86 struct hash_eigen {
87  std::size_t operator()(T const& matrix) const {
88  size_t seed = 0;
89  for (int i = 0; i < (int)matrix.size(); i++) {
90  auto elem = *(matrix.data() + i);
91  seed ^= std::hash<typename T::Scalar>()(elem) + 0x9e3779b9 +
92  (seed << 6) + (seed >> 2);
93  }
94  return seed;
95  }
96 };
97 
98 // Hash function for enum class for C++ standard less than C++14
99 // https://stackoverflow.com/a/24847480/1255535
101  template <typename T>
102  std::size_t operator()(T t) const {
103  return static_cast<std::size_t>(t);
104  }
105 };
106 
109 void SplitString(std::vector<std::string>& tokens,
110  const std::string& str,
111  const std::string& delimiters = " ",
112  bool trim_empty_str = true);
113 
117 size_t WordLength(const std::string& doc,
118  size_t start_pos,
119  const std::string& valid_chars = "_");
120 
121 std::string& LeftStripString(std::string& str,
122  const std::string& chars = "\t\n\v\f\r ");
123 
124 std::string& RightStripString(std::string& str,
125  const std::string& chars = "\t\n\v\f\r ");
126 
129 std::string& StripString(std::string& str,
130  const std::string& chars = "\t\n\v\f\r ");
131 
133 std::string ToLower(const std::string& s);
134 
136 std::string ToUpper(const std::string& s);
137 
138 void Sleep(int milliseconds);
139 
141 inline int DivUp(int x, int y) {
142  div_t tmp = std::div(x, y);
143  return tmp.quot + (tmp.rem != 0 ? 1 : 0);
144 }
145 
149 int UniformRandInt(const int min, const int max);
150 
164 template <typename T>
165 T UniformRandFloatBinaryFriendly(unsigned int power = 5) {
166  double p = std::pow(2, power);
167  int n = UniformRandInt(0, p - 1);
168  return static_cast<T>(1. / p * n);
169 }
170 
171 } // namespace utility
172 } // namespace open3d
std::size_t operator()(T t) const
Definition: Helper.h:102
T UniformRandFloatBinaryFriendly(unsigned int power=5)
Definition: Helper.h:165
int UniformRandInt(const int min, const int max)
Definition: Helper.cpp:116
Definition: Helper.h:48
int DivUp(int x, int y)
Computes the quotient of x/y with rounding up.
Definition: Helper.h:141
Definition: Optional.h:922
Definition: Helper.h:86
size_t WordLength(const std::string &doc, size_t start_pos, const std::string &valid_chars)
Definition: Helper.cpp:87
Definition: Helper.h:100
size_t operator()(TT const &tt) const
Definition: Helper.h:49
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:476
std::size_t operator()(T const &matrix) const
Definition: Helper.h:87
Definition: PinholeCameraIntrinsic.cpp:35
std::string & RightStripString(std::string &str, const std::string &chars)
Definition: Helper.cpp:63
void SplitString(std::vector< std::string > &tokens, const std::string &str, const std::string &delimiters, bool trim_empty_str)
Definition: Helper.cpp:43
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:721
std::string & StripString(std::string &str, const std::string &chars)
Definition: Helper.cpp:68
std::string ToUpper(const std::string &str)
Convert string to the upper case.
Definition: Helper.cpp:79
void Sleep(int milliseconds)
Definition: Helper.cpp:108
std::string ToLower(const std::string &str)
Convert string to the lower case.
Definition: Helper.cpp:72
std::string & LeftStripString(std::string &str, const std::string &chars)
Definition: Helper.cpp:58
size_t operator()(std::tuple< TT... > const &tt) const
Definition: Helper.h:78