c++:vtk9.3内vtkRenderer内有1个vtkAnnotatedCubeActor,设置其大小不随鼠标中键上下滚动而放大和缩小 给出详细注释的代码实现
时间: 2024-10-18 09:22:27 浏览: 26
在VTK( Visualization Toolkit )中,如果你想要控制vtkAnnotatedCubeActor的大小不会随着鼠标中键的滚动而自动调整缩放,你可以通过禁用它的InteractorStyleZoom配合手动设置相机的焦距(focal point)和视点(position)来实现。以下是详细的代码示例:
```cpp
// 引入必要的VTK头文件
#include <vtkSmartPointer.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkAnnotatedCubeActor.h>
int main(int argc, char *argv[])
{
// 创建渲染窗口
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
// 创建渲染器
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->SetBackground(0, 0, 0); // 设置背景颜色
// 创建vtkAnnotatedCubeActor并设置大小
vtkSmartPointer<vtkAnnotatedCubeActor> annotatedCubeActor = vtkSmartPointer<vtkAnnotatedCubeActor>::New();
annotatedCubeActor->GetMapper()->SetInputConnection(
vtkSmartPointer<vtkSphereSource>::New()->GetOutputPort());
annotatedCubeActor->SetScale(1.0, 1.0, 1.0); // 设置初始大小为1
// 添加到渲染器
renderer->AddActor(annotatedCubeActor);
// 禁用默认的交互式缩放风格
// 使用自定义的交互风格,如平移或固定视角
vtkSmartPointer<vtkInteractorStyleTrackballCamera> customStyle = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetInteractorStyle(customStyle);
// 绑定交互事件处理函数,防止缩放
connect(customStyle, &vtkInteractorStyle::MiddleButtonPressEvent,
[renderer](const auto &event) {
event.SetInteractor(NULL);
});
connect(customStyle, &vtkInteractorStyle::MiddleButtonReleaseEvent,
[renderer](const auto &event) {
event.SetInteractor(NULL);
});
// 设置相机属性,固定视点和焦距
renderer->SetActiveCamera(customStyle->GetActiveCamera());
renderer->GetActiveCamera()->SetPosition(0.0, 0.0, 5.0); // 视点
renderer->GetActiveCamera()->SetFocalPoint(0.0, 0.0, 0.0); // 焦距
// 将渲染器添加到渲染窗口
renderWindow->AddRenderer(renderer);
// 显示窗口
renderWindow->Render();
// 开启渲染循环
renderWindowInteractor->Start();
return 0;
}
```
阅读全文