VTK 点拾取


```cpp


#include "vtkDICOMImageReader.h"
#include "vtkPiecewiseFunction.h"
#include "vtkColorTransferFunction.h"
#include "vtkVolumeProperty.h"
#include "vtkVolumeRayCastCompositeFunction.h"
#include "vtkVolumeRayCastMapper.h"
#include "vtkVolume.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkImageCast.h"
#include "vtkImageShiftScale.h"
#include "vtkImageShrink3D.h"
#include "vtkCubeAxesActor2D.h"
#include "vtkTextProperty.h"
#include "vtkCamera.h"
#include "vtkPointWidget.h"
#include "vtkProperty.h"


void main()
{
     
  vtkRenderer *ren = vtkRenderer::New();
  vtkRenderWindow *renWin = vtkRenderWindow::New();
	renWin->AddRenderer(ren);

  vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
	iren->SetRenderWindow(renWin);

  vtkDICOMImageReader   *reader =  vtkDICOMImageReader::New();
	reader->SetDataByteOrderToLittleEndian();
	reader->SetDirectoryName("e://ct");
	reader->SetDataOrigin(0.0, 0.0, 0.0);

	vtkImageShiftScale *ShiftScale = vtkImageShiftScale::New();
		ShiftScale->SetInput((vtkDataObject *)reader->GetOutput());
		ShiftScale->SetOutputScalarTypeToShort();
		ShiftScale->SetShift (1024);
		ShiftScale->ClampOverflowOn();

	vtkImageCast *readerImageCast = vtkImageCast::New();
		readerImageCast->SetInput((vtkDataObject *)ShiftScale->GetOutput());
		readerImageCast->SetOutputScalarTypeToUnsignedShort();
		readerImageCast->ClampOverflowOn();

  reader->Delete();

	vtkPiecewiseFunction *opacityTransferFunction=
		vtkPiecewiseFunction::New();
		opacityTransferFunction->AddPoint(1024-585,0.07);
		opacityTransferFunction->AddPoint(1024-500,0.4);
		opacityTransferFunction->AddPoint(1024-211,0.53);
		opacityTransferFunction->AddPoint(1024-100,0.07);
		opacityTransferFunction->AddPoint(1024+100,0.13);
		opacityTransferFunction->AddPoint(1024+160,0.58);
		opacityTransferFunction->AddPoint(1024+200,1);

		opacityTransferFunction->ClampingOff();
	
	vtkColorTransferFunction *colorTransferFunction=
		vtkColorTransferFunction::New();

		colorTransferFunction->AddRGBPoint
			(1024-512,255/255.0, 153/255.0, 20/255.0);//皮肤
		colorTransferFunction->AddRGBPoint
			(1024-200,230/255.0, 140/255.0, 15/255.0);//皮肤
		colorTransferFunction->AddRGBPoint
			(1024,1.0, 0.9, 0.01);//软组织
		colorTransferFunction->AddRGBPoint//骨骼
			(1024+200,1, 1, 0.96);//white



  vtkVolumeProperty *volumeProperty = vtkVolumeProperty::New();
  //设定一个体绘容器的属性
	volumeProperty->SetColor(colorTransferFunction);
	volumeProperty->SetScalarOpacity(opacityTransferFunction);
	volumeProperty->ShadeOn();
	volumeProperty->SetInterpolationTypeToLinear();	
	volumeProperty->SetAmbient(0.2);
    volumeProperty->SetDiffuse(0.9);
    volumeProperty->SetSpecular(0.2);
    volumeProperty->SetSpecularPower(10);
	

  vtkVolumeRayCastCompositeFunction *compositeFunction = vtkVolumeRayCastCompositeFunction::New();
	//运行沿着光线合成 

  vtkVolumeRayCastMapper *volumeMapper = vtkVolumeRayCastMapper::New();
	  //体绘制器,特点:slow but accurate 
	  volumeMapper->SetVolumeRayCastFunction(compositeFunction); //体绘光线投射函数
	  volumeMapper->SetInputConnection(readerImageCast->GetOutputPort());//图像数据输入

	vtkVolume *volume = vtkVolume::New();
	//表示透示图中的一组三维数据
	  volume->SetMapper(volumeMapper);
	  volume->SetProperty(volumeProperty);

	ren->AddVolume(volume);
	ren->SetBackground(0, 0, 0);

  vtkCamera *aCamera = vtkCamera::New();
    aCamera->SetViewUp (0, 0, -1);
    aCamera->SetPosition (0, 1, 0);
    aCamera->SetFocalPoint (0, 0, 0);
    aCamera->ComputeViewPlaneNormal();

  ren->SetActiveCamera(aCamera);
  ren->ResetCamera ();

    vtkPointWidget *pointWidget = vtkPointWidget::New();
	  pointWidget->SetInteractor(iren);
	  pointWidget->SetInput((vtkDataSet *)readerImageCast->GetOutput());
	  pointWidget->PlaceWidget();
	  pointWidget->GetProperty()->SetColor(1,0,0);
	  pointWidget->OutlineOff ();
	  pointWidget->On();


	renWin->SetSize(600, 600);
	renWin->Render();


//    iren-> SetStillUpdateRate(0.5);
    iren-> SetDesiredUpdateRate(100);
	iren->Initialize();
	iren->Start();
    ren->ResetCameraClippingRange ();

	volumeMapper->Delete();
	iren->Delete();
	ren->Delete();
	renWin->Delete();
	opacityTransferFunction->Delete();
	volumeProperty->Delete();
	compositeFunction->Delete();
	volume->Delete();
	colorTransferFunction->Delete();

} 

你可能感兴趣的:(VTK,ITK)