import '@kitware/vtk.js/favicon';
import '@kitware/vtk.js/Rendering/Profiles/Geometry';
import vtkFullScreenRenderWindow from '@kitware/vtk.js/Rendering/Misc/FullScreenRenderWindow'; import vtkActor from '@kitware/vtk.js/Rendering/Core/Actor'; import vtkMapper from '@kitware/vtk.js/Rendering/Core/Mapper'; import vtkPlaneSource from '@kitware/vtk.js/Filters/Sources/PlaneSource'; import vtkTIFFReader from '@kitware/vtk.js/IO/Image/TIFFReader'; import vtkTexture from '@kitware/vtk.js/Rendering/Core/Texture'; import vtkURLExtract from '@kitware/vtk.js/Common/Core/URLExtract';
const userParams = vtkURLExtract.extractURLParameters();
const reader = vtkTIFFReader.newInstance(); const texture = vtkTexture.newInstance(); const planeSource = vtkPlaneSource.newInstance(); const mapper = vtkMapper.newInstance(); const actor = vtkActor.newInstance(); mapper.setInputConnection(planeSource.getOutputPort()); actor.setMapper(mapper);
const myContainer = document.querySelector('body'); const fileContainer = document.createElement('div'); fileContainer.innerHTML = '<div>Select a tiff file.<br/><input type="file" class="file"/></div>'; myContainer.appendChild(fileContainer);
const fileInput = fileContainer.querySelector('input');
function zoomCameraToFitPlane(camera, planeWidth, planeHeight) { const fov = 60;
const distance = Math.max(planeWidth, planeHeight) / (2 * Math.tan((fov * Math.PI) / 180 / 2));
camera.setPosition(planeWidth / 2, planeHeight / 2, distance); camera.setFocalPoint(planeWidth / 2, planeHeight / 2, 0); camera.setViewUp(0, 1, 0);
camera.setParallelScale(planeHeight / 2); }
function update() { const imageData = reader.getOutputData(0);
texture.setInputData(imageData);
const [xMin, xMax, yMin, yMax] = imageData.getExtent(); const [spacingX, spacingY] = imageData.getSpacing();
const planeWidth = (xMax - xMin + 1) * spacingX; const planeHeight = (yMax - yMin + 1) * spacingY;
planeSource.setOrigin(0, 0, 0); planeSource.setPoint1(planeWidth, 0, 0); planeSource.setPoint2(0, planeHeight, 0);
actor.addTexture(texture);
const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance(); const renderer = fullScreenRenderer.getRenderer(); const renderWindow = fullScreenRenderer.getRenderWindow(); const camera = renderer.getActiveCamera(); const interactor = renderWindow.getInteractor();
interactor.setInteractorStyle(null);
renderer.addActor(actor);
zoomCameraToFitPlane(camera, planeWidth, planeHeight); renderer.resetCameraClippingRange();
renderWindow.render(); }
function handleFile(event) { event.preventDefault(); const dataTransfer = event.dataTransfer; const files = event.target.files || dataTransfer.files; if (files.length === 1) { const file = files[0]; const fileReader = new FileReader(); fileReader.onload = () => { reader.parse(fileReader.result); update(); }; fileReader.readAsArrayBuffer(file); } }
fileInput.addEventListener('change', handleFile);
if (userParams.fileURL) { reader.setUrl(userParams.fileURL).then(() => { reader.loadData().then(() => { update(); }); }); }
|