Open3D (C++ API)
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 namespace hash_tuple {
48 
49 template <typename TT>
50 struct hash {
51  size_t operator()(TT const& tt) const { return std::hash<TT>()(tt); }
52 };
53 
54 namespace {
55 
56 template <class T>
57 inline void hash_combine(std::size_t& seed, T const& v) {
58  seed ^= hash_tuple::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
59 }
60 
61 template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1>
62 struct HashValueImpl {
63  static void apply(size_t& seed, Tuple const& tuple) {
64  HashValueImpl<Tuple, Index - 1>::apply(seed, tuple);
65  hash_combine(seed, std::get<Index>(tuple));
66  }
67 };
68 
69 template <class Tuple>
70 struct HashValueImpl<Tuple, 0> {
71  static void apply(size_t& seed, Tuple const& tuple) {
72  hash_combine(seed, std::get<0>(tuple));
73  }
74 };
75 
76 } // unnamed namespace
77 
78 template <typename... TT>
79 struct hash<std::tuple<TT...>> {
80  size_t operator()(std::tuple<TT...> const& tt) const {
81  size_t seed = 0;
82  HashValueImpl<std::tuple<TT...>>::apply(seed, tt);
83  return seed;
84  }
85 };
86 
87 } // namespace hash_tuple
88 
89 namespace hash_eigen {
90 
91 template <typename T>
92 struct hash {
93  std::size_t operator()(T const& matrix) const {
94  size_t seed = 0;
95  for (int i = 0; i < (int)matrix.size(); i++) {
96  auto elem = *(matrix.data() + i);
97  seed ^= std::hash<typename T::Scalar>()(elem) + 0x9e3779b9 +
98  (seed << 6) + (seed >> 2);
99  }
100  return seed;
101  }
102 };
103 
104 } // namespace hash_eigen
105 
106 namespace hash_enum_class {
107 
108 // Hash function for enum class for C++ standard less than C++14
109 // https://stackoverflow.com/a/24847480/1255535
110 struct hash {
111  template <typename T>
112  std::size_t operator()(T t) const {
113  return static_cast<std::size_t>(t);
114  }
115 };
116 
117 } // namespace hash_enum_class
118 
121 void SplitString(std::vector<std::string>& tokens,
122  const std::string& str,
123  const std::string& delimiters = " ",
124  bool trim_empty_str = true);
125 
129 size_t WordLength(const std::string& doc,
130  size_t start_pos,
131  const std::string& valid_chars = "_");
132 
133 std::string& LeftStripString(std::string& str,
134  const std::string& chars = "\t\n\v\f\r ");
135 
136 std::string& RightStripString(std::string& str,
137  const std::string& chars = "\t\n\v\f\r ");
138 
141 std::string& StripString(std::string& str,
142  const std::string& chars = "\t\n\v\f\r ");
143 
145 std::string ToLower(const std::string& s);
146 
147 void Sleep(int milliseconds);
148 
150 inline int DivUp(int x, int y) {
151  div_t tmp = std::div(x, y);
152  return tmp.quot + (tmp.rem != 0 ? 1 : 0);
153 }
154 
158 int UniformRandInt(const int min, const int max);
159 
173 template <typename T>
174 T UniformRandFloatBinaryFriendly(unsigned int power = 5) {
175  double p = std::pow(2, power);
176  int n = UniformRandInt(0, p - 1);
177  return static_cast<T>(1. / p * n);
178 }
179 
180 } // namespace utility
181 } // namespace open3d
T UniformRandFloatBinaryFriendly(unsigned int power=5)
Definition: Helper.h:174
Definition: Helper.h:92
int UniformRandInt(const int min, const int max)
Definition: Helper.cpp:109
Definition: Helper.h:50
int DivUp(int x, int y)
Computes the quotient of x/y with rounding up.
Definition: Helper.h:150
Definition: RendererHandle.h:165
size_t operator()(std::tuple< TT... > const &tt) const
Definition: Helper.h:80
size_t WordLength(const std::string &doc, size_t start_pos, const std::string &valid_chars)
Definition: Helper.cpp:80
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:475
Definition: Open3DViewer.h:29
std::string & RightStripString(std::string &str, const std::string &chars)
Definition: Helper.cpp:63
std::size_t operator()(T const &matrix) const
Definition: Helper.h:93
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:720
std::string & StripString(std::string &str, const std::string &chars)
Definition: Helper.cpp:68
void Sleep(int milliseconds)
Definition: Helper.cpp:101
std::size_t operator()(T t) const
Definition: Helper.h:112
size_t operator()(TT const &tt) const
Definition: Helper.h:51
std::string ToLower(const std::string &str)
Convet string to the lower case.
Definition: Helper.cpp:72
std::string & LeftStripString(std::string &str, const std::string &chars)
Definition: Helper.cpp:58