ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
polyline.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 // CV_CORE_LIB
9 #include <Logging.h>
10 #include <ReferenceCloud.h>
11 
12 // CV_DB_LIB
13 #include <ecvCircle.h>
14 #include <ecvFacet.h>
15 #include <ecvMesh.h>
16 #include <ecvObject.h>
18 #include <ecvPointCloud.h>
19 #include <ecvPolyline.h>
20 
21 // LOCAL
22 #include "pybind/docstring.h"
26 
27 using namespace cloudViewer;
28 namespace cloudViewer {
29 namespace geometry {
30 
31 void pybind_polyline(py::module& m) {
32  py::class_<cloudViewer::Polyline,
34  std::shared_ptr<cloudViewer::Polyline>, ReferenceCloud>
35  polyline(m, "Polyline",
36  "The polyline is considered as a cloud of points "
37  "(in a specific order) with a open closed state "
38  "information..");
39  polyline.def(py::init([](std::shared_ptr<GenericIndexedCloudPersist>
40  associated_cloud) {
41  return new cloudViewer::Polyline(associated_cloud.get());
42  }),
43  "Polyline constructor", "associated_cloud"_a)
44  .def("__repr__",
45  [](const cloudViewer::Polyline& poly) {
46  std::string info = fmt::format(
47  "cloudViewer::Polyline with {} points and is "
48  "closed {}",
49  poly.size(), poly.isClosed() ? "True" : "False");
50  return info;
51  })
52  .def("is_closed", &cloudViewer::Polyline::isClosed,
53  "Returns whether the polyline is closed or not.")
54  .def("set_closed", &cloudViewer::Polyline::setClosed,
55  "Sets whether the polyline is closed or not.", "state"_a);
56  docstring::ClassMethodDocInject(m, "Polyline", "is_closed");
57  docstring::ClassMethodDocInject(m, "Polyline", "set_closed");
58 
59  // cloudViewer.geometry.ccPolyline
60  py::class_<ccPolyline, PyGeometry<ccPolyline>, std::shared_ptr<ccPolyline>,
62  pyply(m, "ccPolyline", py::multiple_inheritance(),
63  "Colored polyline, Extends the cloudViewer::Polyline class.");
64  py::detail::bind_copy_functions<ccPolyline>(pyply);
65  // pyply.def(py::init([](std::shared_ptr<cloudViewer::GenericIndexedCloudPersist>
66  // cloud) { return new ccPolyline(cloud.get());
67  // }), "cloud -> the associated point cloud (i.e. the vertices)", "cloud"_a
68  // = nullptr)
69  pyply.def(py::init([](std::shared_ptr<ccPointCloud> cloud) {
70  if (cloud) {
71  return new ccPolyline(*cloud);
72  } else {
73  return new ccPolyline(nullptr);
74  }
75  }),
76  "cloud -> the associated point cloud (i.e. the vertices)",
77  "cloud"_a = nullptr)
78  .def("__repr__",
79  [](const ccPolyline& polyline) {
80  bool is2D = polyline.is2DMode();
81  PointCoordinateType length = polyline.computeLength();
82  std::string info = fmt::format(
83  "ccPolyline with segment count {}, length {} and "
84  "2D {}",
85  polyline.segmentCount(), length,
86  is2D ? "True" : "False");
87  return info;
88  })
89  .def(py::self + py::self)
90  .def(py::self += py::self)
91  .def("append", &ccPolyline::add, "Append another point cloud.",
92  "cloud"_a)
93  .def("set_2d_mode", &ccPolyline::set2DMode,
94  "Defines if the polyline is considered as 2D or 3D.",
95  "state"_a)
96  .def("is_2d_mode", &ccPolyline::is2DMode,
97  "Returns whether the polyline is considered as 2D or 3D.")
98  .def("set_transform_flag", &ccPolyline::setTransformFlag,
99  "Defines if the polyline is considered as processed polyline.",
100  "state"_a)
101  .def("need_transform", &ccPolyline::needTransform,
102  "Returns whether the polyline is considered as 2D or 3D.")
103  .def("set_width", &ccPolyline::setWidth,
104  "Sets the width of the line.", "width"_a)
105  .def("get_width", &ccPolyline::getWidth,
106  "Returns the width of the line.")
107  .def("compute_length", &ccPolyline::computeLength,
108  "Computes the polyline length.")
109  .def("segment_count", &ccPolyline::segmentCount,
110  " Returns the number of segments.")
111  .def("paint_uniform_color", &ccPolyline::PaintUniformColor,
112  " Assigns each line in the polyline the same color.",
113  "color"_a)
114  .def(
115  "set_color",
116  [](ccPolyline& polyline, const Eigen::Vector3d& color) {
117  polyline.setColor(ecvColor::Rgb::FromEigen(color));
118  },
119  "Sets the polyline color.", "color"_a)
120  .def(
121  "get_color",
122  [](const ccPolyline& polyline) {
123  return ecvColor::Rgb::ToEigen(polyline.getColor());
124  },
125  "Returns the polyline color.")
126  .def(
127  "split",
128  [](ccPolyline& polyline,
129  PointCoordinateType max_edge_length) {
130  std::vector<ccPolyline*> parts;
131  bool success = polyline.split(max_edge_length, parts);
132  std::vector<std::shared_ptr<ccPolyline>> outParts;
133  for (size_t i = 0; i < parts.size(); ++i) {
134  outParts.push_back(
135  std::shared_ptr<ccPolyline>(parts[i]));
136  }
137  return std::make_tuple(outParts, success);
138  },
139  "Splits the polyline into several parts based on a maximum "
140  "edge length",
141  "max_edge_length"_a)
142  .def("get_arrow_index", &ccPolyline::getArrowIndex,
143  "Returns arrow index.")
144  .def("get_arrow_length", &ccPolyline::getArrowLength,
145  "Returns arrow length.")
146  .def("vertices_shown", &ccPolyline::verticesShown,
147  "Whether the polyline vertices should be displayed or not.")
148  .def("show_vertices", &ccPolyline::showVertices,
149  "Sets whether to display or hide the polyline vertices.",
150  "state"_a)
151  .def("get_vertex_marker_width", &ccPolyline::getVertexMarkerWidth,
152  "Returns the width of vertex markers.")
153  .def("set_vertex_marker_width", &ccPolyline::setVertexMarkerWidth,
154  "Sets the width of vertex markers.", "width"_a)
155  .def(
156  "init_with",
157  [](ccPolyline& polyline, ccPointCloud& vertices,
158  const ccPolyline& other_polyline) {
159  ccPointCloud* cloud = &vertices;
160  return polyline.initWith(cloud, other_polyline);
161  },
162  "Initializes the polyline with a given set of vertices and "
163  "the parameters of another polyline",
164  "vertices"_a, "other_polyline"_a)
165  .def(
166  "import_parameters_from",
167  [](ccPolyline& polyline, const ccPolyline& other_polyline) {
168  polyline.importParametersFrom(other_polyline);
169  },
170  "Copy the parameters from another polyline",
171  "other_polyline"_a)
172  .def("show_arrow", &ccPolyline::showArrow,
173  "Shows an arrow in place of a given vertex.", "state"_a,
174  "vertex_index"_a, "length"_a)
175  .def(
176  "sample_points",
177  [](ccPolyline& polyline, bool density_based,
178  double sampling_parameter, bool with_rgb) {
179  ccPointCloud* sampledCloud = polyline.samplePoints(
180  density_based, sampling_parameter, with_rgb);
181  return std::shared_ptr<ccPointCloud>(sampledCloud);
182  },
183  "Samples points on the polyline", "density_based"_a,
184  "sampling_parameter"_a, "with_rgb"_a);
185 
186  docstring::ClassMethodDocInject(m, "ccPolyline", "segment_count");
187  docstring::ClassMethodDocInject(m, "ccPolyline", "compute_length");
188  docstring::ClassMethodDocInject(m, "ccPolyline", "append");
189  docstring::ClassMethodDocInject(m, "ccPolyline", "set_2d_mode");
190  docstring::ClassMethodDocInject(m, "ccPolyline", "is_2d_mode");
191  docstring::ClassMethodDocInject(m, "ccPolyline", "set_transform_flag");
192  docstring::ClassMethodDocInject(m, "ccPolyline", "need_transform");
193  docstring::ClassMethodDocInject(m, "ccPolyline", "paint_uniform_color");
194  docstring::ClassMethodDocInject(m, "ccPolyline", "get_color");
195  docstring::ClassMethodDocInject(m, "ccPolyline", "set_color",
196  {{"color", "The polyline rgb color."}});
197  docstring::ClassMethodDocInject(m, "ccPolyline", "get_width");
198  docstring::ClassMethodDocInject(m, "ccPolyline", "set_width",
199  {{"width", "The polyline width."}});
201  m, "ccPolyline", "split",
202  {{"max_edge_length",
203  "maximum edge length "
204  "(warning output polylines set (parts) may be "
205  "empty if all the vertices are too far from each other!)"}});
206  docstring::ClassMethodDocInject(m, "ccPolyline", "get_arrow_index");
207  docstring::ClassMethodDocInject(m, "ccPolyline", "get_arrow_length");
208  docstring::ClassMethodDocInject(m, "ccPolyline", "vertices_shown");
209  docstring::ClassMethodDocInject(m, "ccPolyline", "show_vertices");
210  docstring::ClassMethodDocInject(m, "ccPolyline", "get_vertex_marker_width");
211  docstring::ClassMethodDocInject(m, "ccPolyline", "set_vertex_marker_width");
213  m, "ccPolyline", "init_with",
214  {{"vertices",
215  "set of vertices (can be null, in which case the polyline "
216  "vertices will be cloned)."},
217  {"other_polyline", "The other polyline."}});
219  m, "ccPolyline", "import_parameters_from",
220  {{"other_polyline", "The other polyline."}});
221  docstring::ClassMethodDocInject(m, "ccPolyline", "show_arrow",
222  {{"state", "The state."},
223  {"vertex_index", "The vertex index."},
224  {"length", "The length."}});
226  m, "ccPolyline", "sample_points",
227  {{"density_based", "The density based."},
228  {"sampling_parameter", "The sampling parameter."},
229  {"with_rgb", "with rgb."}});
230 
231  // cloudViewer.geometry.ccCircle
232  py::class_<ccCircle, PyGeometry<ccCircle>, std::shared_ptr<ccCircle>,
233  ccPolyline>
234  pycircle(m, "ccCircle", py::multiple_inheritance(),
235  "A 3D circle represented as a polyline.");
236  py::detail::bind_copy_functions<ccCircle>(pycircle);
237  pycircle.def(py::init([](double radius, unsigned resolution,
238  unsigned uniqueID) {
239  return new ccCircle(radius, resolution, uniqueID);
240  }),
241  "Circle constructor", "radius"_a = 0.0, "resolution"_a = 48,
243  .def("__repr__",
244  [](const ccCircle& circle) {
245  std::string info = fmt::format(
246  "ccCircle with radius {}, resolution {} and {} "
247  "segments",
248  circle.getRadius(), circle.getResolution(),
249  circle.segmentCount());
250  return info;
251  })
252  .def("get_radius", &ccCircle::getRadius,
253  "Returns the radius of the circle.")
254  .def("set_radius", &ccCircle::setRadius,
255  "Sets the radius of the circle.", "radius"_a)
256  .def("get_resolution", &ccCircle::getResolution,
257  "Returns the resolution of the displayed circle.")
258  .def("set_resolution", &ccCircle::setResolution,
259  "Sets the resolution of the displayed circle.", "resolution"_a)
260  .def(
261  "clone",
262  [](const ccCircle& circle) {
263  return std::shared_ptr<ccCircle>(circle.clone());
264  },
265  "Clones this circle.");
266 
267  docstring::ClassMethodDocInject(m, "ccCircle", "get_radius");
268  docstring::ClassMethodDocInject(m, "ccCircle", "set_radius",
269  {{"radius", "The desired radius."}});
270  docstring::ClassMethodDocInject(m, "ccCircle", "get_resolution");
272  m, "ccCircle", "set_resolution",
273  {{"resolution", "The displayed resolution (>= 4)."}});
274  docstring::ClassMethodDocInject(m, "ccCircle", "clone");
275 }
276 
277 void pybind_polyline_methods(py::module& m) {}
278 
279 } // namespace geometry
280 } // namespace cloudViewer
float PointCoordinateType
Type of the coordinates of a (N-D) point.
Definition: CVTypes.h:16
filament::Texture::InternalFormat format
math::float4 color
Circle (as a polyline)
Definition: ecvCircle.h:16
ccCircle * clone() const
Clones this circle.
double getRadius() const
Returns the radius of the circle.
Definition: ecvCircle.h:50
unsigned getResolution() const
Returns the resolution of the displayed circle.
Definition: ecvCircle.h:58
void setRadius(double radius)
Sets the radius of the circle.
void setResolution(unsigned resolution)
Sets the resolution of the displayed circle.
Hierarchical CLOUDVIEWER Object.
Definition: ecvHObject.h:25
A 3D cloud and its associated features (color, normals, scalar fields, etc.)
Colored polyline.
Definition: ecvPolyline.h:24
void setTransformFlag(bool state)
Defines if the polyline is considered as processed polyline.
Definition: ecvPolyline.h:68
ccPolyline & PaintUniformColor(const Eigen::Vector3d &color)
Assigns each line in the LineSet the same color.
Definition: ecvPolyline.h:210
PointCoordinateType computeLength() const
Computes the polyline length.
PointCoordinateType getArrowLength() const
Definition: ecvPolyline.h:134
bool verticesShown() const
Whether the polyline vertices should be displayed or not.
Definition: ecvPolyline.h:131
void set2DMode(bool state)
Defines if the polyline is considered as 2D or 3D.
bool needTransform() const
Returns whether the polyline is considered as 2D or 3D.
Definition: ecvPolyline.h:71
bool is2DMode() const
Returns whether the polyline is considered as 2D or 3D.
Definition: ecvPolyline.h:63
void showArrow(bool state, unsigned vertIndex, PointCoordinateType length)
Shows an arrow in place of a given vertex.
void setVertexMarkerWidth(int width)
Sets the width of vertex markers.
Definition: ecvPolyline.h:137
unsigned getArrowIndex() const
Definition: ecvPolyline.h:133
void showVertices(bool state)
Sets whether to display or hide the polyline vertices.
Definition: ecvPolyline.h:129
bool add(const ccPointCloud &cloud)
Add another reference cloud.
unsigned segmentCount() const
Returns the number of segments.
PointCoordinateType getWidth() const
Returns the width of the line.
Definition: ecvPolyline.h:94
int getVertexMarkerWidth() const
Returns the width of vertex markers.
Definition: ecvPolyline.h:139
void setWidth(PointCoordinateType width)
Sets the width of the line.
static constexpr unsigned InvalidUniqueID
Definition: ecvObject.h:24
A simple polyline class.
Definition: Polyline.h:20
void setClosed(bool state)
Sets whether the polyline is closed or not.
Definition: Polyline.h:29
bool isClosed() const
Returns whether the polyline is closed or not.
Definition: Polyline.h:26
A very simple point cloud (no point duplication)
unsigned size() const override
Returns the number of points.
constexpr static RgbTpl FromEigen(const Eigen::Vector3d &t)
Definition: ecvColorTypes.h:86
static Eigen::Vector3d ToEigen(const Type col[3])
Definition: ecvColorTypes.h:72
__host__ __device__ float length(float2 v)
Definition: cutil_math.h:1162
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_polyline(py::module &m)
Definition: polyline.cpp:31
void pybind_polyline_methods(py::module &m)
Definition: polyline.cpp:277
Generic file read and write utility for python interface.