Skip to content

Commit 897a3e6

Browse files
ARTUSI Xavierjourdain
authored andcommitted
feat(SynchronizableRenderWindow): More generic updater to fetch arrays
This PR add updater for volumes and image slices and extend alias with new classes while adding support for an optional state.arrays field that can be used to bind vtkDataArray to any object.
1 parent a55ebad commit 897a3e6

1 file changed

Lines changed: 122 additions & 16 deletions

File tree

Sources/Rendering/Misc/SynchronizableRenderWindow/vtkObjectManager.js

Lines changed: 122 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import vtkActor from 'vtk.js/Sources/Rendering/Core/Actor';
22
import vtkCamera from 'vtk.js/Sources/Rendering/Core/Camera';
33
import vtkColorTransferFunction from 'vtk.js/Sources/Rendering/Core/ColorTransferFunction';
44
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
5+
import vtkPoints from 'vtk.js/Sources/Common/Core/Points';
6+
import vtkCellArray from 'vtk.js/Sources/Common/Core/CellArray';
57
import vtkGlyph3DMapper from 'vtk.js/Sources/Rendering/Core/Glyph3DMapper';
68
import vtkLight from 'vtk.js/Sources/Rendering/Core/Light';
79
import vtkLookupTable from 'vtk.js/Sources/Common/Core/LookupTable';
@@ -12,6 +14,13 @@ import vtkProperty from 'vtk.js/Sources/Rendering/Core/Property';
1214
import vtkRenderer from 'vtk.js/Sources/Rendering/Core/Renderer';
1315
import vtkRenderWindow from 'vtk.js/Sources/Rendering/Core/RenderWindow';
1416
import vtkTexture from 'vtk.js/Sources/Rendering/Core/Texture';
17+
import vtkVolume from 'vtk.js/Sources/Rendering/Core/Volume';
18+
import vtkVolumeMapper from 'vtk.js/Sources/Rendering/Core/VolumeMapper';
19+
import vtkVolumeProperty from 'vtk.js/Sources/Rendering/Core/VolumeProperty';
20+
import vtkImageSlice from 'vtk.js/Sources/Rendering/Core/ImageSlice';
21+
import vtkImageMapper from 'vtk.js/Sources/Rendering/Core/ImageMapper';
22+
import vtkImageProperty from 'vtk.js/Sources/Rendering/Core/ImageProperty';
23+
import vtkPiecewiseFunction from 'vtk.js/Sources/Common/DataModel/PiecewiseFunction';
1524

1625
// ----------------------------------------------------------------------------
1726
// Some internal, module-level variables and methods
@@ -23,6 +32,11 @@ const WRAP_ID = (id) => `instance:$\{${id}}`;
2332
const ONE_TIME_INSTANCE_TRACKERS = {};
2433
const SKIPPED_INSTANCE_IDS = [];
2534
const EXCLUDE_INSTANCE_MAP = {};
35+
const DATA_ARRAY_MAPPER = {
36+
vtkPoints,
37+
vtkCellArray,
38+
vtkDataArray,
39+
};
2640

2741
function extractCallArgs(synchronizerContext, argList) {
2842
return argList.map((arg) => {
@@ -133,6 +147,22 @@ function notSkippedInstance(call) {
133147
return keep;
134148
}
135149

150+
function validateDataset(
151+
instance,
152+
context,
153+
arraysToBind,
154+
arraysToBingTargetSize
155+
) {
156+
if (arraysToBind.length === arraysToBingTargetSize) {
157+
while (arraysToBind.length) {
158+
const [fn, args] = arraysToBind.shift();
159+
fn(...args);
160+
}
161+
instance.modified();
162+
context.end();
163+
}
164+
}
165+
136166
// ----------------------------------------------------------------------------
137167
// Updater functions
138168
// ----------------------------------------------------------------------------
@@ -172,7 +202,37 @@ function genericUpdater(instance, state, context) {
172202
});
173203
}
174204

175-
context.end();
205+
// if some arrays need to be be fetch
206+
if (state.arrays) {
207+
const arraysToBind = [];
208+
const nbArrayToDownload = state.arrays.length;
209+
210+
state.arrays.forEach((arrayMetadata) => {
211+
context
212+
.getArray(arrayMetadata.hash, arrayMetadata.dataType, context)
213+
.then(
214+
(values) => {
215+
const vtkClass = arrayMetadata.vtkClass
216+
? arrayMetadata.vtkClass
217+
: 'vtkDataArray';
218+
const array = DATA_ARRAY_MAPPER[vtkClass].newInstance(
219+
Object.assign({ values }, arrayMetadata)
220+
);
221+
const regMethod = arrayMetadata.registration
222+
? arrayMetadata.registration
223+
: 'addArray';
224+
const location = arrayMetadata.location
225+
? instance.get(arrayMetadata.location)[arrayMetadata.location]
226+
: instance;
227+
arraysToBind.push([location[regMethod], [array]]);
228+
validateDataset(instance, context, arraysToBind, nbArrayToDownload);
229+
},
230+
(error) => {
231+
console.log('error fetching array', error);
232+
}
233+
);
234+
});
235+
} else context.end();
176236
}
177237

178238
// ----------------------------------------------------------------------------
@@ -301,6 +361,20 @@ function colorTransferFunctionUpdater(instance, state, context) {
301361
context.end();
302362
}
303363

364+
function piecewiseFunctionUpdater(instance, state, context) {
365+
context.start();
366+
const nodes = state.properties.nodes.map(([x, y, midpoint, sharpness]) => ({
367+
x,
368+
y,
369+
midpoint,
370+
sharpness,
371+
}));
372+
instance.set(Object.assign({}, state.properties, { nodes }), true);
373+
instance.sortAndUpdateRange();
374+
instance.modified();
375+
context.end();
376+
}
377+
304378
// ----------------------------------------------------------------------------
305379

306380
function createDataSetUpdate(piecesToFetch = []) {
@@ -323,18 +397,6 @@ function createDataSetUpdate(piecesToFetch = []) {
323397
[instance.getCellData().removeAllArrays, []],
324398
];
325399

326-
function validateDataset() {
327-
if (arraysToBind.length - 2 === nbArrayToDownload) {
328-
while (arraysToBind.length) {
329-
const [fn, args] = arraysToBind.shift();
330-
fn(...args);
331-
}
332-
333-
instance.modified();
334-
context.end();
335-
}
336-
}
337-
338400
// Fetch geometry
339401
piecesToFetch.forEach((arrayName) => {
340402
if (props[arrayName]) {
@@ -348,7 +410,12 @@ function createDataSetUpdate(piecesToFetch = []) {
348410
instance.get(arrayName)[arrayName].setData,
349411
[values, arrayMetadata.numberOfComponents],
350412
]);
351-
validateDataset();
413+
validateDataset(
414+
instance,
415+
context,
416+
arraysToBind,
417+
nbArrayToDownload + 2
418+
);
352419
},
353420
(error) => {
354421
console.log('error geometry fetching array', error);
@@ -375,7 +442,12 @@ function createDataSetUpdate(piecesToFetch = []) {
375442
],
376443
[array],
377444
]);
378-
validateDataset();
445+
validateDataset(
446+
instance,
447+
context,
448+
arraysToBind,
449+
nbArrayToDownload + 2
450+
);
379451
},
380452
(error) => {
381453
console.log('error field fetching array', error);
@@ -410,14 +482,20 @@ function setTypeMapping(type, buildFn = null, updateFn = genericUpdater) {
410482
// ----------------------------------------------------------------------------
411483

412484
const DEFAULT_ALIASES = {
413-
vtkMapper: ['vtkOpenGLPolyDataMapper', 'vtkCompositePolyDataMapper2'],
485+
vtkMapper: [
486+
'vtkOpenGLPolyDataMapper',
487+
'vtkCompositePolyDataMapper2',
488+
'vtkDataSetMapper',
489+
],
414490
vtkProperty: ['vtkOpenGLProperty'],
415491
vtkRenderer: ['vtkOpenGLRenderer'],
416492
vtkCamera: ['vtkOpenGLCamera'],
417493
vtkColorTransferFunction: ['vtkPVDiscretizableColorTransferFunction'],
418494
vtkActor: ['vtkOpenGLActor', 'vtkPVLODActor'],
419495
vtkLight: ['vtkOpenGLLight', 'vtkPVLight'],
420496
vtkTexture: ['vtkOpenGLTexture'],
497+
vtkImageMapper: ['vtkOpenGLImageSliceMapper'],
498+
vtkVolumeMapper: ['vtkFixedPointVolumeRayCastMapper'],
421499
};
422500

423501
// ----------------------------------------------------------------------------
@@ -475,6 +553,34 @@ const DEFAULT_MAPPING = {
475553
build: vtkTexture.newInstance,
476554
update: genericUpdater,
477555
},
556+
vtkVolume: {
557+
build: vtkVolume.newInstance,
558+
update: genericUpdater,
559+
},
560+
vtkVolumeMapper: {
561+
build: vtkVolumeMapper.newInstance,
562+
update: genericUpdater,
563+
},
564+
vtkVolumeProperty: {
565+
build: vtkVolumeProperty.newInstance,
566+
update: genericUpdater,
567+
},
568+
vtkImageSlice: {
569+
build: vtkImageSlice.newInstance,
570+
update: genericUpdater,
571+
},
572+
vtkImageMapper: {
573+
build: vtkImageMapper.newInstance,
574+
update: genericUpdater,
575+
},
576+
vtkImageProperty: {
577+
build: vtkImageProperty.newInstance,
578+
update: genericUpdater,
579+
},
580+
vtkPiecewiseFunction: {
581+
build: vtkPiecewiseFunction.newInstance,
582+
update: piecewiseFunctionUpdater,
583+
},
478584
};
479585

480586
// ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)