Open3D (C++ API)
0.19.0
Toggle main menu visibility
Loading...
Searching...
No Matches
cpp
open3d
core
TensorInit.h
Go to the documentation of this file.
1
// ----------------------------------------------------------------------------
2
// - Open3D: www.open3d.org -
3
// ----------------------------------------------------------------------------
4
// Copyright (c) 2018-2024 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>
25
struct
NestedInitializerImpl
{
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>
36
using
NestedInitializerList
=
typename
NestedInitializerImpl<T, D>::type
;
37
38
template
<
typename
L>
39
struct
InitializerDim
{
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>
49
struct
InitializerShapeImpl
{
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) {
57
if
(dim !=
InitializerShapeImpl<D - 1>::value
(
value
)) {
58
utility::LogError(
59
"Input contains ragged nested sequences"
60
"(nested lists with unequal sizes or shapes)."
);
61
}
62
}
63
return
dim;
64
}
65
};
66
67
template
<>
68
struct
InitializerShapeImpl
<0> {
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
SizeVector.h
open3d::core::SizeVector
Definition
SizeVector.h:69
open3d::core::SizeVector::NumElements
int64_t NumElements() const
Definition
SizeVector.cpp:108
open3d::core::SmallVectorImpl::resize
void resize(size_type N)
Definition
SmallVector.h:679
open3d::core::SmallVectorTemplateCommon::size
size_t size() const
Definition
SmallVector.h:120
open3d::core::tensor_init
Definition
TensorInit.h:17
open3d::core::tensor_init::NestedInitializerList
typename NestedInitializerImpl< T, D >::type NestedInitializerList
Definition
TensorInit.h:36
open3d::core::tensor_init::NestedCopy
void NestedCopy(T &&iter, const L &list)
Definition
TensorInit.h:95
open3d::core::tensor_init::InferShape
SizeVector InferShape(const L &list)
Definition
TensorInit.h:82
open3d::core::tensor_init::ToFlatVector
std::vector< T > ToFlatVector(const SizeVector &shape, const tensor_init::NestedInitializerList< T, D > &nested_list)
Definition
TensorInit.h:107
open3d::core::tensor_init::InitializerShape
SizeVector InitializerShape(const L &list, std::index_sequence< D... >)
Definition
TensorInit.h:76
open3d::core
Definition
AdvancedIndexing.cpp:15
open3d
Definition
PinholeCameraIntrinsic.cpp:16
std
Definition
Device.h:111
open3d::core::tensor_init::InitializerDim< std::initializer_list< L > >::value
static constexpr size_t value
Definition
TensorInit.h:45
open3d::core::tensor_init::InitializerDim
Definition
TensorInit.h:39
open3d::core::tensor_init::InitializerDim::value
static constexpr size_t value
Definition
TensorInit.h:40
open3d::core::tensor_init::InitializerShapeImpl< 0 >::value
static constexpr size_t value(const L &list)
Definition
TensorInit.h:70
open3d::core::tensor_init::InitializerShapeImpl
Definition
TensorInit.h:49
open3d::core::tensor_init::InitializerShapeImpl::value
static constexpr size_t value(const L &list)
Definition
TensorInit.h:51
open3d::core::tensor_init::NestedInitializerImpl< T, 0 >::type
T type
Definition
TensorInit.h:32
open3d::core::tensor_init::NestedInitializerImpl
Definition
TensorInit.h:25
open3d::core::tensor_init::NestedInitializerImpl::type
std::initializer_list< typename NestedInitializerImpl< T, D - 1 >::type > type
Definition
TensorInit.h:26
Generated by
1.17.0