File: vtkOrderedTriangulator.cxx

package info (click to toggle)
vtk6 6.3.0%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 118,880 kB
  • sloc: cpp: 1,442,792; ansic: 113,395; python: 72,383; tcl: 46,998; xml: 8,119; yacc: 4,525; java: 4,239; perl: 3,108; lex: 1,694; sh: 1,093; asm: 154; makefile: 103; objc: 17
file content (1715 lines) | stat: -rw-r--r-- 55,831 bytes parent folder | download | duplicates (3)
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
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkOrderedTriangulator.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 "vtkOrderedTriangulator.h"

#include "vtkCellArray.h"
#include "vtkEdgeTable.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkTetra.h"
#include "vtkUnstructuredGrid.h"
#include "vtkHeap.h"
#include "vtkDataArray.h"
#include "vtkDoubleArray.h"
#include "vtkIncrementalPointLocator.h"
#include "vtkPointData.h"
#include "vtkCellData.h"

#include <list>
#include <vector>
#include <stack>
#include <map>
#include <cassert>

vtkStandardNewMacro(vtkOrderedTriangulator);

#ifdef _WIN32_WCE
# ifndef __PLACEMENT_NEW_INLINE
#  define __PLACEMENT_NEW_INLINE
   inline void *__cdecl operator new(size_t, void *_P) { return (_P); }
   inline void __cdecl operator delete(void *, void *) { return; }
# endif
#else
# include <new>
#endif

// Old HP compiler does not support operator delete that is called
// when a constructor called by operator new throws.
#if defined(__HP_aCC) && (__HP_aCC < 061200)
# define VTK_NO_PLACEMENT_DELETE
#endif
// SGI compiler does not support placement delete that is called when
// a constructor called by placement new throws.
#if defined(__sgi) && !defined(__GNUC__)
# define VTK_NO_PLACEMENT_DELETE
#endif

// Classes are used to represent points, faces, and tetras-------------------
// This data structure consists of points and tetras, with the face used
// temporarily as a place holder during triangulation.
struct OTPoint;
struct OTFace;
struct OTTetra;

//---Class represents a point (and related typedefs)--------------------------
// Note that the points has two sets of coordinates: the first the actual
// position X[3] and the second the coordinate used for performing
// triangulation (usually a parametric coordinate P[3]).
struct OTPoint
{
  OTPoint() : Type(Inside), Id(0), SortId(0), SortId2(0), OriginalId(0),
              InsertionId(0)
    {
      this->X[0] = this->X[1] = this->X[2] = 0.0;
      this->P[0] = this->P[1] = this->P[2] = 0.0;
    }

  enum PointClassification
    {Inside=0,Outside=1,Boundary=2,Added=3,NoInsert=4};
  PointClassification Type;
  double X[3]; //Actual position of point
  double P[3]; //Triangulation coordinate (typically parametric coordinate)

  //Id of originating point
  vtkIdType Id;

  //Id used to sort points prior to triangulation
  vtkIdType SortId;

  //Second id used to sort in triangulation
  //This can be used in situations where one id is not enough
  //(for example, when the id is related to an edge which
  // is described by two points)
  vtkIdType SortId2;

  //Id based on order seen in InsertPoint()
  vtkIdType OriginalId;
  //Id after sorting the points (i.e. order inserted into mesh)
  vtkIdType InsertionId;
};
struct PointListType : public std::vector<OTPoint>
{
  PointListType() : std::vector<OTPoint>() {}
  OTPoint* GetPointer(int ptId)
    {return &( *(this->begin()+ptId) ); }
};
typedef PointListType::iterator PointListIterator;

//---Class represents a face (and related typedefs)--------------------------
struct OTFace //used during tetra construction
{
  void *operator new(size_t size, vtkHeap *heap)
    {return heap->AllocateMemory(size);}
#if !defined(VTK_NO_PLACEMENT_DELETE)
  void operator delete(void*,vtkHeap*) {}
#endif

  OTPoint *Points[3]; //the three points of the face
  OTTetra *Neighbor;
  double   Normal[3];
  double   N2;

  void ComputePseudoNormal()
    {
      double v20[3], v10[3];
      v20[0] = this->Points[2]->P[0] - this->Points[0]->P[0];
      v20[1] = this->Points[2]->P[1] - this->Points[0]->P[1];
      v20[2] = this->Points[2]->P[2] - this->Points[0]->P[2];
      v10[0] = this->Points[1]->P[0] - this->Points[0]->P[0];
      v10[1] = this->Points[1]->P[1] - this->Points[0]->P[1];
      v10[2] = this->Points[1]->P[2] - this->Points[0]->P[2];
      vtkMath::Cross(v10,v20,this->Normal);
      this->N2 = vtkMath::Dot(this->Normal,this->Normal);
    }
  int IsValidCavityFace(double X[3],double tol2)
    {
      double vp[3], d;
      vp[0] = X[0] - this->Points[0]->P[0];
      vp[1] = X[1] - this->Points[0]->P[1];
      vp[2] = X[2] - this->Points[0]->P[2];
      d = vtkMath::Dot(vp,this->Normal);
      return ( (d > 0.0L && (d*d) > (tol2*this->N2)) ? 1 : 0 );
    }
};
typedef std::vector<OTFace*>            FaceListType;
typedef std::vector<OTFace*>::iterator  FaceListIterator;

//---Class represents a tetrahedron (and related typedefs)--------------------
typedef std::list<OTTetra*>             TetraListType;
typedef std::list<OTTetra*>::iterator   TetraListIterator;
struct TetraStackType : public std::stack<OTTetra*>
{
  TetraStackType() : std::stack<OTTetra*>() {}
  void clear() {while (!this->empty()) this->pop();}
};
typedef std::vector<OTTetra*>           TetraQueueType;
typedef std::vector<OTTetra*>::iterator TetraQueueIterator;

struct OTTetra
{
  void *operator new(size_t size, vtkHeap *heap)
    {return heap->AllocateMemory(size);}
#if !defined(VTK_NO_PLACEMENT_DELETE)
  void operator delete(void*,vtkHeap*) {}
#endif

  OTTetra() : Radius2(0.0L), CurrentPointId(-1), Type(OutsideCavity)
    {
    this->Center[0] = this->Center[1] = this->Center[2] = 0.0L;
    this->Points[0] = this->Points[1] = this->Points[2] = this->Points[3] = 0;
    this->Neighbors[0] = this->Neighbors[1] =
      this->Neighbors[2] = this->Neighbors[3] = 0;
    this->DeleteMe = 0;
    }

  // Center and radius squared of circumsphere of this tetra
  double Radius2;
  double Center[3];

  // Note: there is a direct correlation between the points and the faces
  // i.e., the ordering of the points and face neighbors.
  OTTetra *Neighbors[4]; //the four face neighbors
  OTPoint *Points[4]; //the four points

  // The following are used during point insertion
  int CurrentPointId; //indicated current point being inserted
  enum TetraClassification
    {Inside=0,Outside=1,All=2,InCavity=3,OutsideCavity=4,Exterior=5};
  TetraClassification Type;

  // Supporting triangulation operators
  void GetFacePoints(int i, OTFace *face);
  int InCircumSphere(double x[3]);
  TetraClassification DetermineType(); //inside, outside
  int DeleteMe;
};

//---Class represents the Delaunay triangulation using points and tetras.
// Additional support for the Delaunay triangulation process.
struct vtkOTMesh
{
  vtkOTMesh(vtkHeap *heap) :
    NumberOfTetrasClassifiedInside(0), NumberOfTemplates(0)
    {
      this->EdgeTable = vtkEdgeTable::New();
      this->Heap = heap;
    }
  ~vtkOTMesh()
    {
      this->EdgeTable->Delete();
    }

  PointListType   Points;          //Points in the mesh
  TetraListType   Tetras;          //Tetrahedra in the mesh
  FaceListType    CavityFaces;     //Faces forming an insertion cavity
  TetraQueueType  VisitedTetras;   //Those tetra already visited during insertion
  TetraStackType  TetraStack;      //Stack of tetra visited during point insertion
  TetraQueueType  DegenerateQueue; //Tetra involved in degenerate triangulation
  vtkEdgeTable   *EdgeTable;       //Edges used to create triangulation of cavity
  double          Tolerance2;      //Used to control error
  vtkHeap        *Heap;            //Many allocations occur in efficient heap

  int             NumberOfTetrasClassifiedInside;
  int             NumberOfTemplates;
  TetraListIterator CurrentTetra;

  void Reset()
    {
      this->Points.clear();
      this->Tetras.clear();
      this->CavityFaces.clear();
      this->VisitedTetras.clear();
      this->TetraStack.clear();
      this->DegenerateQueue.clear();
      this->EdgeTable->Reset();
    }

  OTTetra *CreateTetra(OTPoint *p, OTFace *face);
  OTTetra *WalkToTetra(OTTetra *t,double x[3],int depth,double bc[4]);
  int CreateInsertionCavity(OTPoint* p, OTTetra *tetra, double bc[4]);
  int ClassifyTetras();
  void DumpInsertionCavity(double x[3]);
};

//---Classes and typedefs used to support triangulation templates.
// Triangulation templates are used instead of Delaunay triangulation
// because they are so much faster. Because there are so many possible
// triangulations/templates possible, triangulation templates are
// computed on the fly and then cached.
//
// Two lists are kept. The first is a list of lists of templates for
// each cell type. The second is a list of templates for each cell.
//
// A specific template. The number of tetras and the tetra connectivity.
struct OTTemplate
{
  vtkIdType  NumberOfTetras;
  vtkIdType *Tetras;

  OTTemplate(vtkIdType numberOfTetras, vtkHeap *heap)
    {
      this->NumberOfTetras = numberOfTetras;
      this->Tetras = static_cast<vtkIdType*>(
        heap->AllocateMemory(sizeof(vtkIdType)*numberOfTetras*4) );
    }
  void *operator new(size_t size, vtkHeap *heap)
    {return heap->AllocateMemory(size);}
#if !defined(VTK_NO_PLACEMENT_DELETE)
  void operator delete(void*,vtkHeap*) {}
#endif
};


// Typedefs for a list of templates for a particular cell. Key is the
// template index.
typedef std::map<TemplateIDType,OTTemplate*>           TemplateList;
typedef std::map<TemplateIDType,OTTemplate*>::iterator TemplateListIterator;

//
// Typedefs for a list of lists of templates keyed on cell type
struct vtkOTTemplates : public std::map<int,TemplateList*> {};
typedef std::map<int,TemplateList*>::iterator TemplatesIterator;


//------------------------------------------------------------------------
vtkOrderedTriangulator::vtkOrderedTriangulator()
{
  // In place news (using allocators) are done here
  this->Heap = vtkHeap::New();
  this->Heap->SetBlockSize(500000);

  this->Mesh = new vtkOTMesh(this->Heap);
  this->NumberOfPoints = 0;
  this->PreSorted = 0;
  this->UseTwoSortIds = 0;

  this->UseTemplates = 0;
  this->NumberOfCellPoints = 0;
  this->NumberOfCellEdges = 0;
  this->Templates = new vtkOTTemplates;
  this->TemplateHeap = vtkHeap::New();
  this->TemplateHeap->SetBlockSize(250000);
}

//------------------------------------------------------------------------
vtkOrderedTriangulator::~vtkOrderedTriangulator()
{
  delete this->Mesh;
  this->Heap->Delete();

  TemplatesIterator titer;
  for (titer=this->Templates->begin(); titer != this->Templates->end(); ++titer)
    {
    delete (*titer).second;
    }
  delete this->Templates;
  this->TemplateHeap->Delete();
}

//------------------------------------------------------------------------
void vtkOrderedTriangulator::InitTriangulation(double xmin, double xmax,
                                               double ymin, double ymax,
                                               double zmin, double zmax,
                                               int numPts)
{
  double bounds[6];
  bounds[0] = xmin;
  bounds[1] = xmax;
  bounds[2] = ymin;
  bounds[3] = ymax;
  bounds[4] = zmin;
  bounds[5] = zmax;

  this->InitTriangulation(bounds,numPts);

  // The templates remain valid and are reused.
}

//------------------------------------------------------------------------
void vtkOrderedTriangulator::InitTriangulation(double bounds[6], int numPts)
{
  this->Heap->Reset();
  this->Mesh->Reset();
  this->NumberOfPoints = 0;
  this->MaximumNumberOfPoints = numPts;
  this->Mesh->Points.resize(numPts+6);

  for (int i=0; i<6; i++)
    {
    this->Bounds[i] = bounds[i];
    }
}

//------------------------------------------------------------------------
// Create an initial bounding Delaunay triangulation consisting of four
// tetras arranged in an octahedron.
void vtkOrderedTriangulator::Initialize()
{
  double length;
  double center[3];
  double radius2;

  // Set up the internal data structures. Space for six extra points
  // is allocated for the bounding triangulation.
  int numPts = this->MaximumNumberOfPoints;
  double *bounds = this->Bounds;

  // Create the initial Delaunay triangulation which is a
  // bounding octahedron: 6 points & 4 tetra.
  center[0] = (bounds[0]+bounds[1])/2.0;
  center[1] = (bounds[2]+bounds[3])/2.0;
  center[2] = (bounds[4]+bounds[5])/2.0;
  length = 2.0 * sqrt( (radius2 = (bounds[1]-bounds[0])*(bounds[1]-bounds[0]) +
                 (bounds[3]-bounds[2])*(bounds[3]-bounds[2]) +
                 (bounds[5]-bounds[4])*(bounds[5]-bounds[4])) );
  radius2 /= 2.0;
  this->Mesh->Tolerance2 = length*length*1.0e-10;

  // Define the points (-x,+x,-y,+y,-z,+z). Theses added points are
  // used to create a bounding octahedron.
  this->Mesh->Points[numPts].P[0] = center[0] - length;
  this->Mesh->Points[numPts].P[1] = center[1];
  this->Mesh->Points[numPts].P[2] = center[2];
  this->Mesh->Points[numPts].Id = numPts;
  this->Mesh->Points[numPts].InsertionId = numPts;
  this->Mesh->Points[numPts].Type = OTPoint::Added;

  this->Mesh->Points[numPts+1].P[0] = center[0] + length;
  this->Mesh->Points[numPts+1].P[1] = center[1];
  this->Mesh->Points[numPts+1].P[2] = center[2];
  this->Mesh->Points[numPts+1].Id = numPts + 1;
  this->Mesh->Points[numPts+1].InsertionId = numPts + 1;
  this->Mesh->Points[numPts+1].Type = OTPoint::Added;

  this->Mesh->Points[numPts+2].P[0] = center[0];
  this->Mesh->Points[numPts+2].P[1] = center[1] - length;
  this->Mesh->Points[numPts+2].P[2] = center[2];
  this->Mesh->Points[numPts+2].Id = numPts + 2;
  this->Mesh->Points[numPts+2].InsertionId = numPts + 2;
  this->Mesh->Points[numPts+2].Type = OTPoint::Added;

  this->Mesh->Points[numPts+3].P[0] = center[0];
  this->Mesh->Points[numPts+3].P[1] = center[1] + length;
  this->Mesh->Points[numPts+3].P[2] = center[2];
  this->Mesh->Points[numPts+3].Id = numPts + 3;
  this->Mesh->Points[numPts+3].InsertionId = numPts + 3;
  this->Mesh->Points[numPts+3].Type = OTPoint::Added;

  this->Mesh->Points[numPts+4].P[0] = center[0];
  this->Mesh->Points[numPts+4].P[1] = center[1];
  this->Mesh->Points[numPts+4].P[2] = center[2] - length;
  this->Mesh->Points[numPts+4].Id = numPts + 4;
  this->Mesh->Points[numPts+4].InsertionId = numPts + 4;
  this->Mesh->Points[numPts+4].Type = OTPoint::Added;

  this->Mesh->Points[numPts+5].P[0] = center[0];
  this->Mesh->Points[numPts+5].P[1] = center[1];
  this->Mesh->Points[numPts+5].P[2] = center[2] + length;
  this->Mesh->Points[numPts+5].Id = numPts + 5;
  this->Mesh->Points[numPts+5].InsertionId = numPts + 5;
  this->Mesh->Points[numPts+5].Type = OTPoint::Added;

  // Create bounding tetras (there are four) as well as the associated faces
  // They all share the same center and radius
  OTTetra *tetras[4];
  for (int i=0; i<4; ++i)
    {
    tetras[i] = new(this->Heap) OTTetra();
    this->Mesh->Tetras.push_front(tetras[i]);
    tetras[i]->Center[0] = center[0];
    tetras[i]->Center[1] = center[1];
    tetras[i]->Center[2] = center[2];
    tetras[i]->Radius2 = radius2;
    }

  //Okay now set up the points and neighbors in the tetras
  tetras[0]->Points[0] = this->Mesh->Points.GetPointer(numPts + 0);
  tetras[0]->Points[1] = this->Mesh->Points.GetPointer(numPts + 2);
  tetras[0]->Points[2] = this->Mesh->Points.GetPointer(numPts + 4);
  tetras[0]->Points[3] = this->Mesh->Points.GetPointer(numPts + 5);
  tetras[0]->Neighbors[0] = 0; //outside
  tetras[0]->Neighbors[1] = tetras[1];
  tetras[0]->Neighbors[2] = tetras[3];
  tetras[0]->Neighbors[3] = 0;

  tetras[1]->Points[0] = this->Mesh->Points.GetPointer(numPts + 2);
  tetras[1]->Points[1] = this->Mesh->Points.GetPointer(numPts + 1);
  tetras[1]->Points[2] = this->Mesh->Points.GetPointer(numPts + 4);
  tetras[1]->Points[3] = this->Mesh->Points.GetPointer(numPts + 5);
  tetras[1]->Neighbors[0] = 0;
  tetras[1]->Neighbors[1] = tetras[2];
  tetras[1]->Neighbors[2] = tetras[0];
  tetras[1]->Neighbors[3] = 0;

  tetras[2]->Points[0] = this->Mesh->Points.GetPointer(numPts + 1);
  tetras[2]->Points[1] = this->Mesh->Points.GetPointer(numPts + 3);
  tetras[2]->Points[2] = this->Mesh->Points.GetPointer(numPts + 4);
  tetras[2]->Points[3] = this->Mesh->Points.GetPointer(numPts + 5);
  tetras[2]->Neighbors[0] = 0;
  tetras[2]->Neighbors[1] = tetras[3];
  tetras[2]->Neighbors[2] = tetras[1];
  tetras[2]->Neighbors[3] = 0;

  tetras[3]->Points[0] = this->Mesh->Points.GetPointer(numPts + 3);
  tetras[3]->Points[1] = this->Mesh->Points.GetPointer(numPts + 0);
  tetras[3]->Points[2] = this->Mesh->Points.GetPointer(numPts + 4);
  tetras[3]->Points[3] = this->Mesh->Points.GetPointer(numPts + 5);
  tetras[3]->Neighbors[0] = 0;
  tetras[3]->Neighbors[1] = tetras[0];
  tetras[3]->Neighbors[2] = tetras[2];
  tetras[3]->Neighbors[3] = 0;
}


//------------------------------------------------------------------------
// Add a point to the list of points to be triangulated.
vtkIdType vtkOrderedTriangulator::InsertPoint(vtkIdType id, double x[3],
                                              double p[3], int type)
{
  vtkIdType idx = this->NumberOfPoints++;
  if ( idx >= this->MaximumNumberOfPoints )
    {
    vtkErrorMacro(<< "Trying to insert more points than specified max="
      << this->MaximumNumberOfPoints << " idx=" << idx);
    return idx;
    }

  this->Mesh->Points[idx].Id = id;
  this->Mesh->Points[idx].SortId = id;
  this->Mesh->Points[idx].SortId2 = -1;
  this->Mesh->Points[idx].OriginalId = idx;
  this->Mesh->Points[idx].InsertionId = -1; //dummy value until inserted
  this->Mesh->Points[idx].X[0] = x[0];
  this->Mesh->Points[idx].X[1] = x[1];
  this->Mesh->Points[idx].X[2] = x[2];
  this->Mesh->Points[idx].P[0] = p[0];
  this->Mesh->Points[idx].P[1] = p[1];
  this->Mesh->Points[idx].P[2] = p[2];
  this->Mesh->Points[idx].Type =
    static_cast<OTPoint::PointClassification>(type);

  return idx;
}

//------------------------------------------------------------------------
// Add a point to the list of points to be triangulated.
vtkIdType vtkOrderedTriangulator::InsertPoint(vtkIdType id, vtkIdType sortid,
                                              double x[3], double p[3],
                                              int type)
{
  vtkIdType idx = this->NumberOfPoints++;
  if ( idx >= this->MaximumNumberOfPoints )
    {
    vtkErrorMacro(<< "Trying to insert more points than specified");
    return idx;
    }

  this->Mesh->Points[idx].Id = id;
  this->Mesh->Points[idx].SortId = sortid;
  this->Mesh->Points[idx].SortId2 = -1;
  this->Mesh->Points[idx].OriginalId = idx;
  this->Mesh->Points[idx].InsertionId = -1; //dummy value until inserted
  this->Mesh->Points[idx].X[0] = x[0];
  this->Mesh->Points[idx].X[1] = x[1];
  this->Mesh->Points[idx].X[2] = x[2];
  this->Mesh->Points[idx].P[0] = p[0];
  this->Mesh->Points[idx].P[1] = p[1];
  this->Mesh->Points[idx].P[2] = p[2];
  this->Mesh->Points[idx].Type =
    static_cast<OTPoint::PointClassification>(type);

  return idx;
}

//------------------------------------------------------------------------
// Add a point to the list of points to be triangulated.
vtkIdType vtkOrderedTriangulator::InsertPoint(vtkIdType id, vtkIdType sortid,
                                              vtkIdType sortid2,
                                              double x[3], double p[3],
                                              int type)
{
  vtkIdType idx = this->NumberOfPoints++;
  if ( idx >= this->MaximumNumberOfPoints )
    {
    vtkErrorMacro(<< "Trying to insert more points than specified");
    return idx;
    }

  this->Mesh->Points[idx].Id = id;
  this->Mesh->Points[idx].SortId = sortid;
  this->Mesh->Points[idx].SortId2 = sortid2;
  this->Mesh->Points[idx].OriginalId = idx;
  this->Mesh->Points[idx].InsertionId = -1; //dummy value until inserted
  this->Mesh->Points[idx].X[0] = x[0];
  this->Mesh->Points[idx].X[1] = x[1];
  this->Mesh->Points[idx].X[2] = x[2];
  this->Mesh->Points[idx].P[0] = p[0];
  this->Mesh->Points[idx].P[1] = p[1];
  this->Mesh->Points[idx].P[2] = p[2];
  this->Mesh->Points[idx].Type =
    static_cast<OTPoint::PointClassification>(type);

  return idx;
}

//------------------------------------------------------------------------
// Used when an already inserted point must have its classification changed
// (e.g., an intersection point is very near another point).
void vtkOrderedTriangulator::UpdatePointType(vtkIdType internalId, int type)
{
  assert("pre: valid_range" && internalId>=0 &&
         internalId<this->NumberOfPoints);
  this->Mesh->Points[internalId].Type =
    static_cast<OTPoint::PointClassification>(type);
}

//------------------------------------------------------------------------
double *vtkOrderedTriangulator::GetPointPosition(vtkIdType internalId)
{
  assert("pre: valid_range" && internalId>=0 &&
         internalId<this->NumberOfPoints);
  return this->Mesh->Points[internalId].P;
}

//------------------------------------------------------------------------
double *vtkOrderedTriangulator::GetPointLocation(vtkIdType internalId)
{
  assert("pre: valid_range" && internalId>=0 &&
         internalId<this->NumberOfPoints);
  return this->Mesh->Points[internalId].X;
}

//------------------------------------------------------------------------
vtkIdType vtkOrderedTriangulator::GetPointId(vtkIdType internalId)
{
  assert("pre: valid_range" && internalId>=0 &&
         internalId<this->NumberOfPoints);
  return this->Mesh->Points[internalId].Id;
}
//------------------------------------------------------------------------
// For a particular tetra and given a face id, return the three points
// defining the face.
void OTTetra::GetFacePoints(int i, OTFace *face)
{
  // The order is carefully chosen to produce a tetrahedron
  // that is not inside out; i.e., the ordering produces a positive
  // Jacobian (computed from first three points points to fourth).
  switch (i)
    {
    case 0:
      face->Points[0] = this->Points[0];
      face->Points[1] = this->Points[3];
      face->Points[2] = this->Points[1];
      break;
    case 1:
      face->Points[0] = this->Points[1];
      face->Points[1] = this->Points[3];
      face->Points[2] = this->Points[2];
      break;
    case 2:
      face->Points[0] = this->Points[0];
      face->Points[1] = this->Points[2];
      face->Points[2] = this->Points[3];
      break;
    case 3:
      face->Points[0] = this->Points[0];
      face->Points[1] = this->Points[1];
      face->Points[2] = this->Points[2];
      break;
    }
  face->ComputePseudoNormal();
}

//------------------------------------------------------------------------
// Routines used to sort the points based on id.
extern "C" {
#ifdef _WIN32_WCE
  static int __cdecl vtkSortOnIds(const void *val1, const void *val2)
#else
  static int vtkSortOnIds(const void *val1, const void *val2)
#endif
  {
    if (((OTPoint *)val1)->SortId < ((OTPoint *)val2)->SortId)
      {
      return (-1);
      }
    else if (((OTPoint *)val1)->SortId > ((OTPoint *)val2)->SortId)
      {
      return (1);
      }
    else
      {
      return (0);
      }
  }
}

extern "C" {
#ifdef _WIN32_WCE
  static int __cdecl vtkSortOnTwoIds(const void *val1, const void *val2)
#else
  static int vtkSortOnTwoIds(const void *val1, const void *val2)
#endif
  {
    if (((OTPoint *)val1)->SortId2 < ((OTPoint *)val2)->SortId2)
      {
      return (-1);
      }
    else if (((OTPoint *)val1)->SortId2 > ((OTPoint *)val2)->SortId2)
      {
      return (1);
      }

    if (((OTPoint *)val1)->SortId < ((OTPoint *)val2)->SortId)
      {
      return (-1);
      }
    else if (((OTPoint *)val1)->SortId > ((OTPoint *)val2)->SortId)
      {
      return (1);
      }
    else
      {
      return (0);
      }
  }
}

//------------------------------------------------------------------------
// See whether point is in sphere of tetrahedron.
int OTTetra::InCircumSphere(double x[3])
{
  double dist2;

  // check if inside/outside circumsphere
  dist2 = (x[0] - this->Center[0]) * (x[0] - this->Center[0]) +
          (x[1] - this->Center[1]) * (x[1] - this->Center[1]) +
          (x[2] - this->Center[2]) * (x[2] - this->Center[2]);

  return (dist2 < (0.999999L * this->Radius2) ? 1 : 0);
}

//------------------------------------------------------------------------
// Determine the classification of a tetra based on point types.
inline OTTetra::TetraClassification OTTetra::DetermineType()
{
  if ( (this->Points[0]->Type == OTPoint::Inside ||
        this->Points[0]->Type == OTPoint::Boundary ) &&
       (this->Points[1]->Type == OTPoint::Inside ||
        this->Points[1]->Type == OTPoint::Boundary ) &&
       (this->Points[2]->Type == OTPoint::Inside ||
        this->Points[2]->Type == OTPoint::Boundary ) &&
       (this->Points[3]->Type == OTPoint::Inside ||
        this->Points[3]->Type == OTPoint::Boundary ) )
    {
    this->Type = OTTetra::Inside;
    return OTTetra::Inside;
    }
  else if ( (this->Points[0]->Type == OTPoint::Outside ||
             this->Points[0]->Type == OTPoint::Boundary ) &&
            (this->Points[1]->Type == OTPoint::Outside ||
             this->Points[1]->Type == OTPoint::Boundary ) &&
            (this->Points[2]->Type == OTPoint::Outside ||
             this->Points[2]->Type == OTPoint::Boundary ) &&
            (this->Points[3]->Type == OTPoint::Outside ||
             this->Points[3]->Type == OTPoint::Boundary ) )
    {
    this->Type = OTTetra::Outside;
    return OTTetra::Outside;
    }
  else
    {
    this->Type = OTTetra::Exterior;
    return OTTetra::Exterior;
    }
}

//------------------------------------------------------------------------
// Determine whether the point is used by a specified tetra.
inline static int IsAPoint(OTTetra *t, vtkIdType id)
{
  if ( id == t->Points[0]->InsertionId || id == t->Points[1]->InsertionId ||
       id == t->Points[2]->InsertionId || id == t->Points[3]->InsertionId )
    {
    return 1;
    }
  else
    {
    return 0;
    }
}

//------------------------------------------------------------------------
// Given two tetra face neighbors, assign the neighbor pointers to each tetra.
static void AssignNeighbors(OTTetra* t1, OTTetra* t2)
{
  static int CASE_MASK[4] = {1,2,4,8};
  int i, index;

  for (i=0, index=0; i<4; ++i)
    {
    if (IsAPoint(t2,t1->Points[i]->InsertionId) )
      {
      index |= CASE_MASK[i];
      }
    }
  switch (index)
    {
    case 11:
      t1->Neighbors[0] = t2;
      break;
    case 14:
      t1->Neighbors[1] = t2;
      break;
    case 13:
      t1->Neighbors[2] = t2;
      break;
    case 7:
      t1->Neighbors[3] = t2;
      break;
    default:
      vtkGenericWarningMacro(<<"Really bad");
    }

  for (i=0, index=0; i<4; ++i)
    {
    if (IsAPoint(t1,t2->Points[i]->InsertionId) )
      {
      index |= CASE_MASK[i];
      }
    }
  switch (index)
    {
    case 11:
      t2->Neighbors[0] = t1;
      break;
    case 14:
      t2->Neighbors[1] = t1;
      break;
    case 13:
      t2->Neighbors[2] = t1;
      break;
    case 7:
      t2->Neighbors[3] = t1;
      break;
    default:
      vtkGenericWarningMacro(<<"Really bad");
    }
}

//------------------------------------------------------------------------
// Instantiate and initialize a tetra.
OTTetra *vtkOTMesh::CreateTetra(OTPoint *p, OTFace *face)
{
  OTTetra *tetra = new(this->Heap) OTTetra;
  this->Tetras.push_front(tetra);
  tetra->Radius2 = vtkTetra::Circumsphere(p->P,
                                          face->Points[0]->P,
                                          face->Points[1]->P,
                                          face->Points[2]->P,
                                          tetra->Center);

  // the order is carefully chosen to produce a tetrahedron
  // that is not inside out; i.e., the ordering produces a positive
  // jacobian (normal computed from first three points points to fourth).
  tetra->Points[0] = face->Points[0];
  tetra->Points[1] = face->Points[1];
  tetra->Points[2] = face->Points[2];
  tetra->Points[3] = p;

  if ( face->Neighbor )
    {
    AssignNeighbors(tetra,face->Neighbor);
    }

  return tetra;
}

//------------------------------------------------------------------------
// We start with a point that is inside a tetrahedron. We find face
// neighbors of the tetrahedron that also contain the point. The
// process continues recursively until no more tetrahedron are found.
// Faces that lie between a tetrahedron that is in the cavity and one
// that is not form the cavity boundary, these are kept track of in
// a list. Eventually the point and boundary faces form new tetrahedra.
int vtkOTMesh::CreateInsertionCavity(OTPoint* p, OTTetra *initialTet,
                                     double [4])
{
  // Prepare to insert deleted tetras and cavity faces
  //
  this->CavityFaces.clear(); //cavity face boundary
  this->VisitedTetras.clear(); //tetras involved in creating cavity
  this->TetraStack.clear(); //queue of tetras being processed
  this->DegenerateQueue.clear(); //queue of tetras that have degenerate faces
  this->TetraStack.push(initialTet);
  initialTet->Type = OTTetra::InCavity; //the seed of the cavity
  initialTet->CurrentPointId = p->InsertionId; //mark visited
  this->VisitedTetras.push_back(initialTet);

  // Process queue of tetras until exhausted
  //
  int i, valid;
  int somethingNotValid=0;
  OTTetra *nei, *tetra;
  TetraQueueIterator t;
  for ( int numCycles=0; !this->TetraStack.empty(); numCycles++)
    {
    tetra = this->TetraStack.top();
    this->TetraStack.pop();

    //for each face, see whether the neighbors are in the cavity
    for (valid=1, i=0; i<4 && valid; ++i)
      {
      nei = tetra->Neighbors[i];
      // If a mesh boundary face, the face is added to the
      // list of insertion cavity faces
      if ( nei == 0 )
        {
        OTFace *face = new(this->Heap) OTFace;
        tetra->GetFacePoints(i,face);
        face->Neighbor = 0;
        this->CavityFaces.push_back(face);
        valid = face->IsValidCavityFace(p->P,this->Tolerance2);
        }
      // Neighbor tetra has not been visited, check for possible face boundary
      else if ( nei->CurrentPointId != p->InsertionId )
        {
        this->VisitedTetras.push_back(nei);
        nei->CurrentPointId = p->InsertionId; //mark visited
        if ( nei->InCircumSphere(p->P) )
          {
          nei->Type = OTTetra::InCavity;
          this->TetraStack.push(nei);
          }
        else //a cavity boundary
          {
          nei->Type = OTTetra::OutsideCavity;
          OTFace *face = new(this->Heap) OTFace;
          tetra->GetFacePoints(i,face);
          face->Neighbor = nei;
          this->CavityFaces.push_back(face);
          valid = face->IsValidCavityFace(p->P,this->Tolerance2);
          }
        }//if a not-visited face neighbor
      // Visited before, add this face as a boundary
      else if ( nei->Type == OTTetra::OutsideCavity )
        {
        OTFace *face = new(this->Heap) OTFace;
        tetra->GetFacePoints(i,face);
        face->Neighbor = nei;
        this->CavityFaces.push_back(face);
        valid = face->IsValidCavityFace(p->P,this->Tolerance2);
        }
      }//for each of the four tetra faces

    //check for validity
    if ( !valid ) //broke out due to invalid face
      {
      somethingNotValid++;
      //add this tetra to queue
      this->DegenerateQueue.push_back(tetra);

      //mark all current tetras unvisited
      for (t = this->VisitedTetras.begin();
           t != this->VisitedTetras.end(); ++t)
        {
        (*t)->CurrentPointId = -1;
        }

      //mark degenerate tetras visited and outside cavity
      TetraQueueIterator titer;
      for ( titer=this->DegenerateQueue.begin();
            titer != this->DegenerateQueue.end(); ++titer)
        {
        (*titer)->CurrentPointId = p->InsertionId;
        (*titer)->Type = OTTetra::OutsideCavity;
        }

      //reinitialize queue
      this->CavityFaces.clear();  //cavity face boundary
      this->VisitedTetras.clear(); //tetras visited during cavity creation
      this->TetraStack.clear();   //reprocess
      this->TetraStack.push(initialTet);
      initialTet->CurrentPointId = p->InsertionId;
      initialTet->Type = OTTetra::InCavity;
      this->VisitedTetras.push_back(initialTet);
      }
    if ( numCycles > 1000 ) return 0;
    }//while queue not empty

  // Make final pass and delete tetras inside the cavity
  for (t = this->VisitedTetras.begin(); t != this->VisitedTetras.end(); ++t)
    {
    tetra = *t;
    if ( tetra->CurrentPointId == p->InsertionId &&
         tetra->Type == OTTetra::InCavity )
      {
      tetra->DeleteMe = 1;
      }
    }

  TetraListIterator it;
  for (it = this->Tetras.begin(); it != this->Tetras.end(); )
    {
    if ((*it)->DeleteMe)
      {
      it = this->Tetras.erase(it);
      }
    else
      {
      ++it;
      }
    }

#if 0
  //please leave this for debugging purposes
  if ( somethingNotValid )
    {
    this->DumpInsertionCavity(p->P);
//    exit(1);
    }
#endif

  return 1;
}

//------------------------------------------------------------------------
// Returns the number of tetras classified inside; a side effect is that
// all tetra are classified.
int vtkOTMesh::ClassifyTetras()
{
  TetraListIterator t;
  vtkIdType numInsideTetras=0;

  // loop over all tetras getting the ones with the classification requested
  for (t=this->Tetras.begin(); t != this->Tetras.end(); ++t)
    {
    if ( (*t)->DetermineType() == OTTetra::Inside )
      {
      numInsideTetras++;
      }
    }//for all tetras

  return numInsideTetras;
}

//------------------------------------------------------------------------
// Used to debug (writes a VTK file representing the current insertion cavity).
void vtkOTMesh::DumpInsertionCavity(double x[3])
{
  OTFace *face;
  FaceListIterator fptr;

  cout << "# vtk DataFile Version 3.0\n";
  cout << "ordered triangulator output\n";
  cout << "ASCII\n";
  cout << "DATASET POLYDATA\n";

  //write out points
  int numFaces = static_cast<int>(this->CavityFaces.size());
  cout << "POINTS " << 3*numFaces+1 << " double\n";

  for (fptr=this->CavityFaces.begin();
       fptr != this->CavityFaces.end(); ++fptr)
    {
    face = *fptr;
    cout << face->Points[0]->P[0] << " "
         << face->Points[0]->P[1] << " "
         << face->Points[0]->P[2] << " "
         << face->Points[1]->P[0] << " "
         << face->Points[1]->P[1] << " "
         << face->Points[1]->P[2] << " "
         << face->Points[2]->P[0] << " "
         << face->Points[2]->P[1] << " "
         << face->Points[2]->P[2] << "\n";
    }

  //write out point insertion vertex
  cout << x[0] << " " << x[1] << " " << x[2] << "\n\n";
  cout << "VERTICES 1 2 \n";
  cout << "1 " << 3*numFaces << "\n\n";

  //write out triangles
  cout << "POLYGONS " << numFaces << " " <<4*numFaces << "\n";

  int idx=0;
  for (fptr=this->CavityFaces.begin();
       fptr != this->CavityFaces.end(); ++fptr, idx+=3)
    {
    cout << 3 << " " << idx << " " << idx+1 << " " << idx+2 << "\n";
    }
}

//------------------------------------------------------------------------
// Walk to the tetra tha contains this point. Walking is done by moving
// in the direction of the most negative barycentric coordinate (i.e.,
// into the face neighbor).
OTTetra*
vtkOTMesh::WalkToTetra(OTTetra *tetra, double x[3], int depth, double bc[4])
{
  int neg = 0;
  int j, numNeg;
  double negValue;

  // prevent aimless wandering and death by recursion
  if ( depth > 200 )
    {
    return 0;
    }

  vtkTetra::BarycentricCoords(x, tetra->Points[0]->P, tetra->Points[1]->P,
                              tetra->Points[2]->P, tetra->Points[3]->P, bc);

  // find the most negative face
  for ( negValue=VTK_DOUBLE_MAX, numNeg=j=0; j<4; j++ )
    {
    if ( bc[j] < -0.000001 ) //if close enough that's okay
      {
      numNeg++;
      if ( bc[j] < negValue )
        {
        negValue = bc[j];
        neg = j;
        }
      }
    }

  // if no negatives, then inside this tetra
  if ( numNeg <= 0 )
    {
    return tetra;
    }

  // okay, march towards the most negative direction
  switch (neg)
    {
    case 0:
      tetra = tetra->Neighbors[1];
      break;
    case 1:
      tetra = tetra->Neighbors[2];
      break;
    case 2:
      tetra = tetra->Neighbors[0];
      break;
    case 3:
      tetra = tetra->Neighbors[3];
      break;
    }

  if ( tetra )
    {
    return this->WalkToTetra(tetra, x, ++depth, bc);
    }
  else
    {
    return 0;
    }
}

//------------------------------------------------------------------------
// Use an ordered insertion process in combination with a consistent
// degenerate resolution process to generate a unique Delaunay triangulation.
void vtkOrderedTriangulator::Triangulate()
{
  OTPoint *p;
  int i;
  vtkIdType ptId;

  // Sort the points according to id. The last six points are left
  // where they are (at the end of the list).
  if ( ! this->PreSorted )
    {
    if (this->UseTwoSortIds)
      {
      qsort(this->Mesh->Points.GetPointer(0), this->NumberOfPoints,
            sizeof(OTPoint), vtkSortOnTwoIds);
      }
    else
      {
      qsort(this->Mesh->Points.GetPointer(0), this->NumberOfPoints,
            sizeof(OTPoint), vtkSortOnIds);
      }
    }

  // Prepare the data structures (e.g., mesh) for an ordererd triangulation.
  this->Initialize();

  // Insert each point into the triangulation. Assign internal ids
  // as we progress.
  for (ptId=0, p=this->Mesh->Points.GetPointer(0);
       ptId < this->NumberOfPoints; ++p, ++ptId)
    {
    if ( p->Type == OTPoint::NoInsert )
      {
      continue; //skip this point
      }

    p->InsertionId = ptId;

    // Walk to a tetrahedron (start with first one on list)
    double bc[4];
    OTTetra *tetra =
      this->Mesh->WalkToTetra(*(this->Mesh->Tetras.begin()),p->P,0,bc);

    if ( tetra == 0 || !this->Mesh->CreateInsertionCavity(p, tetra, bc) )
      {
      vtkDebugMacro(<<"Point not in tetrahedron");
      continue;
      }

    // For each face on the boundary of the cavity, create a new
    // tetrahedron with the face and point. We've also got to set
    // up tetrahedron face neighbors, so we'll use an edge table
    // to keep track of the tetrahedron that generated the face as
    // a result of sweeping an edge.
    vtkIdType v1, v2;

    this->Mesh->EdgeTable->InitEdgeInsertion(this->MaximumNumberOfPoints+6,2);
    this->Mesh->TetraStack.clear();
    FaceListIterator fptr;
    void *tptr;
    OTTetra *neiTetra;
    OTFace *face;

    for (fptr=this->Mesh->CavityFaces.begin();
         fptr != this->Mesh->CavityFaces.end(); ++fptr)
      {
      face = *fptr;
      //create a tetra (it's added to the list of tetras as a side effect)
      tetra = this->Mesh->CreateTetra(p,face);

      for (i=0; i<3; ++i)
        {
        v1 = face->Points[i%3]->InsertionId;
        v2 = face->Points[(i+1)%3]->InsertionId;
        this->Mesh->EdgeTable->IsEdge(v1,v2,tptr);
        if ( ! tptr )
          {
          this->Mesh->EdgeTable->InsertEdge(v1,v2,tetra);
          }
        else
          {
          neiTetra = static_cast<OTTetra*>(tptr);
          AssignNeighbors(tetra, neiTetra);
          }
        }//for three edges
      }//for each face on the insertion cavity
    }//for all points to be inserted

  // Final classification
  this->Mesh->NumberOfTetrasClassifiedInside = this->Mesh->ClassifyTetras();
}


//------------------------------------------------------------------------
// Perform triangulation using templates (when possible).
void vtkOrderedTriangulator::TemplateTriangulate(int cellType,
                                                 int numPts, int numEdges)
{
  this->CellType = cellType;
  if ( ! this->UseTemplates || cellType != VTK_HEXAHEDRON)
    {
    this->Triangulate();
    return;
    }

  this->NumberOfCellPoints = numPts;
  this->NumberOfCellEdges = numEdges;

  // Sort the points according to id.
  if ( ! this->PreSorted )
    {
    if (this->UseTwoSortIds)
      {
      qsort(this->Mesh->Points.GetPointer(0), this->NumberOfPoints,
            sizeof(OTPoint), vtkSortOnTwoIds);
      }
    else
      {
      qsort(this->Mesh->Points.GetPointer(0), this->NumberOfPoints,
            sizeof(OTPoint), vtkSortOnIds);
      }
    }

  if ( ! this->TemplateTriangulation() )
    {//template triangulation didn't work, triangulate it and add to template cache
    int preSorted = this->PreSorted; //prevents resorting
    this->PreSorted = 1;
    this->Triangulate();
    this->AddTemplate();
    this->PreSorted = preSorted;
    }
}

//------------------------------------------------------------------------
// Add the tetras classified as specified to an unstructured grid.
vtkIdType vtkOrderedTriangulator::GetTetras(int classification,
                                            vtkUnstructuredGrid *ugrid)
{
  // Create the points
  //
  int i;
  vtkIdType numTetras=0;
  PointListIterator p;
  vtkPoints *points = vtkPoints::New();
  points->SetNumberOfPoints(this->NumberOfPoints);
  for ( i=0, p=this->Mesh->Points.begin(); i<this->NumberOfPoints; ++i, ++p)
    {
    points->SetPoint(p->InsertionId,p->X);
    }
  ugrid->SetPoints(points);
  points->Delete();

  ugrid->Allocate(1000);
  TetraListIterator t;
  OTTetra *tetra;

  // loop over all tetras getting the ones with the classification requested
  vtkIdType pts[4];
  for (t=this->Mesh->Tetras.begin(); t != this->Mesh->Tetras.end(); ++t)
    {
    tetra = *t;

    if ( tetra->Type == classification || classification == OTTetra::All)
      {
      numTetras++;
      pts[0] = tetra->Points[0]->Id;
      pts[1] = tetra->Points[1]->Id;
      pts[2] = tetra->Points[2]->Id;
      pts[3] = tetra->Points[3]->Id;
      ugrid->InsertNextCell(VTK_TETRA,4,pts);
      }
    }//for all tetras

  return numTetras;
}

//------------------------------------------------------------------------
// Add the tetras classified as specified to an unstructured grid
vtkIdType vtkOrderedTriangulator::AddTetras(int classification,
                                            vtkCellArray *outConnectivity)
{
  TetraListIterator t;
  OTTetra *tetra;
  vtkIdType numTetras=0;

  // loop over all tetras getting the ones with the classification requested
  for (t=this->Mesh->Tetras.begin(); t != this->Mesh->Tetras.end(); ++t)
    {
    tetra = *t;

    if ( tetra->Type == classification || classification == OTTetra::All)
      {
      numTetras++;
      outConnectivity->InsertNextCell(4);
      outConnectivity->InsertCellPoint(tetra->Points[0]->Id);
      outConnectivity->InsertCellPoint(tetra->Points[1]->Id);
      outConnectivity->InsertCellPoint(tetra->Points[2]->Id);
      outConnectivity->InsertCellPoint(tetra->Points[3]->Id);
      }
    }//for all tetras

  return numTetras;
}

//-----------------------------------------------------------------------------
// Assuming that all the inserted points come from a cell `cellId' to
// triangulate, get the tetrahedra in outConnectivity, the points in locator
// and copy point data and cell data. Return the number of added tetras.
// \pre locator_exists: locator!=0
// \pre outConnectivity: outConnectivity!=0
// \pre inPD_exists: inPD!=0
// \pre outPD_exists:  outPD!=0
// \pre inCD_exists: inCD!=0
// \pre outCD_exists: outCD!=0
vtkIdType vtkOrderedTriangulator::AddTetras(int classification,
                                            vtkIncrementalPointLocator *locator,
                                            vtkCellArray *outConnectivity,
                                            vtkPointData *inPD,
                                            vtkPointData *outPD,
                                            vtkCellData *inCD,
                                            vtkIdType cellId,
                                            vtkCellData *outCD)
{
  assert("pre: locator_exists" && locator!=0);
  assert("pre: outConnectivity" && outConnectivity!=0);
  assert("inPD_exists" && inPD!=0);
  assert("pre: outPD_exists" && outPD!=0);
  assert("inCD_exists" && inCD!=0);
  assert("pre: outCD_exists" && outCD!=0);

  TetraListIterator t;
  OTTetra *tetra;
  vtkIdType result=0;

  // loop over all tetras getting the ones with the classification requested
  for (t=this->Mesh->Tetras.begin(); t != this->Mesh->Tetras.end(); ++t)
    {
    tetra = *t;

    if ( tetra->Type == classification || classification == OTTetra::All)
      {
      // Insert the points
      vtkIdType pts[4];

      int i=0;
      while(i<4)
        {
        if(locator->InsertUniquePoint(tetra->Points[i]->X,pts[i]))
          {
          outPD->CopyData(inPD,tetra->Points[i]->Id,pts[i]);
          }
        ++i;
        }

      // Insert the connectivity
      result++;
      vtkIdType newCellId = outConnectivity->InsertNextCell(4,pts);
      outCD->CopyData(inCD,cellId,newCellId);
      }
    }//for all tetras

  return result;
}
//------------------------------------------------------------------------
// Initialize tetra traversal. Used in conjunction with GetNextTetra().
void vtkOrderedTriangulator::InitTetraTraversal()
{
  this->Mesh->CurrentTetra = this->Mesh->Tetras.begin();
}

//------------------------------------------------------------------------
// Retrieve a single tetra. Used in conjunction with InitTetraTraversal().
// Returns 0 when the list is exhausted.
int vtkOrderedTriangulator::GetNextTetra(int classification, vtkTetra *tet,
                                         vtkDataArray *cellScalars,
                                         vtkDoubleArray *tetScalars)
{
  // Find the next tetra with the right classification
  while ( this->Mesh->CurrentTetra != this->Mesh->Tetras.end() &&
          (*this->Mesh->CurrentTetra)->Type != classification &&
          (*this->Mesh->CurrentTetra)->Type != OTTetra::All )
    {
    ++this->Mesh->CurrentTetra;
    }

  if ( this->Mesh->CurrentTetra != this->Mesh->Tetras.end() )
    {
    OTTetra *tetra = *(this->Mesh->CurrentTetra);
    for (int i=0; i<4; i++)
      {
      tet->PointIds->SetId(i,tetra->Points[i]->Id);
      tet->Points->SetPoint(i,tetra->Points[i]->X);
      tetScalars->SetTuple(i,
                           cellScalars->GetTuple(
                             tetra->Points[i]->OriginalId));
      }
    ++this->Mesh->CurrentTetra;
    return 1;
    }
  else
    {
    return 0;
    }
}

//------------------------------------------------------------------------
// Add the tetras classified as specified to a list of point ids and
// point coordinates.
vtkIdType vtkOrderedTriangulator::AddTetras(int classification,
                                            vtkIdList *ptIds,
                                            vtkPoints *pts)
{
  TetraListIterator t;
  OTTetra *tetra;
  vtkIdType numTetras=0;
  int i;

  // loop over all tetras getting the ones with the classification requested
  for (t=this->Mesh->Tetras.begin(); t != this->Mesh->Tetras.end(); ++t)
    {
    tetra = *t;

    if ( tetra->Type == classification || classification == OTTetra::All)
      {
      numTetras++;
      for (i=0; i<4; i++)
        {
        ptIds->InsertNextId(tetra->Points[i]->Id);
        pts->InsertNextPoint(tetra->Points[i]->X);
        }
      }
    }//for all tetras

  return numTetras;
}


//------------------------------------------------------------------------
// Add the tetras classified as specified to an unstructured grid
vtkIdType vtkOrderedTriangulator::AddTetras(int classification,
                                            vtkUnstructuredGrid *ugrid)

{
  vtkIdType numTetras=0;
  TetraListIterator t;
  OTTetra *tetra;

  // loop over all tetras getting the ones with the classification requested
  vtkIdType pts[4];
  for (t=this->Mesh->Tetras.begin(); t != this->Mesh->Tetras.end(); ++t)
    {
    tetra = *t;

    if ( tetra->Type == classification || classification == OTTetra::All)
      {
      numTetras++;
      pts[0] = tetra->Points[0]->Id;
      pts[1] = tetra->Points[1]->Id;
      pts[2] = tetra->Points[2]->Id;
      pts[3] = tetra->Points[3]->Id;
      ugrid->InsertNextCell(VTK_TETRA,4,pts);
      }
    }//for all tetras

  return numTetras;
}

//------------------------------------------------------------------------
// Add the tetras classified as specified to a call array (connectivity list)
vtkIdType vtkOrderedTriangulator::AddTriangles(vtkCellArray *tris)
{
  vtkIdType numTris=0;
  int i;

  // Loop over all tetras examining each unvisited face. Faces whose
  // points are all classified "boundary" are added to the list of
  // faces.
  TetraListIterator t;
  OTTetra *tetra;
  OTFace *face = new(this->Heap) OTFace;

  // loop over all tetras getting the faces classified on the boundary
  for (t=this->Mesh->Tetras.begin(); t != this->Mesh->Tetras.end(); ++t)
    {
    tetra = *t;
    tetra->CurrentPointId = VTK_INT_MAX; //mark visited
    for (i=0; i<4; i++)
      {
      if ( tetra->Neighbors[i] &&
           tetra->Neighbors[i]->CurrentPointId != VTK_INT_MAX &&
           tetra->Type != tetra->Neighbors[i]->Type )
        {//face not yet visited
        tetra->GetFacePoints(i,face);
        numTris++;
        tris->InsertNextCell(3);
        tris->InsertCellPoint(face->Points[0]->Id);
        tris->InsertCellPoint(face->Points[1]->Id);
        tris->InsertCellPoint(face->Points[2]->Id);
        }
      }
    }//for all tetras

  return numTris;
}

//------------------------------------------------------------------------
// Add faces classified on the boundary to a cell array (connectivity list)
vtkIdType vtkOrderedTriangulator::AddTriangles(vtkIdType id, vtkCellArray *tris)
{
  vtkIdType numTris=0;
  int i;

  // Loop over all tetras examining each unvisited face. Faces whose
  // points are all classified "boundary" are added to the list of
  // faces.
  TetraListIterator t;
  OTTetra *tetra;
  OTFace *face = new(this->Heap) OTFace;

  // loop over all tetras getting the faces classified on the boundary
  for (t=this->Mesh->Tetras.begin(); t != this->Mesh->Tetras.end(); ++t)
    {
    tetra = *t;
    tetra->CurrentPointId = VTK_INT_MAX; //mark visited
    for (i=0; i<4; i++)
      {
      if ( tetra->Neighbors[i] &&
           tetra->Neighbors[i]->CurrentPointId != VTK_INT_MAX &&
           tetra->Type != tetra->Neighbors[i]->Type )
        {//face not yet visited
        tetra->GetFacePoints(i,face);
        if ( face->Points[0]->Id == id || face->Points[1]->Id == id ||
             face->Points[2]->Id == id )
          {
          numTris++;
          tris->InsertNextCell(3);
          tris->InsertCellPoint(face->Points[0]->Id);
          tris->InsertCellPoint(face->Points[1]->Id);
          tris->InsertCellPoint(face->Points[2]->Id);
          }
        }
      }
    }//for all tetras

  return numTris;
}

//---The following code supports templates----------------------------------
// Rather than predefining templates for the many possible triangulations, the
// ordered triangulator is used to generate the template which is then cached
// for later use. The key is that templates are uniquely characterized by a
// template id---a number representing a permutation of the sort of the
// original points.

//---Define template id type. Note: type must be 32 bits--------------------
// Currently the templates are set up for a maximum of eight point ids per
// cell maximum (this is due to the use of the vtkHexahedron). Any point can
// be exchanged with any of the other ids during the sort operation, so each
// exchange is represented with four bits as follows:
//
// +----+----+----+----+----+----+----+----+
// | p0 | p1 | p2 | p3 | p4 | p5 | p6 | p7 |
// +----+----+----+----+----+----+----+----+
//

//------------------------------------------------------------------------
// Given the results of the sorting, compute an index used to specify
// a template id.
inline TemplateIDType vtkOrderedTriangulator::ComputeTemplateIndex()
{
  static TemplateIDType mask[8]={0xF0000000,0x0F000000,0x00F00000,0x000F0000,
                                 0x0000F000,0x00000F00,0x000000F0,0x0000000F};

  int i;
  PointListIterator p;
  TemplateIDType templateID=0;

  for (p=this->Mesh->Points.begin(), i=0; i<this->NumberOfCellPoints; ++i, ++p)
    {
    templateID |= ((templateID & mask[i]) | (p->OriginalId << (32-4*(i+1))));
    }

  return templateID;
}


//------------------------------------------------------------------------
// If a template is missing, add it to the list of templates.
void vtkOrderedTriangulator::AddTemplate()
{
  // Find the template list for the given cell type
  int templateMayBeAvailable;
  TemplateList *tlist;
  TemplatesIterator titer = this->Templates->find(this->CellType);
  if ( titer != this->Templates->end() ) //something found
    {
    templateMayBeAvailable = 1;
    tlist = (*titer).second;
    }
  else //nothing found, have to create an entry for this cell type
    {
    templateMayBeAvailable = 0;
    tlist = new TemplateList;
    (*this->Templates)[this->CellType] = tlist;
    }

  // Create the template: its index and connectivity list
  TemplateIDType index = this->ComputeTemplateIndex();

  // Make sure template has not been created before
  TemplateListIterator tplate = tlist->find(index);
  if ( templateMayBeAvailable && tplate != tlist->end() )
    {
    vtkGenericWarningMacro(<<"Template found when it should not have been");
    }
  else
    {
    this->Mesh->NumberOfTemplates++;

    // The tetras have been classified previously. So allocate space
    // and add it as a template list.
    OTTemplate *otplate = new(this->TemplateHeap)
      OTTemplate(this->Mesh->NumberOfTetrasClassifiedInside,this->TemplateHeap);
    (*tlist)[index] = otplate;

    // Now fill in the connectivity list
    int i;
    TetraListIterator t;
    OTTetra *tetra;
    vtkIdType *clist=otplate->Tetras;
    for (t=this->Mesh->Tetras.begin(); t != this->Mesh->Tetras.end(); ++t)
      {
      if ( (*t)->Type == OTTetra::Inside )
        {
        tetra = *t;
        for (i=0; i<4; i++)
          {
          *clist++ = tetra->Points[i]->InsertionId;
          }
        }
      }//for all tetras
    }
}


//------------------------------------------------------------------------
// Use a template to create the triangulation. Return 0 if a template
// could not be used.
int vtkOrderedTriangulator::TemplateTriangulation()
{
  TemplatesIterator titer = this->Templates->find(this->CellType);
  if ( titer != this->Templates->end() ) //something found
    {
    TemplateIDType index = this->ComputeTemplateIndex();
    TemplateList *tlist = (*titer).second;
    TemplateListIterator tlistIter=tlist->find(index);
    if (  tlistIter != tlist->end() ) //something found
      {
      int i, j;
      OTTemplate *tets = (*tlistIter).second;
      vtkIdType numTets = tets->NumberOfTetras;
      vtkIdType *clist = tets->Tetras;
      OTTetra *tetra;
      for (i=0; i<numTets; i++)
        {
        tetra = new(this->Heap) OTTetra();
        this->Mesh->Tetras.push_front(tetra);
        tetra->Type = OTTetra::Inside;
        for (j=0; j<4; j++)
          {
          tetra->Points[j] = this->Mesh->Points.GetPointer(*clist++);
          }
        }//for all tetras in template
      return 1;
      }//if a template found
    }//if a template list for this cell type found

  return 0;
}


//------------------------------------------------------------------------
void vtkOrderedTriangulator::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);

  os << indent << "PreSorted: " << (this->PreSorted ? "On\n" : "Off\n");
  os << indent << "UseTwoSortIds: " << (this->UseTwoSortIds ? "On\n" : "Off\n");
  os << indent << "UseTemplates: " << (this->UseTemplates ? "On\n" : "Off\n");
  os << indent << "NumberOfPoints: " << this->NumberOfPoints << endl;

}