Skip to content

Commit 7d4c58f

Browse files
jmannaufloryst
authored andcommitted
perf: pre-set range of ImageMapper scalars to improve performance
1 parent 472832c commit 7d4c58f

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

Sources/Rendering/OpenGL/ImageMapper/index.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,14 @@ function vtkOpenGLImageMapper(publicAPI, model) {
12371237
const spatialExt = image.getSpatialExtent();
12381238
const basicScalars = imgScalars.getData();
12391239
let scalars = null;
1240+
/**
1241+
* if available, used to store the range of the scalars array. If
1242+
* available, pre-setting the scalras range can prevent it being
1243+
* calculated again.
1244+
*
1245+
* @type{ import("../../../interfaces").vtkRange[]|undefined }
1246+
**/
1247+
let ranges = undefined;
12401248
// Get right scalars according to slicing mode
12411249
if (ijkMode === SlicingMode.I) {
12421250
scalars = new basicScalars.constructor(dims[2] * dims[1] * numComp);
@@ -1310,6 +1318,24 @@ function vtkOpenGLImageMapper(publicAPI, model) {
13101318
ptsArray[9] = spatialExt[1];
13111319
ptsArray[10] = spatialExt[3];
13121320
ptsArray[11] = sliceDepth;
1321+
if( sliceOffset === 0 ) {
1322+
// If the sliceOffset is 0, the scalars array is a view of the
1323+
// original scalars array. As a micro-optimzation, we can get the
1324+
// range from the original scalars array. and set the range to the
1325+
// scalars array. This means there is not need to re-calculate the
1326+
// ranges for the scalars array.
1327+
ranges = []
1328+
for( let i = 0; i < numComp; i++ ) {
1329+
const [min, max] = imgScalars.getRange(i);
1330+
/** @type{ import("../../../interfaces").vtkRange } */
1331+
const range = {
1332+
min,
1333+
max,
1334+
component: i,
1335+
}
1336+
ranges.push( range );
1337+
}
1338+
}
13131339
} else {
13141340
vtkErrorMacro('Reformat slicing not yet supported.');
13151341
}
@@ -1323,7 +1349,8 @@ function vtkOpenGLImageMapper(publicAPI, model) {
13231349
numComp,
13241350
imgScalars.getDataType(),
13251351
scalars,
1326-
model.renderable.getPreferSizeOverAccuracy?.()
1352+
model.renderable.getPreferSizeOverAccuracy?.(),
1353+
ranges
13271354
);
13281355
model.openGLTexture.activate();
13291356
model.openGLTexture.sendParameters();

Sources/Rendering/OpenGL/Texture/index.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import vtkOpenGLRenderWindow from '../RenderWindow';
33
import { Nullable } from '../../../types';
44
import { VtkDataTypes } from '../../../Common/Core/DataArray';
55
import { vtkViewNode } from '../../../Rendering/SceneGraph/ViewNode';
6-
import { vtkObject } from '../../../interfaces';
6+
import { vtkObject, vtkRange } from '../../../interfaces';
77

88
/**
99
* Initial values for creating a new instance of vtkOpenGLTexture.
@@ -245,6 +245,7 @@ export interface vtkOpenGLTexture extends vtkViewNode {
245245
* @param dataType The data type of the texture.
246246
* @param data The raw data for the texture.
247247
* @param preferSizeOverAccuracy Whether to prefer texture size over accuracy.
248+
* @param ranges The ranges of the data (optional).
248249
* @returns {boolean} True if the texture was successfully created, false otherwise.
249250
*/
250251
create2DFilterableFromRaw(
@@ -253,7 +254,8 @@ export interface vtkOpenGLTexture extends vtkViewNode {
253254
numComps: number,
254255
dataType: VtkDataTypes,
255256
data: any,
256-
preferSizeOverAccuracy: boolean
257+
preferSizeOverAccuracy: boolean,
258+
ranges?: vtkRange[]
257259
): boolean;
258260

259261
/**

Sources/Rendering/OpenGL/Texture/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1368,7 +1368,8 @@ function vtkOpenGLTexture(publicAPI, model) {
13681368
numberOfComponents,
13691369
dataType,
13701370
values,
1371-
preferSizeOverAccuracy = false
1371+
preferSizeOverAccuracy = false,
1372+
ranges = undefined
13721373
) =>
13731374
publicAPI.create2DFilterableFromDataArray(
13741375
width,
@@ -1377,6 +1378,7 @@ function vtkOpenGLTexture(publicAPI, model) {
13771378
numberOfComponents,
13781379
dataType,
13791380
values,
1381+
ranges,
13801382
}),
13811383
preferSizeOverAccuracy
13821384
);

0 commit comments

Comments
 (0)