forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkPolyhedron.cxx
3309 lines (2945 loc) · 96.2 KB
/
vtkPolyhedron.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: vtkPolyhedron.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 "vtkPolyhedron.h"
#include "vtkCellArray.h"
#include "vtkIdTypeArray.h"
#include "vtkDoubleArray.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkOrderedTriangulator.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkTetra.h"
#include "vtkTriangle.h"
#include "vtkQuad.h"
#include "vtkPolygon.h"
#include "vtkLine.h"
#include "vtkEdgeTable.h"
#include "vtkPolyData.h"
#include "vtkCellLocator.h"
#include "vtkGenericCell.h"
#include "vtkPointLocator.h"
#include "vtkMeanValueCoordinatesInterpolator.h"
#include "vtkSmartPointer.h"
#include "vtkMergePoints.h"
#include "vtkCellData.h"
#include "vtkDataArray.h"
#include "vtkType.h"
#include <vtkstd/map>
#include <vtkstd/vector>
#include <vtkstd/set>
#include <vtkstd/list>
#include <limits>
vtkStandardNewMacro(vtkPolyhedron);
// Special typedef
typedef vtkstd::vector<vtkIdType> vtkIdVectorType;
class vtkPointIdMap : public vtkstd::map<vtkIdType,vtkIdType>{};
class vtkIdToIdMapType : public vtkstd::map<vtkIdType, vtkIdType>{};
class vtkIdToIdVectorMapType : public vtkstd::map<vtkIdType, vtkIdVectorType>{};
typedef vtkstd::map<vtkIdType,vtkIdType*>::iterator PointIdMapIterator;
typedef vtkIdToIdVectorMapType::iterator vtkIdToIdVectorMapIteratorType;
typedef vtkstd::pair<vtkIdType, vtkIdVectorType> vtkIdToIdVectorPairType;
typedef vtkstd::pair<vtkIdType, vtkIdType> vtkIdToIdPairType;
typedef vtkstd::set<vtkIdType> vtkIdSetType;
// Special class for iterating through polyhedron faces
//----------------------------------------------------------------------------
class vtkPolyhedronFaceIterator
{
public:
vtkIdType CurrentPolygonSize;
vtkIdType *Polygon;
vtkIdType *Current;
vtkIdType NumberOfPolygons;
vtkIdType Id;
vtkPolyhedronFaceIterator(vtkIdType numFaces, vtkIdType *t)
{
this->CurrentPolygonSize = t[0];
this->Polygon = t;
this->Current = t+1;
this->NumberOfPolygons = numFaces;
this->Id = 0;
}
vtkIdType* operator++()
{
this->Current += this->CurrentPolygonSize + 1;
this->Polygon = this->Current - 1;
this->CurrentPolygonSize = this->Polygon[0];
this->Id++;
return this->Current;
}
};
// Special class for iterating through vertices on a polygon face
//----------------------------------------------------------------------------
class vtkPolygonVertexIterator
{
public:
vtkIdType *Current;
vtkIdType NumberOfVertices;
vtkIdType Id;
// 1 or 0 for iterating along its original direction or reverse
vtkIdType IterDirection;
vtkPolygonVertexIterator(vtkIdType numVertices, vtkIdType startVertex,
vtkIdType *startVertexPointer, vtkIdType nextVertex)
{
this->Current = startVertexPointer;
this->NumberOfVertices = numVertices;
this->Id = startVertex;
this->IterDirection = 1;
vtkIdType nextId = this->Id + 1;
vtkIdType *next = this->Current + 1;
if (nextId == this->NumberOfVertices)
{
next -= this->NumberOfVertices;
}
if (*next != nextVertex)
{
this->IterDirection = 0;
}
}
vtkIdType* operator++()
{
if (this->IterDirection)
{
this->Id++;
this->Current++;
if (this->Id == this->NumberOfVertices)
{
this->Id = 0;
this->Current -= this->NumberOfVertices;
}
}
else
{
this->Id--;
this->Current--;
if (this->Id == -1)
{
this->Id = this->NumberOfVertices - 1;
this->Current += this->NumberOfVertices;
}
}
return this->Current;
}
};
//----------------------------------------------------------------------------
class vtkPolyhedron::vtkInternal
{
public:
vtkIdTypeArray * FacesBackup;
vtkEdgeTable * EdgeTableBackup;
vtkInternal()
{
this->FacesBackup = NULL;
this->EdgeTableBackup = NULL;
}
~vtkInternal()
{
this->FacesBackup = NULL;
this->EdgeTableBackup = NULL;
}
//----------------------------------------------------------------------------
// Here we use a point merger to try to prevent the problem of duplicated
// points in the input.
void RemoveDuplicatedPointsFromFaceArrayAndEdgeTable(vtkPoints * points,
vtkIdTypeArray * & faces,
vtkEdgeTable * & edgeTable,
double *bounds)
{
const double eps = 0.000001;
vtkSmartPointer<vtkPoints> newPoints = vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkPointLocator> merge = vtkSmartPointer<vtkPointLocator>::New();
merge->SetTolerance(eps);
merge->InitPointInsertion(newPoints, bounds);
bool foundDupPoint = false;
vtkIdType pid = -1;
vtkIdToIdMapType pidMap0;
for (vtkIdType i = 0; i < points->GetNumberOfPoints(); i++)
{
if (!merge->InsertUniquePoint(points->GetPoint(i), pid))
{
foundDupPoint = true;
}
if (pidMap0.find(pid) == pidMap0.end())
{
pidMap0.insert(vtkIdToIdPairType(pid,i));
}
}
// update face array and edge table if necessary.
if (foundDupPoint)
{
vtkIdToIdMapType pidMap;
for (vtkIdType i = 0; i < points->GetNumberOfPoints(); i++)
{
pid = merge->IsInsertedPoint(points->GetPoint(i));
pidMap.insert(vtkIdToIdPairType(i, pidMap0.find(pid)->second));
}
this->FacesBackup = faces;
this->EdgeTableBackup = edgeTable;
vtkIdType nfaces = 0;
vtkIdType insertId = 0;
faces = vtkIdTypeArray::New();
faces->SetNumberOfTuples(points->GetNumberOfPoints()*10);
faces->InsertComponent(insertId++, 0, 0); // allocate space for nfaces
edgeTable = vtkEdgeTable::New();
edgeTable->InitEdgeInsertion(points->GetNumberOfPoints());
vtkPolyhedronFaceIterator
faceIter(this->FacesBackup->GetValue(0), this->FacesBackup->GetPointer(1));
while (faceIter.Id < faceIter.NumberOfPolygons)
{
vtkIdVectorType vVector;
for (vtkIdType i = 0; i < faceIter.CurrentPolygonSize; i++)
{
pid = pidMap.find(faceIter.Current[i])->second;
vVector.push_back(pid);
}
bool dupPointRemoved = true;
while (dupPointRemoved && vVector.size() > 2)
{
dupPointRemoved = false;
if (vVector[0] == vVector[vVector.size()-1])
{
vVector.erase(vVector.begin()+vVector.size()-1);
dupPointRemoved = true;
}
for (size_t i = 1; i < vVector.size(); i++)
{
if (vVector[i] == vVector[i-1])
{
vVector.erase(vVector.begin()+i);
dupPointRemoved = true;
}
}
}
if (vVector.size() < 3)
{
++faceIter;
continue;
}
nfaces++;
faces->InsertComponent(insertId++, 0, vVector.size());
for (size_t i = 0; i < vVector.size(); i++)
{
faces->InsertComponent(insertId++, 0, vVector[i]);
}
if (edgeTable->IsEdge(vVector[0],vVector[vVector.size()-1]) == (-1))
{
edgeTable->InsertEdge(vVector[0],vVector[vVector.size()-1]);
}
for (size_t i = 1; i < vVector.size(); i++)
{
if (edgeTable->IsEdge(vVector[i],vVector[i-1]) == (-1))
{
edgeTable->InsertEdge(vVector[i],vVector[i-1]);
}
}
++faceIter;
}
faces->SetComponent(0,0,nfaces);
}
else
{
this->FacesBackup = NULL;
this->EdgeTableBackup = NULL;
}
}
//----------------------------------------------------------------------------
// Here we use a point merger to try to prevent the problem of duplicated
// points in the input.
void RestoreFaceArrayAndEdgeTable(vtkIdTypeArray * & faces,
vtkEdgeTable * & edgeTable)
{
if (this->FacesBackup)
{
faces->Delete();
faces = this->FacesBackup;
}
if (this->EdgeTableBackup)
{
edgeTable->Delete();
edgeTable = this->EdgeTableBackup;
}
}
//----------------------------------------------------------------------------
// insert new id element in between two existing adjacent id elements.
// this is a convenient function. no check whether the input elements
// exist in the vector. no check for element adjacency.
int InsertNewIdToIdVector(vtkIdVectorType & idVector, vtkIdType id,
vtkIdType id0, vtkIdType id1)
{
if (idVector.size() < 2)
{
return 0;
}
size_t num = idVector.size();
if ((idVector[0] == id0 && idVector[num-1] == id1)
||(idVector[0] == id1 && idVector[num-1] == id0))
{
idVector.push_back(id);
return 1;
}
vtkIdVectorType::iterator iter = idVector.begin();
for (; iter != idVector.end(); ++iter)
{
if (*iter == id0 || *iter == id1)
{
++iter;
idVector.insert(iter, id);
return 1;
}
}
return 0;
};
// Convinient function used by clip. The id is the vector index of the positive
// point, id0 is the vector index of the start point, and id1 is the vector index
// of the end point.
//----------------------------------------------------------------------------
int EraseSegmentFromIdVector(vtkIdVectorType & idVector, vtkIdType id,
vtkIdType id0, vtkIdType id1)
{
// three possible cases
// first case: 0 -- id0 -- id -- id1 -- size-1
if (id0 < id && id < id1)
{
idVector.erase(idVector.begin() + id0 + 1, idVector.begin() + id1);
}
// second case: 0 -- id1 -- id0 -- id -- size-1
// third case: 0 -- id -- id1 -- id0 -- size-1
else if (id1 < id0 && (id0 < id || id < id1))
{
idVector.erase(idVector.begin() + id0 + 1, idVector.end());
idVector.erase(idVector.begin(), idVector.begin() + id1);
}
else
{
// we should never get here.
return 0;
}
return 1;
};
// convert the point ids from map.first to map.second
//----------------------------------------------------------------------------
int ConvertPointIds(vtkIdType npts, vtkIdType * pts,
vtkIdToIdMapType & map, vtkIdType reverse = 0)
{
for (vtkIdType i = 0; i < npts; i++)
{
vtkIdType id = reverse ? npts-1-i : i;
vtkIdToIdMapType::iterator iter = map.find(pts[id]);
if (iter == map.end())
{
return 0;
}
pts[id] = iter->second;
}
return 1;
};
//----------------------------------------------------------------------------
// The connected contour points are found by (1) locating the current
// contour point in the face loop, (2) looping through face point:
// meet a positive point, keep going.
// meet a contour point, store it and stop marching in this direction.
// meet a negative point, stop marching in this direction.
// meet the same point from both directions, stop.
// This loop may find zero, one or two connected contour points.
void FindConnectedContourPointsOnFace(vtkIdVectorType & facePtsVector,
vtkIdVectorType & faceContourPtsVec,
vtkIdType currContourPoint,
vtkIdVectorType & pointLabelVec,
vtkIdSetType & connectedContourPtsSet,
vtkIdSetType & unConnectedContourPtsSet)
{
vtkIdType numFacePoints = static_cast<vtkIdType>(facePtsVector.size());
if (numFacePoints < 3)
{
return;
}
if (faceContourPtsVec.size() < 2)
{
return;
}
// locate the id of the startContourPt inside the face loop
vtkIdType startPt = -1;
for (vtkIdType i = 0; i < numFacePoints; i++)
{
if (currContourPoint == facePtsVector[i])
{
startPt = i;
break;
}
}
if (startPt < 0 || startPt >= numFacePoints)
{
return;
}
vtkIdType leftEndPt = -1; // face loop index
vtkIdType rightEndPt = -1; // face loop index
vtkIdType leftEndPoint = -1; // point id
vtkIdType rightEndPoint = -1; // point id
vtkIdType leftEndPassPositivePoint = 0;
vtkIdType rightEndPassPositivePoint = 0;
// search in one direction.
vtkIdType endPt = startPt - 1;
for (; endPt != startPt; endPt--)
{
if (endPt < 0)
{
endPt = numFacePoints - 1;
if (endPt == startPt)
{
break;
}
}
if (pointLabelVec[facePtsVector[endPt]] == -1)//negative point reached. stop
{
break;
}
else if (pointLabelVec[facePtsVector[endPt]] == 0)//contour pt reached. stop
{
leftEndPt = endPt;
leftEndPoint = facePtsVector[endPt];
break;
}
else
{
leftEndPassPositivePoint = 1;
}
// positive pt reached. continue.
}
// check if already loop through the entire face
if (endPt != startPt)
{
vtkIdType prevEndPt = endPt;
// search in the other direction
for (endPt = startPt + 1; endPt != prevEndPt; endPt++)
{
if (endPt > numFacePoints - 1)
{
endPt = 0;
if (endPt == prevEndPt)
{
break;
}
if (endPt == startPt)
{
break;
}
}
if (pointLabelVec[facePtsVector[endPt]] == -1)//negative point reached. stop
{
break;
}
else if (pointLabelVec[facePtsVector[endPt]] == 0)//contour pt reached. stop
{
rightEndPt = endPt;
rightEndPoint = facePtsVector[endPt];
break;
}
else
{
rightEndPassPositivePoint = 1;
}
}
}
// need to check a special case where startPt, leftEndPoint and rightEndPoint
// are directly connected or connected by a series of other contour points,
// and startPt is at one end of the contour strip. We can check this situation
// using leftEndPassPositivePoint and leftEndPassPositivePoint. If both are
// 1, then the three points are not on a contour strip. If both are 0, then
// startPt is not at one end of the contour strip.
if (leftEndPoint >= 0 && rightEndPoint >=0 && leftEndPoint != rightEndPoint)
{
if (leftEndPassPositivePoint != rightEndPassPositivePoint)
{
bool foundNonContourPoint = false;
for (endPt = leftEndPt - 1; endPt != rightEndPt; endPt--)
{
if (endPt < 0)
{
endPt = numFacePoints - 1;
if (endPt == rightEndPt)
{
break;
}
}
if (pointLabelVec[facePtsVector[endPt]] != 0)
{
foundNonContourPoint = true;
break;
}
}
if (!foundNonContourPoint)// startPt on one end of the contour strip
{
if (leftEndPassPositivePoint)
{
leftEndPoint = -1;
}
else
{
rightEndPoint = -1;
}
}
}
}
if (leftEndPoint >= 0)
{
connectedContourPtsSet.insert(leftEndPoint);
}
if (rightEndPoint >= 0)
{
connectedContourPtsSet.insert(rightEndPoint);
}
for (size_t i = 0; i < faceContourPtsVec.size(); i++)
{
if (faceContourPtsVec[i] != leftEndPoint &&
faceContourPtsVec[i] != rightEndPoint &&
faceContourPtsVec[i] != currContourPoint)
{
unConnectedContourPtsSet.insert(faceContourPtsVec[i]);
}
}
};
//----------------------------------------------------------------------------
void RemoveIdFromIdToIdVectorMap(vtkIdToIdVectorMapType & map, vtkIdType id)
{
vtkIdToIdVectorMapIteratorType mit = map.begin();
for (; mit != map.end(); ++mit)
{
vtkIdVectorType::iterator vit = mit->second.begin();
for (; vit != mit->second.end(); ++vit)
{
if ((*vit) == id)
{
mit->second.erase(vit);
break;
}
}
}
};
//----------------------------------------------------------------------------
// For each contour point, extract its adjacent faces, then extract other
// contour points on the same face that can be connected to the current
// points.
// The connected contour points are found by (1) locating the current
// contour point in the face loop, (2) looping through face point:
// meet a positive point, keep going.
// meet a contour point, store it and stop marching in this direction.
// meet a negative point, stop marching in this direction.
// meet the same point from both directions, stop.
// This loop may find zero, one or two connected contour points.
int ExtractContourConnectivities(
vtkIdToIdVectorMapType & ceMap,
vtkIdSetType & cpSet,
vtkIdVectorType & pointLabelVector,
vtkIdToIdVectorMapType & pointToFacesMap,
vtkIdToIdVectorMapType & faceToPointsMap,
vtkIdToIdVectorMapType & faceToContourPointsMap)
{
int maxConnectivity = 0;
if (cpSet.empty())
{
return 0;
}
vtkIdSetType contourBranchesSet;
vtkIdSetType nonContourBranchesSet;
vtkIdVectorType contourBranchesVector;
vtkIdSetType::iterator cpSetIt;
vtkIdToIdVectorMapType::iterator fcpMapIt, fvMapIt, ceMapIt, ceMapIt1;
for (cpSetIt = cpSet.begin(); cpSetIt != cpSet.end(); /*manual increment*/)
{
contourBranchesSet.clear();
nonContourBranchesSet.clear();
contourBranchesVector.clear();
vtkIdType pid = *cpSetIt;
vtkIdVectorType fVector = pointToFacesMap.find(pid)->second;
for (size_t i = 0; i < fVector.size(); i++)
{
// find adjacent faces that contain contour points
fcpMapIt = faceToContourPointsMap.find(fVector[i]);
if (fcpMapIt == faceToContourPointsMap.end())
{
continue;
}
fvMapIt = faceToPointsMap.find(fVector[i]);
if (fvMapIt == faceToPointsMap.end())
{
cout << "Cannot find point ids of a face. We should never get "
"here. Contouring aborted." << endl;
return 0;
}
// find connected contour points and store them in the set. Notice that
// some weird topology will classify a point as a connected contour point
// in one face and a non-connected contour point in some other face. we
// will extract the union.
FindConnectedContourPointsOnFace(
fvMapIt->second, fcpMapIt->second, pid,
pointLabelVector, contourBranchesSet, nonContourBranchesSet);
}
if (!contourBranchesSet.empty())
{
vtkIdSetType::iterator ccpSetIt = contourBranchesSet.begin();
for (; ccpSetIt != contourBranchesSet.end(); ++ccpSetIt)
{
if (nonContourBranchesSet.find(*ccpSetIt) == nonContourBranchesSet.end())
{
contourBranchesVector.push_back(*ccpSetIt);
}
}
}
if (contourBranchesVector.size() >= 2)
{
ceMap.insert(
vtkIdToIdVectorPairType(pid, contourBranchesVector));
++cpSetIt;
}
else // throw away point contour or edge contour.
{
if (cpSetIt != cpSet.begin())
{
vtkIdSetType::iterator tempIt = cpSetIt;
--cpSetIt;
cpSet.erase(tempIt);
++cpSetIt;
}
else
{
cpSet.erase(cpSetIt);
cpSetIt = cpSet.begin();
}
}
}
// sanity check, all edges should be listed twice
for (ceMapIt = ceMap.begin(); ceMapIt != ceMap.end(); ++ceMapIt)
{
vtkIdVectorType edges = ceMapIt->second;
for (size_t i = 0; i < edges.size(); i++)
{
bool foundMatch = false;
ceMapIt1 = ceMap.find(edges[i]);
if (ceMapIt1 != ceMap.end())
{
for (size_t j = 0; j < ceMapIt1->second.size(); j++)
{
if (ceMapIt->first == ceMapIt1->second[j])
{
foundMatch = true;
break;
}
}
}
if (!foundMatch)
{
edges.erase(edges.begin()+i);
i--;
}
}
ceMapIt->second = edges;
}
// clean 0 or 1-connected contour from ceMap
for (ceMapIt = ceMap.begin(); ceMapIt != ceMap.end(); /*manual increment*/)
{
if (ceMapIt->second.size() >= 2)
{
++ceMapIt;
continue;
}
cpSetIt = cpSet.find(ceMapIt->first);
if (cpSetIt != cpSet.end())
{
cpSet.erase(cpSetIt);
}
if (ceMapIt != ceMap.begin())
{
vtkIdToIdVectorMapType::iterator tempIt = ceMapIt;
--ceMapIt;
ceMap.erase(tempIt);
++ceMapIt;
}
else
{
ceMap.erase(ceMapIt);
ceMapIt = ceMap.begin();
}
}
// set maxConnectivity.
for (ceMapIt = ceMap.begin(); ceMapIt != ceMap.end(); ++ceMapIt)
{
if (static_cast<int>(ceMapIt->second.size()) > maxConnectivity)
{
maxConnectivity = static_cast<int>(ceMapIt->second.size());
}
}
return maxConnectivity;
};
//----------------------------------------------------------------------------
// Use eigenvalues to determine the dimension of the input contour points.
// This chunk of code is mostly copied from vtkOBBTree::ComputeOBB()
// Function return 0 if input is a single point, 1 if co-linear,
// 2 if co-planar, 3 if 3D. It also returns the center as well as the normal
// (the eigenvector with the smallest eigenvalue) of the input contour pointset.
int CheckContourDimensions(vtkPoints* points, vtkIdType npts, vtkIdType * ptIds,
double * normal, double * center)
{
static const double eigenvalueRatioThresh = 0.001;
if (npts < 3)
{
return npts - 1;
}
vtkIdType i, j;
double x[3], mean[3], xp[3], *v[3], v0[3], v1[3], v2[3];
double *a[3], a0[3], a1[3], a2[3], eigValue[3];
// Compute mean
mean[0] = mean[1] = mean[2] = 0.0;
for (i=0; i < npts; i++ )
{
points->GetPoint(ptIds[i], x);
mean[0] += x[0];
mean[1] += x[1];
mean[2] += x[2];
}
for (i=0; i < 3; i++)
{
mean[i] /= npts;
}
// Compute covariance matrix
a[0] = a0; a[1] = a1; a[2] = a2;
for (i=0; i < 3; i++)
{
a0[i] = a1[i] = a2[i] = 0.0;
}
for (j = 0; j < npts; j++ )
{
points->GetPoint(ptIds[j], x);
xp[0] = x[0] - mean[0]; xp[1] = x[1] - mean[1]; xp[2] = x[2] - mean[2];
for (i = 0; i < 3; i++)
{
a0[i] += xp[0] * xp[i];
a1[i] += xp[1] * xp[i];
a2[i] += xp[2] * xp[i];
}
}//for all points
for (i=0; i < 3; i++)
{
a0[i] /= npts;
a1[i] /= npts;
a2[i] /= npts;
}
// Extract axes (i.e., eigenvectors) from covariance matrix.
v[0] = v0; v[1] = v1; v[2] = v2;
vtkMath::Jacobi(a,eigValue,v);
int ret = 3;
if ((eigValue[2] / eigValue[0]) < eigenvalueRatioThresh)
{
ret--;
}
if ((eigValue[1] / eigValue[0]) < eigenvalueRatioThresh)
{
ret--;
}
if (normal)
{
for (i =0; i < 3; i++)
{
double norm = vtkMath::Norm(a[i], 3);
if (norm > 0.000001)
{
break;
}
}
if (i < 3)
{
normal[0] = v2[0];
normal[1] = v2[1];
normal[2] = v2[2];
}
else
{
points->GetPoint(ptIds[0], v0);
points->GetPoint(ptIds[1], v1);
v0[0] = v0[0] - mean[0];
v0[1] = v0[1] - mean[1];
v0[2] = v0[2] - mean[2];
v1[0] = v1[0] - mean[0];
v1[1] = v1[1] - mean[1];
v1[2] = v1[2] - mean[2];
vtkMath::Normalize(v0);
vtkMath::Normalize(v1);
vtkMath::Cross(v0, v1, normal);
vtkMath::Normalize(normal);
}
}
if (center)
{
center[0] = mean[0];
center[1] = mean[1];
center[2] = mean[2];
}
return ret;
};
//----------------------------------------------------------------------------
// For each contour point, compute the normal (pointing to the positive side),
// then sort the other contour points connected to it, such that the connecting
// edges are ordered contour-clockwise when viewed from the normal direction.
// Input ceMap shows that a contour point (map->first) is connected to a number
// of other contour points (map->second). It does not distinguish boundary
// edges from internal edges. The following function also update ceMap such that
// a boundary edge a-->b (assuming traversing from the counter-clockwise
// direction) is only stored once ({a, [b, ...]}). an internal edge a<-->b is
// stored twice ({a, [b, ...] and {b, [a, ...]}}.
// Current implementation of this function assumes planar contours, we only
// compute normal once and reuse it for all other contour points.
// TODO: for non-planar cut, need to compute normal for each contour point. We
// then project edges onto a tangent plane and sort them.
void OrderMultiConnectedContourPoints(vtkIdToIdVectorMapType & cpMap,
vtkIdToIdVectorMapType & cpBackupMap,
vtkIdSetType & cpSet,
vtkPoints * points)
{
double o[3], p[3], x0[3], x1[3], e0[3], e1[3], n[3], nn[3];
vtkIdSetType::iterator setIt;
vtkIdVectorType pids;
for (setIt = cpSet.begin(); setIt != cpSet.end(); ++setIt)
{
pids.push_back(*setIt);
}
// return if the input contour points are 1D. Note: the function also
// compute normal n and center c.
if (CheckContourDimensions(
points, static_cast<vtkIdType>(pids.size()), &(pids[0]), n, o) < 2)
{
return;
}
vtkMath::Normalize(n);
// locate an extreme point in a direction normal to the normal. this
// extreme point is a convex vertex.
vtkIdToIdVectorMapType::iterator mapIt = cpMap.begin();
points->GetPoint(mapIt->first, p);
e0[0] = p[0] - o[0];
e0[1] = p[1] - o[1];
e0[2] = p[2] - o[2];
vtkMath::Normalize(e0);
vtkMath::Cross(e0, n, nn);
vtkMath::Normalize(nn);
double maxDistance = VTK_DOUBLE_MIN;
vtkIdType maxPid = -1;
for (; mapIt != cpMap.end(); ++mapIt)
{
points->GetPoint(mapIt->first, p);
e0[0] = p[0] - o[0];
e0[1] = p[1] - o[1];
e0[2] = p[2] - o[2];
double distance = vtkMath::Dot(nn, e0);
if (distance > maxDistance)
{
maxDistance = distance;
maxPid = mapIt->first;
}
}
// Order edges of the contour point contour-clockwise. Note that a boundary
// point has two boundary edges. We will remove the incoming boundary edge
// and store the outgoing boundary edge at the end (after all internal edges).
// incoming and outgoing boudnary edges are defined when they are traversed
// counter-clockwisely.
std::vector<double> extremePointAngles; // record the angles of extreme point
vtkIdVectorType edges;
size_t edgesSize = 0;
const double eps = 0.0000001;
for (mapIt = cpMap.begin(); mapIt != cpMap.end(); ++mapIt)
{
edges = mapIt->second;
edgesSize = edges.size();
// If the contour point is 2-connected we don't need to order them.
if (edgesSize >=3 || mapIt->first == maxPid)
{
// get the current first edge
points->GetPoint(mapIt->first, p);
points->GetPoint(edges[0], x0);
e0[0] = x0[0] - p[0];
e0[1] = x0[1] - p[1];
e0[2] = x0[2] - p[2];
vtkMath::Normalize(e0);
vtkMath::Cross(e0, n, x0);
vtkMath::Cross(n, x0, e0);
vtkMath::Normalize(e0);
// compute the angles from other edges to the first edge
std::vector<double> angles;
angles.push_back(0);
const double maxDotProduct = 0.95;
for (size_t i = 1; i < edgesSize; i++)
{
points->GetPoint(edges[i], x1);
e1[0] = x1[0] - p[0];
e1[1] = x1[1] - p[1];
e1[2] = x1[2] - p[2];
vtkMath::Normalize(e1);
vtkMath::Cross(e1, n, x1);
vtkMath::Cross(n, x1, e1);
vtkMath::Normalize(e1);
double dotproduct = vtkMath::Dot(e0, e1);
double angle = acos(dotproduct);
if (dotproduct < maxDotProduct && dotproduct > -maxDotProduct)
{
vtkMath::Cross(e0, e1, nn);
if (vtkMath::Dot(n, nn) < 0)
{
angle = 2.0*vtkMath::Pi() - angle;
}
}
else if (dotproduct > maxDotProduct)
{
vtkMath::Cross(e0, n, nn);
angle = acos(vtkMath::Dot(nn, e1)) - vtkMath::Pi()/2.0;
}
else if (dotproduct < -maxDotProduct)
{
vtkMath::Cross(n, e0, nn);
angle = acos(vtkMath::Dot(nn, e1)) + vtkMath::Pi()/2.0;
}
if (angle < -eps)
{
angle += 2.0*vtkMath::Pi();
}
if (angle > 2.0*vtkMath::Pi()+eps)
{
angle -= 2.0*vtkMath::Pi();
}
angles.push_back(angle);
}
// sort edges
for (size_t i = 1; i < edgesSize-1; i++)
{
for (size_t j = i+1; j < edgesSize; j++)
{
if (angles[i] > angles[j])
{
vtkIdType temp = edges[i];
edges[i] = edges[j];
edges[j] = temp;