|
| 1 | +import '@kitware/vtk.js/favicon'; |
| 2 | + |
| 3 | +// Load the rendering pieces we want to use (for both WebGL and WebGPU) |
| 4 | +import '@kitware/vtk.js/Rendering/Profiles/Geometry'; |
| 5 | + |
| 6 | +import vtkFullScreenRenderWindow from '@kitware/vtk.js/Rendering/Misc/FullScreenRenderWindow'; |
| 7 | +import vtkActor from '@kitware/vtk.js/Rendering/Core/Actor'; |
| 8 | +import vtkMapper from '@kitware/vtk.js/Rendering/Core/Mapper'; |
| 9 | +import vtkPlaneSource from '@kitware/vtk.js/Filters/Sources/PlaneSource'; |
| 10 | +import vtkTGAReader from '@kitware/vtk.js/IO/Image/TGAReader'; |
| 11 | +import vtkTexture from '@kitware/vtk.js/Rendering/Core/Texture'; |
| 12 | +import vtkURLExtract from '@kitware/vtk.js/Common/Core/URLExtract'; |
| 13 | + |
| 14 | +// ---------------------------------------------------------------------------- |
| 15 | +// Example code |
| 16 | +// ---------------------------------------------------------------------------- |
| 17 | +const userParams = vtkURLExtract.extractURLParameters(); |
| 18 | + |
| 19 | +const reader = vtkTGAReader.newInstance(); |
| 20 | +const texture = vtkTexture.newInstance(); |
| 21 | +const planeSource = vtkPlaneSource.newInstance(); |
| 22 | +const mapper = vtkMapper.newInstance(); |
| 23 | +const actor = vtkActor.newInstance(); |
| 24 | +mapper.setInputConnection(planeSource.getOutputPort()); |
| 25 | +actor.setMapper(mapper); |
| 26 | + |
| 27 | +// ---------------------------------------------------------------------------- |
| 28 | +// Use a file reader to load a local file |
| 29 | +// ---------------------------------------------------------------------------- |
| 30 | + |
| 31 | +const myContainer = document.querySelector('body'); |
| 32 | +const fileContainer = document.createElement('div'); |
| 33 | +fileContainer.innerHTML = |
| 34 | + '<div>Select a tga file.<br/><input type="file" class="file"/></div>'; |
| 35 | +myContainer.appendChild(fileContainer); |
| 36 | + |
| 37 | +const fileInput = fileContainer.querySelector('input'); |
| 38 | + |
| 39 | +function zoomCameraToFitPlane(camera, planeWidth, planeHeight) { |
| 40 | + const fov = 60; // Field of view in degrees |
| 41 | + |
| 42 | + // Calculate the distance needed to fit the plane in view |
| 43 | + const distance = |
| 44 | + Math.max(planeWidth, planeHeight) / |
| 45 | + (2 * Math.tan((fov * Math.PI) / 180 / 2)); |
| 46 | + |
| 47 | + // Set camera position |
| 48 | + camera.setPosition(planeWidth / 2, planeHeight / 2, distance); |
| 49 | + camera.setFocalPoint(planeWidth / 2, planeHeight / 2, 0); |
| 50 | + camera.setViewUp(0, 1, 0); |
| 51 | + |
| 52 | + // Set parallel scale for orthographic projection |
| 53 | + camera.setParallelScale(planeHeight / 2); |
| 54 | +} |
| 55 | + |
| 56 | +function update() { |
| 57 | + // Get the vtkImageData from the reader |
| 58 | + const imageData = reader.getOutputData(0); |
| 59 | + |
| 60 | + // Set the vtkImageData as the texture input |
| 61 | + texture.setInputData(imageData); |
| 62 | + |
| 63 | + // // Get the image's extent and spacing |
| 64 | + const [xMin, xMax, yMin, yMax] = imageData.getExtent(); |
| 65 | + const [spacingX, spacingY] = imageData.getSpacing(); |
| 66 | + |
| 67 | + // // Calculate the plane's width and height based on the image's dimensions |
| 68 | + const planeWidth = (xMax - xMin + 1) * spacingX; |
| 69 | + const planeHeight = (yMax - yMin + 1) * spacingY; |
| 70 | + |
| 71 | + // Set the plane's origin and corners based on calculated width and height |
| 72 | + planeSource.setOrigin(0, 0, 0); |
| 73 | + planeSource.setPoint1(planeWidth, 0, 0); // Horizontal edge |
| 74 | + planeSource.setPoint2(0, planeHeight, 0); // Vertical edge |
| 75 | + |
| 76 | + actor.addTexture(texture); |
| 77 | + |
| 78 | + const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance(); |
| 79 | + const renderer = fullScreenRenderer.getRenderer(); |
| 80 | + const renderWindow = fullScreenRenderer.getRenderWindow(); |
| 81 | + const camera = renderer.getActiveCamera(); |
| 82 | + const interactor = renderWindow.getInteractor(); |
| 83 | + |
| 84 | + // Disable default interactor style |
| 85 | + interactor.setInteractorStyle(null); |
| 86 | + |
| 87 | + renderer.addActor(actor); |
| 88 | + |
| 89 | + // Adjust the camera to fit the plane in the view |
| 90 | + zoomCameraToFitPlane(camera, planeWidth, planeHeight); |
| 91 | + renderer.resetCameraClippingRange(); |
| 92 | + |
| 93 | + renderWindow.render(); |
| 94 | +} |
| 95 | + |
| 96 | +function handleFile(event) { |
| 97 | + event.preventDefault(); |
| 98 | + const dataTransfer = event.dataTransfer; |
| 99 | + const files = event.target.files || dataTransfer.files; |
| 100 | + if (files.length === 1) { |
| 101 | + const file = files[0]; |
| 102 | + const fileReader = new FileReader(); |
| 103 | + fileReader.onload = () => { |
| 104 | + reader.parse(fileReader.result); |
| 105 | + update(); |
| 106 | + }; |
| 107 | + fileReader.readAsArrayBuffer(file); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +fileInput.addEventListener('change', handleFile); |
| 112 | + |
| 113 | +// ---------------------------------------------------------------------------- |
| 114 | +// Use the reader to download a file |
| 115 | +// ---------------------------------------------------------------------------- |
| 116 | +if (userParams.fileURL) { |
| 117 | + reader.setUrl(userParams.fileURL).then(() => { |
| 118 | + reader.loadData().then(() => { |
| 119 | + update(); |
| 120 | + }); |
| 121 | + }); |
| 122 | +} |
0 commit comments