@@ -3,7 +3,7 @@ import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
33import vtkScalarsToColors from 'vtk.js/Sources/Common/Core/ScalarsToColors' ;
44import Constants from 'vtk.js/Sources/Rendering/Core/ColorTransferFunction/Constants' ;
55
6- const { ColorSpace, Scale } = Constants ;
6+ const { ColorSpace, Scale, ScalarMappingMode } = Constants ;
77const { ScalarMappingTarget } = vtkScalarsToColors ;
88const { vtkDebugMacro, vtkErrorMacro, vtkWarningMacro } = macro ;
99
@@ -493,6 +493,8 @@ function vtkColorTransferFunction(publicAPI, model) {
493493 ] ;
494494 }
495495
496+ const useSigmoid =
497+ publicAPI . getScalarMappingMode ( ) == ScalarMappingMode . SIGMOID ;
496498 // For each table entry
497499 for ( let i = 0 ; i < size ; i ++ ) {
498500 // Find our location in the table
@@ -501,11 +503,23 @@ function vtkColorTransferFunction(publicAPI, model) {
501503 // Find our X location. If we are taking only 1 sample, make
502504 // it halfway between start and end (usually start and end will
503505 // be the same in this case)
506+ let c = 0 ,
507+ w = 0 ;
504508 if ( size > 1 ) {
509+ c = ( xEnd + xStart ) / 2.0 ;
510+ w = xEnd - xStart ;
505511 x = xStart + ( i / ( size - 1.0 ) ) * ( xEnd - xStart ) ;
506512 } else {
513+ c = ( scaledMappingRange [ 0 ] + scaledMappingRange [ 1 ] ) / 2.0 ;
514+ w = scaledMappingRange [ 1 ] - scaledMappingRange [ 0 ] ;
507515 x = 0.5 * ( xStart + xEnd ) ;
508516 }
517+ if ( useSigmoid ) {
518+ // Apply the sigmoid function to x,
519+ const y = 1 / ( 1 + Math . exp ( ( model . sigmoidGrowthRate * ( x - c ) ) / w ) ) ;
520+ // and re-interpret it as a linear parameter.
521+ x = y * w + ( c - w / 2 ) ;
522+ }
509523
510524 // Linearly map x from mappingRange to [0, numberOfValues-1],
511525 // discretize (round down to the closest integer),
@@ -1273,6 +1287,12 @@ function vtkColorTransferFunction(publicAPI, model) {
12731287
12741288 return modifiedInvoked || callModified ;
12751289 } ;
1290+
1291+ publicAPI . setScalarMappingModeToLinear = ( ) =>
1292+ publicAPI . setScalarMappingMode ( ScalarMappingMode . LINEAR ) ;
1293+
1294+ publicAPI . setScalarMappingModeToSigmoid = ( ) =>
1295+ publicAPI . setScalarMappingMode ( ScalarMappingMode . SIGMOID ) ;
12761296}
12771297
12781298// ----------------------------------------------------------------------------
@@ -1284,6 +1304,7 @@ const DEFAULT_VALUES = {
12841304 colorSpace : ColorSpace . RGB ,
12851305 hSVWrap : true ,
12861306 scale : Scale . LINEAR ,
1307+ scalarMappingMode : ScalarMappingMode . LINEAR ,
12871308
12881309 nanColor : null ,
12891310 belowRangeColor : null ,
@@ -1301,6 +1322,7 @@ const DEFAULT_VALUES = {
13011322
13021323 discretize : false ,
13031324 numberOfValues : 256 ,
1325+ sigmoidGrowthRate : - 4 ,
13041326} ;
13051327
13061328// ----------------------------------------------------------------------------
@@ -1331,8 +1353,10 @@ export function extend(publicAPI, model, initialValues = {}) {
13311353 'useBelowRangeColor' ,
13321354 'discretize' ,
13331355 'numberOfValues' ,
1356+ 'sigmoidGrowthRate' ,
13341357 { type : 'enum' , name : 'colorSpace' , enum : ColorSpace } ,
13351358 { type : 'enum' , name : 'scale' , enum : Scale } ,
1359+ { type : 'enum' , name : 'scalarMappingMode' , enum : ScalarMappingMode } ,
13361360 ] ) ;
13371361
13381362 macro . setArray (
0 commit comments