VTK:讀取raw檔案進行面繪製

HW140701發表於2017-12-15
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkRayCastImageDisplayHelper.h>
#include <vtkSmartPointer.h>
#include <vtkImageReader.h>
#include <vtkImageCast.h>
#include <vtkMarchingCubes.h>
#include <vtkStripper.h>
#include <vtkDataObject.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkOutlineFilter.h>
void main()

{
	vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer>::New();//設定繪製者(繪製物件指標)
	vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();//設定繪製視窗
	renWin->AddRenderer(ren);//將繪製者加入繪製視窗
	vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();//設定繪製互動操作視窗的
	iren->SetRenderWindow(renWin);//將繪製視窗新增到互動視窗

	vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();//互動攝像機
	iren->SetInteractorStyle(style);//style為互動模式
	vtkSmartPointer<vtkImageReader> reader = vtkSmartPointer<vtkImageReader>::New();

	reader->SetFileName("C:\\Users\\HuangWang\\Desktop\\Head_256x256x256.raw");
	reader->SetFileDimensionality(3);//設定顯示影像的維數
	reader->SetDataScalarType(VTK_UNSIGNED_CHAR);//VTK_UNSIGNED_short將資料轉換為unsigned char型
	reader->SetDataExtent(0, 255, 0, 255, 0, 255);
	reader->SetDataSpacing(0.9, 0.9, 0.9); //設定畫素間間距
	reader->SetDataOrigin(0.0, 0.0, 0.0);//設定基準點,(一般沒有用)做虛擬切片時可能會用的上
	reader->Update();

	vtkSmartPointer<vtkImageCast> readerImageCast = vtkSmartPointer<vtkImageCast>::New();//資料型別轉換  
	readerImageCast->SetInputConnection(reader->GetOutputPort());  
	readerImageCast->SetOutputScalarTypeToUnsignedChar();  
	readerImageCast->ClampOverflowOn();//閥值 

	vtkSmartPointer<vtkMarchingCubes>marchingcube=vtkSmartPointer<vtkMarchingCubes>::New();  
	marchingcube->SetInput((vtkDataObject*)readerImageCast->GetOutput());  
	//marchingcube->SetInputConnection(Reader->GetOutputPort());      //第二種讀取資料的方法  
	marchingcube->SetValue(0,160);    

	vtkSmartPointer<vtkStripper>Stripper=vtkSmartPointer<vtkStripper>::New();  
	Stripper->SetInput( marchingcube->GetOutput());   

	vtkSmartPointer<vtkPolyDataMapper>polyMapper=vtkSmartPointer<vtkPolyDataMapper>::New();  
	polyMapper->SetInput(Stripper->GetOutput());  
	polyMapper->ScalarVisibilityOff();  

	vtkSmartPointer<vtkActor>actor=vtkSmartPointer<vtkActor>::New();  
	actor->SetMapper(polyMapper);  
	actor->GetProperty()->SetDiffuseColor(1,0.19,0.15);  
	actor->GetProperty()->SetSpecular(0.1);  
	actor->GetProperty()->SetSpecularPower(10);  
	actor->GetProperty()->SetColor(1,0,0);  

	vtkSmartPointer<vtkOutlineFilter>outlinefilter=vtkSmartPointer<vtkOutlineFilter>::New();  
	outlinefilter->SetInputConnection(reader->GetOutputPort());  

	vtkSmartPointer<vtkPolyDataMapper>outlineMapper=vtkSmartPointer<vtkPolyDataMapper>::New();  
	outlineMapper->SetInputConnection(outlinefilter->GetOutputPort());  

	vtkSmartPointer<vtkActor>outlineActor=vtkSmartPointer<vtkActor>::New();  
	outlineActor->SetMapper(outlineMapper);  
	outlineActor->GetProperty()->SetColor(0,0,0);  

	
	ren->AddActor(actor);  
	ren->AddActor(outlineActor);  
	ren->SetBackground(1,1,1);  
	renWin->SetSize(640,640);  
	iren->Initialize();  
	iren->Start(); 
	ren->ResetCameraClippingRange();  
	 
}



相關文章