render single slice of a volume with vtkVolume shows no result

hey!

im using vtk 9.2 to render 3d volumes with the following code

auto mapper = vtkSmartPointer<vtkOpenGLGPUVolumeRayCastMapper>::New();
mapper->SetInputConnection(imagedata->GetOutputPort());

auto actor = vtkSmartPointer<vtkVolume>::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(colortransferfunction);
actor->GetProperty()->SetScalarOpacity(scalaropacityfunction);
actor->GetProperty()->ShadeOn();
actor->GetProperty()->SetInterpolationTypeToLinear();

renderer->AddViewProp(actor);

everythings works fine for volumes with a z dimension greater than 1 (fx 500x500x500)

when i load “volumetric” data with z dimension 1 (for example an extracted slice, 500x500x1), the same code produces simply a black and opaque image in the renderwindow.

Using vtkImageSliceMapper and vtkImageSlice, a render result is visible for the same data. As far as i understand, i cannot make use of a scalar opacity function with vtkImageSlice. It simply uses the alpha values of the image, which i dont have since my data is grayscale CT data and the scalar opacity is created via code/user interaction in a widget.

I tried to check the behaviour of paraview, but when loading a single slice and setting the rendering to volume, paraview crashes.

Why does the code above produce no result for a volume with 1 slice? Is there a setting/property for that case on vtkVolume/vtkRenderer/vtkOpenGLGPUVolumeRayCastMapper?

Or is there a way to use vtkPiecewiseFunction with vtkImageSlice for scalar opacity?

thanks!

Set the blend mode to slicing via vtkVolumeMapper::SetBlendMode

hey @sankhesh :slightly_smiling_face:

when calling

mapper->SetBlendModeToSlice();

im getting a shader compile error in the vtk debug output. the compile error states:

ERROR: In vtkShaderProgram.cxx, line 453
vtkShaderProgram (000001E6C389E050): 0(475) : error C1503: undefined variable “g_intersection”

here is a minimum example of code i use:

#include <vtkOpenGLGPUVolumeRayCastMapper.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
#include <vtkXMLImageDataReader.h>

int main(int argc, char** argv) {
	vtkNew<vtkXMLImageDataReader> reader;
	reader->SetFileName("C:/Users/amr/Desktop/ruffy_extracted_slice.vti");
	reader->Update();

	vtkNew<vtkOpenGLGPUVolumeRayCastMapper> mapper;
	mapper->SetInputConnection(reader->GetOutputPort());
	mapper->SetBlendModeToSlice();

	vtkNew<vtkVolume> actor;
	actor->SetMapper(mapper);

	vtkNew<vtkRenderer> renderer;
	renderer->AddViewProp(actor);
	renderer->ResetCamera();

	vtkNew<vtkRenderWindow> window;
	window->SetSize(300, 300);
	window->AddRenderer(renderer);
	window->SetWindowName("Example");

	vtkNew<vtkRenderWindowInteractor> interactor;
	interactor->SetRenderWindow(window);

	window->Render();
	interactor->Start();
}

im attaching the log output and an example file im using for testing. the vtk version i use is 9.2.20220823

shader_compileerror.txt (18.4 KB)
ruffy_extracted_slice.vti (726.6 KB)

edit: i digged into the code that creates the shader code and i noticed that

actor->GetProperty()->GetSliceFunction(); 

returns nullptr and thats why vtk doesnt create the shader code that calculates g_intersection. so i would rephrase my question:

edit: i tried setting the plane and moving it through the volume with a slider. this worked when using a volume with z > 1 as expected, but when setting the plane and/or moving it through a volume with z dimension = 1 no render result is produced.

Ideally, thin volumes (z = 1) should be renderable but my guess is that the GPU raycaster is running into boundary conditions where start and stop ray positions are near-coincident.

So, to answer the question about your other approach, vtkImageSlice allows specifying properties via vtkImageProperty which in turn, accepts a lookup table via SetLookupTable(vtkScalarsToColors*). You could set the lookup table to a vtkLookupTable with RGBA table values.That should let you map scalars to both color and opacity.