Open3D (C++ API)  0.16.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-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 <memory>
33#include <random>
34#include <stdexcept>
35#include <string>
36#include <tuple>
37#include <vector>
38
39namespace open3d {
40namespace utility {
41
49
50template <typename TT>
51struct hash_tuple {
52 size_t operator()(TT const& tt) const { return std::hash<TT>()(tt); }
53};
54
55namespace {
56
57template <class T>
58inline void hash_combine(std::size_t& hash_seed, T const& v) {
59 hash_seed ^= std::hash<T>()(v) + 0x9e3779b9 + (hash_seed << 6) +
60 (hash_seed >> 2);
61}
62
63template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1>
64struct HashValueImpl {
65 static void apply(size_t& hash_seed, Tuple const& tuple) {
66 HashValueImpl<Tuple, Index - 1>::apply(hash_seed, tuple);
67 hash_combine(hash_seed, std::get<Index>(tuple));
68 }
69};
70
71template <class Tuple>
72struct HashValueImpl<Tuple, 0> {
73 static void apply(size_t& hash_seed, Tuple const& tuple) {
74 hash_combine(hash_seed, std::get<0>(tuple));
75 }
76};
77
78} // unnamed namespace
79
80template <typename... TT>
81struct hash_tuple<std::tuple<TT...>> {
82 size_t operator()(std::tuple<TT...> const& tt) const {
83 size_t hash_seed = 0;
84 HashValueImpl<std::tuple<TT...>>::apply(hash_seed, tt);
85 return hash_seed;
86 }
87};
88
89template <typename T>
90struct hash_eigen {
91 std::size_t operator()(T const& matrix) const {
92 size_t hash_seed = 0;
93 for (int i = 0; i < (int)matrix.size(); i++) {
94 auto elem = *(matrix.data() + i);
95 hash_seed ^= std::hash<typename T::Scalar>()(elem) + 0x9e3779b9 +
96 (hash_seed << 6) + (hash_seed >> 2);
97 }
98 return hash_seed;
99 }
100};
101
102// Hash function for enum class for C++ standard less than C++14
103// https://stackoverflow.com/a/24847480/1255535
105 template <typename T>
107 return static_cast<std::size_t>(t);
108 }
109};
110
113std::vector<std::string> SplitString(const std::string& str,
114 const std::string& delimiters = " ",
115 bool trim_empty_str = true);
116
120bool ContainsString(const std::string& src, const std::string& dst);
121
125bool StringStartsWith(const std::string& src, const std::string& tar);
126
130bool StringEndsWith(const std::string& src, const std::string& tar);
131
132std::string JoinStrings(const std::vector<std::string>& strs,
133 const std::string& delimiter = ", ");
134
138size_t WordLength(const std::string& doc,
139 size_t start_pos,
140 const std::string& valid_chars = "_");
141
142std::string& LeftStripString(std::string& str,
143 const std::string& chars = "\t\n\v\f\r ");
144
145std::string& RightStripString(std::string& str,
146 const std::string& chars = "\t\n\v\f\r ");
147
150std::string& StripString(std::string& str,
151 const std::string& chars = "\t\n\v\f\r ");
152
154std::string ToLower(const std::string& s);
155
157std::string ToUpper(const std::string& s);
158
160template <typename... Args>
161inline std::string FormatString(const std::string& format, Args... args) {
162 int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) +
163 1; // Extra space for '\0'
164 if (size_s <= 0) {
165 throw std::runtime_error("Error during formatting.");
166 }
167 auto size = static_cast<size_t>(size_s);
168 auto buf = std::make_unique<char[]>(size);
169 std::snprintf(buf.get(), size, format.c_str(), args...);
170 return std::string(buf.get(),
171 buf.get() + size - 1); // We don't want the '\0' inside
172};
173
174void Sleep(int milliseconds);
175
177inline int DivUp(int x, int y) {
178 div_t tmp = std::div(x, y);
179 return tmp.quot + (tmp.rem != 0 ? 1 : 0);
180}
181
183std::string GetCurrentTimeStamp();
184
185} // namespace utility
186} // namespace open3d
filament::Texture::InternalFormat format
Definition: FilamentResourceManager.cpp:214
int size
Definition: FilePCD.cpp:59
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:738
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:493
int DivUp(int x, int y)
Computes the quotient of x/y with rounding up.
Definition: Helper.h:177
std::string ToLower(const std::string &str)
Convert string to the lower case.
Definition: Helper.cpp:102
bool ContainsString(const std::string &src, const std::string &dst)
Definition: Helper.cpp:61
bool StringEndsWith(const std::string &src, const std::string &tar)
Definition: Helper.cpp:70
void Sleep(int milliseconds)
Definition: Helper.cpp:138
std::string GetCurrentTimeStamp()
Returns current time stamp.
Definition: Helper.cpp:146
std::string JoinStrings(const std::vector< std::string > &strs, const std::string &delimiter)
Definition: Helper.cpp:76
std::string & LeftStripString(std::string &str, const std::string &chars)
Definition: Helper.cpp:88
std::string & RightStripString(std::string &str, const std::string &chars)
Definition: Helper.cpp:93
bool StringStartsWith(const std::string &src, const std::string &tar)
Definition: Helper.cpp:65
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
std::string FormatString(const std::string &format, Args... args)
Format string.
Definition: Helper.h:161
std::vector< std::string > SplitString(const std::string &str, const std::string &delimiters, bool trim_empty_str)
Definition: Helper.cpp:45
size_t WordLength(const std::string &doc, size_t start_pos, const std::string &valid_chars)
Definition: Helper.cpp:117
Definition: PinholeCameraIntrinsic.cpp:35
Definition: Device.h:126
Definition: Helper.h:90
std::size_t operator()(T const &matrix) const
Definition: Helper.h:91
Definition: Helper.h:104
std::size_t operator()(T t) const
Definition: Helper.h:106
size_t operator()(std::tuple< TT... > const &tt) const
Definition: Helper.h:82
Definition: Helper.h:51
size_t operator()(TT const &tt) const
Definition: Helper.h:52