vtkHyperTreeGridGeometry.cxx 27.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkHyperTreeGridGeometry.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkHyperTreeGridGeometry.h"

17
#include "vtkBitArray.h"
18
19
#include "vtkCellArray.h"
#include "vtkCellData.h"
20
#include "vtkDataSetAttributes.h"
21
#include "vtkDoubleArray.h"
22
#include "vtkExtentTranslator.h"
23
#include "vtkHyperTreeGrid.h"
24
#include "vtkHyperTreeGridCursor.h"
25
26
27
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
28
#include "vtkMath.h"
29
#include "vtkPointData.h"
30
#include "vtkPolyData.h"
31
#include "vtkStreamingDemandDrivenPipeline.h"
32

33
34
35
static const unsigned int VonNeumannCursors3D[] = { 0, 1, 2, 4, 5, 6 };
static const unsigned int VonNeumannOrientations3D[]  = { 2, 1, 0, 0, 1, 2 };
static const unsigned int VonNeumannOffsets3D[]  = { 0, 0, 0, 1, 1, 1 };
36
37
38
39
40
41
42
43
44
45
46
static const unsigned int EdgeIndices[3][2][4] = {
  {
    { 3,11,7,8 }, { 1,10,5,9 }
  },
  {
    { 0,9,4,8 }, { 2,10,6,11 }
  },
  {
    { 0,1,2,3 }, { 4,5,6,7 }
  }
};
47

48
49
vtkStandardNewMacro(vtkHyperTreeGridGeometry);

50
//-----------------------------------------------------------------------------
51
52
vtkHyperTreeGridGeometry::vtkHyperTreeGridGeometry()
{
53
54
55
56
57
  // Create storage for corners of leaf cells
  this->Points = vtkPoints::New();

  // Create storage for untructured leaf cells
  this->Cells = vtkCellArray::New();
58

59
60
  // Default dimension is 0
  this->Dimension = 0;
61

62
63
  // Default orientation is 0
  this->Orientation = 0;
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

  // Default interface values
  this->HasInterface = false;
  this->Normals = nullptr;
  this->Intercepts = nullptr;
  this->FaceIDs = vtkIdList::New();
  this->FacePoints = vtkPoints::New();
  this->FacePoints->SetNumberOfPoints( 4 );
  this->FacesA = vtkIdTypeArray::New();
  this->FacesA->SetNumberOfComponents( 2 );
  this->FacesB = vtkIdTypeArray::New();
  this->FacesB->SetNumberOfComponents( 2 );
  this->FaceScalarsA = vtkDoubleArray::New();
  this->FaceScalarsA->SetNumberOfTuples( 4 );
  this->FaceScalarsB = vtkDoubleArray::New();
  this->FaceScalarsB->SetNumberOfTuples( 4 );
80
81
}

82
83
84
//-----------------------------------------------------------------------------
vtkHyperTreeGridGeometry::~vtkHyperTreeGridGeometry()
{
85
86
87
  if ( this->Points )
  {
    this->Points->Delete();
88
    this->Points = nullptr;
89
90
91
92
93
  }

  if ( this->Cells )
  {
    this->Cells->Delete();
94
    this->Cells = nullptr;
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

  if ( this->FacePoints )
  {
    this->FacePoints->Delete();
    this->FacePoints = nullptr;
  }

  if ( this->FaceIDs )
  {
    this->FaceIDs->Delete();
    this->FaceIDs = nullptr;
  }

  if ( this->FacesA )
  {
    this->FacesA->Delete();
    this->FacesA = nullptr;
  }

  if ( this->FacesB )
  {
    this->FacesB->Delete();
    this->FacesB = nullptr;
  }

  if ( this->FaceScalarsA )
  {
    this->FaceScalarsA->Delete();
    this->FaceScalarsA = nullptr;
  }

  if ( this->FaceScalarsB )
  {
    this->FaceScalarsB->Delete();
    this->FaceScalarsB = nullptr;
  }

133
134
}

135
//----------------------------------------------------------------------------
136
void vtkHyperTreeGridGeometry::PrintSelf( ostream& os, vtkIndent indent )
137
138
{
  this->Superclass::PrintSelf( os, indent );
139

140
141
  os << indent << "Dimension: " << this->Dimension << endl;
  os << indent << "Orientation: " << this->Orientation << endl;
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
  if( this->Points )
  {
    os << indent << "Points:\n";
    this->Points->PrintSelf( os, indent.GetNextIndent() );
  }
  else
  {
    os << indent << "Points: ( none )\n";
  }
  if( this->Cells )
  {
    os << indent << "Cells:\n";
    this->Cells->PrintSelf( os, indent.GetNextIndent() );
  }
  else
  {
    os << indent << "Cells: ( none )\n";
  }
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
  os << indent << "HasInterface: " << this->HasInterface << endl;
  if( this->Normals )
  {
    os << indent << ":\n";
    this->Normals->PrintSelf( os, indent.GetNextIndent() );
  }
  else
  {
    os << indent << "Normals: ( none )\n";
  }
  if( this->Intercepts )
  {
    os << indent << ":\n";
    this->Intercepts->PrintSelf( os, indent.GetNextIndent() );
  }
  else
  {
    os << indent << "Intercepts: ( none )\n";
  }
  if( this->FacePoints )
  {
    os << indent << ":\n";
    this->FacePoints->PrintSelf( os, indent.GetNextIndent() );
  }
  else
  {
    os << indent << "FacePoints: ( none )\n";
  }
  if( this->FaceIDs )
  {
    os << indent << ":\n";
    this->FaceIDs->PrintSelf( os, indent.GetNextIndent() );
  }
  else
  {
    os << indent << "FaceIDs: ( none )\n";
  }
  os << indent << "EdgesA:";
  for ( unsigned int i = 0; i < 12; ++ i )
  {
    os << " " << EdgesA[i];
  }
  os << endl;
  os << indent << "EdgesB:";
  for ( unsigned int i = 0; i < 12; ++ i )
  {
    os << " " << EdgesB[i];
  }
  os << endl;
  if( this->FacesA )
  {
    os << indent << ":\n";
    this->FacesA->PrintSelf( os, indent.GetNextIndent() );
  }
  else
  {
    os << indent << "FacesA: ( none )\n";
  }
  if( this->FacesB )
  {
    os << indent << ":\n";
    this->FacesB->PrintSelf( os, indent.GetNextIndent() );
  }
  else
  {
    os << indent << "FacesB: ( none )\n";
  }
  if( this->FaceScalarsA )
  {
    os << indent << ":\n";
    this->FaceScalarsA->PrintSelf( os, indent.GetNextIndent() );
  }
  else
  {
    os << indent << "FaceScalarsA: ( none )\n";
  }
  if( this->FaceScalarsB )
  {
    os << indent << ":\n";
    this->FaceScalarsB->PrintSelf( os, indent.GetNextIndent() );
  }
  else
  {
    os << indent << "FaceScalarsB: ( none )\n";
  }
245
246
}

247
248
249
//----------------------------------------------------------------------------
int vtkHyperTreeGridGeometry::FillOutputPortInformation( int,
                                                         vtkInformation* info )
250
{
251
  info->Set( vtkDataObject::DATA_TYPE_NAME(), "vtkPolyData" );
252
253
254
  return 1;
}

255
256
257
//-----------------------------------------------------------------------------
int vtkHyperTreeGridGeometry::ProcessTrees( vtkHyperTreeGrid* input,
                                            vtkDataObject* outputDO )
258
{
259
260
261
262
263
264
265
266
  // Downcast output data object to polygonal data set
  vtkPolyData* output = vtkPolyData::SafeDownCast( outputDO );
  if ( ! output )
  {
    vtkErrorMacro( "Incorrect type of output: "
                   << outputDO->GetClassName() );
    return 0;
  }
267

268
269
270
  // Retrieve useful grid parameters for speed of access
  this->Dimension = input->GetDimension();
  this->Orientation = input->GetOrientation();
271
272

  // Initialize output cell data
273
274
  this->InData = input->GetPointData();
  this->OutData = output->GetCellData();
275
  this->OutData->CopyAllocate( this->InData );
276

277
278
279
  // Retrieve material mask
  vtkBitArray* mask
    = input->HasMaterialMask() ? input->GetMaterialMask() : nullptr;
280

281
282
283
284
285
286
287
288
289
290
  // Retrieve interface data when relevant
  this->HasInterface = input->GetHasInterface();
  if ( this->HasInterface )
  {
    this->Normals
      = vtkDoubleArray::SafeDownCast( this->InData->GetArray( input->GetInterfaceNormalsName() ) );
    this->Intercepts
      = vtkDoubleArray::SafeDownCast( this->InData->GetArray( input->GetInterfaceInterceptsName() ) );
  } // this->HasInterface

291
  // Iterate over all hyper trees
292
  vtkIdType index;
293
294
  vtkHyperTreeGrid::vtkHyperTreeGridIterator it;
  input->InitializeTreeIterator( it );
295
  while ( it.GetNextTree( index ) )
296
  {
297
298
299
300
301
302
    // Initialize new cursor at root of current tree
    vtkHyperTreeGridCursor* cursor;
    if ( this->Dimension == 3 )
    {
      // In 3 dimensions, von Neumann neighborhood information is needed
      cursor = input->NewVonNeumannSuperCursor( index );
303

304
305
306
307
308
309
310
      // Build geometry recursively
      this->RecursivelyProcessTree( cursor, mask );
    } // if ( this->Dimension == 3 )
    else
    {
      // Otherwise, geometric properties of the cells suffice
      cursor = input->NewGeometricCursor( index );
311

312
313
314
315
316
317
      // Build geometry recursively
      this->RecursivelyProcessTree( cursor, mask );
    } // else

    // Clean up
    cursor->Delete();
318
  } // it
319

320
  // Set output geometry and topology
321
322
  output->SetPoints( this->Points );
  if ( this->Dimension == 1  )
323
  {
324
    output->SetLines( this->Cells );
325
  }
326
  else
327
  {
328
    output->SetPolys( this->Cells );
329
  }
330

331
  return 1;
332
333
334
}

//----------------------------------------------------------------------------
335
336
void vtkHyperTreeGridGeometry::RecursivelyProcessTree( vtkHyperTreeGridCursor* cursor,
                                                       vtkBitArray* mask )
337
{
338
339
340
341
342
  // Retrieve input grid
  vtkHyperTreeGrid* input = cursor->GetGrid();

  // Create geometry output if cursor is at leaf
  if ( cursor->IsLeaf() )
343
  {
344
345
    // Cursor is at leaf, process it depending on its dimension
    switch ( this->Dimension )
346
    {
347
      case 1:
348
        this->ProcessLeaf1D( cursor );
349
350
        break;
      case 2:
351
        this->ProcessLeaf2D( cursor, mask );
352
353
        break;
      case 3:
354
        this->ProcessLeaf3D( cursor, mask );
355
        break;
356
357
358
359
      default:
        return;
    } // switch ( this->Dimension )
  } // if ( cursor->IsLeaf() )
360
  else
361
  {
362
363
    // Cursor is not at leaf, recurse to all children
    int numChildren = input->GetNumberOfChildren();
364
    for ( int child = 0; child < numChildren; ++ child )
365
    {
366
367
368
369
370
371
372
373
374
      // Create child cursor from parent
      vtkHyperTreeGridCursor* childCursor = cursor->Clone();
      childCursor->ToChild( child );

      // Recurse
      this->RecursivelyProcessTree( childCursor, mask );

      // Clean up
      childCursor->Delete();
375
      childCursor = nullptr;
376
377
    } // child
  } // else
378
379
}

Philippe Pébay's avatar
Philippe Pébay committed
380
//----------------------------------------------------------------------------
381
void vtkHyperTreeGridGeometry::ProcessLeaf1D( vtkHyperTreeGridCursor* cursor )
382
{
383
384
385
386
387
388
  // In 1D the geometry is composed of edges, create storage for endpoint IDs
  vtkIdType id[2];

  // First endpoint is at origin of cursor
  double* origin = cursor->GetOrigin();
  id[0] = this->Points->InsertNextPoint( origin );
389

390
  // Second endpoint is at origin of cursor plus its length
391
  double pt[3];
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
  memcpy( pt, origin, 3 * sizeof( double ) );
  switch ( this->Orientation )
  {
    case 3: // 1 + 2
      pt[2] += cursor->GetSize()[2];
      break;
    case 5: // 1 + 4
      pt[1] += cursor->GetSize()[1];
      break;
    case 6: // 2 + 4
      pt[0] += cursor->GetSize()[0];
      break;
  } // switch
  id[1] = this->Points->InsertNextPoint( pt );

  // Insert edge into 1D geometry
  this->Cells->InsertNextCell( 2, id );
409
410
}

Philippe Pébay's avatar
Philippe Pébay committed
411
//----------------------------------------------------------------------------
412
413
414
void vtkHyperTreeGridGeometry::ProcessLeaf2D( vtkHyperTreeGridCursor* cursor,
                                              vtkBitArray* mask )

415
{
416
417
418
419
420
421
  // Cell at cursor center is a leaf, retrieve its global index
  vtkIdType id = cursor->GetGlobalNodeIndex();
  if ( id < 0 )
  {
    return;
  }
422

423
424
425
426
427
428
429
430
431
432
  // Reset interface variables if needed
  if ( this->HasInterface )
  {
    size_t int12sz = 12 * sizeof( vtkIdType );
    memset( this->EdgesA, -1,  int12sz );
    memset( this->EdgesB, -1,  int12sz );
    this->FacesA->Reset();
    this->FacesB->Reset();
  } // if ( this->HasInterface )

433
  // In 2D all unmasked faces are generated
434
  if ( ! mask  || ! mask->GetValue( id ) )
435
  {
436
437
    // Insert face into 2D geometry depending on orientation
    this->AddFace( id, cursor->GetOrigin(), cursor->GetSize(), 0, this->Orientation );
438
  }
439
440
}

Philippe Pébay's avatar
Philippe Pébay committed
441
//----------------------------------------------------------------------------
442
443
void vtkHyperTreeGridGeometry::ProcessLeaf3D( vtkHyperTreeGridCursor* superCursor,
                                              vtkBitArray* mask )
444
{
445
446
447
448
449
  // Cell at super cursor center is a leaf, retrieve its global index, level, and mask
  vtkIdType id = superCursor->GetGlobalNodeIndex();
  unsigned level = superCursor->GetLevel();
  int masked = mask ? mask->GetValue( id ) : 0;

450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
  // Default cell type is pure
  double type = 2.;

  // Reset interface variables if needed
  if ( this->HasInterface )
  {
    size_t int12sz = 12 * sizeof( vtkIdType );
    memset( this->EdgesA, -1,  int12sz );
    memset( this->EdgesB, -1,  int12sz );
    this->FacesA->Reset();
    this->FacesB->Reset();

    // Retrieve intercept type
    type = this->Intercepts->GetComponent( id, 2 );
  } // if ( this->HasInterface )

466
  // Iterate over all cursors of Von Neumann neighborhood around center
467
  unsigned int nc = superCursor->GetNumberOfCursors() - 1;
468
  for ( unsigned int c = 0 ; c < nc; ++ c )
469
  {
470
471
472
473
474
475
476
477
478
    // Retrieve cursor to neighbor across face
    vtkHyperTreeGridCursor* cursorN = superCursor->GetCursor( VonNeumannCursors3D[c] );

    // Retrieve tree, leaf flag, and mask of neighbor cursor
    vtkHyperTree* treeN = cursorN->GetTree();
    bool leafN = cursorN->IsLeaf();
    vtkIdType idN = cursorN->GetGlobalNodeIndex();
    int maskedN  = mask ? mask->GetValue( idN ) : 0;

479
480
    // In 3D masked and unmasked cells are handled differently
    if ( masked )
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
      // If cell is masked, and face neighbor exists and is an unmasked leaf,
      // generate face, breaking ties at same level. This ensures that faces
      // between unmasked and masked cells will be generated once and only once.
      if ( treeN && leafN && cursorN->GetLevel() < level && ! maskedN )
      {
        // Generate face with corresponding normal and offset
        this->AddFace( id, superCursor->GetOrigin(), superCursor->GetSize(),
                       VonNeumannOffsets3D[c], VonNeumannOrientations3D[c] );
      }
    } // if ( masked )
    else
    {
      // If cell is unmasked, and face neighbor is a masked leaf, or no
      // such neighbor exists, generate face except in some interface cases.
      bool addFace = ! treeN || ( leafN && maskedN );
      bool create = true;
      if ( ! addFace && type < 2. )
      {
        // Mixed cells must be handled but not created
        create  = false;
        addFace = true;
      }
      if ( ( ! addFace || ! create ) && this->HasInterface && leafN )
      {
        // Face must be created if neighbor is a mixed cell
        create  = this->Intercepts->GetComponent( idN, 2 ) < 2.;
        addFace |= create;
      }
      if ( addFace )
      {
        // Generate or handle face with corresponding normal and offset
        this->AddFace( id, superCursor->GetOrigin(), superCursor->GetSize(),
                       VonNeumannOffsets3D[c], VonNeumannOrientations3D[c],
                       create );
      } // if ( addFace )
    } // else
518
  } // c
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

  // Handle interfaces separately
  if ( this->HasInterface )
  {
    // Create face A when its edges are present
    vtkIdType nA = this->FacesA->GetNumberOfTuples();
    if ( nA > 0 )
    {
      this->FaceIDs->Reset();
      vtkIdType i0 = 0;
      vtkIdType edge0[2];
      this->FacesA->GetTypedTuple( i0, edge0 );
      this->FaceIDs->InsertNextId( this->EdgesA[edge0[1]] );
      while ( edge0[0] != edge0[1] )
      {
        // Iterate over edges of face A
        for ( vtkIdType i = 0; i < nA; ++ i )
        {
          // Seek next edge then break out from loop
          vtkIdType edge[2];
          this->FacesA->GetTypedTuple( i, edge );
          if( i0 != i )
          {
            if ( edge[0] == edge0[1] )
            {
              edge0[1] = edge[1];
              i0 = i;
              break;
            }
            if ( edge[1] == edge0[1] )
            {
              edge0[1] = edge[0];
              i0 = i;
              break;
            }
          } // if ( i != i0 )
        } // nA
        this->FaceIDs->InsertNextId( this->EdgesA[edge0[1]] );
      } // while ( edge0[0] != edge0[1] )

      // Create new face
      vtkIdType outId = this->Cells->InsertNextCell( this->FaceIDs );

      // Copy face data from that of the cell from which it comes
      this->OutData->CopyData( this->InData, id, outId );
    } // if ( nA > 0 )

    // Create face B when its vertices are present
    vtkIdType nB = this->FacesB->GetNumberOfTuples();
    if ( nB > 0 )
    {
      this->FaceIDs->Reset();
      int i0 = 0;
      vtkIdType edge0[2];
      this->FacesB->GetTypedTuple( i0, edge0 );
      this->FaceIDs->InsertNextId( this->EdgesB[edge0[1]] );
      while ( edge0[0] != edge0[1] )
      {
        // Iterate over faces B
        for ( vtkIdType i = 0; i < nB; ++ i )
        {
          // Seek next edge then break out from loop
          vtkIdType edge[2];
          this->FacesB->GetTypedTuple( i, edge );
          if ( i0 != i )
          {
            if ( edge[0] == edge0[1] )
            {
              edge0[1] = edge[1];
              i0 = i;
              break;
            }
            if ( edge[1] == edge0[1] )
            {
              edge0[1] = edge[0];
              i0 = i;
              break;
            }
          } // if ( i0 != i )
        } // nB
        this->FaceIDs->InsertNextId(this->EdgesB[edge0[1]]);
      } // while ( edge0[0] != edge0[1] )

      // Create new face
      vtkIdType outId = this->Cells->InsertNextCell( this->FaceIDs );

      // Copy face data from that of the cell from which it comes
      this->OutData->CopyData( this->InData, id, outId );
    } // if ( nB > 0 )
  } // if ( this->HasInterface )
609
}
610
611
612

//----------------------------------------------------------------------------
void vtkHyperTreeGridGeometry::AddFace( vtkIdType inId,
613
614
615
                                        double* origin,
                                        double* size,
                                        int offset,
616
617
                                        unsigned int orientation,
                                        bool create )
618
{
619
  // First cell vertex is always at origin of cursor
620
  double pt[3];
621
  memcpy( pt, origin, 3 * sizeof( double ) );
622
  if ( offset )
623
  {
624
    // Offset point coordinate as needed
625
    pt[orientation] += size[orientation];
626
  }
627

628
629
630
631
632
  // Storage for face vertex IDs
  vtkIdType ids[4];
  unsigned int nPts = 4;

  // Keep track of face axes
633
634
  unsigned int axis1 = orientation ? 0 : 1;
  unsigned int axis2 = orientation == 2 ? 1 : 2;
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

  if ( this->HasInterface )
  {
    // Retrieve intercept tuple and type
    double* inter = this->Intercepts->GetTuple( inId );
    double type = inter[2];

    // Distinguish cases depending on intercept type
    if ( type < 2 )
    {
      // Create interface intersection points
      this->FacePoints->SetPoint( 0, pt );
      pt[axis1] += size[axis1];
      this->FacePoints->SetPoint( 1, pt );
      pt[axis2] += size[axis2];
      this->FacePoints->SetPoint( 2, pt );
      pt[axis1] = origin[axis1];
      this->FacePoints->SetPoint( 3, pt );

      // Create interface intersection faces
      double coordsA[3];
      double* normal = this->Normals->GetTuple( inId );
      for ( vtkIdType pId = 0; pId < 4; ++ pId )
      {
        // Retrieve vertex coordinates
        this->FacePoints->GetPoint( pId, coordsA );

        // Set face scalars
        if ( type != 1. )
        {
          double val = inter[0]
            + normal[0] * coordsA[0]
            + normal[1] * coordsA[1]
            + normal[2] * coordsA[2];
          this->FaceScalarsA->SetTuple1( pId, val );
        } // if ( type != 1. )
        if ( type != -1. )
        {
          double val = inter[1]
            + normal[0] * coordsA[0]
            + normal[1] * coordsA[1]
            + normal[2] * coordsA[2];
          this->FaceScalarsB->SetTuple1( pId, val );
        } // if ( type != -1. )
      } // p

      // Storage for points
      double coordsB[3];
      double coordsC[3];
      nPts = 0;

      // Distinguish between relevant types
      if ( type == 1 )
      {
        // Take negative values in B
        vtkIdType pair[2];
        unsigned int indPair = 0;

        // Loop over face vertices
        for( int p = 0; p < 4; ++ p )
        {
          // Retrieve vertex coordinates
          this->FacePoints->GetPoint( p, coordsA );

          // Retrieve vertex scalars
          double A = this->FaceScalarsB->GetTuple1( p );
          double B = this->FaceScalarsB->GetTuple1( ( p + 1 ) % 4 );

          // Add point when necessary
          if ( create && A <= 0. )
          {
            ids[nPts] = this->Points->InsertNextPoint( coordsA );
            ++ nPts;
          }
          if ( A * B < 0 )
          {
            unsigned int i = EdgeIndices[orientation][offset][p];
            if ( this->EdgesB[i] == -1 )
            {
              //Compute barycenter of A and B
              this->FacePoints->GetPoint( ( p + 1 ) % 4, coordsB );
              for( int j = 0; j < 3 ;++ j )
              {
                coordsC[j] = ( B * coordsA[j] - A * coordsB[j] ) / ( B - A );
              }
              this->EdgesB[i] = this->Points->InsertNextPoint( coordsC );
            } // if ( this->EdgesB[i] == -1 )

            // Update points
            ids[nPts] = this->EdgesB[i];
            ++ nPts;
            if( indPair )
            {
              pair[1] = i;
            }
            else
            {
              pair[0] = i;
            };
            ++ indPair;
          } // if ( A * B < 0 )
        } // p

        // Insert pair only if it makes sense
        if ( indPair == 2 )
        {
          this->FacesB->InsertNextTypedTuple( pair );
        }
      } // if (type = 1 )

      else if( ! type )
      {
        // Take positive values in A
        vtkIdType pairA[2];
        unsigned int indPairA = 0;

        // Take negative values in B
        vtkIdType pairB[2];
        unsigned int indPairB = 0;

        // Loop over face vertices
        for( int p = 0; p < 4; ++ p )
        {
          // Retrieve vertex coordinates
          this->FacePoints->GetPoint( p, coordsA );

          // Retrieve vertex scalars
          double A1 = this->FaceScalarsA->GetTuple1( p );
          double B1 = this->FaceScalarsA->GetTuple1( ( p + 1 ) % 4 );
          double A2 = this->FaceScalarsB->GetTuple1( p );
          double B2 = this->FaceScalarsB->GetTuple1( ( p + 1 ) % 4 );

          // Add point when necessary
          if ( create && A1 >= 0. && A2 <= 0. )
          {
            ids[nPts] = this->Points->InsertNextPoint( coordsA );
            ++ nPts;
          }
          if ( A1 < 0. && A1 * B1 < 0. )
          {
            unsigned int i = EdgeIndices[orientation][offset][p];
            if ( this->EdgesA[i] == -1 )
            {
              //Compute barycenter of A and B
              this->FacePoints->GetPoint( ( p + 1 ) % 4, coordsB );
              for( int j = 0; j < 3 ;++ j )
              {
                coordsC[j] = ( B1 * coordsA[j] - A1 * coordsB[j] ) / ( B1 - A1 );
              }
              this->EdgesA[i] = this->Points->InsertNextPoint( coordsC );
            } // if ( this->EdgesB[i] == -1 )

            // Update points
            ids[nPts] = this->EdgesA[i];
            ++ nPts;
            if( indPairA )
            {
              pairA[1] = i;
            }
            else
            {
              pairA[0] = i;
            };
            ++ indPairA;
          } // if ( A1 < 0. && A1 * B1 < 0. )
          if ( A2* B2 < 0. )
          {
            unsigned int i = EdgeIndices[orientation][offset][p];
            if ( this->EdgesA[i] == -1 )
            {
              //Compute barycenter of A and B
              this->FacePoints->GetPoint( ( p + 1 ) % 4, coordsB );
              for( int j = 0; j < 3 ;++ j )
              {
                coordsC[j] = ( B2 * coordsA[j] - A2 * coordsB[j] ) / ( B2 - A2 );
              }
              this->EdgesB[i] = this->Points->InsertNextPoint( coordsC );
            } // if ( this->EdgesA[i] == -1 )

            // Update points
            ids[nPts] = this->EdgesB[i];
            ++ nPts;
            if( indPairB )
            {
              pairB[1] = i;
            }
            else
            {
              pairB[0] = i;
            };
            ++ indPairB;
          } // if ( A2 * B2 < 0. )
          if ( A1 > 0. && A1 * B1 < 0. )
          {
            unsigned int i = EdgeIndices[orientation][offset][p];
            if ( this->EdgesA[i] == -1 )
            {
              //Compute barycenter of A and B
              this->FacePoints->GetPoint( ( p + 1 ) % 4, coordsB );
              for( int j = 0; j < 3 ;++ j )
              {
                coordsC[j] = ( B1 * coordsA[j] - A1 * coordsB[j] ) / ( B1 - A1 );
              }
              this->EdgesA[i] = this->Points->InsertNextPoint( coordsC );
            } // if ( this->EdgesA[i] == -1 )

            // Update points
            ids[nPts] = this->EdgesA[i];
            ++ nPts;
            if( indPairA )
            {
              pairA[1] = i;
            }
            else
            {
              pairA[0] = i;
            };
            ++ indPairA;
          } // if ( A1 > 0. && A1 * B1 < 0. )
        } // p

        // Insert pairs only if it makes sense
        if ( indPairA == 2 )
        {
          this->FacesA->InsertNextTypedTuple( pairA );
        }
        if ( indPairB == 2 )
        {
          this->FacesB->InsertNextTypedTuple( pairB );
        }
      } // else if( ! type )
      else if ( type == -1. )
      {
        // Take positive values in A
        vtkIdType pair[] = { -1, -1 };
        unsigned int indPair = 0;

        // Loop over face vertices
        for( int p = 0; p < 4; ++ p )
        {
          // Retrieve vertex coordinates
          this->FacePoints->GetPoint( p, coordsA );

          // Retrieve vertex scalars
          double A = this->FaceScalarsA->GetTuple1( p );
          double B = this->FaceScalarsA->GetTuple1( ( p + 1 ) % 4 );

          // Add point when necessary
          if ( create && A >= 0. )
          {
            ids[nPts] = this->Points->InsertNextPoint( coordsA );
            ++ nPts;
          }
          if ( A * B < 0. )
          {
            unsigned int i = EdgeIndices[orientation][offset][p];
            if ( this->EdgesA[i] == -1 )
            {
              //Compute barycenter of A and B
              this->FacePoints->GetPoint( ( p + 1 ) % 4, coordsB );
              for( int j = 0; j < 3 ;++ j )
              {
                coordsC[j] = ( B * coordsA[j] - A * coordsB[j] ) / ( B - A );
              }
              this->EdgesA[i] = this->Points->InsertNextPoint( coordsC );
            } // if ( this->EdgesB[i] == -1 )

            // Update points
            ids[nPts] = this->EdgesA[i];
            ++ nPts;
            if( indPair )
            {
              pair[1] = i;
            }
            else
            {
              pair[0] = i;
            };
            ++ indPair;
          } // if ( A * B < 0. )
        } // p

        // Insert pair only if it makes sense
        if ( indPair == 2 )
        {
          this->FacesA->InsertNextTypedTuple( pair );
        }
      } // else if ( type == -1. )
    } // if ( type < 2 )
    else
    {
      // Create quadrangle vertices depending on orientation
      ids[0] = this->Points->InsertNextPoint( pt );
      pt[axis1] += size[axis1];
      ids[1] = this->Points->InsertNextPoint( pt );
      pt[axis2] += size[axis2];
      ids[2] = this->Points->InsertNextPoint( pt );
      pt[axis1] = origin[axis1];
      ids[3] = this->Points->InsertNextPoint( pt );
    } // else
  } // if ( this->HasInterface )
  else
  {
    // Create quadrangle vertices depending on orientation
    ids[0] = this->Points->InsertNextPoint( pt );
    pt[axis1] += size[axis1];
    ids[1] = this->Points->InsertNextPoint( pt );
    pt[axis2] += size[axis2];
    ids[2] = this->Points->InsertNextPoint( pt );
    pt[axis1] = origin[axis1];
    ids[3] = this->Points->InsertNextPoint( pt );
  } // else

  // Insert next face if needed
  if ( create )
  {
    // Create cell and corresponding ID
    vtkIdType outId = this->Cells->InsertNextCell( nPts, ids );

    // Copy face data from that of the cell from which it comes
    this->OutData->CopyData( this->InData, inId, outId );
  } // if ( create )
957
}