forked from FNNDSC/KWWidgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkKWHistogram.cxx
1399 lines (1179 loc) · 39.2 KB
/
vtkKWHistogram.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
/*=========================================================================
Module: $RCSfile: vtkKWHistogram.cxx,v $
Copyright (c) Kitware, Inc.
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 "vtkKWHistogram.h"
#include "vtkColorTransferFunction.h"
#include "vtkCommand.h"
#include "vtkDataArray.h"
#include "vtkImageData.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkDoubleArray.h"
#include "vtkIntArray.h"
#include "vtkMath.h"
#include <float.h>
#define VTK_KW_HIST_TESTING 0
#define VTK_KW_ASSUME_LOWER_BOUND_IS_IN_TYPE_RANGE 1
//----------------------------------------------------------------------------
vtkStandardNewMacro(vtkKWHistogram);
//vtkCxxRevisionMacro(vtkKWHistogram, "$Revision: 1.15 $");
//----------------------------------------------------------------------------
vtkKWHistogram::vtkKWHistogram()
{
this->Range[0] = 0;
this->Range[1] = 1.0;
this->Bins = vtkDoubleArray::New();
this->EmptyHistogram();
this->Image = NULL;
this->ImageCoordinates = NULL;
this->LastImageDescriptor = NULL;
this->LastImageBuildTime = 0;
this->LastTransferFunctionTime = 0;
this->LogMode = 1;
this->LastStatisticsBuildTime = 0;
this->MaximumNumberOfBins = 10000;
}
//----------------------------------------------------------------------------
vtkKWHistogram::~vtkKWHistogram()
{
if (this->Bins)
{
this->Bins->Delete();
this->Bins = NULL;
}
if (this->Image)
{
this->Image->Delete();
this->Image = NULL;
}
if (this->ImageCoordinates)
{
this->ImageCoordinates->Delete();
this->ImageCoordinates = NULL;
}
if (this->LastImageDescriptor)
{
delete this->LastImageDescriptor;
this->LastImageDescriptor = NULL;
}
}
//----------------------------------------------------------------------------
vtkIdType vtkKWHistogram::GetNumberOfBins()
{
if (this->Bins)
{
return this->Bins->GetNumberOfTuples();
}
return 0;
}
//----------------------------------------------------------------------------
// Special version for unsigned long: since we can not promote to any
// signed bigger type, use double for safety (I have no data to verify)
void vtkKWHistogramBuildInt(
unsigned long *data, vtkIdType nb_tuples, int nb_of_components, vtkKWHistogram *self)
{
if (!data || !nb_tuples || nb_of_components < 1 || !self)
{
return;
}
typedef double cast_type;
double *bins_ptr = self->GetBins()->GetPointer(0);
cast_type shift = (cast_type)self->GetRange()[0];
unsigned long *data_end = data + nb_tuples * nb_of_components;
while (data < data_end)
{
bins_ptr[(int)(cast_type(*data) - shift)]++;
data += nb_of_components;
}
}
// Normal version for any integer type
#if VTK_KW_ASSUME_LOWER_BOUND_IS_IN_TYPE_RANGE
template <class T>
void vtkKWHistogramBuildInt(
T *data, vtkIdType nb_tuples, int nb_of_components, vtkKWHistogram *self)
{
if (!data || !nb_tuples || nb_of_components < 1 || !self)
{
return;
}
double *bins_ptr = self->GetBins()->GetPointer(0);
T shift = (T)self->GetRange()[0];
T *data_end = data + nb_tuples * nb_of_components;
while (data < data_end)
{
bins_ptr[*data - shift]++;
data += nb_of_components;
}
}
#else
template <class T>
void vtkKWHistogramBuildInt(
T *data, vtkIdType nb_tuples, int nb_of_components, vtkKWHistogram *self)
{
if (!data || !nb_tuples || nb_of_components < 1 || !self)
{
return;
}
typedef long cast_type;
double *bins_ptr = self->GetBins()->GetPointer(0);
cast_type shift = (cast_type)self->GetRange()[0];
T *data_end = data + nb_tuples * nb_of_components;
while (data < data_end)
{
bins_ptr[cast_type(*data) - shift]++;
data += nb_of_components;
}
}
#endif
template <class T>
void vtkKWHistogramBuildFloat(
T *data, vtkIdType nb_tuples, int nb_of_components, vtkKWHistogram *self)
{
if (!data || !nb_tuples || nb_of_components < 1 || !self)
{
return;
}
double range[2];
self->GetRange(range);
double bin_width =
(range[1] == range[0] ? 1 :
(double)self->GetNumberOfBins() / (range[1] - range[0]));
double *bins_ptr = self->GetBins()->GetPointer(0);
int index;
T *data_end = data + nb_tuples * nb_of_components;
while (data < data_end)
{
#if defined(WIN32) && defined(_MSC_VER)
if (!_isnan((double)*data))
#endif
{
index = vtkMath::Floor(((double)*data - range[0]) * bin_width);
bins_ptr[index]++;
}
data += nb_of_components;
}
}
template <class T>
void vtkKWHistogramBuildIntOrFloat(
T *data, vtkIdType nb_tuples, int nb_of_components, vtkKWHistogram *self)
{
if (!data || !nb_tuples || nb_of_components < 1 || !self)
{
return;
}
double range[2];
self->GetRange(range);
if (self->GetNumberOfBins() == (vtkIdType)(range[1] - range[0]))
{
vtkKWHistogramBuildInt(data, nb_tuples, nb_of_components, self);
}
else
{
vtkKWHistogramBuildFloat(data, nb_tuples, nb_of_components, self);
}
}
//----------------------------------------------------------------------------
void vtkKWHistogram::EmptyHistogram()
{
if (this->Bins)
{
this->Bins->SetNumberOfComponents(1);
this->Bins->SetNumberOfTuples(0);
}
}
//----------------------------------------------------------------------------
void vtkKWHistogram::EstimateHistogramRangeAndNumberOfBins(
vtkDataArray *scalars,
int comp,
double range[2],
vtkIdType *nb_of_bins)
{
if (!scalars ||
comp < 0 || comp >= scalars->GetNumberOfComponents() ||
!range || !nb_of_bins)
{
vtkWarningMacro( "Missing parameters" );
return;
}
// Get the histogram range required for those scalars.
// For some big data types (int, long, double), let's use the more
// accurate GetScalarRange(..., double) function, which does
// not cache the range (it iterates over data each time), but
// get the proper result and avoid accessing memory out of the array.
// If 'reset_range' is true, the Range ivar is reset to the range
// that was just computed. If not (accumulate mode), make sure the
// current range fits inside the Range ivar.
// Given the range, compute the number of bins required.
double dscalars_range[2];
double try_nb_of_bins, delta;
switch (scalars->GetDataType())
{
case VTK_CHAR:
case VTK_SIGNED_CHAR:
case VTK_UNSIGNED_CHAR:
range[0] = scalars->GetDataTypeMin();
range[1] = scalars->GetDataTypeMax() + 1.0;
try_nb_of_bins = (range[1] - range[0]);
break;
case VTK_SHORT:
case VTK_UNSIGNED_SHORT:
scalars->GetRange(dscalars_range, comp);
range[0] = dscalars_range[0];
range[1] = dscalars_range[1] + 1.0;
try_nb_of_bins = (range[1] - range[0]);
break;
case VTK_INT:
case VTK_UNSIGNED_INT:
case VTK_LONG:
case VTK_UNSIGNED_LONG:
scalars->GetRange(dscalars_range, comp);
range[0] = dscalars_range[0];
range[1] = dscalars_range[1] + 1.0;
try_nb_of_bins = (range[1] - range[0]);
break;
case VTK_FLOAT:
scalars->GetRange(dscalars_range, comp);
delta = (dscalars_range[1] - dscalars_range[0]) * 0.01;
range[0] = dscalars_range[0];
range[1] = dscalars_range[1] + delta;
try_nb_of_bins = this->MaximumNumberOfBins;
break;
case VTK_DOUBLE:
scalars->GetRange(dscalars_range, comp);
delta = (dscalars_range[1] - dscalars_range[0]) * 0.01;
range[0] = dscalars_range[0];
range[1] = dscalars_range[1] + delta;
try_nb_of_bins = this->MaximumNumberOfBins;
break;
default:
vtkErrorMacro( "Unhandled data type: " << scalars->GetDataType() );
return;
}
// Check if too large number of bins, or exceeded capacity
if (try_nb_of_bins > this->MaximumNumberOfBins || try_nb_of_bins < 1)
{
*nb_of_bins = this->MaximumNumberOfBins;
}
else
{
*nb_of_bins = (vtkIdType)ceil(try_nb_of_bins);
}
}
//----------------------------------------------------------------------------
void vtkKWHistogram::EstimateHistogramRange(
vtkDataArray *scalars,
int comp,
double range[2])
{
vtkIdType nb_bins;
vtkKWHistogram::EstimateHistogramRangeAndNumberOfBins(
scalars, comp, range, &nb_bins);
}
//----------------------------------------------------------------------------
void vtkKWHistogram::UpdateHistogram(vtkDataArray *scalars,
int comp,
int reset_range)
{
if (!scalars)
{
vtkErrorMacro(<< "Can not build histogram from NULL scalars!");
return;
}
if (comp < 0 || comp >= scalars->GetNumberOfComponents())
{
vtkErrorMacro(<< "Can not build histogram from invalid component!");
return;
}
// Get the histogram range required for those scalars.
// Also, given the range, compute the number of bins needed
// if 'reset_range' is true, reset the Range ivar, otherwise check
// that the current range is within the ivar (accumulate mode)
double range[2];
vtkIdType nb_of_bins;
this->EstimateHistogramRangeAndNumberOfBins(
scalars, comp, range, &nb_of_bins);
if (reset_range)
{
this->Range[0] = range[0];
this->Range[1] = range[1];
}
else
{
if (range[0] < this->Range[0] || range[1] > this->Range[1])
{
vtkErrorMacro(<< "Scalars range ["
<< range[0] << ".." << range[1] << "] "
<< "does not fit in the current Range ["
<< this->Range[0] << ".." << this->Range[1] << "]!");
return;
}
}
this->InvokeEvent(vtkCommand::StartEvent, NULL);
// Allocate the bins
// if 'reset_range' is true, reset the number of bins, otherwise reset
// it only if it was 0 before (i.e., EmptyHistogram was called).
this->Bins->SetNumberOfComponents(1);
vtkIdType old_nb_of_bins = this->Bins->GetNumberOfTuples();
if (reset_range)
{
this->Bins->SetNumberOfTuples(nb_of_bins);
}
else
{
if (old_nb_of_bins == 0)
{
this->Bins->SetNumberOfTuples(nb_of_bins);
}
}
// Reset the bins to 0
if (nb_of_bins != old_nb_of_bins)
{
double *bins_ptr = this->Bins->GetPointer(0);
double *bins_ptr_end = bins_ptr + nb_of_bins;
while (bins_ptr < bins_ptr_end)
{
*bins_ptr++ = 0.0;
}
}
double progress = 0.2;
this->InvokeEvent(vtkCommand::ProgressEvent, &progress);
// Loop over the data and fill in the bins
vtkIdType nb_of_components = scalars->GetNumberOfComponents();
vtkIdType nb_of_tuples = scalars->GetNumberOfTuples();
vtkIdType inc_tuple = (vtkIdType)ceil((double)nb_of_tuples / 5.0);
vtkIdType start_tuple = 0;
while (start_tuple < nb_of_tuples)
{
// Make sure we are really one after...
if (start_tuple + inc_tuple >= nb_of_tuples)
{
inc_tuple = nb_of_tuples - start_tuple;
}
switch (scalars->GetDataType())
{
case VTK_CHAR:
{
typedef char type;
type *data = static_cast<type *>
(scalars->GetVoidPointer(start_tuple * nb_of_components)) + comp;
vtkKWHistogramBuildIntOrFloat(
data, inc_tuple, nb_of_components, this);
}
break;
case VTK_SIGNED_CHAR:
{
typedef signed char type;
type *data = static_cast<type *>
(scalars->GetVoidPointer(start_tuple * nb_of_components)) + comp;
vtkKWHistogramBuildIntOrFloat(
data, inc_tuple, nb_of_components, this);
}
break;
case VTK_UNSIGNED_CHAR:
{
typedef unsigned char type;
type *data = static_cast<type *>
(scalars->GetVoidPointer(start_tuple * nb_of_components)) + comp;
vtkKWHistogramBuildIntOrFloat(
data, inc_tuple, nb_of_components, this);
}
break;
case VTK_SHORT:
{
typedef short type;
type *data = static_cast<type *>
(scalars->GetVoidPointer(start_tuple * nb_of_components)) + comp;
vtkKWHistogramBuildIntOrFloat(
data, inc_tuple, nb_of_components, this);
}
break;
case VTK_UNSIGNED_SHORT:
{
typedef unsigned short type;
type *data = static_cast<type *>
(scalars->GetVoidPointer(start_tuple * nb_of_components)) + comp;
vtkKWHistogramBuildIntOrFloat(
data, inc_tuple, nb_of_components, this);
}
break;
case VTK_INT:
{
typedef int type;
type *data = static_cast<type *>
(scalars->GetVoidPointer(start_tuple * nb_of_components)) + comp;
vtkKWHistogramBuildIntOrFloat(
data, inc_tuple, nb_of_components, this);
}
break;
case VTK_UNSIGNED_INT:
{
typedef unsigned int type;
type *data = static_cast<type *>
(scalars->GetVoidPointer(start_tuple * nb_of_components)) + comp;
vtkKWHistogramBuildIntOrFloat(
data, inc_tuple, nb_of_components, this);
}
break;
case VTK_LONG:
{
typedef long type;
type *data = static_cast<type *>
(scalars->GetVoidPointer(start_tuple * nb_of_components)) + comp;
vtkKWHistogramBuildIntOrFloat(
data, inc_tuple, nb_of_components, this);
}
break;
case VTK_UNSIGNED_LONG:
{
typedef unsigned long type;
type *data = static_cast<type *>
(scalars->GetVoidPointer(start_tuple * nb_of_components)) + comp;
vtkKWHistogramBuildIntOrFloat(
data, inc_tuple, nb_of_components, this);
}
break;
case VTK_FLOAT:
{
typedef float type;
type *data = static_cast<type *>
(scalars->GetVoidPointer(start_tuple * nb_of_components)) + comp;
vtkKWHistogramBuildFloat(
data, inc_tuple, nb_of_components, this);
}
break;
case VTK_DOUBLE:
{
typedef double type;
type *data = static_cast<type *>
(scalars->GetVoidPointer(start_tuple * nb_of_components)) + comp;
vtkKWHistogramBuildFloat(
data, inc_tuple, nb_of_components, this);
}
break;
default:
vtkErrorMacro(
<< "Can not build histogram from unsupported data type!");
return;
}
progress = 0.2 + ((double)start_tuple / (double)nb_of_tuples) * 0.8;
this->InvokeEvent(vtkCommand::ProgressEvent, &progress);
start_tuple += inc_tuple;
}
progress = 1.0;
this->InvokeEvent(vtkCommand::ProgressEvent, &progress);
this->Bins->Modified();
this->Modified();
this->InvokeEvent(vtkCommand::EndEvent, NULL);
}
//----------------------------------------------------------------------------
void vtkKWHistogram::BuildHistogram(vtkDataArray *scalars, int comp)
{
this->UpdateHistogram(scalars, comp, 1);
}
//----------------------------------------------------------------------------
void vtkKWHistogram::AccumulateHistogram(vtkDataArray *scalars, int comp)
{
this->UpdateHistogram(scalars, comp, 0);
}
//----------------------------------------------------------------------------
vtkKWHistogram::ImageDescriptor::ImageDescriptor()
{
this->Style = vtkKWHistogram::ImageDescriptor::StyleBars;
this->Width = 0;
this->Height = 0;
this->DrawForeground = 1;
this->DrawBackground = 1;
this->DrawGrid = 0;
this->GridSize = 15;
this->ColorTransferFunction = NULL;
this->DefaultMaximumOccurence = 0.0;
this->LastMaximumOccurence = 0.0;
this->Range[0] = 0.0;
this->Range[1] = 0.0;
this->Color[0] = 0.63;
this->Color[1] = 0.63;
this->Color[2] = 0.63;
this->BackgroundColor[0] = 0.83;
this->BackgroundColor[1] = 0.83;
this->BackgroundColor[2] = 0.83;
this->OutOfRangeColor[0] = 0.83;
this->OutOfRangeColor[1] = 0.83;
this->OutOfRangeColor[2] = 0.83;
this->GridColor[0] = 0.93;
this->GridColor[1] = 0.93;
this->GridColor[2] = 0.93;
}
//----------------------------------------------------------------------------
int vtkKWHistogram::ImageDescriptor::IsEqualTo(const ImageDescriptor *desc)
{
return (
desc &&
this->Range[0] == desc->Range[0] &&
this->Range[1] == desc->Range[1] &&
this->Width == desc->Width &&
this->Height == desc->Height &&
this->Style == desc->Style &&
this->DrawForeground == desc->DrawForeground &&
this->DrawBackground == desc->DrawBackground &&
this->DrawGrid == desc->DrawGrid &&
this->GridSize == desc->GridSize &&
this->Color[0] == desc->Color[0] &&
this->Color[1] == desc->Color[1] &&
this->Color[2] == desc->Color[2] &&
this->BackgroundColor[0] == desc->BackgroundColor[0] &&
this->BackgroundColor[1] == desc->BackgroundColor[1] &&
this->BackgroundColor[2] == desc->BackgroundColor[2] &&
this->OutOfRangeColor[0] == desc->OutOfRangeColor[0] &&
this->OutOfRangeColor[1] == desc->OutOfRangeColor[1] &&
this->OutOfRangeColor[2] == desc->OutOfRangeColor[2] &&
this->GridColor[0] == desc->GridColor[0] &&
this->GridColor[1] == desc->GridColor[1] &&
this->GridColor[2] == desc->GridColor[2] &&
this->ColorTransferFunction == desc->ColorTransferFunction
);
}
//----------------------------------------------------------------------------
void vtkKWHistogram::ImageDescriptor::Copy(const ImageDescriptor *desc)
{
if (!desc)
{
return;
}
this->Range[0] = desc->Range[0];
this->Range[1] = desc->Range[1];
this->Width = desc->Width;
this->Height = desc->Height;
this->Style = desc->Style;
this->DrawForeground = desc->DrawForeground;
this->DrawBackground = desc->DrawBackground;
this->DrawGrid = desc->DrawGrid;
this->GridSize = desc->GridSize;
this->Color[0] = desc->Color[0];
this->Color[1] = desc->Color[1];
this->Color[2] = desc->Color[2];
this->BackgroundColor[0] = desc->BackgroundColor[0];
this->BackgroundColor[1] = desc->BackgroundColor[1];
this->BackgroundColor[2] = desc->BackgroundColor[2];
this->OutOfRangeColor[0] = desc->OutOfRangeColor[0];
this->OutOfRangeColor[1] = desc->OutOfRangeColor[1];
this->OutOfRangeColor[2] = desc->OutOfRangeColor[2];
this->GridColor[0] = desc->GridColor[0];
this->GridColor[1] = desc->GridColor[1];
this->GridColor[2] = desc->GridColor[2];
this->ColorTransferFunction = desc->ColorTransferFunction;
}
//----------------------------------------------------------------------------
int vtkKWHistogram::ImageDescriptor::IsValid() const
{
return (
this->Range[0] != this->Range[1] &&
this->Width &&
this->Height &&
(this->Color[0] >= 0.0 && this->Color[0] <= 1.0) &&
(this->Color[1] >= 0.0 && this->Color[1] <= 1.0) &&
(this->Color[2] >= 0.0 && this->Color[2] <= 1.0) &&
(this->BackgroundColor[0] >= 0.0 && this->BackgroundColor[0] <= 1.0) &&
(this->BackgroundColor[1] >= 0.0 && this->BackgroundColor[1] <= 1.0) &&
(this->BackgroundColor[2] >= 0.0 && this->BackgroundColor[2] <= 1.0) &&
(this->OutOfRangeColor[0] >= 0.0 && this->OutOfRangeColor[0] <= 1.0) &&
(this->OutOfRangeColor[1] >= 0.0 && this->OutOfRangeColor[1] <= 1.0) &&
(this->OutOfRangeColor[2] >= 0.0 && this->OutOfRangeColor[2] <= 1.0) &&
(this->GridColor[0] >= 0.0 && this->GridColor[0] <= 1.0) &&
(this->GridColor[1] >= 0.0 && this->GridColor[1] <= 1.0) &&
(this->GridColor[2] >= 0.0 && this->GridColor[2] <= 1.0) &&
this->GridSize > 0
);
}
//----------------------------------------------------------------------------
void vtkKWHistogram::ImageDescriptor::SetRange(double range0, double range1)
{
this->Range[0] = range0;
this->Range[1] = range1;
}
//----------------------------------------------------------------------------
void vtkKWHistogram::ImageDescriptor::SetRange(double range[2])
{
this->SetRange(range[0], range[1]);
}
//----------------------------------------------------------------------------
void vtkKWHistogram::ImageDescriptor::SetDimensions(unsigned int width,
unsigned int height)
{
this->Width = width;
this->Height = height;
}
//----------------------------------------------------------------------------
void vtkKWHistogram::ImageDescriptor::SetColor(double color[3])
{
this->Color[0] = color[0];
this->Color[1] = color[1];
this->Color[2] = color[2];
}
//----------------------------------------------------------------------------
void vtkKWHistogram::ImageDescriptor::SetBackgroundColor(double color[3])
{
this->BackgroundColor[0] = color[0];
this->BackgroundColor[1] = color[1];
this->BackgroundColor[2] = color[2];
}
//----------------------------------------------------------------------------
void vtkKWHistogram::ImageDescriptor::SetOutOfRangeColor(double color[3])
{
this->OutOfRangeColor[0] = color[0];
this->OutOfRangeColor[1] = color[1];
this->OutOfRangeColor[2] = color[2];
}
//----------------------------------------------------------------------------
void vtkKWHistogram::ImageDescriptor::SetGridColor(double color[3])
{
this->GridColor[0] = color[0];
this->GridColor[1] = color[1];
this->GridColor[2] = color[2];
}
//----------------------------------------------------------------------------
int vtkKWHistogram::IsImageUpToDate(
const ImageDescriptor *desc)
{
// Create new image if it does not exist (same with descriptor)
if (!this->Image)
{
this->Image = vtkImageData::New();
}
if (!this->ImageCoordinates)
{
this->ImageCoordinates = vtkIntArray::New();
}
if (!this->LastImageDescriptor)
{
this->LastImageDescriptor = new vtkKWHistogram::ImageDescriptor;
}
return (this->LastImageBuildTime >= this->Bins->GetMTime() &&
(!desc ||
(this->LastImageDescriptor->IsEqualTo(desc) &&
(!desc->ColorTransferFunction ||
this->LastTransferFunctionTime >=
desc->ColorTransferFunction->GetMTime()))));
}
//----------------------------------------------------------------------------
int vtkKWHistogram::RefreshImage(ImageDescriptor *desc)
{
// Check histogram
if (!this->GetNumberOfBins())
{
vtkErrorMacro(<< "Can not compute histogram image from empty histogram!");
return 0;
}
// Check descriptor
if (desc && !desc->IsValid())
{
vtkErrorMacro(
<< "Can not compute histogram image with invalid descriptor!");
return 0;
}
// Do we really need to recreate ?
if (this->IsImageUpToDate(desc))
{
return 1;
}
// We need width x height RGBA (unsigned char)
int draw_image = desc->DrawForeground || desc->DrawGrid;
int nb_of_components = 3 + (desc->DrawBackground ? 0 : 1);
unsigned char *image_ptr = NULL;
if (draw_image)
{
this->Image->SetDimensions(desc->Width, desc->Height, 1);
this->Image->SetExtent(this->Image->GetExtent());
//this->Image->SetUpdateExtent(this->Image->GetExtent());
//this->Image->SetScalarTypeToUnsignedChar();
//this->Image->SetNumberOfScalarComponents(nb_of_components);
this->Image->AllocateScalars(VTK_UNSIGNED_CHAR, nb_of_components);
image_ptr =
static_cast<unsigned char*>(this->Image->GetScalarPointer());
}
unsigned char *column_ptr = NULL;
// Colors
unsigned char color[3], bgcolor[3], gridcolor[3], oorcolor[3];
color[0] = (unsigned char)(255.0 * desc->Color[0]);
color[1] = (unsigned char)(255.0 * desc->Color[1]);
color[2] = (unsigned char)(255.0 * desc->Color[2]);
bgcolor[0] = (unsigned char)(255.0 * desc->BackgroundColor[0]);
bgcolor[1] = (unsigned char)(255.0 * desc->BackgroundColor[1]);
bgcolor[2] = (unsigned char)(255.0 * desc->BackgroundColor[2]);
oorcolor[0] = (unsigned char)(255.0 * desc->OutOfRangeColor[0]);
oorcolor[1] = (unsigned char)(255.0 * desc->OutOfRangeColor[1]);
oorcolor[2] = (unsigned char)(255.0 * desc->OutOfRangeColor[2]);
gridcolor[0] = (unsigned char)(255.0 * desc->GridColor[0]);
gridcolor[1] = (unsigned char)(255.0 * desc->GridColor[1]);
gridcolor[2] = (unsigned char)(255.0 * desc->GridColor[2]);
double *tfunccolors = NULL;
if (desc->ColorTransferFunction)
{
tfunccolors = new double[3 * desc->Width];
desc->ColorTransferFunction->GetTable(
desc->Range[0], desc->Range[1], desc->Width, tfunccolors);
}
// Quickly set the whole image to be transparent if no background color
// is going to be used
if (draw_image && !desc->DrawBackground)
{
memset(image_ptr, 0, desc->Width * desc->Height * nb_of_components);
}
// Fill the image
double *bins_ptr = this->Bins->GetPointer(0);
double value, next_value;
double occurrence;
// width of bins in the original histogram.
double bin_width =
(this->Range[1] == this->Range[0] ? 1 :
(this->Range[1] - this->Range[0]) / (double)this->GetNumberOfBins());
// Range of the original histogram that is visible in the interface.
double hist_subrange = desc->Range[1] - desc->Range[0];
// Scale between pixel units in the image of the histogram on the interface
// and value units on the scale of the original histogram. Multiplying by
// x_scale converts pixel units into histogram domain units.
double x_scale = hist_subrange / (double)(desc->Width - 1);
vtkIdType bin, next_bin, iter_bin;
int row_length = desc->Width * nb_of_components;
double bin_real;
double next_bin_real;
double max_occurrence = 0.0;
this->ImageCoordinates->SetNumberOfComponents(1);
this->ImageCoordinates->SetNumberOfTuples(desc->Width);
int *image_coords = this->ImageCoordinates->GetPointer(0);
double *resampled_histogram = new double[desc->Width];
unsigned int x, y, ylimit;
for (x = 0; x < desc->Width; x++)
{
// Get the value and occurrence for this x (column)
value = desc->Range[0] + x_scale * (double)x;
// "occurrence" cumulates area under the histogram inside a particular
// range.
occurrence = 0;
// Has to be in the range (upper range is exclusive)
// Collect all the bins up to the next value (for the next pixel)
if (value >= this->Range[0] && value < this->Range[1])
{
bin_real = (value - this->Range[0]) / bin_width;
bin = (vtkIdType)floor(bin_real);
next_value = desc->Range[0] + x_scale * (double)(x+1);
next_bin_real = (next_value - this->Range[0]) / bin_width;
next_bin = (vtkIdType)floor(next_bin_real);
if (next_bin > this->GetNumberOfBins())
{
next_bin = this->GetNumberOfBins();
}
iter_bin = bin;
while (++iter_bin < next_bin)
{
// We should cumulate values from the bins
occurrence += bins_ptr[iter_bin];
}
// Compute the partial contribution from the first bin and last bin
// This is done differently depending on whether bin and next_bin are
// the same or not.
if( next_bin != bin )
{
// Compute the partial contribution from the first bin plus the
// contribution of the last one. This is a fraction of the counts on
// each bin.
occurrence += bins_ptr[bin] * (1.0 - (bin_real - bin));
if (next_bin < this->GetNumberOfBins())
{
occurrence += bins_ptr[next_bin] * (next_bin_real - next_bin);
}
}
else
{
// Otherwise, if they are the same, the number of counts must
// be scaled by the ratio between bin widths of the original
// and the resampled histogram.
occurrence = bins_ptr[bin] * x_scale / bin_width ;
}
// Height of histogram bars should be such that the area is conserved.
// we should divide by the range of values that are now mapped in a
// single bin on the histogram visible on the interface.
resampled_histogram[x] = x_scale ? (occurrence / x_scale) : 0;
if( resampled_histogram[x] > max_occurrence )
{
max_occurrence = resampled_histogram[x];
}
}
// Out of range, set the occurence to -1, so that it can be
// used as a flag in the next loop, which draws pixels
else
{
resampled_histogram[x] = -1.0;
}
}
if (desc->DefaultMaximumOccurence > 0.0 &&
desc->DefaultMaximumOccurence > max_occurrence)
{
max_occurrence = desc->DefaultMaximumOccurence;
}
desc->LastMaximumOccurence = max_occurrence;
#if VTK_KW_HIST_TESTING
cout << this << " : vtkKWHistogram::GetImage: "
<< "(" << desc->Range[0] << ".." << desc->Range[1] << ") from "
<< "(" << this->Range[0] << ".." << this->Range[1] << ") "
<< desc->Width << "x" << desc->Height
<< " (max occ: " << max_occurrence << ")"
<< endl;
#endif
for (x = 0; x < desc->Width; x++)
{
// If the occurence is < 0, it is a flag that in fact we were
// out of range for that pixel column
int out_of_range = (resampled_histogram[x] < 0);
occurrence = resampled_histogram[x] < 0 ? 0 : resampled_histogram[x];