Skip to content

Commit e360a23

Browse files
committed
perf(ColorTransferFunction): less JSON.stringify in applyColorMap
1 parent 8fc5433 commit e360a23

File tree

2 files changed

+41
-24
lines changed

2 files changed

+41
-24
lines changed

Sources/Rendering/Core/ColorTransferFunction/index.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,16 +1162,18 @@ function vtkColorTransferFunction(publicAPI, model) {
11621162
model.colorSpace = ColorSpace.RGB;
11631163
}
11641164
}
1165+
let isModified = oldColorSpace !== JSON.stringify(model.colorSpace);
11651166

1166-
const oldNanColor = JSON.stringify(model.nanColor);
1167+
const oldNanColor = isModified || JSON.stringify(model.nanColor);
11671168
if (colorMap.NanColor) {
11681169
model.nanColor = [].concat(colorMap.NanColor);
11691170
while (model.nanColor.length < 4) {
11701171
model.nanColor.push(1.0);
11711172
}
11721173
}
1174+
isModified = isModified || oldNanColor !== JSON.stringify(model.nanColor);
11731175

1174-
const oldNodes = JSON.stringify(model.nodes);
1176+
const oldNodes = isModified || JSON.stringify(model.nodes);
11751177
if (colorMap.RGBPoints) {
11761178
const size = colorMap.RGBPoints.length;
11771179
model.nodes = [];
@@ -1191,16 +1193,12 @@ function vtkColorTransferFunction(publicAPI, model) {
11911193

11921194
const modifiedInvoked = publicAPI.sortAndUpdateRange();
11931195

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;
1196+
const callModified =
1197+
!modifiedInvoked &&
1198+
(isModified || oldNodes !== JSON.stringify(model.nodes));
1199+
if (callModified) publicAPI.modified();
1200+
1201+
return modifiedInvoked || callModified;
12041202
};
12051203
}
12061204

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

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@ 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';
1312

1413
import { areEquals } from 'vtk.js/Sources/Common/Core/Math';
1514

1615
import baseline from './testColorTransferFunction.png';
1716
import baseline2 from './testColorTransferFunction2.png';
1817

19-
const { ColorSpace } = Constants;
20-
2118
test('Test Color Transfer Function', (t) => {
2219
const gc = testUtils.createGarbageCollector(t);
2320
t.ok('rendering', 'vtkMapper ColorTransferFunction');
@@ -163,38 +160,60 @@ test('Test discretized color transfer function', (t) => {
163160
t.end();
164161
});
165162

166-
test('Test applyColorMap calls modfied', (t) => {
163+
test('Test applyColorMap calls modified', (t) => {
167164
const ctf = vtkColorTransferFunction.newInstance();
168165

169166
const colorMapA = {
170-
ColorSpace: ColorSpace.RGB,
167+
ColorSpace: 'RGB',
171168
RGBPoints: [0, 0, 0, 0, 1, 0, 0, 0],
172169
};
173170
const colorMapB = {
174-
ColorSpace: ColorSpace.RGB,
171+
ColorSpace: 'RGB',
175172
RGBPoints: [0, 0, 0, 0, 1, 1, 1, 1],
176173
};
177174

178175
ctf.applyColorMap(colorMapA);
179176

180-
let isModfied = false;
177+
let isModified = false;
181178
ctf.onModified(() => {
182-
isModfied = true;
179+
isModified = true;
183180
});
184181

185182
ctf.applyColorMap(colorMapA);
186-
187183
t.notOk(
188-
isModfied,
184+
isModified,
189185
`Expect applyColorMap does not call modified with same color map`
190186
);
191187

192188
ctf.applyColorMap(colorMapB);
193-
194189
t.ok(
195-
isModfied,
190+
isModified,
196191
`Expect applyColorMap calls modified with different color map`
197192
);
198193

194+
colorMapB.ColorSpace = 'LAB';
195+
isModified = false;
196+
let modifiedReturn = ctf.applyColorMap(colorMapB);
197+
t.ok(
198+
isModified && modifiedReturn,
199+
`Expect applyColorMap calls modified with different ColorSpace`
200+
);
201+
202+
colorMapB.NanColor = [0, 0, 0, 1];
203+
isModified = false;
204+
modifiedReturn = ctf.applyColorMap(colorMapB);
205+
t.ok(
206+
isModified && modifiedReturn,
207+
`Expect applyColorMap calls modified with different NanColor`
208+
);
209+
210+
colorMapB.RGBPoints = [0, 0, 0, 0, 0.5, 1, 1, 1, 1, 1, 1, 1];
211+
isModified = false;
212+
modifiedReturn = ctf.applyColorMap(colorMapB);
213+
t.ok(
214+
isModified && modifiedReturn,
215+
`Expect applyColorMap calls modified with different RGBPoints`
216+
);
217+
199218
t.end();
200219
});

0 commit comments

Comments
 (0)