ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
CCGeom.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 <pybind11/operators.h>
9 #include <pybind11/pybind11.h>
10 
11 #include <CVGeom.h>
12 
13 namespace py = pybind11;
14 using namespace pybind11::literals;
15 
16 // TODO operator -()
17 #define DEFINE_VECTOR2TPL_TYPE(cppname, pyname, type_) \
18  py::class_<cppname>(cccorelib, pyname) \
19  .def(py::init<>()) \
20  .def(py::init<type_, type_>()) \
21  .def_readwrite("x", &cppname::x) \
22  .def_readwrite("y", &cppname::y) \
23  .def("norm2", &cppname::norm2) \
24  .def("norm", &cppname::norm) \
25  .def("normalize", &cppname::normalize) \
26  .def("dot", &cppname::dot) \
27  .def("cross", &cppname::cross) \
28  .def("__getitem__", \
29  [](const cppname &self, unsigned index) \
30  { \
31  if (index >= 2) \
32  { \
33  throw py::index_error(); \
34  } \
35  return self[index]; \
36  }) \
37  .def(py::self += py::self) \
38  .def(py::self -= py::self) \
39  .def(py::self *= type_()) \
40  .def( \
41  "__itruediv__", \
42  [](cppname &self, type_ val) \
43  { \
44  if (val == static_cast<type_>(0.0)) \
45  { \
46  throw std::runtime_error("Division by 0"); \
47  } \
48  return self /= val; \
49  }, \
50  py::is_operator()) \
51  .def(py::self + py::self) \
52  .def(py::self - py::self) \
53  .def(py::self *type_()) \
54  .def( \
55  "__truediv__", \
56  [](cppname &self, type_ val) \
57  { \
58  if (val == static_cast<type_>(0.0)) \
59  { \
60  throw std::runtime_error("Division by 0"); \
61  } \
62  return self / val; \
63  }, \
64  py::is_operator()) \
65  .def("__repr__", \
66  [](const cppname &self) \
67  { \
68  return std::string("<") + pyname + "(" + std::to_string(self.x) + ", " + \
69  std::to_string(self.y) + ")>"; \
70  });
71 
72 #define DEFINE_TUPLE3TPL(cppname, pyname, type_) \
73  py::class_<cppname>(cccorelib, pyname) \
74  .def(py::init<>()) \
75  .def(py::init<type_, type_, type_>()) \
76  .def_readwrite("x", &cppname::x) \
77  .def_readwrite("y", &cppname::y) \
78  .def_readwrite("z", &cppname::z) \
79  .def(py::self -= py::self) \
80  .def(py::self += py::self) \
81  .def(py::self *= type_()) \
82  .def( \
83  "__itruediv__", \
84  [](cppname &self, type_ val) \
85  { \
86  if (val == static_cast<type_>(0.0)) \
87  { \
88  throw std::runtime_error("Division by 0"); \
89  } \
90  return self /= val; \
91  }, \
92  py::is_operator()) \
93  .def(py::self - py::self) \
94  .def(py::self + py::self) \
95  .def(py::self *type_()) \
96  .def( \
97  "__truediv__", \
98  [](cppname &self, type_ val) \
99  { \
100  if (val == static_cast<type_>(0.0)) \
101  { \
102  throw std::runtime_error("Division by 0"); \
103  } \
104  return self / val; \
105  }, \
106  py::is_operator()) \
107  .def("__getitem__", \
108  [](const cppname &self, unsigned index) \
109  { \
110  switch (index) \
111  { \
112  case 0: \
113  return self.x; \
114  case 1: \
115  return self.y; \
116  case 2: \
117  return self.z; \
118  default: \
119  throw py::index_error("index out of range"); \
120  } \
121  });
122 
123 #define DEFINE_CCVECTOR3(cppname, pyname, type_) \
124  py::class_<cppname, Tuple3Tpl<type_>>(cccorelib, pyname) \
125  .def(py::init<>()) \
126  .def(py::init<type_, type_, type_>()) \
127  .def("__mul__", [](const cppname &self, PointCoordinateType val) { return self * val; }) \
128  .def("__sub__", [](const cppname &self, const cppname &other) { return self - other; }) \
129  .def("__div__", &cppname::operator/) \
130  .def("__add__", [](const cppname &self, const cppname &other) { return self + other; }) \
131  .def("__repr__", \
132  [](const cppname &self) \
133  { \
134  return std::string("<") + pyname + "(" + std::to_string(self.x) + ", " + \
135  std::to_string(self.y) + ", " + std::to_string(self.z) + ")>"; \
136  });
137 
138 void define_CCGeom(py::module &cccorelib)
139 {
141  DEFINE_VECTOR2TPL_TYPE(CCVector2d, "CCVector2d", double);
142  DEFINE_VECTOR2TPL_TYPE(CCVector2i, "CCVector2i", int);
143 
144  using uchar = unsigned char;
145  using uint = unsigned int;
146  DEFINE_TUPLE3TPL(Tuple3ub, "Tuple3ub", uchar);
147  DEFINE_TUPLE3TPL(Tuple3s, "Tuple3s", short);
148  DEFINE_TUPLE3TPL(Tuple3i, "Tuple3i", int);
149  DEFINE_TUPLE3TPL(Tuple3ui, "Tuple3ui", uint);
151  DEFINE_TUPLE3TPL(Tuple3Tpl<double>, "Tuple3d", double);
152 
154  DEFINE_CCVECTOR3(CCVector3d, "CCVector3d", double);
155 }
#define DEFINE_VECTOR2TPL_TYPE(cppname, pyname, type_)
Definition: CCGeom.cpp:17
#define DEFINE_TUPLE3TPL(cppname, pyname, type_)
Definition: CCGeom.cpp:72
void define_CCGeom(py::module &cccorelib)
Definition: CCGeom.cpp:138
#define DEFINE_CCVECTOR3(cppname, pyname, type_)
Definition: CCGeom.cpp:123
float PointCoordinateType
Type of the coordinates of a (N-D) point.
Definition: CVTypes.h:16
unsigned int uint
Definition: cutil_math.h:28
unsigned char uchar
Definition: matrix.h:41