@@ -430,12 +430,22 @@ function vtkColorTransferFunction(publicAPI, model) {
430
430
return rgb [ 2 ] ;
431
431
} ;
432
432
433
+ publicAPI . logScaleEnabled = ( ) => model . scale === Scale . LOG10 ;
434
+
435
+ publicAPI . usingLogScale = ( ) =>
436
+ publicAPI . logScaleEnabled ( ) && model . mappingRange [ 0 ] > 0.0 ;
437
+
433
438
//----------------------------------------------------------------------------
434
439
// Returns a table of RGB colors at regular intervals along the function
435
440
publicAPI . getTable = ( xStart_ , xEnd_ , size , table ) => {
441
+ // Note: This requires range[0] <= range[1].
442
+ const usingLogScale = publicAPI . usingLogScale ( ) ;
443
+
436
444
// To handle BigInt limitation
437
- const xStart = Number ( xStart_ ) ;
438
- const xEnd = Number ( xEnd_ ) ;
445
+ const xStart = usingLogScale
446
+ ? Math . log10 ( Number ( xStart_ ) )
447
+ : Number ( xStart_ ) ;
448
+ const xEnd = usingLogScale ? Math . log10 ( Number ( xEnd_ ) ) : Number ( xEnd_ ) ;
439
449
440
450
// Special case: If either the start or end is a NaN, then all any
441
451
// interpolation done on them is also a NaN. Therefore, fill the table with
@@ -475,18 +485,12 @@ function vtkColorTransferFunction(publicAPI, model) {
475
485
const tmpVec = [ ] ;
476
486
477
487
// If the scale is logarithmic, make sure the range is valid.
478
- let usingLogScale = model . scale === Scale . LOG10 ;
488
+ let scaledMappingRange = model . mappingRange ;
479
489
if ( usingLogScale ) {
480
- // Note: This requires range[0] <= range[1].
481
- usingLogScale = model . mappingRange [ 0 ] > 0.0 ;
482
- }
483
-
484
- let logStart = 0.0 ;
485
- let logEnd = 0.0 ;
486
- let logX = 0.0 ;
487
- if ( usingLogScale ) {
488
- logStart = Math . log10 ( xStart ) ;
489
- logEnd = Math . log10 ( xEnd ) ;
490
+ scaledMappingRange = [
491
+ Math . log10 ( model . mappingRange [ 0 ] ) ,
492
+ Math . log10 ( model . mappingRange [ 1 ] ) ,
493
+ ] ;
490
494
}
491
495
492
496
// For each table entry
@@ -498,15 +502,7 @@ function vtkColorTransferFunction(publicAPI, model) {
498
502
// it halfway between start and end (usually start and end will
499
503
// be the same in this case)
500
504
if ( size > 1 ) {
501
- if ( usingLogScale ) {
502
- logX = logStart + ( i / ( size - 1.0 ) ) * ( logEnd - logStart ) ;
503
- x = 10.0 ** logX ;
504
- } else {
505
- x = xStart + ( i / ( size - 1.0 ) ) * ( xEnd - xStart ) ;
506
- }
507
- } else if ( usingLogScale ) {
508
- logX = 0.5 * ( logStart + logEnd ) ;
509
- x = 10.0 ** logX ;
505
+ x = xStart + ( i / ( size - 1.0 ) ) * ( xEnd - xStart ) ;
510
506
} else {
511
507
x = 0.5 * ( xStart + xEnd ) ;
512
508
}
@@ -515,7 +511,7 @@ function vtkColorTransferFunction(publicAPI, model) {
515
511
// discretize (round down to the closest integer),
516
512
// then map back to mappingRange
517
513
if ( model . discretize ) {
518
- const range = model . mappingRange ;
514
+ const range = scaledMappingRange ;
519
515
if ( x >= range [ 0 ] && x <= range [ 1 ] ) {
520
516
const numberOfValues = model . numberOfValues ;
521
517
const deltaRange = range [ 1 ] - range [ 0 ] ;
@@ -543,10 +539,6 @@ function vtkColorTransferFunction(publicAPI, model) {
543
539
if ( idx < numNodes ) {
544
540
x1 = model . nodes [ idx - 1 ] . x ;
545
541
x2 = model . nodes [ idx ] . x ;
546
- if ( usingLogScale ) {
547
- x1 = Math . log10 ( x1 ) ;
548
- x2 = Math . log10 ( x2 ) ;
549
- }
550
542
551
543
rgb1 [ 0 ] = model . nodes [ idx - 1 ] . r ;
552
544
rgb2 [ 0 ] = model . nodes [ idx ] . r ;
@@ -575,7 +567,7 @@ function vtkColorTransferFunction(publicAPI, model) {
575
567
}
576
568
577
569
// Are we at or past the end? If so, just use the last value
578
- if ( x > model . mappingRange [ 1 ] ) {
570
+ if ( x > scaledMappingRange [ 1 ] ) {
579
571
table [ tidx ] = 0.0 ;
580
572
table [ tidx + 1 ] = 0.0 ;
581
573
table [ tidx + 2 ] = 0.0 ;
@@ -590,7 +582,7 @@ function vtkColorTransferFunction(publicAPI, model) {
590
582
table [ tidx + 2 ] = lastB ;
591
583
}
592
584
}
593
- } else if ( x < model . mappingRange [ 0 ] || ( vtkMath . isInf ( x ) && x < 0 ) ) {
585
+ } else if ( x < scaledMappingRange [ 0 ] || ( vtkMath . isInf ( x ) && x < 0 ) ) {
594
586
// we are before the first node? If so, duplicate this node's values.
595
587
// We have to deal with -inf here
596
588
table [ tidx ] = 0.0 ;
@@ -627,11 +619,7 @@ function vtkColorTransferFunction(publicAPI, model) {
627
619
// sharpness to get the curve shape we want and to have
628
620
// it pass through (y1+y2)/2 at the midpoint.
629
621
let s = 0.0 ;
630
- if ( usingLogScale ) {
631
- s = ( logX - x1 ) / ( x2 - x1 ) ;
632
- } else {
633
- s = ( x - x1 ) / ( x2 - x1 ) ;
634
- }
622
+ s = ( x - x1 ) / ( x2 - x1 ) ;
635
623
636
624
// Readjust based on the midpoint - linear adjustment
637
625
if ( s < midpoint ) {
@@ -1064,7 +1052,10 @@ function vtkColorTransferFunction(publicAPI, model) {
1064
1052
//----------------------------------------------------------------------------
1065
1053
publicAPI . setMappingRange = ( min , max ) => {
1066
1054
const range = [ min , max ] ;
1055
+ const scaledRange = [ min , max ] ;
1067
1056
const originalRange = publicAPI . getRange ( ) ;
1057
+ const logScaleEnabled = publicAPI . logScaleEnabled ( ) ;
1058
+
1068
1059
if ( originalRange [ 1 ] === range [ 1 ] && originalRange [ 0 ] === range [ 0 ] ) {
1069
1060
return ;
1070
1061
}
@@ -1074,8 +1065,20 @@ function vtkColorTransferFunction(publicAPI, model) {
1074
1065
return ;
1075
1066
}
1076
1067
1077
- const scale = ( range [ 1 ] - range [ 0 ] ) / ( originalRange [ 1 ] - originalRange [ 0 ] ) ;
1078
- const shift = range [ 0 ] - originalRange [ 0 ] * scale ;
1068
+ if ( logScaleEnabled ) {
1069
+ if ( range [ 0 ] <= 0.0 ) {
1070
+ console . warn (
1071
+ 'attempt to set log scale color range with non-positive minimum'
1072
+ ) ;
1073
+ } else {
1074
+ scaledRange [ 0 ] = Math . log10 ( range [ 0 ] ) ;
1075
+ scaledRange [ 1 ] = Math . log10 ( range [ 1 ] ) ;
1076
+ }
1077
+ }
1078
+
1079
+ const scale =
1080
+ ( scaledRange [ 1 ] - scaledRange [ 0 ] ) / ( originalRange [ 1 ] - originalRange [ 0 ] ) ;
1081
+ const shift = scaledRange [ 0 ] - originalRange [ 0 ] * scale ;
1079
1082
1080
1083
for ( let i = 0 ; i < model . nodes . length ; ++ i ) {
1081
1084
model . nodes [ i ] . x = model . nodes [ i ] . x * scale + shift ;
0 commit comments