ACloudViewer
3.9.4
A Modern Library for 3D Data Processing
pybind_utils.cpp
Go to the documentation of this file.
1
// ----------------------------------------------------------------------------
2
// - CloudViewer: www.cloudViewer.org -
3
// ----------------------------------------------------------------------------
4
// Copyright (c) 2018-2024 www.cloudViewer.org
5
// SPDX-License-Identifier: MIT
6
// ----------------------------------------------------------------------------
7
8
#include "
pybind/pybind_utils.h
"
9
10
#include <string>
11
12
#include "
core/Dtype.h
"
13
#include "
core/Tensor.h
"
14
#include "
pybind/cloudViewer_pybind.h
"
15
16
namespace
cloudViewer
{
17
namespace
pybind_utils {
18
19
core::Dtype
ArrayFormatToDtype
(
const
std::string&
format
,
size_t
byte_size) {
20
// In general, format characters follows the standard:
21
// https://docs.python.org/3/library/struct.html#format-characters
22
//
23
// However, some integer dtypes have aliases. E.g. "l" can be 4 bytes or 8
24
// bytes depending on the OS. To be safe, we always check the byte size.
25
if
(
format
==
py::format_descriptor<float>::format
() && byte_size == 4)
26
return
core::Float32
;
27
if
(
format
==
py::format_descriptor<double>::format
() && byte_size == 8)
28
return
core::Float64
;
29
if
(
format
==
py::format_descriptor<int8_t>::format
() && byte_size == 1)
30
return
core::Int8
;
31
if
(
format
==
py::format_descriptor<int16_t>::format
() && byte_size == 2)
32
return
core::Int16
;
33
if
((
format
==
py::format_descriptor<int32_t>::format
() ||
format
==
"i"
||
34
format
==
"l"
) &&
35
byte_size == 4)
36
return
core::Int32
;
37
if
((
format
==
py::format_descriptor<int64_t>::format
() ||
format
==
"q"
||
38
format
==
"l"
) &&
39
byte_size == 8)
40
return
core::Int64
;
41
if
(
format
==
py::format_descriptor<uint8_t>::format
() && byte_size == 1)
42
return
core::UInt8
;
43
if
(
format
==
py::format_descriptor<uint16_t>::format
() && byte_size == 2)
44
return
core::UInt16
;
45
if
((
format
==
py::format_descriptor<uint32_t>::format
() ||
46
format
==
"L"
) &&
47
byte_size == 4)
48
return
core::UInt32
;
49
if
((
format
==
py::format_descriptor<uint64_t>::format
() ||
50
format
==
"L"
) &&
51
byte_size == 8)
52
return
core::UInt64
;
53
if
(
format
==
py::format_descriptor<bool>::format
() && byte_size == 1)
54
return
core::Bool
;
55
utility::LogError
(
56
"ArrayFormatToDtype: unsupported python array format {} with "
57
"byte_size {}."
,
58
format
, byte_size);
59
return
core::Undefined
;
60
}
61
62
std::string
DtypeToArrayFormat
(
const
core::Dtype
& dtype) {
63
if
(dtype ==
core::Float32
)
return
py::format_descriptor<float>::format
();
64
if
(dtype ==
core::Float64
)
return
py::format_descriptor<double>::format
();
65
if
(dtype ==
core::Int8
)
return
py::format_descriptor<int8_t>::format
();
66
if
(dtype ==
core::Int16
)
return
py::format_descriptor<int16_t>::format
();
67
if
(dtype ==
core::Int32
)
return
py::format_descriptor<int32_t>::format
();
68
if
(dtype ==
core::Int64
)
return
py::format_descriptor<int64_t>::format
();
69
if
(dtype ==
core::UInt8
)
return
py::format_descriptor<uint8_t>::format
();
70
if
(dtype ==
core::UInt16
)
return
py::format_descriptor<uint16_t>::format
();
71
if
(dtype ==
core::UInt32
)
return
py::format_descriptor<uint32_t>::format
();
72
if
(dtype ==
core::UInt64
)
return
py::format_descriptor<uint64_t>::format
();
73
if
(dtype ==
core::Bool
)
return
py::format_descriptor<bool>::format
();
74
utility::LogError
(
"Unsupported data type."
);
75
return
std::string();
76
}
77
78
}
// namespace pybind_utils
79
}
// namespace cloudViewer
Dtype.h
format
filament::Texture::InternalFormat format
Definition:
FilamentResourceManager.cpp:181
Tensor.h
cloudViewer::core::Dtype
Definition:
Dtype.h:21
cloudViewer_pybind.h
LogError
#define LogError(...)
Definition:
Logging.h:60
cloudViewer::core::Undefined
const Dtype Undefined
Definition:
Dtype.cpp:41
cloudViewer::core::Int8
const Dtype Int8
Definition:
Dtype.cpp:44
cloudViewer::core::Bool
const Dtype Bool
Definition:
Dtype.cpp:52
cloudViewer::core::Int64
const Dtype Int64
Definition:
Dtype.cpp:47
cloudViewer::core::UInt64
const Dtype UInt64
Definition:
Dtype.cpp:51
cloudViewer::core::UInt32
const Dtype UInt32
Definition:
Dtype.cpp:50
cloudViewer::core::UInt8
const Dtype UInt8
Definition:
Dtype.cpp:48
cloudViewer::core::Int16
const Dtype Int16
Definition:
Dtype.cpp:45
cloudViewer::core::Float64
const Dtype Float64
Definition:
Dtype.cpp:43
cloudViewer::core::UInt16
const Dtype UInt16
Definition:
Dtype.cpp:49
cloudViewer::core::Int32
const Dtype Int32
Definition:
Dtype.cpp:46
cloudViewer::core::Float32
const Dtype Float32
Definition:
Dtype.cpp:42
cloudViewer::pybind_utils::ArrayFormatToDtype
core::Dtype ArrayFormatToDtype(const std::string &format, size_t byte_size)
Definition:
pybind_utils.cpp:19
cloudViewer::pybind_utils::DtypeToArrayFormat
std::string DtypeToArrayFormat(const core::Dtype &dtype)
Definition:
pybind_utils.cpp:62
cloudViewer
Generic file read and write utility for python interface.
Definition:
AutoSegmentationTools.h:16
pybind_utils.h
libs
Python
pybind
pybind_utils.cpp
Generated on Wed Jan 28 2026 09:00:58 for ACloudViewer by
1.9.1