Skip to content

Commit f49d6d3

Browse files
Zaid-Safadiswederik
authored andcommitted
feat(invertVolume): add ability to invert an RGB Transfer Function
1 parent 899e222 commit f49d6d3

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

examples/VTKBasicExample.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import React from 'react';
22
import { Component } from 'react';
3-
import { View2D, vtkInteractorStyleMPRWindowLevel } from '@vtk-viewport';
3+
import {
4+
View2D,
5+
vtkInteractorStyleMPRWindowLevel,
6+
invertVolume,
7+
} from '@vtk-viewport';
48
import vtkHttpDataSetReader from 'vtk.js/Sources/IO/Core/HttpDataSetReader';
59
import vtkVolume from 'vtk.js/Sources/Rendering/Core/Volume';
610
import vtkVolumeMapper from 'vtk.js/Sources/Rendering/Core/VolumeMapper';
@@ -54,6 +58,12 @@ class VTKBasicExample extends Component {
5458
this.updateAllViewports();
5559
};
5660

61+
invert = () => {
62+
const volume = this.state.volumes[0];
63+
64+
invertVolume(volume, this.updateAllViewports);
65+
};
66+
5767
updateAllViewports = () => {
5868
Object.keys(this.components).forEach(viewportIndex => {
5969
const component = this.components[viewportIndex];
@@ -143,8 +153,8 @@ class VTKBasicExample extends Component {
143153
to obtain access to the VTK render window for one or more component.
144154
It also shows how to provide an array of vtkVolumes to the component
145155
for rendering. When we change the RGB Transfer Function for the
146-
volume using the Window/Level buttons, we can see that this is
147-
applied inside both components.
156+
volume using the Window/Level and Invert buttons, we can see that
157+
this is applied inside both components.
148158
</p>
149159
</div>
150160
<div className="col-xs-12">
@@ -162,6 +172,9 @@ class VTKBasicExample extends Component {
162172
>
163173
Head
164174
</button>
175+
<button className="btn btn-primary" onClick={() => this.invert()}>
176+
Invert
177+
</button>
165178
</div>
166179
</div>
167180
<div className="col-xs-12 col-sm-6">

src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import vtkSVGCrosshairsWidget from './VTKViewport/vtkSVGCrosshairsWidget.js';
88
import ViewportOverlay from './ViewportOverlay/ViewportOverlay.js';
99
import getImageData from './lib/getImageData.js';
1010
import loadImageData from './lib/loadImageData.js';
11+
import invertVolume from './lib/invertVolume.js';
1112

1213
export {
1314
View2D,
@@ -20,6 +21,7 @@ export {
2021
vtkInteractorStyleMPRSlice,
2122
vtkSVGWidgetManager,
2223
vtkSVGCrosshairsWidget,
24+
invertVolume,
2325
};
2426

2527
export default View2D;

src/lib/invertVolume.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {vtkVolume} volume A vtkVolume object to invert
3+
* @param {()=>void|{render:()=>void}} [rendering] - A function to render the volume after
4+
* being inverted or a RenderWindow object. Can be null.
5+
* @returns void
6+
*/
7+
export default (volume, rendering = null) => {
8+
if (!volume) {
9+
return;
10+
}
11+
12+
const rgbTransferFunction = volume.getProperty().getRGBTransferFunction(0);
13+
const size = rgbTransferFunction.getSize();
14+
15+
for (let index = 0; index < size; index++) {
16+
const nodeValue1 = [];
17+
18+
rgbTransferFunction.getNodeValue(index, nodeValue1);
19+
20+
nodeValue1[1] = 1 - nodeValue1[1];
21+
nodeValue1[2] = 1 - nodeValue1[2];
22+
nodeValue1[3] = 1 - nodeValue1[3];
23+
24+
rgbTransferFunction.setNodeValue(index, nodeValue1);
25+
}
26+
27+
if (rendering instanceof Function) {
28+
rendering();
29+
} else if (rendering && rendering.render) {
30+
rendering.render();
31+
}
32+
};

0 commit comments

Comments
 (0)