VTK 三维轮廓等值面的提取

目的: 学习VTK可视化


案例一

/************************************************************************/
/* 可视化基础算法-三维轮廓面(等值面)提取                                                                     */
/************************************************************************//

#include "stdafx.h"
#include   
VTK_MODULE_INIT(vtkRenderingOpenGL2);  
VTK_MODULE_INIT(vtkInteractionStyle);  

#include "vtkActor.h"
#include "vtkCamera.h"
#include "vtkPolyData.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindowInteractor.h"
#include 
#include  
#include  
#include 
#include  
#include 
#include  


int _tmain(int argc, _TCHAR* argv[])
{
	vtkSmartPointer pVol16Read = vtkVolume16Reader::New();
	pVol16Read->SetDataDimensions(64, 64); // 设置每幅图像的像数尺寸
	pVol16Read->SetDataByteOrderToLittleEndian();  // 标识所读取文件的字节序,对于PC机,进行该设置
	pVol16Read->SetFilePrefix("F:\\ZYHWorkspace\\Data\\headsq\\quarter"); //设置读取的图像文件名前缀
	pVol16Read->SetImageRange(1, 93);
	pVol16Read->SetDataOrigin(0, 0, 0);
	pVol16Read->SetDataSpacing(3.2, 3.2, 1.5);

	
	// 归并重合的点
	vtkSmartPointer pMergePoints = vtkMergePoints::New();
	pMergePoints->SetDivisions(32, 32, 46);
	pMergePoints->SetNumberOfPointsPerBucket(100);
	// 定义移动立方体过滤器对象
	vtkSmartPointer pMarchCube = vtkMarchingCubes::New();	
	pMarchCube->SetInputData((vtkDataObject *) pVol16Read->GetOutput()); // 处理体数据
	pMarchCube->SetValue(0, 1150); // 设置提取的等值面的值
	pMarchCube->ComputeGradientsOn();
	pMarchCube->ComputeScalarsOff();
	pMarchCube->SetLocator(pMergePoints);
	 
	// 生成等值面单元、顶点的标量值
	vtkSmartPointer pVectorNormal = vtkVectorNorm::New();
	pVectorNormal->SetInputData(pMarchCube->GetOutput());

	double Rang[2];
	pVectorNormal->GetOutput()->GetScalarRange(Rang);// 得到生成的标量值范围

	vtkSmartPointer pMapper = vtkPolyDataMapper::New();
	pMapper->SetInputData((vtkPolyData *) pVectorNormal->GetOutput());
	pMapper->ScalarVisibilityOn();
	pMapper->SetScalarRange(0, 1250);

	vtkSmartPointer pActor = vtkActor::New();
	pActor->SetMapper(pMapper);

	vtkSmartPointer pRen = vtkRenderer::New();
	pRen->AddActor(pActor);
	pRen->ResetCamera();
	pRen->SetBackground(1, 1, 1);
	
	vtkSmartPointer pRenWin = vtkRenderWindow::New();
	pRenWin->AddRenderer(pRen);
	pRenWin->SetSize(500, 500);
	pRenWin->Render();

	vtkSmartPointer pRenI = vtkRenderWindowInteractor::New();
	pRenI->SetRenderWindow(pRenWin);
	pRenI->Start();


	return 0;
}




案例二

// MarchingCubeVTK2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include   
VTK_MODULE_INIT(vtkRenderingOpenGL2);  
VTK_MODULE_INIT(vtkInteractionStyle);  

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int _tmain(int argc, _TCHAR* argv[])
{
	// Create the renderer, the render window, and the interactor. The renderer
	// draws into the render window, the interactor enables mouse- and 
	// keyboard-based interaction with the data within the render window.
	//
	vtkSmartPointer aRenderer = vtkSmartPointer::New();
	vtkSmartPointer renWin = vtkSmartPointer::New();
	renWin->AddRenderer(aRenderer);

	vtkSmartPointer iren = vtkSmartPointer::New();
	iren->SetRenderWindow(renWin);

	// The following reader is used to read a series of 2D slices (images)
	// that compose the volume. The slice dimensions are set, and the
	// pixel spacing. The data Endianness must also be specified. The reader
	// uses the FilePrefix in combination with the slice number to construct
	// filenames using the format FilePrefix.%d. (In this case the FilePrefix
	// is the root name of the file: quarter.)
	vtkSmartPointer v16 = vtkSmartPointer::New();
	v16->SetDataDimensions (64,64);
	v16->SetImageRange (1,93);
	v16->SetDataByteOrderToLittleEndian();
	v16->SetFilePrefix ("F:\\ZYHWorkspace\\Data\\headsq\\quarter");
	v16->SetDataSpacing (3.2, 3.2, 1.5);

	// An isosurface, or contour value of 500 is known to correspond to the
	// skin of the patient. Once generated, a vtkPolyDataNormals filter is
	// is used to create normals for smooth surface shading during rendering.
	vtkSmartPointer skinExtractor = vtkSmartPointer::New();
	skinExtractor->SetInputConnection(v16->GetOutputPort());
	skinExtractor->SetValue(0, 500);

	vtkSmartPointer skinNormals = vtkSmartPointer::New();
	skinNormals->SetInputConnection(skinExtractor->GetOutputPort());
	skinNormals->SetFeatureAngle(60.0);

	vtkSmartPointer skinMapper = vtkSmartPointer::New();
	skinMapper->SetInputConnection(skinNormals->GetOutputPort());
	skinMapper->ScalarVisibilityOff();

	vtkSmartPointer skin = vtkSmartPointer::New();
	skin->SetMapper(skinMapper);

	// An outline provides context around the data.
	vtkSmartPointer outlineData = vtkSmartPointer::New();
	outlineData->SetInputConnection(v16->GetOutputPort());

	vtkSmartPointer mapOutline = vtkSmartPointer::New();
	mapOutline->SetInputConnection(outlineData->GetOutputPort());

	vtkSmartPointer outline = vtkSmartPointer::New();
	outline->SetMapper(mapOutline);
	outline->GetProperty()->SetColor(0,0,0);

	// It is convenient to create an initial view of the data. The FocalPoint
	// and Position form a vector direction. Later on (ResetCamera() method)
	// this vector is used to position the camera to look at the data in
	// this direction.
	vtkSmartPointer aCamera = vtkSmartPointer::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);

	// Actors are added to the renderer. An initial camera view is created.
	// The Dolly() method moves the camera towards the FocalPoint,
	// thereby enlarging the image.
	aRenderer->AddActor(outline);
	aRenderer->AddActor(skin);
	aRenderer->SetActiveCamera(aCamera);
	aRenderer->ResetCamera ();
	aCamera->Dolly(2.5);

	// Set a background color for the renderer and set the size of the
	// render window (expressed in pixels).
	aRenderer->SetBackground(.2, .3, .4);
	renWin->SetSize(640, 480);

	// Note that when camera movement occurs (as it does in the Dolly()
	// method), the clipping planes often need adjusting. Clipping planes
	// consist of two planes: near and far along the view direction. The 
	// near plane clips out objects in front of the plane; the far plane
	// clips out objects behind the plane. This way only what is drawn
	// between the planes is actually rendered.
	aRenderer->ResetCameraClippingRange ();

	// Initialize the event loop and then start it.
	iren->Initialize();
	iren->Start();
	return 0;
}



不明白为什么有错结果出不来。。。


读取数据有问题吗? 不应该啊我都设置成绝对路径了,怎么还不对!

你可能感兴趣的:(VTK)