Open3D (C++ API)  0.16.1
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
33
34namespace open3d {
35namespace core {
36namespace 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
43template <typename T, size_t D>
45 using type = std::initializer_list<
46 typename NestedInitializerImpl<T, D - 1>::type>;
47};
48
49template <typename T>
51 using type = T;
52};
53
54template <typename T, size_t D>
56
57template <typename L>
59 static constexpr size_t value = 0;
60};
61
62template <typename L>
63struct InitializerDim<std::initializer_list<L>> {
64 static constexpr size_t value = 1 + InitializerDim<L>::value;
65};
66
67template <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) {
78 "Input contains ragged nested sequences"
79 "(nested lists with unequal sizes or shapes).");
80 }
81 }
82 return dim;
83 }
84};
85
86template <>
88 template <typename L>
89 static constexpr size_t value(const L& list) {
90 return list.size();
91 }
92};
93
94template <typename L, size_t... D>
95SizeVector InitializerShape(const L& list, std::index_sequence<D...>) {
96 return SizeVector{
97 static_cast<int64_t>(InitializerShapeImpl<D>::value(list))...};
98}
99
100template <typename L>
101SizeVector 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
113template <typename T, typename L>
114void NestedCopy(T&& iter, const L& list) {
115 *iter++ = list;
116}
117
118template <typename T, typename L>
119void 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
125template <typename T, size_t D>
126std::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
#define LogError(...)
Definition: Logging.h:67
Definition: SizeVector.h:88
int64_t NumElements() const
Definition: SizeVector.cpp:127
size_t size() const
Definition: SmallVector.h:138
void resize(size_type N)
Definition: SmallVector.h:697
void NestedCopy(T &&iter, const L &list)
Definition: TensorInit.h:114
SizeVector InferShape(const L &list)
Definition: TensorInit.h:101
typename NestedInitializerImpl< T, D >::type NestedInitializerList
Definition: TensorInit.h:55
std::vector< T > ToFlatVector(const SizeVector &shape, const tensor_init::NestedInitializerList< T, D > &nested_list)
Definition: TensorInit.h:126
SizeVector InitializerShape(const L &list, std::index_sequence< D... >)
Definition: TensorInit.h:95
Definition: PinholeCameraIntrinsic.cpp:35
Definition: Device.h:126
static constexpr size_t value
Definition: TensorInit.h:59
static constexpr size_t value(const L &list)
Definition: TensorInit.h:89
static constexpr size_t value(const L &list)
Definition: TensorInit.h:70
std::initializer_list< typename NestedInitializerImpl< T, D - 1 >::type > type
Definition: TensorInit.h:46