Skip to content

Commit 7a330c0

Browse files
Antti-Palolafloryst
authored andcommitted
fix(mapper.js): fix color texture coordinate mapping when using logarithmic scale
Scalars array was already mapped with log scaling but texture coordinates were incorrectly mapped using the linear range and value. Log color mapping was working if scalar interpolation was off when scalars were not directly mapped to textures. Also added some missing types to ColorTransferFunction.
1 parent 05d7cf7 commit 7a330c0

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

Sources/Rendering/Core/ColorTransferFunction/index.d.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,30 @@ export interface vtkColorTransferFunction extends vtkScalarsToColors {
312312
* @param colorMap
313313
*/
314314
applyColorMap(colorMap: any): void;
315+
316+
/**
317+
* Get the color space of the color transfer function.
318+
* @returns {ColorSpace}
319+
*/
320+
getColorSpace(): ColorSpace;
321+
322+
/**
323+
* Set the color space of the color transfer function.
324+
* @param {ColorSpace} colorSpace
325+
*/
326+
setColorSpace(colorSpace: ColorSpace): void;
327+
328+
/**
329+
* Get the scale of the color transfer function.
330+
* @returns {Scale}
331+
*/
332+
getScale(): Scale;
333+
334+
/**
335+
* Set the scale of the color transfer function.
336+
* @param {Scale} scale
337+
*/
338+
setScale(scale: Scale): void;
315339
}
316340

317341
/**

Sources/Rendering/Core/Mapper/index.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ const colorTextureCoordinatesCache = new WeakMap();
136136
* @param {vtkDataArray} input The input data array used for coloring
137137
* @param {Number} component The component of the input data array that is used for coloring (-1 for magnitude of the vectors)
138138
* @param {Range} range The range of the scalars
139+
* @param {boolean} useLogScale Should the values be transformed to logarithmic scale. When true, the range must already be in logarithmic scale.
139140
* @param {Number} numberOfColorsInRange The number of colors that are used in the range
140141
* @param {vec3} dimensions The dimensions of the texture
141142
* @param {boolean} useZigzagPattern If a zigzag pattern should be used. Otherwise 1 row for colors (including min and max) and 1 row for NaN are used.
@@ -145,6 +146,7 @@ function getOrCreateColorTextureCoordinates(
145146
input,
146147
component,
147148
range,
149+
useLogScale,
148150
numberOfColorsInRange,
149151
dimensions,
150152
useZigzagPattern
@@ -219,6 +221,10 @@ function getOrCreateColorTextureCoordinates(
219221
} else {
220222
scalarValue = inputV[inputIdx + component];
221223
}
224+
if (useLogScale) {
225+
scalarValue = Math.log10(scalarValue);
226+
}
227+
222228
inputIdx += numComps;
223229

224230
// Convert to texture coordinates and update output
@@ -446,6 +452,9 @@ function vtkMapper(publicAPI, model) {
446452
const range = model.lookupTable.getRange();
447453
const useLogScale = model.lookupTable.usingLogScale();
448454
const origAlpha = model.lookupTable.getAlpha();
455+
const scaledRange = useLogScale
456+
? [Math.log10(range[0]), Math.log10(range[1])]
457+
: range;
449458

450459
// Get rid of vertex color array. Only texture or vertex coloring
451460
// can be active at one time. The existence of the array is the
@@ -518,9 +527,6 @@ function vtkMapper(publicAPI, model) {
518527
const numberOfNonNaNColors = numberOfNonSpecialColors + 2;
519528
const textureCoordinates = [0, 0, 0];
520529

521-
const scaledRange = useLogScale
522-
? [Math.log10(range[0]), Math.log10(range[1])]
523-
: range;
524530
const rangeMin = scaledRange[0];
525531
const rangeDifference = scaledRange[1] - scaledRange[0];
526532
for (let i = 0; i < numberOfNonNaNColors; ++i) {
@@ -576,7 +582,8 @@ function vtkMapper(publicAPI, model) {
576582
model.colorCoordinates = getOrCreateColorTextureCoordinates(
577583
scalars,
578584
scalarComponent,
579-
range,
585+
scaledRange,
586+
useLogScale,
580587
model.numberOfColorsInRange,
581588
model.colorTextureMap.getDimensions(),
582589
cellFlag

0 commit comments

Comments
 (0)