Skip to content

Commit 8fc5433

Browse files
committed
fix(ColorTransferFunction): applyColorMap calls modified in more cases
Similar shaped color maps passed to applyColorMap did not always trigger publicAPI.modified()
1 parent b182613 commit 8fc5433

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

Sources/Rendering/Core/ColorTransferFunction/index.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,7 @@ function vtkColorTransferFunction(publicAPI, model) {
11521152

11531153
//----------------------------------------------------------------------------
11541154
publicAPI.applyColorMap = (colorMap) => {
1155+
const oldColorSpace = JSON.stringify(model.colorSpace);
11551156
if (colorMap.ColorSpace) {
11561157
model.colorSpace = ColorSpace[colorMap.ColorSpace.toUpperCase()];
11571158
if (model.colorSpace === undefined) {
@@ -1161,12 +1162,16 @@ function vtkColorTransferFunction(publicAPI, model) {
11611162
model.colorSpace = ColorSpace.RGB;
11621163
}
11631164
}
1165+
1166+
const oldNanColor = JSON.stringify(model.nanColor);
11641167
if (colorMap.NanColor) {
11651168
model.nanColor = [].concat(colorMap.NanColor);
11661169
while (model.nanColor.length < 4) {
11671170
model.nanColor.push(1.0);
11681171
}
11691172
}
1173+
1174+
const oldNodes = JSON.stringify(model.nodes);
11701175
if (colorMap.RGBPoints) {
11711176
const size = colorMap.RGBPoints.length;
11721177
model.nodes = [];
@@ -1183,13 +1188,19 @@ function vtkColorTransferFunction(publicAPI, model) {
11831188
});
11841189
}
11851190
}
1186-
// FIXME: not supported ?
1187-
// if (colorMap.IndexedColors) {
1188-
// }
1189-
// if (colorMap.Annotations) {
1190-
// }
11911191

1192-
publicAPI.sortAndUpdateRange();
1192+
const modifiedInvoked = publicAPI.sortAndUpdateRange();
1193+
1194+
if (!modifiedInvoked) {
1195+
const isModfied = [
1196+
[oldColorSpace, model.colorSpace],
1197+
[oldNanColor, model.nanColor],
1198+
[oldNodes, model.nodes],
1199+
].some(([old, current]) => old !== JSON.stringify(current));
1200+
if (isModfied) publicAPI.modified();
1201+
return isModfied;
1202+
}
1203+
return modifiedInvoked;
11931204
};
11941205
}
11951206

Sources/Rendering/Core/ColorTransferFunction/test/testColorTransferFunction.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ import vtkActor from 'vtk.js/Sources/Rendering/Core/Actor';
99
import vtkMapper from 'vtk.js/Sources/Rendering/Core/Mapper';
1010
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
1111
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
12+
import Constants from 'vtk.js/Sources/Rendering/Core/ColorTransferFunction/Constants';
1213

1314
import { areEquals } from 'vtk.js/Sources/Common/Core/Math';
1415

1516
import baseline from './testColorTransferFunction.png';
1617
import baseline2 from './testColorTransferFunction2.png';
1718

19+
const { ColorSpace } = Constants;
20+
1821
test('Test Color Transfer Function', (t) => {
1922
const gc = testUtils.createGarbageCollector(t);
2023
t.ok('rendering', 'vtkMapper ColorTransferFunction');
@@ -159,3 +162,39 @@ test('Test discretized color transfer function', (t) => {
159162

160163
t.end();
161164
});
165+
166+
test('Test applyColorMap calls modfied', (t) => {
167+
const ctf = vtkColorTransferFunction.newInstance();
168+
169+
const colorMapA = {
170+
ColorSpace: ColorSpace.RGB,
171+
RGBPoints: [0, 0, 0, 0, 1, 0, 0, 0],
172+
};
173+
const colorMapB = {
174+
ColorSpace: ColorSpace.RGB,
175+
RGBPoints: [0, 0, 0, 0, 1, 1, 1, 1],
176+
};
177+
178+
ctf.applyColorMap(colorMapA);
179+
180+
let isModfied = false;
181+
ctf.onModified(() => {
182+
isModfied = true;
183+
});
184+
185+
ctf.applyColorMap(colorMapA);
186+
187+
t.notOk(
188+
isModfied,
189+
`Expect applyColorMap does not call modified with same color map`
190+
);
191+
192+
ctf.applyColorMap(colorMapB);
193+
194+
t.ok(
195+
isModfied,
196+
`Expect applyColorMap calls modified with different color map`
197+
);
198+
199+
t.end();
200+
});

0 commit comments

Comments
 (0)