Open3D (C++ API)  0.12.0
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 
95  TensorMap& operator=(const TensorMap&) = default;
96 
97  TensorMap& operator=(TensorMap&&) = default;
98 
100  std::string GetPrimaryKey() const { return primary_key_; }
101 
103  bool IsSizeSynchronized() const;
104 
106  void AssertSizeSynchronized() const;
107 
110  bool Contains(const std::string& key) const { return count(key) != 0; }
111 
112 private:
115  void AssertPrimaryKeyInMapOrEmpty() const;
116 
118  int64_t GetPrimarySize() const { return at(primary_key_).GetLength(); }
119 
121  core::Device GetPrimaryDevice() const {
122  return at(primary_key_).GetDevice();
123  }
124 
126  std::string primary_key_;
127 };
128 
129 } // namespace geometry
130 } // namespace t
131 } // namespace open3d
void LogError(const char *format, const Args &... args)
Definition: Console.h:176
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
TensorMap & operator=(const TensorMap &)=default
bool Contains(const std::string &key) const
Definition: TensorMap.h:110
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:100
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
TensorMap(TensorMap &&other)
Move constructor performs a "shallow" copy of the Tensors.
Definition: TensorMap.h:89
Definition: TensorMap.h:49