Open3D (C++ API)  0.11.0
TensorListMap.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/TensorList.h"
33 
34 namespace open3d {
35 namespace t {
36 namespace geometry {
37 
44 class TensorListMap : public std::unordered_map<std::string, core::TensorList> {
45 public:
47  TensorListMap(const std::string& primary_key)
48  : std::unordered_map<std::string, core::TensorList>(),
49  primary_key_(primary_key) {}
50 
52  TensorListMap(const std::string& primary_key,
53  const std::unordered_map<std::string, core::TensorList>&
54  map_keys_to_tensorlists)
55  : std::unordered_map<std::string, core::TensorList>(),
56  primary_key_(primary_key) {
57  Assign(map_keys_to_tensorlists);
58  }
59 
63  TensorListMap() : TensorListMap("Undefined") {
64  utility::LogError("Please construct TensorListMap with a primary key.");
65  }
66 
73  void Assign(const std::unordered_map<std::string, core::TensorList>&
74  map_keys_to_tensorlists);
75 
83  const std::unordered_map<std::string, core::Tensor>&
84  map_keys_to_tensors);
85 
87  std::string GetPrimaryKey() const { return primary_key_; }
88 
90  bool IsSizeSynchronized() const;
91 
93  void AssertSizeSynchronized() const;
94 
97  bool Contains(const std::string& key) const { return count(key) != 0; }
98 
99 private:
105  void AssertTensorMapSameKeys(
106  const std::unordered_map<std::string, core::Tensor>&
107  map_keys_to_tensors) const;
108 
114  void AssertTensorMapSameDevice(
115  const std::unordered_map<std::string, core::Tensor>&
116  map_keys_to_tensors) const;
117 
119  int64_t GetPrimarySize() const { return at(primary_key_).GetSize(); }
120 
122  core::Device GetPrimaryDevice() const {
123  return at(primary_key_).GetDevice();
124  }
125 
129  std::string primary_key_;
130 };
131 
132 } // namespace geometry
133 } // namespace t
134 } // namespace open3d
void SynchronizedPushBack(const std::unordered_map< std::string, core::Tensor > &map_keys_to_tensors)
Definition: TensorListMap.cpp:89
bool IsSizeSynchronized() const
Returns true if all tensorlists in the map have the same size.
Definition: TensorListMap.cpp:64
std::string GetPrimaryKey() const
Returns the primary key of the tensorlistmap.
Definition: TensorListMap.h:87
TensorListMap()
Definition: TensorListMap.h:63
void Assign(const std::unordered_map< std::string, core::TensorList > &map_keys_to_tensorlists)
Definition: TensorListMap.cpp:41
void AssertSizeSynchronized() const
Assert IsSizeSynchronized().
Definition: TensorListMap.cpp:73
void LogError(const char *format, const Args &... args)
Definition: Console.h:176
Definition: Optional.h:922
TensorListMap(const std::string &primary_key)
Create empty TensorListMap and set primary key.
Definition: TensorListMap.h:47
Definition: TensorListMap.h:44
Definition: Device.h:39
int count
Definition: FilePCD.cpp:61
Definition: PinholeCameraIntrinsic.cpp:35
TensorListMap(const std::string &primary_key, const std::unordered_map< std::string, core::TensorList > &map_keys_to_tensorlists)
Create TensorListMap with pre-populated values.
Definition: TensorListMap.h:52
bool Contains(const std::string &key) const
Definition: TensorListMap.h:97