|
| 1 | +import React from 'react'; |
| 2 | +import { Component } from 'react'; |
| 3 | + |
| 4 | +import { View2D, getImageData, loadImageData } from '@vtk-viewport'; |
| 5 | +import cornerstone from 'cornerstone-core'; |
| 6 | +import cornerstoneTools from 'cornerstone-tools'; |
| 7 | +import './initCornerstone.js'; |
| 8 | +import vtkVolumeMapper from 'vtk.js/Sources/Rendering/Core/VolumeMapper'; |
| 9 | +import vtkVolume from 'vtk.js/Sources/Rendering/Core/Volume'; |
| 10 | + |
| 11 | +window.cornerstoneTools = cornerstoneTools; |
| 12 | + |
| 13 | +function createActorMapper(imageData) { |
| 14 | + const mapper = vtkVolumeMapper.newInstance(); |
| 15 | + mapper.setInputData(imageData); |
| 16 | + |
| 17 | + const actor = vtkVolume.newInstance(); |
| 18 | + actor.setMapper(mapper); |
| 19 | + |
| 20 | + return { |
| 21 | + actor, |
| 22 | + mapper, |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +const ROOT_URL = |
| 27 | + window.location.hostname === 'localhost' |
| 28 | + ? window.location.host |
| 29 | + : window.location.hostname; |
| 30 | + |
| 31 | +const imageIds = [ |
| 32 | + `dicomweb://${ROOT_URL}/PTCTStudy/1.3.6.1.4.1.25403.52237031786.3872.20100510032221.1.dcm`, |
| 33 | + `dicomweb://${ROOT_URL}/PTCTStudy/1.3.6.1.4.1.25403.52237031786.3872.20100510032221.2.dcm`, |
| 34 | + `dicomweb://${ROOT_URL}/PTCTStudy/1.3.6.1.4.1.25403.52237031786.3872.20100510032221.3.dcm`, |
| 35 | + `dicomweb://${ROOT_URL}/PTCTStudy/1.3.6.1.4.1.25403.52237031786.3872.20100510032221.4.dcm`, |
| 36 | + `dicomweb://${ROOT_URL}/PTCTStudy/1.3.6.1.4.1.25403.52237031786.3872.20100510032221.5.dcm`, |
| 37 | +]; |
| 38 | + |
| 39 | +class VTKLoadImageDataExample extends Component { |
| 40 | + state = { |
| 41 | + volumes: null, |
| 42 | + vtkImageData: null, |
| 43 | + cornerstoneViewportData: null, |
| 44 | + focusedWidgetId: null, |
| 45 | + isSetup: false, |
| 46 | + }; |
| 47 | + |
| 48 | + componentDidMount() { |
| 49 | + this.components = {}; |
| 50 | + this.cornerstoneElements = {}; |
| 51 | + |
| 52 | + // Pre-retrieve the images for demo purposes |
| 53 | + // Note: In a real application you wouldn't need to do this |
| 54 | + // since you would probably have the image metadata ahead of time. |
| 55 | + // In this case, we preload the images so the WADO Image Loader can |
| 56 | + // read and store all of their metadata and subsequently the 'getImageData' |
| 57 | + // can run properly (it requires metadata). |
| 58 | + const promises = imageIds.map(imageId => { |
| 59 | + return cornerstone.loadAndCacheImage(imageId); |
| 60 | + }); |
| 61 | + |
| 62 | + Promise.all(promises).then( |
| 63 | + () => { |
| 64 | + const displaySetInstanceUid = '12345'; |
| 65 | + const cornerstoneViewportData = { |
| 66 | + stack: { |
| 67 | + imageIds, |
| 68 | + currentImageIdIndex: 0, |
| 69 | + }, |
| 70 | + displaySetInstanceUid, |
| 71 | + }; |
| 72 | + |
| 73 | + const imageDataObject = getImageData(imageIds, displaySetInstanceUid); |
| 74 | + |
| 75 | + loadImageData(imageDataObject).then(() => { |
| 76 | + const { actor } = createActorMapper(imageDataObject.vtkImageData); |
| 77 | + |
| 78 | + this.setState({ |
| 79 | + vtkImageData: imageDataObject.vtkImageData, |
| 80 | + volumes: [actor], |
| 81 | + cornerstoneViewportData, |
| 82 | + }); |
| 83 | + }); |
| 84 | + }, |
| 85 | + error => { |
| 86 | + throw new Error(error); |
| 87 | + } |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + saveCornerstoneElements = viewportIndex => { |
| 92 | + return event => { |
| 93 | + this.cornerstoneElements[viewportIndex] = event.detail.element; |
| 94 | + }; |
| 95 | + }; |
| 96 | + |
| 97 | + setWidget = event => { |
| 98 | + const widgetId = event.target.value; |
| 99 | + |
| 100 | + if (widgetId === 'rotate') { |
| 101 | + this.setState({ |
| 102 | + focusedWidgetId: null, |
| 103 | + }); |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + render() { |
| 108 | + return ( |
| 109 | + <div className="row"> |
| 110 | + <div className="col-xs-12"> |
| 111 | + <h1>Loading a cornerstone displayset into vtkjs</h1> |
| 112 | + <p> |
| 113 | + The example demonstrates loading cornerstone images already |
| 114 | + available in the application into a vtkjs viewport. |
| 115 | + </p> |
| 116 | + <hr /> |
| 117 | + </div> |
| 118 | + <div className="col-xs-12"> |
| 119 | + <div className="col-xs-12"> |
| 120 | + <label> |
| 121 | + <input |
| 122 | + type="radio" |
| 123 | + value="rotate" |
| 124 | + name="widget" |
| 125 | + onChange={this.setWidget} |
| 126 | + checked={this.state.focusedWidgetId === null} |
| 127 | + />{' '} |
| 128 | + Rotate |
| 129 | + </label> |
| 130 | + </div> |
| 131 | + <div className="col-xs-12 col-sm-6"> |
| 132 | + {this.state.volumes && <View2D volumes={this.state.volumes} />} |
| 133 | + </div> |
| 134 | + </div> |
| 135 | + </div> |
| 136 | + ); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +export default VTKLoadImageDataExample; |
0 commit comments