forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkVolumeRayCastSpaceLeapingImageFilter.cxx
1240 lines (1031 loc) · 37.1 KB
/
vtkVolumeRayCastSpaceLeapingImageFilter.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: vtkVolumeRayCastSpaceLeapingImageFilter.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 "vtkVolumeRayCastSpaceLeapingImageFilter.h"
#include "vtkImageData.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkDataArray.h"
#include "vtkPointData.h"
#include <sstream>
#include <fstream>
#include <iostream>
#ifdef vtkVolumeRayCastSpaceLeapingImageFilter_DEBUG
#include "vtkMetaImageWriter.h"
#endif
#include <cmath>
// Space leaping block size
#define VTK_SL_BLK 4
//----------------------------------------------------------------------------
vtkStandardNewMacro(vtkVolumeRayCastSpaceLeapingImageFilter);
vtkCxxSetObjectMacro(vtkVolumeRayCastSpaceLeapingImageFilter,
CurrentScalars, vtkDataArray);
//----------------------------------------------------------------------------
vtkVolumeRayCastSpaceLeapingImageFilter::vtkVolumeRayCastSpaceLeapingImageFilter()
{
this->ComputeMinMax = 0;
this->ComputeGradientOpacity = 0;
this->UpdateGradientOpacityFlags = 0;
this->IndependentComponents = 1;
this->CurrentScalars = nullptr;
this->MinNonZeroScalarIndex = nullptr;
this->MinNonZeroGradientMagnitudeIndex = nullptr;
this->GradientMagnitude = nullptr;
for (int i = 0; i < 4; i++)
{
this->TableSize[i] = 0;
this->TableShift[i] = 0;
this->TableScale[i] = 1;
this->ScalarOpacityTable[i] = nullptr;
this->GradientOpacityTable[i] = nullptr;
}
this->Cache = nullptr;
// Ensure that no splits occur along X or Y axes when multithreading,
// there seems to be a bug with the increments that requires this.
this->SplitPath[0] = 2;
this->SplitPathLength = 1;
}
//----------------------------------------------------------------------------
vtkVolumeRayCastSpaceLeapingImageFilter::~vtkVolumeRayCastSpaceLeapingImageFilter()
{
this->SetCurrentScalars(nullptr);
delete [] this->MinNonZeroScalarIndex;
delete [] this->MinNonZeroGradientMagnitudeIndex;
}
//----------------------------------------------------------------------------
void vtkVolumeRayCastSpaceLeapingImageFilter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "ComputeMinMax: " << this->ComputeMinMax << "\n";
os << indent << "ComputeGradientOpacity: " << this->ComputeGradientOpacity << "\n";
os << indent << "UpdateGradientOpacityFlags: " << this->UpdateGradientOpacityFlags << "\n";
os << indent << "IndependentComponents: " << this->IndependentComponents << "\n";
os << indent << "CurrentScalars: " << this->CurrentScalars << "\n";
// this->TableShift
// this->TableScale
// this->TableSize
// this->ScalarOpacityTable
// this->GradientOpacityTable
// this->MinNonZeroScalarIndex
// this->MinNonZeroGradientMagnitudeIndex
}
//----------------------------------------------------------------------------
int vtkVolumeRayCastSpaceLeapingImageFilter::RequestUpdateExtent (
vtkInformation * vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *vtkNotUsed(outputVector))
{
// get the info objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
// Ask for the whole input
int wholeExtent[6];
inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), wholeExtent);
inInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(), wholeExtent, 6);
return 1;
}
//----------------------------------------------------------------------------
void vtkVolumeRayCastSpaceLeapingImageFilter
::SetCache( vtkImageData * cache )
{
// Do not reference count it to avoid reference counting loops
this->Cache = cache;
}
//----------------------------------------------------------------------------
void vtkVolumeRayCastSpaceLeapingImageFilter
::InternalRequestUpdateExtent( int *inExt,
int *wholeExtent)
{
int dim[3];
// We group four cells (which require 5 samples) into one element in the min/max tree
for ( int i = 0; i < 3; i++ )
{
// size of the input image.
dim[i] = wholeExtent[2*i+1] - wholeExtent[2*i] + 1;
inExt[2*i] = 0; // The output extent is 0 based.
inExt[2*i+1] = (dim[i] < 2) ? (0) :
(static_cast<int>((dim[i] - 2)/VTK_SL_BLK));
}
}
//----------------------------------------------------------------------------
static void
vtkVolumeRayCastSpaceLeapingImageFilterClearOutput(vtkImageData *outData,
int outExt[6],
int nComponents )
{
unsigned short *tmpPtr = static_cast< unsigned short * >(
outData->GetScalarPointerForExtent(outExt));
// Get increments to march through the thread's output extents
vtkIdType outInc0, outInc1, outInc2;
outData->GetContinuousIncrements(outExt, outInc0, outInc1, outInc2);
// A. Initialize the arrays with a blank flag.
int i,j,k;
int c;
for (k = outExt[4]; k <= outExt[5]; ++k, tmpPtr += outInc2)
{
for (j = outExt[2]; j <= outExt[3]; ++j, tmpPtr += outInc1)
{
for (i = outExt[0]; i <= outExt[1]; ++i)
{
for ( c = 0; c < nComponents; ++c )
{
*(tmpPtr++) = 0xffff; // Min Scalar
*(tmpPtr++) = 0; // Max Scalar
*(tmpPtr++) = 0; // Max Gradient Magnitude and
} // Flag computed from transfer functions
}
}
}
}
//----------------------------------------------------------------------------
void vtkVolumeRayCastSpaceLeapingImageFilter
::ComputeInputExtentsForOutput( int inExt[6], int inDim[3],
int outExt[6], vtkImageData *inData )
{
int inWholeExt[6];
inData->GetExtent(inWholeExt);
for ( int i = 0; i < 3; i++ )
{
inExt[2*i] = outExt[2*i] * VTK_SL_BLK + inWholeExt[2*i];
// Extra +1 needed here since we group four cells (which require 5
// samples) into one element in the min/max tree
inExt[2*i+1] = (outExt[2*i+1]+1) * VTK_SL_BLK + inWholeExt[2*i] + 1;
// Clip the extents with the whole extent.
if (inExt[2*i] < inWholeExt[2*i])
{
inExt[2*i] = inWholeExt[2*i];
}
if (inExt[2*i+1] > inWholeExt[2*i+1])
{
inExt[2*i+1] = inWholeExt[2*i+1];
}
inDim[i] = inExt[2*i+1] - inExt[2*i] + 1;
}
}
//----------------------------------------------------------------------------
// Fill in the min-max space leaping information.
template <class T>
void
vtkVolumeRayCastSpaceLeapingImageFilterMinMaxExecute(
vtkVolumeRayCastSpaceLeapingImageFilter *self,
vtkImageData *inData,
vtkImageData *outData, int outExt[6],
T )
{
// the number of independent components for which we need to keep track of
// min/max
vtkDataArray * scalars = self->GetCurrentScalars();
const int components = scalars->GetNumberOfComponents();
const int independent = self->GetIndependentComponents();
const int nComponents = (independent) ? components : 1;
// B. Now fill in the max-min-gradient volume structure
// B.1 First compute the extents of the input that contribute to this structure
int inExt[6], inWholeExt[6];
int inDim[3];
int outWholeDim[3];
vtkVolumeRayCastSpaceLeapingImageFilter::ComputeInputExtentsForOutput(
inExt, inDim, outExt, inData );
inData->GetExtent(inWholeExt);
outData->GetDimensions(outWholeDim);
float shift[4], scale[4];
self->GetTableShift(shift);
self->GetTableScale(scale);
// B.2 Get increments to march through the input extents
vtkIdType inInc0, inInc1, inInc2;
inData->GetContinuousIncrements(scalars,
inExt, inInc0, inInc1, inInc2);
// Get increments to march through the output extents
const vtkIdType outInc0 = 3*nComponents;
const vtkIdType outInc1 = outInc0*outWholeDim[0];
const vtkIdType outInc2 = outInc1*outWholeDim[1];
// B.3 Now fill in the min-max volume.
int i, j, k;
int c;
int sx1, sx2, sy1, sy2, sz1, sz2;
int x, y, z;
T *dptr = static_cast< T * >(scalars->GetVoidPointer(0));
unsigned short val;
unsigned short *outBasePtr = static_cast< unsigned short * >(
outData->GetScalarPointer());
// Initialize pointer to the starting extents given by inExt.
dptr += self->ComputeOffset( inExt, inWholeExt, nComponents );
// The pointer into the space-leaping output volume.
unsigned short *tmpPtr, *tmpPtrK, *tmpPtrJ, *tmpPtrI;
for ( k = 0; k < inDim[2]; k++, dptr += inInc2 )
{
sz1 = (k < 1)?(0):((k-1)/4);
sz2 = ((k )/4);
sz2 = ( k == inDim[2]-1 )?(sz1):(sz2);
sz1 += outExt[4];
sz2 += outExt[4];
// Bounds check
if (sz2 > outExt[5])
{
sz2 = outExt[5];
}
tmpPtrK = outBasePtr + sz1 * outInc2;
for ( j = 0; j < inDim[1]; j++, dptr+= inInc1 )
{
sy1 = (j < 1)?(0):((j-1)/4);
sy2 = ((j )/4);
sy2 = ( j == inDim[1]-1 )?(sy1):(sy2);
sy1 += outExt[2];
sy2 += outExt[2];
// Bounds check
if (sy2 > outExt[3])
{
sy2 = outExt[3];
}
tmpPtrJ = tmpPtrK + sy1 * outInc1;
for ( i = 0; i < inDim[0]; i++ )
{
sx1 = (i < 1)?(0):((i-1)/4);
sx2 = ((i )/4);
sx2 = ( i == inDim[0]-1 )?(sx1):(sx2);
sx1 += outExt[0];
sx2 += outExt[0];
// Bounds check
if (sx2 > outExt[1])
{
sx2 = outExt[1];
}
tmpPtrI = tmpPtrJ + sx1 * outInc0;
for ( c = 0; c < nComponents; c++, tmpPtrI += 3 )
{
if ( independent )
{
val = static_cast<unsigned short>((*dptr + shift[c]) * scale[c]);
++dptr;
}
else
{
val = static_cast<unsigned short>((*(dptr+components-1) +
shift[components-1]) * scale[components-1]);
dptr += components;
}
for ( z = sz1; z <= sz2; z++ )
{
for ( y = sy1; y <= sy2; y++ )
{
tmpPtr = tmpPtrI + (z-sz1)*outInc2 + (y-sy1)*outInc1;
for ( x = sx1; x <= sx2; x++, tmpPtr += outInc0 )
{
if (val < tmpPtr[0])
{
tmpPtr[0] = val;
}
if (val > tmpPtr[1])
{
tmpPtr[1] = val;
}
}
}
}
}
}
}
}
}
//----------------------------------------------------------------------------
// Fill in the maximum gradient magnitude space leaping information.
template <class T>
void
vtkVolumeRayCastSpaceLeapingImageFilterMaxGradientMagnitudeExecute(
vtkVolumeRayCastSpaceLeapingImageFilter *self,
vtkImageData *inData,
vtkImageData *outData, int outExt[6],
T )
{
// the number of independent components for which we need to keep track of
// min/max
const int nComponents = self->GetNumberOfIndependentComponents();
// B. Now fill in the max-min-gradient volume structure
// B.1 First compute the extents of the input that contribute to this structure
int inExt[6], inWholeExt[6];
int inDim[3];
int outWholeDim[3];
vtkVolumeRayCastSpaceLeapingImageFilter::ComputeInputExtentsForOutput(
inExt, inDim, outExt, inData );
inData->GetExtent(inWholeExt);
outData->GetDimensions(outWholeDim);
float shift[4], scale[4];
self->GetTableShift(shift);
self->GetTableScale(scale);
// B.2 Get increments to march through the input extents
vtkIdType inInc0, inInc1, inInc2;
inData->GetContinuousIncrements(self->GetCurrentScalars(),
inExt, inInc0, inInc1, inInc2);
// Get increments to march through the output extents
const vtkIdType outInc0 = 3*nComponents;
const vtkIdType outInc1 = outInc0*outWholeDim[0];
const vtkIdType outInc2 = outInc1*outWholeDim[1];
// B.3 Now fill in the min-max volume.
int i, j, k;
int c;
int sx1, sx2, sy1, sy2, sz1, sz2;
int x, y, z;
unsigned char val;
unsigned short *outBasePtr = static_cast< unsigned short * >(
outData->GetScalarPointer());
// The pointer into the space-leaping output volume.
unsigned short *tmpPtr, *tmpPtrK, *tmpPtrJ, *tmpPtrI;
// pointer to the slice of the gradient magnitude
unsigned char **gsptr = self->GetGradientMagnitude();
// Initialize pointer to the starting extents given by inExt.
gsptr += (inExt[4]-inWholeExt[4]);
for ( k = 0; k < inDim[2]; k++, ++gsptr )
{
sz1 = (k < 1)?(0):((k-1)/4);
sz2 = ((k )/4);
sz2 = ( k == inDim[2]-1 )?(sz1):(sz2);
sz1 += outExt[4];
sz2 += outExt[4];
// Bounds check
if (sz2 > outExt[5])
{
sz2 = outExt[5];
}
tmpPtrK = outBasePtr + sz1 * outInc2;
unsigned char *gptr = *gsptr;
for ( j = 0; j < inDim[1]; j++, gptr+= inInc1 )
{
sy1 = (j < 1)?(0):((j-1)/4);
sy2 = ((j )/4);
sy2 = ( j == inDim[1]-1 )?(sy1):(sy2);
sy1 += outExt[2];
sy2 += outExt[2];
// Bounds check
if (sy2 > outExt[3])
{
sy2 = outExt[3];
}
tmpPtrJ = tmpPtrK + sy1 * outInc1;
for ( i = 0; i < inDim[0]; i++ )
{
sx1 = (i < 1)?(0):((i-1)/4);
sx2 = ((i )/4);
sx2 = ( i == inDim[0]-1 )?(sx1):(sx2);
sx1 += outExt[0];
sx2 += outExt[0];
// Bounds check
if (sx2 > outExt[1])
{
sx2 = outExt[1];
}
tmpPtrI = tmpPtrJ + sx1 * outInc0;
for ( c = 0; c < nComponents; c++, tmpPtrI += 3 )
{
val = *gptr;
++gptr;
for ( z = sz1; z <= sz2; z++ )
{
for ( y = sy1; y <= sy2; y++ )
{
tmpPtr = tmpPtrI + (z-sz1)*outInc2 + (y-sy1)*outInc1;
for ( x = sx1; x <= sx2; x++, tmpPtr += outInc0 )
{
// Need to keep track of max gradient magnitude in upper
// eight bits. No need to preserve lower eight (the flag)
// since we will be recomputing this.
if (val>(tmpPtr[2]>>8))
{
tmpPtr[2] = (val<<8);
}
}
}
}
}
}
}
}
}
//----------------------------------------------------------------------------
// Optimized method that does both the following in one pass
// - Fill in the min-max space leaping information.
// - Fill in the maximum gradient magnitude space leaping information.
template <class T>
void
vtkVolumeRayCastSpaceLeapingImageFilterMinMaxAndMaxGradientMagnitudeExecute(
vtkVolumeRayCastSpaceLeapingImageFilter *self,
vtkImageData *inData,
vtkImageData *outData, int outExt[6],
T )
{
// the number of independent components for which we need to keep track of
// min/max
vtkDataArray * scalars = self->GetCurrentScalars();
const int components = scalars->GetNumberOfComponents();
const int independent = self->GetIndependentComponents();
const int nComponents = (independent) ? components : 1;
// B.1 First compute the extents of the input that contribute to this structure
int inExt[6], inWholeExt[6];
int inDim[3];
int outWholeDim[3];
vtkVolumeRayCastSpaceLeapingImageFilter::ComputeInputExtentsForOutput(
inExt, inDim, outExt, inData );
inData->GetExtent(inWholeExt);
outData->GetDimensions(outWholeDim);
float shift[4], scale[4];
self->GetTableShift(shift);
self->GetTableScale(scale);
// B.2 Get increments to march through the input extents
vtkIdType inInc0, inInc1, inInc2;
inData->GetContinuousIncrements(scalars,
inExt, inInc0, inInc1, inInc2);
// Get increments to march through the output extents
const vtkIdType outInc0 = 3*nComponents;
const vtkIdType outInc1 = outInc0*outWholeDim[0];
const vtkIdType outInc2 = outInc1*outWholeDim[1];
// B.3 Now fill in the min-max and gradient max structure
int i, j, k;
int c;
int sx1, sx2, sy1, sy2, sz1, sz2;
int x, y, z;
T *dptr = static_cast< T * >(scalars->GetVoidPointer(0));
unsigned char val;
unsigned short minMaxVal;
unsigned short *outBasePtr = static_cast< unsigned short * >(
outData->GetScalarPointer());
// pointer to the slice of the gradient magnitude
unsigned char **gsptr = self->GetGradientMagnitude();
// Initialize pointers to the starting extents given by inExt.
gsptr += (inExt[4]-inWholeExt[4]); // pointer to slice gradient
dptr += self->ComputeOffset( inExt, inWholeExt, nComponents );
// The pointer into the space-leaping output volume.
unsigned short *tmpPtr, *tmpPtrK, *tmpPtrJ, *tmpPtrI;
for ( k = 0; k < inDim[2]; k++, dptr += inInc2, ++gsptr )
{
sz1 = (k < 1)?(0):((k-1)/4);
sz2 = ((k )/4);
sz2 = ( k == inDim[2]-1 )?(sz1):(sz2);
sz1 += outExt[4];
sz2 += outExt[4];
// Bounds check
if (sz2 > outExt[5])
{
sz2 = outExt[5];
}
tmpPtrK = outBasePtr + sz1 * outInc2;
unsigned char *gptr = *gsptr;
for ( j = 0; j < inDim[1]; j++, dptr+= inInc1, gptr+= inInc1 )
{
sy1 = (j < 1)?(0):((j-1)/4);
sy2 = ((j )/4);
sy2 = ( j == inDim[1]-1 )?(sy1):(sy2);
sy1 += outExt[2];
sy2 += outExt[2];
// Bounds check
if (sy2 > outExt[3])
{
sy2 = outExt[3];
}
tmpPtrJ = tmpPtrK + sy1 * outInc1;
for ( i = 0; i < inDim[0]; i++ )
{
sx1 = (i < 1)?(0):((i-1)/4);
sx2 = ((i )/4);
sx2 = ( i == inDim[0]-1 )?(sx1):(sx2);
sx1 += outExt[0];
sx2 += outExt[0];
// Bounds check
if (sx2 > outExt[1])
{
sx2 = outExt[1];
}
tmpPtrI = tmpPtrJ + sx1 * outInc0;
for ( c = 0; c < nComponents; c++, tmpPtrI += 3 )
{
val = *gptr;
++gptr;
if ( independent )
{
minMaxVal = static_cast<unsigned short>((*dptr + shift[c]) * scale[c]);
++dptr;
}
else
{
minMaxVal = static_cast<unsigned short>((*(dptr+components-1) +
shift[components-1]) * scale[components-1]);
dptr += components;
}
for ( z = sz1; z <= sz2; z++ )
{
for ( y = sy1; y <= sy2; y++ )
{
tmpPtr = tmpPtrI + (z-sz1)*outInc2 + (y-sy1)*outInc1;
for ( x = sx1; x <= sx2; x++, tmpPtr += outInc0 )
{
if (minMaxVal<tmpPtr[0])
{
tmpPtr[0] = minMaxVal;
}
if (minMaxVal>tmpPtr[1])
{
tmpPtr[1] = minMaxVal;
}
if (val>(tmpPtr[2]>>8))
{
tmpPtr[2] = (val<<8);
}
}
}
}
}
}
}
}
}
//----------------------------------------------------------------------------
void vtkVolumeRayCastSpaceLeapingImageFilter
::FillScalarAndGradientOpacityFlags( vtkImageData *outData, int outExt[6] )
{
// Get increments to march through the output
vtkIdType outInc0, outInc1, outInc2;
outData->GetContinuousIncrements(outExt, outInc0, outInc1, outInc2);
// Now process the flags
unsigned short *tmpPtr = static_cast< unsigned short * >(
outData->GetScalarPointerForExtent(outExt));
unsigned short *minNonZeroScalarIndex
= this->GetMinNonZeroScalarIndex();
unsigned char *minNonZeroGradientMagnitudeIndex
= this->GetMinNonZeroGradientMagnitudeIndex();
int i, j, k, c, loop;
// the number of independent components for which we need to keep track of
// min/max/gradient
const int nComponents = this->GetNumberOfIndependentComponents();
// Loop over the data with in the supplied extents
for (k = outExt[4]; k <= outExt[5]; ++k, tmpPtr += outInc2)
{
for (j = outExt[2]; j <= outExt[3]; ++j, tmpPtr += outInc1)
{
for (i = outExt[0]; i <= outExt[1]; ++i)
{
for ( c = 0; c < nComponents; ++c, tmpPtr += 3 )
{
// We definite have 0 opacity because our maximum scalar value in
// this region is below the minimum scalar value with non-zero opacity
// for this component
if ( tmpPtr[1] < minNonZeroScalarIndex[c] )
{
tmpPtr[2] &= 0xff00;
}
// We have 0 opacity because we are using gradient magnitudes and
// the maximum gradient magnitude in this area is below the minimum
// gradient magnitude with non-zero opacity for this component
else if ( (tmpPtr[2]>>8) < minNonZeroGradientMagnitudeIndex[c] )
{
tmpPtr[2] &= 0xff00;
}
// We definitely have non-zero opacity because our minimum scalar
// value is lower than our first scalar with non-zero opacity, and
// the maximum scalar value is greater than this threshold - so
// we must encounter scalars with opacity in between
else if ( tmpPtr[0] < minNonZeroScalarIndex[c] )
{
tmpPtr[2] &= 0xff00;
tmpPtr[2] |= 0x0001;
}
// We have to search between min scalar value and the
// max scalar stored in the minmax volume to look for non-zero
// opacity since both values must be above our first non-zero
// threshold so we don't have information in this area
else
{
for ( loop = tmpPtr[0]; loop <= tmpPtr[1]; ++loop )
{
if ( this->ScalarOpacityTable[c][loop] )
{
break;
}
}
if ( loop <= tmpPtr[1] )
{
tmpPtr[2] &= 0xff00;
tmpPtr[2] |= 0x0001;
}
else
{
tmpPtr[2] &= 0xff00;
}
}
}
}
}
}
}
//----------------------------------------------------------------------------
void vtkVolumeRayCastSpaceLeapingImageFilter
::FillScalarOpacityFlags( vtkImageData *outData, int outExt[6] )
{
// Get increments to march through the output
vtkIdType outInc0, outInc1, outInc2;
outData->GetContinuousIncrements(outExt, outInc0, outInc1, outInc2);
// Now process the flags
unsigned short *tmpPtr = static_cast< unsigned short * >(
outData->GetScalarPointerForExtent(outExt));
unsigned short *minNonZeroScalarIndex
= this->GetMinNonZeroScalarIndex();
int i, j, k, c, loop;
// the number of independent components for which we need to keep track of
// min/max/gradient
const int nComponents = this->GetNumberOfIndependentComponents();
// Loop over the data with in the supplied extents
for (k = outExt[4]; k <= outExt[5]; ++k, tmpPtr += outInc2)
{
for (j = outExt[2]; j <= outExt[3]; ++j, tmpPtr += outInc1)
{
for (i = outExt[0]; i <= outExt[1]; ++i)
{
for ( c = 0; c < nComponents; ++c, tmpPtr += 3 )
{
// We definite have 0 opacity because our maximum scalar value in
// this region is below the minimum scalar value with non-zero opacity
// for this component
if ( tmpPtr[1] < minNonZeroScalarIndex[c] )
{
tmpPtr[2] &= 0xff00;
}
// We definitely have non-zero opacity because our minimum scalar
// value is lower than our first scalar with non-zero opacity, and
// the maximum scalar value is greater than this threshold - so
// we must encounter scalars with opacity in between
else if ( tmpPtr[0] < minNonZeroScalarIndex[c] )
{
tmpPtr[2] &= 0xff00;
tmpPtr[2] |= 0x0001;
}
// We have to search between min scalar value and the
// max scalar stored in the minmax volume to look for non-zero
// opacity since both values must be above our first non-zero
// threshold so we don't have information in this area
else
{
for ( loop = tmpPtr[0]; loop <= tmpPtr[1]; ++loop )
{
if ( this->ScalarOpacityTable[c][loop] )
{
break;
}
}
if ( loop <= tmpPtr[1] )
{
tmpPtr[2] &= 0xff00;
tmpPtr[2] |= 0x0001;
}
else
{
tmpPtr[2] &= 0xff00;
}
}
}
}
}
}
}
//----------------------------------------------------------------------------
void vtkVolumeRayCastSpaceLeapingImageFilter::ThreadedRequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **vtkNotUsed(inputVector),
vtkInformationVector *vtkNotUsed(outputVector),
vtkImageData ***inData,
vtkImageData **outData,
int outExt[6], int vtkNotUsed(id))
{
#ifdef vtkVolumeRayCastSpaceLeapingImageFilter_DEBUG
std::cout << "Thread id = " << id << std::endl;
#endif
// A. Initialize the data with a blank flag.
// - Get the number of independent components for which we need to keep
// track of min/max
if (!this->GetCurrentScalars())
{
return;
}
const int components = this->GetCurrentScalars()->GetNumberOfComponents();
const int nComponents = (this->GetIndependentComponents()) ? components : 1;
// Clear the output if we are computing the min-max. In other cases, we
// will be re-using the cache. (See the method AllocateOutputData)
if (this->ComputeMinMax)
{
vtkVolumeRayCastSpaceLeapingImageFilterClearOutput(
outData[0], outExt, nComponents );
}
// If only scalar min-max need to be re-computed
if (this->ComputeMinMax && !this->ComputeGradientOpacity)
{
int scalarType = this->CurrentScalars->GetDataType();
switch (scalarType)
{
vtkTemplateMacro(
vtkVolumeRayCastSpaceLeapingImageFilterMinMaxExecute(
this, inData[0][0], outData[0], outExt, static_cast<VTK_TT>(0))
);
default:
vtkErrorMacro("Unknown scalar type");
return;
}
}
// If only gradient max needs to be recomputed
else if (this->ComputeGradientOpacity && !this->ComputeMinMax)
{
int scalarType = this->CurrentScalars->GetDataType();
switch (scalarType)
{
vtkTemplateMacro(
vtkVolumeRayCastSpaceLeapingImageFilterMaxGradientMagnitudeExecute(
this, inData[0][0], outData[0], outExt, static_cast<VTK_TT>(0))
);
default:
vtkErrorMacro("Unknown scalar type");
return;
}
}
// If both scalar min-max need to be re-computed and gradient max needs to
// be re-computed
else if (this->ComputeGradientOpacity && this->ComputeMinMax)
{
int scalarType = this->CurrentScalars->GetDataType();
switch (scalarType)
{
vtkTemplateMacro(
vtkVolumeRayCastSpaceLeapingImageFilterMinMaxAndMaxGradientMagnitudeExecute(
this, inData[0][0], outData[0], outExt, static_cast<VTK_TT>(0))
);
default:
vtkErrorMacro("Unknown scalar type");
return;
}
}
// Update the flags now for this extent. There are two specialized methods
// here, depending on what mode we are in, so that we may do the flag update
// in one pass through the data.
if (this->UpdateGradientOpacityFlags)
{
// Process the flags based on the computed min-max volume
this->FillScalarAndGradientOpacityFlags(outData[0], outExt);
}
else
{
this->FillScalarOpacityFlags(outData[0], outExt);
}
}
//----------------------------------------------------------------------------
// Override superclass method to maintain a last successful execution time
int vtkVolumeRayCastSpaceLeapingImageFilter::RequestData(
vtkInformation* request,
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
#ifdef vtkVolumeRayCastSpaceLeapingImageFilter_DEBUG
cout << "ComputingGradientOpacity: " << ComputeGradientOpacity;
cout << " ComputingMinMax: " << ComputeMinMax << " UpdatingFlags: 1" << endl;
#endif
// Find the first non-zero scalar opacity and gradient opacity points on
// the respective transfer functions
this->ComputeFirstNonZeroOpacityIndices();
// The actual work is done in the line below.
if (this->Superclass::RequestData(request, inputVector, outputVector))
{
// if we recomputed the first two shorts in the output, update this.
if (this->ComputeGradientOpacity || this->ComputeMinMax)
{
this->LastMinMaxBuildTime.Modified();
}
// flags were rebuilt. update this.
this->LastMinMaxFlagTime.Modified();
return 1;
}
return 0;
}
//----------------------------------------------------------------------------
int vtkVolumeRayCastSpaceLeapingImageFilter::RequestInformation (
vtkInformation * request,
vtkInformationVector** inputVector,
vtkInformationVector * outputVector)
{
this->vtkImageAlgorithm::RequestInformation(request, inputVector, outputVector);
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation* outInfo = outputVector->GetInformationObject(0);
// Output scalar type is unsigned short,
// 3 unsigned short values are needed to represent the min, max and gradient,
// flag values. This is to be done for each independent component.
vtkDataObject::SetPointDataActiveScalarInfo(outInfo,
VTK_UNSIGNED_SHORT,
3 * this->GetNumberOfIndependentComponents() );
// The whole extent of the output is the whole extent of the input divided