#include <vtkSmartPointer.h>
#include <vtkMarchingCubes.h>
#include <vtkMetaImageReader.h>
#include <vtkSphereSource.h>
#include <vtkProbeFilter.h>
#include <vtkSphere.h>
#include <vtkClipDataSet.h>
#include <vtkImplicitVolume.h>
#include <vtkUnstructuredGrid.h>
#include <vtkLookupTable.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkDataSetMapper.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkProperty.h>
int main(int argc, char* argv[])
{
// Read the volume data
vtkSmartPointer<vtkMetaImageReader> reader =
vtkSmartPointer<vtkMetaImageReader>::New();
reader->SetFileName("E:\\Downloads\\brain\\brain.mhd");
reader->Update();
// 已知等值面或等值线值500对应于
// 病人的皮肤。
vtkSmartPointer<vtkMarchingCubes> skinExtractor =
vtkSmartPointer<vtkMarchingCubes>::New();
skinExtractor->SetInputConnection(reader->GetOutputPort());
skinExtractor->SetValue(0, 100);
// 定义球形剪裁函数以剪裁等高线
vtkSmartPointer<vtkSphere> clipFunction =
vtkSmartPointer<vtkSphere>::New();
clipFunction->SetRadius(50);
clipFunction->SetCenter(73, 52, 15);
//用球体剪裁等高线
vtkSmartPointer<vtkClipDataSet> skinClip =
vtkSmartPointer<vtkClipDataSet>::New();
skinClip->SetInputConnection(skinExtractor->GetOutputPort());
skinClip->SetClipFunction(clipFunction);
skinClip->SetValue(0);
skinClip->GenerateClipScalarsOn();
skinClip->Update();
vtkSmartPointer<vtkDataSetMapper> skinMapper =
vtkSmartPointer<vtkDataSetMapper>::New();
skinMapper->SetInputConnection(skinClip->GetOutputPort());
skinMapper->ScalarVisibilityOff();
vtkSmartPointer<vtkActor> skin =
vtkSmartPointer<vtkActor>::New();
skin->SetMapper(skinMapper);
skin->GetProperty()->SetDiffuseColor(1, .49, .25);
vtkSmartPointer<vtkProperty> backProp =
vtkSmartPointer<vtkProperty>::New();
backProp->SetDiffuseColor(0.8900, 0.8100, 0.3400);
skin->SetBackfaceProperty(backProp);
//定义“镜头”的模型。它的几何结构与隐式
//用于剪裁等高线的球体
vtkSmartPointer<vtkSphereSource> lensModel =
vtkSmartPointer<vtkSphereSource>::New();
lensModel->SetRadius(50);
lensModel->SetCenter(73, 52, 15);
lensModel->SetPhiResolution(201);
lensModel->SetThetaResolution(101);
// 使用镜头模型几何形状采样输入体积
vtkSmartPointer<vtkProbeFilter> lensProbe =
vtkSmartPointer<vtkProbeFilter>::New();
lensProbe->SetInputConnection(lensModel->GetOutputPort());
lensProbe->SetSourceConnection(reader->GetOutputPort());
// 用等表面值剪辑镜头数据
vtkSmartPointer<vtkClipDataSet> lensClip =
vtkSmartPointer<vtkClipDataSet>::New();
lensClip->SetInputConnection(lensProbe->GetOutputPort());
lensClip->SetValue(500);
lensClip->GenerateClipScalarsOff();
lensClip->Update();
// 定义一个合适的灰度lut
vtkSmartPointer<vtkLookupTable> bwLut =
vtkSmartPointer<vtkLookupTable>::New();
bwLut->SetTableRange(0, 2048);
bwLut->SetSaturationRange(0, 0);
bwLut->SetHueRange(0, 0);
bwLut->SetValueRange(.2, 1);
bwLut->Build();
vtkSmartPointer<vtkDataSetMapper> lensMapper =
vtkSmartPointer<vtkDataSetMapper>::New();
lensMapper->SetInputConnection(lensClip->GetOutputPort());
lensMapper->SetScalarRange(lensClip->GetOutput()->GetScalarRange());
lensMapper->SetLookupTable(bwLut);
vtkSmartPointer<vtkActor> lens =
vtkSmartPointer<vtkActor>::New();
lens->SetMapper(lensMapper);
//创建数据的初始视图很方便。的FocalPoint而位置构成一个矢量方向。稍后(ResetCamera()方法)
//这个矢量是用来定位摄像机来查看数据的
//这个方向。
vtkSmartPointer<vtkCamera> aCamera =
vtkSmartPointer<vtkCamera>::New();
aCamera->SetViewUp(0, 0, -1);
aCamera->SetPosition(0, -1, 0);
aCamera->SetFocalPoint(0, 0, 0);
aCamera->ComputeViewPlaneNormal();
aCamera->Azimuth(30.0);
aCamera->Elevation(30.0);
vtkSmartPointer<vtkRenderer> aRenderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renWin =
vtkSmartPointer<vtkRenderWindow>::New();
renWin->AddRenderer(aRenderer);
vtkSmartPointer<vtkRenderWindowInteractor> iren =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow(renWin);
aRenderer->AddActor(lens);
aRenderer->AddActor(skin);
aRenderer->SetActiveCamera(aCamera);
aRenderer->ResetCamera();
aCamera->Dolly(1.5);
aRenderer->SetBackground(.2, .3, .4);
renWin->SetSize(640, 480);
aRenderer->ResetCameraClippingRange();
renWin->Render();
iren->Initialize();
iren->Start();
return EXIT_SUCCESS;
}
VTK vtkMarchingCubes
最新推荐文章于 2024-01-08 10:04:39 发布