ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Neighbourhood.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 // Local
11 #include "CVMiscTools.h"
13 #include "SquareMatrix.h"
14 
15 namespace cloudViewer {
16 
17 class GenericIndexedMesh;
18 
21 
26 public:
27  static constexpr int IGNORE_MAX_EDGE_LENGTH = 0;
28 
29  static constexpr bool DUPLICATE_VERTICES = true;
30  static constexpr bool DO_NOT_DUPLICATE_VERTICES = false;
31 
34  enum GeomElement {
35  FLAG_DEPRECATED = 0,
36  FLAG_GRAVITY_CENTER = 1,
37  FLAG_LS_PLANE = 2,
38  FLAG_QUADRIC = 4
39  };
40 
42  enum CurvatureType { GAUSSIAN_CURV = 1, MEAN_CURV, NORMAL_CHANGE_RATE };
43 
45 
47  explicit Neighbourhood(GenericIndexedCloudPersist* associatedCloud);
48 
50  virtual ~Neighbourhood() = default;
51 
53  virtual void reset();
54 
57  return m_associatedCloud;
58  }
59 
61 
67  GenericIndexedMesh* triangulateOnPlane(bool duplicateVertices,
68  PointCoordinateType maxEdgeLength,
69  std::string& outputErrorStr);
70 
73  GenericIndexedMesh* triangulateFromQuadric(unsigned stepsX,
74  unsigned stepsY);
75 
76  enum InputVectorsUsage { UseOXYasBase, UseYAsUpDir, None };
77 
79 
88  template <class Vec2D>
90  std::vector<Vec2D>& points2D,
91  const PointCoordinateType* planeEquation = nullptr,
92  CCVector3* O = nullptr,
93  CCVector3* X = nullptr,
94  CCVector3* Y = nullptr,
95  InputVectorsUsage vectorsUsage = None) {
96  // need at least one point ;)
97  unsigned count = (m_associatedCloud ? m_associatedCloud->size() : 0);
98  if (!count) return false;
99 
100  // if no custom plane equation is provided, get the default best LS one
101  if (!planeEquation) {
102  planeEquation = getLSPlane();
103  if (!planeEquation) return false;
104  }
105 
106  // reserve memory for output set
107  try {
108  points2D.resize(count);
109  } catch (const std::bad_alloc&) {
110  // out of memory
111  return false;
112  }
113 
114  // we construct the plane local base
115  CCVector3 G(0, 0, 0), u(1, 0, 0), v(0, 1, 0);
116  if ((vectorsUsage == UseOXYasBase) && O && X && Y) {
117  G = *O;
118  u = *X;
119  v = *Y;
120  } else {
121  CCVector3 N(planeEquation);
122  if ((vectorsUsage == UseYAsUpDir) && Y) {
123  v = (*Y - Y->dot(N) * N);
124  v.normalize();
125  u = v.cross(N);
126  } else {
128  }
129  // get the barycenter
130  const CCVector3* _G = getGravityCenter();
131  assert(_G);
132  G = *_G;
133  }
134 
135  // project the points
136  for (unsigned i = 0; i < count; ++i) {
137  // we recenter current point
138  const CCVector3 P = *m_associatedCloud->getPoint(i) - G;
139 
140  // then we project it on plane (with scalar prods)
141  points2D[i] = Vec2D(P.dot(u), P.dot(v));
142  }
143 
144  // output the local base if necessary
145  if (vectorsUsage != UseOXYasBase) {
146  if (O) *O = G;
147  if (X) *X = u;
148  if (Y) *Y = v;
149  }
150 
151  return true;
152  }
153 
155 
161  enum GeomFeature {
162  EigenValuesSum = 1,
175  EigenValue3
176  };
177 
179 
181  double computeFeature(GeomFeature feature);
182 
185 
187  ScalarType computeMomentOrder1(const CCVector3& P);
188 
191 
196  ScalarType computeRoughness(const CCVector3& P,
197  const CCVector3* roughnessUpDir = nullptr);
198 
200 
203  ScalarType computeCurvature(const CCVector3& P, CurvatureType cType);
204 
205  /**** GETTERS ****/
206 
208 
210  const CCVector3* getGravityCenter();
211 
213 
216  void setGravityCenter(const CCVector3& G);
217 
219 
223  const PointCoordinateType* getLSPlane();
224 
226 
232  void setLSPlane(const PointCoordinateType eq[4],
233  const CCVector3& X,
234  const CCVector3& Y,
235  const CCVector3& N);
236 
238 
241  const CCVector3* getLSPlaneX();
243 
246  const CCVector3* getLSPlaneY();
248 
251  const CCVector3* getLSPlaneNormal();
252 
254 
260  const PointCoordinateType* getQuadric(Tuple3ub* dims = nullptr);
261 
263 
268  bool compute3DQuadric(double quadricEquation[10]);
269 
271  cloudViewer::SquareMatrixd computeCovarianceMatrix();
272 
275  PointCoordinateType computeLargestRadius();
276 
277 protected:
279 
284  PointCoordinateType m_quadricEquation[6];
285 
287 
291 
293 
296  PointCoordinateType m_lsPlaneEquation[4];
297 
299 
301  CCVector3 m_lsPlaneVectors[3];
302 
304 
307 
309  unsigned char m_structuresValidity;
310 
312  void computeGravityCenter();
314  bool computeLeastSquareBestFittingPlane();
316  bool computeQuadric();
317 
320 };
321 
322 } // namespace cloudViewer
#define CV_CORE_LIB_API
Definition: CVCoreLibWin.h:15
float PointCoordinateType
Type of the coordinates of a (N-D) point.
Definition: CVTypes.h:16
int count
void * X
Definition: SmallVector.cpp:45
void normalize()
Sets vector norm to unity.
Definition: CVGeom.h:428
Type dot(const Vector3Tpl &v) const
Dot product.
Definition: CVGeom.h:408
Vector3Tpl cross(const Vector3Tpl &v) const
Cross product.
Definition: CVGeom.h:412
static void ComputeBaseVectors(const CCVector3 &N, CCVector3 &X, CCVector3 &Y)
Computes base vectors for a given 3D plane.
A generic 3D point cloud with index-based and presistent access to points.
A generic mesh with index-based vertex access.
bool projectPointsOn2DPlane(std::vector< Vec2D > &points2D, const PointCoordinateType *planeEquation=nullptr, CCVector3 *O=nullptr, CCVector3 *X=nullptr, CCVector3 *Y=nullptr, InputVectorsUsage vectorsUsage=None)
Projects points on the best fitting LS plane.
Definition: Neighbourhood.h:89
virtual ~Neighbourhood()=default
Default destructor.
GeomFeature
Geometric feature computed from eigen values/vectors.
CCVector3 m_gravityCenter
Gravity center.
GenericIndexedCloudPersist * associatedCloud() const
Returns associated cloud.
Definition: Neighbourhood.h:56
CurvatureType
Curvature type.
Definition: Neighbourhood.h:42
GenericIndexedCloudPersist * m_associatedCloud
Associated cloud.
unsigned char m_structuresValidity
Geometrical elements validity (flags)
Tuple3ub m_quadricEquationDirections
2.5D Quadric equation dimensions
Generic file read and write utility for python interface.