Open3D (C++ API)  0.18.0
TensorMap.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 <string>
11 #include <unordered_map>
12 #include <unordered_set>
13 
14 #include "open3d/core/Tensor.h"
15 
16 namespace open3d {
17 namespace t {
18 namespace geometry {
19 
31 class TensorMap : public std::unordered_map<std::string, core::Tensor> {
32 public:
34  explicit TensorMap(const std::string& primary_key)
35  : std::unordered_map<std::string, core::Tensor>(),
36  primary_key_(primary_key) {
37  AssertPrimaryKeyInMapOrEmpty();
38  AssertNoReservedKeys();
39  }
40 
44  explicit TensorMap() : TensorMap("Undefined") {
45  utility::LogError("Please construct TensorMap with a primary key.");
46  }
47 
48  template <class InputIt>
49  TensorMap(const std::string& primary_key, InputIt first, InputIt last)
50  : std::unordered_map<std::string, core::Tensor>(first, last),
51  primary_key_(primary_key) {
52  AssertPrimaryKeyInMapOrEmpty();
53  AssertNoReservedKeys();
54  }
55 
56  TensorMap(const std::string& primary_key,
57  const std::unordered_map<std::string, core::Tensor>& tensor_map)
58  : TensorMap(primary_key, tensor_map.begin(), tensor_map.end()) {
59  AssertPrimaryKeyInMapOrEmpty();
60  AssertNoReservedKeys();
61  }
62 
63  TensorMap(const std::string& primary_key,
64  std::initializer_list<value_type> init)
65  : std::unordered_map<std::string, core::Tensor>(init),
66  primary_key_(primary_key) {
67  AssertPrimaryKeyInMapOrEmpty();
68  AssertNoReservedKeys();
69  }
70 
72  TensorMap(const TensorMap& other)
73  : std::unordered_map<std::string, core::Tensor>(other),
74  primary_key_(other.primary_key_) {
75  AssertPrimaryKeyInMapOrEmpty();
76  AssertNoReservedKeys();
77  }
78 
81  : std::unordered_map<std::string, core::Tensor>(other),
82  primary_key_(other.primary_key_) {
83  AssertPrimaryKeyInMapOrEmpty();
84  AssertNoReservedKeys();
85  }
86 
92  std::size_t Erase(const std::string key) {
93  if (key == primary_key_) {
94  utility::LogError("Primary key \"{}\" cannot be deleted.",
95  primary_key_);
96  } else if (!Contains(key)) {
97  utility::LogWarning("Key \"{}\" is not present.", key);
98  }
99  return this->erase(key);
100  }
101 
102  std::pair<iterator, bool> insert(const value_type& value) {
103  if (GetReservedKeys().count(value.first)) {
104  utility::LogError("Key \"{}\" is reserved.", value.first);
105  }
106  return std::unordered_map<std::string, core::Tensor>::insert(value);
107  }
108 
109  template <class P>
110  std::pair<iterator, bool> insert(P&& value) {
111  if (GetReservedKeys().count(value.first)) {
112  utility::LogError("Key \"{}\" is reserved.", value.first);
113  }
114  return std::unordered_map<std::string, core::Tensor>::insert(
115  std::forward<P>(value));
116  }
117 
118  iterator insert(const_iterator hint, const value_type& value) {
119  if (GetReservedKeys().count(value.first)) {
120  utility::LogError("Key \"{}\" is reserved.", value.first);
121  }
122  return std::unordered_map<std::string, core::Tensor>::insert(hint,
123  value);
124  }
125 
126  template <class P>
127  iterator insert(const_iterator hint, P&& value) {
128  if (GetReservedKeys().count(value.first)) {
129  utility::LogError("Key \"{}\" is reserved.", value.first);
130  }
131  return std::unordered_map<std::string, core::Tensor>::insert(
132  hint, std::forward<P>(value));
133  }
134 
135  template <class InputIt>
136  void insert(InputIt first, InputIt last) {
137  for (auto it = first; it != last; ++it) {
138  if (GetReservedKeys().count(it->first)) {
139  utility::LogError("Key \"{}\" is reserved.", it->first);
140  }
141  }
142  std::unordered_map<std::string, core::Tensor>::insert(first, last);
143  }
144 
145  void insert(std::initializer_list<value_type> ilist) {
146  for (auto it = ilist.begin(); it != ilist.end(); ++it) {
147  if (GetReservedKeys().count(it->first)) {
148  utility::LogError("Key \"{}\" is reserved.", it->first);
149  }
150  }
151  std::unordered_map<std::string, core::Tensor>::insert(ilist);
152  }
153 
154  TensorMap& operator=(const TensorMap&) = default;
155 
157 
159  std::string GetPrimaryKey() const { return primary_key_; }
160 
162  std::unordered_set<std::string> GetKeySet() const {
163  std::unordered_set<std::string> keys;
164  for (const auto& item : *this) {
165  keys.insert(item.first);
166  }
167  return keys;
168  }
169 
171  bool IsSizeSynchronized() const;
172 
174  void AssertSizeSynchronized() const;
175 
178  bool IsContiguous() const;
179 
183  TensorMap Contiguous() const;
184 
187  bool Contains(const std::string& key) const { return count(key) != 0; }
188 
190  static std::unordered_set<std::string> GetReservedKeys();
191 
193  std::string ToString() const;
194 
195 private:
198  void AssertPrimaryKeyInMapOrEmpty() const;
199 
202  void AssertNoReservedKeys() const;
203 
205  int64_t GetPrimarySize() const { return at(primary_key_).GetLength(); }
206 
208  core::Device GetPrimaryDevice() const {
209  return at(primary_key_).GetDevice();
210  }
211 
213  std::string primary_key_;
214 };
215 
216 } // namespace geometry
217 } // namespace t
218 } // namespace open3d
#define LogWarning(...)
Definition: Logging.h:60
#define LogError(...)
Definition: Logging.h:48
Definition: Device.h:18
Definition: TensorMap.h:31
TensorMap()
Definition: TensorMap.h:44
TensorMap Contiguous() const
Definition: TensorMap.cpp:75
TensorMap(TensorMap &&other)
Move constructor performs a "shallow" copy of the Tensors.
Definition: TensorMap.h:80
void AssertSizeSynchronized() const
Assert IsSizeSynchronized().
Definition: TensorMap.cpp:49
TensorMap & operator=(const TensorMap &)=default
std::size_t Erase(const std::string key)
Erase elements for the TensorMap by key value, if the key exists. If the key does not exists,...
Definition: TensorMap.h:92
TensorMap(const std::string &primary_key)
Create empty TensorMap and set primary key.
Definition: TensorMap.h:34
void insert(InputIt first, InputIt last)
Definition: TensorMap.h:136
std::string ToString() const
Print the TensorMap to string.
Definition: TensorMap.cpp:136
TensorMap(const std::string &primary_key, std::initializer_list< value_type > init)
Definition: TensorMap.h:63
void insert(std::initializer_list< value_type > ilist)
Definition: TensorMap.h:145
TensorMap(const std::string &primary_key, InputIt first, InputIt last)
Definition: TensorMap.h:49
TensorMap(const std::string &primary_key, const std::unordered_map< std::string, core::Tensor > &tensor_map)
Definition: TensorMap.h:56
static std::unordered_set< std::string > GetReservedKeys()
Get reserved keys for the map. A map cannot contain any of these keys.
Definition: TensorMap.cpp:84
std::pair< iterator, bool > insert(const value_type &value)
Definition: TensorMap.h:102
bool Contains(const std::string &key) const
Definition: TensorMap.h:187
bool IsContiguous() const
Definition: TensorMap.cpp:66
iterator insert(const_iterator hint, const value_type &value)
Definition: TensorMap.h:118
std::pair< iterator, bool > insert(P &&value)
Definition: TensorMap.h:110
bool IsSizeSynchronized() const
Returns true if all tensors in the map have the same size.
Definition: TensorMap.cpp:22
std::string GetPrimaryKey() const
Returns the primary key of the TensorMap.
Definition: TensorMap.h:159
iterator insert(const_iterator hint, P &&value)
Definition: TensorMap.h:127
TensorMap(const TensorMap &other)
Copy constructor performs a "shallow" copy of the Tensors.
Definition: TensorMap.h:72
std::unordered_set< std::string > GetKeySet() const
Returns a set with all keys.
Definition: TensorMap.h:162
TensorMap & operator=(TensorMap &&)=default
int count
Definition: FilePCD.cpp:42
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
Definition: PinholeCameraIntrinsic.cpp:16
Definition: Device.h:107