21 std::map<MElement *, MElement *> newP;
22 std::map<MElement *, MElement *> newD;
26 for(std::size_t i = 0; i < (*fit)->getNumMeshElements(); i++) {
27 MElement *e = (*fit)->getMeshElement(i);
35 for(std::size_t i = 0; i < (*rit)->getNumMeshElements(); i++) {
36 MElement *e = (*rit)->getMeshElement(i);
50 _dim = elements[0]->getDim();
51 std::map<MElement *, MElement *> newP;
52 std::map<MElement *, MElement *> newD;
54 for(std::size_t i = 0; i < elements.size(); i++) {
65 const std::vector<double> ¶meters)
82 Msg::Error(
"Can't intersect metrics, no metric registered");
86 auto it =
_adj.begin();
87 for(; it !=
_adj.end(); it++) {
108 std::stringstream sg, sm, sl, sh, shm;
109 sg <<
"meshmetric_gradients_" << fileendname;
110 sm <<
"meshmetric_metric_" << fileendname;
111 sl <<
"meshmetric_levelset_" << fileendname;
112 sh <<
"meshmetric_hessian_" << fileendname;
113 shm <<
"meshmetric_hessianmat_" << fileendname;
114 std::ofstream out_grad(sg.str().c_str());
115 std::ofstream out_metric(sm.str().c_str());
116 std::ofstream out_ls(sl.str().c_str());
117 std::ofstream out_hess(sh.str().c_str());
118 std::ofstream out_hessmat(shm.str().c_str());
119 out_grad <<
"View \"ls_gradient\"{" << std::endl;
120 out_metric <<
"View \"metric\"{" << std::endl;
121 out_ls <<
"View \"ls\"{" << std::endl;
122 out_hess <<
"View \"hessian\"{" << std::endl;
123 out_hessmat <<
"View \"hessian_mat\"{" << std::endl;
126 for(; itelem != itelemen; itelem++) {
133 out_hessmat <<
"TT(";
140 out_hessmat <<
"TS(";
144 out_metric << ver->
x() <<
"," << ver->
y() <<
"," << ver->
z();
145 out_grad << ver->
x() <<
"," << ver->
y() <<
"," << ver->
z();
146 out_ls << ver->
x() <<
"," << ver->
y() <<
"," << ver->
z();
147 out_hess << ver->
x() <<
"," << ver->
y() <<
"," << ver->
z();
148 out_hessmat << ver->
x() <<
"," << ver->
y() <<
"," << ver->
z();
170 out_ls <<
"};" << std::endl;
171 out_hess <<
"};" << std::endl;
177 for(
int k = 0; k < 3; k++) {
178 out_grad <<
grads[ver](k);
180 out_grad <<
"};" << std::endl;
183 for(
int l = 0; l < 3; l++) {
187 out_metric <<
"};" << std::endl;
188 out_hessmat <<
"};" << std::endl;
198 out_grad <<
"};" << std::endl;
199 out_metric <<
"};" << std::endl;
200 out_ls <<
"};" << std::endl;
201 out_hess <<
"};" << std::endl;
202 out_hessmat <<
"};" << std::endl;
218 auto it =
_adj.begin();
219 while(it !=
_adj.end()) {
220 std::vector<MElement *> lt = it->second;
222 vals[ver] = (*_fct)(ver->
x(), ver->
y(), ver->
z());
228 std::vector<MVertex *>
getLSBlob(std::size_t minNbPt, v2t_cont::iterator it,
240 std::vector<MVertex *> vv(1, it->first),
243 std::set<MVertex *> nbvv;
244 for(
auto itBV = bvv.begin(); itBV != bvv.end();
246 std::vector<MElement *> &adjBV = adj[*itBV];
247 for(
auto itBVEl = adjBV.begin(); itBVEl != adjBV.end(); itBVEl++) {
248 for(std::size_t iV = 0; iV < (*itBVEl)->getNumVertices();
250 MVertex *v = (*itBVEl)->getVertex(iV);
253 if(find(vv.begin(), vv.end(), v) == vv.end())
262 bvv.assign(nbvv.begin(), nbvv.end());
263 vv.insert(vv.end(), nbvv.begin(), nbvv.end());
266 }
while(vv.size() < minNbPt);
277 std::size_t sysDim = (
_dim == 2) ? 6 : 10;
278 std::size_t minNbPtBlob = 3 * sysDim;
280 for(
auto it =
_adj.begin(); it !=
_adj.end(); it++) {
282 std::vector<MVertex *> vv =
getLSBlob(minNbPtBlob, it,
_adj);
285 for(std::size_t i = 0; i < vv.size(); i++) {
286 const double &x = vv[i]->x(), &y = vv[i]->y(), &
z = vv[i]->z();
309 ATA.
gemm(A, A, 1., 0.,
true,
false);
310 A.multWithATranspose(b, 1., 0., ATb);
312 const double &x = ver->
x(), &y = ver->
y(), &
z = ver->
z();
313 double d2udx2, d2udy2, d2udz2, d2udxy, d2udxz, d2udyz, dudx, dudy, dudz;
315 d2udx2 = 2. * coeffs(0);
316 d2udy2 = 2. * coeffs(2);
321 dudx = d2udx2 * x + d2udxy * y + coeffs(3);
322 dudy = d2udxy * x + d2udy2 * y + coeffs(4);
326 d2udx2 = 2. * coeffs(0);
327 d2udy2 = 2. * coeffs(3);
328 d2udz2 = 2. * coeffs(5);
332 dudx = d2udx2 * x + d2udxy * y + d2udxz *
z + coeffs(6);
333 dudy = d2udxy * x + d2udy2 * y + d2udyz *
z + coeffs(7);
334 dudz = d2udxz * x + d2udyz * y + d2udz2 *
z + coeffs(8);
336 double duNorm = sqrt(dudx * dudx + dudy * dudy + dudz * dudz);
341 grads[ver] =
SVector3(dudx / duNorm, dudy / duNorm, dudz / duNorm);
355 SMetric3 &metric,
double &size,
double x,
361 signed_dist =
vals[ver];
366 signed_dist = (*_fct)(x, y,
z);
368 _fct->
hessian(x, y,
z, hessian(0, 0), hessian(0, 1), hessian(0, 2),
369 hessian(1, 0), hessian(1, 1), hessian(1, 2), hessian(2, 0),
370 hessian(2, 1), hessian(2, 2));
373 double dist = fabs(signed_dist);
376 double norm = gr(0) * gr(0) + gr(1) * gr(1) + gr(2) * gr(2);
377 if(dist <
_e &&
norm != 0.0) {
379 double C = 1. / (h * h) - 1. / (
hmax *
hmax);
380 H(0, 0) += C * gr(0) * gr(0) /
norm;
381 H(1, 1) += C * gr(1) * gr(1) /
norm;
382 H(2, 2) += C * gr(2) * gr(2) /
norm;
383 H(1, 0) = H(0, 1) = C * gr(1) * gr(0) /
norm;
384 H(2, 0) = H(0, 2) = C * gr(2) * gr(0) /
norm;
385 H(2, 1) = H(1, 2) = C * gr(2) * gr(1) /
norm;
392 double lambda1, lambda2, lambda3;
395 lambda3 = (
_dim == 3) ?
S(2) : 1.;
397 SVector3 t1(V(0, 0), V(1, 0), V(2, 0));
398 SVector3 t2(V(0, 1), V(1, 1), V(2, 1));
399 SVector3 t3(V(0, 2), V(1, 2), V(2, 2));
402 std::min(std::min(1 / sqrt(lambda1), 1 / sqrt(lambda2)), 1 / sqrt(lambda3));
403 metric =
SMetric3(lambda1, lambda2, lambda3, t1, t2, t3);
407 SMetric3 &metric,
double &size,
double x,
415 else if(ver ==
nullptr) {
417 _fct->
hessian(x, y,
z, hessian(0, 0), hessian(0, 1), hessian(0, 2),
418 hessian(1, 0), hessian(1, 1), hessian(1, 2), hessian(2, 0),
419 hessian(2, 1), hessian(2, 2));
422 double _epsilonP = 1.;
423 double hminP = 1.e-12;
424 double hmaxP = 1.e+12;
431 std::min(std::max(fabs(
S(0)) / _epsilonP, 1. / (hmaxP * hmaxP)),
432 1. / (hminP * hminP));
434 std::min(std::max(fabs(
S(1)) / _epsilonP, 1. / (hmaxP * hmaxP)),
435 1. / (hminP * hminP));
438 std::min(std::max(fabs(
S(2)) / _epsilonP, 1. / (hmaxP * hmaxP)),
439 1. / (hminP * hminP)) :
442 SVector3 t1(V(0, 0), V(1, 0), V(2, 0));
443 SVector3 t2(V(0, 1), V(1, 1), V(2, 1));
448 std::min(std::min(1 / sqrt(lambda1), 1 / sqrt(lambda2)), 1 / sqrt(lambda3));
449 metric =
SMetric3(lambda1, lambda2, lambda3, t1, t2, t3);
453 SMetric3 &metric,
double &size,
double x,
459 signed_dist =
vals[ver];
464 signed_dist = (*_fct)(x, y,
z);
466 _fct->
hessian(x, y,
z, hessian(0, 0), hessian(0, 1), hessian(0, 2),
467 hessian(1, 0), hessian(1, 1), hessian(1, 2), hessian(2, 0),
468 hessian(2, 1), hessian(2, 2));
471 double dist = fabs(signed_dist);
474 double norm = gr(0) * gr(0) + gr(1) * gr(1) + gr(2) * gr(2);
475 if(dist <
_e &&
norm != 0.0) {
477 double C = 1. / (h * h) - 1. / (
hmax *
hmax);
478 double kappa = hessian(0, 0) + hessian(1, 1) + hessian(2, 2);
479 double epsGeom = 4.0 * 3.14 * 3.14 / (kappa *
_np *
_np);
480 H(0, 0) += C * gr(0) * gr(0) / (
norm) + hessian(0, 0) / epsGeom;
481 H(1, 1) += C * gr(1) * gr(1) / (
norm) + hessian(1, 1) / epsGeom;
482 H(2, 2) += C * gr(2) * gr(2) / (
norm) + hessian(2, 2) / epsGeom;
483 H(1, 0) = H(0, 1) = C * gr(1) * gr(0) / (
norm) + hessian(1, 0) / epsGeom;
484 H(2, 0) = H(0, 2) = C * gr(2) * gr(0) / (
norm) + hessian(2, 0) / epsGeom;
485 H(2, 1) = H(1, 2) = C * gr(2) * gr(1) / (
norm) + hessian(2, 1) / epsGeom;
492 double lambda1, lambda2, lambda3;
495 lambda3 = (
_dim == 3) ?
S(2) : 1.;
502 lambda3 = (
_dim == 3) ?
508 SVector3 t1(V(0, 0), V(1, 0), V(2, 0));
509 SVector3 t2(V(0, 1), V(1, 1), V(2, 1));
510 SVector3 t3(V(0, 2), V(1, 2), V(2, 2));
513 std::min(std::min(1 / sqrt(lambda1), 1 / sqrt(lambda2)), 1 / sqrt(lambda3));
514 metric =
SMetric3(lambda1, lambda2, lambda3, t1, t2, t3);
518 SMetric3 &metric,
double &size,
double x,
524 signed_dist =
vals[ver];
529 signed_dist = (*_fct)(x, y,
z);
531 _fct->
hessian(x, y,
z, hessian(0, 0), hessian(0, 1), hessian(0, 2),
532 hessian(1, 0), hessian(1, 1), hessian(1, 2), hessian(2, 0),
533 hessian(2, 1), hessian(2, 2));
536 double dist = fabs(signed_dist);
538 const double metric_value_hmax = 1. / (
hmax *
hmax);
539 const double gMag = gVec.
norm(), invGMag = 1. / gMag;
541 if(signed_dist < _e && signed_dist >
_e_moins && gMag != 0.0) {
542 const double metric_value_hmin = 1. / (
hmin *
hmin);
543 const SVector3 nVec = invGMag * gVec;
551 double h_dista = std::min((
hmin + (dist * log(beta))),
hmax);
552 lambda_n = 1. / (h_dista * h_dista);
555 const double maximum_distance =
561 ((metric_value_hmax - metric_value_hmin) / maximum_distance) * dist;
563 std::vector<SVector3> tVec;
564 std::vector<double> kappa;
570 fabs(-gVec(1) * (-gVec(1) * hessian(0, 0) + gVec(0) * hessian(0, 1)) +
571 gVec(0) * (-gVec(1) * hessian(1, 0) + gVec(0) * hessian(1, 1))) *
575 tVec[0] =
SVector3(-nVec(1), nVec(0), 0.);
582 ImGG(0, 0) = 1. - gVec(0) * gVec(0);
583 ImGG(0, 1) = -gVec(0) * gVec(1);
584 ImGG(0, 2) = -gVec(0) * gVec(2);
585 ImGG(1, 0) = -gVec(1) * gVec(0);
586 ImGG(1, 1) = 1. - gVec(1) * gVec(1);
587 ImGG(1, 2) = -gVec(1) * gVec(2);
588 ImGG(2, 0) = -gVec(2) * gVec(0);
589 ImGG(2, 1) = -gVec(2) * gVec(1);
590 ImGG(2, 2) = 1. - gVec(2) * gVec(2);
594 gN.
gemm(ImGG, hess, 1., 0.);
598 gN.
eig(eigValRe, eigValIm, eigVecL, eigVecR,
602 kappa[0] = fabs(eigValRe(0));
603 kappa[1] = fabs(eigValRe(1));
604 kappa[2] = fabs(eigValRe(2));
607 tVec[0] =
SVector3(eigVecR(0, 0), eigVecR(1, 0), eigVecR(2, 0));
609 tVec[1] =
SVector3(eigVecR(0, 1), eigVecR(1, 1), eigVecR(2, 1));
611 tVec[2] =
SVector3(eigVecR(0, 2), eigVecR(1, 2), eigVecR(2, 2));
613 std::vector<double> tVecDotNVec(3);
615 tVecDotNVec[0] = fabs(
dot(tVec[0], nVec));
616 tVecDotNVec[1] = fabs(
dot(tVec[1], nVec));
617 tVecDotNVec[2] = fabs(
dot(tVec[2], nVec));
618 const int i_N = max_element(tVecDotNVec.begin(), tVecDotNVec.end()) -
622 kappa.erase(kappa.begin() + i_N);
623 tVec.erase(tVec.begin() + i_N);
625 const double invh_t0 = (
_np * kappa[0]) / 6.283185307,
626 invh_t1 = (
_np * kappa[1]) /
628 const double lambda_t0 = invh_t0 * invh_t0, lambda_t1 = invh_t1 * invh_t1;
629 const double lambdaP_n =
630 std::min(std::max(lambda_n, metric_value_hmax),
632 const double lambdaP_t0 =
633 std::min(std::max(lambda_t0, metric_value_hmax), metric_value_hmin);
634 const double lambdaP_t1 =
637 std::min(std::max(lambda_t1, metric_value_hmax), metric_value_hmin);
639 SMetric3(lambdaP_n, lambdaP_t0, lambdaP_t1, nVec, tVec[0], tVec[1]);
640 const double h_n = 1. / sqrt(lambdaP_n), h_t0 = 1. / sqrt(lambdaP_t0),
641 h_t1 = 1. / sqrt(lambdaP_t1);
642 size = std::min(std::min(h_n, h_t0), h_t1);
645 SMetric3 mymetric(metric_value_hmax);
653 double x,
double y,
double z)
658 signed_dist =
vals[ver];
663 signed_dist = (*_fct)(x, y,
z);
665 _fct->
hessian(x, y,
z, hessian(0, 0), hessian(0, 1), hessian(0, 2),
666 hessian(1, 0), hessian(1, 1), hessian(1, 2), hessian(2, 0),
667 hessian(2, 1), hessian(2, 2));
670 double dist = fabs(signed_dist);
675 if((signed_dist >= 0) && (signed_dist <
_e))
677 else if((signed_dist < 0) && (signed_dist >
_e_moins))
681 double lambda = 1. / size / size;
682 metric(0, 0) = lambda;
686 metric(1, 1) = lambda;
690 metric(2, 2) = (
_dim == 3) ? lambda : 1.;
710 for(std::size_t i = 0; i <
_elements.size(); i++) {
725 double scale = pow((
double)nbElementsTarget / N, 2.0 /
_dim);
726 for(
auto it = nmt.begin(); it != nmt.end(); ++it) {
729 it->second(0, 0) *=
scale;
730 it->second(1, 0) *=
scale;
731 it->second(1, 1) *=
scale;
741 SVector3 t1(V(0, 0), V(1, 0), V(2, 0));
742 SVector3 t2(V(0, 1), V(1, 1), V(2, 1));
743 SVector3 t3(V(0, 2), V(1, 2), V(2, 2));
757 _e_moins = (parameters.size() >= 5) ? parameters[4] : -parameters[0];
761 _np = (parameters.size() >= 4) ? parameters[3] : 15.;
766 for(
auto it =
_adj.begin(); it !=
_adj.end(); it++) {
785 setOfSizes[metricNumber].insert(std::make_pair(ver, size));
786 setOfMetrics[metricNumber].insert(std::make_pair(ver, metric));
811 value = e->
interpolate(val, uvw[0], uvw[1], uvw[2]);
815 Msg::Warning(
"point %g %g %g not found, looking for nearest node", x, y,
z);
816 double minDist = 1.e100;
818 const double dist = xyz.distance(it->first->point());
819 if(dist <= minDist) {
841 std::vector<SMetric3> newSetOfMetrics(nbMetrics);
842 for(
int iMetric = 0; iMetric < nbMetrics; iMetric++) {
868 newSetOfMetrics[iMetric] = metric;
887 metric =
interpolation(m1, m2, m3, m4, uvw[0], uvw[1], uvw[2]);
889 newSetOfMetrics[iMetric] = metric;
892 Msg::Warning(
"point %g %g %g not found, looking for nearest node", x,
898 metr = newSetOfMetrics[0];
899 for(
int i = 1; i < nbMetrics; i++)
919 metr =
interpolation(m1, m2, m3, m4, uvw[0], uvw[1], uvw[2]);
923 Msg::Warning(
"point %g %g %g not found, looking for nearest node", x, y,
925 double minDist = 1.e100;
927 const double dist = xyz.distance(it->first->point());
928 if(dist <= minDist) {
942 return h(0, 0) + h(1, 1) + h(2, 2);
948 auto it =
grads.find(vNew);