ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
cloudbase.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 // LOCAL
9 #include "cloudbase.h"
10 
11 // CV_CORE_LIB
12 #include <BoundingBox.h>
13 #include <Polyline.h>
14 
15 // CV_DB_LIB
17 
18 #include "pybind/docstring.h"
21 
22 #ifdef CV_WINDOWS
23 #pragma warning(disable : 4715)
24 #endif
25 
26 using namespace cloudViewer;
27 namespace cloudViewer {
28 namespace geometry {
29 
30 void pybind_cloudbase(py::module& m) {
31  // cloudViewer.geometry.GenericCloud functions
32  py::class_<GenericCloud, PyGenericCloud<GenericCloud>,
33  std::shared_ptr<GenericCloud>>
34  pygc(m, "GenericCloud",
35  "A generic 3D point cloud interface for data communication"
36  " between library and client applications.");
37  pygc.def("size", &GenericCloud::size, "Returns the number of points.")
38  .def("has_points", &GenericCloud::hasPoints,
39  "Returns whether has points.")
40  .def(
41  "get_bounding_box",
42  [](GenericCloud& obj) {
43  CCVector3 bbMin, bbMax;
44  obj.getBoundingBox(bbMin, bbMax);
45  return std::make_tuple(CCVector3d::fromArray(bbMin),
46  CCVector3d::fromArray(bbMax));
47  },
48  "Returns the cloud bounding box.")
49  .def(
50  "test_visibility",
51  [](const GenericCloud& obj, const Eigen::Vector3d& point) {
53  },
54  "Returns a given point visibility state (relatively to a "
55  "sensor for instance).",
56  "point"_a)
57  .def("place_iterator_at_beginning",
59  "Sets the cloud iterator at the beginning.")
60  .def(
61  "get_next_point",
62  [](GenericCloud& obj) {
63  return CCVector3d::fromArray(*obj.getNextPoint());
64  },
65  "Returns the next point (relatively to the global iterator "
66  "position).")
67  .def("enable_scalar_field", &GenericCloud::enableScalarField,
68  "Enables the scalar field associated to the cloud.")
69  .def("is_scalar_field_enabled", &GenericCloud::isScalarFieldEnabled,
70  "Returns true if the scalar field is enabled, false "
71  "otherwise.")
72  .def("set_scalar_value", &GenericCloud::setPointScalarValue,
73  "Sets the ith point associated scalar value.", "point_index"_a,
74  "value"_a)
75  .def("get_scalar_value", &GenericCloud::getPointScalarValue,
76  "Returns the ith point associated scalar value.",
77  "point_index"_a);
78  docstring::ClassMethodDocInject(m, "GenericCloud", "size");
79  docstring::ClassMethodDocInject(m, "GenericCloud", "has_points");
80  docstring::ClassMethodDocInject(m, "GenericCloud", "get_bounding_box");
82  m, "GenericCloud", "test_visibility",
83  {{"point",
84  "the 3D point to test, "
85  "return visibility (default: POINT_VISIBLE)"
86  "Generic method to request a point visibility (should be "
87  "overloaded if this functionality is required)."
88  "The point visibility is such as defined in Daniel Girardeau - "
89  "Montaut's PhD manuscript (see Chapter 2, "
90  "section 2 - 3 - 3).In this case, a ground based laser sensor "
91  "model should be used to determine it."
92  "This method is called before performing any point - to - cloud "
93  "comparison.If the result is not"
94  "POINT_VISIBLE, then the comparison won't be performed and the "
95  "scalar field value associated"
96  "to this point will be this visibility value."}});
97  docstring::ClassMethodDocInject(m, "GenericCloud",
98  "place_iterator_at_beginning");
99  docstring::ClassMethodDocInject(m, "GenericCloud", "get_next_point");
100  docstring::ClassMethodDocInject(m, "GenericCloud", "enable_scalar_field");
101  docstring::ClassMethodDocInject(m, "GenericCloud",
102  "is_scalar_field_enabled");
103  docstring::ClassMethodDocInject(m, "GenericCloud", "set_scalar_value");
104  docstring::ClassMethodDocInject(m, "GenericCloud", "get_scalar_value");
105 
106  // cloudViewer.geometry.GenericIndexedCloud functions
107  py::class_<GenericIndexedCloud, PyGenericIndexedCloud<GenericIndexedCloud>,
108  std::shared_ptr<GenericIndexedCloud>, GenericCloud>
109  pygic(m, "GenericIndexedCloud",
110  " A generic 3D point cloud with index-based point access.");
111  pygic.def(
112  "get_point",
113  [](const GenericIndexedCloud& obj, unsigned index) {
114  return CCVector3d::fromArray(*obj.getPoint(index));
115  },
116  "Returns the ith point(virtual method to request a point with a "
117  "specific index).",
118  "index"_a);
119  docstring::ClassMethodDocInject(m, "GenericIndexedCloud", "get_point");
120 
121  // cloudViewer.geometry.GenericIndexedCloudPersist functions
122  py::class_<GenericIndexedCloudPersist,
124  std::shared_ptr<GenericIndexedCloudPersist>, GenericIndexedCloud>
125  pygicp(m, "GenericIndexedCloudPersist",
126  "A generic 3D point cloud with index-based point access.");
127  pygicp.def(
128  "get_point_persistent",
129  [](GenericIndexedCloudPersist& obj, unsigned index) {
130  return CCVector3d::fromArray(*obj.getPointPersistentPtr(index));
131  },
132  "Returns the ith point as a persistent point).", "index"_a);
133  docstring::ClassMethodDocInject(m, "GenericIndexedCloudPersist",
134  "get_point_persistent");
135 
136  // cloudViewer.geometry.ReferenceCloud functions
137  py::class_<ReferenceCloud, PyGenericReferenceCloud<ReferenceCloud>,
138  std::shared_ptr<ReferenceCloud>, GenericIndexedCloudPersist>
139  pyrefcloud(m, "ReferenceCloud",
140  "The polyline is considered as a cloud of points "
141  "(in a specific order) with a open closed state "
142  "information.");
143  py::detail::bind_copy_functions<ReferenceCloud>(pyrefcloud);
144  pyrefcloud
145  .def(py::init([](std::shared_ptr<GenericIndexedCloudPersist>
146  associated_cloud) {
147  return new ReferenceCloud(associated_cloud.get());
148  }),
149  "ReferenceCloud constructor", "associated_cloud"_a)
150  .def("__repr__",
151  [](const cloudViewer::Polyline& poly) {
152  return fmt::format("ReferenceCloud with {} points",
153  poly.size());
154  })
155  .def("get_point_global_index", &ReferenceCloud::getPointGlobalIndex,
156  "Returns global index (i.e. relative to the associated cloud) "
157  "of a given element.",
158  "local_index"_a)
159  .def("get_cur_point_global_index",
161  "Returns the global index of the point pointed by the current "
162  "element.")
163  .def("get_cur_point_scalar",
165  "Returns the current point associated scalar value.")
166  .def("set_cur_point_scalar",
168  "Sets the current point associated scalar value.", "value"_a)
169  .def("forward_iterator", &ReferenceCloud::forwardIterator,
170  "Forwards the local element iterator.")
171  .def(
172  "get_cur_point_coordinates",
173  [](const ReferenceCloud& obj) {
174  return CCVector3d::fromArray(
175  *obj.getCurrentPointCoordinates());
176  },
177  "Returns the coordinates of the point pointed by the "
178  "current element.")
179  .def(
180  "get_associated_cloud",
181  [](ReferenceCloud& obj) {
182  if (obj.getAssociatedCloud()) {
183  return std::ref(*obj.getAssociatedCloud());
184  } else {
186  "[cloudViewer::ReferenceCloud] does not "
187  "have associated cloud!");
188  }
189  },
190  "Returns the associated (source) cloud.")
191  .def(
192  "get_associated_cloud",
193  [](const ReferenceCloud& obj) {
194  if (obj.getAssociatedCloud()) {
195  return std::ref(*obj.getAssociatedCloud());
196  } else {
198  "[cloudViewer::ReferenceCloud] does not "
199  "have associated cloud!");
200  }
201  },
202  "Returns the associated (source) cloud (const version).")
203  .def(
204  "set_associated_cloud",
205  [](ReferenceCloud& obj,
206  std::shared_ptr<GenericIndexedCloudPersist> cloud) {
207  obj.setAssociatedCloud(cloud.get());
208  },
209  "Sets the associated (source) cloud.")
210  .def("add", &ReferenceCloud::add, "Add another reference cloud.",
211  "cloud"_a)
212  .def("clear", &ReferenceCloud::clear, "Clears the cloud.",
213  "release_memory"_a = false)
214  .def("swap", &ReferenceCloud::swap, "Swaps two point references.",
215  "first_index"_a, "second_index"_a)
216  .def("reserve", &ReferenceCloud::reserve,
217  "Reserves some memory for hosting the point references.",
218  "n"_a)
219  .def("resize", &ReferenceCloud::resize,
220  "Presets the size of the vector used to store point "
221  "references.",
222  "n"_a)
223  .def("capacity", &ReferenceCloud::capacity,
224  "Reserves some memory for hosting the point references.")
225  .def("invalidate_boundingBox",
227  "Invalidates the bounding-box")
228  .def("add_point_index",
229  py::overload_cast<unsigned>(&ReferenceCloud::addPointIndex),
230  "Point global index insertion mechanism.", "global_index"_a)
231  .def("add_point_index",
232  py::overload_cast<unsigned, unsigned>(
234  "Point global index insertion mechanism (range).",
235  "first_index"_a, "last_index"_a)
236  .def("set_point_index", &ReferenceCloud::setPointIndex,
237  "Sets global index for a given element.", "local_index"_a,
238  "global_index"_a)
239  .def("remove_point_global_index",
241  "Removes a given element.", "local_ndex"_a)
242  .def("remove_cur_point_global_index",
244  "Removes current global element, WARNING: this method changes "
245  "the cloud size!.");
246 
247  docstring::ClassMethodDocInject(m, "ReferenceCloud", "add");
248  docstring::ClassMethodDocInject(m, "ReferenceCloud", "swap");
249  docstring::ClassMethodDocInject(m, "ReferenceCloud", "clear");
250  docstring::ClassMethodDocInject(m, "ReferenceCloud", "resize");
251  docstring::ClassMethodDocInject(m, "ReferenceCloud", "reserve");
252  docstring::ClassMethodDocInject(m, "ReferenceCloud", "capacity");
253  docstring::ClassMethodDocInject(m, "ReferenceCloud",
254  "get_point_global_index");
255  docstring::ClassMethodDocInject(m, "ReferenceCloud",
256  "get_cur_point_global_index");
257  docstring::ClassMethodDocInject(m, "ReferenceCloud",
258  "get_cur_point_scalar");
259  docstring::ClassMethodDocInject(m, "ReferenceCloud",
260  "set_cur_point_scalar");
261  docstring::ClassMethodDocInject(m, "ReferenceCloud", "forward_iterator");
262  docstring::ClassMethodDocInject(m, "ReferenceCloud",
263  "get_cur_point_coordinates");
264  docstring::ClassMethodDocInject(m, "ReferenceCloud",
265  "get_associated_cloud");
266  docstring::ClassMethodDocInject(m, "ReferenceCloud",
267  "set_associated_cloud");
268  docstring::ClassMethodDocInject(m, "ReferenceCloud",
269  "invalidate_boundingBox");
270  docstring::ClassMethodDocInject(m, "ReferenceCloud", "add_point_index");
271  docstring::ClassMethodDocInject(m, "ReferenceCloud", "set_point_index");
272  docstring::ClassMethodDocInject(m, "ReferenceCloud",
273  "remove_point_global_index");
274  docstring::ClassMethodDocInject(m, "ReferenceCloud",
275  "remove_cur_point_global_index");
276 
277  // cloudViewer.geometry.ccGenericPointCloud functions
278  py::class_<ccGenericPointCloud, PyGeometry<ccGenericPointCloud>,
279  std::shared_ptr<ccGenericPointCloud>,
281  genericPointCloud(m, "ccGenericPointCloud",
282  py::multiple_inheritance(),
283  "A 3D cloud interface with associated features "
284  "(color, normals, octree, etc.).");
285  genericPointCloud
286  .def("__repr__",
287  [](const ccGenericPointCloud& cloud) {
288  return fmt::format("ccGenericPointCloud with {} points",
289  cloud.size());
290  })
291  .def(
292  "clone",
293  [](ccGenericPointCloud& cloud, bool ignore_children) {
294  return std::shared_ptr<ccGenericPointCloud>(
295  cloud.clone(nullptr, ignore_children));
296  },
297  "Clones this entity.", "ignore_children"_a = false)
298  .def("clear", &ccGenericPointCloud::clear,
299  "Clears the entity from all its points and features.")
300  .def(
301  "get_scalar_value_color",
302  [](const ccGenericPointCloud& cloud, ScalarType value) {
303  return ecvColor::Rgb::ToEigen(
304  *cloud.getScalarValueColor(value));
305  },
306  "Returns color corresponding to a given scalar value.",
307  "value"_a)
308  .def(
309  "get_point_scalar_value_color",
310  [](const ccGenericPointCloud& cloud, unsigned point_index) {
311  return ecvColor::Rgb::ToEigen(
312  *cloud.getPointScalarValueColor(point_index));
313  },
314  "Returns color corresponding to a given point associated "
315  "scalar value.",
316  "point_index"_a)
317  .def(
318  "get_point_color",
319  [](const ccGenericPointCloud& cloud, unsigned point_index) {
320  return ecvColor::Rgb::ToEigen(
321  cloud.getPointColor(point_index));
322  },
323  "Returns color corresponding to a given point.",
324  "point_index"_a)
325  .def("get_point_displayed_distance",
327  "Returns scalar value associated to a given point.",
328  "point_index"_a)
329  .def("get_point_normal_index",
331  "Returns compressed normal corresponding to a given point.",
332  "point_index"_a)
333  .def(
334  "get_point_normal",
335  [](const ccGenericPointCloud& cloud, unsigned point_index) {
336  return CCVector3d::fromArray(
337  cloud.getPointNormal(point_index));
338  },
339  "Returns normal corresponding to a given point.",
340  "point_index"_a)
341  .def(
342  "get_visibility_array",
343  [](const ccGenericPointCloud& cloud) {
344  return cloud.getTheVisibilityArray();
345  },
346  "Returns associated visibility array.")
347  .def(
348  "get_visibility_array",
349  [](ccGenericPointCloud& cloud) {
350  return cloud.getTheVisibilityArray();
351  },
352  "Returns associated visibility array (const version).")
353  .def(
354  "get_visible_points",
355  [](const ccGenericPointCloud& cloud,
356  const std::vector<unsigned char>& vis_table,
357  bool silent) {
358  const std::vector<unsigned char>* visTable = nullptr;
359  if (!vis_table.empty()) {
360  visTable = &vis_table;
361  }
362  return std::ref(
363  *cloud.getTheVisiblePoints(visTable, silent));
364  },
365  "Returns a ReferenceCloud equivalent to the visibility "
366  "array.",
367  "vis_table"_a, "silent"_a = false)
368  .def("is_visibility_table_instantiated",
370  "Returns whether the visibility array is allocated or not")
371  .def("reset_visibility_array",
373  "Resets the associated visibility array")
374  .def("invert_visibility_array",
376  "Inverts the visibility array")
377  .def("unallocate_visibility_array",
379  "Erases the points visibility information")
380  .def("refresh_bbox", &ccGenericPointCloud::refreshBB,
381  "Forces bounding-box update.")
382  .def(
383  "create_cloud_from_visibility_selection",
384  [](ccGenericPointCloud& cloud, bool remove_selected_points,
386  bool silent) {
388  nullptr;
389  if (!vis_table.empty()) {
390  visTable = &vis_table;
391  }
392  return std::shared_ptr<ccGenericPointCloud>(
393  cloud.createNewCloudFromVisibilitySelection(
394  remove_selected_points, visTable,
395  nullptr, silent));
396  },
397  "Creates a new point cloud with only the 'visible' points "
398  "(as defined by the visibility array).",
399  "remove_selected_points"_a = false, "vis_table"_a = nullptr,
400  "silent"_a = false)
401  .def(
402  "crop",
403  [](ccGenericPointCloud& cloud, const ccBBox& box,
404  bool inside) {
405  return std::shared_ptr<ReferenceCloud>(
406  cloud.crop(box, inside));
407  },
408  "Crops the cloud inside (or outside) a bounding box.",
409  "box"_a, "inside"_a = true)
410  .def(
411  "crop",
412  [](ccGenericPointCloud& cloud, const ecvOrientedBBox& box) {
413  return std::shared_ptr<ReferenceCloud>(cloud.crop(box));
414  },
415  "Crops the cloud inside a oriented bounding box.", "box"_a)
416  .def("remove_points", &ccGenericPointCloud::removePoints,
417  "Remove points.", "index"_a)
418  .def("set_point_size", &ccGenericPointCloud::setPointSize,
419  "Sets point size", "size"_a)
420  .def("get_point_size", &ccGenericPointCloud::getPointSize,
421  "Returns current point size")
422  .def(
423  "import_parameters_from",
424  [](ccGenericPointCloud& cloud,
425  const ccGenericPointCloud& source) {
426  cloud.importParametersFrom(&source);
427  },
428  "Imports the parameters from another cloud", "source"_a)
429  .def("compute_mean_and_covariance",
431  "Function to compute the mean and covariance matrix of a "
432  "point cloud.");
433 
434  docstring::ClassMethodDocInject(m, "ccGenericPointCloud", "clone");
435  docstring::ClassMethodDocInject(m, "ccGenericPointCloud", "clear");
436  docstring::ClassMethodDocInject(m, "ccGenericPointCloud",
437  "get_scalar_value_color");
438  docstring::ClassMethodDocInject(m, "ccGenericPointCloud",
439  "get_point_scalar_value_color");
440  docstring::ClassMethodDocInject(m, "ccGenericPointCloud",
441  "get_point_color");
442  docstring::ClassMethodDocInject(m, "ccGenericPointCloud",
443  "get_point_displayed_distance");
444  docstring::ClassMethodDocInject(m, "ccGenericPointCloud",
445  "get_point_normal_index");
446  docstring::ClassMethodDocInject(m, "ccGenericPointCloud",
447  "get_point_normal");
448  docstring::ClassMethodDocInject(m, "ccGenericPointCloud",
449  "get_visibility_array");
450  docstring::ClassMethodDocInject(m, "ccGenericPointCloud",
451  "get_visible_points");
452  docstring::ClassMethodDocInject(m, "ccGenericPointCloud",
453  "is_visibility_table_instantiated");
454  docstring::ClassMethodDocInject(m, "ccGenericPointCloud",
455  "reset_visibility_array");
456  docstring::ClassMethodDocInject(m, "ccGenericPointCloud",
457  "invert_visibility_array");
458  docstring::ClassMethodDocInject(m, "ccGenericPointCloud",
459  "unallocate_visibility_array");
460  docstring::ClassMethodDocInject(m, "ccGenericPointCloud", "refresh_bbox");
461  docstring::ClassMethodDocInject(m, "ccGenericPointCloud",
462  "create_cloud_from_visibility_selection");
464  m, "ccGenericPointCloud", "crop",
465  {{"box", "Axis Aligned BoundingBox"},
466  {"inside",
467  "whether selected points are inside or outside the box"}});
468  docstring::ClassMethodDocInject(m, "ccGenericPointCloud", "crop",
469  {{"box", "Oriented BoundingBox"}});
470  docstring::ClassMethodDocInject(m, "ccGenericPointCloud", "remove_points");
471  docstring::ClassMethodDocInject(m, "ccGenericPointCloud", "set_point_size");
472  docstring::ClassMethodDocInject(m, "ccGenericPointCloud", "get_point_size");
473  docstring::ClassMethodDocInject(m, "ccGenericPointCloud",
474  "import_parameters_from");
475  docstring::ClassMethodDocInject(m, "ccGenericPointCloud",
476  "compute_mean_and_covariance");
477 }
478 
479 void pybind_cloudbase_methods(py::module& m) {}
480 
481 } // namespace geometry
482 } // namespace cloudViewer
filament::Texture::InternalFormat format
static Vector3Tpl fromArray(const int a[3])
Constructor from an int array.
Definition: CVGeom.h:268
Bounding box structure.
Definition: ecvBBox.h:25
A 3D cloud interface with associated features (color, normals, octree, etc.)
unsigned char getPointSize() const
Returns current point size.
virtual void refreshBB()=0
Forces bounding-box update.
virtual bool isVisibilityTableInstantiated() const
Returns whether the visibility array is allocated or not.
virtual void removePoints(size_t index)=0
void setPointSize(unsigned size=0)
Sets point size.
std::tuple< Eigen::Vector3d, Eigen::Matrix3d > computeMeanAndCovariance() const
virtual void invertVisibilityArray()
Inverts the visibility array.
virtual ScalarType getPointDisplayedDistance(unsigned pointIndex) const =0
Returns scalar value associated to a given point.
virtual bool resetVisibilityArray()
Resets the associated visibility array.
std::vector< unsigned char > VisibilityTableType
Array of "visibility" information for each point.
virtual const CompressedNormType & getPointNormalIndex(unsigned pointIndex) const =0
Returns compressed normal corresponding to a given point.
virtual void unallocateVisibilityArray()
Erases the points visibility information.
virtual void clear()
Clears the entity from all its points and features.
Hierarchical CLOUDVIEWER Object.
Definition: ecvHObject.h:25
virtual void getBoundingBox(CCVector3 &bbMin, CCVector3 &bbMax)=0
Returns the cloud bounding box.
virtual bool enableScalarField()=0
Enables the scalar field associated to the cloud.
virtual void placeIteratorAtBeginning()=0
Sets the cloud iterator at the beginning.
virtual unsigned char testVisibility(const CCVector3 &P) const
Definition: GenericCloud.h:65
virtual const CCVector3 * getNextPoint()=0
Returns the next point (relatively to the global iterator position)
virtual unsigned size() const =0
Returns the number of points.
virtual ScalarType getPointScalarValue(unsigned pointIndex) const =0
Returns the ith point associated scalar value.
virtual bool hasPoints() const
Definition: GenericCloud.h:37
virtual void setPointScalarValue(unsigned pointIndex, ScalarType value)=0
Sets the ith point associated scalar value.
virtual bool isScalarFieldEnabled() const =0
Returns true if the scalar field is enabled, false otherwise.
A generic 3D point cloud with index-based and presistent access to points.
A generic 3D point cloud with index-based point access.
A simple polyline class.
Definition: Polyline.h:20
A very simple point cloud (no point duplication)
virtual ScalarType getCurrentPointScalarValue() const
Returns the current point associated scalar value.
virtual void swap(unsigned i, unsigned j)
Swaps two point references.
virtual bool addPointIndex(unsigned globalIndex)
Point global index insertion mechanism.
virtual unsigned getCurrentPointGlobalIndex() const
Returns the global index of the point pointed by the current element.
unsigned size() const override
Returns the number of points.
virtual void setPointIndex(unsigned localIndex, unsigned globalIndex)
Sets global index for a given element.
void invalidateBoundingBox()
Invalidates the bounding-box.
virtual void removeCurrentPointGlobalIndex()
Removes current element.
virtual unsigned getPointGlobalIndex(unsigned localIndex) const
virtual bool resize(unsigned n)
Presets the size of the vector used to store point references.
virtual unsigned capacity() const
Returns max capacity.
virtual void clear(bool releaseMemory=false)
Clears the cloud.
virtual void setCurrentPointScalarValue(ScalarType value)
Sets the current point associated scalar value.
bool add(const ReferenceCloud &cloud)
Add another reference cloud.
virtual bool reserve(unsigned n)
Reserves some memory for hosting the point references.
virtual void removePointGlobalIndex(unsigned localIndex)
Removes a given element.
virtual void forwardIterator()
Forwards the local element iterator.
static Eigen::Vector3d ToEigen(const Type col[3])
Definition: ecvColorTypes.h:72
#define LogWarning(...)
Definition: Logging.h:72
void ClassMethodDocInject(py::module &pybind_module, const std::string &class_name, const std::string &function_name, const std::unordered_map< std::string, std::string > &map_parameter_body_docs)
Definition: docstring.cpp:27
void pybind_cloudbase(py::module &m)
Definition: cloudbase.cpp:30
void pybind_cloudbase_methods(py::module &m)
Definition: cloudbase.cpp:479
Generic file read and write utility for python interface.
Definition: lsd.c:149