ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
facet.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 
11 // CV_DB_LIB
12 #include <ecvFacet.h>
13 #include <ecvMesh.h>
15 #include <ecvPointCloud.h>
16 #include <ecvPolyline.h>
17 
18 // LOCAL
19 #include "pybind/docstring.h"
22 
23 #ifdef CV_WINDOWS
24 #pragma warning(disable : 4715)
25 #endif
26 
27 namespace cloudViewer {
28 namespace geometry {
29 
30 void pybind_facet(py::module& m) {
31  // cloudViewer.geometry.ccFacet
32  py::class_<ccFacet, PyGeometry<ccFacet>, std::shared_ptr<ccFacet>,
34  pyfacet(m, "ccFacet", py::multiple_inheritance(),
35  "The Composite object: "
36  "point cloud + 2D1/2 contour polyline + 2D1/2 surface "
37  "mesh.");
38  py::detail::bind_default_constructor<ccFacet>(pyfacet);
39  py::detail::bind_copy_functions<ccFacet>(pyfacet);
40  pyfacet.def(py::init([](PointCoordinateType max_edge_length,
41  const std::string& name) {
42  return new ccFacet(max_edge_length, name.c_str());
43  }),
44  "-param max_edge_length max edge length (if possible - ignored "
45  "if 0); "
46  "\n"
47  "-param name name",
48  "max_edge_length"_a = 0, "name"_a = "Facet")
49  .def("__repr__",
50  [](const ccFacet& facet) {
51  double area = facet.getSurface();
52  double rms = facet.getRMS();
53  unsigned facesNumber = 0;
54  unsigned verticeNumber = 0;
55  PointCoordinateType perimeter = 0.0f;
56  if (facet.getPolygon()) {
57  facesNumber = facet.getPolygon()->size();
58  }
59  if (facet.getContour()) {
60  verticeNumber = facet.getContour()->size();
61  perimeter = facet.getContour()->computeLength();
62  }
63 
64  std::string info = fmt::format(
65  "ccFacet (with {} faces, {} points, area {}, "
66  "perimeter {}, rms {})",
67  facesNumber, verticeNumber, area, perimeter, rms);
68  return info;
69  })
70  .def(py::self + py::self)
71  .def(py::self += py::self)
72  .def("get_rms", &ccFacet::getRMS, "Returns associated RMS.")
73  .def("get_area", &ccFacet::getSurface,
74  "Returns associated surface area.")
75  .def("get_normal_mesh", &ccFacet::getNormalVectorMesh,
76  "Gets normal vector mesh.", "update"_a = false)
77  .def(
78  "set_color",
79  [](ccFacet& facet, const Eigen::Vector3d& color) {
81  },
82  "Sets the facet unique color.", "color"_a)
83  .def(
84  "get_center",
85  [](const ccFacet& facet) {
86  return CCVector3d::fromArray(facet.getCenter());
87  },
88  "Returns the facet center.")
89  .def(
90  "get_plane_equation",
91  [](const ccFacet& facet) {
92  const PointCoordinateType* eq =
93  facet.getPlaneEquation();
94  Eigen::Vector4d equation(4);
95  for (size_t i = 0; i < 4; ++i) {
96  equation(4) = eq[i];
97  }
98  return equation;
99  },
100  "Returns Plane equation - as usual in CC plane equation is "
101  "ax + by + cz = d")
102  .def("invert_normal", &ccFacet::invertNormal,
103  "Inverts the facet normal.")
104  .def(
105  "get_polygon",
106  [](ccFacet& facet) {
107  if (facet.getPolygon()) {
108  return std::ref(*facet.getPolygon());
109  } else {
111  "[ccFacet] ccFacet do not have polygons!");
112  return std::ref(*std::make_shared<ccMesh>());
113  }
114  },
115  "Returns polygon mesh (if any)")
116  .def(
117  "set_polygon",
118  [](ccFacet& facet, ccMesh& mesh) {
119  facet.setPolygon(&mesh);
120  },
121  "Sets polygon mesh", "mesh"_a)
122  .def(
123  "get_contour",
124  [](ccFacet& facet) {
125  if (facet.getContour()) {
126  return std::ref(*facet.getContour());
127  } else {
129  "[ccFacet] ccFacet do not have contours!");
130  return std::ref(
131  *std::make_shared<ccPolyline>(nullptr));
132  }
133  },
134  "Returns contour polyline (if any)")
135  .def(
136  "set_contour",
137  [](ccFacet& facet, ccPolyline& poly) {
138  facet.setContour(&poly);
139  },
140  "Sets contour polyline", "poly"_a)
141  .def(
142  "get_contour_vertices",
143  [](ccFacet& facet) {
144  if (facet.getContourVertices()) {
145  return std::ref(*facet.getContourVertices());
146  } else {
148  "[ccFacet] ccFacet do not have origin "
149  "points!");
150  return std::ref(*std::make_shared<ccPointCloud>());
151  }
152  },
153  "Returns contour vertices (if any)")
154  .def(
155  "set_contour_vertices",
156  [](ccFacet& facet, ccPointCloud& vertices) {
157  facet.setContourVertices(&vertices);
158  },
159  "Sets contour vertices", "vertices"_a)
160  .def(
161  "get_origin_points",
162  [](ccFacet& facet) {
163  if (facet.getOriginPoints()) {
164  return std::ref(*facet.getOriginPoints());
165  } else {
167  "[ccFacet] ccFacet do not have origin "
168  "points!");
169  return std::ref(*std::make_shared<ccPointCloud>());
170  }
171  },
172  "Returns origin points (if any)")
173  .def(
174  "set_origin_points",
175  [](ccFacet& facet, ccPointCloud& cloud) {
176  facet.setOriginPoints(&cloud);
177  },
178  "Sets origin points", "cloud"_a)
179  .def(
180  "clone",
181  [](const ccFacet& facet) {
182  return std::shared_ptr<ccFacet>(facet.clone());
183  },
184  "Clones this facet.")
185  .def("paint_uniform_color", &ccFacet::PaintUniformColor,
186  " Assigns facet the same color.", "color"_a)
187  .def_static(
188  "Create",
189  [](std::shared_ptr<ccPointCloud> cloud,
190  PointCoordinateType max_edge_length,
191  bool transfer_ownership,
192  const Eigen::Vector4d& plane_equation) {
194  cloud.get();
195  if (!persistCloud) {
197  "[ccFacet::Create] Illegal input "
198  "parameters, only support point cloud!");
199  return std::make_shared<ccFacet>();
200  }
201 
202  PointCoordinateType eq[4];
203  for (size_t i = 0; i < 4; ++i) {
204  eq[i] = static_cast<PointCoordinateType>(
205  plane_equation(i));
206  }
207  ccFacet* facet = ccFacet::Create(
208  persistCloud, max_edge_length,
209  transfer_ownership,
210  plane_equation.isZero() ? nullptr : eq);
211  return std::shared_ptr<ccFacet>(facet);
212  },
213  "Creates a facet from a set of points", "cloud"_a,
214  "max_edge_length"_a = 0, "transfer_ownership"_a = false,
215  "plane_equation"_a = Eigen::Vector4d::Zero());
216 
217  docstring::ClassMethodDocInject(m, "ccFacet", "clone");
218  docstring::ClassMethodDocInject(m, "ccFacet", "get_rms");
219  docstring::ClassMethodDocInject(m, "ccFacet", "get_area");
220  docstring::ClassMethodDocInject(m, "ccFacet", "set_color",
221  {{"color", "facet rgb color."}});
222  docstring::ClassMethodDocInject(m, "ccFacet", "get_center");
223  docstring::ClassMethodDocInject(m, "ccFacet", "get_normal_mesh");
224  docstring::ClassMethodDocInject(m, "ccFacet", "get_plane_equation");
225  docstring::ClassMethodDocInject(m, "ccFacet", "paint_uniform_color");
226  docstring::ClassMethodDocInject(m, "ccFacet", "invert_normal");
227  docstring::ClassMethodDocInject(m, "ccFacet", "get_polygon");
228  docstring::ClassMethodDocInject(m, "ccFacet", "set_polygon");
229  docstring::ClassMethodDocInject(m, "ccFacet", "get_contour");
230  docstring::ClassMethodDocInject(m, "ccFacet", "set_contour");
231  docstring::ClassMethodDocInject(m, "ccFacet", "get_contour_vertices");
232  docstring::ClassMethodDocInject(m, "ccFacet", "set_contour_vertices");
233  docstring::ClassMethodDocInject(m, "ccFacet", "get_origin_points");
234  docstring::ClassMethodDocInject(m, "ccFacet", "set_origin_points");
235 }
236 
237 void pybind_facet_methods(py::module& m) {}
238 
239 } // namespace geometry
240 } // namespace cloudViewer
float PointCoordinateType
Type of the coordinates of a (N-D) point.
Definition: CVTypes.h:16
filament::Texture::InternalFormat format
std::string name
math::float4 color
static Vector3Tpl fromArray(const int a[3])
Constructor from an int array.
Definition: CVGeom.h:268
Facet.
Definition: ecvFacet.h:25
ccFacet * clone() const
Clones this facet.
ccMesh * getPolygon()
Returns polygon mesh (if any)
Definition: ecvFacet.h:81
double getSurface() const
Returns associated surface area.
Definition: ecvFacet.h:70
const PointCoordinateType * getPlaneEquation() const
Returns plane equation.
Definition: ecvFacet.h:72
ccPolyline * getContour()
Returns contour polyline (if any)
Definition: ecvFacet.h:86
void invertNormal()
Inverts the facet normal.
void setContour(ccPolyline *poly)
Sets contour polyline.
Definition: ecvFacet.h:107
double getRMS() const
Returns associated RMS.
Definition: ecvFacet.h:68
void setColor(const ecvColor::Rgb &rgb)
Sets the facet unique color.
void setContourVertices(ccPointCloud *cloud)
Sets contour vertices.
Definition: ecvFacet.h:109
static ccFacet * Create(cloudViewer::GenericIndexedCloudPersist *cloud, PointCoordinateType maxEdgeLength=0, bool transferOwnership=false, const PointCoordinateType *planeEquation=nullptr)
Creates a facet from a set of points.
ccFacet & PaintUniformColor(const Eigen::Vector3d &color)
Assigns each line in the LineSet the same color.
Definition: ecvFacet.h:148
const CCVector3 & getCenter() const
Returns the facet center.
Definition: ecvFacet.h:78
ccPointCloud * getContourVertices()
Returns contour vertices (if any)
Definition: ecvFacet.h:91
void setPolygon(ccMesh *mesh)
Sets polygon mesh.
Definition: ecvFacet.h:105
std::shared_ptr< ccMesh > getNormalVectorMesh(bool update=false)
Gets normal vector mesh.
void setOriginPoints(ccPointCloud *cloud)
Sets origin points.
Definition: ecvFacet.h:113
ccPointCloud * getOriginPoints()
Returns origin points (if any)
Definition: ecvFacet.h:98
Hierarchical CLOUDVIEWER Object.
Definition: ecvHObject.h:25
Triangular mesh.
Definition: ecvMesh.h:35
virtual unsigned size() const override
Returns the number of triangles.
Interface for a planar entity.
A 3D cloud and its associated features (color, normals, scalar fields, etc.)
Colored polyline.
Definition: ecvPolyline.h:24
PointCoordinateType computeLength() const
Computes the polyline length.
A generic 3D point cloud with index-based and presistent access to points.
unsigned size() const override
Returns the number of points.
constexpr static RgbTpl FromEigen(const Eigen::Vector3d &t)
Definition: ecvColorTypes.h:86
#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_facet(py::module &m)
Definition: facet.cpp:30
void pybind_facet_methods(py::module &m)
Definition: facet.cpp:237
Generic file read and write utility for python interface.