-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathView3D.js
326 lines (277 loc) · 9.32 KB
/
View3D.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import vtkGenericRenderWindow from 'vtk.js/Sources/Rendering/Misc/GenericRenderWindow';
import vtkWidgetManager from 'vtk.js/Sources/Widgets/Core/WidgetManager';
import vtkPaintFilter from 'vtk.js/Sources/Filters/General/PaintFilter';
import vtkPaintWidget from 'vtk.js/Sources/Widgets/Widgets3D/PaintWidget';
import 'vtk.js/Sources/Rendering/Profiles/Volume';
import ViewportOverlay from '../ViewportOverlay/ViewportOverlay.js';
import { ViewTypes } from 'vtk.js/Sources/Widgets/Core/WidgetManager/Constants';
import { createSub } from '../lib/createSub.js';
import createLabelPipeline from './createLabelPipeline';
export default class View3D extends Component {
static propTypes = {
volumes: PropTypes.array,
actors: PropTypes.array,
painting: PropTypes.bool.isRequired,
paintFilterBackgroundImageData: PropTypes.object,
paintFilterLabelMapImageData: PropTypes.object,
onPaint: PropTypes.func,
onPaintStart: PropTypes.func,
onPaintEnd: PropTypes.func,
sliceNormal: PropTypes.array.isRequired,
dataDetails: PropTypes.object,
onCreated: PropTypes.func,
onDestroyed: PropTypes.func,
labelmapRenderingOptions: PropTypes.object,
};
static defaultProps = {
painting: false,
sliceNormal: [0, 0, 1],
labelmapRenderingOptions: {
visible: true,
renderOutline: false,
},
};
constructor(props) {
super(props);
this.genericRenderWindow = null;
this.widgetManager = vtkWidgetManager.newInstance();
this.container = React.createRef();
this.subs = {
interactor: createSub(),
data: createSub(),
labelmap: createSub(),
paint: createSub(),
paintStart: createSub(),
paintEnd: createSub(),
};
}
componentDidMount() {
this.genericRenderWindow = vtkGenericRenderWindow.newInstance({
background: [0, 0, 0],
});
this.genericRenderWindow.setContainer(this.container.current);
let widgets = [];
let filters = [];
let actors = [];
let volumes = [];
const radius = 5;
const label = 1;
this.renderer = this.genericRenderWindow.getRenderer();
this.renderWindow = this.genericRenderWindow.getRenderWindow();
this.widgetManager.disablePicking();
this.widgetManager.setRenderer(this.renderer);
this.paintWidget = vtkPaintWidget.newInstance();
this.paintWidget.setRadius(radius);
this.paintFilter = vtkPaintFilter.newInstance();
this.paintFilter.setLabel(label);
this.paintFilter.setRadius(radius);
// trigger pipeline update
this.componentDidUpdate({});
if (this.props.actors) {
actors = actors.concat(this.props.actors);
}
if (this.labelmap && this.labelmap.actor) {
actors = actors.concat(this.labelmap.actor);
}
if (this.props.volumes) {
volumes = volumes.concat(this.props.volumes);
}
filters = [this.paintFilter];
widgets = [this.paintWidget];
// must be added AFTER the data volume is added so that this can be rendered in front
if (this.labelmap && this.labelmap.actor) {
this.renderer.addVolume(this.labelmap.actor);
}
this.renderer.resetCamera();
this.renderer.updateLightsGeometryToFollowCamera();
// TODO: Not sure why this is necessary to force the initial draw
this.genericRenderWindow.resize();
if (this.props.onCreated) {
/**
* Note: The contents of this Object are
* considered part of the API contract
* we make with consumers of this component.
*/
const api = {
genericRenderWindow: this.genericRenderWindow,
widgetManager: this.widgetManager,
container: this.container.current,
widgets,
filters,
actors,
volumes,
type: 'VIEW3D',
_component: this, // Backdoor still open for now whilst the API isn't as mature as View2D.
};
this.props.onCreated(api);
}
}
componentDidUpdate(prevProps) {
console.time('View3D componentDidUpdate');
if (prevProps.volumes !== this.props.volumes) {
this.props.volumes.forEach(volume => {
if (!volume.isA('vtkVolume')) {
console.warn('Data to <Vtk2D> is not vtkVolume data');
}
});
if (this.props.volumes.length) {
this.props.volumes.forEach(this.renderer.addVolume);
} else {
// TODO: Remove all volumes
}
this.renderWindow.render();
}
if (prevProps.actors !== this.props.actors && this.props.actors) {
this.props.actors.forEach(actor => {
if (!actor.isA('vtkActor')) {
console.warn('Data to <Vtk2D> is not vtkActor data');
}
});
if (this.props.actors.length) {
this.props.actors.forEach(this.renderer.addActor);
} else {
// TODO: Remove all actors
}
this.renderWindow.render();
}
if (
!prevProps.paintFilterBackgroundImageData &&
this.props.paintFilterBackgroundImageData
) {
// re-render if data has updated
this.subs.data.sub(
this.props.paintFilterBackgroundImageData.onModified(() =>
this.renderWindow.render()
)
);
this.paintFilter.setBackgroundImage(
this.props.paintFilterBackgroundImageData
);
} else if (
prevProps.paintFilterBackgroundImageData &&
!this.props.paintFilterBackgroundImageData
) {
this.paintFilter.setBackgroundImage(null);
this.subs.data.unsubscribe();
}
if (
prevProps.paintFilterLabelMapImageData !==
this.props.paintFilterLabelMapImageData &&
this.props.paintFilterLabelMapImageData
) {
this.subs.labelmap.unsubscribe();
const labelmapImageData = this.props.paintFilterLabelMapImageData;
const labelmap = createLabelPipeline(
this.props.paintFilterBackgroundImageData,
labelmapImageData,
this.props.labelmapRenderingOptions,
false
);
this.labelmap = labelmap;
labelmap.mapper.setInputConnection(this.paintFilter.getOutputPort());
// You can update the labelmap externally just by calling modified()
this.paintFilter.setLabelMap(labelmapImageData);
this.subs.labelmap.sub(
labelmapImageData.onModified(() => {
labelmap.mapper.modified();
this.renderWindow.render();
})
);
}
if (prevProps.painting !== this.props.painting) {
if (this.props.painting) {
console.time('turnOnPainting');
this.viewWidget = this.widgetManager.addWidget(
this.paintWidget,
ViewTypes.VOLUME
);
this.subs.paintStart.sub(
this.viewWidget.onStartInteractionEvent(() => {
this.paintFilter.startStroke();
this.paintFilter.addPoint(
this.paintWidget.getWidgetState().getTrueOrigin()
);
if (this.props.onPaintStart) {
this.props.onPaintStart();
}
})
);
this.subs.paint.sub(
this.viewWidget.onInteractionEvent(() => {
if (this.viewWidget.getPainting()) {
this.paintFilter.addPoint(
this.paintWidget.getWidgetState().getTrueOrigin()
);
if (this.props.onPaint) {
this.props.onPaint();
}
}
})
);
this.subs.paintEnd.sub(
this.viewWidget.onEndInteractionEvent(() => {
const strokeBufferPromise = this.paintFilter.endStroke();
if (this.props.onPaintEnd) {
strokeBufferPromise.then(strokeBuffer => {
this.props.onPaintEnd(strokeBuffer);
});
}
})
);
this.widgetManager.grabFocus(this.paintWidget);
this.widgetManager.enablePicking();
console.timeEnd('turnOnPainting');
} else if (this.viewWidget) {
console.time('turnOffPainting');
this.widgetManager.releaseFocus();
this.widgetManager.removeWidget(this.paintWidget);
this.widgetManager.disablePicking();
this.subs.paintStart.unsubscribe();
this.subs.paint.unsubscribe();
this.subs.paintEnd.unsubscribe();
this.viewWidget = null;
console.timeEnd('turnOffPainting');
}
}
console.timeEnd('View3D componentDidUpdate');
}
componentWillUnmount() {
Object.keys(this.subs).forEach(k => {
this.subs[k].unsubscribe();
});
if (this.props.onDestroyed) {
this.props.onDestroyed();
}
this.genericRenderWindow.delete();
}
render() {
if (!this.props.volumes && !this.props.actors) {
return null;
}
const style = { width: '100%', height: '100%', position: 'relative' };
let voi = {
windowCenter: 0,
windowWidth: 0,
};
if (this.pipeline) {
const actor = this.props.volumes[0];
// Note: This controls window/level
const rgbTransferFunction = actor.getProperty().getRGBTransferFunction(0);
const range = rgbTransferFunction.getMappingRange();
const windowWidth = range[0] + range[1];
const windowCenter = range[0] + windowWidth / 2;
voi = {
windowCenter,
windowWidth,
};
}
return (
<div style={style}>
<div ref={this.container} style={style} />
<ViewportOverlay {...this.props.dataDetails} voi={voi} />
</div>
);
}
}