vtkSmartVolumeMapper简单使用
VTK体绘制的体绘制映射器:vtkSmartVolumeMapper
的分析与简单使用。可以用于替代之前常用的vtkVolumeRayCastMapper
。
1. 缘起
在阅读《VTK图形图像开发进阶》一书中关于体绘制的章节时,发现里面提到的vtkVolumeRayCastCompositeFunction
和vtkVolumeRayCastMapper
在后来的vtk版本中已经不再支持。
书中的代码如下所示:
vtkSmartPointer<vtkStructuredPointsReader> reader =
vtkSmartPointer<vtkStructuredPointsReader>::New();
reader->SetFileName(filePath);
reader->Update();
vtkSmartPointer<vtkVolumeRayCastCompositeFunction> rayCastFun =
vtkSmartPointer<vtkVolumeRayCastCompositeFunction>::New();
vtkSmartPointer<vtkVolumeRayCastMapper> volumeMapper =
vtkSmartPointer<vtkVolumeRayCastMapper>::New();
volumeMapper->SetInput(reader->GetOutput());
volumeMapper->SetVolumeRayCastFunction(rayCastFun);
2. 分析
对于上面提到的vtkVolumeRayCastMapper
,起的作用就是体映射。
很明显,它一种具体的体映射的实现,那么顺着往上找,应该能找到统一的基类,很容易就找到了vtkVolumeMapper
类,这个类是一个抽象类。
在它的官方文档中可以看到,如下的类图:
可以看到具体实现了vtkVolumeMapper
有好几个类,但是咋一看,有的类名里面包含GPU这样的字样,一看就大概心里有数,这个对于不支持CPU的不太友好。其他的呢?我现在初始了解这些内容,一下子也很难了解到其中的具体使用场景。
看到里面有一个名叫vtkSmartVolumeMapper
的子类,再一查看,是一个具体类,仅仅从名字上看,这个类的能耐应该就是上面全部的子类或者大部分子类的和,可以自动选择调用那一个。
3. vtkSmartVolumeMapper帮组文档的描述
以下的文档是基于vtk9.x的,其他版本的枚举值以及代表的内容可能有变化。
Adaptive volume mapper.
自适应的体绘制。
vtkSmartVolumeMapper is a volume mapper that will delegate to a specific volume mapper based on rendering parameters and available hardware. Use the SetRequestedRenderMode() method to control the behavior of the selection. The following options are available:
基于渲染参数和可用硬件,vtkSmartVolumeMapper这个体绘制映射器将委托给一个特定的体绘制映射器。使用SetRequestedRenderMode()方法来控制选择的行为。提供以下选项:
vtkSmartVolumeMapper::DefaultRenderMode(默认渲染模式):
Allow the vtkSmartVolumeMapper to select the best mapper based on rendering parameters and hardware support. If GPU ray casting is supported, this mapper will be used for all rendering. If not, then the vtkFixedPointRayCastMapper will be used exclusively. This is the default requested render mode, and is generally the best option. When you use this option, your volume will always be rendered, but the method used to render it may vary based on parameters and platform.
允许 vtkSmartVolumeMapper 根据渲染参数和硬件支持选择最佳映射器。 如果支持 GPU 光线投射(ray casting),则此(RayCast)映射器将用于所有渲染。 如果不支持,则将只使用 vtkFixedPointRayCastMapper。 这是默认的被请求的渲染模式,通常是最好的选择。 当您使用此选项时,您的体对象将始终被渲染,但用于渲染它的方法可能会因参数和平台而异。
vtkSmartVolumeMapper::RayCastRenderMode(光线投射渲染模式):
Use the vtkFixedPointVolumeRayCastMapper for both interactive and still rendering. When you use this option your volume will always be rendered with the vtkFixedPointVolumeRayCastMapper.
使用 vtkFixedPointVolumeRayCastMapper 进行交互式和静态渲染。 当您使用此选项时,您的体对象将始终使用 vtkFixedPointVolumeRayCastMapper 进行渲染。
vtkSmartVolumeMapper::GPURenderMode(GPU渲染模式):
Use the vtkGPUVolumeRayCastMapper, if supported, for both interactive and still rendering. If the GPU ray caster is not supported (due to hardware limitations or rendering parameters) then no image will be rendered. Use this option only if you have already checked for supported based on the current hardware, number of scalar components, and rendering parameters in the vtkVolumeProperty.
使用 vtkGPUVolumeRayCastMapper(如果支持)进行交互式和静态渲染。 如果不支持 GPU 光线投射器(由于硬件限制或渲染参数),则不会渲染任何图像。 仅当您已经根据当前硬件、标量组件的数量和 vtkVolumeProperty 中的渲染参数检查支持后,才使用此选项。
You can adjust the contrast and brightness in the rendered image using the FinalColorWindow and FinalColorLevel ivars. By default the FinalColorWindow is set to 1.0, and the FinalColorLevel is set to 0.5, which applies no correction to the computed image. To apply the window / level operation to the computer image color, first a Scale and Bias value are computed:
您可以使用 FinalColorWindow 和 FinalColorLevel调整渲染图像中的对比度和亮度。 默认情况下,FinalColorWindow 设置为 1.0,FinalColorLevel 设置为 0.5,这对计算图像不应用任何校正。 要将窗口/级别操作应用于计算机图像颜色,首先要计算 Scale 和 Bias 值:
scale = 1.0 / this->FinalColorWindow
bias = 0.5 - this->FinalColorLevel / this->FinalColorWindow
To compute a new color (R', G', B', A') from an existing color (R,G,B,A) for a pixel, the following equation is used:
要从像素的现有颜色 (R,G,B,A) 计算新颜色 (R', G', B', A'),使用以下等式:
R' = Rscale + biasA
G' = Gscale + biasA
B' = Bscale + biasA
A' = A
Note that bias is multiplied by the alpha component before adding because the red, green, and blue component of the color are already pre-multiplied by alpha. Also note that the window / level operation leaves the alpha component unchanged - it only adjusts the RGB values.
请注意,bias在添加之前乘以 alpha 分量,因为颜色的红色、绿色和蓝色分量已经预先乘以 alpha。 另请注意,窗口/级别操作使 alpha 组件保持不变 - 它仅调整 RGB 值。
4. 总结
通过上一个小节的官方文档的描述,使用vtkSmartVolumeMapper
来执行通常的操作是比较优秀的,除非已经完全了解软硬件的支持,才选择特定得映射器类型。
上面的程序可以写成:
vtkNew<vtkStructuredPointsReader> reader;
reader->SetFileName(filePath);
reader->Update();
vtkNew<vtkSmartVolumeMapper> volumeMapper;
volumeMapper->SetInputConnection(reader->GetOutputPort());