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"
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;
34  format == "l") &&
35  byte_size == 4)
36  return core::Int32;
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;
46  format == "L") &&
47  byte_size == 4)
48  return core::UInt32;
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;
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) {
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
filament::Texture::InternalFormat format
#define LogError(...)
Definition: Logging.h:60
const Dtype Undefined
Definition: Dtype.cpp:41
const Dtype Int8
Definition: Dtype.cpp:44
const Dtype Bool
Definition: Dtype.cpp:52
const Dtype Int64
Definition: Dtype.cpp:47
const Dtype UInt64
Definition: Dtype.cpp:51
const Dtype UInt32
Definition: Dtype.cpp:50
const Dtype UInt8
Definition: Dtype.cpp:48
const Dtype Int16
Definition: Dtype.cpp:45
const Dtype Float64
Definition: Dtype.cpp:43
const Dtype UInt16
Definition: Dtype.cpp:49
const Dtype Int32
Definition: Dtype.cpp:46
const Dtype Float32
Definition: Dtype.cpp:42
core::Dtype ArrayFormatToDtype(const std::string &format, size_t byte_size)
std::string DtypeToArrayFormat(const core::Dtype &dtype)
Generic file read and write utility for python interface.