Ponca  cb266528645085a12930c827fbb8336cd937ecbb
Point Cloud Analysis library
Loading...
Searching...
No Matches
cnc.hpp
1
9
10#pragma once
11
12#include <random>
13#include <numeric>
14
15namespace Ponca::internal
16{
29 template <TriangleGenerationMethod Method, typename P>
31 {
32 using VectorType = typename P::VectorType;
33
34 template <typename IndexRange, typename IteratorBegin, typename IteratorEnd, typename NeighborFilter>
35 static FIT_RESULT generate(IndexRange&& /*ids*/, const IteratorBegin& /*begin*/, const IteratorEnd& /*end*/,
36 const NeighborFilter& /*w*/, std::vector<Triangle<P>>& /*triangles*/
37 )
38 {
39 throw std::invalid_argument("Triangle generation method not implemented!");
40 }
41
42 template <typename IndexRange, typename PointContainer, typename NeighborFilter>
43 static FIT_RESULT generate(IndexRange&& /*ids*/, const PointContainer& /*points*/, const NeighborFilter& /*w*/,
44 std::vector<Triangle<P>>& /*triangles*/
45 )
46 {
47 throw std::invalid_argument("Triangle generation method not implemented!");
48 }
49 };
50
55 template <typename P>
56 struct TriangleGenerator<UniformGeneration, P>
57 {
58 private:
59 static constexpr int maxTriangles{100};
60
61 public:
62 using VectorType = typename P::VectorType;
63 using Scalar = typename P::Scalar;
64
65 template <typename IndexRange, typename IteratorBegin, typename IteratorEnd, typename NeighborFilter>
66 static FIT_RESULT generate(IndexRange&& ids, const IteratorBegin& begin, const IteratorEnd& end,
67 const NeighborFilter& w, std::vector<Triangle<P>>& triangles)
68 {
69 // Makes a new array
70 std::vector<int> indices;
71 for (int index : ids)
72 {
73 // Skip the points that are outside the kernel radius
74 if (w(*std::next(begin, index)).first == Scalar(0.))
75 continue;
76 indices.push_back(index);
77 }
78 if (indices.empty())
79 return UNDEFINED;
80
81 const int lastIndex = int(indices.size()) - 1;
82 for (int i = 0; i < maxTriangles; ++i)
83 {
84 // Randomly select triangles
85 int i1 = indices[Eigen::internal::random<int>(0, lastIndex)];
86 int i2 = indices[Eigen::internal::random<int>(0, lastIndex)];
87 int i3 = indices[Eigen::internal::random<int>(0, lastIndex)];
88 if (i1 == i2 || i1 == i3 || i2 == i3)
89 continue;
90
91 triangles.push_back(
92 internal::Triangle<P>(*std::next(begin, i1), *std::next(begin, i2), *std::next(begin, i3)));
93 }
94 return STABLE;
95 }
96
97 template <typename IndexRange, typename PointContainer, typename NeighborFilter>
98 static FIT_RESULT generate(IndexRange&& ids, const PointContainer& points, const NeighborFilter& w,
99 std::vector<Triangle<P>>& triangles)
100 {
101 return generate(ids, std::begin(points), std::end(points), w, triangles);
102 }
103 };
104
109 template <typename P>
110 struct TriangleGenerator<IndependentGeneration, P>
111 {
112 private:
113 static constexpr int maxTriangles{100};
114
115 public:
116 using VectorType = typename P::VectorType;
117 using Scalar = typename P::Scalar;
118
119 template <typename IndexRange, typename IteratorBegin, typename IteratorEnd, typename NeighborFilter>
120 static FIT_RESULT generate(IndexRange&& ids, const IteratorBegin& begin, const IteratorEnd& end,
121 const NeighborFilter& w, std::vector<Triangle<P>>& triangles)
122 {
123 // Makes a new array to shuffle
124 std::vector<int> indices;
125 for (int index : ids)
126 {
127 // Skip the points that are outside the kernel radius
128 if (w(*std::next(begin, index)).first == Scalar(0.))
129 continue;
130 indices.push_back(index);
131 }
132 if (indices.empty())
133 return UNDEFINED;
134
135 // Shuffles the neighbors
136 std::random_device rd;
137 std::mt19937 rg(rd());
138 std::shuffle(indices.begin(), indices.end(), rg);
139
140 // Compute the triangles
141 triangles.clear();
142 const int max_triangles = std::min(maxTriangles, int(indices.size() / 3));
143 for (int nb_vt = 0; nb_vt < max_triangles - 2; nb_vt++)
144 {
145 int i1 = indices[nb_vt];
146 int i2 = indices[nb_vt + 1];
147 int i3 = indices[nb_vt + 2];
148 triangles.push_back(
149 internal::Triangle<P>(*std::next(begin, i1), *std::next(begin, i2), *std::next(begin, i3)));
150 }
151 return STABLE;
152 }
153
154 template <typename IndexRange, typename PointContainer, typename NeighborFilter>
155 static FIT_RESULT generate(IndexRange&& ids, const PointContainer& points, const NeighborFilter& w,
156 std::vector<Triangle<P>>& triangles)
157 {
158 return generate(ids, std::begin(points), std::end(points), w, triangles);
159 }
160 };
161
162 template <typename P>
164 {
165 using Scalar = typename P::Scalar;
166 static constexpr Scalar avg_normal_coef{Scalar(0.5)};
167 };
168
173 template <typename P>
174 struct TriangleGenerator<HexagramGeneration, P> : protected HexagramBase<P>
175 {
176 using VectorType = typename P::VectorType;
177 using Scalar = typename P::Scalar;
178
179 template <typename IndexRange, typename IteratorBegin, typename IteratorEnd, typename NeighborFilter>
180 static FIT_RESULT generate(IndexRange&& ids, const IteratorBegin& begin, const IteratorEnd& end,
181 const NeighborFilter& w, std::vector<Triangle<P>>& triangles)
182 {
183 PONCA_MULTIARCH_STD_MATH(abs);
184 // Compute normal and maximum distance.
185 VectorType c = w.center();
186 VectorType n = w.data();
187 VectorType a{VectorType::Zero()};
188 Scalar avg_d = Scalar(0);
189
190 bool isUndefined = true;
191 int valid_points_count = 0;
192 for (int index : ids)
193 {
194 auto p = *std::next(begin, index);
195
196 // Skip the points that are outside the kernel radius
197 if (w(p).first == Scalar(0.))
198 continue;
199
200 avg_d += (p.pos() - c).norm();
201 a += p.normal();
202 isUndefined = false;
203 valid_points_count++;
204 }
205 if (isUndefined)
206 return UNDEFINED;
207
208 a /= a.norm();
209 n = (Scalar(1) - HexagramBase<P>::avg_normal_coef) * n + HexagramBase<P>::avg_normal_coef * a;
210 n /= n.norm();
211 avg_d /= valid_points_count;
212
213 // Define basis for sector analysis.
214 const int m = abs(n[0]) > abs(n[1]) ? (abs(n[0]) > abs(n[2]) ? 0 : 2) : (abs(n[1]) > abs(n[2]) ? 1 : 2);
215
216 const VectorType e = (m == 0) ? VectorType(0, 1, 0) : (m == 1) ? VectorType(0, 0, 1) : VectorType(1, 0, 0);
217
218 VectorType u = n.cross(e);
219 VectorType v = n.cross(u);
220 u /= u.norm();
221 v /= v.norm();
222
223 std::array<VectorType, 6> positions;
224 std::array<VectorType, 6> normals;
225 std::array<Scalar, 6> distance2;
226 std::array<VectorType, 6> targets;
227
228 for (int i = 0; i < 6; i++)
229 {
230 distance2[i] = avg_d * avg_d;
231 targets[i] = avg_d * (u * cos(i * M_PI / 3.0) + v * sin(i * M_PI / 3.0));
232 positions[i] = w.center();
233 normals[i] = w.data();
234 }
235
236 // Compute closest points.
237 for (int index : ids)
238 {
239 auto pt = *std::next(begin, index);
240
241 // Skip the points that are outside the kernel radius
242 if (w(pt).first == Scalar(0.))
243 continue;
244
245 VectorType p = pt.pos();
246 if (p == c)
247 continue; // Skip the eval point
248 const VectorType d = p - c;
249
250 for (int j = 0; j < 6; j++)
251 {
252 const Scalar d2 = (d - targets[j]).squaredNorm();
253 if (d2 < distance2[j])
254 {
255 distance2[j] = d2;
256 positions[j] = p;
257 normals[j] = pt.normal();
258 }
259 }
260 }
261 triangles.push_back(internal::Triangle<P>({positions[0], positions[2], positions[4]},
262 {normals[0], normals[2], normals[4]}));
263 triangles.push_back(internal::Triangle<P>({positions[1], positions[3], positions[5]},
264 {normals[1], normals[3], normals[5]}));
265 return STABLE;
266 }
267
268 template <typename IndexRange, typename PointContainer, typename NeighborFilter>
269 static FIT_RESULT generate(IndexRange&& ids, const PointContainer& points, const NeighborFilter& w,
270 std::vector<Triangle<P>>& triangles)
271 {
272 return generate(ids, std::begin(points), std::end(points), w, triangles);
273 }
274 };
275
280 template <typename P>
281 struct TriangleGenerator<AvgHexagramGeneration, P> : protected HexagramBase<P>
282 {
283 using VectorType = typename P::VectorType;
284 using Scalar = typename P::Scalar;
285 template <typename IndexRange, typename PointIterator, typename NeighborFilter>
286 static FIT_RESULT generate(IndexRange&& ids, const PointIterator& begin, const PointIterator& end,
287 const NeighborFilter& w, std::vector<Triangle<P>>& triangles)
288 {
289 // Compute normal and maximum distance.
290 VectorType c = w.center();
291 VectorType n = w.data();
292 VectorType a = VectorType::Zero();
293 Scalar avg_d = Scalar(0);
294
295 std::array<VectorType, 6> targets;
296 Scalar avg_normal = Scalar(0.5);
297
298 bool isUndefined = true;
299 int valid_points_count = 0;
300 for (int index : ids)
301 {
302 auto p = *std::next(begin, index);
303 if (w(p).first == Scalar(0.))
304 continue; // Skip the points that are outside the kernel radius
305 a += p.normal();
306 avg_d += (p.pos() - c).norm();
307 isUndefined = false;
308 valid_points_count++;
309 }
310 if (isUndefined)
311 return UNDEFINED;
312
313 a /= a.norm();
314 n = (Scalar(1) - HexagramBase<P>::avg_normal_coef) * n + HexagramBase<P>::avg_normal_coef * a;
315 n /= n.norm();
316 avg_d /= valid_points_count;
317
318 // Define basis for sector analysis.
319 const int m = (std::abs(n[0]) > std::abs(n[1])) ? ((std::abs(n[0])) > std::abs(n[2]) ? 0 : 2)
320 : ((std::abs(n[1])) > std::abs(n[2]) ? 1 : 2);
321
322 const VectorType e = (m == 0) ? VectorType(0, 1, 0) : (m == 1) ? VectorType(0, 0, 1) : VectorType(1, 0, 0);
323 VectorType u = n.cross(e);
324 VectorType v = n.cross(u);
325 u /= u.norm();
326 v /= v.norm();
327
328 // Initialize the average values
329 std::array<VectorType, 6> array_avg_normals;
330 std::array<VectorType, 6> array_avg_pos;
331 std::array<int, 6> array_nb{};
332 for (int i = 0; i < 6; i++)
333 {
334 targets[i] = avg_d * (u * cos(i * M_PI / 3.0) + v * sin(i * M_PI / 3.0));
335 array_avg_normals[i] = VectorType::Zero();
336 array_avg_pos[i] = VectorType::Zero();
337 }
338
339 // Compute closest points.
340 for (int index : ids)
341 {
342 auto pt = *std::next(begin, index);
343 if (w(pt).first == Scalar(0.))
344 continue; // Skip the points that are outside the kernel radius
345 VectorType p = pt.pos() - c;
346 int best_k = 0;
347 Scalar best_d2 = (p - targets[0]).squaredNorm();
348 for (int k = 1; k < 6; k++)
349 {
350 const Scalar d2 = (p - targets[k]).squaredNorm();
351 if (d2 < best_d2)
352 {
353 best_k = k;
354 best_d2 = d2;
355 }
356 }
357 array_avg_normals[best_k] += pt.normal();
358 array_avg_pos[best_k] += pt.pos();
359 array_nb[best_k] += 1;
360 }
361
362 for (int i = 0; i < 6; i++)
363 {
364 if (array_nb[i] == 0)
365 {
366 array_avg_normals[i] = w.data();
367 array_avg_pos[i] = w.center();
368 }
369 else
370 {
371 array_avg_normals[i] /= array_avg_normals[i].norm();
372 array_avg_pos[i] /= Scalar(array_nb[i]);
373 }
374 }
375
376 triangles.push_back(
377 internal::Triangle<P>({array_avg_pos[0], array_avg_pos[2], array_avg_pos[4]},
378 {array_avg_normals[0], array_avg_normals[2], array_avg_normals[4]}));
379 triangles.push_back(
380 internal::Triangle<P>({array_avg_pos[1], array_avg_pos[3], array_avg_pos[5]},
381 {array_avg_normals[1], array_avg_normals[3], array_avg_normals[5]}));
382 return STABLE;
383 }
384
385 template <typename IndexRange, typename PointContainer, typename NeighborFilter>
386 static FIT_RESULT generate(IndexRange&& ids, const PointContainer& points, const NeighborFilter& w,
387 std::vector<Triangle<P>>& triangles)
388 {
389 return generate(ids, std::begin(points), std::end(points), w, triangles);
390 }
391 };
392} // namespace Ponca::internal
393
394namespace Ponca
395{
396 template <class P, TriangleGenerationMethod M>
397 requires CNC_REQUIREMENTS
398 template <typename IteratorBegin, typename IteratorEnd>
399 FIT_RESULT CNC<P, M>::compute(const IteratorBegin& begin, const IteratorEnd& end)
400 {
401 init();
402 std::vector<unsigned int> indicesSample(std::distance(begin, end));
403 std::iota(indicesSample.begin(), indicesSample.end(), 0);
404
406 internal::TriangleGenerator<M, P>::generate(indicesSample, begin, end, m_nFilter, m_triangles);
407 if (m_eCurrentState != STABLE)
408 return m_eCurrentState;
409 m_nb_vt = int(m_triangles.size());
410 return finalize();
411 }
412
413 template <class P, TriangleGenerationMethod M>
414 requires CNC_REQUIREMENTS
415 template <typename PointContainer>
416 FIT_RESULT CNC<P, M>::compute(const PointContainer& points)
417 {
418 init();
419 std::vector<unsigned int> indicesSample(points.size());
420 std::iota(indicesSample.begin(), indicesSample.end(), 0);
421
422 m_eCurrentState = internal::TriangleGenerator<M, P>::generate(indicesSample, points, m_nFilter, m_triangles);
423 if (m_eCurrentState != STABLE)
424 return m_eCurrentState;
425 m_nb_vt = int(m_triangles.size());
426 return finalize();
427 }
428
429 template <class P, TriangleGenerationMethod M>
430 requires CNC_REQUIREMENTS
431 template <typename IndexRange, typename PointContainer>
432 FIT_RESULT CNC<P, M>::computeWithIds(IndexRange&& ids, const PointContainer& points)
433 {
434 init();
435 m_eCurrentState = internal::TriangleGenerator<M, P>::generate(ids, points, m_nFilter, m_triangles);
436 if (m_eCurrentState != STABLE)
437 return m_eCurrentState;
438 m_nb_vt = int(m_triangles.size());
439 return finalize();
440 }
441
442 template <class P, TriangleGenerationMethod M>
443 requires CNC_REQUIREMENTS
444 FIT_RESULT CNC<P, M>::finalize()
445 {
446 m_A = Scalar(0);
447 m_H = Scalar(0);
448 m_G = Scalar(0);
449
450 MatrixType localT = MatrixType::Zero();
451
452 for (int t = 0; t < m_nb_vt; ++t)
453 {
454 // Simple estimation.
455 Scalar tA = m_triangles[t].mu0InterpolatedU();
457 {
458 m_A -= tA;
459 m_H += m_triangles[t].template mu1InterpolatedU<true>();
460 m_G += m_triangles[t].template mu2InterpolatedU<true>();
461 localT += m_triangles[t].template muXYInterpolatedU<true>();
462 }
463 else if (tA > internal::CNCEigen<P>::epsilon)
464 {
465 m_A += tA;
466 m_H += m_triangles[t].mu1InterpolatedU();
467 m_G += m_triangles[t].mu2InterpolatedU();
468 localT += m_triangles[t].muXYInterpolatedU();
469 }
470 } // end for t
471
472 m_T11 = localT(0, 0);
473 m_T12 = Scalar(0.5) * (localT(0, 1) + localT(1, 0));
474 m_T13 = Scalar(0.5) * (localT(0, 2) + localT(2, 0));
475 m_T22 = localT(1, 1);
476 m_T23 = Scalar(0.5) * (localT(1, 2) + localT(2, 1));
477 m_T33 = localT(2, 2);
478
479 if (m_A == Scalar(0))
480 {
481 m_H = m_G = m_k1 = m_k2 = Scalar(0);
482 m_v1 = m_v2 = VectorType::Zero();
483 return m_eCurrentState = UNDEFINED;
484 }
485
486 MatrixType T;
487 T << m_T11, m_T12, m_T13, m_T12, m_T22, m_T23, m_T13, m_T23, m_T33;
488 T /= m_A;
489 m_H /= m_A;
490 m_G /= m_A;
491
492 std::tie(m_k2, m_k1, m_v2, m_v1) = internal::CNCEigen<P>::curvaturesFromTensor(T, 1.0, m_nFilter.data());
493
494 return m_eCurrentState = STABLE;
495 }
496} // namespace Ponca
void init()
Set the scalar field values to 0 and reset the isNormalized() status.
Definition cnc.h:161
FIT_RESULT m_eCurrentState
Represent the current state of the fit (finalize function update the state).
Definition cnc.h:152
FIT_RESULT computeWithIds(IndexRange &&ids, const PointContainer &points)
Compute function that iterates over a subset of sampled points from an STL-Like container.
Definition cnc.hpp:432
FIT_RESULT compute(const IteratorBegin &begin, const IteratorEnd &end)
Convenience function for STL-like iterators Add neighbors stored in a container using STL-like iterat...
Definition cnc.hpp:399
Copyright (c) 2022 Jacques-Olivier Lachaud (jacques-olivier.lachaud@univ-savoie.fr) Laboratory of Mat...
This Source Code Form is subject to the terms of the Mozilla Public License, v.
Definition concepts.h:11
FIT_RESULT
Enum corresponding to the state of a fitting method (and what the finalize function returns).
Definition enums.h:15
@ UNDEFINED
The fitting is undefined, you can't use it for valid results.
Definition enums.h:22
@ STABLE
The fitting is stable and ready to use.
Definition enums.h:17
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 constexpr Scalar epsilon
Small constant used to approximate zero.
Stores the three points and normals of the triangles and provides access to Corrected Normal Current ...
Definition cnc.h:35