Open3D (C++ API)  0.11.0
TensorList.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 <cstddef>
30 #include <memory>
31 #include <string>
32 
33 #include "open3d/core/Blob.h"
34 #include "open3d/core/Device.h"
35 #include "open3d/core/Dtype.h"
36 #include "open3d/core/ShapeUtil.h"
37 #include "open3d/core/SizeVector.h"
38 #include "open3d/core/Tensor.h"
39 #include "open3d/core/TensorKey.h"
40 
41 namespace open3d {
42 namespace core {
43 
58 class TensorList {
59 public:
62 
69  TensorList(const SizeVector& element_shape,
70  Dtype dtype,
71  const Device& device = Device("CPU:0"))
72  : element_shape_(element_shape),
73  size_(0),
76  dtype,
77  device) {}
78 
84  TensorList(const std::vector<Tensor>& tensors)
85  : TensorList(tensors.begin(), tensors.end()) {}
86 
92  TensorList(const std::initializer_list<Tensor>& tensors)
93  : TensorList(tensors.begin(), tensors.end()) {}
94 
100  template <class InputIterator>
101  TensorList(InputIterator begin, InputIterator end) {
102  int64_t size = std::distance(begin, end);
103  if (size == 0) {
105  "Empty input tensors cannot initialize a tensorlist.");
106  }
107 
108  // Set size_ and reserved_size_.
109  size_ = size;
111 
112  // Check shape consistency and set element_shape_.
113  element_shape_ = begin->GetShape();
114  std::for_each(begin, end, [&](const Tensor& tensor) -> void {
115  if (tensor.GetShape() != element_shape_) {
117  "Tensors must have the same shape {}, but got {}.",
118  element_shape_, tensor.GetShape());
119  }
120  });
121 
122  // Check dtype consistency.
123  Dtype dtype = begin->GetDtype();
124  std::for_each(begin, end, [&](const Tensor& tensor) -> void {
125  if (tensor.GetDtype() != dtype) {
127  "Tensors must have the same dtype {}, but got {}.",
128  dtype.ToString(), tensor.GetDtype().ToString());
129  }
130  });
131 
132  // Check device consistency.
133  Device device = begin->GetDevice();
134  std::for_each(begin, end, [&](const Tensor& tensor) -> void {
135  if (tensor.GetDevice() != device) {
137  "Tensors must have the same device {}, but got {}.",
138  device.ToString(), tensor.GetDevice().ToString());
139  }
140  });
141 
142  // Construct internal tensor.
145  dtype, device);
146  size_t i = 0;
147  for (auto iter = begin; iter != end; ++iter, ++i) {
148  internal_tensor_[i] = *iter;
149  }
150  }
151 
165  static TensorList FromTensor(const Tensor& tensor, bool inplace = false);
166 
169  TensorList(const TensorList& other) = default;
170 
173  TensorList(TensorList&& other) = default;
174 
177  TensorList& operator=(const TensorList& other) & = default;
178 
181  TensorList& operator=(TensorList&& other) & = default;
182 
186  void CopyFrom(const TensorList& other);
187 
190  void ShallowCopyFrom(const TensorList& other);
191 
194  TensorList Copy() const;
195 
197  Tensor AsTensor() const;
198 
203  void Resize(int64_t new_size);
204 
211  void PushBack(const Tensor& tensor);
212 
217  void Extend(const TensorList& other);
218 
222  static TensorList Concatenate(const TensorList& a, const TensorList& b);
223 
225  TensorList operator+(const TensorList& other) const {
226  return Concatenate(*this, other);
227  }
228 
232  Extend(other);
233  return *this;
234  }
235 
238  Tensor operator[](int64_t index) const;
239 
242  void Clear();
243 
244  std::string ToString() const;
245 
247 
248  void AssertElementShape(const SizeVector& expected_element_shape) const {
249  if (expected_element_shape != element_shape_) {
251  "TensorList has element shape {}, but is expected to have "
252  "element shape {}.",
253  element_shape_, expected_element_shape);
254  }
255  }
256 
257  void AssertDevice(const Device& expected_device) const {
258  if (GetDevice() != expected_device) {
260  "TensorList has device {}, but is expected to be {}.",
261  GetDevice().ToString(), expected_device.ToString());
262  }
263  }
264 
266 
267  Dtype GetDtype() const { return internal_tensor_.GetDtype(); }
268 
269  int64_t GetSize() const { return size_; }
270 
271  int64_t GetReservedSize() const { return reserved_size_; }
272 
273  const Tensor& GetInternalTensor() const { return internal_tensor_; }
274 
275  bool IsResizable() const { return is_resizable_; }
276 
277 protected:
279  TensorList(const SizeVector element_shape,
280  int64_t size,
281  int64_t reserved_size,
282  const Tensor& internal_tensor,
283  bool is_resizable)
284  : element_shape_(element_shape),
285  size_(size),
286  reserved_size_(reserved_size),
287  internal_tensor_(internal_tensor),
288  is_resizable_(is_resizable) {}
289 
298  void ResizeWithExpand(int64_t new_size);
299 
302  static int64_t ComputeReserveSize(int64_t size);
303 
304 protected:
307 
311  int64_t size_ = 0;
312 
322  int64_t reserved_size_ = 0;
323 
326 
330  bool is_resizable_ = true;
331 };
332 } // namespace core
333 } // namespace open3d
static TensorList FromTensor(const Tensor &tensor, bool inplace=false)
Definition: TensorList.cpp:47
SizeVector GetElementShape() const
Definition: TensorList.h:246
bool is_resizable_
Definition: TensorList.h:330
TensorList & operator=(const TensorList &other) &=default
int64_t reserved_size_
Definition: TensorList.h:322
int64_t GetSize() const
Definition: TensorList.h:269
const Tensor & GetInternalTensor() const
Definition: TensorList.h:273
Definition: Dtype.h:39
void LogError(const char *format, const Args &... args)
Definition: Console.h:176
TensorList()
Useful to support operator[] in a map.
Definition: TensorList.h:61
Tensor AsTensor() const
Return the reference of the contained valid tensors with shared memory.
Definition: TensorList.cpp:95
void CopyFrom(const TensorList &other)
Definition: TensorList.cpp:81
Dtype GetDtype() const
Definition: TensorList.h:267
TensorList(InputIterator begin, InputIterator end)
Definition: TensorList.h:101
std::string ToString() const
Definition: Dtype.h:77
Definition: TensorList.h:58
Device GetDevice() const
Definition: Tensor.cpp:944
Definition: SizeVector.h:41
SizeVector Concat(const SizeVector &l_shape, const SizeVector &r_shape)
Concatenate two shapes.
Definition: ShapeUtil.cpp:218
TensorList(const SizeVector element_shape, int64_t size, int64_t reserved_size, const Tensor &internal_tensor, bool is_resizable)
Fully specified constructor.
Definition: TensorList.h:279
Dtype GetDtype() const
Definition: Tensor.h:943
int size
Definition: FilePCD.cpp:59
TensorList(const SizeVector &element_shape, Dtype dtype, const Device &device=Device("CPU:0"))
Definition: TensorList.h:69
TensorList Copy() const
Definition: TensorList.cpp:75
static TensorList Concatenate(const TensorList &a, const TensorList &b)
Definition: TensorList.cpp:156
static int64_t ComputeReserveSize(int64_t size)
Definition: TensorList.cpp:191
Device GetDevice() const
Definition: TensorList.h:265
TensorList(const std::vector< Tensor > &tensors)
Definition: TensorList.h:84
TensorList operator+(const TensorList &other) const
Concatenate two tensorlists.
Definition: TensorList.h:225
Definition: Device.h:39
Tensor internal_tensor_
The internal tensor for data storage.
Definition: TensorList.h:325
TensorList(const std::initializer_list< Tensor > &tensors)
Definition: TensorList.h:92
bool IsResizable() const
Definition: TensorList.h:275
TensorList & operator+=(const TensorList &other)
Definition: TensorList.h:231
void ShallowCopyFrom(const TensorList &other)
Definition: TensorList.cpp:89
void AssertDevice(const Device &expected_device) const
Definition: TensorList.h:257
static const Dtype Float32
Definition: Dtype.h:42
void AssertElementShape(const SizeVector &expected_element_shape) const
Definition: TensorList.h:248
SizeVector GetShape() const
Definition: Tensor.h:923
void ResizeWithExpand(int64_t new_size)
Definition: TensorList.cpp:175
Definition: PinholeCameraIntrinsic.cpp:35
Definition: Tensor.h:48
void PushBack(const Tensor &tensor)
Definition: TensorList.cpp:108
SizeVector element_shape_
The shape for each element tensor in the tensorlist.
Definition: TensorList.h:306
int64_t size_
Definition: TensorList.h:311
void Extend(const TensorList &other)
Definition: TensorList.cpp:129
int64_t GetReservedSize() const
Definition: TensorList.h:271
void Clear()
Definition: TensorList.cpp:169
std::string ToString() const
Definition: Device.h:73
std::string ToString() const
Definition: TensorList.cpp:220
Tensor operator[](int64_t index) const
Definition: TensorList.cpp:163
void Resize(int64_t new_size)
Definition: TensorList.cpp:99