vtk 两个窗体不同的物体联动

 我有一篇是同一个窗体两个物体的相互控制联动,这次是不同的窗体的,加入了一个回调函数类来控制两个物体行动一致

#include <iostream>
#include <QCoreApplication>
#include <qdir.h>
#include <QDebug.h>
#include "vtkConeSource.h"
#include "vtkCubeSource.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkCamera.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkCommand.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkSmartPointer.h"
#include "vtkInteractorStyleSwitch.h"
#include "QDateTime.h"

static vtkSmartPointer<vtkRenderer> renderer01;
static vtkSmartPointer<vtkRenderer> renderer02;
static vtkSmartPointer<vtkRenderWindow> renderWindow01;
static vtkSmartPointer<vtkRenderWindow> renderWindow02;

class vtkMyCamerCallback : public vtkCommand
{
public:
	static vtkMyCamerCallback *New()
	{
		return new vtkMyCamerCallback;
	}
    //设置两个窗体一致的关键代码
	virtual void Execute(vtkObject *caller,
		unsigned long, void*)
	{
		vtkCamera *camera = static_cast<vtkCamera*>(caller);
		double camPos[3];
		double focalPos[3];
		double upVector[3];
		camera->GetPosition(camPos);
		camera->GetFocalPoint(focalPos);
		camera->GetViewUp(upVector);
		renderer02->SetActiveCamera(camera);
		renderWindow02->Render();
		renderer01->SetActiveCamera(camera);
		renderWindow01->Render();
	}

};

int main(int argc, int *argv[])
{
	vtkSmartPointer<vtkConeSource> cone =
		vtkSmartPointer<vtkConeSource>::New();
	vtkSmartPointer<vtkCubeSource> cube =
		vtkSmartPointer<vtkCubeSource>::New();
	cone->SetResolution(20);
	cone->Update();
	cube->Update();
	vtkSmartPointer<vtkPolyDataMapper> coneMapper =
		vtkSmartPointer<vtkPolyDataMapper>::New();
	vtkSmartPointer<vtkPolyDataMapper> cubeMapper =
		vtkSmartPointer<vtkPolyDataMapper>::New();
	coneMapper->SetInputData(cone->GetOutput());
	cubeMapper->SetInputData(cube->GetOutput());
	coneMapper->Update();
	cubeMapper->Update();
	vtkSmartPointer<vtkActor> coneActor =
		vtkSmartPointer<vtkActor>::New();
	vtkSmartPointer<vtkActor> cubeActor =
		vtkSmartPointer<vtkActor>::New();
	coneActor->SetMapper(coneMapper);
	cubeActor->SetMapper(cubeMapper);

	renderer01 = vtkSmartPointer<vtkRenderer>::New();
	renderer02 = vtkSmartPointer<vtkRenderer>::New();
	renderer01->AddActor(coneActor); // 锥形
	renderer02->AddActor(cubeActor); // 立方体

	renderWindow01 = vtkSmartPointer<vtkRenderWindow>::New();
	renderWindow02 = vtkSmartPointer<vtkRenderWindow>::New();

	renderWindow01->AddRenderer(renderer01);
	renderWindow02->AddRenderer(renderer02);
    
    // 使renderWindow01获得交互机制
	vtkSmartPointer<vtkRenderWindowInteractor> windowInteractor01 =
		vtkSmartPointer<vtkRenderWindowInteractor>::New();
	vtkSmartPointer<vtkInteractorStyleSwitch> trackballStyle =
		vtkSmartPointer<vtkInteractorStyleSwitch>::New();
	trackballStyle->SetCurrentStyleToTrackballCamera();
	windowInteractor01->SetInteractorStyle(trackballStyle);
	windowInteractor01->SetRenderWindow(renderWindow01);

	vtkSmartPointer<vtkRenderWindowInteractor> windowInteractor02 =
		vtkSmartPointer<vtkRenderWindowInteractor>::New();
	vtkSmartPointer<vtkInteractorStyleSwitch> trackballStyle1 =
		vtkSmartPointer<vtkInteractorStyleSwitch>::New();
	trackballStyle1->SetCurrentStyleToTrackballCamera();
	windowInteractor02->SetInteractorStyle(trackballStyle1);
	windowInteractor02->SetRenderWindow(renderWindow02);

	vtkSmartPointer<vtkMyCamerCallback> cameraCallback =
		vtkSmartPointer<vtkMyCamerCallback>::New();
    

    //添加观察函数
	renderer01->GetActiveCamera()->AddObserver(vtkCommand::ModifiedEvent, cameraCallback);
	renderer02->GetActiveCamera()->AddObserver(vtkCommand::ModifiedEvent, cameraCallback);
	renderer01->ResetCamera();
	renderer02->ResetCamera();
	windowInteractor01->Initialize();
	windowInteractor01->Start();
	windowInteractor02->Initialize();
	windowInteractor02->Start();

	return 0;
}

 

VTK(Visualization Toolkit)是一个强大的开源软件库,主要用于科学可视化、医学图像处理和图形应用程序开发。当你提到将两个STL(标准三角化语言)文件合并为一个,这是VTK的一个应用场景之一,尤其是在处理几何模型的时候。 在VTK中,你可以使用`vtkSTLReader`来读取单个或多个STL文件,然后使用如`vtkAppendPolyData`或者`vtkMergePolyData`这样的滤波器将它们组合在一起。具体步骤可能包括: 1. 创建`vtkSTLReader`实例,并分别加载两个STL文件。 2. 如果需要对模型进行预处理(例如清理、修剪),可以使用`vtkCleanPolyData`或`vtkClipPolyData`等。 3. 使用`vtkAppendPolyData`过滤器,设置其源数据为读取到的两个`vtkPolyData`对象,将其结果输出到一个新的`vtkPolyData`。 4. 最后,如果需要保存这个合并后的模型,可以创建一个`vtkSTLWriter`,设置输出文件名,然后调用其Write方法将合并后的`vtkPolyData`写入新的STL文件。 如果你想要代码示例,这里有一个简单的伪代码框架: ```python import vtk # 创建STL读者 reader1 = vtk.vtkSTLReader() reader1.SetFileName("file1.stl") reader2 = vtk.vtkSTLReader() reader2.SetFileName("file2.stl") # 合并数据 appendFilter = vtk.vtkAppendPolyData() appendFilter.AddInputConnection(reader1.GetOutputPort()) appendFilter.AddInputConnection(reader2.GetOutputPort()) merged_polydata = appendFilter.GetOutput() # 写入新文件 writer = vtk.vtkSTLWriter() writer.SetFileName("merged.stl") writer.SetInputData(merged_polydata) writer.Write() ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值