Open3D (C++ API)  0.18.0+252c867
TensorInit.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <initializer_list>
11 #include <utility>
12 
13 #include "open3d/core/SizeVector.h"
14 
15 namespace open3d {
16 namespace core {
17 namespace tensor_init {
18 
19 // Conventions used in this file:
20 // T: scalar value type
21 // D: dimension of type size_t
22 // L: (nested) initializer_list, or a scalar value (0-d nested)
23 
24 template <typename T, size_t D>
26  using type = std::initializer_list<
27  typename NestedInitializerImpl<T, D - 1>::type>;
28 };
29 
30 template <typename T>
31 struct NestedInitializerImpl<T, 0> {
32  using type = T;
33 };
34 
35 template <typename T, size_t D>
37 
38 template <typename L>
40  static constexpr size_t value = 0;
41 };
42 
43 template <typename L>
44 struct InitializerDim<std::initializer_list<L>> {
45  static constexpr size_t value = 1 + InitializerDim<L>::value;
46 };
47 
48 template <size_t D>
50  template <typename L>
51  static constexpr size_t value(const L& list) {
52  if (list.size() == 0) {
53  return 0;
54  }
55  size_t dim = InitializerShapeImpl<D - 1>::value(*list.begin());
56  for (const auto& value : list) {
59  "Input contains ragged nested sequences"
60  "(nested lists with unequal sizes or shapes).");
61  }
62  }
63  return dim;
64  }
65 };
66 
67 template <>
69  template <typename L>
70  static constexpr size_t value(const L& list) {
71  return list.size();
72  }
73 };
74 
75 template <typename L, size_t... D>
76 SizeVector InitializerShape(const L& list, std::index_sequence<D...>) {
77  return SizeVector{
78  static_cast<int64_t>(InitializerShapeImpl<D>::value(list))...};
79 }
80 
81 template <typename L>
82 SizeVector InferShape(const L& list) {
83  SizeVector shape = InitializerShape<L>(
84  list, std::make_index_sequence<InitializerDim<L>::value>());
85  // Handle 0-dimensional inputs.
86  size_t last_dim = 0;
87  while (shape.size() > (last_dim + 1) && shape[last_dim] != 0) {
88  last_dim++;
89  }
90  shape.resize(last_dim + 1);
91  return shape;
92 }
93 
94 template <typename T, typename L>
95 void NestedCopy(T&& iter, const L& list) {
96  *iter++ = list;
97 }
98 
99 template <typename T, typename L>
100 void NestedCopy(T&& iter, const std::initializer_list<L>& list) {
101  for (const auto& value : list) {
102  NestedCopy(std::forward<T>(iter), value);
103  }
104 }
105 
106 template <typename T, size_t D>
107 std::vector<T> ToFlatVector(
108  const SizeVector& shape,
109  const tensor_init::NestedInitializerList<T, D>& nested_list) {
110  std::vector<T> values(shape.NumElements());
111  tensor_init::NestedCopy(values.begin(), nested_list);
112  return values;
113 }
114 
115 } // namespace tensor_init
116 } // namespace core
117 } // namespace open3d
#define LogError(...)
Definition: Logging.h:48
Definition: SizeVector.h:69
int64_t NumElements() const
Definition: SizeVector.cpp:108
size_t size() const
Definition: SmallVector.h:119
void resize(size_type N)
Definition: SmallVector.h:678
void NestedCopy(T &&iter, const L &list)
Definition: TensorInit.h:95
SizeVector InferShape(const L &list)
Definition: TensorInit.h:82
typename NestedInitializerImpl< T, D >::type NestedInitializerList
Definition: TensorInit.h:36
std::vector< T > ToFlatVector(const SizeVector &shape, const tensor_init::NestedInitializerList< T, D > &nested_list)
Definition: TensorInit.h:107
SizeVector InitializerShape(const L &list, std::index_sequence< D... >)
Definition: TensorInit.h:76
Definition: PinholeCameraIntrinsic.cpp:16
Definition: Device.h:107
static constexpr size_t value
Definition: TensorInit.h:40
static constexpr size_t value(const L &list)
Definition: TensorInit.h:70
static constexpr size_t value(const L &list)
Definition: TensorInit.h:51
std::initializer_list< typename NestedInitializerImpl< T, D - 1 >::type > type
Definition: TensorInit.h:27