-
Notifications
You must be signed in to change notification settings - Fork 1
/
kstreamtracer.cpp
1268 lines (1103 loc) · 40.3 KB
/
kstreamtracer.cpp
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: StreamTracer.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 "kstreamtracer.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkCompositeDataIterator.h"
#include "vtkCompositeDataPipeline.h"
#include "vtkCompositeDataSet.h"
#include "vtkDataSetAttributes.h"
#include "vtkDoubleArray.h"
#include "vtkExecutive.h"
#include "vtkGenericCell.h"
#include "vtkIdList.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkIntArray.h"
#include "vtkCellLocator.h"
#include "vtkModifiedBSPTree.h"
#include "vtkInterpolatedVelocityField.h"
#include "vtkAbstractInterpolatedVelocityField.h"
#include "vtkCellLocatorInterpolatedVelocityField.h"
#include "vtkMath.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPointSet.h"
#include "vtkPolyData.h"
#include "vtkPolyLine.h"
#include "vtkRungeKutta2.h"
#include "vtkRungeKutta4.h"
#include "vtkRungeKutta45.h"
#include "vtkSmartPointer.h"
vtkStandardNewMacro(StreamTracer);
vtkCxxSetObjectMacro(StreamTracer,Integrator,vtkInitialValueProblemSolver);
vtkCxxSetObjectMacro(StreamTracer,InterpolatorPrototype,vtkAbstractInterpolatedVelocityField);
const double StreamTracer::EPSILON = 1.0E-12;
StreamTracer::StreamTracer()
{
this->Integrator = vtkRungeKutta2::New();
this->IntegrationDirection = FORWARD;
for(int i=0; i<3; i++)
{
this->StartPosition[i] = 0.0;
}
this->MaximumPropagation = 1.0;
this->IntegrationStepUnit = CELL_LENGTH_UNIT;
this->InitialIntegrationStep = 0.5;
this->MinimumIntegrationStep = 1.0E-2;
this->MaximumIntegrationStep = 1.0;
this->MaximumError = 1.0e-6;
this->MaximumNumberOfSteps = 2000;
this->TerminalSpeed = EPSILON;
this->ComputeVorticity = true;
this->RotationScale = 1.0;
this->LastUsedStepSize = 0.0;
this->GenerateNormalsInIntegrate = true;
this->InterpolatorPrototype = 0;
this->SetNumberOfInputPorts(2);
// by default process active point vectors
this->SetInputArrayToProcess(0,0,0,vtkDataObject::FIELD_ASSOCIATION_POINTS,
vtkDataSetAttributes::VECTORS);
}
StreamTracer::~StreamTracer()
{
this->SetIntegrator(0);
this->SetInterpolatorPrototype(0);
}
void StreamTracer::SetSourceConnection(vtkAlgorithmOutput* algOutput)
{
this->SetInputConnection(1, algOutput);
}
void StreamTracer::SetSource(vtkDataSet *source)
{
this->SetInput(1, source);
}
vtkDataSet *StreamTracer::GetSource()
{
if (this->GetNumberOfInputConnections(1) < 1)
{
return 0;
}
return vtkDataSet::SafeDownCast(
this->GetExecutive()->GetInputData(1, 0));
}
int StreamTracer::GetIntegratorType()
{
if (!this->Integrator)
{
return NONE;
}
if (!strcmp(this->Integrator->GetClassName(), "vtkRungeKutta2"))
{
return RUNGE_KUTTA2;
}
if (!strcmp(this->Integrator->GetClassName(), "vtkRungeKutta4"))
{
return RUNGE_KUTTA4;
}
if (!strcmp(this->Integrator->GetClassName(), "vtkRungeKutta45"))
{
return RUNGE_KUTTA45;
}
return UNKNOWN;
}
void StreamTracer::SetInterpolatorTypeToDataSetPointLocator()
{
this->SetInterpolatorType
( static_cast<int> ( INTERPOLATOR_WITH_DATASET_POINT_LOCATOR ) );
}
void StreamTracer::SetInterpolatorTypeToCellLocator()
{
this->SetInterpolatorType
( static_cast<int> ( INTERPOLATOR_WITH_CELL_LOCATOR ) );
}
void StreamTracer::SetInterpolatorType( int interpType )
{
if ( interpType == INTERPOLATOR_WITH_CELL_LOCATOR )
{
// create an interpolator equipped with a cell locator
vtkSmartPointer< vtkCellLocatorInterpolatedVelocityField > cellLoc =
vtkSmartPointer< vtkCellLocatorInterpolatedVelocityField >::New();
// specify the type of the cell locator attached to the interpolator
vtkSmartPointer< vtkModifiedBSPTree > cellLocType =
vtkSmartPointer< vtkModifiedBSPTree >::New();
cellLoc->SetCellLocatorPrototype( cellLocType.GetPointer() );
this->SetInterpolatorPrototype( cellLoc.GetPointer() );
}
else
{
// create an interpolator equipped with a point locator (by default)
vtkSmartPointer< vtkInterpolatedVelocityField > pntLoc =
vtkSmartPointer< vtkInterpolatedVelocityField >::New();
this->SetInterpolatorPrototype( pntLoc.GetPointer() );
}
}
void StreamTracer::SetIntegratorType(int type)
{
vtkInitialValueProblemSolver* ivp=0;
switch (type)
{
case RUNGE_KUTTA2:
ivp = vtkRungeKutta2::New();
break;
case RUNGE_KUTTA4:
ivp = vtkRungeKutta4::New();
break;
case RUNGE_KUTTA45:
ivp = vtkRungeKutta45::New();
break;
default:
vtkWarningMacro("Unrecognized integrator type. Keeping old one.");
break;
}
if (ivp)
{
this->SetIntegrator(ivp);
ivp->Delete();
}
}
void StreamTracer::SetIntegrationStepUnit( int unit )
{
if ( unit != LENGTH_UNIT && unit != CELL_LENGTH_UNIT )
{
unit = CELL_LENGTH_UNIT;
}
if ( unit == this->IntegrationStepUnit )
{
return;
}
this->IntegrationStepUnit = unit;
this->Modified();
}
double StreamTracer::ConvertToLength(
double interval, int unit, double cellLength )
{
double retVal = 0.0;
if ( unit == LENGTH_UNIT )
{
retVal = interval;
}
else
if ( unit == CELL_LENGTH_UNIT )
{
retVal = interval * cellLength;
}
return retVal;
}
double StreamTracer::ConvertToLength(
StreamTracer::IntervalInformation& interval, double cellLength )
{
return ConvertToLength( interval.Interval, interval.Unit, cellLength );
}
void StreamTracer::ConvertIntervals( double& step, double& minStep,
double& maxStep, int direction, double cellLength )
{
minStep = maxStep = step =
direction * this->ConvertToLength( this->InitialIntegrationStep,
this->IntegrationStepUnit, cellLength );
if ( this->MinimumIntegrationStep > 0.0 )
{
minStep = this->ConvertToLength( this->MinimumIntegrationStep,
this->IntegrationStepUnit, cellLength );
}
if ( this->MaximumIntegrationStep > 0.0 )
{
maxStep = this->ConvertToLength( this->MaximumIntegrationStep,
this->IntegrationStepUnit, cellLength );
}
}
void StreamTracer::CalculateVorticity(vtkGenericCell* cell,
double pcoords[3],
vtkDoubleArray* cellVectors,
double vorticity[3])
{
double* cellVel;
double derivs[9];
cellVel = cellVectors->GetPointer(0);
cell->Derivatives(0, pcoords, cellVel, 3, derivs);
vorticity[0] = derivs[7] - derivs[5];
vorticity[1] = derivs[2] - derivs[6];
vorticity[2] = derivs[3] - derivs[1];
}
void StreamTracer::InitializeSeeds(vtkDataArray*& seeds,
vtkIdList*& seedIds,
vtkIntArray*& integrationDirections,
vtkDataSet *source)
{
seedIds = vtkIdList::New();
integrationDirections = vtkIntArray::New();
seeds=0;
if (source)
{
int i;
vtkIdType numSeeds = source->GetNumberOfPoints();
if (numSeeds > 0)
{
// For now, one thread will do all
if (this->IntegrationDirection == BOTH)
{
seedIds->SetNumberOfIds(2*numSeeds);
for (i=0; i<numSeeds; i++)
{
seedIds->SetId(i, i);
seedIds->SetId(numSeeds + i, i);
}
}
else
{
seedIds->SetNumberOfIds(numSeeds);
for (i=0; i<numSeeds; i++)
{
seedIds->SetId(i, i);
}
}
// Check if the source is a PointSet
vtkPointSet* seedPts = vtkPointSet::SafeDownCast(source);
if (seedPts)
{
// If it is, use it's points as source
vtkDataArray* orgSeeds = seedPts->GetPoints()->GetData();
seeds = orgSeeds->NewInstance();
seeds->DeepCopy(orgSeeds);
}
else
{
// Else, create a seed source
seeds = vtkDoubleArray::New();
seeds->SetNumberOfComponents(3);
seeds->SetNumberOfTuples(numSeeds);
for (i=0; i<numSeeds; i++)
{
seeds->SetTuple(i, source->GetPoint(i));
}
}
}
}
else
{
seeds = vtkDoubleArray::New();
seeds->SetNumberOfComponents(3);
seeds->InsertNextTuple(this->StartPosition);
seedIds->InsertNextId(0);
if (this->IntegrationDirection == BOTH)
{
seedIds->InsertNextId(0);
}
}
if (seeds)
{
vtkIdType i;
vtkIdType numSeeds = seeds->GetNumberOfTuples();
if (this->IntegrationDirection == BOTH)
{
for(i=0; i<numSeeds; i++)
{
integrationDirections->InsertNextValue(FORWARD);
}
for(i=0; i<numSeeds; i++)
{
integrationDirections->InsertNextValue(BACKWARD);
}
}
else
{
for(i=0; i<numSeeds; i++)
{
integrationDirections->InsertNextValue(this->IntegrationDirection);
}
}
}
}
int StreamTracer::SetupOutput(vtkInformation* inInfo,
vtkInformation* outInfo)
{
int piece=outInfo->Get(
vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER());
int numPieces =
outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES());
vtkDataObject* input = inInfo->Get(vtkDataObject::DATA_OBJECT());
vtkCompositeDataSet *hdInput = vtkCompositeDataSet::SafeDownCast(input);
vtkDataSet* dsInput = vtkDataSet::SafeDownCast(input);
if (hdInput)
{
this->InputData = hdInput;
hdInput->Register(this);
return 1;
}
else if (dsInput)
{
vtkDataSet* copy = dsInput->NewInstance();
copy->ShallowCopy(dsInput);
vtkMultiBlockDataSet* mb = vtkMultiBlockDataSet::New();
mb->SetNumberOfBlocks(numPieces);
mb->SetBlock(piece, copy);
copy->Delete();
this->InputData = mb;
mb->Register(this);
mb->Delete();
return 1;
}
else
{
vtkErrorMacro("This filter cannot handle input of type: "
<< (input?input->GetClassName():"(none)"));
return 0;
}
}
int StreamTracer::RequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
if (!this->SetupOutput(inInfo, outInfo))
{
return 0;
}
vtkInformation *sourceInfo = inputVector[1]->GetInformationObject(0);
vtkDataSet *source = 0;
if (sourceInfo)
{
source = vtkDataSet::SafeDownCast(
sourceInfo->Get(vtkDataObject::DATA_OBJECT()));
}
vtkPolyData *output = vtkPolyData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkDataArray* seeds = 0;
vtkIdList* seedIds = 0;
vtkIntArray* integrationDirections = 0;
this->InitializeSeeds(seeds, seedIds, integrationDirections, source);
if (seeds)
{
double lastPoint[3];
vtkAbstractInterpolatedVelocityField * func;
int maxCellSize = 0;
if (this->CheckInputs(func, &maxCellSize) != VTK_OK)
{
vtkDebugMacro("No appropriate inputs have been found. Can not execute.");
func->Delete();
seeds->Delete();
integrationDirections->Delete();
seedIds->Delete();
this->InputData->UnRegister(this);
return 1;
}
vtkCompositeDataIterator* iter = this->InputData->NewIterator();
vtkSmartPointer<vtkCompositeDataIterator> iterP(iter);
iter->Delete();
iterP->GoToFirstItem();
vtkDataSet* input0 = 0;
if (!iterP->IsDoneWithTraversal())
{
input0 = vtkDataSet::SafeDownCast(iterP->GetCurrentDataObject());
}
vtkDataArray *vectors = this->GetInputArrayToProcess(0,input0);
if (vectors)
{
const char *vecName = vectors->GetName();
double propagation = 0;
vtkIdType numSteps = 0;
this->Integrate(input0, output,
seeds, seedIds,
integrationDirections,
lastPoint, func,
maxCellSize, vecName,
propagation, numSteps);
}
func->Delete();
seeds->Delete();
}
integrationDirections->Delete();
seedIds->Delete();
this->InputData->UnRegister(this);
return 1;
}
int StreamTracer::CheckInputs(vtkAbstractInterpolatedVelocityField*& func,
int* maxCellSize)
{
if (!this->InputData)
{
return VTK_ERROR;
}
vtkCompositeDataIterator* iter = this->InputData->NewIterator();
vtkSmartPointer<vtkCompositeDataIterator> iterP(iter);
iter->Delete();
iterP->GoToFirstItem();
if (iterP->IsDoneWithTraversal())
{
return VTK_ERROR;
}
// Set the function set to be integrated
if ( !this->InterpolatorPrototype )
{
func = vtkInterpolatedVelocityField::New();
// turn on the following segment, in place of the above line, if an
// interpolator equipped with a cell locator is dedired as the default
//
// func = vtkCellLocatorInterpolatedVelocityField::New();
// vtkSmartPointer< vtkModifiedBSPTree > locator =
// vtkSmartPointer< vtkModifiedBSPTree >::New();
// vtkCellLocatorInterpolatedVelocityField::SafeDownCast( func )
// ->SetCellLocatorPrototype( locator.GetPointer() );
}
else
{
func = this->InterpolatorPrototype->NewInstance();
func->CopyParameters(this->InterpolatorPrototype);
}
// iterP->GetCurrentDataObject()->Print(cout);
vtkDataArray *vectors = this->GetInputArrayToProcess(
0,iterP->GetCurrentDataObject());
if (!vectors)
{
return VTK_ERROR;
}
const char *vecName = vectors->GetName();
func->SelectVectors(vecName);
// Add all the inputs ( except source, of course ) which
// have the appropriate vectors and compute the maximum
// cell size.
int numInputs = 0;
iterP->GoToFirstItem();
while (!iterP->IsDoneWithTraversal())
{
vtkDataSet* inp = vtkDataSet::SafeDownCast(iterP->GetCurrentDataObject());
if (inp)
{
if (!inp->GetPointData()->GetVectors(vecName))
{
vtkDebugMacro("One of the input blocks does not contain a "
"velocity vector.");
iterP->GoToNextItem();
continue;
}
int cellSize = inp->GetMaxCellSize();
if ( cellSize > *maxCellSize )
{
*maxCellSize = cellSize;
}
func->AddDataSet(inp);
numInputs++;
}
iterP->GoToNextItem();
}
if ( numInputs == 0 )
{
vtkDebugMacro("No appropriate inputs have been found. Can not execute.");
return VTK_ERROR;
}
return VTK_OK;
}
void StreamTracer::Integrate(vtkDataSet *input0,
vtkPolyData* output,
vtkDataArray* seedSource,
vtkIdList* seedIds,
vtkIntArray* integrationDirections,
double lastPoint[3],
vtkAbstractInterpolatedVelocityField* func,
int maxCellSize,
const char *vecName,
double& inPropagation,
vtkIdType& inNumSteps)
{
int i;
vtkIdType numLines = seedIds->GetNumberOfIds();
double propagation = inPropagation;
vtkIdType numSteps = inNumSteps;
// Useful pointers
vtkDataSetAttributes* outputPD = output->GetPointData();
vtkDataSetAttributes* outputCD = output->GetCellData();
vtkPointData* inputPD;
vtkDataSet* input;
vtkDataArray* inVectors;
int direction=1;
double* weights = 0;
if ( maxCellSize > 0 )
{
weights = new double[maxCellSize];
}
if (this->GetIntegrator() == 0)
{
vtkErrorMacro("No integrator is specified.");
return;
}
// Used in GetCell()
vtkGenericCell* cell = vtkGenericCell::New();
// Create a new integrator, the type is the same as Integrator
vtkInitialValueProblemSolver* integrator =
this->GetIntegrator()->NewInstance();
integrator->SetFunctionSet(func);
// Since we do not know what the total number of points
// will be, we do not allocate any. This is important for
// cases where a lot of streamers are used at once. If we
// were to allocate any points here, potentially, we can
// waste a lot of memory if a lot of streamers are used.
// Always insert the first point
vtkPoints* outputPoints = vtkPoints::New();
vtkCellArray* outputLines = vtkCellArray::New();
// We will keep track of integration time in this array
vtkDoubleArray* time = vtkDoubleArray::New();
time->SetName("IntegrationTime");
// This will store the length of each stream line
vtkDoubleArray* length = vtkDoubleArray::New();
length->SetName("Length");
// This array explains why the integration stopped
vtkIntArray* retVals = vtkIntArray::New();
retVals->SetName("ReasonForTermination");
vtkIntArray* outputSeedIds = vtkIntArray::New();
outputSeedIds->SetName("SeedId");
vtkDoubleArray* cellVectors = 0;
vtkDoubleArray* vorticity = 0;
vtkDoubleArray* rotation = 0;
vtkDoubleArray* angularVel = 0;
if (this->ComputeVorticity)
{
cellVectors = vtkDoubleArray::New();
cellVectors->SetNumberOfComponents(3);
cellVectors->Allocate(3*VTK_CELL_SIZE);
vorticity = vtkDoubleArray::New();
vorticity->SetName("Vorticity");
vorticity->SetNumberOfComponents(3);
rotation = vtkDoubleArray::New();
rotation->SetName("Rotation");
angularVel = vtkDoubleArray::New();
angularVel->SetName("AngularVelocity");
}
// We will interpolate all point attributes of the input on each point of
// the output (unless they are turned off). Note that we are using only
// the first input, if there are more than one, the attributes have to match.
//
// Note: We have to use a specific value (safe to employ the maximum number
// of steps) as the size of the initial memory allocation here. The
// use of the default argument might incur a crash problem (due to
// "insufficient memory") in the parallel mode. This is the case when
// a streamline intensely shuttles between two processes in an exactly
// interleaving fashion --- only one point is produced on each process
// (and actually two points, after point duplication, are saved to a
// vtkPolyData in vtkDistributedStreamTracer::NoBlockProcessTask) and
// as a consequence a large number of such small vtkPolyData objects
// are needed to represent a streamline, consuming up the memory before
// the intermediate memory is timely released.
outputPD->InterpolateAllocate( input0->GetPointData(),
this->MaximumNumberOfSteps );
vtkIdType numPtsTotal=0;
double velocity[3];
int shouldAbort = 0;
for(int currentLine = 0; currentLine < numLines; currentLine++)
{
double progress = static_cast<double>(currentLine)/numLines;
this->UpdateProgress(progress);
switch (integrationDirections->GetValue(currentLine))
{
case FORWARD:
direction = 1;
break;
case BACKWARD:
direction = -1;
break;
}
// temporary variables used in the integration
double point1[3], point2[3], pcoords[3], vort[3], omega;
vtkIdType index, numPts=0;
// Clear the last cell to avoid starting a search from
// the last point in the streamline
func->ClearLastCellId();
// Initial point
seedSource->GetTuple(seedIds->GetId(currentLine), point1);
memcpy(point2, point1, 3*sizeof(double));
if (!func->FunctionValues(point1, velocity))
{
cout << "Can't evaluate function value at " << point1[0] << "," << point1[1] << "," << point1[2] << endl;
continue;
}
if ( propagation >= this->MaximumPropagation ||
numSteps > this->MaximumNumberOfSteps)
{
continue;
}
numPts++;
numPtsTotal++;
vtkIdType nextPoint = outputPoints->InsertNextPoint(point1);
time->InsertNextValue(0.0);
// We will always pass an arc-length step size to the integrator.
// If the user specifies a step size in cell length unit, we will
// have to convert it to arc length.
IntervalInformation stepSize; // either positive or negative
stepSize.Unit = LENGTH_UNIT;
stepSize.Interval = 0;
IntervalInformation aStep; // always positive
aStep.Unit = LENGTH_UNIT;
double step, minStep=0, maxStep=0;
double stepTaken, accumTime=0;
double speed;
double cellLength;
int retVal=OUT_OF_LENGTH, tmp;
// Make sure we use the dataset found by the vtkAbstractInterpolatedVelocityField
input = func->GetLastDataSet();
inputPD = input->GetPointData();
inVectors = inputPD->GetVectors(vecName);
// Convert intervals to arc-length unit
input->GetCell(func->GetLastCellId(), cell);
cellLength = sqrt(static_cast<double>(cell->GetLength2()));
speed = vtkMath::Norm(velocity);
// Never call conversion methods if speed == 0
if ( speed != 0.0 )
{
this->ConvertIntervals( stepSize.Interval, minStep, maxStep,
direction, cellLength );
}
// Interpolate all point attributes on first point
func->GetLastWeights(weights);
outputPD->InterpolatePoint(inputPD, nextPoint, cell->PointIds, weights);
// Compute vorticity if required
// This can be used later for streamribbon generation.
if (this->ComputeVorticity)
{
inVectors->GetTuples(cell->PointIds, cellVectors);
func->GetLastLocalCoordinates(pcoords);
StreamTracer::CalculateVorticity(cell, pcoords, cellVectors, vort);
vorticity->InsertNextTuple(vort);
// rotation
// local rotation = vorticity . unit tangent ( i.e. velocity/speed )
if (speed != 0.0)
{
omega = vtkMath::Dot(vort, velocity);
omega /= speed;
omega *= this->RotationScale;
}
else
{
omega = 0.0;
}
angularVel->InsertNextValue(omega);
rotation->InsertNextValue(0.0);
}
double error = 0;
// Integrate until the maximum propagation length is reached,
// maximum number of steps is reached or until a boundary is encountered.
// Begin Integration
while ( propagation < this->MaximumPropagation )
{
if (numSteps > this->MaximumNumberOfSteps)
{
retVal = OUT_OF_STEPS;
break;
}
if ( numSteps++ % 1000 == 1 )
{
progress =
( currentLine + propagation / this->MaximumPropagation ) / numLines;
this->UpdateProgress(progress);
if (this->GetAbortExecute())
{
shouldAbort = 1;
break;
}
}
// Never call conversion methods if speed == 0
if ( (speed == 0) || (speed <= this->TerminalSpeed) )
{
retVal = STAGNATION;
break;
}
// If, with the next step, propagation will be larger than
// max, reduce it so that it is (approximately) equal to max.
aStep.Interval = fabs( stepSize.Interval );
if ( ( propagation + aStep.Interval ) > this->MaximumPropagation )
{
aStep.Interval = this->MaximumPropagation - propagation;
if ( stepSize.Interval >= 0 )
{
stepSize.Interval = this->ConvertToLength( aStep, cellLength );
}
else
{
stepSize.Interval = this->ConvertToLength( aStep, cellLength ) * ( -1.0 );
}
maxStep = stepSize.Interval;
}
this->LastUsedStepSize = stepSize.Interval;
// Calculate the next step using the integrator provided
// Break if the next point is out of bounds.
func->SetNormalizeVector( true );
tmp = integrator->ComputeNextStep( point1, point2, 0, stepSize.Interval,
stepTaken, minStep, maxStep,
this->MaximumError, error );
func->SetNormalizeVector( false );
if ( tmp != 0 )
{
retVal = tmp;
memcpy(lastPoint, point2, 3*sizeof(double));
break;
}
// It is not enough to use the starting point for stagnation calculation
// Use delX/stepSize to calculate speed and check if it is below
// stagnation threshold
double disp[3];
for (i=0; i<3; i++)
{
disp[i] = point2[i] - point1[i];
}
if ( (stepSize.Interval == 0) ||
(vtkMath::Norm(disp) / fabs(stepSize.Interval) <= this->TerminalSpeed) )
{
retVal = STAGNATION;
break;
}
accumTime += stepTaken / speed;
// Calculate propagation (using the same units as MaximumPropagation
propagation += fabs( stepSize.Interval );
// This is the next starting point
for(i=0; i<3; i++)
{
point1[i] = point2[i];
}
// Interpolate the velocity at the next point
if ( !func->FunctionValues(point2, velocity) )
{
retVal = OUT_OF_DOMAIN;
memcpy(lastPoint, point2, 3*sizeof(double));
break;
}
// Make sure we use the dataset found by the vtkAbstractInterpolatedVelocityField
input = func->GetLastDataSet();
inputPD = input->GetPointData();
inVectors = inputPD->GetVectors(vecName);
// Point is valid. Insert it.
numPts++;
numPtsTotal++;
nextPoint = outputPoints->InsertNextPoint(point1);
time->InsertNextValue(accumTime);
// Calculate cell length and speed to be used in unit conversions
input->GetCell(func->GetLastCellId(), cell);
cellLength = sqrt(static_cast<double>(cell->GetLength2()));
speed = vtkMath::Norm(velocity);
// Interpolate all point attributes on current point
func->GetLastWeights(weights);
outputPD->InterpolatePoint(inputPD, nextPoint, cell->PointIds, weights);
// Compute vorticity if required
// This can be used later for streamribbon generation.
if (this->ComputeVorticity)
{
inVectors->GetTuples(cell->PointIds, cellVectors);
func->GetLastLocalCoordinates(pcoords);
StreamTracer::CalculateVorticity(cell, pcoords, cellVectors, vort);
vorticity->InsertNextTuple(vort);
// rotation
// angular velocity = vorticity . unit tangent ( i.e. velocity/speed )
// rotation = sum ( angular velocity * stepSize )
omega = vtkMath::Dot(vort, velocity);
omega /= speed;
omega *= this->RotationScale;
index = angularVel->InsertNextValue(omega);
rotation->InsertNextValue(rotation->GetValue(index-1) +
(angularVel->GetValue(index-1) + omega)/2 *
(accumTime - time->GetValue(index-1)));
}
// Never call conversion methods if speed == 0
if ( (speed == 0) || (speed <= this->TerminalSpeed) )
{
retVal = STAGNATION;
break;
}
// Convert all intervals to arc length
this->ConvertIntervals( step, minStep, maxStep, direction, cellLength );
// If the solver is adaptive and the next step size (stepSize.Interval)
// that the solver wants to use is smaller than minStep or larger
// than maxStep, re-adjust it. This has to be done every step
// because minStep and maxStep can change depending on the cell
// size (unless it is specified in arc-length unit)
if (integrator->IsAdaptive())
{
if (fabs(stepSize.Interval) < fabs(minStep))
{
stepSize.Interval = fabs( minStep ) *
stepSize.Interval / fabs( stepSize.Interval );
}
else if (fabs(stepSize.Interval) > fabs(maxStep))
{
stepSize.Interval = fabs( maxStep ) *
stepSize.Interval / fabs( stepSize.Interval );
}
}
else
{
stepSize.Interval = step;
}
// End Integration
}
if (shouldAbort)
{
break;
}
if (numPts > 1)
{
#pragma mark Adding Cells
outputLines->InsertNextCell(numPts);
double distSum = 0;
for (int j = 0, i=numPtsTotal-numPts; i<numPtsTotal; i++, j++)
{
outputLines->InsertCellPoint(i);
if (j > 0) {
double p1[3];
double p2[3];
outputPoints->GetPoint(i-1, p1);
outputPoints->GetPoint(i, p2);
double dist = sqrt(vtkMath::Distance2BetweenPoints(p1, p2));
distSum += dist;
}
}
retVals->InsertNextValue(retVal);
outputSeedIds->InsertNextValue(currentLine);
length->InsertNextValue(distSum);
}
// Initialize these to 0 before starting the next line.
// The values passed in the function call are only used
// for the first line.
inPropagation = propagation;
inNumSteps = numSteps;
propagation = 0;
numSteps = 0;
}
if (!shouldAbort)
{
// Create the output polyline
output->SetPoints(outputPoints);
outputPD->AddArray(time);
if (vorticity)