File: improve2.cpp

package info (click to toggle)
netgen 6.2.2601%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 13,076 kB
  • sloc: cpp: 166,627; tcl: 6,310; python: 2,868; sh: 522; makefile: 90
file content (872 lines) | stat: -rw-r--r-- 25,251 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
#include <mystdlib.h>

#include "meshing.hpp"
#include <opti.hpp>


namespace netgen
{

  class trionedge
  {
  public:
    SurfaceElementIndex tnr;
    int sidenr;

    trionedge () { tnr = 0; sidenr = 0; }
    trionedge (SurfaceElementIndex atnr, int asidenr)
    { tnr = atnr; sidenr = asidenr; }
  };

  // check if element is quad with at least one surface point -> relevant for optimization
  // (quads with 4 edge points are not optimized and can be ignored)
  bool checkMixedElement(const Mesh & mesh, FlatArray<SurfaceElementIndex> seia)
  {
      bool mixed = false;
      ParallelForRange( Range(seia), [&] (auto myrange) NETGEN_LAMBDA_INLINE
         {
            for (auto i : myrange)
            {
              const auto & sel = mesh[seia[i]];

              if(sel.GetNP() == 3)
                  continue;

              for(auto pi : Range(sel.GetNP()))
                  if(mesh[sel[pi]].Type() == SURFACEPOINT)
                      mixed = true;
            }
         });
      return mixed;
  }

  bool MeshOptimize2d :: EdgeSwapping (const int usemetric,
    Array<Neighbour> &neighbors,
    Array<bool> &swapped,
    const SurfaceElementIndex t1, const int o1,
    const int t,
    Array<int,PointIndex> &pdef,
    const bool check_only)
  {
    bool should;
    bool do_swap = false;

    SurfaceElementIndex t2 = neighbors[t1].GetNr (o1);
    int o2 = neighbors[t1].GetOrientation (o1);

    if (t2 == -1) return false;
    if (swapped[t1] || swapped[t2]) return false;
    if (mesh[t2].IsDeleted()) return false;
    if (mesh[t2].GetNP() != 3) return false;

    const int faceindex = mesh[t1].GetIndex();
    const int surfnr = mesh.GetFaceDescriptor (faceindex).SurfNr();

    PointIndex pi1 = mesh[t1].PNumMod(o1+1+1);
    PointIndex pi2 = mesh[t1].PNumMod(o1+1+2);
    PointIndex pi3 = mesh[t1].PNumMod(o1+1);
    PointIndex pi4 = mesh[t2].PNumMod(o2+1);

    PointGeomInfo gi1 = mesh[t1].GeomInfoPiMod(o1+1+1);
    PointGeomInfo gi2 = mesh[t1].GeomInfoPiMod(o1+1+2);
    PointGeomInfo gi3 = mesh[t1].GeomInfoPiMod(o1+1);
    PointGeomInfo gi4 = mesh[t2].GeomInfoPiMod(o2+1);

    bool allowswap = true;

    Vec<3> auxvec1 = mesh[pi3]-mesh[pi4];
    Vec<3> auxvec2 = mesh[pi1]-mesh[pi4];

    allowswap = allowswap && fabs(1.-(auxvec1*auxvec2)/(auxvec1.Length()*auxvec2.Length())) > 1e-4;

    if(!allowswap)
        return false;

    // normal of new
    Vec<3> nv1 = Cross (auxvec1, auxvec2);

    auxvec1 = mesh.Point(pi4)-mesh.Point(pi3);
    auxvec2 = mesh.Point(pi2)-mesh.Point(pi3);
    allowswap = allowswap && fabs(1.-(auxvec1*auxvec2)/(auxvec1.Length()*auxvec2.Length())) > 1e-4;


    if(!allowswap)
        return false;

    Vec<3> nv2 = Cross (auxvec1, auxvec2);


    // normals of original
    Vec<3> nv3 = Cross (mesh[pi1]-mesh[pi4], mesh[pi2]-mesh[pi4]);
    Vec<3> nv4 = Cross (mesh[pi2]-mesh[pi3], mesh[pi1]-mesh[pi3]);

    nv3 *= -1;
    nv4 *= -1;
    nv3.Normalize();
    nv4.Normalize();

    nv1.Normalize();
    nv2.Normalize();

    auto nvp3 = geo.GetNormal (surfnr, mesh.Point(pi3), &gi3);

    nvp3.Normalize();

    auto nvp4 = geo.GetNormal (surfnr, mesh.Point(pi4), &gi4);

    nvp4.Normalize();



    double critval = cos (M_PI / 6);  // 30 degree
    allowswap = allowswap &&
        (nv1 * nvp3 > critval) &&
        (nv1 * nvp4 > critval) &&
        (nv2 * nvp3 > critval) &&
        (nv2 * nvp4 > critval) &&
        (nvp3 * nv3 > critval) &&
        (nvp4 * nv4 > critval);


    double horder = Dist (mesh[pi1], mesh[pi2]);

    if ( // nv1 * nv2 >= 0 &&
            nv1.Length() > 1e-3 * horder * horder &&
            nv2.Length() > 1e-3 * horder * horder &&
            allowswap )
      {
        if (!usemetric)
          {
            int e = pdef[pi1] + pdef[pi2] - pdef[pi3] - pdef[pi4];
            double d =
                Dist2 (mesh[pi1], mesh[pi2]) -
                Dist2 (mesh[pi3], mesh[pi4]);

            should = e >= t && (e > 2 || d > 0);
          }
        else
          {
            double loch = 0.25*(mesh.GetH(pi1) + mesh.GetH(pi2) + mesh.GetH(pi3) + mesh.GetH(pi4));
            should =
                CalcTriangleBadness (mesh[pi4], mesh[pi3], mesh[pi1], metricweight, loch) +
                CalcTriangleBadness (mesh[pi3], mesh[pi4], mesh[pi2], metricweight, loch) <
                CalcTriangleBadness (mesh[pi1], mesh[pi2], mesh[pi3], metricweight, loch) +
                CalcTriangleBadness (mesh[pi2], mesh[pi1], mesh[pi4], metricweight, loch);
          }

        if (allowswap)
          {
            Element2d sw1 (pi4, pi3, pi1);
            Element2d sw2 (pi3, pi4, pi2);

            int legal1 =
                mesh.LegalTrig (mesh[t1]) +
                mesh.LegalTrig (mesh[t2]);
            int legal2 =
                mesh.LegalTrig (sw1) + mesh.LegalTrig (sw2);

            if (legal1 < legal2) should = true;
            if (legal2 < legal1) should = false;
          }

        do_swap = should;
        if (should && !check_only)
          {
            // do swapping !

            mesh[t1] = { { pi1, gi1 }, { pi4, gi4 }, { pi3, gi3 } };
            mesh[t2] = { { pi2, gi2 }, { pi3, gi3 }, { pi4, gi4 } };

            pdef[pi1]--;
            pdef[pi2]--;
            pdef[pi3]++;
            pdef[pi4]++;

            swapped[t1] = true;
            swapped[t2] = true;
          }
      }
    return do_swap;
  }

 
  void MeshOptimize2d :: EdgeSwapping (int usemetric)
  {
    static Timer timer("EdgeSwapping (2D)"); RegionTimer reg(timer);
    static Timer timer_nb("EdgeSwapping-Find neighbors");
    if (usemetric)
      PrintMessage (3, "Edgeswapping, metric");
    else
      PrintMessage (3, "Edgeswapping, topological");

    static Timer timerstart("EdgeSwapping 2D start");
    timerstart.Start();

    Array<SurfaceElementIndex> seia;
    mesh.GetSurfaceElementsOfFace (faceindex, seia);

    if(checkMixedElement(mesh, seia))
    {
        timerstart.Stop();
        return GenericImprove();
    }

    Array<Neighbour> neighbors(mesh.GetNSE());
    auto elements_on_node = mesh.CreateCompressedPoint2SurfaceElementTable(faceindex);

    Array<bool> swapped(mesh.GetNSE());
    Array<int,PointIndex> pdef(mesh.GetNP());
    Array<double,PointIndex> pangle(mesh.GetNP());

    static const double minangle[] = { 0, 1.481, 2.565, 3.627, 4.683, 5.736, 7, 9 };


    if(faceindex == 0)
      {
        ParallelFor( Range(pangle), [&] (auto i) NETGEN_LAMBDA_INLINE
            {
              pangle[i] = 0.0;
            });
      }
    else
      {
        ParallelFor( Range(seia), [&] (auto i) NETGEN_LAMBDA_INLINE
            {
              const Element2d & sel = mesh[seia[i]];
              for (int j = 0; j < 3; j++)
                  pangle[sel[j]] = 0.0;
            });
      }

    ParallelFor( Range(seia), [&] (auto i) NETGEN_LAMBDA_INLINE
        {
          const Element2d & sel = mesh[seia[i]];
          for (int j = 0; j < 3; j++)
            {
              POINTTYPE typ = mesh[sel[j]].Type();
              if (typ == FIXEDPOINT || typ == EDGEPOINT)
                {
                  AtomicAdd(pangle[sel[j]],
                    Angle (mesh[sel[(j+1)%3]] - mesh[sel[j]],
                           mesh[sel[(j+2)%3]] - mesh[sel[j]]));
                }
            }
       });

    ParallelFor( Range(seia), [&] (auto i) NETGEN_LAMBDA_INLINE
        {
          const Element2d & sel = mesh[seia[i]];
          for (int j = 0; j < 3; j++)
            {
              PointIndex pi = sel[j];
              if (mesh[pi].Type() == INNERPOINT || mesh[pi].Type() == SURFACEPOINT)
                pdef[pi] = -6;
              else
                for (int j = 0; j < 8; j++)
                  if (pangle[pi] >= minangle[j])
                    pdef[pi] = -1-j;
            }
       });

    ParallelFor( Range(seia), [this, &pdef, &neighbors, &seia, &elements_on_node] (auto i) NETGEN_LAMBDA_INLINE
        {
          auto sei = seia[i];
          for (PointIndex pi : mesh[sei].template PNums<3>())
              AsAtomic(pdef[pi])++;
          for (int j = 0; j < 3; j++)
            {
              neighbors[sei].SetNr (j, -1);
              neighbors[sei].SetOrientation (j, 0);
            }

          const auto sel = mesh[sei];
          auto index = sel.GetIndex();
          for (int j = 0; j < 3; j++)
            {
              PointIndex pi1 = sel.PNumMod(j+2);
              PointIndex pi2 = sel.PNumMod(j+3);
              if(mesh.IsSegment(pi1, pi2))
                  continue;

              for (auto sei_other : elements_on_node[pi1])
                {
                  if(sei_other==sei) continue;
                  if(mesh[sei_other].GetIndex()!=index) continue;
                  const auto & other = mesh[sei_other];
                  int pi1_other = -1;
                  int pi2_other = -1;
                  bool common_edge = false;
                  for (int k = 0; k < 3; k++)
                    {
                      if(other[k] == pi1)
                          pi1_other = k;
                      if(other[k] == pi2)
                        {
                          pi2_other = k;
                          common_edge = true;
                        }
                    }

                  if(common_edge)
                    {
                      neighbors[sei].SetNr (j, sei_other);
                      neighbors[sei].SetOrientation (j, 3-pi1_other-pi2_other);
                    }
                }
            }
        });


    for (SurfaceElementIndex sei : seia)
      swapped[sei] = false;
    
    timerstart.Stop();
  


    Array<std::pair<SurfaceElementIndex,int>> improvement_candidates(3*seia.Size());
    atomic<int> cnt(0);

    int t = 4;
    bool done = false;
    while (!done && t >= 2)
      {
        cnt = 0;
        ParallelFor( Range(seia), [&] (auto i) NETGEN_LAMBDA_INLINE
          {
            SurfaceElementIndex t1 = seia[i];

            if (mesh[t1].IsDeleted())
              return;

            if (swapped[t1])
              return;

            if(mesh[t1].GetNP() != 3)
              return;

            if (multithread.terminate)
              throw NgException ("Meshing stopped");

            for (int o1 = 0; o1 < 3; o1++)
                if(EdgeSwapping(usemetric, neighbors, swapped, t1, o1, t, pdef, true))
                    improvement_candidates[cnt++]= std::make_pair(t1,o1);
          });

        auto elements_with_improvement = improvement_candidates.Range(0, cnt.load());
        QuickSort(elements_with_improvement);

        for (auto [t1,o1] : elements_with_improvement)
            done |= EdgeSwapping(usemetric, neighbors, swapped, t1, o1, t, pdef, false);
	t--;
      }

    mesh.SetNextTimeStamp();
  }



  template <typename T_PI2SEI>
  double CombineImproveEdge( Mesh & mesh,
                           const T_PI2SEI & elementsonnode,
                           Array<Vec<3>, PointIndex> & normals,
                           Array<bool, PointIndex> & fixed,
                           PointIndex pi1, PointIndex pi2,
                           double metricweight,
                           bool check_only = true)
  {
    Vec<3> nv;
    ArrayMem<SurfaceElementIndex, 20> hasonepi, hasbothpi;

    if (!pi1.IsValid() || !pi2.IsValid())
        return 0.0;

    bool debugflag = 0;

    if (debugflag)
      {
        (*testout) << "Combineimprove "
            << "pi1 = " << pi1 << " pi2 = " << pi2 << endl;
      }

    if (fixed[pi2])
        return 0.0;

    double loch = 0.5*(mesh.GetH(pi1) + mesh.GetH(pi2));
    int faceindex = -1;

    for (SurfaceElementIndex sei2 : elementsonnode[pi1])
      {
        const Element2d & el2 = mesh[sei2];

        if (el2.IsDeleted()) continue;

        if (el2[0] == pi2 || el2[1] == pi2 || el2[2] == pi2)
          {
            faceindex = el2.GetIndex();
            hasbothpi.Append (sei2);
          }
      }

    if(hasbothpi.Size()==0)
        return 0.0;


    nv = normals[pi2];


    for (SurfaceElementIndex sei2 :  elementsonnode[pi2])
      {
        const Element2d & el2 = mesh[sei2];
        if (el2.IsDeleted()) continue;
        if (!el2.PNums<3>().Contains (pi1))
            hasonepi.Append (sei2);
      }

    double bad1 = 0;
    int illegal1 = 0, illegal2 = 0;
    /*
       for (SurfaceElementIndex sei : hasonepi)
       {
       const Element2d & el = mesh[sei];
       bad1 += CalcTriangleBadness (mesh[el[0]], mesh[el[1]], mesh[el[2]],
       nv, -1, loch);
       illegal1 += 1-mesh.LegalTrig(el);
       }
       */
    for (const Element2d & el : mesh.SurfaceElements()[hasonepi])
      {
        bad1 += CalcTriangleBadness (mesh[el[0]], mesh[el[1]], mesh[el[2]],
                nv, metricweight, loch);
        illegal1 += 1-mesh.LegalTrig(el);
      }

    for (int k = 0; k < hasbothpi.Size(); k++)
      {
        const Element2d & el = mesh[hasbothpi[k]];
        bad1 += CalcTriangleBadness (mesh[el[0]], mesh[el[1]], mesh[el[2]],
                nv, metricweight, loch);
        illegal1 += 1-mesh.LegalTrig(el);
      }

    double bad2 = 0;
    for (int k = 0; k < hasonepi.Size(); k++)
      {
        Element2d el = mesh[hasonepi[k]];
        for (auto i : Range(3))
            if(el[i]==pi2)
                el[i] = pi1;

        double err =
            CalcTriangleBadness (mesh[el[0]], mesh[el[1]], mesh[el[2]],
                    nv, metricweight, loch);
        bad2 += err;

        Vec<3> hnv = Cross (Vec3d (mesh[el[0]],
                    mesh[el[1]]),
                Vec3d (mesh[el[0]],
                    mesh[el[2]]));
        if (hnv * nv < 0)
            bad2 += 1e10;

        for (int l = 0; l < 3; l++)
          {
            auto normal = normals[el[l]];
            if(mesh[el[l]].Type() != SURFACEPOINT)
            {
                // point possibly on edge -> multiple normal vectors (for each surface), need to calculate it to be sure
                const int surfnr = mesh.GetFaceDescriptor (el.GetIndex()).SurfNr();
                normal = mesh.GetGeometry()->GetNormal (surfnr, mesh[el[l]], &el.GeomInfo()[l]);
            }
            if ( ( normal * nv) < 0.5)
                bad2 += 1e10;
          }

        illegal2 += 1-mesh.LegalTrig(el);
      }

    if (debugflag)
      {
        (*testout) << "bad1 = " << bad1 << ", bad2 = " << bad2 << endl;
      }

    bool should = (illegal2<=illegal1 && bad2 < bad1 && bad2 < 1e4);
    if(illegal2 < illegal1)
      {
        should = true;
        bad1 += 1e4;
      }

    double d_badness = should * (bad2-bad1);

    if(check_only)
        return d_badness;

    if (should)
      {
        /*
           (*testout) << "combine !" << endl;
           (*testout) << "bad1 = " << bad1 << ", bad2 = " << bad2 << endl;
           (*testout) << "illegal1 = " << illegal1 << ", illegal2 = " << illegal2 << endl;
           (*testout) << "loch = " << loch << endl;
           */

        PointGeomInfo gi;
        // bool gi_set(false);

        /*
           Element2d *el1p(NULL);
           int l = 0;
           while(mesh[elementsonnode[pi1][l]].IsDeleted() && l<elementsonnode[pi1].Size()) l++;
           if(l<elementsonnode[pi1].Size())
           el1p = &mesh[elementsonnode[pi1][l]];
           else
           cerr << "OOPS!" << endl;

           for (l = 0; l < el1p->GetNP(); l++)
           if ((*el1p)[l] == pi1)
           {
           gi = el1p->GeomInfoPi (l+1);
        // gi_set = true;
        }
        */
        for (auto sei : hasbothpi)
          {
            const Element2d & el1p = mesh[sei];
            if (el1p.IsDeleted()) continue;
            if(el1p.GetIndex() != faceindex) continue;

            for (int l = 0; l < el1p.GetNP(); l++)
                if (el1p[l] == pi1)
                    gi = el1p.GeomInfo()[l];
            break;
          }


        // (*testout) << "Connect point " << pi2 << " to " << pi1 << "\n";
        // for (int k = 0; k < elementsonnode[pi2].Size(); k++)
        for (SurfaceElementIndex sei2 : elementsonnode[pi2])
          {
            Element2d & el = mesh[sei2];
            if (el.IsDeleted()) continue;
            if (el.PNums().Contains(pi1)) continue;

            for (auto l : Range(el.GetNP()))
              {
                if (el[l] == pi2)
                  {
                    el[l] = pi1;
                    el.GeomInfo()[l] = gi;
                  }

                fixed[el[l]] = true;
              }
          }

        for (auto sei : hasbothpi)
            mesh[sei].Delete();

      }
    return d_badness;
  }

  void MeshOptimize2d :: CombineImprove ()
  {
    SplitImprove();
    PrintMessage (3, "Combine improve");

    if (multithread.terminate)
        throw NgException ("Meshing stopped");

    static Timer timer ("Combineimprove 2D");
    RegionTimer reg (timer);

    static Timer timerstart ("Combineimprove 2D start");
    timerstart.Start();


    static Timer timerstart1 ("Combineimprove 2D start1");
    timerstart1.Start();

    
    Array<SurfaceElementIndex> seia;
    mesh.GetSurfaceElementsOfFace (faceindex, seia);

    if(checkMixedElement(mesh, seia))
    {
        timerstart1.Stop();
        timerstart.Stop();
        return;
    }

    int np = mesh.GetNP();

    auto elementsonnode = mesh.CreateCompressedPoint2SurfaceElementTable(faceindex);

    // int ntasks = ngcore::TaskManager::GetMaxThreads();
    Array<std::tuple<PointIndex, PointIndex>> edges;

    BuildEdgeList( mesh, elementsonnode, edges );

    Array<bool,PointIndex> fixed(np);
    ParallelFor( fixed.Range(), [&] (auto i) NETGEN_LAMBDA_INLINE
            { fixed[i] = mesh[i].Type() != SURFACEPOINT; });

    timerstart1.Stop();

    ParallelFor( mesh.LockedPoints().Range(), [&] (auto i) NETGEN_LAMBDA_INLINE
            {
              fixed[mesh.LockedPoints()[i]] = true;
            });


    Array<Vec<3>,PointIndex> normals(np);

    ParallelFor( mesh.Points().Range(), [&] (auto pi) NETGEN_LAMBDA_INLINE
        {
            if (elementsonnode[pi].Size())
              {
                Element2d & hel = mesh[elementsonnode[pi][0]];
                for (int k = 0; k < 3; k++)
                  if (hel[k] == pi)
                    {
                      const int faceindex = hel.GetIndex();
                      const int surfnr = mesh.GetFaceDescriptor (faceindex).SurfNr();
                      normals[pi] = geo.GetNormal (surfnr, mesh[pi], &hel.GeomInfoPi(k+1));
                      break;
                    }
              }
        }, TasksPerThread(4));

    timerstart.Stop();

    // Find edges with improvement
    Array<std::tuple<double, int>> candidate_edges(edges.Size());
    std::atomic<int> improvement_counter(0);

    ParallelFor( Range(edges), [&] (auto i) NETGEN_LAMBDA_INLINE
      {
        auto [pi1, pi2] = edges[i];
        double d_badness = CombineImproveEdge(mesh, elementsonnode, normals, fixed, pi1, pi2, metricweight, true);
        if(d_badness < 0.0)
            candidate_edges[improvement_counter++] = make_tuple(d_badness, i);
        d_badness = CombineImproveEdge(mesh, elementsonnode, normals, fixed, pi2, pi1, metricweight, true);
        if(d_badness < 0.0)
            candidate_edges[improvement_counter++] = make_tuple(d_badness, -i);
      }, TasksPerThread(4));

    auto edges_with_improvement = candidate_edges.Part(0, improvement_counter.load());
    QuickSort(edges_with_improvement);

    for(auto [d_badness, ei] : edges_with_improvement)
      {
        auto [pi1, pi2] = edges[ei < 0 ? -ei : ei];
        if(ei<0)
            Swap(pi1,pi2);
        CombineImproveEdge(mesh, elementsonnode, normals, fixed, pi1, pi2, metricweight, false);
      }

    //  mesh.Compress();
    mesh.SetNextTimeStamp();
  }

  void MeshOptimize2d :: SplitImprove()
  {
    if (!faceindex)
      {
        PrintMessage (3, "Split improve");

        mesh.CalcSurfacesOfNode(); // TODO: needed?
        for (faceindex = 1; faceindex <= mesh.GetNFD(); faceindex++)
          {
            SplitImprove();

            if (multithread.terminate)
                throw NgException ("Meshing stopped");
          }

        faceindex = 0;
        mesh.Compress(); // TODO: needed?
        return;
      }

    Array<SurfaceElementIndex> elements;
    mesh.GetSurfaceElementsOfFace (faceindex, elements);

    // return if we have quads in this surface
    for (auto & ei : elements)
        if (mesh[ei].GetNP() != 3)
            return;

    // maps from edges to adjacent trigs
    INDEX_2_HASHTABLE<tuple<SurfaceElementIndex, SurfaceElementIndex>> els_on_edge(2*elements.Size() + 2);

    // build els_on_edge table
    for (SurfaceElementIndex sei : elements)
      {
        const Element2d & sel = mesh[sei];

        for (int j = 0; j < 3; j++)
          {
            PointIndex pi1 = sel.PNumMod(j+2);
            PointIndex pi2 = sel.PNumMod(j+3);

            if (mesh.IsSegment (pi1, pi2)) continue;

            PointIndices<2> ii2 (pi1, pi2);
            ii2.Sort();
            if (els_on_edge.Used (ii2))
              {
                auto els = els_on_edge.Get(ii2);
                get<1>(els) = sei;
                els_on_edge.Set(ii2, els);
              }
            else
              {
                els_on_edge.Set (ii2, make_tuple(sei, sei));
              }
          }
      }

    // split edges of illegal trigs
    for (SurfaceElementIndex sei : elements)
      {
        Element2d & sel = mesh[sei];

        if (sel.IsDeleted()) continue;

        // TODO: split also bad trigs, nut just illegal ones
        if (mesh.LegalTrig(sel)) continue;

        // find longest edge
        PointIndices<2> edge;
        double edge_len = 0;
        PointIndex pi1, pi2, pi3, pi4;
        PointGeomInfo gi1, gi2, gi3, gi4;
        for(auto j : Range(1,4))
          {
            auto test_pi1 = sel.PNumMod(j);
            auto test_pi2 = sel.PNumMod(j+1);
            if (mesh.IsSegment(test_pi1, test_pi2))
              continue;
            auto len = (mesh[test_pi2]-mesh[test_pi1]).Length();
            if(len > edge_len)
              {
                edge = {test_pi1, test_pi2};
                edge.Sort();
                edge_len = len;
                pi1 = test_pi1;
                pi2 = test_pi2;
                pi3 = sel.PNumMod(j+2);
                gi1 = sel.GeomInfoPiMod(j);
                gi2 = sel.GeomInfoPiMod(j+1);
                gi3 = sel.GeomInfoPiMod(j+2);
              }
          }
        if(!edge_len)
          throw Exception("Couldn't find edge to split, something is wrong");
        // get neighbor element
        auto els = els_on_edge.Get(edge);
        SurfaceElementIndex other_i = get<0>(els);
        if(other_i==sei) other_i = get<1>(els);
        auto & other = mesh[other_i];

        // find opposite point of neighbor element
        for (int j = 0; j < 3; j++)
          if(other[j]!=pi1 && other[j]!=pi2)
            {
              pi4 = other[j];
              gi4 = other.GeomInfoPi(j);
              break;
            }

        // split edge pi1,pi2
        Point<3> p5;
        PointIndex pi5;
        PointGeomInfo gi5;

        geo.PointBetween(mesh[pi1], mesh[pi2], 0.5,
                         faceindex,
                         gi1, gi2, p5, gi5);

        pi5 = mesh.AddPoint(p5);

        Element2d e1(3);
        e1.SetIndex(faceindex);
        e1={ {pi1,gi1}, {pi5,gi5}, {pi3,gi3} };
        mesh.AddSurfaceElement( e1 );

        Element2d e2(3);
        e2.SetIndex(faceindex);
        e2 ={ {pi5,gi5}, {pi2,gi2}, {pi3,gi3} };
        mesh.AddSurfaceElement( e2 );

        Element2d e3(3);
        e3.SetIndex(faceindex);
        e3 ={ {pi1,gi1}, {pi4,gi4}, {pi5,gi5} };
        mesh.AddSurfaceElement( e3 );

        Element2d e4(3);
        e4.SetIndex(faceindex);
        e4 ={ {pi4,gi4}, {pi2,gi2}, {pi5,gi5} };
        mesh.AddSurfaceElement( e4 );

        sel.Delete();
        other.Delete();
      }

    mesh.SetNextTimeStamp();
  }

  void MeshOptimize2d :: CheckMeshApproximation (Mesh & mesh)
  {
    // Check angles between elements and normals at corners
    /*
  
    int i, j;
    int ne = mesh.GetNSE();
    int surfnr;
  
    Vec3d n, ng;
    NgArray<Vec3d> ngs(3);

    (*mycout) << "Check Surface Approximation" << endl;
    (*testout) << "Check Surface Approximation" << endl;

    for (i = 1; i <= ne; i++)
    {
    const Element2d & el = mesh.SurfaceElement(i);
    surfnr = mesh.GetFaceDescriptor (el.GetIndex()).SurfNr();
    Vec3d n = Cross (mesh.Point (el.PNum(1)) - mesh.Point (el.PNum(2)),
    mesh.Point (el.PNum(1)) - mesh.Point (el.PNum(3)));
    n /= n.Length();

    for (j = 1; j <= el.GetNP(); j++)
    {
    SelectSurfaceOfPoint (mesh.Point(el.PNum(j)), el.GeomInfoPi(j));
    GetNormalVector (surfnr, mesh.Point(el.PNum(j)), ng);
    ng /= ng.Length();
    ngs.Elem(j) = ng;

    double angle =  (180.0 / M_PI) * Angle (n, ng);
    if (angle > 60)
    {
    (*testout) << "el " << i << " node " << el.PNum(j)
    << "has angle = " << angle << endl;
    }
    }	

    for (j = 1; j <= 3; j++)
    {
    double angle =  (180.0 / M_PI) * Angle (ngs.Get(j), ngs.Get(j%3+1));
    if (angle > 60)
    {
    (*testout) << "el " << i << " node-node " 
    << ngs.Get(j) << " - " << ngs.Get(j%3+1)
    << " has angle = " << angle << endl;
    }
    }
    }
    */
  }
}