ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
LocalModel.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 "LocalModel.h"
9 
10 // local
12 #include "GenericIndexedMesh.h"
13 #include "GenericMesh.h"
14 #include "GenericTriangle.h"
15 
16 using namespace cloudViewer;
17 
19 class LSLocalModel : public LocalModel {
20 public:
23  const CCVector3& center,
24  PointCoordinateType squaredRadius)
25  : LocalModel(center, squaredRadius) {
26  memcpy(m_eq, eq, sizeof(PointCoordinateType) * 4);
27  }
28 
29  // inherited from LocalModel
30  CV_LOCAL_MODEL_TYPES getType() const override { return LS; }
31 
32  // inherited from LocalModel
34  const CCVector3* P,
35  CCVector3* nearestPoint = nullptr) const override {
36  ScalarType dist =
38 
39  if (nearestPoint) {
40  *nearestPoint = *P - static_cast<PointCoordinateType>(dist) *
41  CCVector3(m_eq);
42  }
43 
44  return std::abs(dist);
45  }
46 
47 protected:
50 };
51 
54 public:
57  const CCVector3& center,
58  PointCoordinateType squaredRadius)
59  : LocalModel(center, squaredRadius), m_tri(tri) {
60  assert(tri);
61  }
62 
64  ~DelaunayLocalModel() override { delete m_tri; }
65 
66  // inherited from LocalModel
67  CV_LOCAL_MODEL_TYPES getType() const override { return TRI; }
68 
69  // inherited from LocalModel
71  const CCVector3* P,
72  CCVector3* nearestPoint = nullptr) const override {
73  ScalarType minDist2 = NAN_VALUE;
74  if (m_tri) {
75  m_tri->placeIteratorAtBeginning();
76  unsigned numberOfTriangles = m_tri->size();
77  CCVector3 triNearestPoint;
78  for (unsigned i = 0; i < numberOfTriangles; ++i) {
79  GenericTriangle* tri = m_tri->_getNextTriangle();
80  ScalarType dist2 =
82  P, tri, false,
83  nearestPoint ? &triNearestPoint : nullptr);
84  if (dist2 < minDist2 || i == 0) {
85  // keep track of the smallest distance
86  minDist2 = dist2;
87  if (nearestPoint) *nearestPoint = triNearestPoint;
88  }
89  }
90  }
91 
92  // there should be at least one triangle!
93  assert(minDist2 == minDist2);
94 
95  return sqrt(minDist2);
96  }
97 
98 protected:
101 };
102 
104 
107 public:
110  unsigned char X,
111  unsigned char Y,
112  unsigned char Z,
113  CCVector3 gravityCenter,
114  const CCVector3& center,
115  PointCoordinateType squaredRadius)
116  : LocalModel(center, squaredRadius),
117  m_X(X),
118  m_Y(Y),
119  m_Z(Z),
120  m_gravityCenter(gravityCenter) {
121  memcpy(m_eq, eq, sizeof(PointCoordinateType) * 6);
122  }
123 
124  // inherited from LocalModel
125  CV_LOCAL_MODEL_TYPES getType() const override { return QUADRIC; }
126 
127  // inherited from LocalModel
129  const CCVector3* _P,
130  CCVector3* nearestPoint = nullptr) const override {
131  CCVector3 P = *_P - m_gravityCenter;
132 
133  // height = h0 + h1.x + h2.y + h3.x^2 + h4.x.y + h5.y^2
135  m_eq[0] + m_eq[1] * P.u[m_X] + m_eq[2] * P.u[m_Y] +
136  m_eq[3] * P.u[m_X] * P.u[m_X] + m_eq[4] * P.u[m_X] * P.u[m_Y] +
137  m_eq[5] * P.u[m_Y] * P.u[m_Y];
138 
139  if (nearestPoint) {
140  nearestPoint->u[m_X] = P.u[m_X];
141  nearestPoint->u[m_Y] = P.u[m_Y];
142  nearestPoint->u[m_Z] = z;
143  }
144 
145  return static_cast<ScalarType>(std::abs(P.u[m_Z] - z));
146  }
147 
148 protected:
152  unsigned char m_X;
154  unsigned char m_Y;
156  unsigned char m_Z;
159 };
160 
162  PointCoordinateType squaredRadius)
163  : m_modelCenter(center), m_squaredRadius(squaredRadius) {}
164 
166  Neighbourhood& subset,
167  const CCVector3& center,
168  PointCoordinateType squaredRadius) {
169  switch (type) {
170  case NO_MODEL:
171  assert(false);
172  break;
173 
174  case LS: {
175  const PointCoordinateType* lsPlane = subset.getLSPlane();
176  if (lsPlane) {
177  return new LSLocalModel(lsPlane, center, squaredRadius);
178  }
179  } break;
180 
181  case TRI: {
182  std::string errorStr;
183 
184  GenericMesh* tri = subset.triangulateOnPlane(
187  errorStr); //'subset' is potentially associated to a
188  // volatile ReferenceCloud, so we must duplicate
189  // vertices!
190  if (tri) {
191  return new DelaunayLocalModel(tri, center, squaredRadius);
192  }
193  } break;
194 
195  case QUADRIC: {
196  Tuple3ub dims;
197  const PointCoordinateType* eq = subset.getQuadric(&dims);
198  if (eq) {
199  return new QuadricLocalModel(
200  eq, dims.x, dims.y, dims.z,
201  *subset.getGravityCenter(), // should be ok as the
202  // quadric computation
203  // succeeded!
204  center, squaredRadius);
205  }
206  } break;
207  }
208 
209  // invalid input type or computation failed!
210  return nullptr;
211 }
CV_LOCAL_MODEL_TYPES
Definition: CVConst.h:121
@ LS
Definition: CVConst.h:123
@ NO_MODEL
Definition: CVConst.h:122
@ TRI
Definition: CVConst.h:124
@ QUADRIC
Definition: CVConst.h:125
constexpr ScalarType NAN_VALUE
NaN as a ScalarType value.
Definition: CVConst.h:76
Vector3Tpl< PointCoordinateType > CCVector3
Default 3D Vector.
Definition: CVGeom.h:798
float PointCoordinateType
Type of the coordinates of a (N-D) point.
Definition: CVTypes.h:16
char type
void * X
Definition: SmallVector.cpp:45
Delaunay 2D1/2 "local modelization".
Definition: LocalModel.cpp:53
ScalarType computeDistanceFromModelToPoint(const CCVector3 *P, CCVector3 *nearestPoint=nullptr) const override
Compute the (unsigned) distance between a 3D point and this model.
Definition: LocalModel.cpp:70
~DelaunayLocalModel() override
Destructor.
Definition: LocalModel.cpp:64
CV_LOCAL_MODEL_TYPES getType() const override
Returns the model type.
Definition: LocalModel.cpp:67
GenericMesh * m_tri
Associated triangulation.
Definition: LocalModel.cpp:100
DelaunayLocalModel(GenericMesh *tri, const CCVector3 &center, PointCoordinateType squaredRadius)
Constructor.
Definition: LocalModel.cpp:56
Least Squares Best Fitting Plane "local modelization".
Definition: LocalModel.cpp:19
LSLocalModel(const PointCoordinateType eq[4], const CCVector3 &center, PointCoordinateType squaredRadius)
Constructor.
Definition: LocalModel.cpp:22
CV_LOCAL_MODEL_TYPES getType() const override
Returns the model type.
Definition: LocalModel.cpp:30
ScalarType computeDistanceFromModelToPoint(const CCVector3 *P, CCVector3 *nearestPoint=nullptr) const override
Compute the (unsigned) distance between a 3D point and this model.
Definition: LocalModel.cpp:33
Quadric "local modelization".
Definition: LocalModel.cpp:106
unsigned char m_Y
Height function second dimension (0=X, 1=Y, 2=Z)
Definition: LocalModel.cpp:154
unsigned char m_X
Height function first dimension (0=X, 1=Y, 2=Z)
Definition: LocalModel.cpp:152
QuadricLocalModel(const PointCoordinateType eq[6], unsigned char X, unsigned char Y, unsigned char Z, CCVector3 gravityCenter, const CCVector3 &center, PointCoordinateType squaredRadius)
Constructor.
Definition: LocalModel.cpp:109
CV_LOCAL_MODEL_TYPES getType() const override
Returns the model type.
Definition: LocalModel.cpp:125
CCVector3 m_gravityCenter
Model gravity center.
Definition: LocalModel.cpp:158
ScalarType computeDistanceFromModelToPoint(const CCVector3 *_P, CCVector3 *nearestPoint=nullptr) const override
Compute the (unsigned) distance between a 3D point and this model.
Definition: LocalModel.cpp:128
unsigned char m_Z
Height function third dimension (0=X, 1=Y, 2=Z)
Definition: LocalModel.cpp:156
Type y
Definition: CVGeom.h:137
Type u[3]
Definition: CVGeom.h:139
Type x
Definition: CVGeom.h:137
Type z
Definition: CVGeom.h:137
static ScalarType computePoint2PlaneDistance(const CCVector3 *P, const PointCoordinateType *planeEquation)
Computes the (signed) distance between a point and a plane.
static ScalarType computePoint2TriangleDistance(const CCVector3 *P, const GenericTriangle *theTriangle, bool signedDist, CCVector3 *nearestP=nullptr)
Computes the distance between a point and a triangle.
A generic triangle interface.
Local modelization (generic interface)
Definition: LocalModel.h:17
LocalModel(const CCVector3 &center, PointCoordinateType squaredRadius)
Constructor.
Definition: LocalModel.cpp:161
static LocalModel * New(CV_LOCAL_MODEL_TYPES type, Neighbourhood &subset, const CCVector3 &center, PointCoordinateType squaredRadius)
Factory.
Definition: LocalModel.cpp:165
const PointCoordinateType * getQuadric(Tuple3ub *dims=nullptr)
Returns the best interpolating 2.5D quadric.
const PointCoordinateType * getLSPlane()
Returns best interpolating plane equation (Least-square)
static constexpr bool DUPLICATE_VERTICES
Definition: Neighbourhood.h:29
const CCVector3 * getGravityCenter()
Returns gravity center.
static constexpr int IGNORE_MAX_EDGE_LENGTH
Definition: Neighbourhood.h:27
GenericIndexedMesh * triangulateOnPlane(bool duplicateVertices, PointCoordinateType maxEdgeLength, std::string &outputErrorStr)
Applies 2D Delaunay triangulation.
__host__ __device__ int2 abs(int2 v)
Definition: cutil_math.h:1267
static double dist(double x1, double y1, double x2, double y2)
Definition: lsd.c:207
Generic file read and write utility for python interface.