Open3D (C++ API)  0.13.0
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 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 
32 #include "open3d/core/Tensor.h"
33 
34 namespace open3d {
35 namespace t {
36 namespace geometry {
37 
49 class TensorMap : public std::unordered_map<std::string, core::Tensor> {
50 public:
52  explicit TensorMap(const std::string& primary_key)
53  : std::unordered_map<std::string, core::Tensor>(),
54  primary_key_(primary_key) {}
55 
59  explicit TensorMap() : TensorMap("Undefined") {
60  utility::LogError("Please construct TensorMap with a primary key.");
61  }
62 
63  template <class InputIt>
64  TensorMap(const std::string& primary_key, InputIt first, InputIt last)
65  : std::unordered_map<std::string, core::Tensor>(first, last),
66  primary_key_(primary_key) {
67  AssertPrimaryKeyInMapOrEmpty();
68  }
69 
70  TensorMap(const std::string& primary_key,
71  const std::unordered_map<std::string, core::Tensor>& tensor_map)
72  : TensorMap(primary_key, tensor_map.begin(), tensor_map.end()) {}
73 
74  TensorMap(const std::string& primary_key,
75  std::initializer_list<value_type> init)
76  : std::unordered_map<std::string, core::Tensor>(init),
77  primary_key_(primary_key) {
78  AssertPrimaryKeyInMapOrEmpty();
79  }
80 
82  TensorMap(const TensorMap& other)
83  : std::unordered_map<std::string, core::Tensor>(other),
84  primary_key_(other.primary_key_) {
85  AssertPrimaryKeyInMapOrEmpty();
86  }
87 
90  : std::unordered_map<std::string, core::Tensor>(other),
91  primary_key_(other.primary_key_) {
92  AssertPrimaryKeyInMapOrEmpty();
93  }
94 
100  std::size_t Erase(const std::string key) {
101  if (key == primary_key_) {
102  utility::LogError("Primary key: {} cannot be deleted.",
103  primary_key_);
104  } else if (!Contains(key)) {
105  utility::LogWarning("Key: {} is not present.", key);
106  }
107  return this->erase(key);
108  }
109 
110  TensorMap& operator=(const TensorMap&) = default;
111 
112  TensorMap& operator=(TensorMap&&) = default;
113 
115  std::string GetPrimaryKey() const { return primary_key_; }
116 
118  bool IsSizeSynchronized() const;
119 
121  void AssertSizeSynchronized() const;
122 
125  bool Contains(const std::string& key) const { return count(key) != 0; }
126 
127 private:
130  void AssertPrimaryKeyInMapOrEmpty() const;
131 
133  int64_t GetPrimarySize() const { return at(primary_key_).GetLength(); }
134 
136  core::Device GetPrimaryDevice() const {
137  return at(primary_key_).GetDevice();
138  }
139 
141  std::string primary_key_;
142 };
143 
144 } // namespace geometry
145 } // namespace t
146 } // namespace open3d
#define LogWarning(...)
Definition: Console.h:95
Definition: Optional.h:912
TensorMap(const std::string &primary_key, std::initializer_list< value_type > init)
Definition: TensorMap.h:74
TensorMap()
Definition: TensorMap.h:59
TensorMap(const std::string &primary_key, InputIt first, InputIt last)
Definition: TensorMap.h:64
TensorMap(const std::string &primary_key)
Create empty TensorMap and set primary key.
Definition: TensorMap.h:52
#define LogError(...)
Definition: Console.h:79
TensorMap & operator=(const TensorMap &)=default
bool Contains(const std::string &key) const
Definition: TensorMap.h:125
Definition: Device.h:39
int count
Definition: FilePCD.cpp:61
Definition: PinholeCameraIntrinsic.cpp:35
TensorMap(const std::string &primary_key, const std::unordered_map< std::string, core::Tensor > &tensor_map)
Definition: TensorMap.h:70
TensorMap(const TensorMap &other)
Copy constructor performs a "shallow" copy of the Tensors.
Definition: TensorMap.h:82
std::string GetPrimaryKey() const
Returns the primary key of the TensorMap.
Definition: TensorMap.h:115
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:724
void AssertSizeSynchronized() const
Assert IsSizeSynchronized().
Definition: TensorMap.cpp:58
bool IsSizeSynchronized() const
Returns true if all tensors in the map have the same size.
Definition: TensorMap.cpp:41
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:100
TensorMap(TensorMap &&other)
Move constructor performs a "shallow" copy of the Tensors.
Definition: TensorMap.h:89
Definition: TensorMap.h:49