//移动立方体2
#include "vtkDICOMImageReader.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkMarchingCubes.h"
#include "vtkStripper.h"
#include "vtkActor.h"
#include "vtkPolyDataMapper.h"
#include "vtkProperty.h"
#include "vtkCamera.h"
#include "vtkBoxWidget.h"
//---------------------------//
#include <iostream>
#include <vector>
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkPoints.h"
#include "vtkPolyVertex.h"
#include "vtkUnstructuredGrid.h"
#include "vtkDataSetMapper.h"
#include "vtkPolyData.h"
#include "vtkCellArray.h"
#include "vtkInteractorStyleTrackball.h"
#include "vtkPolyDataMapper.h"
//--------------------------//
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkStructuredPoints.h>
#include <vtkStructuredPointsReader.h>
#include <vtkVolumeTextureMapper3D.h>
#include <vtkColorTransferFunction.h>
#include <vtkPiecewiseFunction.h>
#include <vtkVolumeProperty.h>
#include <vtkAxesActor.h>
#include <vtkImageShiftScale.h>
#include <vtkImageCast.h>
//---------------------------//
#include <iostream>
//-------------回调--------------//
#include <vtkRenderWindowInteractor.h>
#include <vtkCallbackCommand.h>
//-----------------------------//
long pressCounts = 0;
//vtkSmartPointer<vtkPoints> m_Points = vtkSmartPointer<vtkPoints>::New();
//vtkSmartPointer<vtkCellArray> vertices =vtkSmartPointer<vtkCellArray>::New(); //_存放细胞顶点,用于渲染(显示点云所必须的)
//vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
//vtkSmartPointer<vtkPolyDataMapper> pointMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
//vtkSmartPointer<vtkActor> pointActor = vtkSmartPointer<vtkActor>::New();
//
//vtkRenderWindow *renWin = vtkRenderWindow::New();
//第一步,定义回调函数。注意回调函数的签名,不能更改。
// void MyCallbackFunc(vtkObject*, unsigned long eid, void* clientdata, void *calldata)
//{
// std::cout<<"You have clicked: "<<++pressCounts<<" times."<<std::endl;
// //m_Points->SetPoint(0, 0, 600, pressCounts*5); //更新顶点坐标
// //m_Points->Modified();
// //renWin->Render();
// //iren->GetRenderWindow()->Render();
//}
//第一步
//class vtkMyCommand : public vtkCommand
//{
//public:
// static vtkMyCommand *New()
// {
// return new vtkMyCommand;
// }
//
// void SetObject(vtkPoints* Points)
// {
// n_Points = Points;
// }
//
// virtual void Execute(vtkObject *caller, unsigned long eventId, void* callData)
// {
// std::cout<<"You have clicked: "<<++pressCounts<<" times."<<std::endl;
// m_Points->SetPoint(0, 0, 600, pressCounts*5); //更新顶点坐标
// m_Points->Modified();
// renWin->Render();
// }
//
//private:
// vtkPoints* n_Points;
//};
=====================================================//
class ShowPointCallback : public vtkCommand
{
public:
static ShowPointCallback *New() //回调函数初始化函数
{
return new ShowPointCallback;
}
ShowPointCallback()
{
this->M_Points=0;
this->Interactor = 0;
}
void SetPoints(vtkPoints * Points)
{
this->M_Points = Points;
}
vtkPoints *GetPoints()
{
return this->M_Points;
}
void SetInteractor(vtkRenderWindowInteractor *interactor)
{
this->Interactor = interactor;
}
vtkRenderWindowInteractor *GetInteractor()
{
return this->Interactor;
}
virtual void Execute(vtkObject * ,unsigned long event,void *)
{
std::cout<<"You have clicked: "<<++pressCounts<<" times."<<std::endl;
vtkPoints * new_Points =GetPoints();
new_Points->SetPoint(0, 0, 600, pressCounts*5);
new_Points->Modified();
vtkRenderWindowInteractor *interactor = GetInteractor();
interactor->Render();
}
private:
//int Slicing;
//vtkImageReslice *ImageReslice;
vtkRenderWindowInteractor *Interactor;
vtkPoints * M_Points;
};
//**********************************************************************************//
int main()
{
vtkDICOMImageReader *reader = vtkDICOMImageReader::New();
reader->SetDataByteOrderToLittleEndian();//设置图像数据的字端序转换成小字端序
reader->SetDirectoryName("D://DicomImages/SE0/");
reader->SetDataSpacing(1.0,1.0,0.48);//设置图像像素间距和序列间距
//抽取等值面为骨头的信息
vtkMarchingCubes *boneExtractor = vtkMarchingCubes::New();
boneExtractor->SetInputConnection(reader->GetOutputPort());
boneExtractor->SetValue(0,-500); //设置提取的等值信息
//boneExtractor->SetValue(1,333);
//剔除旧的或废除的数据单元,提高绘制速度
vtkStripper *boneStripper = vtkStripper::New(); //三角带连接
boneStripper->SetInputConnection(boneExtractor->GetOutputPort());
//建立映射
vtkPolyDataMapper *boneMapper = vtkPolyDataMapper::New();
boneMapper->SetInput(boneStripper->GetOutput());
boneMapper->ScalarVisibilityOff();
//---------------------面2====================//
//抽取等值面为骨头的信息
vtkMarchingCubes *boneExtractor2 = vtkMarchingCubes::New();
boneExtractor2->SetInputConnection(reader->GetOutputPort());
//boneExtractor->SetValue(0,-500); //设置提取的等值信息
boneExtractor2->SetValue(0,333);
//剔除旧的或废除的数据单元,提高绘制速度
vtkStripper *boneStripper2 = vtkStripper::New(); //三角带连接
boneStripper2->SetInputConnection(boneExtractor2->GetOutputPort());
//建立映射
vtkPolyDataMapper *boneMapper2 = vtkPolyDataMapper::New();
boneMapper2->SetInput(boneStripper2->GetOutput());
boneMapper2->ScalarVisibilityOff();
//建立角色
vtkActor *bone2 = vtkActor::New();
bone2->SetMapper(boneMapper2);
bone2->GetProperty()->SetDiffuseColor(255,0,255);//(.1,.94,.52);
bone2->GetProperty()->SetSpecular(1);
bone2->GetProperty()->SetSpecularPower(20);
bone2->GetProperty()->SetOpacity(0.2) ;
//---------------------------------------//
//---------------------面3====================//
//抽取等值面为骨头的信息
vtkMarchingCubes *boneExtractor3 = vtkMarchingCubes::New();
boneExtractor3->SetInputConnection(reader->GetOutputPort());
//boneExtractor->SetValue(0,-500); //设置提取的等值信息
boneExtractor3->SetValue(0,750);
//剔除旧的或废除的数据单元,提高绘制速度
vtkStripper *boneStripper3 = vtkStripper::New(); //三角带连接
boneStripper3->SetInputConnection(boneExtractor3->GetOutputPort());
//建立映射
vtkPolyDataMapper *boneMapper3 = vtkPolyDataMapper::New();
boneMapper3->SetInput(boneStripper3->GetOutput());
boneMapper3->ScalarVisibilityOff();
//建立角色
vtkActor *bone3 = vtkActor::New();
bone3->SetMapper(boneMapper3);
bone3->GetProperty()->SetDiffuseColor(0,0,255);//(.1,.94,.52);
bone3->GetProperty()->SetSpecular(0.6);
bone3->GetProperty()->SetSpecularPower(20);
bone3->GetProperty()->SetOpacity(0.2) ;
//---------------------------------------//
//--------------------添加点云---------------------------//
//声明设为全局
vtkSmartPointer<vtkPoints> m_Points = vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkCellArray> vertices =vtkSmartPointer<vtkCellArray>::New(); //_存放细胞顶点,用于渲染(显示点云所必须的)
vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
vtkSmartPointer<vtkPolyDataMapper> pointMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
vtkSmartPointer<vtkActor> pointActor = vtkSmartPointer<vtkActor>::New();
m_Points->InsertPoint(0,150,150,150);
vertices->InsertNextCell(1); //_加入细胞顶点信息----用于渲染点集
vertices->InsertCellPoint(0);
//_创建待显示数据源
polyData->SetPoints(m_Points); //_设置点集
polyData->SetVerts(vertices); //_设置渲染顶点
pointMapper->SetInput( polyData );
pointActor->SetMapper( pointMapper );
pointActor->GetProperty()->SetColor(0.0,1.0,0.0);
pointActor->GetProperty()->SetAmbient(0.8);
pointActor->GetProperty()->SetPointSize(8);
//------------------------------------------------------//
//建立角色
vtkActor *bone = vtkActor::New();
bone->SetMapper(boneMapper);
bone->GetProperty()->SetDiffuseColor(1,1,1);//(.1,.94,.52);
bone->GetProperty()->SetSpecular(.3);
bone->GetProperty()->SetSpecularPower(20);
bone->GetProperty()->SetOpacity(0.2) ;
//标准句子
//定义绘制器
vtkRenderer *aRenderer = vtkRenderer::New();
//定义绘制窗口
vtkRenderWindow *renWin = vtkRenderWindow::New();//声明设为全局
renWin->AddRenderer(aRenderer);
renWin->SetSize(600,600) ;
//第二步,设置回调函数。
/*vtkSmartPointer<vtkCallbackCommand> mouseCallback =
vtkSmartPointer<vtkCallbackCommand>::New();
mouseCallback->SetCallback ( MyCallbackFunc );*/
//第二步
/*vtkSmartPointer<vtkMyCommand> callback = vtkSmartPointer<vtkMyCommand>::New();
callback->SetObject(m_Points);
*/
//定义窗口交互器
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
//第三步,将vtkCallbackCommand对象添加到观察者列表。
//iren->SetRenderWindow(polyData->GetRenderWindow());
/*iren->AddObserver(vtkCommand::LeftButtonPressEvent, mouseCallback);*/
//interactor->Initialize();
///interactor->Start();
/*iren->AddObserver(vtkCommand::LeftButtonPressEvent, callback);*/
//创建一个camera
vtkCamera *aCamera = vtkCamera::New();
aCamera->SetViewUp(0,0,-1);
aCamera->SetPosition(0,1,0);
aCamera->SetFocalPoint(0,0,0);
aRenderer->AddActor(bone);
aRenderer->AddActor(bone2);
aRenderer->AddActor(bone3);
aRenderer->AddActor(pointActor);
aRenderer->SetActiveCamera(aCamera);
aRenderer->ResetCamera();
aCamera->Dolly(1.5);
aRenderer->SetBackground(0,0,0);
aRenderer->ResetCameraClippingRange();
//vtkBoxWidget *boxWidget = vtkBoxWidget::New();
//boxWidget->SetInteractor(iren);
//boxWidget->SetPlaceFactor(1.25);
//
//boxWidget->SetProp3D(bone);
//boxWidget->PlaceWidget();
iren->Initialize();
//****************建立 观察者-命令 模式****************//
vtkSmartPointer<ShowPointCallback> callback =
vtkSmartPointer<ShowPointCallback>::New();
callback->SetPoints(m_Points);
callback->SetInteractor(iren);
iren->AddObserver(vtkCommand::LeftButtonPressEvent, callback);
//iren->Start();
//-------------------------------------------------------//
iren->Start();
//m_Points->SetPoint(0, 0, 600, 0); //更新顶点坐标
//m_Points->Modified();
renWin->Render();
//iren->GetRenderWindow()->Render();
reader->Delete();
iren->Delete();
return 0;
}
VTK三维重建+鼠标响应实时刷新跟踪点
最新推荐文章于 2024-06-26 16:44:29 发布