59 static constexpr int maxTriangles{100};
62 using VectorType =
typename P::VectorType;
63 using Scalar =
typename P::Scalar;
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)
70 std::vector<int> indices;
74 if (w(*std::next(begin, index)).first == Scalar(0.))
76 indices.push_back(index);
81 const int lastIndex = int(indices.size()) - 1;
82 for (
int i = 0; i < maxTriangles; ++i)
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)
97 template <
typename IndexRange,
typename Po
intContainer,
typename NeighborFilter>
98 static FIT_RESULT generate(IndexRange&& ids,
const PointContainer& points,
const NeighborFilter& w,
101 return generate(ids, std::begin(points), std::end(points), w, triangles);
113 static constexpr int maxTriangles{100};
116 using VectorType =
typename P::VectorType;
117 using Scalar =
typename P::Scalar;
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)
124 std::vector<int> indices;
125 for (
int index : ids)
128 if (w(*std::next(begin, index)).first == Scalar(0.))
130 indices.push_back(index);
136 std::random_device rd;
137 std::mt19937 rg(rd());
138 std::shuffle(indices.begin(), indices.end(), rg);
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++)
145 int i1 = indices[nb_vt];
146 int i2 = indices[nb_vt + 1];
147 int i3 = indices[nb_vt + 2];
154 template <
typename IndexRange,
typename Po
intContainer,
typename NeighborFilter>
155 static FIT_RESULT generate(IndexRange&& ids,
const PointContainer& points,
const NeighborFilter& w,
158 return generate(ids, std::begin(points), std::end(points), w, triangles);
176 using VectorType =
typename P::VectorType;
177 using Scalar =
typename P::Scalar;
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)
183 PONCA_MULTIARCH_STD_MATH(abs);
185 VectorType c = w.center();
186 VectorType n = w.data();
187 VectorType a{VectorType::Zero()};
188 Scalar avg_d = Scalar(0);
190 bool isUndefined =
true;
191 int valid_points_count = 0;
192 for (
int index : ids)
194 auto p = *std::next(begin, index);
197 if (w(p).first == Scalar(0.))
200 avg_d += (p.pos() - c).norm();
203 valid_points_count++;
209 n = (Scalar(1) - HexagramBase<P>::avg_normal_coef) * n + HexagramBase<P>::avg_normal_coef * a;
211 avg_d /= valid_points_count;
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);
216 const VectorType e = (m == 0) ? VectorType(0, 1, 0) : (m == 1) ? VectorType(0, 0, 1) : VectorType(1, 0, 0);
218 VectorType u = n.cross(e);
219 VectorType v = n.cross(u);
223 std::array<VectorType, 6> positions;
224 std::array<VectorType, 6> normals;
225 std::array<Scalar, 6> distance2;
226 std::array<VectorType, 6> targets;
228 for (
int i = 0; i < 6; i++)
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();
237 for (
int index : ids)
239 auto pt = *std::next(begin, index);
242 if (w(pt).first == Scalar(0.))
245 VectorType p = pt.pos();
248 const VectorType d = p - c;
250 for (
int j = 0; j < 6; j++)
252 const Scalar d2 = (d - targets[j]).squaredNorm();
253 if (d2 < distance2[j])
257 normals[j] = pt.normal();
262 {normals[0], normals[2], normals[4]}));
264 {normals[1], normals[3], normals[5]}));
268 template <
typename IndexRange,
typename Po
intContainer,
typename NeighborFilter>
269 static FIT_RESULT generate(IndexRange&& ids,
const PointContainer& points,
const NeighborFilter& w,
272 return generate(ids, std::begin(points), std::end(points), w, triangles);
283 using VectorType =
typename P::VectorType;
284 using Scalar =
typename P::Scalar;
285 template <
typename IndexRange,
typename Po
intIterator,
typename NeighborFilter>
286 static FIT_RESULT generate(IndexRange&& ids,
const PointIterator& begin,
const PointIterator& end,
287 const NeighborFilter& w, std::vector<
Triangle<P>>& triangles)
290 VectorType c = w.center();
291 VectorType n = w.data();
292 VectorType a = VectorType::Zero();
293 Scalar avg_d = Scalar(0);
295 std::array<VectorType, 6> targets;
296 Scalar avg_normal = Scalar(0.5);
298 bool isUndefined =
true;
299 int valid_points_count = 0;
300 for (
int index : ids)
302 auto p = *std::next(begin, index);
303 if (w(p).first == Scalar(0.))
306 avg_d += (p.pos() - c).norm();
308 valid_points_count++;
314 n = (Scalar(1) - HexagramBase<P>::avg_normal_coef) * n + HexagramBase<P>::avg_normal_coef * a;
316 avg_d /= valid_points_count;
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);
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);
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++)
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();
340 for (
int index : ids)
342 auto pt = *std::next(begin, index);
343 if (w(pt).first == Scalar(0.))
345 VectorType p = pt.pos() - c;
347 Scalar best_d2 = (p - targets[0]).squaredNorm();
348 for (
int k = 1; k < 6; k++)
350 const Scalar d2 = (p - targets[k]).squaredNorm();
357 array_avg_normals[best_k] += pt.normal();
358 array_avg_pos[best_k] += pt.pos();
359 array_nb[best_k] += 1;
362 for (
int i = 0; i < 6; i++)
364 if (array_nb[i] == 0)
366 array_avg_normals[i] = w.data();
367 array_avg_pos[i] = w.center();
371 array_avg_normals[i] /= array_avg_normals[i].norm();
372 array_avg_pos[i] /= Scalar(array_nb[i]);
378 {array_avg_normals[0], array_avg_normals[2], array_avg_normals[4]}));
381 {array_avg_normals[1], array_avg_normals[3], array_avg_normals[5]}));
385 template <
typename IndexRange,
typename Po
intContainer,
typename NeighborFilter>
386 static FIT_RESULT generate(IndexRange&& ids,
const PointContainer& points,
const NeighborFilter& w,
389 return generate(ids, std::begin(points), std::end(points), w, triangles);
487 T << m_T11, m_T12, m_T13, m_T12, m_T22, m_T23, m_T13, m_T23, m_T33;