forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkOrderedTriangulator.cxx
1725 lines (1534 loc) · 54.7 KB
/
vtkOrderedTriangulator.cxx
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
/*=========================================================================
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 <vtkstd/list>
#include <vtkstd/vector>
#include <vtkstd/stack>
#include <vtkstd/map>
#include <assert.h>
vtkStandardNewMacro(vtkOrderedTriangulator);
#ifdef _WIN32_WCE
# ifndef __PLACEMENT_NEW_INLINE
# define __PLACEMENT_NEW_INLINE
inline void *__cdecl operator new(size_t, void *_P) { return (_P); }
# if _MSC_VER >= 1200
inline void __cdecl operator delete(void *, void *) { return; }
# endif
# endif
#else
# ifdef VTK_USE_ANSI_STDLIB
# include <new>
# else
# include <new.h>
# endif
#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 vtkstd::vector<OTPoint>
{
PointListType() : vtkstd::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 vtkstd::vector<OTFace*> FaceListType;
typedef vtkstd::vector<OTFace*>::iterator FaceListIterator;
//---Class represents a tetrahedron (and related typedefs)--------------------
typedef vtkstd::list<OTTetra*> TetraListType;
typedef vtkstd::list<OTTetra*>::iterator TetraListIterator;
struct TetraStackType : public vtkstd::stack<OTTetra*>
{
TetraStackType() : vtkstd::stack<OTTetra*>() {}
void clear() {while (!this->empty()) this->pop();}
};
typedef vtkstd::vector<OTTetra*> TetraQueueType;
typedef vtkstd::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 efficent 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 vtkstd::map<TemplateIDType,OTTemplate*> TemplateList;
typedef vtkstd::map<TemplateIDType,OTTemplate*>::iterator TemplateListIterator;
//
// Typedefs for a list of lists of templates keyed on cell type
struct vtkOTTemplates : public vtkstd::map<int,TemplateList*> {};
typedef vtkstd::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 choosen 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
int __cdecl vtkSortOnIds(const void *val1, const void *val2)
#else
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
int __cdecl vtkSortOnTwoIds(const void *val1, const void *val2)
#else
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 choosen 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;