VTK资料分享:张晓东的《VTK图形图像开发进阶》全套资料(包含书籍电子版,配套每章节所有源码、实现效果图和工程配置文件):https://download.csdn.net/download/qq_32809093/12550945
水灵VTK学习全套资料(包含全部视频、PPT、源码和每个项目对应的工程配置文件CmakeLists.txt):
https://download.csdn.net/download/qq_32809093/12550933
水灵VT视频在线观看:https://www.ixigua.com/6834676787503432203
本文参考的主要是张晓东的《VTK图形图像开发进阶》和水灵的视频Study VTK Together。使用的平台是VS2019+VTK8.2
#include
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2); //错误:no override found for 'vtkRayCastImageDisplayHelper'.
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkInteractionStyle);
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char* argv[])
{
vtkSmartPointer reader =
vtkSmartPointer::New();
reader->SetFileName("C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\vtk_图像处理学习\\第七章_VTK体绘制\\data\\mummy.128.vtk");
reader->Update();
vtkSmartPointer volumeMapper =
vtkSmartPointer::New();
volumeMapper->SetInputData(reader->GetOutput());
//设置光线采样距离
//volumeMapper->SetSampleDistance(volumeMapper->GetSampleDistance()*4);
//设置图像采样步长
//volumeMapper->SetAutoAdjustSampleDistances(0);
//volumeMapper->SetImageSampleDistance(4);
/*************************************************************************/
vtkSmartPointer volumeProperty =
vtkSmartPointer::New();
volumeProperty->SetInterpolationTypeToLinear();
volumeProperty->ShadeOn(); //打开或者关闭阴影测试
volumeProperty->SetAmbient(0.4);
volumeProperty->SetDiffuse(0.6); //漫反射
volumeProperty->SetSpecular(0.2); //镜面反射
//设置不透明度
vtkSmartPointer compositeOpacity =
vtkSmartPointer::New();
compositeOpacity->AddPoint(70, 0.00);
compositeOpacity->AddPoint(90, 0.40);
compositeOpacity->AddPoint(180, 0.60);
volumeProperty->SetScalarOpacity(compositeOpacity); //设置不透明度传输函数
//compositeOpacity->AddPoint(120, 0.00);//测试隐藏部分数据,对比不同的设置
//compositeOpacity->AddPoint(180, 0.60);
//volumeProperty->SetScalarOpacity(compositeOpacity);
//设置梯度不透明属性
vtkSmartPointer volumeGradientOpacity =
vtkSmartPointer::New();
volumeGradientOpacity->AddPoint(10, 0.0);
volumeGradientOpacity->AddPoint(90, 0.5);
volumeGradientOpacity->AddPoint(100, 1.0);
volumeProperty->SetGradientOpacity(volumeGradientOpacity);//设置梯度不透明度效果对比
//设置颜色属性
vtkSmartPointer color =
vtkSmartPointer::New();
color->AddRGBPoint(0.000, 0.00, 0.00, 0.00);
color->AddRGBPoint(64.00, 1.00, 0.52, 0.30);
color->AddRGBPoint(190.0, 1.00, 1.00, 1.00);
color->AddRGBPoint(220.0, 0.20, 0.20, 0.20);
volumeProperty->SetColor(color);
/********************************************************************************/
vtkSmartPointer volume =
vtkSmartPointer::New();
volume->SetMapper(volumeMapper);
volume->SetProperty(volumeProperty);
vtkSmartPointer ren = vtkSmartPointer::New();
ren->SetBackground(0, 1, 0);
ren->AddVolume(volume);
vtkSmartPointer rw = vtkSmartPointer::New();
rw->AddRenderer(ren);
rw->SetSize(640, 480);
rw->Render();
rw->SetWindowName("VolumeRendering PipeLine");
vtkSmartPointer rwi =
vtkSmartPointer::New();
rwi->SetRenderWindow(rw);
/********************************************************************************/
//vtkSmartPointer axes = vtkSmartPointer::New();
//axes->SetScale(10);
//vtkSmartPointer widget =
// vtkSmartPointer::New();
//widget->SetOutlineColor(1, 1, 1);
//widget->SetViewport(0, 0, 0.2, 0.2);
//widget->SetOrientationMarker(axes);
//widget->SetInteractor(rwi);
//widget->SetEnabled(1);
//widget->InteractiveOn();
ren->ResetCamera();
rw->Render();
rwi->Start();
return 0;
}
#include
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2); //错误:no override found for 'vtkRayCastImageDisplayHelper'.
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkInteractionStyle);
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//测试:../data/mummy.128.vtk
int main(int argc, char *argv[])
{
vtkSmartPointer reader =
vtkSmartPointer::New();
reader->SetFileName("C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\vtk_图像处理学习\\第七章_VTK体绘制\\data\\mummy.128.vtk");
reader->Update();
vtkSmartPointer rayCastFun =
vtkSmartPointer::New();
vtkSmartPointer volumeMapper =
vtkSmartPointer::New();
volumeMapper->SetInputData(reader->GetOutput());
//volumeMapper->Set
//SetVolumeRayCastMapperFunction(rayCastFun);//必须设置,否则出错
vtkSmartPointer volumeProperty =
vtkSmartPointer::New();
volumeProperty->SetInterpolationTypeToLinear();
volumeProperty->ShadeOn();
volumeProperty->SetAmbient(0.4);
volumeProperty->SetDiffuse(0.6);
volumeProperty->SetSpecular(0.2);
vtkSmartPointer compositeOpacity =
vtkSmartPointer::New();
compositeOpacity->AddPoint(70, 0.00);
compositeOpacity->AddPoint(90, 0.40);
compositeOpacity->AddPoint(180, 0.60);
volumeProperty->SetScalarOpacity(compositeOpacity);
vtkSmartPointer color =
vtkSmartPointer::New();
color->AddRGBPoint(0.000, 0.00, 0.00, 0.00);
color->AddRGBPoint(64.00, 1.00, 0.52, 0.30);
color->AddRGBPoint(190.0, 1.00, 1.00, 1.00);
color->AddRGBPoint(220.0, 0.20, 0.20, 0.20);
volumeProperty->SetColor(color);
vtkSmartPointer volume =
vtkSmartPointer::New();
volume->SetMapper(volumeMapper);
volume->SetProperty(volumeProperty);
vtkSmartPointer ren = vtkSmartPointer::New();
ren->SetBackground(1.0, 1.0, 1.0);
ren->AddVolume( volume );
vtkSmartPointer renWin = vtkSmartPointer::New();
renWin->AddRenderer(ren);
renWin->SetSize(640, 480);
renWin->Render();
renWin->SetWindowName("MIPVolRenderingApp");
vtkSmartPointer iren =
vtkSmartPointer::New();
iren->SetRenderWindow(renWin);
ren->ResetCamera();
renWin->Render();
iren->Start();
return EXIT_SUCCESS;
}
#include
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkInteractionStyle);
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
int main(int argc, char* argv[])
{
vtkSmartPointer reader =
vtkSmartPointer::New();
reader->SetFileName("C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\vtk_图像处理学习\\第七章_VTK体绘制\\data\\mummy.128.vtk");
reader->Update();
vtkSmartPointer volumeMapper =
vtkSmartPointer::New();
volumeMapper->SetInputData(reader->GetOutput());;
volumeMapper->SetSampleDistance(volumeMapper->GetSampleDistance() / 2); //设置光线采样距离
//volumeMapper->SetAutoAdjustSampleDistances(0);//设置图像采样步长
//volumeMapper->SetImageSampleDistance(4);
/*************************************************************************/
vtkSmartPointer volumeProperty =
vtkSmartPointer::New();
volumeProperty->SetInterpolationTypeToLinear();
volumeProperty->ShadeOn(); //打开或者关闭阴影测试
volumeProperty->SetAmbient(0.4);
volumeProperty->SetDiffuse(0.6); //漫反射
volumeProperty->SetSpecular(0.2); //镜面反射
//设置不透明度
vtkSmartPointer compositeOpacity =
vtkSmartPointer::New();
compositeOpacity->AddPoint(70, 0.00);
compositeOpacity->AddPoint(90, 0.40);
compositeOpacity->AddPoint(180, 0.60);
volumeProperty->SetScalarOpacity(compositeOpacity); //设置不透明度传输函数
//设置梯度不透明属性
vtkSmartPointer volumeGradientOpacity =
vtkSmartPointer::New();
volumeGradientOpacity->AddPoint(10, 0.0);
volumeGradientOpacity->AddPoint(90, 0.5);
volumeGradientOpacity->AddPoint(100, 1.0);
volumeProperty->SetGradientOpacity(volumeGradientOpacity);//设置梯度不透明度效果对比
//设置颜色属性
vtkSmartPointer color =
vtkSmartPointer::New();
color->AddRGBPoint(0.000, 0.00, 0.00, 0.00);
color->AddRGBPoint(64.00, 1.00, 0.52, 0.30);
color->AddRGBPoint(190.0, 1.00, 1.00, 1.00);
color->AddRGBPoint(220.0, 0.20, 0.20, 0.20);
volumeProperty->SetColor(color);
/********************************************************************************/
vtkSmartPointer volume =
vtkSmartPointer::New();
volume->SetMapper(volumeMapper);
volume->SetProperty(volumeProperty);
vtkSmartPointer ren = vtkSmartPointer::New();
ren->SetBackground(0, 1, 0);
ren->AddVolume(volume);
vtkSmartPointer rw = vtkSmartPointer::New();
rw->AddRenderer(ren);
rw->SetSize(640, 480);
rw->Render();
rw->SetWindowName("VolumeRendering");
vtkSmartPointer rwi =
vtkSmartPointer::New();
rwi->SetRenderWindow(rw);
ren->ResetCamera();
rw->Render();
rwi->Start();
return 0;
}
#include
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkInteractionStyle);
#include
#include //vtkStructuredPointsReader* -> vtkGPUVolumeRayCastMapper*
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
vtkSmartPointer reader =
vtkSmartPointer::New();
reader->SetFileName("C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\vtk_图像处理学习\\第七章_VTK体绘制\\data\\mummy.128.vtk");
reader->Update();
vtkSmartPointer origMapper =
vtkSmartPointer::New();
origMapper->SetInputData(reader->GetOutput());
vtkSmartPointer volumeMapper =
vtkSmartPointer::New();
volumeMapper->SetInputData(reader->GetOutput());
volumeMapper->SetCropping(1);//开启Cropping功能
volumeMapper->SetCroppingRegionPlanes(50, 150, 50, 200, 50, 150);
volumeMapper->SetCroppingRegionFlags(0x0002000);
/*****************************************************************/
//设置体绘制相关属性
vtkSmartPointer volumeProperty =
vtkSmartPointer::New();
volumeProperty->SetInterpolationTypeToLinear(); //设置线性插值方式
volumeProperty->ShadeOn();//开启阴影属性
volumeProperty->SetAmbient(0.4);//设置环境温度
volumeProperty->SetDiffuse(0.6);//设置漫反射系数
volumeProperty->SetSpecular(0.2);//设置镜面反射系数
//添加灰度不透明度属性
vtkSmartPointer compositeOpacity =
vtkSmartPointer::New();
compositeOpacity->AddPoint(70, 0.0);
compositeOpacity->AddPoint(90, 0.4);
compositeOpacity->AddPoint(180, 0.6);
volumeProperty->SetScalarOpacity(compositeOpacity);
//添加梯度不同明度属性
vtkSmartPointer gradientOpacity =
vtkSmartPointer::New();
gradientOpacity->AddPoint(10, 0.0);
gradientOpacity->AddPoint(90, 0.5);
gradientOpacity->AddPoint(100, 1.0);
volumeProperty->SetGradientOpacity(gradientOpacity);
//添加颜色传输
vtkSmartPointer color =
vtkSmartPointer::New();
color->AddRGBPoint(0, 0, 0, 0);
color->AddRGBPoint(64, 1.0, 0.52, 0.3);
color->AddRGBPoint(190.0, 1.00, 1.00, 1.00);
color->AddRGBPoint(220.0, 0.20, 0.20, 0.20);
volumeProperty->SetColor(color);
/***********************************************************/
//渲染管道
vtkSmartPointer origVolume =
vtkSmartPointer::New();
origVolume->SetMapper(origMapper);
origVolume->SetProperty(volumeProperty);
vtkSmartPointer croppingVolume =
vtkSmartPointer::New();
croppingVolume->SetMapper(volumeMapper);
croppingVolume->SetProperty(volumeProperty);
/
double origView[4] = { 0, 0, 0.5, 1 };
double croppingView[4] = { 0.5, 0, 1, 1 };
vtkSmartPointer origRender =
vtkSmartPointer::New();
origRender->AddVolume(origVolume);
origRender->SetBackground(1, 1, 0);
origRender->SetViewport(origView);
vtkSmartPointer croppingRender =
vtkSmartPointer::New();
croppingRender->AddVolume(croppingVolume);
croppingRender->SetBackground(0, 1, 0);
croppingRender->SetViewport(croppingView);
///
vtkSmartPointer rw =
vtkSmartPointer::New();
rw->AddRenderer(origRender);
rw->AddRenderer(croppingRender);
rw->SetWindowName("Cropping Volume");
rw->SetSize(640, 320);
vtkSmartPointer rwi =
vtkSmartPointer::New();
rwi->SetRenderWindow(rw);
origRender->GetActiveCamera()->SetPosition(0, -1, 0);
origRender->GetActiveCamera()->SetFocalPoint(0, 0, 0);
origRender->GetActiveCamera()->SetViewUp(0, 0, 1);
origRender->GetActiveCamera()->Azimuth(30);
origRender->GetActiveCamera()->Elevation(30);
origRender->ResetCamera();
croppingRender->SetActiveCamera(origRender->GetActiveCamera());
rw->Render();
rwi->Start();
return 0;
}
#include
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkInteractionStyle);
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
vtkSmartPointer reader =
vtkSmartPointer::New();
reader->SetFileName("C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\vtk_图像处理学习\\第七章_VTK体绘制\\data\\mummy.128.vtk");
reader->Update();
vtkSmartPointer volumeMapper1 =
vtkSmartPointer::New();
volumeMapper1->SetInputData(reader->GetOutput());
vtkSmartPointer volumeMapper2 =
vtkSmartPointer::New();
volumeMapper2->SetInputData(reader->GetOutput());
/****************************************************************/
vtkSmartPointer volumeProperty1 =
vtkSmartPointer::New();
volumeProperty1->SetInterpolationTypeToLinear(); //设置线性插值
volumeProperty1->ShadeOn();//开启阴影功能
volumeProperty1->SetAmbient(0.4);//设置环境温度系数
volumeProperty1->SetDiffuse(0.6);//设置漫反射系数
volumeProperty1->SetSpecular(0.2);//设置镜面反射系数
vtkSmartPointer compositeOpacity1 =
vtkSmartPointer::New();
compositeOpacity1->AddPoint(70, 0.0);
compositeOpacity1->AddPoint(90, 0.4);
compositeOpacity1->AddPoint(180, 0.6);
volumeProperty1->SetScalarOpacity(compositeOpacity1);
vtkSmartPointer volumeProperty2 =
vtkSmartPointer::New();
volumeProperty2->SetInterpolationTypeToLinear(); //设置线性插值
volumeProperty2->ShadeOn();//开启阴影功能
volumeProperty2->SetAmbient(0.4);//设置环境温度系数
volumeProperty2->SetDiffuse(0.6);//设置漫反射系数
volumeProperty2->SetSpecular(0.2);//设置镜面反射系数
vtkSmartPointer compositeOpacity2 =
vtkSmartPointer::New();
compositeOpacity2->AddPoint(100, 0.0);
compositeOpacity2->AddPoint(140, 0.4);
compositeOpacity2->AddPoint(180, 0.6);
volumeProperty2->SetScalarOpacity(compositeOpacity2);
/
vtkSmartPointer gradientOpacity =
vtkSmartPointer::New();
gradientOpacity->AddPoint(10, 0.0);
gradientOpacity->AddPoint(90, 0.5);
gradientOpacity->AddPoint(100, 1.0);
volumeProperty1->SetGradientOpacity(gradientOpacity);
volumeProperty2->SetGradientOpacity(gradientOpacity);
vtkSmartPointer color =
vtkSmartPointer::New();
color->AddRGBPoint(0, 0, 0, 0);
color->AddRGBPoint(64, 1.0, 0.52, 0.3);
color->AddRGBPoint(190.0, 1.00, 1.00, 1.00);
color->AddRGBPoint(220.0, 0.20, 0.20, 0.20);
volumeProperty1->SetColor(color);
volumeProperty2->SetColor(color);
/****************************************************************/
vtkSmartPointer volume1 =
vtkSmartPointer::New();
volume1->SetMapper(volumeMapper1);
volume1->SetProperty(volumeProperty1);
vtkSmartPointer volume2 =
vtkSmartPointer::New();
volume2->SetMapper(volumeMapper2);
volume2->SetProperty(volumeProperty2);
//
double View1[4] = { 0, 0, 0.5, 1 };
double View2[4] = { 0.5, 0, 1, 1 };
vtkSmartPointer render1 =
vtkSmartPointer::New();
render1->AddVolume(volume1);
render1->SetViewport(View1);
render1->SetBackground(1, 1, 0);
vtkSmartPointer render2 =
vtkSmartPointer::New();
render2->AddVolume(volume2);
render2->SetViewport(View2);
render2->SetBackground(0, 1, 0);
vtkSmartPointer rw =
vtkSmartPointer::New();
rw->AddRenderer(render1);
rw->AddRenderer(render2);
rw->SetSize(640, 320);
rw->SetWindowName("Differ Gray Opacity Function");
vtkSmartPointer rwi =
vtkSmartPointer::New();
rwi->SetRenderWindow(rw);
render1->GetActiveCamera()->SetPosition(0, -1, 0);
render1->GetActiveCamera()->SetFocalPoint(0, 0, 0);
render1->GetActiveCamera()->SetViewUp(0, 0, 1);
render1->GetActiveCamera()->Azimuth(30);
render1->GetActiveCamera()->Elevation(30);
render1->ResetCamera();
render2->SetActiveCamera(render1->GetActiveCamera());
rw->Render();
rwi->Start();
return 0;
}