Ponca  a8a21c362dc2c650634ff9a07307bf4e308b2e74
Point Cloud Analysis library
Loading...
Searching...
No Matches
cncFormulaEigen.h
1
62#pragma once
63
64#include "../Common/Assert.h"
65#include <Eigen/Dense>
66
67namespace Ponca::internal
68{
69
74 template <typename DataPoint>
75 struct CNCEigen
76 {
77 using MatrixType = typename DataPoint::MatrixType;
78 using Scalar = typename DataPoint::Scalar;
79 using VectorType = typename DataPoint::VectorType;
81 static constexpr Scalar epsilon = Eigen::NumTraits<Scalar>::epsilon();
82
85 {
87 static bool isDegenerate(const VectorType& a, const VectorType& b, const VectorType& c)
88 {
89 Scalar d[3] = {(a - b).norm(), (a - c).norm(), (b - c).norm()};
90 // Checks that the spherical triangle is small or thin.
91 if ((d[0] < epsilon) || (d[1] < epsilon) || (d[2] < epsilon))
92 return true;
93 // Checks that the spherical triangle is flat.
94 size_t m = 0;
95 if (d[1] > d[m])
96 m = 1;
97 if (d[2] > d[m])
98 m = 2;
99 return (fabs(d[m] - d[(m + 1) % 3] - d[(m + 2) % 3]) < epsilon);
100 }
101
103 static void polarTriangle(const VectorType& a, const VectorType& b, const VectorType& c, VectorType& Ap,
104 VectorType& Bp, VectorType& Cp)
105 {
106 Ap = b.cross(c);
107 Bp = c.cross(a);
108 Cp = a.cross(b);
109 // Reorient points.
110 if (Ap.dot(a) < 0.0)
111 Ap = -Ap;
112 if (Bp.dot(b) < 0.0)
113 Bp = -Bp;
114 if (Cp.dot(c) < 0.0)
115 Cp = -Cp;
116 }
117
122 static void interiorAngles(const VectorType& a, const VectorType& b, const VectorType& c, Scalar& alpha,
123 Scalar& beta, Scalar& gamma)
124 {
125 VectorType Ta, Tb, Tc;
126 polarTriangle(a, b, c, Ta, Tb, Tc);
127 Ta /= Ta.norm();
128 Tb /= Tb.norm();
129 Tc /= Tc.norm();
130 if (Ta == VectorType::Zero() || Tb == VectorType::Zero() || Tc == VectorType::Zero())
131 alpha = beta = gamma = Scalar(0.0);
132 else
133 {
134 Scalar ca = std::max(Scalar(-1.0), std::min(Scalar(1.0), Tb.dot(Tc)));
135 Scalar cb = std::max(Scalar(-1.0), std::min(Scalar(1.0), Tc.dot(Ta)));
136 Scalar cc = std::max(Scalar(-1.0), std::min(Scalar(1.0), Ta.dot(Tb)));
137 alpha = acos(ca);
138 beta = acos(cb);
139 gamma = acos(cc);
140 }
141 }
142
144 static Scalar area(const VectorType& a, const VectorType& b, const VectorType& c)
145 {
146 Scalar alpha, beta, gamma;
147 if (isDegenerate(a, b, c))
148 return Scalar(0.0);
150 return ((fabs(alpha) < epsilon) || (fabs(beta) < epsilon) || (fabs(gamma) < epsilon))
151 ? Scalar(0.0)
152 : Scalar(2.0 * M_PI) - alpha - beta - gamma;
153 }
154
156 static Scalar algebraicArea(const VectorType& a, const VectorType& b, const VectorType& c)
157 {
158 Scalar S = area(a, b, c);
159 VectorType M = a + b + c;
160 VectorType X = (b - a).cross(c - a);
161 if (M.template lpNorm<1>() <= epsilon || X.template lpNorm<1>() <= epsilon)
162 return 0.0;
163 return M.dot(X) < 0.0 ? -S : S;
164 }
165 };
166
167 // ---------------------- Main functions ----------------
168 public:
171
184 static Scalar mu0InterpolatedU(const VectorType& a, const VectorType& b, const VectorType& c,
185 const VectorType& ua, const VectorType& ub, const VectorType& uc,
186 bool unit_u = false)
187 {
188 // MU0=1/2*det( uM, B-A, C-A )
189 // = 1/2 < ( (u_A + u_B + u_C)/3.0 ) | (AB x AC ) >
190 VectorType uM = (ua + ub + uc) / 3.0;
191 if (unit_u)
192 {
193 auto uM_norm = uM.norm();
194 uM = uM_norm == Scalar(0.0) ? uM : uM / uM_norm;
195 }
196 return Scalar(0.5) * ((b - a).cross(c - a)).dot(uM);
197 }
198
211 static Scalar mu1InterpolatedU(const VectorType& a, const VectorType& b, const VectorType& c,
212 const VectorType& ua, const VectorType& ub, const VectorType& uc,
213 bool unit_u = false)
214 {
215 // MU1=1/2( | uM u_C-u_B A | + | uM u_A-u_C B | + | uM u_B-u_A C |
216 VectorType uM = (ua + ub + uc) / Scalar(3.0);
217 if (unit_u)
218 uM /= uM.norm();
219 return Scalar(0.25) * (uM.cross(uc - ub).dot(a) + uM.cross(ua - uc).dot(b) + uM.cross(ub - ua).dot(c));
220 }
221
234 static Scalar mu2InterpolatedU(const VectorType& a, const VectorType& b, const VectorType& c,
235 const VectorType& ua, const VectorType& ub, const VectorType& uc,
236 bool unit_u = false)
237 {
238 // Using non unitary interpolated normals give
239 // MU2=1/2*det( uA, uB, uC )
240 // When normals are unitary, it is the area of a spherical triangle.
241 if (unit_u)
243 else
244 return Scalar(0.5) * (ua.cross(ub).dot(uc));
245 }
246
256 static MatrixType muXYInterpolatedU(const VectorType& a, const VectorType& b, const VectorType& c,
257 const VectorType& ua, const VectorType& ub, const VectorType& uc,
258 bool unit_u = false)
259 {
260 MatrixType T = MatrixType::Zero();
261 VectorType uM = (ua + ub + uc) / 3.0;
262 if (unit_u)
263 uM /= uM.norm();
264 const VectorType uac = uc - ua;
265 const VectorType uab = ub - ua;
266 const VectorType ab = b - a;
267 const VectorType ac = c - a;
268 for (size_t i = 0; i < 3; ++i)
269 {
270 VectorType X = VectorType::Zero();
271 X(i) = Scalar(1.0);
272 for (size_t j = 0; j < 3; ++j)
273 {
274 // Since RealVector Y = RealVector::base( j, 1.0 );
275 // < Y | uac > = uac[ j ]
276 const Scalar tij = Scalar(0.5) * uM.dot(uac[j] * X.cross(ab) - uab[j] * X.cross(ac));
277 T(i, j) = tij;
278 }
279 }
280 return T;
281 }
282
284
285 // ---------------------- Helper functions ----------------
286 public:
289
295 static std::pair<VectorType, VectorType> curvDirFromTensor(const MatrixType& tensor, const Scalar area,
296 const VectorType& N)
297 {
298 auto Mt = tensor.transpose();
299 auto M = tensor;
300 M += Mt;
301 M *= 0.5;
302 const Scalar coef_N = Scalar(1000.0) * area;
303 // Adding 1000 area n x n to anisotropic measure
304 for (int j = 0; j < 3; j++)
305 for (int k = 0; k < 3; k++)
306 M(j, k) += coef_N * N[j] * N[k];
307
308 Eigen::SelfAdjointEigenSolver<MatrixType> eigensolver(M);
309 if (eigensolver.info() != Eigen::Success)
310 abort();
311
312 // SelfAdjointEigenSolver returns sorted eigenvalues, no
313 // need to reorder the eigenvectors.
314 PONCA_ASSERT(eigensolver.eigenvalues()(0) <= eigensolver.eigenvalues()(1));
315 PONCA_ASSERT(eigensolver.eigenvalues()(1) <= eigensolver.eigenvalues()(2));
316 VectorType v1 = eigensolver.eigenvectors().col(1);
317 VectorType v2 = eigensolver.eigenvectors().col(0);
318 return std::pair<VectorType, VectorType>(v1, v2);
319 }
320
326 static std::tuple<Scalar, Scalar, VectorType, VectorType> curvaturesFromTensor(const MatrixType& tensor,
327 const Scalar area,
328 const VectorType& N)
329 {
330 auto Mt = tensor.transpose();
331 auto M = tensor;
332 M += Mt;
333 M *= 0.5;
334 const Scalar coef_N = Scalar(1000.0) * area;
335 // Adding 1000 area n x n to anisotropic measure
336 for (int j = 0; j < 3; j++)
337 for (int k = 0; k < 3; k++)
338 M(j, k) += coef_N * N[j] * N[k];
339
340 Eigen::SelfAdjointEigenSolver<MatrixType> eigensolver(M);
341 if (eigensolver.info() == Eigen::Success)
342 {
343 // SelfAdjointEigenSolver returns sorted eigenvalues, no
344 // need to reorder the eigenvectors.
345 PONCA_ASSERT(eigensolver.eigenvalues()(0) <= eigensolver.eigenvalues()(1));
346 PONCA_ASSERT(eigensolver.eigenvalues()(1) <= eigensolver.eigenvalues()(2));
347 VectorType v1 = eigensolver.eigenvectors().col(0);
348 VectorType v2 = eigensolver.eigenvectors().col(1);
349 return std::make_tuple(-eigensolver.eigenvalues()(0), -eigensolver.eigenvalues()(1), v1, v2);
350 }
351 else
352 {
353 VectorType v1, v2;
354 return std::make_tuple(Scalar(0.0), Scalar(0.0), v1, v2);
355 }
356 }
357
359 }; // struct CNCEigen
360
361} // namespace Ponca::internal
Aggregator class used to declare specialized structures using CRTP.
Definition basket.h:257
Copyright (c) 2022 Jacques-Olivier Lachaud (jacques-olivier.lachaud@univ-savoie.fr) Laboratory of Mat...
Represents a triangle on a sphere of radius one.
static void polarTriangle(const VectorType &a, const VectorType &b, const VectorType &c, VectorType &Ap, VectorType &Bp, VectorType &Cp)
static Scalar area(const VectorType &a, const VectorType &b, const VectorType &c)
static Scalar algebraicArea(const VectorType &a, const VectorType &b, const VectorType &c)
static bool isDegenerate(const VectorType &a, const VectorType &b, const VectorType &c)
Spherical point data type.
static void interiorAngles(const VectorType &a, const VectorType &b, const VectorType &c, Scalar &alpha, Scalar &beta, Scalar &gamma)
Returns the interior angles of the spherical triangle ABC.
This class contains some stand-alone CorrectedNormalCurrent formulas for triangles,...
static Scalar mu2InterpolatedU(const VectorType &a, const VectorType &b, const VectorType &c, const VectorType &ua, const VectorType &ub, const VectorType &uc, bool unit_u=false)
Computes mu2 measure (Gaussian curvature) of triangle abc given an interpolated corrected normal vect...
static Scalar mu0InterpolatedU(const VectorType &a, const VectorType &b, const VectorType &c, const VectorType &ua, const VectorType &ub, const VectorType &uc, bool unit_u=false)
Computes mu0 measure (area) of triangle abc given an interpolated corrected normal vector ua,...
static MatrixType muXYInterpolatedU(const VectorType &a, const VectorType &b, const VectorType &c, const VectorType &ua, const VectorType &ub, const VectorType &uc, bool unit_u=false)
Computes muXY measure (anisotropic curvature) of triangle abc given an interpolated corrected normal ...
static std::tuple< Scalar, Scalar, VectorType, VectorType > curvaturesFromTensor(const MatrixType &tensor, const Scalar area, const VectorType &N)
Computing principal curvatures k1 and k2 from tensor.
static Scalar mu1InterpolatedU(const VectorType &a, const VectorType &b, const VectorType &c, const VectorType &ua, const VectorType &ub, const VectorType &uc, bool unit_u=false)
Computes mu1 measure (mean curvature) of triangle abc given an interpolated corrected normal vector u...
static constexpr Scalar epsilon
Small constant used to approximate zero.
static std::pair< VectorType, VectorType > curvDirFromTensor(const MatrixType &tensor, const Scalar area, const VectorType &N)
Computing principal curvatures k1 and k2 from tensor.