vtk多窗口同步:相机同步+共享数据+contourWidget交互同步

代码效果直接看后面的展示动态效果图

#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkCamera.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkCommand.h"
#include "vtkCallbackCommand.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkSmartPointer.h"
#include "vtkInteractorStyleSwitch.h"
#include "vtkContourWidget.h"
#include "vtkContourRepresentation.h"

static vtkSmartPointer renderer01;
static vtkSmartPointer renderer02;
static vtkSmartPointer renderWindow01;
static vtkSmartPointer renderWindow02;

vtkSmartPointer contourWidget1 = vtkSmartPointer::New();
vtkSmartPointer contourWidget2 = vtkSmartPointer::New();

class vtkMyCameraCallback : public vtkCommand
{
public:
	static vtkMyCameraCallback *New()
	{
		return new vtkMyCameraCallback;
	}
	virtual void Execute(vtkObject *caller, unsigned long, void*)
	{
		vtkCamera *camera = static_cast(caller);
		double camPos[3];
		double focalPos[3];
		double upVector[3];
		camera->GetPosition(camPos);
		camera->GetFocalPoint(focalPos);
		camera->GetViewUp(upVector);

		//遍历所有renderer,都设置一遍最新的相机
		renderer01->SetActiveCamera(camera);
		renderer02->SetActiveCamera(camera);
		renderWindow01->Render();
		renderWindow02->Render();
	}
	vtkSmartPointer m_renderer;
};

class vtkMyContourCallback : public vtkCommand
{
public:
	static vtkMyContourCallback *New()
	{
		return new vtkMyContourCallback;
	}
	void Execute(vtkObject *caller, unsigned long eventId, void *callData)
	{
		vtkSmartPointer polydata = vtkSmartPointer::New();
		m_contourWidget->GetContourRepresentation()->GetNodePolyData(polydata);

		contourWidget1->Initialize(polydata);
		contourWidget2->Initialize(polydata);
		renderWindow01->Render();
		renderWindow02->Render();
	}

	vtkSmartPointer m_contourWidget;
};

int main(int argc, char *argv[])
{
	// 制作数据polydata
	int numPts = 5;
	vtkSmartPointer points = vtkSmartPointer::New();
	for (int i = 0; i < numPts; i++)
	{
		// Create numPts points evenly spread around a circumference of radius 0.1
		const double angle = 2.0*vtkMath::Pi()*i / numPts;
		std::cout << "ponit:" << static_cast(i) << " " << 0.1*cos(angle) << " " << 0.1*sin(angle) << " " << 0.0 << std::endl;
		points->InsertPoint(static_cast(i), 0.1*cos(angle), 0.1*sin(angle), 0.0);
	}
	// Create a cell array to connect the points into meaningful geometry
	vtkIdType* vertexIndices = new vtkIdType[numPts + 1];
	for (int i = 0; i < numPts; i++)
	{
		vertexIndices[i] = static_cast(i);
	}
	// Set the last vertex to 0; this means the last line segment will join the 19th point (vertices[19])
	// with the first one (vertices[0]), thus closing the circle.
	vertexIndices[numPts] = 0;
	for (int i = 0; i < numPts + 1; ++i)
	{
		std::cout << "line:" << vertexIndices[i] << " " << vertexIndices[i + 1] << endl;
	}
	vtkSmartPointer lines = vtkSmartPointer::New();
	lines->InsertNextCell(numPts + 1, vertexIndices);
	// Create polydata to hold the geometry just created, and populate it
	vtkSmartPointer polydata = vtkSmartPointer::New();
	polydata->SetPoints(points);
	polydata->SetLines(lines);


	renderer01 = vtkSmartPointer::New();
	renderer02 = vtkSmartPointer::New();

	renderWindow01 = vtkSmartPointer::New();
	renderWindow02 = vtkSmartPointer::New();

	renderWindow01->AddRenderer(renderer01);
	renderWindow02->AddRenderer(renderer02);

	vtkSmartPointer windowInteractor01 = vtkSmartPointer::New();
	vtkSmartPointer windowInteractor02 = vtkSmartPointer::New();
	windowInteractor01->SetRenderWindow(renderWindow01);
	windowInteractor02->SetRenderWindow(renderWindow02);


	contourWidget1->CreateDefaultRepresentation();
	contourWidget2->CreateDefaultRepresentation();

	//渲染时使用同样的数据
	contourWidget1->SetInteractor(windowInteractor01);
	contourWidget2->SetInteractor(windowInteractor02);
	contourWidget1->On();
	contourWidget2->On();
	contourWidget1->Initialize(polydata);
	contourWidget2->Initialize(polydata);

	//形状使用同样的数据来保持同步
	vtkSmartPointer< vtkMyContourCallback> contourCallBack1 = vtkSmartPointer< vtkMyContourCallback>::New();
	contourCallBack1->m_contourWidget = contourWidget1;
	vtkSmartPointer< vtkMyContourCallback> contourCallBack2 = vtkSmartPointer< vtkMyContourCallback>::New();
	contourCallBack2->m_contourWidget = contourWidget2;
	contourWidget1->AddObserver(vtkCommand::EndInteractionEvent, contourCallBack1);//拖动控制点完成
	contourWidget2->AddObserver(vtkCommand::EndInteractionEvent, contourCallBack2);//拖动控制点完成
	contourWidget1->AddObserver(vtkCommand::KeyReleaseEvent, contourCallBack1);//删除一个控制点
	contourWidget2->AddObserver(vtkCommand::KeyReleaseEvent, contourCallBack2);//删除一个拖动控制点
	contourWidget1->AddObserver(vtkCommand::InteractionEvent, contourCallBack1);//拖动一个控制点
	contourWidget2->AddObserver(vtkCommand::InteractionEvent, contourCallBack2);//拖动一个拖动控制点


	//相机同步
	vtkSmartPointer cameraCallback1 = vtkSmartPointer::New();
	cameraCallback1->m_renderer = renderer01;
	vtkSmartPointer cameraCallback2 = vtkSmartPointer::New();
	cameraCallback2->m_renderer = renderer02;
	renderer01->GetActiveCamera()->AddObserver(vtkCommand::ModifiedEvent, cameraCallback1);
	renderer02->GetActiveCamera()->AddObserver(vtkCommand::ModifiedEvent, cameraCallback2);
	renderer01->ResetCamera();
	renderer02->ResetCamera();
	
	//程序启动
	windowInteractor01->Initialize();
	windowInteractor02->Initialize();
	windowInteractor01->Start();
	windowInteractor02->Start();

	return 0;

}

vtk多窗口同步:相机同步+共享数据+contourWidget交互同步_第1张图片

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