Force actor to fit the renderer size exactly

Hi guys.

I have a vtkImageSlice as var actor = vtkImageSlice.newInstance();, and I would like to make it exactly the size of my renderer. Is there a simple way to do this?

Or any way to resize the ImageSlice, I can’t seem to find a way to do it.

Here is the full code:

 const renderWindow = vtkRenderWindow.newInstance(); //Now uses RenderWindow instead of Fullscreen render window
        var scalars = vtkDataArray.newInstance({
            values: rgbArray,
            numberOfComponents: 3, // number of channels
            dataType: VtkDataTypes.UNSIGNED_CHAR, // values encoding
            name: 'scalars'
        });

        var imageData = vtkImageData.newInstance();
        imageData.setOrigin(0, 0, 0);
        imageData.setSpacing(1.0, 1.0, 1.0);
        imageData.setExtent(0, width - 1, 0, height - 1, 0, depth - 1);
        imageData.getPointData().setScalars(scalars);

        var mapper = vtkImageMapper.newInstance();
        mapper.setInputData(imageData);

        var actor = vtkImageSlice.newInstance();
        actor.setMapper(mapper);

        var view3d = document.getElementById("view3d");

        const openglRenderWindow = vtkOpenGLRenderWindow.newInstance();
        openglRenderWindow.setContainer(view3d)

        const dims = view3d.getBoundingClientRect();
        openglRenderWindow.setSize(dims.width, dims.height)

        renderWindow.addView(openglRenderWindow);

        const sliceRenderer = vtkRenderer.newInstance({
            background: [255, 255, 255]
        });

        sliceRenderer.addVolume(actor);
        renderWindow.addRenderer(sliceRenderer) 
        renderWindow.render();

You will need to modify the camera parameters to make your image the size of the renderer. If you are using orthographic/parallel projection, check out this code: https://github.com/KitwareMedical/paraview-medical/blob/master/src/vtk/proxyUtils.js#L26.

Hey have you found a solution to this? I really need the same thing

It was a while ago, and I remember adjusting the camera view until it was better, but not perfect. I don’t remember the details unfortunately, it was a while ago and I had a little time.

Here’s a github issue I opened on it, maybe it can give you some more insight.

Thank you for your answer I really appreciate it

I’d recommend camera.zoom(factor). Don’t forget to call renderWindow.render() afterwards. Alternatively, if you’re in parallel projection, then you can set camera.setParallelScale(scale) directly to either your image height or width (I think). If you’re in perspective projection, then you need to know the distance between the camera and your image slice, and then set the viewAngle based on that.

If the viewport resizes, you will need to update the zoom accordingly.

2 Likes