ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
cloudViewer_pybind.h
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 #pragma once
9 
10 #undef slots
11 // PYBIND_11
12 // corecrt.h before pybind so that the _STL_ASSERT macro is defined in a
13 // compatible way.
14 //
15 // pybind11/pybind11.h includes pybind11/detail/common.h, which undefines _DEBUG
16 // whilst including the Python headers (which in turn include corecrt.h). This
17 // alters how the _STL_ASSERT macro is defined and causes the build to fail.
18 //
19 // see https://github.com/microsoft/onnxruntime/issues/9735
20 // https://github.com/microsoft/onnxruntime/pull/11495
21 //
22 #if defined(_MSC_FULL_VER) && defined(_DEBUG) && _MSC_FULL_VER >= 192930145
23 #include <corecrt.h>
24 #endif
25 
26 #include <pybind11/detail/common.h>
27 #include <pybind11/detail/internals.h>
28 #include <pybind11/eigen.h>
29 #include <pybind11/functional.h>
30 #include <pybind11/native_enum.h>
31 #include <pybind11/numpy.h>
32 #include <pybind11/operators.h>
33 #include <pybind11/pybind11.h> // Include first to suppress compiler warnings
34 #include <pybind11/stl.h>
35 #include <pybind11/stl_bind.h>
36 
37 // CV_CORE_LIB
38 #include <CVGeom.h>
39 #include <Eigen.h>
40 #include <Optional.h>
41 
42 // CV_DB_LIB
44 #include <ecvGLMatrix.h>
45 #include <ecvMesh.h>
46 #include <ecvPointCloud.h>
47 
48 // QT
49 #include <QString>
50 // must be included after Qt to avoid compiling issues
51 // PYBIND11_MODULE error: expected unqualified-id before ‘=’ token
52 // PYBIND11_MODULE error: expected primary-expression before ‘.’ token
53 #undef slots
54 
55 // We include the type caster for tensor here because it must be included in
56 // every compilation unit.
58 
59 // Replace with <pybind11/stl/filesystem.h> when we require C++17.
60 #include "pybind_filesystem.h"
61 
62 namespace py = pybind11;
63 using namespace py::literals;
64 
65 typedef std::vector<Eigen::Matrix4d, cloudViewer::utility::Matrix4d_allocator>
67 typedef std::vector<Eigen::Vector4i, cloudViewer::utility::Vector4i_allocator>
69 
70 PYBIND11_MAKE_OPAQUE(std::vector<char>);
71 PYBIND11_MAKE_OPAQUE(std::vector<int>);
72 PYBIND11_MAKE_OPAQUE(std::vector<int64_t>);
73 PYBIND11_MAKE_OPAQUE(std::vector<uint8_t>);
74 PYBIND11_MAKE_OPAQUE(std::vector<float>);
75 PYBIND11_MAKE_OPAQUE(std::vector<double>);
76 PYBIND11_MAKE_OPAQUE(std::vector<Eigen::Vector3d>);
77 PYBIND11_MAKE_OPAQUE(std::vector<Eigen::Vector3i>);
78 PYBIND11_MAKE_OPAQUE(std::vector<Eigen::Vector2d>);
79 PYBIND11_MAKE_OPAQUE(std::vector<Eigen::Vector2i>);
80 PYBIND11_MAKE_OPAQUE(std::vector<Eigen::Matrix3d>);
84  std::vector<cloudViewer::pipelines::registration::PoseGraphEdge>);
86  std::vector<cloudViewer::pipelines::registration::PoseGraphNode>);
87 
88 PYBIND11_MAKE_OPAQUE(std::vector<CCVector3>);
89 PYBIND11_MAKE_OPAQUE(std::vector<CCVector3d>);
92 PYBIND11_MAKE_OPAQUE(std::vector<QString>);
93 
94 // some helper functions
95 namespace pybind11 {
96 namespace detail {
97 
98 template <typename T, typename Class_>
99 void bind_default_constructor(Class_ &cl) {
100  cl.def(py::init([]() { return new T(); }), "Default constructor");
101 }
102 
103 template <typename T, typename Class_>
104 void bind_copy_functions(Class_ &cl) {
105  cl.def(py::init([](const T &cp) { return new T(cp); }), "Copy constructor");
106  cl.def("__copy__", [](T &v) { return T(v); });
107  cl.def("__deepcopy__", [](T &v, py::dict &memo) { return T(v); });
108 }
109 
148 template <typename T>
150  using value_conv = make_caster<typename T::value_type>;
151 
152  template <typename T_>
153  static handle cast(T_ &&src, return_value_policy policy, handle parent) {
154  if (!src) return none().inc_ref();
155  if (!std::is_lvalue_reference<T>::value) {
156  policy = return_value_policy_override<T>::policy(policy);
157  }
158  return value_conv::cast(*std::forward<T_>(src), policy, parent);
159  }
160 
161  bool load(handle src, bool convert) {
162  if (!src) {
163  return false;
164  } else if (src.is_none()) {
165  return true; // default-constructed value is already empty
166  }
167  value_conv inner_caster;
168  if (!inner_caster.load(src, convert)) return false;
169 
170  value.emplace(
171  cast_op<typename T::value_type &&>(std::move(inner_caster)));
172  return true;
173  }
174 
175  PYBIND11_TYPE_CASTER(T, _("Optional[") + value_conv::name + _("]"));
176 };
177 
178 template <typename T>
179 struct type_caster<cloudViewer::utility::optional<T>>
180  : public cloudViewer_optional_caster<cloudViewer::utility::optional<T>> {};
181 
182 template <>
183 struct type_caster<cloudViewer::utility::nullopt_t>
184  : public void_caster<cloudViewer::utility::nullopt_t> {};
185 
186 } // namespace detail
187 } // namespace pybind11
void convert(int argc, char **argv, const std::string &file_in, const std::string &file_out)
std::string name
Double version of ccGLMatrixTpl.
Definition: ecvGLMatrix.h:56
std::vector< Eigen::Vector4i, cloudViewer::utility::Vector4i_allocator > temp_eigen_vector4i
std::vector< Eigen::Matrix4d, cloudViewer::utility::Matrix4d_allocator > temp_eigen_matrix4d
PYBIND11_MAKE_OPAQUE(std::vector< char >)
Generic file read and write utility for python interface.
void bind_default_constructor(Class_ &cl)
void bind_copy_functions(Class_ &cl)
static handle cast(T_ &&src, return_value_policy policy, handle parent)
make_caster< typename T::value_type > value_conv
PYBIND11_TYPE_CASTER(T, _("Optional[")+value_conv::name+_("]"))