vtkPlaneWidget 实现实时切割

 #include "vtkDICOMImageReader.h"

#include "vtkFixedPointVolumeRayCastMapper.h"
#include "vtkColorTransferFunction.h"
#include "vtkPiecewiseFunction.h"
#include "vtkVolumeProperty.h"
#include "vtkVolume.h"

#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"

#include "vtkImageMapToColors.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkCutter.h"
#include "vtkPlane.h"
#include "vtkImageActor.h"
#include "vtkImageData.h"
#include "vtkImageCast.h"
#include "vtkPointData.h"
#include "vtkImageShiftScale.h"
#include "vtkPolyDataToImageStencil.h"
#include "vtkImageStencil.h"
#include "vtkLookupTable.h"
#include "vtkImagePlaneWidget.h"
#include "vtkCellArray.h"
#include "vtkType.h"
#include "vtkPlaneWidget.h"
#include "vtkProperty.h"
#include "vtkCommand.h"
#include "vtkProbeFilter.h"
#include "vtkImplicitPlaneWidget.h"
#include "vtkClipPolyData.h"
class vtkWidgetCall : public vtkCommand
{
public:


 static vtkWidgetCall *New()
 {
  return new vtkWidgetCall;
 }
public:
 virtual void Execute(vtkObject *caller, unsigned long eventId, void *callData)
 {
  vtkPlaneWidget *pWidget = vtkPlaneWidget::SafeDownCast(caller);

  pWidget->GetPlane(pPlane);

 }
public:

 vtkPlane *pPlane ;
 
};
void main()
{

 vtkRenderer *pRender1 = vtkRenderer::New(); 
 vtkRenderer *pRender2 = vtkRenderer::New(); 
 vtkRenderWindow *pWin = vtkRenderWindow::New();
 vtkRenderWindowInteractor *pInt = vtkRenderWindowInteractor::New();
 pWin->SetSize(600, 600);
 pWin->AddRenderer(pRender1);
 pWin->AddRenderer(pRender2);
 pRender1->SetViewport(0, 0, .5, 1);
 pRender2->SetViewport(0.5, 0, 1, 1);
 pRender2->SetBackground(.3, .3, .3);
 pInt->SetRenderWindow(pWin);
 

 vtkDICOMImageReader* pReader = vtkDICOMImageReader::New();
 pReader->SetDirectoryName("E:/ExportedImages/2");
 pReader->SetDataByteOrderToLittleEndian();
 pReader->Update(); //读取

 double range[2];
 pReader->GetOutput()->GetScalarRange(range);
 vtkImageShiftScale *m_pShift = vtkImageShiftScale::New();
 m_pShift->SetShift(-1.0*range[0]);
 m_pShift->SetScale(255.0/(range[1]-range[0]));
 m_pShift->SetOutputScalarTypeToUnsignedChar();
 m_pShift->SetInputConnection(pReader->GetOutputPort());
 m_pShift->ReleaseDataFlagOff();
 m_pShift->Update();

 vtkFixedPointVolumeRayCastMapper *pMapper = vtkFixedPointVolumeRayCastMapper::New();
 pMapper->SetInputConnection(pReader->GetOutputPort()); //映射:第一步

 vtkColorTransferFunction* pColor = vtkColorTransferFunction::New(); //第二步:color
 pColor->AddRGBSegment(0, 1, 1, 1, 255, 1, 1, 1);

 vtkPiecewiseFunction* pPiceFun = vtkPiecewiseFunction::New();
 pPiceFun->AddSegment(0, 0, 3000, 1);//第三步: vtkPiecwise分段

 pMapper->SetBlendModeToMaximumIntensity();

 vtkVolumeProperty* pProperty = vtkVolumeProperty::New();//设置属性
 pProperty->SetColor(pColor);

 pProperty->SetScalarOpacity(pPiceFun);
 pProperty->SetInterpolationTypeToLinear();
 pProperty->ShadeOff();

 vtkVolume* pVolume = vtkVolume::New();
 pVolume->SetProperty(pProperty);
 pVolume->SetMapper(pMapper); //生成体对象

 pRender1->AddVolume(pVolume); //增加体数据到渲染器中渲染


 vtkPlaneWidget *pWidget = vtkPlaneWidget::New();
 pWidget->SetInteractor(pInt);
 pWidget->SetInput(m_pShift->GetOutput());
 pWidget->SetResolution(10);
 pWidget->GetPlaneProperty()->SetColor(.9, .4, .4);
 pWidget->GetHandleProperty()->SetColor(0, .4, .7);
 pWidget->GetHandleProperty()->SetLineWidth(1.5);
 pWidget->NormalToYAxisOn();
 pWidget->SetRepresentationToSurface();
 pWidget->SetCenter(m_pShift->GetOutput()->GetCenter());
 pWidget->PlaceWidget();
 
 pWidget->On();


 vtkPlane *pPlane = vtkPlane::New();
 pPlane->SetOrigin(pReader->GetOutput()->GetCenter());
 pPlane->SetNormal(1, 0, 0);
 vtkCutter *pCut = vtkCutter::New();
 pCut->SetCutFunction(pPlane);
 pCut->SetInputConnection(m_pShift->GetOutputPort());   
 pCut->Update();
 
  
 vtkPolyDataMapper *selectMapper = vtkPolyDataMapper::New();
 selectMapper->SetInputConnection(pCut->GetOutputPort());

 vtkActor *pActor = vtkActor::New(); 
 pActor->SetMapper(selectMapper);
 pRender2->AddActor(pActor);


 vtkWidgetCall *pCall = vtkWidgetCall::New();
 pCall->pPlane = pPlane; 
 pWidget->AddObserver(vtkCommand::EndInteractionEvent, pCall);

 pInt->Initialize();
 pInt->Start();


}

 

效果

 

本文来源:http://hi.baidu.com/vived/item/476b8d80365761efe596e060

你可能感兴趣的:(切割,vtkPlaneWidget)