VTK第二篇之菜鸟入门——三维立体显示

廉价感情. 提交于 2020-02-06 13:27:35

先说些题外话:在C3P里面找到了一些材料,都挺不错的。下面的代码,惭愧,忘了作者是谁了。是一个很基础的三维显示。十分万分地感谢作者。

首先,关于这个VTK程序的结构,较之第一篇文章,稍微有点新的认识。

VTK的一些基本的东西,下面这些也是从整理的,虽然不是原创,写得还是相当好的,所以就“剽窃”了。

Sources:通过这些类产生数据源,比如vtkDICOMImageReader,读取DICOM 文件,产生输出数据。第一篇里面就是vtkConeSource。
Filters:Filter 是一种数据处理机制,有一个或多个输入,有一个输出。其目的是对图形图像数据进行处理,以得到期望中的数据。比如vtkMarchingCubes,对数据进行MarchingCubes 算法的处理,输出重建的物体表面数据。
Mappers:通过这些类将经过各种filter 处理后的应用数据映射为几何数据为原始数据与图像数据之间定义了接口。不同的Mapper 可以共享同一个输入数据,但是使用不同的方式渲染它。
Props/Actors:这些类用来表带绘制场景中的一个实体。它通过SetMapper()方法将几何数据的属性告诉演员然后通过 Renderer 将结果在窗口中显示出来。
RenderWindow:RenderWindow 是计算机屏幕上用来显示图像的一片真实的区域。其中包含交互器,通过它可以处理用户鼠标和键盘的输入。

 

下面继续像往常一样,开始解释程序吧。还是要声明下,咱是菜鸟,可能有理解错误的地方。

 

1.这些头文件就先放着吧,复制粘贴程序的时候可能有用到吧。至于重要类的解释,还是放在具体的程序里面吧。

#include "vtkRenderer.h"
#include
"vtkRenderWindow.h"
#include
"vtkRenderWindowInteractor.h"
#include
"vtkProperty.h"
#include
"vtkCamera.h"
#include
"vtkStructuredPointsReader.h"
#include
"vtkPiecewiseFunction.h"
#include
"vtkColorTransferFunction.h"
#include
"vtkVolumeProperty.h"
#include
"vtkColorTransferFunction.h"
#include
"vtkVolumeRayCastCompositeFunction.h"
#include
"vtkVolumeRayCastMapper.h"

#pragma comment (lib, "vtkFiltering.lib")
#pragma comment (lib, "vtkRendering.lib")
#pragma comment (lib, "vtkIO.lib")
#pragma comment (lib, "vtkVolumeRendering.lib")

 

2.这部分的代码貌似是比较固定的,就是搭上一个显示用的窗口框架而已。vtkRenderWindowInteractor是一个可以接受鼠标和键盘输入的窗口。它包着vtkRenderWindow,而vtkRenderWindow又包着vtkRenderer。

int main (int argc, char**argv)
{
vtkRenderer
*aRenderer = vtkRenderer::New();
vtkRenderWindow
*renWin = vtkRenderWindow::New();
renWin
->AddRenderer(aRenderer);
vtkRenderWindowInteractor
*iren = vtkRenderWindowInteractor::New();
iren
->SetRenderWindow(renWin);

 

3.读入数据的部分了。VTK可以读各种类型的数据,这些都有成型的类了,虽然用着方便,但是由于VTK教材多是英文,还不知道怎么用的时候却是一件很麻烦的事情。英语不咋滴,所以把原文也放上去了。

vtkStructuredPointsReader是一个用于读取ASCII或者二进制结构化的vtk格式的数据文件的类。(vtkStructuredPointsReader is a source object that reads ASCII or binary structured points data files in vtk format (see text for format details).)

vtkVolumeRayCastCompositeFunction是一个和光线有关的函数,可以在vtkVolumeRayCastMapper类中使用。它根据vtkVolumeProperty中的属性来对光线进行混合操作。(vtkVolumeRayCastCompositeFunction is a ray function that can be used within a vtkVolumeRayCastMapper. This function performs compositing along the ray according to the properties stored in the vtkVolumeProperty for the volume.)

vtkVolumeRayCastMapper是一个以慢但是精确地速度渲染场景。(vtkVolumeRayCastMapper: A slow but accurate mapper for rendering volumes.)

vtkVolumeProperty这个类的解释太长了,其实顾名思义,就是设置一下Volume的属性。(vtkVolumeProperty is used to represent common properties associated with volume rendering. This includes properties for determining the type of interpolation to use when sampling a volume, the color of a volume, the scalar opacity of a volume, the gradient opacity of a volume, and the shading parameters of a volume.

When the scalar opacity or the gradient opacity of a volume is not set, then the function is defined to be a constant value of 1.0. When a scalar and gradient opacity are both set simultaneously, then the opacity is defined to be the product of the scalar opacity and gradient opacity transfer functions.

Most properties can be set per "component" for volume mappers that support multiple independent components. If you are using 2 component data as LV or 4 component data as RGBV (as specified in the mapper) only the first scalar opacity and gradient opacity transfer functions will be used (and all color functions will be ignored). Omitting the index parameter on the Set/Get methods will access index = 0.

vtkVolume在渲染场景的时候代表一卷。从后面的程序来看,貌似和Actor是同一类型的。(vtkVolume represents a volume (data & properties) in a rendered scene)

    vtkStructuredPointsReader *reader = vtkStructuredPointsReader::New();    reader->SetFileName(".\\ironProt.vtk");    vtkVolumeRayCastCompositeFunction *ComositeFun =       vtkVolumeRayCastCompositeFunction::New();    vtkVolumeRayCastMapper *VolMapper = vtkVolumeRayCastMapper::New();    VolMapper->SetInputConnection(reader->GetOutputPort());    VolMapper->SetVolumeRayCastFunction(ComositeFun);        vtkVolume *Volume = vtkVolume::New();    Volume->SetMapper(VolMapper);

 

4.下面是对属性的具体设置了,不是很明白所以就不翻译啦。

vtkColorTransferFunction

Defines a transfer function for mapping a property to an RGB color value.

vtkColorTransferFunction is a color mapping in RGB or HSV space that uses piecewise hermite functions to allow interpolation that can be piecewise constant, piecewise linear, or somewhere in-between (a modified piecewise hermite function that squishes the function according to a sharpness parameter). The function also allows for the specification of the midpoint (the place where the function reaches the average of the two bounding nodes) as a normalize distance between nodes.

AddRGBSegment
void vtkColorTransferFunction::AddRGBSegment ( double x1,
double r1,
double g1,
double b1,
double x2,
double r2,
double g2,
double b2
)

Add two points to the function and remove all the points between them
SetColor
void vtkVolumeProperty::SetColor ( vtkColorTransferFunction * f ) [inline]

Set the color of a volume to an RGB transfer function
for the component indicated
by index. This will set the color channels forthis component to 3.
This will also recompute the color channels

vtkPiecewiseFunction

Defines a 1D piecewise function.

Defines a piecewise function mapping. This mapping allows the addition of control points, and allows the user to control the function between the control points. A piecewise hermite curve is used between control points, based on the sharpness and midpoint parameters. A sharpness of 0 yields a piecewise linear function and a sharpness of 1 yields a piecewise constant function. The midpoint is the normalized distance between control points at which the curve reaches the median Y value. The midpoint and sharpness values specified when adding a node are used to control the transition to the next node (the last node's values are ignored) Outside the range of nodes, the values are 0 if Clamping is off, or the nearest node point if Clamping is on. Using the legacy methods for adding points (which do not have Sharpness and Midpoint parameters) will default to Midpoint = 0.5 (halfway between the control points) and Sharpness = 0.0 (linear).

 

vtkPiecewiseFunction::AddSegment
void vtkPiecewiseFunction::AddSegment ( double x1,
double y1,
double x2,
double y2
)

Add a line segment to the function. All points defined between the two points
specified are removed from the function. This is a legacy method that does not
allow the specification of the sharpness and midpoint values for the two nodes.

 

vtkVolumeProperty::SetScalarOpacity
void vtkVolumeProperty::SetScalarOpacity ( int index,
vtkPiecewiseFunction
* function
)

Set the opacity of a volume to an opacity transfer function based on scalar
value for the component indicated by index.
void vtkVolumeProperty::SetInterpolationTypeToNearest ()

Set the interpolation type
for sampling a volume.
Initial value is VTK_NEAREST_INTERPOLATION.

 

 

 

 

ShadeOn
void vtkVolumeProperty::ShadeOn ( int index ) Set/Get the shading of a volume.
If shading is turned off, then the mapper for the volume will not perform shading
calculations. If shading is turned on, the mapper may perform shading calculations
-in some cases shading does not apply (for example, in a maximum intensity
projection) and therefore shading will not be performed even ifthis flag is on.
For a compositing type of mapper, turning shading off is generally the same as
setting ambient=1, diffuse=0, specular=0. Shading can be independently turned
on/off per component.

 

 

 

 

vtkColorTransferFunction *gTFun = vtkColorTransferFunction::New();
gTFun
->AddRGBSegment(128.0,126/255.0,16/255.0,16/255.0,
  
255.0,64/255.0,255/255.0,16/255.0);
vtkVolumeProperty
*VolProperty = vtkVolumeProperty::New();
VolProperty
->SetColor(gTFun);


vtkPiecewiseFunction
*oTFun = vtkPiecewiseFunction::New();
oTFun
->AddSegment(48,0.0,256,1.0);//改变第一个数据,可以得到不同的体透明度
VolProperty->SetScalarOpacity(oTFun);
VolProperty
->SetInterpolationTypeToLinear();
VolProperty
->ShadeOn();
Volume
->SetProperty(VolProperty);

 

5.收尾工作啦!

 

aRenderer->AddActor(Volume);
aRenderer
->SetBackground(1,1,1);
renWin
->SetSize(640,480);
aRenderer
->ResetCameraClippingRange();
iren
->Initialize();
iren
->Start();

aRenderer
->Delete();
renWin
->Delete();
iren
->Delete();

return0;
}

 

OK,一切完毕,可以看看到底显示出来的是什么了,是从各个角度看的。其中数据文件,我不知道博客能否上传文件,呵呵

 
 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!