VTK资料分享:张晓东的《VTK图形图像开发进阶》全套资料(包含书籍电子版,配套每章节所有源码、实现效果图和工程配置文件)
水灵VTK学习全套资料(包含全部视频、PPT、源码和每个项目对应的工程配置文件CmakeLists.txt)
水灵VT视频在线观看
本文参考的主要是张晓东的《VTK图形图像开发进阶》和水灵的视频Study VTK Together。使用的平台是VS2019+VTK8.2
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include //包含头文件(类)
#include
int main()
{
//使用智能指针定义一个vtkRenderWindow类型的对象renWin
//也可换成 vtkRenderWindow* renWin = vtkRenderWindow::New();
vtkSmartPointer renWin =
vtkSmartPointer::New();
renWin->SetWindowName("Hadley");//设置窗口名字
renWin->SetSize(300, 150);//设置窗口大小,长*宽=300*150像素
renWin->Render();//调用vtkRenderWindow里的Render()方法显示并渲染VTK窗口
std::cin.get();//等待用户输入,让程序停下来,窗口便不会一闪而过
return EXIT_SUCCESS;
}
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include //源数据
#include //数据映射
#include //绘制器
#include //绘制窗口
#include //演员
#include //照相机
int main()
{
//指针创建一个圆锥cone
vtkConeSource *cone=vtkConeSource::New();
cone->SetResolution (10);//圆锥的棱边数
cone->SetHeight(3);//指定高度
cone->SetRadius(1);//指定半径
//实例化一个映射器coneMapper
vtkPolyDataMapper *coneMapper=vtkPolyDataMapper::New();
coneMapper->SetInputConnection(cone->GetOutputPort());//源数据输出给映射器输入
//创建一个演员对象coneActor
vtkActor *coneActor=vtkActor::New();
coneActor->SetMapper(coneMapper);//为演员指定mapper进行映射
//搭建舞台 实例化对象renderer
vtkRenderer *renderer=vtkRenderer::New();
renderer->AddActor(coneActor);//将演员加入场景
renderer->SetBackground(0.2,0.2,0.4);//设置场景背景颜色是(R,G,B)
//实例化一个窗口对象renWin
vtkRenderWindow *renWin=vtkRenderWindow::New();
renWin->SetSize(400,200);//设置窗口大小宽*高
renWin->AddRenderer(renderer);//将场景renderer加入renWin窗口中
//旋转5圈
for (int i=0; i<360*5; i++)
{
//绘制舞台上的东西
renWin->Render();
//让场景中照相机旋转1度
renderer->GetActiveCamera()->Azimuth(1);
}
//释放内存
cone->Delete();
coneActor->Delete();
coneMapper->Delete();
renderer->Delete();
renWin->Delete();
return 0;
}
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include //源数据
#include //数据映射
#include //绘制器
#include //绘制窗口
#include //演员
#include //照相机
#include //加入交互机制类
int main()
{
//指针创建一个圆锥cone
vtkConeSource *cone=vtkConeSource::New();
cone->SetResolution (5);//圆锥的棱边数
cone->SetHeight(3);//指定高度
cone->SetRadius(1);//指定半径
//实例化一个映射器coneMapper
vtkPolyDataMapper *coneMapper=vtkPolyDataMapper::New();
coneMapper->SetInputConnection(cone->GetOutputPort());//源数据输出给映射器输入
//创建一个演员对象coneActor
vtkActor *coneActor=vtkActor::New();
coneActor->SetMapper(coneMapper);//为演员指定mapper进行映射
//搭建舞台 实例化对象renderer
vtkRenderer *renderer=vtkRenderer::New();
renderer->AddActor(coneActor);//将演员加入场景
renderer->SetBackground(0.1,0.2,0.4);//设置场景背景颜色是(R,G,B)
//实例化一个窗口对象renWin
vtkRenderWindow *renWin=vtkRenderWindow::New();
renWin->SetSize(400,200);//设置窗口大小宽*高
renWin->AddRenderer(renderer);//将场景renderer加入renWin窗口中
//实例化一个交互对象
vtkRenderWindowInteractor* interactor = vtkRenderWindowInteractor::New();//交互对象加入renWin窗口中
interactor->SetRenderWindow(renWin);
renWin->Render();//绘制舞台上的东西
interactor->Initialize();//交互对象初始化
interactor->Start();//开始交互事件
//释放内存
cone->Delete();
coneActor->Delete();
coneMapper->Delete();
renderer->Delete();
renWin->Delete();
interactor->Delete();
return 0;
}
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include //源数据
#include //数据映射
#include //绘制器
#include //绘制窗口
#include //演员
#include //照相机
#include //加入交互机制类
#include //一种交互方式,操纵相机
#include //另一种交互方式,操纵演员
int main()
{
//指针创建一个圆锥cone
vtkConeSource* cone = vtkConeSource::New();
cone->SetResolution(5);//圆锥的棱边数
cone->SetHeight(3);//指定高度
cone->SetRadius(1);//指定半径
//实例化一个映射器coneMapper
vtkPolyDataMapper* coneMapper = vtkPolyDataMapper::New();
coneMapper->SetInputConnection(cone->GetOutputPort());//源数据输出给映射器输入
//创建一个演员对象coneActor
vtkActor* coneActor = vtkActor::New();
coneActor->SetMapper(coneMapper);//为演员指定mapper进行映射
//搭建舞台 实例化对象renderer
vtkRenderer* renderer = vtkRenderer::New();
renderer->AddActor(coneActor);//将演员加入场景
renderer->SetBackground(0.1, 0.2, 0.4);//设置场景背景颜色是(R,G,B)
//实例化一个窗口对象renWin
vtkRenderWindow* renWin = vtkRenderWindow::New();
renWin->SetSize(400, 200);//设置窗口大小宽*高
renWin->AddRenderer(renderer);//将场景renderer加入renWin窗口中
//实例化一个交互对象
vtkRenderWindowInteractor* interactor = vtkRenderWindowInteractor::New();//交互对象加入renWin窗口中
interactor->SetRenderWindow(renWin);
操纵相机的一种交互方式
//vtkInteractorStyleTrackballCamera* style =
// vtkInteractorStyleTrackballCamera::New();
//interactor->SetInteractorStyle(style);
//操纵演员的一种交互方式
vtkInteractorStyleTrackballActor* style =
vtkInteractorStyleTrackballActor::New();
interactor->SetInteractorStyle(style);
renWin->Render();//绘制舞台上的东西
interactor->Initialize();//交互对象初始化
interactor->Start();//开始交互事件
//释放内存
cone->Delete();
coneActor->Delete();
coneMapper->Delete();
renderer->Delete();
renWin->Delete();
interactor->Delete();
style->Delete();
return 0;
}
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include //源数据
#include //数据映射
#include //绘制器
#include //绘制窗口
#include //演员
#include //照相机
#include //加入交互机制类
#include
int main()
{
//指针创建一个圆锥cone
vtkConeSource* cone = vtkConeSource::New();
cone->SetResolution(5);//圆锥的棱边数
cone->SetHeight(3);//指定高度
cone->SetRadius(1);//指定半径
//实例化一个映射器coneMapper
vtkPolyDataMapper* coneMapper = vtkPolyDataMapper::New();
coneMapper->SetInputConnection(cone->GetOutputPort());//源数据输出给映射器输入
//创建一个演员对象coneActor
vtkActor* coneActor = vtkActor::New();
coneActor->SetMapper(coneMapper);//为演员指定mapper进行映射
//方法1:设置演员颜色
coneActor->GetProperty()->SetColor(1.0,0.0,0.0);//设置对象颜色为红色
//方法2:设置演员颜色
//vtkProperty* coneProperty = vtkProperty::New();//设置一个Property对象
//coneProperty->SetColor(1.0, 0.0, 0.0);
//coneActor->SetProperty(coneProperty);
//搭建舞台 实例化对象renderer
vtkRenderer* renderer = vtkRenderer::New();
renderer->AddActor(coneActor);//将演员加入场景
renderer->SetBackground(0.0, 1.0, 0.0);//设置场景背景颜色是绿色(R,G,B)
//实例化一个窗口对象renWin
vtkRenderWindow* renWin = vtkRenderWindow::New();
renWin->SetSize(400, 200);//设置窗口大小宽*高
renWin->AddRenderer(renderer);//将场景renderer加入renWin窗口中
//实例化一个交互对象
vtkRenderWindowInteractor* interactor = vtkRenderWindowInteractor::New();//交互对象加入renWin窗口中
interactor->SetRenderWindow(renWin);
renWin->Render();//绘制舞台上的东西
interactor->Initialize();//交互对象初始化
interactor->Start();//开始交互事件
//释放内存
cone->Delete();
coneActor->Delete();
coneMapper->Delete();
renderer->Delete();
renWin->Delete();
interactor->Delete();
return 0;
}
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include //源数据
#include //数据映射
#include //绘制器
#include //绘制窗口
#include //演员
#include //照相机
#include //加入交互机制类
#include //属性
#include //光照类
int main()
{
//指针创建一个圆锥cone
vtkConeSource* cone = vtkConeSource::New();
cone->SetResolution(5);//圆锥的棱边数
cone->SetHeight(3);//指定高度
cone->SetRadius(1);//指定半径
//实例化一个映射器coneMapper
vtkPolyDataMapper* coneMapper = vtkPolyDataMapper::New();
coneMapper->SetInputConnection(cone->GetOutputPort());//源数据输出给映射器输入
//创建一个演员对象coneActor
vtkActor* coneActor = vtkActor::New();
coneActor->SetMapper(coneMapper);//为演员指定mapper进行映射
//coneActor->GetProperty()->SetColor(0.0, 0.0, 1.0);//设置对象颜色为蓝色
//搭建舞台 实例化渲染器对象renderer
vtkRenderer* renderer = vtkRenderer::New();
renderer->AddActor(coneActor);//将演员加入场景
renderer->SetBackground(0.0, 0.0, 1.0);//设置场景背景颜色为蓝色(R,G,B)
vtkLight* light = vtkLight::New();//实例化一个灯光对象
//light->SetSpecularColor(1.0, 0.0, 0.0);//设置镜面反射光颜色为Red
//light->SetDiffuseColor(0.0, 1.0, 0.0);//设置散射光为Green
light->SetColor(1.0,0.0,0.0);//设置环境光为红色
renderer->AddLight(light);//将灯光加入渲染器
//实例化一个窗口对象renWin
vtkRenderWindow* renWin = vtkRenderWindow::New();
renWin->SetSize(400, 200);//设置窗口大小宽*高
renWin->AddRenderer(renderer);//将场景renderer加入renWin窗口中
//实例化一个交互对象
vtkRenderWindowInteractor* interactor = vtkRenderWindowInteractor::New();//交互对象加入renWin窗口中
interactor->SetRenderWindow(renWin);
renWin->Render();//绘制舞台上的东西
interactor->Initialize();//交互对象初始化
interactor->Start();//开始交互事件
//释放内存
cone->Delete();
coneActor->Delete();
coneMapper->Delete();
renderer->Delete();
renWin->Delete();
interactor->Delete();
light->Delete();
return 0;
}
未加入相机控制时演员位置:
加入相机控制时演员位置(视角为x轴负方向):
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include //源数据
#include //数据映射
#include //绘制器
#include //绘制窗口
#include //演员
#include //照相机
#include //加入交互机制类
#include //属性
#include //光照类
int main()
{
//指针创建一个圆锥cone
vtkConeSource* cone = vtkConeSource::New();
cone->SetResolution(5);//圆锥的棱边数
cone->SetHeight(3);//指定高度
cone->SetRadius(1);//指定半径
//实例化一个映射器coneMapper
vtkPolyDataMapper* coneMapper = vtkPolyDataMapper::New();
coneMapper->SetInputConnection(cone->GetOutputPort());//源数据输出给映射器输入
//创建一个演员对象coneActor
vtkActor* coneActor = vtkActor::New();
coneActor->SetMapper(coneMapper);//为演员指定mapper进行映射
coneActor->GetProperty()->SetColor(1.0, 1.0, 1.0);//设置对象颜色为白色
//搭建舞台 实例化渲染器对象renderer
vtkRenderer* renderer = vtkRenderer::New();
renderer->AddActor(coneActor);//将演员加入场景
renderer->SetBackground(0.0, 0.0, 1.0);//设置场景背景颜色为蓝色(R,G,B)
vtkLight* light = vtkLight::New();//实例化一个灯光对象
//light->SetSpecularColor(1.0, 0.0, 0.0);//设置镜面反射光颜色为Red
//light->SetDiffuseColor(0.0, 1.0, 0.0);//设置散射光为Green
light->SetColor(1.0, 0.0, 0.0);//设置环境光为红色
renderer->AddLight(light);//将灯光加入渲染器
//实例化一个照相机对象camera
//相对屏幕,x轴正方向:从左往右 y轴正方向:从下往上 z轴正方向:从里往外
//方法一:
vtkCamera *camera = vtkCamera::New();
camera->SetPosition(5.0,0.0,0.0);//照相机位置(x,y,z)
camera->SetFocalPoint(-1.0,0.0,0.0);//焦点方向
camera->SetViewUp(0.0,1.0,0.0);//线上向量
renderer->SetActiveCamera(camera);//设置我们创建的camera对象为活动的照相机
//方法二:
/*renderer->GetActiveCamera()->SetPosition(5.0, 0.0, 0.0);
renderer->GetActiveCamera()->SetFocalPoint(-1.0, 0.0, 0.0);
renderer->GetActiveCamera()->SetViewUp(0.0, 1.0, 0.0);*/
//实例化一个窗口对象renWin
vtkRenderWindow* renWin = vtkRenderWindow::New();
renWin->SetSize(400, 200);//设置窗口大小宽*高
renWin->AddRenderer(renderer);//将场景renderer加入renWin窗口中
//实例化一个交互对象
vtkRenderWindowInteractor* interactor = vtkRenderWindowInteractor::New();//交互对象加入renWin窗口中
interactor->SetRenderWindow(renWin);
renWin->Render();//绘制舞台上的东西
interactor->Initialize();//交互对象初始化
interactor->Start();//开始交互事件
//释放内存
cone->Delete();
coneActor->Delete();
coneMapper->Delete();
renderer->Delete();
renWin->Delete();
interactor->Delete();
light->Delete();
camera->Delete();
return 0;
}
相机绕着焦点旋转:
焦点绕着相机旋转:
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include
#include
#include
#include
#include
#include
#include
#include
#include
//自定义类vtkMyCallback,输出当前相机坐标(x,y,z)
class vtkMyCallback : public vtkCommand
{
public://公共成员
static vtkMyCallback *New() //静态重载New函数,提醒返回用New创建的类vtkMyCallback
{ return new vtkMyCallback; }
//重载command虚拟函数Execute
virtual void Execute(vtkObject *caller, unsigned long, void*)
{
//将caller转型为renderer对象
vtkRenderer *renderer = reinterpret_cast(caller);
//输出相机位置(x,y,z)
cout << renderer->GetActiveCamera()->GetPosition()[0] << " "
<< renderer->GetActiveCamera()->GetPosition()[1] << " "
<< renderer->GetActiveCamera()->GetPosition()[2] << "\n";
}
};
//自定义类vtkMyCallback2,设置演员的颜色为红色
class vtkMyCallback2 : public vtkCommand
{
public:
static vtkMyCallback2 *New()
{
return new vtkMyCallback2;
}
//重载command虚拟函数Execute
virtual void Execute(vtkObject *caller, unsigned long eventId, void *callData)
{
//将caller转型为actor对象
vtkActor *actor = reinterpret_cast(caller);
//设置演员的颜色为红色
actor->GetProperty()->SetColor(1.0,0.0,0.0);
}
};
int main()
{
//实例化一个椎体对象cone
vtkConeSource *cone=vtkConeSource::New();
cone->SetResolution (10);
cone->SetHeight(3);
cone->SetRadius(1);
vtkPolyDataMapper *coneMapper=vtkPolyDataMapper::New();
coneMapper->SetInputConnection(cone->GetOutputPort());
vtkActor *coneActor=vtkActor::New();
coneActor->SetMapper(coneMapper);
//实例化回调函数mycallback2
vtkMyCallback2 *mycallback2 = vtkMyCallback2::New();
//监听器Observer如果发现有拾取事件PickEvent发生,则调用mycallback2对象
coneActor->AddObserver(vtkCommand::PickEvent,mycallback2);
mycallback2->Delete();
vtkRenderer *renderer=vtkRenderer::New();
renderer->AddActor(coneActor);
renderer->SetBackground(0.1,0.2,0.4);
//实例化回调函数mycallback
vtkMyCallback *mo1 = vtkMyCallback::New();
//监听器Observer如果发现renderer有交互开始事件StartEvent发生,则调用mol对象
renderer->AddObserver(vtkCommand::StartEvent,mo1);
mo1->Delete();
vtkRenderWindow *renWin=vtkRenderWindow::New();
renWin->SetSize(400,400);
renWin->AddRenderer(renderer);
vtkRenderWindowInteractor *interactor = vtkRenderWindowInteractor::New();
interactor->SetRenderWindow(renWin);
renWin->Render();
interactor->Initialize();
interactor->Start();
cone->Delete();
coneActor->Delete();
coneMapper->Delete();
renderer->Delete();
renWin->Delete();
interactor->Delete();
return 0;
}
按下p键进行拾取
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include //源数据
#include //立方体
#include //数据映射
#include //绘制器
#include //绘制窗口
#include //照相机
#include //加入交互机制类
int main()
{
//指针创建一个锥体cone
vtkConeSource* cone = vtkConeSource::New();
cone->SetResolution(5);//圆锥的棱边数
cone->SetHeight(3);//指定高度
cone->SetRadius(1);//指定半径
//指针创建一个柱体
vtkCubeSource* cube = vtkCubeSource::New();
//实例化一个映射器coneMapper
vtkPolyDataMapper* coneMapper = vtkPolyDataMapper::New();
coneMapper->SetInputConnection(cone->GetOutputPort());//源数据输出给映射器输入
//实例化一个映射器cubeMapper
vtkPolyDataMapper* cubeMapper = vtkPolyDataMapper::New();
cubeMapper->SetInputConnection(cube->GetOutputPort());//源数据输出给映射器输入
//创建一个演员对象coneActor
vtkActor* coneActor = vtkActor::New();
coneActor->SetMapper(coneMapper);//为演员指定mapper进行映射
//创建一个演员对象cubeActor
vtkActor* cubeActor = vtkActor::New();
cubeActor->SetMapper(cubeMapper);//为演员指定mapper进行映射
//搭建舞台 实例化渲染器对象renderer
vtkRenderer* renderer = vtkRenderer::New();
renderer->AddActor(coneActor);//将演员加入场景
renderer->SetBackground(0.0, 0.0, 1.0);//设置场景背景颜色为蓝色(R,G,B)
//在绘制窗口指定视口(Xmin,Ymin,Xmax,Ymax)(x,y范围都为0~1)
renderer->SetViewport(0.0, 0.0, 0.5, 1.0);//左边视口
//搭建舞台 实例化渲染器对象renderer2
vtkRenderer* renderer2 = vtkRenderer::New();
renderer2->AddActor(cubeActor);//将演员加入场景
renderer2->SetBackground(1.0, 0.0, 0.0);//设置场景背景颜色为红色(R,G,B)
renderer2->SetViewport(0.5, 0.0, 1.0, 1.0);//右边视口
//实例化一个窗口对象renWin
vtkRenderWindow* renWin = vtkRenderWindow::New();
renWin->SetSize(400, 200);//设置窗口大小宽*高
renWin->AddRenderer(renderer);//将场景renderer加入renWin窗口中
renWin->AddRenderer(renderer2);//将场景renderer2加入renWin窗口中
//实例化一个交互对象
vtkRenderWindowInteractor* interactor = vtkRenderWindowInteractor::New();//交互对象加入renWin窗口中
interactor->SetRenderWindow(renWin);
renWin->Render();//绘制舞台上的东西
interactor->Initialize();//交互对象初始化
interactor->Start();//开始交互事件
cone->Delete();
cube->Delete();
coneActor->Delete();
cubeActor->Delete();
coneMapper->Delete();
cubeMapper->Delete();
renderer->Delete();
renderer2->Delete();
renWin->Delete();
interactor->Delete();
return 0;
}
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include //源数据
#include //立方体
#include //数据映射
#include //绘制器
#include //绘制窗口
#include //照相机
#include //加入交互机制类
int main()
{
//指针创建一个锥体cone
vtkConeSource* cone = vtkConeSource::New();
cone->SetResolution(5);//圆锥的棱边数
cone->SetHeight(3);//指定高度
cone->SetRadius(1);//指定半径
//指针创建一个柱体
vtkCubeSource* cube = vtkCubeSource::New();
//实例化一个映射器coneMapper
vtkPolyDataMapper* coneMapper = vtkPolyDataMapper::New();
coneMapper->SetInputConnection(cone->GetOutputPort());//源数据输出给映射器输入
//实例化一个映射器cubeMapper
vtkPolyDataMapper* cubeMapper = vtkPolyDataMapper::New();
cubeMapper->SetInputConnection(cube->GetOutputPort());//源数据输出给映射器输入
//创建一个演员对象coneActor
vtkActor* coneActor = vtkActor::New();
coneActor->SetMapper(coneMapper);//为演员指定mapper进行映射
//创建一个演员对象cubeActor
vtkActor* cubeActor = vtkActor::New();
cubeActor->SetMapper(cubeMapper);//为演员指定mapper进行映射
//搭建舞台 实例化渲染器对象renderer
vtkRenderer* renderer = vtkRenderer::New();
renderer->AddActor(coneActor);//将演员加入场景
renderer->SetBackground(1.0, 0.0, 0.0);//设置场景背景颜色为红色(R,G,B)
//在绘制窗口指定视口(Xmin,Ymin,Xmax,Ymax)(x,y范围都为0~1)
renderer->SetViewport(0.0, 0.0, 0.5, 0.5);//视口1
//搭建舞台 实例化渲染器对象renderer2
vtkRenderer* renderer2 = vtkRenderer::New();
renderer2->AddActor(cubeActor);//将演员加入场景
renderer2->SetBackground(0.0, 1.0, 0.0);//设置场景背景颜色为绿色(R,G,B)
renderer2->SetViewport(0.5, 0.0, 1.0, 0.5);//视口2
vtkRenderer* renderer3 = vtkRenderer::New();
renderer3->AddActor(cubeActor);
renderer3->SetBackground(0.0, 0.0, 1.0);//蓝色
renderer3->SetViewport(0.0, 0.5, 0.5, 1.0);//视口3
vtkRenderer* renderer4 = vtkRenderer::New();
renderer4->AddActor(coneActor);
renderer4->SetBackground(1.0, 1.0, 0.0);//黄色
renderer4->SetViewport(0.5, 0.5, 1.0, 1.0);//视口4
//实例化一个窗口对象renWin
vtkRenderWindow* renWin = vtkRenderWindow::New();
renWin->SetSize(640, 480);//设置窗口大小宽*高
renWin->AddRenderer(renderer);//将场景renderer加入renWin窗口中
renWin->AddRenderer(renderer2);//将场景renderer2加入renWin窗口中
renWin->AddRenderer(renderer3);
renWin->AddRenderer(renderer4);
//实例化一个交互对象
vtkRenderWindowInteractor* interactor = vtkRenderWindowInteractor::New();//交互对象加入renWin窗口中
interactor->SetRenderWindow(renWin);
renWin->Render();//绘制舞台上的东西
interactor->Initialize();//交互对象初始化
interactor->Start();//开始交互事件
cone->Delete();
cube->Delete();
coneActor->Delete();
cubeActor->Delete();
coneMapper->Delete();
cubeMapper->Delete();
renderer->Delete();
renderer2->Delete();
renderer3->Delete();
renderer4->Delete();
renWin->Delete();
interactor->Delete();
return 0;
}
窗宽(Window Width):表示所显示信号强度值的范围(CT图像上所包含的CT值范围)
窗位(Window Level):又称窗水平。窗位是指窗宽上下限的平均数(窗位的高低影响图像的亮度)
例如:窗宽为300H,当窗位是0H时,其CT值范围为-150H~0H~+150H;如窗位是+40H时,则CT值范围为
-110H~+40H~+190H。通常欲观察某一组织的结构及发生的病变,应以该组织的CT值为窗位。
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include //BMP读取类
#include //显示2D图像类
#include //交互器类
int main()
{
//实例化一个对象bmpReader
vtkBMPReader* bmpReader = vtkBMPReader::New();
bmpReader->SetFileName("1234.bmp");//读取图片
//实例化一个对象bmpViewer
vtkImageViewer* bmpViewer = vtkImageViewer::New();
bmpViewer->SetInputConnection(bmpReader->GetOutputPort());//读取的图像数据输出给显示对象输入
bmpViewer->SetColorLevel(128);//设置窗位
bmpViewer->SetColorWindow(256);//设置窗宽
bmpViewer->Render();//显示图像
//加入交互机制
//实例化一个交互器对象interactor
vtkRenderWindowInteractor* interactor = vtkRenderWindowInteractor::New();
bmpViewer->SetupInteractor(interactor);
interactor->Initialize();
interactor->Start();//使交互器处于等待状态
//getchar();
//释放内存
bmpReader->Delete();
bmpViewer->Delete();
interactor->Delete();
return 0;
}
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include //PNG读取类
#include //显示2D图像类
#include //交互器类
int main()
{
//实例化一个对象pngReader
vtkPNGReader* pngReader = vtkPNGReader::New();
pngReader->SetFileName("wode.png");//读取图片
//实例化一个对象pngViewer
vtkImageViewer* bmpViewer = vtkImageViewer::New();
bmpViewer->SetInputConnection(pngReader->GetOutputPort());//读取的图像数据输出给显示对象输入
bmpViewer->SetColorLevel(128);//设置窗位
bmpViewer->SetColorWindow(256);//设置窗宽
bmpViewer->Render();//显示图像
//加入交互机制
//实例化一个交互器对象interactor
vtkRenderWindowInteractor* interactor = vtkRenderWindowInteractor::New();
bmpViewer->SetupInteractor(interactor);
interactor->Initialize();
interactor->Start();//使交互器处于等待状态
//getchar();
//释放内存
pngReader->Delete();
bmpViewer->Delete();
interactor->Delete();
return 0;
}
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include //JPG读取类
#include //显示2D图像类
#include //交互器类
int main()
{
//实例化一个对象jpegReader
vtkJPEGReader* jpegReader = vtkJPEGReader::New();
jpegReader->SetFileName("789.jpg");//读取图片
//实例化一个对象jpegViewer
vtkImageViewer* jpegViewer = vtkImageViewer::New();
jpegViewer->SetInputConnection(jpegReader->GetOutputPort());//读取的图像数据输出给显示对象输入
jpegViewer->SetColorLevel(128);//设置窗位
jpegViewer->SetColorWindow(256);//设置窗宽
jpegViewer->Render();//显示图像
//加入交互机制
//实例化一个交互器对象interactor
vtkRenderWindowInteractor* interactor = vtkRenderWindowInteractor::New();
jpegViewer->SetupInteractor(interactor);
interactor->Initialize();
interactor->Start();//使交互器处于等待状态
//getchar();
//释放内存
jpegReader->Delete();
jpegViewer->Delete();
interactor->Delete();
return 0;
}
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include //DCM医学文件读取类
#include //显示2D图像类
#include //交互器类
int main()
{
int dim[2];
//实例化一个对象dcmReader
vtkDICOMImageReader* dcmReader = vtkDICOMImageReader::New();
dcmReader->SetFileName("C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\CT\\123.dcm");//读单张切片
//reader->SetDirectoryName("C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\CT_all");//读序列切片
//实例化一个对象dcmViewer
vtkImageViewer* dcmViewer = vtkImageViewer::New();
dcmViewer->SetInputConnection(dcmReader->GetOutputPort());//读取的图像数据输出给显示对象输入
//dcmViewer->SetColorLevel(400);//设置窗位
//dcmViewer->SetColorWindow(800);//设置窗宽
dim[0] = dcmViewer->GetColorLevel();//显示窗位1000
dim[1] = dcmViewer->GetColorWindow();//显示窗宽2000
printf("%d %d", dim[0], dim[1]);
dcmViewer->Render();//显示图像
//加入交互机制
//实例化一个交互器对象interactor
vtkRenderWindowInteractor* interactor = vtkRenderWindowInteractor::New();
dcmViewer->SetupInteractor(interactor);
interactor->Initialize();
interactor->Start();//使交互器处于等待状态
/*getchar();*/
//释放内存
dcmReader->Delete();
dcmViewer->Delete();
interactor->Delete();
return 0;
}
鼠标左键按住不放,上下移动调整窗位,左右移动调整窗宽
默认窗位1000,窗宽2000
// ±?ày×ó?éó?óú?áè?μ¥·ùCTí???2¢??ê??àó|DICOM???t?Dí·D??¢?£
// First include the required header files for the VTK classes we are using.
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include "vtkDICOMImageReader.h"
#include "vtkBMPReader.h"
#include "vtkVolume16Reader.h"
#include "vtkImageViewer.h"
#include "vtkRenderWindowInteractor.h"
//??2¨oˉêy
#include "vtkImageMagnify.h"
#include "vtkTransform.h"
int main()
{
vtkBMPReader *dr = vtkBMPReader::New();
dr->SetDataByteOrderToLittleEndian();
dr->SetFileName("1234.bmp");
dr->SetDataOrigin(0,0,0.0);
vtkTransform *t1=vtkTransform::New();
t1->RotateZ(0);
dr->SetTransform(t1);
//=================================================
//??2¨2ù×÷
vtkImageMagnify *mfy=vtkImageMagnify::New();
//í???·?′ó,2??áó?
mfy->SetInputConnection(dr->GetOutputPort());
mfy->SetMagnificationFactors(3,2,2);//???ü·?′ó??êy±?,??DCMí????TD§
//=======================================================
vtkImageViewer *viewer = vtkImageViewer::New();
viewer->SetInputConnection(mfy->GetOutputPort());
viewer->SetColorWindow(1000);
viewer->SetColorLevel(200);
viewer->SetPosition(0,0);
viewer->Render();
vtkRenderWindowInteractor *viewerinter = vtkRenderWindowInteractor::New();
viewer->SetupInteractor(viewerinter);
vtkImageViewer *viewer2 = vtkImageViewer::New();
viewer2->SetInputConnection(dr->GetOutputPort());
viewer2->SetColorWindow(256);
viewer2->SetColorLevel(200);
viewer2->SetPosition(0,100);
viewer2->Render();
vtkRenderWindowInteractor *viewerinter2 = vtkRenderWindowInteractor::New();
viewer2->SetupInteractor(viewerinter2);
viewerinter->Initialize();
viewerinter->Start();
// coutDelete();
viewer->Delete();
viewerinter->Delete();
viewer2->Delete();
viewerinter2->Delete();
return 0;
}
// This example shows how to manually create vtkPolyData.
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include "vtkActor.h"
#include "vtkCamera.h"
#include "vtkCellArray.h" //基元数组类
#include "vtkFloatArray.h" //浮点型数组类
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h" //多边形数据类型类
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
int main()
{
int i;
//立方体的八个顶点(每个顶点坐标由三个点组成)
static float x[8][3]={{0,0,0}, {1,0,0}, {1,1,0}, {0,1,0},
{0,0,1}, {1,0,1}, {1,1,1}, {0,1,1}};
//立方体的六个面(每个面由四个点的标号组成)
static vtkIdType pts[6][4]={{0,1,2,3}, {4,5,6,7}, {0,1,5,4},
{1,2,6,5}, {2,3,7,6}, {3,0,4,7}};
// We'll create the building blocks of polydata including data attributes.
//实例化一个多边形数据对象cube
vtkPolyData *cube = vtkPolyData::New();
//实例化一个点对象points
vtkPoints *points = vtkPoints::New();
//单元对象
vtkCellArray *polys = vtkCellArray::New();
//浮点数组对象
vtkFloatArray *scalars = vtkFloatArray::New();
// Load the point, cell, and data attributes.
for (i=0; i<8; i++) points->InsertPoint(i,x[i]);//加载立方体的8个顶点
for (i=0; i<6; i++) polys->InsertNextCell(4,pts[i]);//加载每个面由4个点组成的六个面
for (i=0; i<8; i++) scalars->InsertTuple1(i,i);//绑定标量数据
// We now assign the pieces to the vtkPolyData.
//与vtkPolyData型数据对象进行关联
cube->SetPoints(points);//进行点关联
points->Delete();
cube->SetPolys(polys);//进行面关联
polys->Delete();
cube->GetPointData()->SetScalars(scalars);//进行元组绑定
scalars->Delete();
// Now we'll look at it.
//多边形数据送入图像界面进行绘制
vtkPolyDataMapper *cubeMapper = vtkPolyDataMapper::New();
cubeMapper->SetInputData(cube);//vtkPolyData数据输出给映射器输入
cubeMapper->SetScalarRange(0,7);//设置标量数据范围0~7
//实例化一个演员cubeActor
vtkActor *cubeActor = vtkActor::New();
cubeActor->SetMapper(cubeMapper);
vtkCamera* camera = vtkCamera::New();
camera->SetPosition(1, 1, 1);//设置相机位置为(1,1,1)
camera->SetFocalPoint(0, 0, 0);
vtkRenderer* renderer = vtkRenderer::New();
renderer->AddActor(cubeActor);
renderer->SetActiveCamera(camera);
renderer->ResetCamera();
renderer->SetBackground(1, 1, 1);
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer(renderer);
renWin->SetSize(300, 300);
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
// interact with data
renWin->Render();//绘制舞台上的东西
iren->Start();//开始交互
// Clean up
cube->Delete();
cubeMapper->Delete();
cubeActor->Delete();
camera->Delete();
renderer->Delete();
renWin->Delete();
iren->Delete();
return 0;
}
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkImageData.h"
#include "vtkFloatArray.h"
#include "vtkContourFilter.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkPointData.h"
int main ()
{
int i, j, k, kOffset, jOffset, offset;
float x, y, z, s, sp;
vtkRenderer *renderer = vtkRenderer::New();
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer(renderer);
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
//实例化一个vtkImageData数据对象vol
vtkImageData *vol = vtkImageData::New();
vol->SetDimensions(26,26,26);//设置各个方向坐标轴的大小
vol->SetOrigin(-0.5,-0.5,-0.5);//定义原点
sp = 1.0/25.0;
vol->SetSpacing(sp, sp, sp);
//绑定一个属性数据
vtkFloatArray *scalars = vtkFloatArray::New();
for (k=0; k<26; k++)
{
z = -0.5 + k*sp;
kOffset = k * 26 * 26;
for (j=0; j<26; j++)
{
y = -0.5 + j*sp;
jOffset = j * 26;
for (i=0; i<26; i++)
{
x = -0.5 + i*sp;
s = x*x + y*y + z*z - (0.4*0.4);
offset = i + jOffset + kOffset;
scalars->InsertTuple1(offset,s);
}
}
}
vol->GetPointData()->SetScalars(scalars);
scalars->Delete();
//求取等式面、等高线等
//vtkSmartPointer contour = vtkSmartPointer::New();
vtkContourFilter *contour = vtkContourFilter::New();
contour->SetInputData(vol);
contour->SetValue(0,0.0);//设置等值
contour->Update();
vtkPolyDataMapper *volMapper = vtkPolyDataMapper::New();
volMapper->SetInputData(contour->GetOutput());
volMapper->ScalarVisibilityOff();
vtkActor *volActor = vtkActor::New();
volActor->SetMapper(volMapper);
renderer->AddActor(volActor);
renderer->SetBackground(1,1,1);
renWin->SetSize(450,450);
// interact with data
renWin->Render();
iren->Start();
// Clean up
renderer->Delete();
renWin->Delete();
iren->Delete();
vol->Delete();
scalars->Delete();
contour->Delete();
volMapper->Delete();
volActor->Delete();
return 0;
}
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
//定义一个vtkCylinderSource类型的对象cylinder
//vtkCylinderSource主要是生成一个中心在渲染场景原点的柱体,类型为(vtkPolyData)
vtkSmartPointer cylinder =
vtkSmartPointer::New();
cylinder->SetHeight( 3.0 );//设置柱体的高
cylinder->SetRadius( 1.0 );//设置柱体横截面半径
cylinder->SetResolution( 10 ); //设置柱体横截面的等边多边形的边数
//将输入的数据转换为几何图元(点、线、多边形)进行渲染
vtkSmartPointer cylinderMapper =
vtkSmartPointer::New();
cylinderMapper->SetInputConnection( cylinder->GetOutputPort() );
vtkSmartPointer cylinderActor =
vtkSmartPointer::New();
//设置生成几何图元的Mapper,即连接一个Actor到可视化管线的末端
cylinderActor->SetMapper( cylinderMapper );
cylinderActor->GetProperty()->SetColor(1.0, 0.0, 0.0);
vtkSmartPointer renderer =
vtkSmartPointer::New();
renderer->AddActor( cylinderActor );
renderer->SetBackground( 1.0, 1.0, 1.0 );
//将操作系统与VTK渲染引擎连接到一起
vtkSmartPointer renWin =
vtkSmartPointer::New();
renWin->AddRenderer( renderer );//加入vtkRenderer对象
renWin->SetSize( 640, 480 );//设置窗口大小,像素为单位
renWin->Render();//场景渲染
renWin->SetWindowName("RenderCylinder");
//提供平台独立的响应鼠标、键盘和时钟事件的交互机制
vtkSmartPointer iren =
vtkSmartPointer::New();
iren->SetRenderWindow(renWin);
//交互器样式的一种,可通过控制相机对物体作旋转、放大、缩小等操作
vtkSmartPointer style =
vtkSmartPointer::New();
iren->SetInteractorStyle(style);
iren->Initialize();
iren->Start();
return EXIT_SUCCESS;
}