ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
size_vector.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/SizeVector.h"
10 #include "pybind/core/core.h"
11 #include "pybind/docstring.h"
12 
13 namespace cloudViewer {
14 namespace core {
15 
16 void pybind_core_size_vector(py::module& m) {
17  // In Python, We want (3), (3,), [3] and [3,] to represent the same thing.
18  // The following are all equivalent to core::SizeVector({3}):
19  // - cv3d.core.SizeVector(3) # int
20  // - cv3d.core.SizeVector((3)) # int, not tuple
21  // - cv3d.core.SizeVector((3,)) # tuple
22  // - cv3d.core.SizeVector([3]) # list
23  // - cv3d.core.SizeVector([3,]) # list
24  //
25  // Difference between C++ and Python:
26  // - cv3d.core.SizeVector(3) creates a 1-D SizeVector: {3}.
27  // - core::SizeVector(3) creates a 3-D SizeVector: {0, 0, 0}.
28  //
29  // The API difference is due to the NumPy convention which allows integer to
30  // represent a 1-element tuple, and the C++ constructor for vectors.
31  auto sv = py::bind_vector<SizeVector>(
32  m, "SizeVector",
33  "A vector of integers for specifying shape, strides, etc.");
34  sv.def(py::init([](int64_t i) { return SizeVector({i}); }));
35  py::implicitly_convertible<py::int_, SizeVector>();
36 
37  // Allows tuple and list implicit conversions to SizeVector.
38  py::implicitly_convertible<py::tuple, SizeVector>();
39  py::implicitly_convertible<py::list, SizeVector>();
40  auto dsv = py::bind_vector<DynamicSizeVector>(
41  m, "DynamicSizeVector",
42  "A vector of integers for specifying shape, strides, etc. Some "
43  "elements can be None.");
44  dsv.def("__repr__",
45  [](const DynamicSizeVector& dsv) { return dsv.ToString(); });
46 }
47 
48 } // namespace core
49 } // namespace cloudViewer
void pybind_core_size_vector(py::module &m)
Definition: size_vector.cpp:16
Generic file read and write utility for python interface.