ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
dtype.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 "core/Dtype.h"
9 
11 #include "pybind/core/core.h"
12 #include "pybind/docstring.h"
13 
14 namespace cloudViewer {
15 namespace core {
16 
17 void pybind_core_dtype(py::module &m) {
18  // cloudViewer.core.Dtype class
19  py::class_<Dtype, std::shared_ptr<Dtype>> dtype(m, "Dtype",
20  "CloudViewer data types.");
21  py::native_enum<Dtype::DtypeCode>(dtype, "DtypeCode", "enum.Enum")
22  .value("Undefined", Dtype::DtypeCode::Undefined)
23  .value("Bool", Dtype::DtypeCode::Bool)
24  .value("Int", Dtype::DtypeCode::Int)
25  .value("UInt", Dtype::DtypeCode::UInt)
26  .value("Float", Dtype::DtypeCode::Float)
27  .value("Object", Dtype::DtypeCode::Object)
28  .finalize();
29 
30  dtype.def(py::init<Dtype::DtypeCode, int64_t, const std::string &>());
31  dtype.def_readonly_static("Undefined", &core::Undefined);
32  dtype.def_readonly_static("Float32", &core::Float32);
33  dtype.def_readonly_static("Float64", &core::Float64);
34  dtype.def_readonly_static("Int8", &core::Int8);
35  dtype.def_readonly_static("Int16", &core::Int16);
36  dtype.def_readonly_static("Int32", &core::Int32);
37  dtype.def_readonly_static("Int64", &core::Int64);
38  dtype.def_readonly_static("UInt8", &core::UInt8);
39  dtype.def_readonly_static("UInt16", &core::UInt16);
40  dtype.def_readonly_static("UInt32", &core::UInt32);
41  dtype.def_readonly_static("UInt64", &core::UInt64);
42  dtype.def_readonly_static("Bool", &core::Bool);
43  dtype.def("byte_size", &Dtype::ByteSize);
44  dtype.def("byte_code", &Dtype::GetDtypeCode);
45  dtype.def("__eq__", &Dtype::operator==);
46  dtype.def("__hash__", [](const Dtype &dt) {
47  using DtypeTuple = std::tuple<size_t, size_t, std::string>;
49  std::make_tuple(static_cast<size_t>(dt.GetDtypeCode()),
50  dt.ByteSize(), dt.ToString()));
51  });
52  dtype.def("__ene__", &Dtype::operator!=);
53  dtype.def("__repr__", &Dtype::ToString);
54  dtype.def("__str__", &Dtype::ToString);
55 
56  // Dtype shortcuts.
57  // E.g. cloudViewer.core.float32
58  m.attr("undefined") = &core::Undefined;
59  m.attr("float32") = core::Float32;
60  m.attr("float64") = core::Float64;
61  m.attr("int8") = core::Int8;
62  m.attr("int16") = core::Int16;
63  m.attr("int32") = core::Int32;
64  m.attr("int64") = core::Int64;
65  m.attr("uint8") = core::UInt8;
66  m.attr("uint16") = core::UInt16;
67  m.attr("uint32") = core::UInt32;
68  m.attr("uint64") = core::UInt64;
69  m.attr("bool") = core::Bool;
70  m.attr("bool8") = core::Bool;
71 }
72 
73 } // namespace core
74 } // namespace cloudViewer
std::string ToString() const
Definition: Dtype.h:65
int64_t ByteSize() const
Definition: Dtype.h:59
DtypeCode GetDtypeCode() const
Definition: Dtype.h:61
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
void pybind_core_dtype(py::module &m)
Definition: dtype.cpp:17
const Dtype UInt16
Definition: Dtype.cpp:49
const Dtype Int32
Definition: Dtype.cpp:46
const Dtype Float32
Definition: Dtype.cpp:42
Generic file read and write utility for python interface.