forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkColorTransferFunction.cxx
1943 lines (1715 loc) · 51.7 KB
/
vtkColorTransferFunction.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: vtkColorTransferFunction.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 "vtkColorTransferFunction.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include <algorithm>
#include <iterator>
#include <cmath>
#include <set>
#include <vector>
vtkStandardNewMacro(vtkColorTransferFunction);
#define MY_MAX(x, y) ((x) > (y) ? (x) : (y))
//=============================================================================
class vtkCTFNode
{
public:
double X;
double R;
double G;
double B;
double Sharpness;
double Midpoint;
};
class vtkCTFCompareNodes
{
public:
bool operator () ( const vtkCTFNode *node1,
const vtkCTFNode *node2 )
{
return node1->X < node2->X;
}
};
class vtkCTFFindNodeEqual
{
public:
double X;
bool operator () ( const vtkCTFNode *node )
{
return node->X == this->X;
}
};
class vtkCTFFindNodeInRange
{
public:
double X1;
double X2;
bool operator () (const vtkCTFNode *node )
{
return ( node->X >= this->X1 &&
node->X <= this->X2 );
}
};
class vtkCTFFindNodeOutOfRange
{
public:
double X1;
double X2;
bool operator () (const vtkCTFNode *node )
{
return ( node->X < this->X1 ||
node->X > this->X2 );
}
};
class vtkColorTransferFunctionInternals
{
public:
std::vector<vtkCTFNode*> Nodes;
vtkCTFCompareNodes CompareNodes;
vtkCTFFindNodeEqual FindNodeEqual;
vtkCTFFindNodeInRange FindNodeInRange;
vtkCTFFindNodeOutOfRange FindNodeOutOfRange;
};
//=============================================================================
// Convert to and from a special polar version of CIELAB (useful for creating
// continuous diverging color maps).
inline void vtkColorTransferFunctionLabToMsh(const double lab[3], double msh[3])
{
const double &L = lab[0];
const double &a = lab[1];
const double &b = lab[2];
double &M = msh[0];
double &s = msh[1];
double &h = msh[2];
M = sqrt(L*L + a*a + b*b);
s = (M > 0.001) ? acos(L/M) : 0.0;
h = (s > 0.001) ? atan2(b,a) : 0.0;
}
inline void vtkColorTransferFunctionMshToLab(const double msh[3], double lab[3])
{
const double &M = msh[0];
const double &s = msh[1];
const double &h = msh[2];
double &L = lab[0];
double &a = lab[1];
double &b = lab[2];
L = M*cos(s);
a = M*sin(s)*cos(h);
b = M*sin(s)*sin(h);
}
// Given two angular orientations, returns the smallest angle between the two.
inline double vtkColorTransferFunctionAngleDiff(double a1, double a2)
{
double adiff = a1 - a2;
if (adiff < 0.0) adiff = -adiff;
while (adiff >= 2.0 * vtkMath::Pi()) adiff -= (2.0 * vtkMath::Pi());
if (adiff > vtkMath::Pi()) adiff = (2.0 * vtkMath::Pi()) - adiff;
return adiff;
}
// For the case when interpolating from a saturated color to an unsaturated
// color, find a hue for the unsaturated color that makes sense.
inline double vtkColorTransferFunctionAdjustHue(const double msh[3],
double unsatM)
{
if (msh[0] >= unsatM - 0.1)
{
// The best we can do is hold hue constant.
return msh[2];
}
else
{
// This equation is designed to make the perceptual change of the
// interpolation to be close to constant.
double hueSpin = ( msh[1]*sqrt(unsatM*unsatM - msh[0]*msh[0])
/ (msh[0]*sin(msh[1])) );
// Spin hue away from 0 except in purple hues.
if (msh[2] > -0.3*vtkMath::Pi())
{
return msh[2] + hueSpin;
}
else
{
return msh[2] - hueSpin;
}
}
}
// Interpolate a diverging color map.
inline void vtkColorTransferFunctionInterpolateDiverging(double s,
const double rgb1[3],
const double rgb2[3],
double result[3])
{
double lab1[3], lab2[3];
vtkMath::RGBToLab(rgb1, lab1);
vtkMath::RGBToLab(rgb2, lab2);
double msh1[3], msh2[3];
vtkColorTransferFunctionLabToMsh(lab1, msh1);
vtkColorTransferFunctionLabToMsh(lab2, msh2);
// If the endpoints are distinct saturated colors, then place white in between
// them.
if ( (msh1[1] > 0.05) && (msh2[1] > 0.05)
&& (vtkColorTransferFunctionAngleDiff(msh1[2], msh2[2]) > 0.33*vtkMath::Pi()) )
{
// Insert the white midpoint by setting one end to white and adjusting the
// scalar value.
double Mmid = MY_MAX(msh1[0], msh2[0]);
Mmid = MY_MAX(88.0, Mmid);
if (s < 0.5)
{
msh2[0] = Mmid; msh2[1] = 0.0; msh2[2] = 0.0;
s = 2.0*s;
}
else
{
msh1[0] = Mmid; msh1[1] = 0.0; msh1[2] = 0.0;
s = 2.0*s - 1.0;
}
}
// If one color has no saturation, then its hue value is invalid. In this
// case, we want to set it to something logical so that the interpolation of
// hue makes sense.
if ((msh1[1] < 0.05) && (msh2[1] > 0.05))
{
msh1[2] = vtkColorTransferFunctionAdjustHue(msh2, msh1[0]);
}
else if ((msh2[1] < 0.05) && (msh1[1] > 0.05))
{
msh2[2] = vtkColorTransferFunctionAdjustHue(msh1, msh2[0]);
}
double mshTmp[3];
mshTmp[0] = (1-s)*msh1[0] + s*msh2[0];
mshTmp[1] = (1-s)*msh1[1] + s*msh2[1];
mshTmp[2] = (1-s)*msh1[2] + s*msh2[2];
// Now convert back to RGB
double labTmp[3];
vtkColorTransferFunctionMshToLab(mshTmp, labTmp);
vtkMath::LabToRGB(labTmp, result);
}
//----------------------------------------------------------------------------
// Construct a new vtkColorTransferFunction with default values
vtkColorTransferFunction::vtkColorTransferFunction()
{
this->UnsignedCharRGBAValue[0] = 0;
this->UnsignedCharRGBAValue[1] = 0;
this->UnsignedCharRGBAValue[2] = 0;
this->UnsignedCharRGBAValue[3] = 0;
this->Range[0] = 0;
this->Range[1] = 0;
this->Clamping = 1;
this->ColorSpace = VTK_CTF_RGB;
this->HSVWrap = 1; //By default HSV will be wrap
this->Scale = VTK_CTF_LINEAR;
this->NanColor[0] = 0.5;
this->NanColor[1] = 0.0;
this->NanColor[2] = 0.0;
this->BelowRangeColor[0] = 0.0;
this->BelowRangeColor[1] = 0.0;
this->BelowRangeColor[2] = 0.0;
this->UseBelowRangeColor = 0;
this->AboveRangeColor[0] = 1.0;
this->AboveRangeColor[1] = 1.0;
this->AboveRangeColor[2] = 1.0;
this->UseAboveRangeColor = 0;
this->Function = NULL;
this->Table = NULL;
this->TableSize = 0;
this->AllowDuplicateScalars = 0;
this->Internal = new vtkColorTransferFunctionInternals;
}
//----------------------------------------------------------------------------
// Destruct a vtkColorTransferFunction
vtkColorTransferFunction::~vtkColorTransferFunction()
{
delete [] this->Table;
delete [] this->Function;
this->Function = NULL;
for(unsigned int i=0;i<this->Internal->Nodes.size();i++)
{
delete this->Internal->Nodes[i];
}
this->Internal->Nodes.clear();
delete this->Internal;
}
// Return the number of points which specify this function
int vtkColorTransferFunction::GetSize()
{
return static_cast<int>(this->Internal->Nodes.size());
}
// Since we no longer store the data in an array, we must
// copy out of the vector into an array. No modified check -
// could be added if performance is a problem
double *vtkColorTransferFunction::GetDataPointer()
{
int size = static_cast<int>(this->Internal->Nodes.size());
delete [] this->Function;
this->Function = NULL;
if ( size > 0 )
{
this->Function = new double[size*4];
for ( int i = 0; i < size; i++ )
{
this->Function[4*i ] = this->Internal->Nodes[i]->X;
this->Function[4*i+1] = this->Internal->Nodes[i]->R;
this->Function[4*i+2] = this->Internal->Nodes[i]->G;
this->Function[4*i+3] = this->Internal->Nodes[i]->B;
}
}
return this->Function;
}
//----------------------------------------------------------------------------
// Add a point defined in RGB
int vtkColorTransferFunction::AddRGBPoint( double x, double r,
double g, double b )
{
return this->AddRGBPoint( x, r, g, b, 0.5, 0.0 );
}
//----------------------------------------------------------------------------
// Add a point defined in RGB
int vtkColorTransferFunction::AddRGBPoint( double x, double r,
double g, double b,
double midpoint,
double sharpness )
{
// Error check
if ( midpoint < 0.0 || midpoint > 1.0 )
{
vtkErrorMacro("Midpoint outside range [0.0, 1.0]");
return -1;
}
if ( sharpness < 0.0 || sharpness > 1.0 )
{
vtkErrorMacro("Sharpness outside range [0.0, 1.0]");
return -1;
}
// remove any node already at this X location
if (!this->AllowDuplicateScalars)
{
this->RemovePoint( x );
}
// Create the new node
vtkCTFNode *node = new vtkCTFNode;
node->X = x;
node->R = r;
node->G = g;
node->B = b;
node->Midpoint = midpoint;
node->Sharpness = sharpness;
// Add it, then sort to get everything in order
this->Internal->Nodes.push_back(node);
this->SortAndUpdateRange();
// We need to find the index of the node we just added in order
// to return this value
unsigned int i;
for ( i = 0; i < this->Internal->Nodes.size(); i++ )
{
if ( this->Internal->Nodes[i]->X == x )
{
break;
}
}
int retVal;
// If we didn't find it, something went horribly wrong so
// return -1
if ( i < this->Internal->Nodes.size() )
{
retVal = i;
}
else
{
retVal = -1;
}
return retVal;
}
//----------------------------------------------------------------------------
// Add a point defined in HSV
int vtkColorTransferFunction::AddHSVPoint( double x, double h,
double s, double v )
{
double r, b, g;
vtkMath::HSVToRGB(h, s, v, &r, &g, &b);
return this->AddRGBPoint( x, r, g, b );
}
//----------------------------------------------------------------------------
// Add a point defined in HSV
int vtkColorTransferFunction::AddHSVPoint( double x, double h,
double s, double v,
double midpoint,
double sharpness )
{
double r, b, g;
vtkMath::HSVToRGB(h, s, v, &r, &g, &b);
return this->AddRGBPoint( x, r, g, b, midpoint, sharpness );
}
//----------------------------------------------------------------------------
// Sort the vector in increasing order, then fill in
// the Range
void vtkColorTransferFunction::SortAndUpdateRange()
{
std::sort( this->Internal->Nodes.begin(),
this->Internal->Nodes.end(),
this->Internal->CompareNodes );
bool modifiedInvoked = this->UpdateRange();
// If range is updated, Modified() has been called, don't call it again.
if (!modifiedInvoked)
{
this->Modified();
}
}
//----------------------------------------------------------------------------
bool vtkColorTransferFunction::UpdateRange()
{
double oldRange[2];
oldRange[0] = this->Range[0];
oldRange[1] = this->Range[1];
int size = static_cast<int>(this->Internal->Nodes.size());
if ( size )
{
this->Range[0] = this->Internal->Nodes[0]->X;
this->Range[1] = this->Internal->Nodes[size-1]->X;
}
else
{
this->Range[0] = 0;
this->Range[1] = 0;
}
// If the range is the same, then no need to call Modified()
if (oldRange[0] == this->Range[0] && oldRange[1] == this->Range[1])
{
return false;
}
this->Modified();
return true;
}
//----------------------------------------------------------------------------
// Remove a point
int vtkColorTransferFunction::RemovePoint( double x )
{
// First find the node since we need to know its
// index as our return value
unsigned int i;
for ( i = 0; i < this->Internal->Nodes.size(); i++ )
{
if ( this->Internal->Nodes[i]->X == x )
{
break;
}
}
int retVal;
// If the node doesn't exist, we return -1
if ( i < this->Internal->Nodes.size() )
{
retVal = i;
}
else
{
return -1;
}
// Now use STL to find it, so that we can remove it
this->Internal->FindNodeEqual.X = x;
std::vector<vtkCTFNode*>::iterator iter =
std::find_if(this->Internal->Nodes.begin(),
this->Internal->Nodes.end(),
this->Internal->FindNodeEqual );
// Actually delete it
if ( iter != this->Internal->Nodes.end() )
{
delete *iter;
this->Internal->Nodes.erase(iter);
// If the first or last point has been removed, then we update the range
// No need to sort here as the order of points hasn't changed.
bool modifiedInvoked = false;
if (i == 0 || i == this->Internal->Nodes.size())
{
modifiedInvoked = this->UpdateRange();
}
if (!modifiedInvoked)
{
this->Modified();
}
}
else
{
// This should never happen - we already returned if the node
// didn't exist...
return -1;
}
return retVal;
}
//----------------------------------------------------------------------------
void vtkColorTransferFunction::MovePoint(double oldX, double newX)
{
if (oldX == newX)
{
// Nothing to do.
return;
}
this->RemovePoint(newX);
for (unsigned int i = 0; i < this->Internal->Nodes.size(); i++ )
{
if ( this->Internal->Nodes[i]->X == oldX )
{
this->Internal->Nodes[i]->X = newX;
this->SortAndUpdateRange();
break;
}
}
}
//----------------------------------------------------------------------------
// Remove all points
void vtkColorTransferFunction::RemoveAllPoints()
{
for(unsigned int i=0;i<this->Internal->Nodes.size();i++)
{
delete this->Internal->Nodes[i];
}
this->Internal->Nodes.clear();
this->SortAndUpdateRange();
}
//----------------------------------------------------------------------------
// Add a line defined in RGB
void vtkColorTransferFunction::AddRGBSegment( double x1, double r1,
double g1, double b1,
double x2, double r2,
double g2, double b2 )
{
int done;
// First, find all points in this range and remove them
done = 0;
while ( !done )
{
done = 1;
this->Internal->FindNodeInRange.X1 = x1;
this->Internal->FindNodeInRange.X2 = x2;
std::vector<vtkCTFNode*>::iterator iter =
std::find_if(this->Internal->Nodes.begin(),
this->Internal->Nodes.end(),
this->Internal->FindNodeInRange );
if ( iter != this->Internal->Nodes.end() )
{
delete *iter;
this->Internal->Nodes.erase(iter);
this->Modified();
done = 0;
}
}
// Now add the points
this->AddRGBPoint( x1, r1, g1, b1, 0.5, 0.0 );
this->AddRGBPoint( x2, r2, g2, b2, 0.5, 0.0 );
}
//----------------------------------------------------------------------------
// Add a line defined in HSV
void vtkColorTransferFunction::AddHSVSegment( double x1, double h1,
double s1, double v1,
double x2, double h2,
double s2, double v2 )
{
double r1, r2, b1, b2, g1, g2;
vtkMath::HSVToRGB(h1, s1, v1, &r1, &g1, &b1);
vtkMath::HSVToRGB(h2, s2, v2, &r2, &g2, &b2);
this->AddRGBSegment( x1, r1, g1, b1, x2, r2, g2, b2 );
}
//----------------------------------------------------------------------------
// Returns the RGBA color evaluated at the specified location
unsigned char *vtkColorTransferFunction::MapValue( double x )
{
double rgb[3];
this->GetColor( x, rgb );
this->UnsignedCharRGBAValue[0] =
static_cast<unsigned char>(255.0*rgb[0] + 0.5);
this->UnsignedCharRGBAValue[1] =
static_cast<unsigned char>(255.0*rgb[1] + 0.5);
this->UnsignedCharRGBAValue[2] =
static_cast<unsigned char>(255.0*rgb[2] + 0.5);
this->UnsignedCharRGBAValue[3] = 255;
return this->UnsignedCharRGBAValue;
}
//----------------------------------------------------------------------------
// Returns the RGB color evaluated at the specified location
void vtkColorTransferFunction::GetColor(double x, double rgb[3])
{
if ( this->IndexedLookup )
{
int numNodes = this->GetSize();
vtkVariant xv( x );
vtkIdType idx = this->GetAnnotatedValueIndexInternal( xv );
if ( idx < 0 || numNodes == 0 )
{
this->GetNanColor( rgb );
}
else
{
double nodeVal[6];
this->GetNodeValue( idx % numNodes, nodeVal );
for ( int i = 0; i < 3; ++ i )
rgb[i] = nodeVal[i + 1];
}
return;
}
this->GetTable( x, x, 1, rgb );
}
//----------------------------------------------------------------------------
// Returns the red color evaluated at the specified location
double vtkColorTransferFunction::GetRedValue( double x )
{
double rgb[3];
this->GetColor( x, rgb );
return rgb[0];
}
//----------------------------------------------------------------------------
// Returns the green color evaluated at the specified location
double vtkColorTransferFunction::GetGreenValue( double x )
{
double rgb[3];
this->GetColor( x, rgb );
return rgb[1];
}
//----------------------------------------------------------------------------
// Returns the blue color evaluated at the specified location
double vtkColorTransferFunction::GetBlueValue( double x )
{
double rgb[3];
this->GetColor( x, rgb );
return rgb[2];
}
//----------------------------------------------------------------------------
// Returns a table of RGB colors at regular intervals along the function
void vtkColorTransferFunction::GetTable( double xStart, double xEnd,
int size, double* table )
{
int i, j;
// Special case: If either the start or end is a NaN, then all any
// interpolation done on them is also a NaN. Therefore, fill the table with
// the NaN color.
if (vtkMath::IsNan(xStart) || vtkMath::IsNan(xEnd))
{
double *tableEntry = table;
for (i = 0; i < size; i++)
{
tableEntry[0] = this->NanColor[0];
tableEntry[1] = this->NanColor[1];
tableEntry[2] = this->NanColor[2];
tableEntry += 3;
}
return;
}
int idx = 0;
int numNodes = static_cast<int>(this->Internal->Nodes.size());
// Need to keep track of the last value so that
// we can fill in table locations past this with
// this value if Clamping is On.
double lastR = 0.0;
double lastG = 0.0;
double lastB = 0.0;
if ( numNodes != 0 )
{
lastR = this->Internal->Nodes[numNodes-1]->R;
lastG = this->Internal->Nodes[numNodes-1]->G;
lastB = this->Internal->Nodes[numNodes-1]->B;
}
double *tptr = NULL;
double x = 0.0;
double x1 = 0.0;
double x2 = 0.0;
double rgb1[3] = {0.0, 0.0, 0.0};
double rgb2[3] = {0.0, 0.0, 0.0};
double midpoint = 0.0;
double sharpness = 0.0;
// If the scale is logarithmic, make sure the range is valid.
bool usingLogScale = this->Scale == VTK_CTF_LOG10;
if(usingLogScale)
{
// Note: This requires range[0] <= range[1].
usingLogScale = this->Range[0] > 0.0;
}
double logStart = 0.0;
double logEnd = 0.0;
double logX = 0.0;
if(usingLogScale)
{
logStart = log10(xStart);
logEnd = log10(xEnd);
}
// For each table entry
for ( i = 0; i < size; i++ )
{
// Find our location in the table
tptr = table + 3*i;
// Find our X location. If we are taking only 1 sample, make
// it halfway between start and end (usually start and end will
// be the same in this case)
if ( size > 1 )
{
if(usingLogScale)
{
logX = logStart +
(static_cast<double>(i)/static_cast<double>(size-1))
*(logEnd-logStart);
x = pow(static_cast<double>(10.0), logX);
}
else
{
x = xStart + (static_cast<double>(i)/static_cast<double>(size-1))
*(xEnd-xStart);
}
}
else
{
if(usingLogScale)
{
logX = 0.5*(logStart+logEnd);
x = pow(static_cast<double>(10.0), logX);
}
else
{
x = 0.5*(xStart+xEnd);
}
}
// Do we need to move to the next node?
while ( idx < numNodes &&
x > this->Internal->Nodes[idx]->X )
{
idx++;
// If we are at a valid point index, fill in
// the value at this node, and the one before (the
// two that surround our current sample location)
// idx cannot be 0 since we just incremented it.
if ( idx < numNodes )
{
x1 = this->Internal->Nodes[idx-1]->X;
x2 = this->Internal->Nodes[idx ]->X;
if(usingLogScale)
{
x1 = log10(x1);
x2 = log10(x2);
}
rgb1[0] = this->Internal->Nodes[idx-1]->R;
rgb2[0] = this->Internal->Nodes[idx ]->R;
rgb1[1] = this->Internal->Nodes[idx-1]->G;
rgb2[1] = this->Internal->Nodes[idx ]->G;
rgb1[2] = this->Internal->Nodes[idx-1]->B;
rgb2[2] = this->Internal->Nodes[idx ]->B;
// We only need the previous midpoint and sharpness
// since these control this region
midpoint = this->Internal->Nodes[idx-1]->Midpoint;
sharpness = this->Internal->Nodes[idx-1]->Sharpness;
// Move midpoint away from extreme ends of range to avoid
// degenerate math
if ( midpoint < 0.00001 )
{
midpoint = 0.00001;
}
if ( midpoint > 0.99999 )
{
midpoint = 0.99999;
}
}
}
// Are we at or past the end? If so, just use the last value
if ( x > this->Range[1])
{
tptr[0] = 0.0;
tptr[1] = 0.0;
tptr[2] = 0.0;
if (this->Clamping)
{
if (this->GetUseAboveRangeColor())
{
this->GetAboveRangeColor(tptr);
}
else
{
tptr[0] = lastR;
tptr[1] = lastG;
tptr[2] = lastB;
}
}
}
// Are we before the first node? If so, duplicate this node's values.
// We have to deal with -inf here
else if (x < this->Range[0] || (vtkMath::IsInf(x) && x < 0))
{
tptr[0] = 0.0;
tptr[1] = 0.0;
tptr[2] = 0.0;
if (this->Clamping)
{
if (this->GetUseBelowRangeColor())
{
this->GetBelowRangeColor(tptr);
}
else
{
if (numNodes > 0)
{
tptr[0] = this->Internal->Nodes[0]->R;
tptr[1] = this->Internal->Nodes[0]->G;
tptr[2] = this->Internal->Nodes[0]->B;
}
}
}
}
else if (idx == 0 && std::fabs(x - xStart) < 1e-6)
{
if (numNodes > 0)
{
tptr[0] = this->Internal->Nodes[0]->R;
tptr[1] = this->Internal->Nodes[0]->G;
tptr[2] = this->Internal->Nodes[0]->B;
}
else
{
tptr[0] = 0.0;
tptr[1] = 0.0;
tptr[2] = 0.0;
}
}
// Otherwise, we are between two nodes - interpolate
else
{
// Our first attempt at a normalized location [0,1] -
// we will be modifying this based on midpoint and
// sharpness to get the curve shape we want and to have
// it pass through (y1+y2)/2 at the midpoint.
double s = 0.0;
if(usingLogScale)
{
s = (logX - x1) / (x2 - x1);
}
else
{
s = (x - x1) / (x2 - x1);
}
// Readjust based on the midpoint - linear adjustment
if ( s < midpoint )
{
s = 0.5 * s / midpoint;
}
else
{
s = 0.5 + 0.5*(s-midpoint)/(1.0-midpoint);
}
// override for sharpness > 0.99
// In this case we just want piecewise constant
if ( sharpness > 0.99 )
{
// Use the first value since we are below the midpoint
if ( s < 0.5 )
{
tptr[0] = rgb1[0];
tptr[1] = rgb1[1];
tptr[2] = rgb1[2];
continue;
}
// Use the second value at or above the midpoint
else
{
tptr[0] = rgb2[0];
tptr[1] = rgb2[1];
tptr[2] = rgb2[2];
continue;
}
}
// Override for sharpness < 0.01
// In this case we want piecewise linear
if ( sharpness < 0.01 )
{
// Simple linear interpolation
if ( this->ColorSpace == VTK_CTF_RGB )
{
tptr[0] = (1-s)*rgb1[0] + s*rgb2[0];
tptr[1] = (1-s)*rgb1[1] + s*rgb2[1];
tptr[2] = (1-s)*rgb1[2] + s*rgb2[2];
}
else if ( this->ColorSpace == VTK_CTF_HSV )
{
double hsv1[3], hsv2[3];
vtkMath::RGBToHSV(rgb1, hsv1);
vtkMath::RGBToHSV(rgb2, hsv2);
if ( this->HSVWrap &&
(hsv1[0] - hsv2[0] > 0.5 ||
hsv2[0] - hsv1[0] > 0.5) )
{
if ( hsv1[0] > hsv2[0] )
{
hsv1[0] -= 1.0;
}
else
{
hsv2[0] -= 1.0;
}
}
double hsvTmp[3];
hsvTmp[0] = (1-s)*hsv1[0] + s*hsv2[0];
if ( hsvTmp[0] < 0.0 )
{
hsvTmp[0] += 1.0;
}
hsvTmp[1] = (1-s)*hsv1[1] + s*hsv2[1];
hsvTmp[2] = (1-s)*hsv1[2] + s*hsv2[2];
// Now convert this back to RGB
vtkMath::HSVToRGB( hsvTmp, tptr );
}
else if (this->ColorSpace == VTK_CTF_LAB)
{
double lab1[3], lab2[3];
vtkMath::RGBToLab(rgb1, lab1);
vtkMath::RGBToLab(rgb2, lab2);
double labTmp[3];
labTmp[0] = (1-s)*lab1[0] + s*lab2[0];
labTmp[1] = (1-s)*lab1[1] + s*lab2[1];
labTmp[2] = (1-s)*lab1[2] + s*lab2[2];
// Now convert back to RGB
vtkMath::LabToRGB(labTmp, tptr);
}
else if (this->ColorSpace == VTK_CTF_DIVERGING)
{
vtkColorTransferFunctionInterpolateDiverging(s, rgb1, rgb2, tptr);
}
else
{
vtkErrorMacro("ColorSpace set to invalid value.");