Open3D (C++ API)  0.17.0
Helper.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 <cmath>
11 #include <cstdlib>
12 #include <functional>
13 #include <memory>
14 #include <random>
15 #include <stdexcept>
16 #include <string>
17 #include <tuple>
18 #include <vector>
19 
20 namespace open3d {
21 namespace utility {
22 
30 
31 template <typename TT>
32 struct hash_tuple {
33  size_t operator()(TT const& tt) const { return std::hash<TT>()(tt); }
34 };
35 
36 namespace {
37 
38 template <class T>
39 inline void hash_combine(std::size_t& hash_seed, T const& v) {
40  hash_seed ^= std::hash<T>()(v) + 0x9e3779b9 + (hash_seed << 6) +
41  (hash_seed >> 2);
42 }
43 
44 template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1>
45 struct HashValueImpl {
46  static void apply(size_t& hash_seed, Tuple const& tuple) {
47  HashValueImpl<Tuple, Index - 1>::apply(hash_seed, tuple);
48  hash_combine(hash_seed, std::get<Index>(tuple));
49  }
50 };
51 
52 template <class Tuple>
53 struct HashValueImpl<Tuple, 0> {
54  static void apply(size_t& hash_seed, Tuple const& tuple) {
55  hash_combine(hash_seed, std::get<0>(tuple));
56  }
57 };
58 
59 } // unnamed namespace
60 
61 template <typename... TT>
62 struct hash_tuple<std::tuple<TT...>> {
63  size_t operator()(std::tuple<TT...> const& tt) const {
64  size_t hash_seed = 0;
65  HashValueImpl<std::tuple<TT...>>::apply(hash_seed, tt);
66  return hash_seed;
67  }
68 };
69 
70 template <typename T>
71 struct hash_eigen {
72  std::size_t operator()(T const& matrix) const {
73  size_t hash_seed = 0;
74  for (int i = 0; i < (int)matrix.size(); i++) {
75  auto elem = *(matrix.data() + i);
76  hash_seed ^= std::hash<typename T::Scalar>()(elem) + 0x9e3779b9 +
77  (hash_seed << 6) + (hash_seed >> 2);
78  }
79  return hash_seed;
80  }
81 };
82 
83 // Hash function for enum class for C++ standard less than C++14
84 // https://stackoverflow.com/a/24847480/1255535
86  template <typename T>
87  std::size_t operator()(T t) const {
88  return static_cast<std::size_t>(t);
89  }
90 };
91 
94 std::vector<std::string> SplitString(const std::string& str,
95  const std::string& delimiters = " ",
96  bool trim_empty_str = true);
97 
101 bool ContainsString(const std::string& src, const std::string& dst);
102 
106 bool StringStartsWith(const std::string& src, const std::string& tar);
107 
111 bool StringEndsWith(const std::string& src, const std::string& tar);
112 
113 std::string JoinStrings(const std::vector<std::string>& strs,
114  const std::string& delimiter = ", ");
115 
119 size_t WordLength(const std::string& doc,
120  size_t start_pos,
121  const std::string& valid_chars = "_");
122 
123 std::string& LeftStripString(std::string& str,
124  const std::string& chars = "\t\n\v\f\r ");
125 
126 std::string& RightStripString(std::string& str,
127  const std::string& chars = "\t\n\v\f\r ");
128 
131 std::string& StripString(std::string& str,
132  const std::string& chars = "\t\n\v\f\r ");
133 
135 std::string ToLower(const std::string& s);
136 
138 std::string ToUpper(const std::string& s);
139 
141 template <typename... Args>
142 inline std::string FormatString(const std::string& format, Args... args) {
143  int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) +
144  1; // Extra space for '\0'
145  if (size_s <= 0) {
146  throw std::runtime_error("Error during formatting.");
147  }
148  auto size = static_cast<size_t>(size_s);
149  auto buf = std::make_unique<char[]>(size);
150  std::snprintf(buf.get(), size, format.c_str(), args...);
151  return std::string(buf.get(),
152  buf.get() + size - 1); // We don't want the '\0' inside
153 };
154 
155 void Sleep(int milliseconds);
156 
158 inline int DivUp(int x, int y) {
159  div_t tmp = std::div(x, y);
160  return tmp.quot + (tmp.rem != 0 ? 1 : 0);
161 }
162 
164 std::string GetCurrentTimeStamp();
165 
166 } // namespace utility
167 } // namespace open3d
filament::Texture::InternalFormat format
Definition: FilamentResourceManager.cpp:195
int size
Definition: FilePCD.cpp:40
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:719
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:474
int DivUp(int x, int y)
Computes the quotient of x/y with rounding up.
Definition: Helper.h:158
std::string ToLower(const std::string &str)
Convert string to the lower case.
Definition: Helper.cpp:83
bool ContainsString(const std::string &src, const std::string &dst)
Definition: Helper.cpp:42
bool StringEndsWith(const std::string &src, const std::string &tar)
Definition: Helper.cpp:51
void Sleep(int milliseconds)
Definition: Helper.cpp:119
std::string GetCurrentTimeStamp()
Returns current time stamp.
Definition: Helper.cpp:127
std::string JoinStrings(const std::vector< std::string > &strs, const std::string &delimiter)
Definition: Helper.cpp:57
std::string & LeftStripString(std::string &str, const std::string &chars)
Definition: Helper.cpp:69
std::string & RightStripString(std::string &str, const std::string &chars)
Definition: Helper.cpp:74
bool StringStartsWith(const std::string &src, const std::string &tar)
Definition: Helper.cpp:46
std::string & StripString(std::string &str, const std::string &chars)
Definition: Helper.cpp:79
std::string ToUpper(const std::string &str)
Convert string to the upper case.
Definition: Helper.cpp:90
std::string FormatString(const std::string &format, Args... args)
Format string.
Definition: Helper.h:142
std::vector< std::string > SplitString(const std::string &str, const std::string &delimiters, bool trim_empty_str)
Definition: Helper.cpp:26
size_t WordLength(const std::string &doc, size_t start_pos, const std::string &valid_chars)
Definition: Helper.cpp:98
Definition: PinholeCameraIntrinsic.cpp:16
Definition: Device.h:107
Definition: Helper.h:71
std::size_t operator()(T const &matrix) const
Definition: Helper.h:72
Definition: Helper.h:85
std::size_t operator()(T t) const
Definition: Helper.h:87
size_t operator()(std::tuple< TT... > const &tt) const
Definition: Helper.h:63
Definition: Helper.h:32
size_t operator()(TT const &tt) const
Definition: Helper.h:33