Loading [MathJax]/extensions/TeX/AMSsymbols.js
Open3D (C++ API)  0.14.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TensorInit.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-2021 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 <initializer_list>
30 #include <utility>
31 
32 #include "open3d/core/SizeVector.h"
33 
34 namespace open3d {
35 namespace core {
36 namespace tensor_init {
37 
38 // Conventions used in this file:
39 // T: scalar value type
40 // D: dimension of type size_t
41 // L: (nested) initializer_list, or a scalar value (0-d nested)
42 
43 template <typename T, size_t D>
45  using type = std::initializer_list<
46  typename NestedInitializerImpl<T, D - 1>::type>;
47 };
48 
49 template <typename T>
50 struct NestedInitializerImpl<T, 0> {
51  using type = T;
52 };
53 
54 template <typename T, size_t D>
56 
57 template <typename L>
59  static constexpr size_t value = 0;
60 };
61 
62 template <typename L>
63 struct InitializerDim<std::initializer_list<L>> {
64  static constexpr size_t value = 1 + InitializerDim<L>::value;
65 };
66 
67 template <size_t D>
69  template <typename L>
70  static constexpr size_t value(const L& list) {
71  if (list.size() == 0) {
72  return 0;
73  }
74  size_t dim = InitializerShapeImpl<D - 1>::value(*list.begin());
75  for (const auto& value : list) {
76  if (dim != InitializerShapeImpl<D - 1>::value(value)) {
78  "Input contains ragged nested sequences"
79  "(nested lists with unequal sizes or shapes).");
80  }
81  }
82  return dim;
83  }
84 };
85 
86 template <>
88  template <typename L>
89  static constexpr size_t value(const L& list) {
90  return list.size();
91  }
92 };
93 
94 template <typename L, size_t... D>
95 SizeVector InitializerShape(const L& list, std::index_sequence<D...>) {
96  return SizeVector{
97  static_cast<int64_t>(InitializerShapeImpl<D>::value(list))...};
98 }
99 
100 template <typename L>
101 SizeVector InferShape(const L& list) {
102  SizeVector shape = InitializerShape<L>(
103  list, std::make_index_sequence<InitializerDim<L>::value>());
104  // Handle 0-dimensional inputs.
105  size_t last_dim = 0;
106  while (shape.size() > (last_dim + 1) && shape[last_dim] != 0) {
107  last_dim++;
108  }
109  shape.resize(last_dim + 1);
110  return shape;
111 }
112 
113 template <typename T, typename L>
114 void NestedCopy(T&& iter, const L& list) {
115  *iter++ = list;
116 }
117 
118 template <typename T, typename L>
119 void NestedCopy(T&& iter, const std::initializer_list<L>& list) {
120  for (const auto& value : list) {
121  NestedCopy(std::forward<T>(iter), value);
122  }
123 }
124 
125 template <typename T, size_t D>
126 std::vector<T> ToFlatVector(
127  const SizeVector& shape,
128  const tensor_init::NestedInitializerList<T, D>& nested_list) {
129  std::vector<T> values(shape.NumElements());
130  tensor_init::NestedCopy(values.begin(), nested_list);
131  return values;
132 }
133 
134 } // namespace tensor_init
135 } // namespace core
136 } // namespace open3d
static constexpr size_t value(const L &list)
Definition: TensorInit.h:89
std::vector< T > ToFlatVector(const SizeVector &shape, const tensor_init::NestedInitializerList< T, D > &nested_list)
Definition: TensorInit.h:126
std::initializer_list< typename NestedInitializerImpl< T, D - 1 >::type > type
Definition: TensorInit.h:46
Definition: Device.h:138
SizeVector InitializerShape(const L &list, std::index_sequence< D... >)
Definition: TensorInit.h:95
Definition: SizeVector.h:79
static constexpr size_t value(const L &list)
Definition: TensorInit.h:70
typename NestedInitializerImpl< T, D >::type NestedInitializerList
Definition: TensorInit.h:55
SizeVector InferShape(const L &list)
Definition: TensorInit.h:101
int64_t NumElements() const
Definition: SizeVector.cpp:126
Definition: PinholeCameraIntrinsic.cpp:35
void NestedCopy(T &&iter, const L &list)
Definition: TensorInit.h:114
#define LogError(...)
Definition: Logging.h:72