File: vtkHyperTreeGridGeometry3DImpl.cxx

package info (click to toggle)
vtk9 9.5.2%2Bdfsg3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 205,916 kB
  • sloc: cpp: 2,336,565; ansic: 327,116; python: 111,200; yacc: 4,104; java: 3,977; sh: 3,032; xml: 2,771; perl: 2,189; lex: 1,787; makefile: 178; javascript: 165; objc: 153; tcl: 59
file content (957 lines) | stat: -rw-r--r-- 34,346 bytes parent folder | download
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
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkHyperTreeGridGeometry3DImpl.h"
#include "vtkBitArray.h"
#include "vtkCellArray.h"
#include "vtkDataSetAttributes.h"
#include "vtkHyperTreeGrid.h"
#include "vtkHyperTreeGridNonOrientedVonNeumannSuperCursor.h"
#include "vtkMathUtilities.h"
#include "vtkMergePoints.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkUnsignedCharArray.h"

#include <set>
#include <vector>

VTK_ABI_NAMESPACE_BEGIN
namespace
{
constexpr unsigned int NUMBER_OF_POINTS = 8;
constexpr unsigned int NUMBER_OF_EDGES = 12;
constexpr unsigned int MAX_NUMBER_OF_INTERFACE_EDGES = 8;

// Point Ids for each face of a cell
constexpr unsigned int FACE_PTS_IDS[6][4] = { { 0, 1, 3, 2 }, { 0, 4, 5, 1 }, { 0, 2, 6, 4 },
  { 1, 3, 7, 5 }, { 2, 6, 7, 3 }, { 4, 5, 7, 6 } };

// Edge Ids for each face of a cell
constexpr unsigned int FACE_EDGES_IDS[6][4] = { { 1, 5, 3, 0 }, { 0, 4, 8, 2 }, { 2, 9, 6, 1 },
  { 3, 7, 10, 4 }, { 6, 11, 7, 5 }, { 8, 10, 11, 9 } };

// Point Ids for each edge of a cell
const std::vector<std::pair<unsigned int, unsigned int>> EDGE_PTS_IDS = { { 0, 1 } /*  0 */,
  { 0, 2 } /*  1 */, { 0, 4 } /* 2 */, { 1, 3 } /* 3 */, { 1, 5 } /* 4 */, { 2, 3 } /*  5 */,
  { 2, 6 } /*  6 */, { 3, 7 } /* 7 */, { 4, 5 } /* 8 */, { 4, 6 } /* 9 */, { 5, 7 } /* 10 */,
  { 6, 7 } /* 11 */ };

// Orientation axis for each edge of a cell
// 0:X, 1:Y, 2:Z
constexpr unsigned int EDGE_AXIS[12] = { 0, 1, 2, 1, 2, 0, 2, 2, 0, 1, 1, 0 };

// Flag used to indicate to treat all faces of a coarse cell
// All bytes are set to 1 : all faces should be considered
constexpr unsigned char TREAT_ALL_FACES = std::numeric_limits<unsigned char>::max();

// Neighbor ids of the VonNeumann cursor
constexpr unsigned int VON_NEUMANN_NEIGH_ID[] = { 0, 1, 2, 4, 5, 6 };

// Orientation (normal of the plane) for each face of a cell : 0:YZ, 1:XZ, 2:XY
constexpr unsigned int FACE_ORIENTATION[] = { 2, 1, 0, 0, 1, 2 };

// Indicate if an offset (cell size) should be applied for a given face
// Concerns the faces that do not share the cell origin
constexpr unsigned int FACE_OFFSET[] = { 0, 0, 0, 1, 1, 1 };

// Arbitrary default edge index
// Should be superior to 12, i.e. the number of edges of a given cell
const unsigned int VTK_DEFAULT_EDGE_INDEX = 42;
}

//------------------------------------------------------------------------------
struct vtkHyperTreeGridGeometry3DImpl::HTG3DPoint
{
  double Coords[3] = { 0., 0., 0. };
  bool IsValid = false;
  vtkIdType Id = -1;
  bool HasInterfaceA = false;
  bool HasInterfaceB = false;
  double DistanceToInterfaceA = 0.;
  double DistanceToInterfaceB = 0.;
};

//------------------------------------------------------------------------------
vtkHyperTreeGridGeometry3DImpl::vtkHyperTreeGridGeometry3DImpl(bool mergePoints,
  vtkHyperTreeGrid* input, vtkPoints* outPoints, vtkCellArray* outCells,
  vtkDataSetAttributes* inCellDataAttributes, vtkDataSetAttributes* outCellDataAttributes,
  bool passThroughCellIds, const std::string& originalCellIdArrayName, bool fillMaterial)
  : vtkHyperTreeGridGeometryImpl(input, outPoints, outCells, inCellDataAttributes,
      outCellDataAttributes, passThroughCellIds, originalCellIdArrayName, fillMaterial)
{
  if (mergePoints)
  {
    this->Locator = vtkSmartPointer<vtkMergePoints>::New();
    this->Locator->InitPointInsertion(outPoints, input->GetBounds());
  }
  this->BranchFactor = static_cast<int>(this->Input->GetBranchFactor());
  this->InPureMaskArray = this->Input->GetPureMask();
}

//----------------------------------------------------------------------------------------------
vtkHyperTreeGridGeometry3DImpl::~vtkHyperTreeGridGeometry3DImpl() = default;

//----------------------------------------------------------------------------------------------
void vtkHyperTreeGridGeometry3DImpl::GenerateGeometry()
{
  vtkHyperTreeGrid::vtkHyperTreeGridIterator it;
  this->Input->InitializeTreeIterator(it);

  vtkIdType hyperTreeId;
  vtkNew<vtkHyperTreeGridNonOrientedVonNeumannSuperCursor> cursor;

  // Recursively process all HyperTrees
  while (it.GetNextTree(hyperTreeId))
  {
    this->Input->InitializeNonOrientedVonNeumannSuperCursor(cursor, hyperTreeId);
    this->RecursivelyProcessTree(cursor, ::TREAT_ALL_FACES);
  }
}

//----------------------------------------------------------------------------------------------
void vtkHyperTreeGridGeometry3DImpl::RecursivelyProcessTree(
  vtkHyperTreeGridNonOrientedVonNeumannSuperCursor* cursor,
  unsigned char coarseCellFacesToBeTreated)
{
  vtkIdType cellId = cursor->GetGlobalNodeIndex();

  // For a given cell, we can generate faces if the cell is a leaf or if the cell is masked
  if (cursor->IsLeaf() || this->IsMaskedOrGhost(cellId))
  {
    // Improvement: coarseCellFacesToBeTreated can also be used there (not used for now)
    this->GenerateCellSurface(cursor, coarseCellFacesToBeTreated, cellId);
    return;
  }

  // Case of a pure, non-masked coarse cell (optimisation)
  if (this->InPureMaskArray && this->InPureMaskArray->GetValue(cellId) == 0)
  {
    // All child cells are in the same material, so we can only treat
    // the ones at the border of the coarse cell
    std::set<int> childList;
    std::vector<unsigned char> childCellFacesToBeTreated(cursor->GetNumberOfChildren(), 0);

    for (unsigned int f = 0; f < 3; ++f) // dimension
    {
      for (unsigned int o = 0; o < 2; ++o) // left, center, right
      {
        int neighborIdx = (2 * o - 1) * (f + 1);
        if ((coarseCellFacesToBeTreated & (1 << (3 + neighborIdx))))
        {
          bool isValidN = cursor->HasTree(3 + neighborIdx);
          vtkIdType neighboringCellId = 0;
          if (isValidN)
          {
            neighboringCellId = cursor->GetGlobalNodeIndex(3 + neighborIdx);
          }
          if (!isValidN || this->InPureMaskArray->GetValue(neighboringCellId))
          {
            // If the neighboring cells do not exist or are not pure,
            // we have border children
            int iMin = (f == 0 && o == 1) ? this->BranchFactor - 1 : 0;
            int iMax = (f == 0 && o == 0) ? 1 : this->BranchFactor;
            int jMin = (f == 1 && o == 1) ? this->BranchFactor - 1 : 0;
            int jMax = (f == 1 && o == 0) ? 1 : this->BranchFactor;
            int kMin = (f == 2 && o == 1) ? this->BranchFactor - 1 : 0;
            int kMax = (f == 2 && o == 0) ? 1 : this->BranchFactor;
            for (int i = iMin; i < iMax; ++i)
            {
              for (int j = jMin; j < jMax; ++j)
              {
                for (int k = kMin; k < kMax; ++k)
                {
                  unsigned int ichild = i + this->BranchFactor * (j + this->BranchFactor * k);

                  // We can request a border child cell more than one time,
                  // once for each of it's "exposed" face
                  childList.insert(ichild);
                  childCellFacesToBeTreated[ichild] |= (1 << (3 + neighborIdx));
                } // k
              }   // j
            }     // i
          }       // if ...
        }
      } // o
    }   // f
    for (std::set<int>::iterator it = childList.begin(); it != childList.end(); ++it)
    {
      cursor->ToChild(*it);
      this->RecursivelyProcessTree(cursor, childCellFacesToBeTreated[*it]);
      cursor->ToParent();
    } // ichild
    return;
  }

  // Search for any child cell that is not present in the coarse cell material
  for (unsigned int ichild = 0; ichild < cursor->GetNumberOfChildren(); ++ichild)
  {
    cursor->ToChild(ichild);
    this->RecursivelyProcessTree(cursor, ::TREAT_ALL_FACES);
    cursor->ToParent();
  }
}

//------------------------------------------------------------------------------
void vtkHyperTreeGridGeometry3DImpl::GenerateCellSurface(
  vtkHyperTreeGridNonOrientedVonNeumannSuperCursor* cursor,
  unsigned char vtkNotUsed(coarseCellFacesToBeTreated), vtkIdType cellId)
{
  // Determine if the current cell contains an interface and
  // fill the related member variables accordingly
  this->ProbeForCellInterface(cellId);

  // Retrieve info about the current cell
  unsigned level = cursor->GetLevel();
  bool masked = cursor->IsMasked();
  const double* cellOrigin = cursor->GetOrigin();
  const double* cellSize = cursor->GetSize();

  std::vector<HTG3DPoint> cellPoints;
  cellPoints.resize(NUMBER_OF_POINTS);
  std::vector<std::pair<HTG3DPoint, HTG3DPoint>> edgePoints;
  edgePoints.resize(NUMBER_OF_EDGES + MAX_NUMBER_OF_INTERFACE_EDGES);

  std::map<unsigned int, std::pair<HTG3DPoint*, unsigned int>> internalFaceA;
  std::map<unsigned int, std::pair<HTG3DPoint*, unsigned int>> internalFaceB;

  // Iterate over all neighboring cells using the Von Neumann neighborhood
  for (unsigned int faceId = 0; faceId < 6; ++faceId)
  {
    const unsigned int& neighborId = ::VON_NEUMANN_NEIGH_ID[faceId];
    const unsigned int& faceOrientation = ::FACE_ORIENTATION[faceId];
    const unsigned int& faceOffset = ::FACE_OFFSET[faceId];

    // Retrieve cursor to neighbor across face
    // Retrieve tree, leaf flag, and mask of neighbor cursor
    bool leafN = false;
    vtkIdType neighborCellId = 0;
    unsigned int levelN = 0;
    vtkHyperTree* treeN = cursor->GetInformation(neighborId, levelN, leafN, neighborCellId);
    int maskedN = cursor->IsMasked(neighborId);
    bool hasInterfaceCellN = this->GetHasInterface(cursor->GetGlobalNodeIndex(neighborId));

    // We generate a face if one of the following conditions are fulfilled:
    // - The current cell is unmasked, and the neighboring cell is masked
    // - The current cell is unmasked, and has no neighbouring cell
    // - The current cell is unmasked, and has an interface
    // - The current cell is unmasked, and has the neighboring cell has an interface
    // - The current cell is masked, and has a neighbor that is a non-masked leaf of lower level
    // This ensures that faces between unmasked and masked cells will be generated once and only
    // once.
    if ((!masked && (!treeN || maskedN || this->HasInterfaceOnThisCell || hasInterfaceCellN)) ||
      (masked && treeN && leafN && levelN < level && !maskedN))
    {
      // Generate face with corresponding normal and offset
      // Here we differentiate the case where the current cell is masked.
      // In that case, we must copy the data from the neighboring cell to the created face,
      // and not from the current cell.
      this->GenerateOneCellFace(cellPoints, edgePoints, faceId, (masked ? neighborCellId : cellId),
        cellOrigin, cellSize, faceOffset, faceOrientation, internalFaceA, internalFaceB);
    }
  }

  auto createInterfacePoints =
    [&](std::map<unsigned int, std::pair<HTG3DPoint*, unsigned int>>& interface)
  {
    if (!interface.empty() && interface.size() >= 3)
    {
      std::vector<vtkIdType> newOutputPointIds;
      unsigned int firstEdge = interface.begin()->first;
      if (firstEdge == VTK_DEFAULT_EDGE_INDEX)
      {
        vtkWarningWithObjectMacro(nullptr, "Uninitialized edge encountered");
        return;
      }
      else
      {
        HTG3DPoint* pt = interface[firstEdge].first;
        newOutputPointIds.emplace_back(pt->Id);
        unsigned int next = interface[firstEdge].second;
        while (next != firstEdge && next != VTK_DEFAULT_EDGE_INDEX)
        {
          // XXX: think adding an "emergency" breaking condition to
          // avoid potential infinite looping
          pt = interface[next].first;
          newOutputPointIds.emplace_back(pt->Id);
          next = interface[next].second;
        }
        if (next == VTK_DEFAULT_EDGE_INDEX)
        {
          vtkWarningWithObjectMacro(nullptr, "Uninitialized edge encountered");
          return;
        }
      }

      // XXX: We need to clarify a criterion for valid cells
      // (i.e. that can be added to the output)
      if (!newOutputPointIds.empty())
      {
        this->CreateNewCellAndCopyData(newOutputPointIds, cellId);
      }
    }
  };

  // Create interface points for interface A and B if they are defined
  createInterfacePoints(internalFaceA);
  createInterfacePoints(internalFaceB);
}

//------------------------------------------------------------------------------
void vtkHyperTreeGridGeometry3DImpl::GenerateOneCellFace(std::vector<HTG3DPoint>& cellPoints,
  std::vector<std::pair<HTG3DPoint, HTG3DPoint>>& edgePoints, unsigned int faceId, vtkIdType cellId,
  const double* cellOrigin, const double* cellSize, unsigned int offset, unsigned int orientation,
  std::map<unsigned int, std::pair<HTG3DPoint*, unsigned int>>& internalFaceA,
  std::map<unsigned int, std::pair<HTG3DPoint*, unsigned int>>& internalFaceB)
{
  double pt[3] = { 0., 0., 0. };

  // We compute the current cell points coordinates only if we didn't do it before
  // (i.e. if IsValid == false for a given point)
  // XXX: The code below can be reworked. We can think about computing all
  // cell points once and for all before calling this function and remove all
  // the logic below.
  HTG3DPoint* currentPt = &cellPoints[::FACE_PTS_IDS[faceId][0]];
  if (currentPt->IsValid)
  {
    currentPt = &cellPoints[::FACE_PTS_IDS[faceId][1]];
    if (currentPt->IsValid)
    {
      currentPt = &cellPoints[::FACE_PTS_IDS[faceId][2]];
      if (currentPt->IsValid)
      {
        currentPt = &cellPoints[::FACE_PTS_IDS[faceId][3]];
        if (!currentPt->IsValid)
        {
          memcpy(pt, cellOrigin, 3 * sizeof(double));
          if (offset)
          {
            pt[orientation] += cellSize[orientation];
          }
          unsigned int axis2 = (orientation + 2) % 3;
          pt[axis2] += cellSize[axis2];
          this->SetXYZ(*currentPt, pt);
        }
      }
      else
      {
        memcpy(pt, cellOrigin, 3 * sizeof(double));
        if (offset)
        {
          pt[orientation] += cellSize[orientation];
        }
        unsigned int axis1 = (orientation + 1) % 3;
        unsigned int axis2 = (orientation + 2) % 3;
        pt[axis1] += cellSize[axis1];
        pt[axis2] += cellSize[axis2];
        this->SetXYZ(*currentPt, pt);
        currentPt = &cellPoints[::FACE_PTS_IDS[faceId][3]];
        if (!currentPt->IsValid)
        {
          pt[axis1] = cellOrigin[axis1];
          this->SetXYZ(*currentPt, pt);
        }
      }
    }
    else
    {
      memcpy(pt, cellOrigin, 3 * sizeof(double));
      if (offset)
      {
        pt[orientation] += cellSize[orientation];
      }
      unsigned int axis1 = (orientation + 1) % 3;
      pt[axis1] += cellSize[axis1];
      this->SetXYZ(*currentPt, pt);
      currentPt = &cellPoints[::FACE_PTS_IDS[faceId][2]];
      if (currentPt->IsValid)
      {
        currentPt = &cellPoints[::FACE_PTS_IDS[faceId][3]];
        if (!currentPt->IsValid)
        {
          unsigned int axis2 = (orientation + 2) % 3;
          pt[axis2] += cellSize[axis2];
          this->SetXYZ(*currentPt, pt);
        }
      }
      else
      {
        unsigned int axis2 = (orientation + 2) % 3;
        pt[axis2] += cellSize[axis2];
        this->SetXYZ(*currentPt, pt);
        currentPt = &cellPoints[::FACE_PTS_IDS[faceId][3]];
        if (!currentPt->IsValid)
        {
          pt[axis1] = cellOrigin[axis1];
          this->SetXYZ(*currentPt, pt);
        }
      }
    }
  }
  else
  {
    memcpy(pt, cellOrigin, 3 * sizeof(double));
    if (offset)
    {
      pt[orientation] += cellSize[orientation];
    }
    this->SetXYZ(*currentPt, pt);
    currentPt = &cellPoints[::FACE_PTS_IDS[faceId][1]];
    if (currentPt->IsValid)
    {
      currentPt = &cellPoints[::FACE_PTS_IDS[faceId][2]];
      if (currentPt->IsValid)
      {
        currentPt = &cellPoints[::FACE_PTS_IDS[faceId][3]];
        if (!currentPt->IsValid)
        {
          unsigned int axis2 = (orientation + 2) % 3;
          pt[axis2] += cellSize[axis2];
          this->SetXYZ(*currentPt, pt);
        }
      }
      else
      {
        unsigned int axis1 = (orientation + 1) % 3;
        unsigned int axis2 = (orientation + 2) % 3;
        pt[axis1] += cellSize[axis1];
        pt[axis2] += cellSize[axis2];
        this->SetXYZ(*currentPt, pt);
        currentPt = &cellPoints[::FACE_PTS_IDS[faceId][3]];
        if (!currentPt->IsValid)
        {
          pt[axis1] = cellOrigin[axis1];
          this->SetXYZ(*currentPt, pt);
        }
      }
    }
    else
    {
      unsigned int axis1 = (orientation + 1) % 3;
      pt[axis1] += cellSize[axis1];
      this->SetXYZ(*currentPt, pt);
      currentPt = &cellPoints[::FACE_PTS_IDS[faceId][2]];
      if (currentPt->IsValid)
      {
        currentPt = &cellPoints[::FACE_PTS_IDS[faceId][3]];
        if (!currentPt->IsValid)
        {
          unsigned int axis2 = (orientation + 2) % 3;
          pt[axis2] += cellSize[axis2];
          this->SetXYZ(*currentPt, pt);
        }
      }
      else
      {
        unsigned int axis2 = (orientation + 2) % 3;
        pt[axis2] += cellSize[axis2];
        this->SetXYZ(*currentPt, pt);
        currentPt = &cellPoints[::FACE_PTS_IDS[faceId][3]];
        if (!currentPt->IsValid)
        {
          pt[axis1] = cellOrigin[axis1];
          this->SetXYZ(*currentPt, pt);
        }
      }
    }
  }

  // Storage for new face vertex IDs
  std::vector<vtkIdType> outputIndexPoints;

  unsigned int currentEdgePointA = VTK_DEFAULT_EDGE_INDEX;
  vtkIdType lastId = -1;
  unsigned int currentEdgePointB = VTK_DEFAULT_EDGE_INDEX;

  // Iterate over the edges of the current face to add face points.
  // If there is no interface, simply insert the 4 points of the face.
  // If one or two interfaces pass through the cell, also compute the
  // additional points of the interface (intersection between the interface and
  // the edges of the cell).
  for (unsigned int edgeId = 0; edgeId < 4; ++edgeId)
  {
    unsigned int faceEdgeId = ::FACE_EDGES_IDS[faceId][edgeId];
    std::pair<unsigned int, unsigned int> edgePointId = ::EDGE_PTS_IDS[faceEdgeId];
    this->ComputeEdge(cellPoints[edgePointId.first], cellPoints[edgePointId.second], edgePoints,
      ::EDGE_AXIS[faceEdgeId], faceEdgeId, internalFaceA, internalFaceB, currentEdgePointA,
      currentEdgePointB);

    // The order of points insertion is important in order to considerate all face points.
    // Regarding the way IDs are stored in FACE_PTS_IDS, FACE_EDGES_IDS and EDGE_PTS_IDS,
    // we have to retrieve the first point of the edge for the 1st and 2nd edges,
    // and the second point for the 3rd and 4th edges of the face.
    std::vector<HTG3DPoint*> points;
    if (edgeId < 2)
    {
      points.emplace_back(&cellPoints[edgePointId.first]); // first vertex quad face
      points.emplace_back(&edgePoints[faceEdgeId].first);  // first point one edge
      points.emplace_back(&edgePoints[faceEdgeId].second); // second point one edge
    }
    else
    {
      points.emplace_back(&cellPoints[edgePointId.second]); // second vertex quad face
      points.emplace_back(&edgePoints[faceEdgeId].second);  // second point one edge
      points.emplace_back(&edgePoints[faceEdgeId].first);   // first point one edge
    }
    for (auto point : points)
    {
      if (point->IsValid)
      {
        vtkIdType pointId = -1;
        if (this->IsInside(*point))
        {
          pointId = this->InsertUniquePoint(*point);
        }
        if (pointId >= 0 && pointId != lastId) // lastId is used to avoid repetitions
        {
          outputIndexPoints.emplace_back(pointId);
          lastId = pointId;
        }
      }
    }
  }

  // Insert a new face
  if (outputIndexPoints.size() > 2)
  {
    this->CreateNewCellAndCopyData(outputIndexPoints, cellId);
  }
}

//------------------------------------------------------------------------------
void vtkHyperTreeGridGeometry3DImpl::SetInterfaceFace(unsigned int edgeId,
  std::map<unsigned int, std::pair<vtkHyperTreeGridGeometry3DImpl::HTG3DPoint*, unsigned int>>&
    internalFace,
  vtkHyperTreeGridGeometry3DImpl::HTG3DPoint* point)
{
  if (internalFace.count(edgeId) <= 0)
  {
    internalFace[edgeId].first = point;
    internalFace[edgeId].second = VTK_DEFAULT_EDGE_INDEX;
  }
}

//------------------------------------------------------------------------------
void vtkHyperTreeGridGeometry3DImpl::CompleteLinkage(
  std::map<unsigned int, std::pair<vtkHyperTreeGridGeometry3DImpl::HTG3DPoint*, unsigned int>>&
    internalFace,
  unsigned int edgePointId1, unsigned int edgePointId2)
{
  if (edgePointId1 == VTK_DEFAULT_EDGE_INDEX || edgePointId2 == VTK_DEFAULT_EDGE_INDEX)
  {
    // XXX: this seems to happen quite often.
    // We must clarify if it is intended
    return;
  }
  if (edgePointId1 == edgePointId2)
  {
    vtkErrorWithObjectMacro(nullptr, "Edge with 2 identical points found !");
    return;
  }

  // Build link between edgePointId1 et edgePointId2
  // XXX: need to clarify naming for nextEdgePointId1 and nextEdgePointId2
  unsigned int nextEdgePointId1 = internalFace[edgePointId1].second;
  unsigned int nextEdgePointId2 = internalFace[edgePointId2].second;
  if (nextEdgePointId1 == VTK_DEFAULT_EDGE_INDEX)
  {
    if (nextEdgePointId2 == VTK_DEFAULT_EDGE_INDEX || nextEdgePointId2 != edgePointId1)
    {
      // Arbitrary choice of linking direction,
      // or nextEdgePointId2 already describes a different linking
      internalFace[edgePointId1].second = edgePointId2;
    }
  }
  else if (nextEdgePointId2 == VTK_DEFAULT_EDGE_INDEX)
  {
    // Arbitrary choice of linking direction
    internalFace[edgePointId2].second = edgePointId1;
  }
  else if (nextEdgePointId2 != edgePointId1)
  {
    // If nextEdgePointId1 is involved in a different linkage, this is also the case for
    // nextEdgePointId2 We have to invert both linkages and connect them together
    std::vector<unsigned int> chainette;
    chainette.emplace_back(edgePointId1);
    unsigned int next = internalFace[edgePointId1].second;
    while (next != VTK_DEFAULT_EDGE_INDEX)
    {
      // XXX: think adding an "emergency" breaking condition to
      // avoid potential infinite looping
      chainette.emplace_back(next);
      next = internalFace[next].second;
    }
    unsigned int current = VTK_DEFAULT_EDGE_INDEX;
    for (std::vector<unsigned int>::reverse_iterator it = chainette.rbegin();
         it != chainette.rend(); ++it)
    {
      if (current == VTK_DEFAULT_EDGE_INDEX)
      {
        current = *it;
      }
      else
      {
        next = *it;
        internalFace[current].second = next;
        current = next;
      }
    }
    if (current != edgePointId1)
    {
      vtkWarningWithObjectMacro(nullptr,
        << "Unexpected edge encountered. Expected " << edgePointId1 << ", got " << current << ".");
    }
    internalFace[current].second = edgePointId2;
  }
}

//------------------------------------------------------------------------------
bool vtkHyperTreeGridGeometry3DImpl::ComputeEdgeInterface(const HTG3DPoint& firstPoint,
  const HTG3DPoint& secondPoint, std::vector<std::pair<HTG3DPoint, HTG3DPoint>>& edgePoints,
  unsigned int edgeAxis, unsigned int edgeId,
  std::map<unsigned int, std::pair<HTG3DPoint*, unsigned int>>& internalFace,
  HTG3DPoint& pointInter, unsigned int& edgePointId, bool isInterfaceA)
{
  if (!firstPoint.IsValid)
  {
    vtkWarningWithObjectMacro(nullptr, "First edge point is invalid.");
  }
  if (!secondPoint.IsValid)
  {
    vtkWarningWithObjectMacro(nullptr, "Second edge point is invalid.");
  }

  auto firstPointDist =
    (isInterfaceA ? firstPoint.DistanceToInterfaceA : firstPoint.DistanceToInterfaceB);
  auto secondPointDist =
    (isInterfaceA ? secondPoint.DistanceToInterfaceA : secondPoint.DistanceToInterfaceB);

  if (firstPointDist == 0.)
  {
    if (secondPointDist == 0.)
    {
      // Edge case : the interface corresponds to the edge
      // XXX: need to clarify naming for iEdgePoint1 and iEdgePoint2
      unsigned int iEdgePoint1 = ::EDGE_PTS_IDS[edgeId].first + NUMBER_OF_EDGES;
      edgePoints[iEdgePoint1].first = firstPoint;
      edgePoints[iEdgePoint1].second.IsValid = false;
      this->SetInterfaceFace(iEdgePoint1, internalFace, &edgePoints[iEdgePoint1].first);

      unsigned int iEdgePoint2 = ::EDGE_PTS_IDS[edgeId].second + NUMBER_OF_EDGES;
      edgePoints[iEdgePoint2].first = secondPoint;
      edgePoints[iEdgePoint2].second.IsValid = false;
      this->SetInterfaceFace(iEdgePoint2, internalFace, &edgePoints[iEdgePoint2].first);
      this->CompleteLinkage(internalFace, iEdgePoint1, iEdgePoint2);

      return true;
    }

    // The interface point is a the first point of the cell
    pointInter = firstPoint;
    edgePointId = ::EDGE_PTS_IDS[edgeId].first + NUMBER_OF_EDGES;
  }
  else if (secondPointDist == 0.)
  {
    // The interface point is a the second point of the cell
    pointInter = secondPoint;
    edgePointId = ::EDGE_PTS_IDS[edgeId].second + NUMBER_OF_EDGES;
  }
  else if (firstPointDist * secondPointDist < 0.)
  {
    // Compute the position of the interface point on the edge,
    // between the first and the second point
    double xyz[3] = { 0., 0., 0. };
    memcpy(xyz, firstPoint.Coords, 3 * sizeof(double));
    xyz[edgeAxis] = (secondPointDist * firstPoint.Coords[edgeAxis] -
                      firstPointDist * secondPoint.Coords[edgeAxis]) /
      (secondPointDist - firstPointDist);
    this->SetIntersectXYZ(pointInter, xyz, isInterfaceA);

    if (pointInter.Coords[edgeAxis] == firstPoint.Coords[edgeAxis] ||
      pointInter.Coords[edgeAxis] == secondPoint.Coords[edgeAxis])
    {
      vtkWarningWithObjectMacro(nullptr, "Interface point coincide with an edge point");
      pointInter.IsValid = false;
    }

    edgePointId = edgeId;
  }
  return false;
}

//------------------------------------------------------------------------------
void vtkHyperTreeGridGeometry3DImpl::ComputeEdge(const HTG3DPoint& firstPoint,
  const HTG3DPoint& secondPoint, std::vector<std::pair<HTG3DPoint, HTG3DPoint>>& edgePoints,
  unsigned int edgeAxis, unsigned int edgeId,
  std::map<unsigned int, std::pair<HTG3DPoint*, unsigned int>>& internalFaceA,
  std::map<unsigned int, std::pair<HTG3DPoint*, unsigned int>>& internalFaceB,
  unsigned int& currentEdgePointA, unsigned int& currentEdgePointB)
{
  HTG3DPoint pointA, pointB;
  unsigned int iEdgePointA = edgeId, iEdgePointB = edgeId;

  // Compute the intersection point for the first interface, if any.
  if (firstPoint.HasInterfaceA &&
    this->ComputeEdgeInterface(firstPoint, secondPoint, edgePoints, edgeAxis, edgeId, internalFaceA,
      pointA, iEdgePointA, true))
  {
    return;
  }

  // Compute the intersection point for the second interface, if any.
  if (firstPoint.HasInterfaceB &&
    this->ComputeEdgeInterface(firstPoint, secondPoint, edgePoints, edgeAxis, edgeId, internalFaceB,
      pointB, iEdgePointB, false))
  {
    return;
  }

  // If intersection points are computed, add them to the construction (linkage)
  // of the interface faces
  if (pointA.IsValid)
  {
    if (pointB.IsValid)
    {
      if (pointA.Coords[edgeAxis] < pointB.Coords[edgeAxis])
      {
        if (edgeId == iEdgePointA && edgeId == iEdgePointB)
        {
          edgePoints[edgeId].first = pointA;
          this->SetInterfaceFace(edgeId, internalFaceA, &edgePoints[edgeId].first);

          this->CompleteLinkage(internalFaceA, currentEdgePointA, edgeId);
          currentEdgePointA = edgeId;

          edgePoints[edgeId].second = pointB;
          this->SetInterfaceFace(edgeId, internalFaceB, &edgePoints[edgeId].second);

          this->CompleteLinkage(internalFaceB, currentEdgePointB, edgeId);
          currentEdgePointB = edgeId;
        }
        else
        {
          edgePoints[iEdgePointA].first = pointA;
          this->SetInterfaceFace(iEdgePointA, internalFaceA, &edgePoints[iEdgePointA].first);

          this->CompleteLinkage(internalFaceA, currentEdgePointA, iEdgePointA);
          currentEdgePointA = iEdgePointA;

          edgePoints[iEdgePointB].second = pointB;
          this->SetInterfaceFace(iEdgePointB, internalFaceB, &edgePoints[iEdgePointB].second);

          this->CompleteLinkage(internalFaceB, currentEdgePointB, iEdgePointB);
          currentEdgePointB = iEdgePointB;
        }
      }
      if (pointA.Coords[edgeAxis] > pointB.Coords[edgeAxis])
      {
        if (edgeId == iEdgePointA && edgeId == iEdgePointB)
        {
          edgePoints[edgeId].first = pointB;
          this->SetInterfaceFace(edgeId, internalFaceB, &edgePoints[edgeId].first);

          this->CompleteLinkage(internalFaceB, currentEdgePointB, edgeId);
          currentEdgePointB = edgeId;

          edgePoints[edgeId].second = pointA;
          this->SetInterfaceFace(edgeId, internalFaceA, &edgePoints[edgeId].second);

          this->CompleteLinkage(internalFaceA, currentEdgePointA, edgeId);
          currentEdgePointA = edgeId;
        }
        else
        {
          edgePoints[iEdgePointA].first = pointA;
          edgePoints[iEdgePointA].second.IsValid = false;
          this->SetInterfaceFace(iEdgePointA, internalFaceA, &edgePoints[iEdgePointA].first);

          this->CompleteLinkage(internalFaceA, currentEdgePointA, iEdgePointA);
          currentEdgePointA = iEdgePointA;

          edgePoints[iEdgePointB].second = pointB;
          edgePoints[iEdgePointB].second.IsValid = false;
          this->SetInterfaceFace(iEdgePointB, internalFaceB, &edgePoints[iEdgePointB].second);

          this->CompleteLinkage(internalFaceB, currentEdgePointB, iEdgePointB);
          currentEdgePointB = iEdgePointB;
        }
      }
    }
    else
    {
      edgePoints[iEdgePointA].first = pointA;
      this->SetInterfaceFace(iEdgePointA, internalFaceA, &edgePoints[iEdgePointA].first);

      this->CompleteLinkage(internalFaceA, currentEdgePointA, iEdgePointA);
      currentEdgePointA = iEdgePointA;
    }
  }
  else if (pointB.IsValid)
  {
    edgePoints[iEdgePointB].first = pointB;
    this->SetInterfaceFace(iEdgePointB, internalFaceB, &edgePoints[iEdgePointB].first);

    this->CompleteLinkage(internalFaceB, currentEdgePointB, iEdgePointB);
    currentEdgePointB = iEdgePointB;
  }
}

//------------------------------------------------------------------------------
bool vtkHyperTreeGridGeometry3DImpl::IsInside(const HTG3DPoint& point)
{
  if (!point.IsValid)
  {
    return false;
  }
  if (this->CellInterfaceType == -1)
  {
    if (point.HasInterfaceA)
    {
      if (point.DistanceToInterfaceA < 0)
      {
        return false;
      }
    }
    return true;
  }
  else if (this->CellInterfaceType == 0)
  {
    if (point.DistanceToInterfaceA > 0)
    {
      return false;
    }
    if (point.DistanceToInterfaceB < 0)
    {
      return false;
    }
    return true;
  }
  else if (this->CellInterfaceType == 1)
  {
    if (point.HasInterfaceB)
    {
      if (point.DistanceToInterfaceB > 0)
      {
        return false;
      }
    }
    return true;
  }
  else
  {
    // Pure cell
    return true;
  }
}

//------------------------------------------------------------------------------
void vtkHyperTreeGridGeometry3DImpl::SetXYZ(HTG3DPoint& point, const double* coords)
{
  point.Coords[0] = coords[0];
  point.Coords[1] = coords[1];
  point.Coords[2] = coords[2];

  point.Id = -1;
  if (this->HasInterfaceOnThisCell)
  {
    if (this->CellInterfaceType != 1.)
    {
      point.HasInterfaceA = true;
      point.DistanceToInterfaceA = this->ComputeDistanceToInterfaceA(point.Coords);
    }
    if (this->CellInterfaceType != -1.)
    {
      point.HasInterfaceB = true;
      point.DistanceToInterfaceB = this->ComputeDistanceToInterfaceB(point.Coords);
    }
  }
  point.IsValid = true;
}

//------------------------------------------------------------------------------
void vtkHyperTreeGridGeometry3DImpl::SetIntersectXYZ(
  HTG3DPoint& point, const double* coords, bool isInterfaceA)
{
  point.Coords[0] = coords[0];
  point.Coords[1] = coords[1];
  point.Coords[2] = coords[2];
  point.Id = -1;
  if (isInterfaceA)
  {
    point.HasInterfaceA = true;
    point.DistanceToInterfaceA = 0.;
    if (this->HasInterfaceOnThisCell && this->CellInterfaceType != -1.)
    {
      point.HasInterfaceB = true;
      point.DistanceToInterfaceB = this->ComputeDistanceToInterfaceB(point.Coords);
    }
    else
    {
      point.HasInterfaceB = false;
    }
  }
  else
  {
    point.HasInterfaceB = true;
    point.DistanceToInterfaceB = 0.;
    if (this->HasInterfaceOnThisCell && this->CellInterfaceType != 1.)
    {
      point.HasInterfaceA = true;
      point.DistanceToInterfaceA = this->ComputeDistanceToInterfaceA(point.Coords); // 1 is A
    }
  }
  point.IsValid = true;
}

//------------------------------------------------------------------------------
vtkIdType vtkHyperTreeGridGeometry3DImpl::InsertUniquePoint(HTG3DPoint& point)
{
  if (point.IsValid && point.Id < 0)
  {
    // Insert a point
    if (this->Locator)
    {
      this->Locator->InsertUniquePoint(point.Coords, point.Id);
    }
    else
    {
      point.Id = this->OutPoints->InsertNextPoint(point.Coords);
    }
  }
  return point.Id;
}

//----------------------------------------------------------------------------------------------
bool vtkHyperTreeGridGeometry3DImpl::GetHasInterface(vtkIdType cellId) const
{
  // Only useful in 3D, this method makes it possible to know if the neighboring cell
  // of _inputCellIndex offset is pure or describes an interface.
  // It is pure if:
  // - there is no defined interface (m_hasInterface);
  // - there is no description of the interfaces (m_inputIntercepts);
  // - there is a description of the interfaces but the mixed cell type is not 2
  //   (pure cell) (m_inputIntercepts[2]); -1 and 1 describes a case of a mixed
  //   cell of a material with a single interface, 0 a case of a mixed cell of a
  //   material with a double interface;
  // - there is no description of the normals (m_inputNormals);
  // - there is a description of the normals but not zero.
  if (cellId < 0)
  {
    return false;
  }
  if (!this->HasInterface)
  {
    return false;
  }

  bool ret =
    (this->InIntercepts && ((this->InIntercepts->GetTuple(cellId))[2] < 2) && this->InNormals);
  if (ret)
  {
    double* normal = this->InNormals->GetTuple(cellId);
    ret = !(normal[0] == 0. && normal[1] == 0. && normal[2] == 0.);
  }
  return ret;
}

VTK_ABI_NAMESPACE_END