-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathIdentifyStructures.cxx
1472 lines (1244 loc) · 48.5 KB
/
IdentifyStructures.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
/*
* =====================================================================================
*
* Filename: IdentifyStructures.cxx
*
* Description: Finds Coherent Structures
*
* Version: 1.0
* Created: 02/14/2014 12:21:46 AM
* Revision: none
* Compiler: gcc
*
* Author: Siavash Ameli
* Organization: University Of California, Berkeley
*
* =====================================================================================
*/
// =======
// Headers
// =======
#include "IdentifyStructures.h"
#include "SmoothStructuredPoints.h"
// Pipeline
#include <vtkInformation.h>
#include <vtkInformationVector.h>
#include <vtkDataObject.h>
#include <vtkObjectFactory.h>
#include <vtkDemandDrivenPipeline.h>
// Data
#include <vtkDataSet.h>
#include <vtkStructuredData.h> // for vtkStructuredData::ComputePointId
#include <vtkStructuredPoints.h>
#include <vtkPointData.h>
#include <vtkPolyData.h>
#include "PointDataType.h"
// Elements
// #include <vtkCellIterator.h>
#include <vtkCell.h>
#include <vtkGenericCell.h>
#include <vtkCellArray.h>
#include <vtkTriangle.h>
// Arrays
#include <vtkIdList.h>
#include <vtkDataArray.h>
#include <vtkDataArrayCollection.h>
#include <vtkDoubleArray.h>
// General
#include <vtkSmartPointer.h>
#include <vtkCallbackCommand.h>
#include <vector>
#include <algorithm> // for std::sort
#include <cmath> // for sqrt
#include <stdlib.h> // for std::div
#include <sstream> // for stringstream
#ifdef _OPENMP
#include <omp.h>
#endif
// Algorithms
#include <vtkGradientFilter.h>
#include <vtkPolyDataNormals.h>
#include <vtkMath.h>
#include "CubeCell.h"
// Debug
#include <time.h>
// ======
// Macros
// ======
vtkStandardNewMacro(IdentifyStructures);
vtkCxxRevisionMacro(IdentifyStructures,"$Revision 1.0$");
#define DIMENSION 3
#define CUBESIZE 8
// ===========
// Constructor
// ===========
IdentifyStructures::IdentifyStructures()
{
// Default member data
this->StructureMode = StructureType::MAX_STRAIN;
this->SmoothingStatus = true;
this->SmoothingKernelSize = 7;
this->DebugStatus = false;
this->ProgressStatus = false;
// Callbacks
vtkSmartPointer<vtkCallbackCommand> ProgressCallback = vtkSmartPointer<vtkCallbackCommand>::New();
ProgressCallback->SetCallback(this->ProgressFunction);
this->AddObserver(vtkCommand::ProgressEvent,ProgressCallback);
}
// ==========
// Destructor
// ==========
IdentifyStructures::~IdentifyStructures()
{
}
// ====================
// Accessors / Mutators
// ====================
// Set Structure Mode
void IdentifyStructures::SetStructureMode(int InputStructureMode)
{
switch(InputStructureMode)
{
// Max Strain
case static_cast<int>(StructureType::MAX_STRAIN):
{
this->StructureMode = StructureType::MAX_STRAIN;
break;
}
// Min Strain
case static_cast<int>(StructureType::MIN_STRAIN):
{
this->StructureMode = StructureType::MIN_STRAIN;
break;
}
// Shear
case static_cast<int>(StructureType::SHEAR):
{
this->StructureMode = StructureType::SHEAR;
break;
}
// Undefined mode
default:
{
vtkErrorMacro("Structure mode is undefined.");
}
}
}
// Debug On
void IdentifyStructures::DebugOn()
{
this->DebugStatus = true;
}
// Debug Off
void IdentifyStructures::DebugOff()
{
this->DebugStatus = false;
}
// ==========
// Print Self
// ==========
void IdentifyStructures::PrintSelf(ostream &os,vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
// ============================
// Fill Output Port Information
// ============================
int IdentifyStructures::FillOutputPortInformation(int port,vtkInformation *info)
{
if(port == 0)
{
info->Set(vtkDataObject::DATA_TYPE_NAME(),"vtkPolyData");
return 1;
}
return 0;
}
// ===================
// Request Data Object
// ===================
int IdentifyStructures::RequestDataObject(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **vtkNotUsed(InputInfo),
vtkInformationVector *OutputVector)
{
vtkInformation *OutputInfo = OutputVector->GetInformationObject(0);
vtkPolyData *OutputPolyData = vtkPolyData::SafeDownCast(OutputInfo->Get(vtkDataObject::DATA_OBJECT()));
// Create new instance
if(OutputPolyData == NULL)
{
OutputPolyData = vtkPolyData::New();
OutputInfo->Set(vtkDataObject::DATA_OBJECT(),OutputPolyData);
OutputPolyData->FastDelete();
this->GetOutputPortInformation(0)->Set(vtkDataObject::DATA_EXTENT_TYPE(),OutputPolyData->GetExtentType());
}
return 1;
}
// =================
// Progress Function
// =================
void IdentifyStructures::ProgressFunction(
vtkObject *Caller,
unsigned long int vtkNotUsed(EventId),
void *vtkNotUsed(ClientData),
void *vtkNotUsed(CallData))
{
IdentifyStructures *Filter = IdentifyStructures::SafeDownCast(Caller);
int ProgressValuePercent = 10*floor(0.5+10*Filter->GetProgress());
if(Filter->GetProgressStatus() == true && ProgressValuePercent >= 10)
{
std::cout << "IdentifyStructures Filter Progress: "
<< std::setw(3) << std::right
<< ProgressValuePercent << " %"<< std::endl;
}
}
// ======================
// Filter Update Progress
// ======================
void IdentifyStructures::FilterUpdateProgress(
unsigned int Step,
unsigned int NumberOfSteps)
{
unsigned int ProgressPercentIncrement = 10;
// Update Progress
if(this->ProgressStatus == true)
{
if(Step % static_cast<int>(NumberOfSteps/ProgressPercentIncrement) == 0 ||
Step == NumberOfSteps-1)
{
double ProgressValue = static_cast<double>(Step)/(NumberOfSteps-1);
this->UpdateProgress(ProgressValue);
}
}
}
// ============
// Request Data
// ============
int IdentifyStructures::RequestData(
vtkInformation *vtkNotUsed(Request),
vtkInformationVector **InputVector,
vtkInformationVector *OutputVector)
{
// Input
vtkInformation *InputInfo = InputVector[0]->GetInformationObject(0);
vtkDataObject *InputDataObject = InputInfo->Get(vtkDataObject::DATA_OBJECT());
vtkDataSet *InputDataSet = vtkDataSet::SafeDownCast(InputDataObject);
// Output
vtkInformation *OutputInfo = OutputVector->GetInformationObject(0);
vtkDataObject *OutputDataObject = OutputInfo->Get(vtkDataObject::DATA_OBJECT());
vtkPolyData *OutputPolyData = vtkPolyData::SafeDownCast(OutputDataObject);
// Define Deformation Value and Vector
vtkDoubleArray *DeformationValue = NULL;
vtkDoubleArray *DeformationVector = NULL;
// Get Deformation Value and Vector from InputDataSet
this->GetDeformationValueAndVector(InputDataSet,DeformationValue,DeformationVector);
// Input Grid Resolution
vtkStructuredPoints *InputStructuredPoints = vtkStructuredPoints::SafeDownCast(InputDataSet);
int *GridResolution = InputStructuredPoints->GetDimensions();
// Smooth Deformation Vectors
// if(this->SmoothingStatus == true)
// {
// SmoothStructuredPoints::SelfSmoothUnitVectorField(
// this->SmoothingKernelSize,
// GridResolution,
// DeformationVector);
// }
// Compute Gradient of Deformation Value
vtkSmartPointer<vtkDoubleArray> DeformationValueGradient = vtkSmartPointer<vtkDoubleArray>::New();
if(this->SmoothingStatus == true)
{
SmoothStructuredPoints::SmoothGradientOfScalarField(
this->SmoothingKernelSize,
GridResolution,
DeformationValue,
DeformationValueGradient);
}
else
{
// this->ComputeDeformationValueGradient(
// InputDataSet,
// DeformationValue,
// DeformationValueGradient); // Output
this->ComputeDeformationValueGradient2(
InputDataSet,
DeformationValueGradient); // Output
}
// Find Manifold Structures
this->FindManifoldStructures(
InputDataSet,
DeformationValue,
DeformationValueGradient,
DeformationVector,
OutputPolyData); // Output
// Add normals to the PolyData
this->AddNormalsArray(OutputPolyData);
// Compute and Add DeformationVector Deviation
this->AddDeformationVectorDeviation(OutputPolyData);
return 1;
}
// ================================
// Get Deformation Value And Vector
// ================================
// Description:
// Pointer to DeformationValue and DeformationVectors are declared outside the function. But
// they will be changed in this function to point to arrays in InputDataSet. So we need to
// "pass pointers by reference". Otherwise they will be null again outside this function.
void IdentifyStructures::GetDeformationValueAndVector(
vtkDataSet *InputDataSet,
vtkDoubleArray *& DeformationValue, // Output
vtkDoubleArray *& DeformationVector) // Output
{
// Get Point Data
vtkPointData *InputPointData = InputDataSet->GetPointData();
// Assign Deformation variables based on Structure mode to be identified
switch(this->StructureMode)
{
// Max Strain
case StructureType::MAX_STRAIN:
{
DeformationValue = vtkDoubleArray::SafeDownCast(InputPointData->GetArray("MaxStrainValues"));
DeformationVector = vtkDoubleArray::SafeDownCast(InputPointData->GetArray("MaxStrainVectors"));
break;
}
// Min Strain
case StructureType::MIN_STRAIN:
{
DeformationValue = vtkDoubleArray::SafeDownCast(InputPointData->GetArray("MinStrainValues"));
DeformationVector = vtkDoubleArray::SafeDownCast(InputPointData->GetArray("MinStrainVectors"));
break;
}
// Undefined Structure mode
default:
{
vtkErrorMacro("The Structure mode is undefined.");
}
}
// Check DeformationValue Array
if(DeformationValue == NULL)
{
vtkErrorMacro("Deformation Value array is NULL.");
}
else if(DeformationValue->GetNumberOfTuples() < 2)
{
vtkErrorMacro("DeformationValue array has no tuples.");
}
// Check DeformationVector Array
if(DeformationVector == NULL)
{
vtkErrorMacro("Deformation Vector Array is NULL.");
}
else if(DeformationVector->GetNumberOfTuples() < 2)
{
vtkErrorMacro("Deformation Vector array has no tuples.");
}
}
// ====================================
// Compute Deformation Value Gradient 2
// ====================================
void IdentifyStructures::ComputeDeformationValueGradient2(
vtkDataSet *InputDataSet,
vtkDoubleArray *DeformationValueGradient) // Output
{
// Declare Deformation Value array name
char DeformationValueArrayName[256];
// Get Deformation Value
switch(this->StructureMode)
{
// Max Strain
case StructureType::MAX_STRAIN:
{
strcpy(DeformationValueArrayName,"MaxStrainValues");
break;
}
// Min Strain
case StructureType::MIN_STRAIN:
{
strcpy(DeformationValueArrayName,"MinStrainValues");
break;
}
// Shear
case StructureType::SHEAR:
{
strcpy(DeformationValueArrayName,"ShearValues");
break;
}
// Undefined Structure type
default:
{
vtkErrorMacro("Structure type is undefined.");
}
}
// use vtk's Gradient Filter
vtkSmartPointer<vtkGradientFilter> GradientFilter = vtkSmartPointer<vtkGradientFilter>::New();
#if VTK_MAJOR_VERSION <= 5
GradientFilter->SetInput(InputDataSet);
#else
GradientFilter->SetInputData(InputDataSet);
#endif
GradientFilter->SetInputArrayToProcess(0,0,0,vtkDataObject::FIELD_ASSOCIATION_POINTS,DeformationValueArrayName);
GradientFilter->Update();
// Get the output dataset of GradientFilter
vtkSmartPointer<vtkDataSet> OutputDataSet = GradientFilter->GetOutput();
// Set output of the function
DeformationValueGradient->DeepCopy(vtkDoubleArray::SafeDownCast(OutputDataSet->GetPointData()->GetVectors("Gradients")));
// Check Deformation Value Gradient
if(DeformationValueGradient == NULL)
{
vtkErrorMacro("Deformation value gradient is NULL.");
}
else if(DeformationValueGradient->GetNumberOfTuples() < 1)
{
vtkErrorMacro("DeformationValueGradient does not have any tuples.");
}
}
// ==================================
// Compute Deformation Value Gradient
// ==================================
void IdentifyStructures::ComputeDeformationValueGradient(
vtkDataSet *InputDataSet,
vtkDoubleArray *DeformationValue,
vtkDoubleArray *DeformationValueGradient) // Output
{
// Check Input DataSet
if(InputDataSet == NULL)
{
vtkErrorMacro("InputDataSet is NULL.");
}
else if(InputDataSet->GetNumberOfPoints() < 1)
{
vtkErrorMacro("InputDataSet has no points.");
}
// Check DeformationValue
if(DeformationValue == NULL)
{
vtkErrorMacro("DeformationValue is NULL.");
}
else if(DeformationValue->GetNumberOfTuples() < 2 ||
DeformationValue->GetNumberOfComponents() < 1)
{
vtkErrorMacro("DeformationValues does not have data.");
}
// Cast input to structured points
vtkSmartPointer<vtkStructuredPoints> InputStructuredPoints = vtkStructuredPoints::SafeDownCast(InputDataSet);
// Check casting
if(InputStructuredPoints == NULL)
{
vtkErrorMacro("InputStructuredGrid is NULL.");
}
else if(InputStructuredPoints->GetNumberOfPoints() < 1)
{
vtkErrorMacro("InputStructuredGrid has no points.");
}
// Structured Points info
unsigned int NumberOfPoints = InputDataSet->GetNumberOfPoints();
int *GridResolution = InputStructuredPoints->GetDimensions();
// Set the DeformationValueGradient array
DeformationValueGradient->SetNumberOfComponents(DIMENSION);
DeformationValueGradient->SetNumberOfTuples(NumberOfPoints);
DeformationValueGradient->SetName("DeformationValueGradient");
// Loop over points
for(unsigned int PointId = 0; PointId < NumberOfPoints; PointId++)
{
// declare Gradient at point
double Gradient[DIMENSION];
// Loop over dimension
for(unsigned int DimensionIterator = 0; DimensionIterator < DIMENSION; DimensionIterator++)
{
// Get Stencil Ids in the current dimension
vtkIdType StencilIds[2];
this->GetStencilIds(GridResolution,PointId,DimensionIterator,StencilIds);
// Get DeformationValue on 1D stencil
double BackValue = DeformationValue->GetTuple1(StencilIds[0]);
double FrontValue = DeformationValue->GetTuple1(StencilIds[1]);
// Get Positions on 1D stencil
double BackPoint[DIMENSION];
double FrontPoint[DIMENSION];
InputDataSet->GetPoint(StencilIds[0],BackPoint);
InputDataSet->GetPoint(StencilIds[1],FrontPoint);
// Compute Gradient in specific direction
Gradient[DimensionIterator] = (FrontValue - BackValue) /
(FrontPoint[DimensionIterator] - BackPoint[DimensionIterator]);
}
// Set gradient to output array
DeformationValueGradient->SetTuple(PointId,Gradient);
}
}
// ===============
// Get Point Index
// ===============
// Decsription:
// Converts the list style ID if a point into coordinate-wise indices if the point
// in a structured grid. Grid Resolution is number of points in each direction.
// Note: PointId is 0-offset. So the first point is 0. Therefore, the PointIndex are
// also offset from 0. So the first point is (0,0,0) in 3D.
void IdentifyStructures::GetPointIndex(
int *GridResolution,
vtkIdType PointId,
unsigned int *PointIndex) // Output
{
for(unsigned int DimensionIterator = 0; DimensionIterator < DIMENSION; DimensionIterator++)
{
div_t DivisionResult;
DivisionResult = std::div(static_cast<int>(PointId),GridResolution[DimensionIterator]);
PointIndex[DimensionIterator] = DivisionResult.rem;
PointId = DivisionResult.quot;
}
}
// ============
// Get Point Id
// ============
// Description:
// Given coordinate index for a point, it returns the point Id in structured grid.
// All components of PointIndex are offset form 0. So the output PointId will be
// offset from 0. GridResolution is number of points at each direction.
inline vtkIdType IdentifyStructures::GetPointId(
int *GridResolution,
vtkIdType *PointIndex)
{
vtkIdType PointId = 0;
unsigned int DimensionProduct = 1;
for(unsigned int DimensionIterator = 0; DimensionIterator < DIMENSION; DimensionIterator++)
{
PointId += DimensionProduct * PointIndex[DimensionIterator];
DimensionProduct *= GridResolution[DimensionIterator];
}
return PointId;
}
// ===============
// Get Stencil Ids
// ===============
// Description:
// Finds the point Ids of stencil neighbors of a point on a structured grid.
// Note: If point is on the boundary, it considers the point itself instead of a stencil outdside
// the domain. Thus the central point duplicates.
// Note: The stencil is only two points in a specific direction. Direction = 0 returns the two
// stencilpoints in x-axis, Direction = 1 returns the two stencils in y-axis, etc.
// Output is an array with two elements as following
// StencilIds[0] is the back point Id.
// StencilIds[1] is the front point Id.
void IdentifyStructures::GetStencilIds(
int *GridResolution,
vtkIdType PointId,
unsigned int TargetDirection,
vtkIdType *StencilIds) // Output
{
// Get point index
unsigned int PointIndex[DIMENSION];
this->GetPointIndex(GridResolution,PointId,PointIndex);
// Stencils Index
int BackStencilIndex[DIMENSION];
int FrontStencilIndex[DIMENSION];
for(unsigned int DimensionIterator = 0; DimensionIterator < DIMENSION; DimensionIterator++)
{
// Initialize Stencil indices the same as point index
BackStencilIndex[DimensionIterator] = PointIndex[DimensionIterator];
FrontStencilIndex[DimensionIterator] = PointIndex[DimensionIterator];
// Set stencil indices in the requested direction
if(DimensionIterator == TargetDirection)
{
// Check right boundaries for front stencil
if(PointIndex[TargetDirection] < static_cast<unsigned int>(GridResolution[TargetDirection]-1))
{
FrontStencilIndex[TargetDirection]++;
}
// Check left boundaries for back stencil
if(PointIndex[TargetDirection] > 0)
{
BackStencilIndex[TargetDirection]--;
}
}
}
// Convert Stencil index to list Id
StencilIds[0] = vtkStructuredData::ComputePointId(GridResolution,BackStencilIndex); // Back stencil
StencilIds[1] = vtkStructuredData::ComputePointId(GridResolution,FrontStencilIndex); // Front stencil
}
// ======================
// Normalize Vector Array
// ======================
// Description:
// This filter over-writes the anchoral array.
// This operates on tuple vectors of vtkDoubleArray.
void IdentifyStructures::NormalizeVectorArray(vtkDoubleArray *VectorArray)
{
// Check Input
if(VectorArray == NULL)
{
vtkErrorMacro("VectorArray is NULL.");
exit(0);
}
else if(VectorArray->GetNumberOfTuples() < 1)
{
vtkErrorMacro("VectroArray does not have any tuples.");
exit(0);
}
// Number of Tuples
unsigned int NumberOfTuples = VectorArray->GetNumberOfTuples();
// Loop over tuples
for(unsigned int TupleIterator = 0; TupleIterator < NumberOfTuples; TupleIterator++)
{
// Get a pointer to the tuple
double *Tuple = VectorArray->GetTuple(TupleIterator);
// Normalize tuple
vtkMath::Normalize(Tuple);
// Set normalized tuples again to the array (this is necessary)
VectorArray->SetTuple(TupleIterator,Tuple);
}
}
// ========================
// Find Manifold Structures
// ========================
// Description:
// This is the main method of this class.
// Note: This method works on Structured Points dataset.
void IdentifyStructures::FindManifoldStructures(
vtkDataSet *InputDataSet,
vtkDoubleArray *DeformationValue,
vtkDoubleArray *DeformationValueGradient,
vtkDoubleArray *DeformationVector,
vtkPolyData *OutputPolyData)
{
// Check Input DataSet
if(InputDataSet == NULL)
{
vtkErrorMacro("Input DataSet is NULL.");
}
else if(InputDataSet->GetNumberOfCells() < 1)
{
vtkErrorMacro("Input DataSet does not have cell.");
}
// Declare outputs of this function
PointsListType IsoSurfacePointsList;
TrianglesListType IsoSurfaceTrianglesList;
// Get Grid Resolution
vtkSmartPointer<vtkStructuredPoints> InputStructuredPoints = vtkStructuredPoints::SafeDownCast(InputDataSet);
int *GridResolution = InputStructuredPoints->GetDimensions();
// Define an iterator object for cells
// vtkSmartPointer<vtkCellIterator> CellIterator = InputDataSet->NewCellIterator();
unsigned int NumberOfCells = InputDataSet->GetNumberOfCells(); // CHANGED
unsigned int ProgressCounter = 0;
// Iterate over cubes
// for(CellIterator->InitTraversal(); CellIterator->IsDoneWithTraversal(); CellIterator->GoToNextCell())
#ifdef _OPENMP
#pragma omp parallel for \
default (none) \
shared(NumberOfCells,InputDataSet,GridResolution,DeformationValue,DeformationValueGradient, \
DeformationVector,IsoSurfacePointsList,IsoSurfaceTrianglesList,ProgressCounter)
#endif
for(unsigned int CellIterator = 0; CellIterator < NumberOfCells; CellIterator++) // CHANGED
{
// Check cell type to be a Voxel
#ifdef _OPENMP
vtkSmartPointer<vtkGenericCell> Cell = vtkSmartPointer<vtkGenericCell>::New();
InputDataSet->GetCell(CellIterator,Cell);
#else
vtkSmartPointer<vtkCell> Cell = InputDataSet->GetCell(CellIterator);
#endif
// if(CellIterator->GetCellType() != VTK_VOXEL)
if(Cell->GetCellType() != VTK_VOXEL) // CHANGED
{
vtkErrorMacro("Cell type is not supported for Marching Cubes algorithm.");
}
// Get a cube (as 8 Ids)
// vtkSmartPointer<vtkIdList> CubeIds = CellIterator->GetPointIds();
vtkSmartPointer<vtkIdList> CubeIds = Cell->GetPointIds(); // CHANGED
// Find Anchor vertex of Cube
int AnchorVertexId = FindMinimumOfIds(CubeIds);
// Find Cube Ids in order
vtkSmartPointer<vtkIdList> OrderedCubeIds = vtkSmartPointer<vtkIdList>::New();
this->FindOrderedCubePointIds(GridResolution,AnchorVertexId,OrderedCubeIds);
// Get Anchor Index
unsigned int AnchorVertexIndex[DIMENSION];
this->GetPointIndex(GridResolution,AnchorVertexId,AnchorVertexIndex);
// Get Points Coordinates on the cube
double PointsCoordinatesOnCube[CUBESIZE][DIMENSION];
this->GetPointsCoordinatesOnCube(
InputDataSet,
OrderedCubeIds,
PointsCoordinatesOnCube); // Output
// Get Deformation values on cube vertices
double DeformationValuesOnCube[CUBESIZE];
this->GetScalarDataOnCube(
DeformationValue,
OrderedCubeIds,
DeformationValuesOnCube);
// Get Deformation Value Gradients on the Cube vertices
double DeformationValueGradientsOnCube[CUBESIZE][DIMENSION];
this->GetVectorDataOnCube(
DeformationValueGradient,
OrderedCubeIds,
DeformationValueGradientsOnCube); // Output
// Get Deformation Vectors on the Cube vertices
double DeformationVectorsOnCube[CUBESIZE][DIMENSION];
// double ** DeformationVectorsOnCube; // CHANGED
this->GetVectorDataOnCube(
DeformationVector,
OrderedCubeIds,
DeformationVectorsOnCube); // Output
// Define a Cube object
CubeCell Cube;
Cube.SetVertexPointsCoordinates(PointsCoordinatesOnCube);
Cube.SetVertexPointsOriginalIds(OrderedCubeIds);
Cube.SetDeformationValues(DeformationValue);
Cube.SetDeformationValuesOnCube(DeformationValuesOnCube);
Cube.SetDeformationValueGradientsOnCube(DeformationValueGradientsOnCube);
Cube.SetDeformationVectorsOnCube(DeformationVectorsOnCube);
Cube.SetIsoSurfaceLevel(0.0);
Cube.SetGridResolution(GridResolution,DIMENSION);
Cube.SetAnchorVertexIndex(AnchorVertexIndex,DIMENSION);
// Apply the Cube algorithm
bool IsoSurfaceStatus = Cube.FindIsoSurface();
// Get output of Cube algorithm
if(IsoSurfaceStatus == true)
{
PointsListType IntersectionPointsListOnCube = Cube.GetIntersectionPointsList();
TrianglesListType IntersectionTrianglesListOnCube = Cube.GetIntersectionTrianglesList();
// Store Intersection point
this->StoreIntersectionPoints(
GridResolution,
AnchorVertexId,
IntersectionPointsListOnCube,
IsoSurfacePointsList); // Output
// Store Intersection Triangles
this->StoreIntersectionTriangles(
GridResolution,
AnchorVertexId,
IntersectionTrianglesListOnCube,
IsoSurfaceTrianglesList); // Output
}
// Clear Cell
Cube.ClearCell();
// Update Progress
#ifdef _OPENMP
#pragma omp atomic
#endif
ProgressCounter++;
this->FilterUpdateProgress(ProgressCounter,NumberOfCells);
}
// Create Output PolyData
this->CreateOutputPolyData(
IsoSurfacePointsList,
IsoSurfaceTrianglesList,
OutputPolyData);
// Delete Cell Iterator
// CellIterator->Delete(); // CHANGED
}
// ===================
// Find Minimum Of Ids
// ===================
// Description:
// Find minim Id among list of Ids. Ids are vertex of cube Ids. The minimum id indicates
// the anchor (or bae vertex) of cube, which is used for re-ordering of vertices.
inline vtkIdType IdentifyStructures::FindMinimumOfIds(vtkIdList *CubeIds)
{
vtkIdType MinimumId = CubeIds->GetId(0);
// Loop over Ids
for(unsigned int IdIterator = 1;
IdIterator < static_cast<unsigned int>(CubeIds->GetNumberOfIds());
IdIterator++)
{
if(MinimumId > CubeIds->GetId(IdIterator))
{
MinimumId = CubeIds->GetId(IdIterator);
}
}
return MinimumId;
}
// ===========================
// Find Ordered Cube Point Ids
// ===========================
// Description:
// Adds vertices of cube in a special order.
// Starting from anchor vertex as vertex 0, a counter clockwise counting of verices on buttom face
// of cube adds points 1,2 and 3. The same for points in the upper face of cube adds vertices 4,5,6
// and 7.
void IdentifyStructures::FindOrderedCubePointIds(
int *GridResolution,
vtkIdType AnchorVertexId,
vtkIdList *OrderedCubeIds) // Output
{
}
// ==============================
// Get Points Coordinates On Cube
// ==============================
// Note: This method only performs shallow copy. It does not allocate memory to copy point
// coordinates to output. It only copeis the pointers to point coordinates and set them into
// a 2D array.
void IdentifyStructures::GetPointsCoordinatesOnCube(
vtkDataSet *InputDataSet,
vtkIdList *OrderedCubeIds,
double PointsCoordinatesOnCube[CUBESIZE][DIMENSION]) // Output
{
// Loop over cube points
for(unsigned int PointIterator = 0; PointIterator < CUBESIZE; PointIterator++)
{
#ifdef _OPENMP
double APointOnCube[DIMENSION];
InputDataSet->GetPoint(OrderedCubeIds->GetId(PointIterator),APointOnCube);
#else
double *APointOnCube = InputDataSet->GetPoint(OrderedCubeIds->GetId(PointIterator));
#endif
// Deep Copy
for(unsigned int DimensionIterator = 0; DimensionIterator < DIMENSION; DimensionIterator++)
{
PointsCoordinatesOnCube[PointIterator][DimensionIterator] = APointOnCube[DimensionIterator];
}
}
}
// =======================
// Get Scalar Data On Cube
// =======================
void IdentifyStructures::GetScalarDataOnCube(
vtkDoubleArray *InputDoubleArray,
vtkIdList *OrderedCubeIds,
double ScalarDataOnCube[CUBESIZE])
{
// Loop over cube points
for(unsigned int PointIterator = 0; PointIterator < CUBESIZE; PointIterator++)
{
#ifdef _OPENMP
double TempScalar[1];
InputDoubleArray->GetTuple(OrderedCubeIds->GetId(PointIterator),TempScalar);
ScalarDataOnCube[PointIterator] = TempScalar[0];
#else
ScalarDataOnCube[PointIterator] = InputDoubleArray->GetTuple1(OrderedCubeIds->GetId(PointIterator));
#endif
}
}
// =======================
// Get Vector Data On Cube
// =======================
// Gets vtkDoubleArray as input. It can be a Point Data of the actual data set.
// Then it gets data from the Ids List that is provided in OrderedCubeIds.
// The output is an array of Vectors class. Each Vector holds a tuple from DoubleArray.
// Note: This perfomrs a shallow copy; just puts the pointers of actual data tuples
// into a 2D array, but does not allocate any memory.
void IdentifyStructures::GetVectorDataOnCube(
vtkDoubleArray *InputDoubleArray,
vtkIdList *OrderedCubeIds,
double VectorDataOnCube[CUBESIZE][DIMENSION]) // Output
{
// Loop over cube points
for(unsigned int PointIterator = 0; PointIterator < CUBESIZE; PointIterator++)
{
#ifdef _OPENMP
double TempTuple[DIMENSION];
InputDoubleArray->GetTuple(OrderedCubeIds->GetId(PointIterator),TempTuple);
for(unsigned int DimensionIterator = 0; DimensionIterator < DIMENSION; DimensionIterator++)
{
VectorDataOnCube[PointIterator][DimensionIterator] = TempTuple[DimensionIterator];
}
#else
for(unsigned int DimensionIterator = 0; DimensionIterator < DIMENSION; DimensionIterator++)
{
InputDoubleArray->GetComponent(OrderedCubeIds->GetId(PointIterator),DimensionIterator);
}
#endif
}
}
// ==================
// Get Global Edge Id
// ==================
// Description:
// Gives a unique Id for an edge of any cube in the whole Structured point data set. Although cubes
// share edges, but a unique Id can be given for an edge, irregardless of which cube(s) it belongs to.
// Input to this method is "Local" edge Id on a cube (from 0 to 11).
unsigned int IdentifyStructures::GetGlobalEdgeId(
int *GridResolution,
vtkIdType AnchorVertexId,
vtkIdType LocalEdgeId) // Output
{
// Get Index of Anchor vertex of the cube w.r.t integer coordinates of structured point