美文网首页
VTK获取当前视图模型相机视角

VTK获取当前视图模型相机视角

作者: 荻庐夜雪 | 来源:发表于2021-01-05 11:02 被阅读0次

这个demo用来取当前模型的正确相机位置
做了一个交互,拖动和滚轮可以用来调整视图;确认好当前视图时再单击,控制台会输出当前的相机位置和视角
把数值赋值至主函数中的camera,注意调整相机的viewup
记得在render中设置所需的camera,否则会是默认的

  • 记得包含所有所需的头文件,如果出现报错“不允许指针指向不完整的类类型”一定是所需指针 包括返回指针 指向内容的头文件没有包含!

GetCameraInteractorDemo.h

#pragma once
#ifndef __GETCAMERAINTERACTORDEMO__
#define __GETCAMERAINTERACTORDEMO__
#include "header.h"
//1、 vtkInteractorStyleTrackballCamera:相当于移动摄像头,按键,屏幕上的所有内容都会动起来。
//2、vtkInteractorStyleTrackballActor:相当于实体对象,当鼠标在某一个对象上按键时,只有这个对象会动。
class GetCameraInteractorStyle : public vtkInteractorStyleTrackballCamera
{
public:
    static GetCameraInteractorStyle* New();
    vtkTypeMacro(GetCameraInteractorStyle, vtkInteractorStyleTrackballCamera);
    GetCameraInteractorStyle();
    virtual ~GetCameraInteractorStyle();
    virtual void OnLeftButtonDown();
    virtual void OnLeftButtonUp();
    virtual void OnMouseMove();
    vtkSmartPointer<vtkCamera> getCamera() const {      return _vtkCamera;  };
private:
    bool  mouseMoved; // 是否移动鼠标
    vtkSmartPointer<vtkCamera> _vtkCamera;
};
#endif

GetCameraInteractorDemo.cpp

#include "GetCameraInteractorDemo.h"

vtkStandardNewMacro(GetCameraInteractorStyle);
GetCameraInteractorStyle::GetCameraInteractorStyle()
    :mouseMoved(false), _vtkCamera(NULL)
{
    _vtkCamera = vtkSmartPointer<vtkCamera>::New();
}

GetCameraInteractorStyle::~GetCameraInteractorStyle()
{

}

void GetCameraInteractorStyle::OnLeftButtonDown()
{
    if (mouseMoved)
    {

    }
    mouseMoved = false;
    vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
}

void GetCameraInteractorStyle::OnLeftButtonUp()
{
    if (!mouseMoved) {
        _vtkCamera = this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->GetActiveCamera();
        cout << "GetPosition: " << _vtkCamera->GetPosition()[0] << " " << _vtkCamera->GetPosition()[1] << " " << _vtkCamera->GetPosition()[2] << endl;
        cout << "GetFocalPoint: " << _vtkCamera->GetFocalPoint()[0] << " " << _vtkCamera->GetFocalPoint()[1] << " " << _vtkCamera->GetFocalPoint()[2] << endl;
        cout << "GetViewAngle: " << _vtkCamera->GetViewAngle() << endl;
        vtkInteractorStyleTrackballCamera::OnLeftButtonUp();
    }
    else
    {
        vtkInteractorStyleTrackballCamera::OnLeftButtonUp();
    }

}

void GetCameraInteractorStyle::OnMouseMove()
{
    mouseMoved = true;
    vtkInteractorStyleTrackballCamera::OnMouseMove();
}

main


#include"header.h"
#include "GetCameraInteractorDemo.h"

int main()
{

    string STLfile = "pelvis.stl";
    vtkSmartPointer<vtkSTLReader> _pelvisSTLReader = vtkSmartPointer<vtkSTLReader>::New();
    _pelvisSTLReader->SetFileName(STLfile.c_str());
    _pelvisSTLReader->Update();

    vtkSmartPointer<vtkPolyDataMapper> _PolyDataMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    _PolyDataMapper->SetInputConnection(_pelvisSTLReader->GetOutputPort());


    vtkSmartPointer<vtkActor> _vtkActor = vtkSmartPointer<vtkActor>::New();
    _vtkActor->SetMapper(_PolyDataMapper);
    _vtkActor->SetPosition(0,0,0);

    /*GetPosition: 217.951 -289.087 -640.366
    GetFocalPoint: -120.33 160.945 -86.5276*/
    vtkSmartPointer<vtkCamera>_vtkCamera = vtkSmartPointer<vtkCamera>::New();
    _vtkCamera->SetPosition(217.951, - 289.087, - 640.366);
    _vtkCamera->SetFocalPoint(-120.33, 160.945, - 86.5276);
    _vtkCamera->SetViewUp(0,-1,0);

    vtkSmartPointer<vtkRenderer> _vtkRenderer = vtkSmartPointer<vtkRenderer>::New();
    _vtkRenderer->AddActor(_vtkActor);
    _vtkRenderer->SetActiveCamera(_vtkCamera);

    vtkSmartPointer<vtkRenderWindow> _vtkRenderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    _vtkRenderWindow->AddRenderer(_vtkRenderer);
    _vtkRenderWindow->SetSize(640, 640);
    _vtkRenderWindow->SetWindowName("OsteotomyPlanes");

    vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    iren->SetRenderWindow(_vtkRenderWindow);
    //vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
    vtkSmartPointer<GetCameraInteractorStyle> style =
        vtkSmartPointer<GetCameraInteractorStyle>::New();
    iren->SetInteractorStyle(style);

    // 显示坐标系的vtk组件 
    vtkSmartPointer<vtkAxesActor> axes_actor = vtkSmartPointer<vtkAxesActor>::New();
    axes_actor->SetPosition(0, 0, 0);
    axes_actor->SetTotalLength(2, 2, 2);
    axes_actor->SetShaftType(0);
    axes_actor->SetCylinderRadius(0.02);

    // 控制坐标系,使之随视角共同变化 
    vtkSmartPointer<vtkOrientationMarkerWidget> widget = vtkSmartPointer<vtkOrientationMarkerWidget>::New();
    widget->SetOrientationMarker(axes_actor);
    widget->SetInteractor(iren);
    widget->SetEnabled(1);
    widget->InteractiveOn();



    //iren->Initialize();
    iren->Start();

    system("pause");
    return 0;
}

相关文章

网友评论

      本文标题:VTK获取当前视图模型相机视角

      本文链接:https://www.haomeiwen.com/subject/yguioktx.html