Open3D (C++ API)  0.16.1
TensorMap.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 <string>
30#include <unordered_map>
31#include <unordered_set>
32
33#include "open3d/core/Tensor.h"
34
35namespace open3d {
36namespace t {
37namespace geometry {
38
50class TensorMap : public std::unordered_map<std::string, core::Tensor> {
51public:
53 explicit TensorMap(const std::string& primary_key)
54 : std::unordered_map<std::string, core::Tensor>(),
55 primary_key_(primary_key) {
56 AssertPrimaryKeyInMapOrEmpty();
57 AssertNoReservedKeys();
58 }
59
63 explicit TensorMap() : TensorMap("Undefined") {
64 utility::LogError("Please construct TensorMap with a primary key.");
65 }
66
67 template <class InputIt>
68 TensorMap(const std::string& primary_key, InputIt first, InputIt last)
69 : std::unordered_map<std::string, core::Tensor>(first, last),
70 primary_key_(primary_key) {
71 AssertPrimaryKeyInMapOrEmpty();
72 AssertNoReservedKeys();
73 }
74
75 TensorMap(const std::string& primary_key,
76 const std::unordered_map<std::string, core::Tensor>& tensor_map)
77 : TensorMap(primary_key, tensor_map.begin(), tensor_map.end()) {
78 AssertPrimaryKeyInMapOrEmpty();
79 AssertNoReservedKeys();
80 }
81
82 TensorMap(const std::string& primary_key,
83 std::initializer_list<value_type> init)
84 : std::unordered_map<std::string, core::Tensor>(init),
85 primary_key_(primary_key) {
86 AssertPrimaryKeyInMapOrEmpty();
87 AssertNoReservedKeys();
88 }
89
91 TensorMap(const TensorMap& other)
92 : std::unordered_map<std::string, core::Tensor>(other),
93 primary_key_(other.primary_key_) {
94 AssertPrimaryKeyInMapOrEmpty();
95 AssertNoReservedKeys();
96 }
97
100 : std::unordered_map<std::string, core::Tensor>(other),
101 primary_key_(other.primary_key_) {
102 AssertPrimaryKeyInMapOrEmpty();
103 AssertNoReservedKeys();
104 }
105
111 std::size_t Erase(const std::string key) {
112 if (key == primary_key_) {
113 utility::LogError("Primary key \"{}\" cannot be deleted.",
114 primary_key_);
115 } else if (!Contains(key)) {
116 utility::LogWarning("Key \"{}\" is not present.", key);
117 }
118 return this->erase(key);
119 }
120
121 std::pair<iterator, bool> insert(const value_type& value) {
122 if (GetReservedKeys().count(value.first)) {
123 utility::LogError("Key \"{}\" is reserved.", value.first);
124 }
125 return std::unordered_map<std::string, core::Tensor>::insert(value);
126 }
127
128 template <class P>
129 std::pair<iterator, bool> insert(P&& value) {
130 if (GetReservedKeys().count(value.first)) {
131 utility::LogError("Key \"{}\" is reserved.", value.first);
132 }
133 return std::unordered_map<std::string, core::Tensor>::insert(
134 std::forward<P>(value));
135 }
136
137 iterator insert(const_iterator hint, const value_type& value) {
138 if (GetReservedKeys().count(value.first)) {
139 utility::LogError("Key \"{}\" is reserved.", value.first);
140 }
141 return std::unordered_map<std::string, core::Tensor>::insert(hint,
142 value);
143 }
144
145 template <class P>
146 iterator insert(const_iterator hint, P&& value) {
147 if (GetReservedKeys().count(value.first)) {
148 utility::LogError("Key \"{}\" is reserved.", value.first);
149 }
150 return std::unordered_map<std::string, core::Tensor>::insert(
151 hint, std::forward<P>(value));
152 }
153
154 template <class InputIt>
155 void insert(InputIt first, InputIt last) {
156 for (auto it = first; it != last; ++it) {
157 if (GetReservedKeys().count(it->first)) {
158 utility::LogError("Key \"{}\" is reserved.", it->first);
159 }
160 }
161 std::unordered_map<std::string, core::Tensor>::insert(first, last);
162 }
163
164 void insert(std::initializer_list<value_type> ilist) {
165 for (auto it = ilist.begin(); it != ilist.end(); ++it) {
166 if (GetReservedKeys().count(it->first)) {
167 utility::LogError("Key \"{}\" is reserved.", it->first);
168 }
169 }
170 std::unordered_map<std::string, core::Tensor>::insert(ilist);
171 }
172
173 TensorMap& operator=(const TensorMap&) = default;
174
176
178 std::string GetPrimaryKey() const { return primary_key_; }
179
181 std::unordered_set<std::string> GetKeySet() const {
182 std::unordered_set<std::string> keys;
183 for (const auto& item : *this) {
184 keys.insert(item.first);
185 }
186 return keys;
187 }
188
190 bool IsSizeSynchronized() const;
191
193 void AssertSizeSynchronized() const;
194
197 bool IsContiguous() const;
198
202 TensorMap Contiguous() const;
203
206 bool Contains(const std::string& key) const { return count(key) != 0; }
207
209 static std::unordered_set<std::string> GetReservedKeys();
210
212 std::string ToString() const;
213
214private:
217 void AssertPrimaryKeyInMapOrEmpty() const;
218
221 void AssertNoReservedKeys() const;
222
224 int64_t GetPrimarySize() const { return at(primary_key_).GetLength(); }
225
227 core::Device GetPrimaryDevice() const {
228 return at(primary_key_).GetDevice();
229 }
230
232 std::string primary_key_;
233};
234
235} // namespace geometry
236} // namespace t
237} // namespace open3d
#define LogWarning(...)
Definition: Logging.h:79
#define LogError(...)
Definition: Logging.h:67
Definition: Device.h:37
Definition: TensorMap.h:50
TensorMap()
Definition: TensorMap.h:63
TensorMap Contiguous() const
Definition: TensorMap.cpp:94
TensorMap(TensorMap &&other)
Move constructor performs a "shallow" copy of the Tensors.
Definition: TensorMap.h:99
void AssertSizeSynchronized() const
Assert IsSizeSynchronized().
Definition: TensorMap.cpp:68
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:111
TensorMap(const std::string &primary_key)
Create empty TensorMap and set primary key.
Definition: TensorMap.h:53
void insert(InputIt first, InputIt last)
Definition: TensorMap.h:155
std::string ToString() const
Print the TensorMap to string.
Definition: TensorMap.cpp:155
TensorMap(const std::string &primary_key, std::initializer_list< value_type > init)
Definition: TensorMap.h:82
void insert(std::initializer_list< value_type > ilist)
Definition: TensorMap.h:164
TensorMap(const std::string &primary_key, InputIt first, InputIt last)
Definition: TensorMap.h:68
TensorMap(const std::string &primary_key, const std::unordered_map< std::string, core::Tensor > &tensor_map)
Definition: TensorMap.h:75
static std::unordered_set< std::string > GetReservedKeys()
Get reserved keys for the map. A map cannot contain any of these keys.
Definition: TensorMap.cpp:103
bool Contains(const std::string &key) const
Definition: TensorMap.h:206
bool IsContiguous() const
Definition: TensorMap.cpp:85
iterator insert(const_iterator hint, const value_type &value)
Definition: TensorMap.h:137
bool IsSizeSynchronized() const
Returns true if all tensors in the map have the same size.
Definition: TensorMap.cpp:41
std::unordered_set< std::string > GetKeySet() const
Returns a set with all keys.
Definition: TensorMap.h:181
TensorMap & operator=(TensorMap &&)=default
std::string GetPrimaryKey() const
Returns the primary key of the TensorMap.
Definition: TensorMap.h:178
iterator insert(const_iterator hint, P &&value)
Definition: TensorMap.h:146
TensorMap(const TensorMap &other)
Copy constructor performs a "shallow" copy of the Tensors.
Definition: TensorMap.h:91
std::pair< iterator, bool > insert(P &&value)
Definition: TensorMap.h:129
TensorMap & operator=(const TensorMap &)=default
std::pair< iterator, bool > insert(const value_type &value)
Definition: TensorMap.h:121
int count
Definition: FilePCD.cpp:61
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
Definition: PinholeCameraIntrinsic.cpp:35
Definition: Device.h:126