vtk设置相机位置和点云颜色

1.头文件

#ifndef QVTK3DWIDGET_H
#define QVTK3DWIDGET_H

#include "./common/common.h"
#include 
#include 
#include 

#include 

namespace Ui {
class QVTK3DWidget;
}

class QVTK3DWidget : public QWidget
{
    Q_OBJECT

public:
    explicit QVTK3DWidget(QWidget *parent = nullptr);
    ~QVTK3DWidget();

    void ShowPoints(QVector& vec);      // 显示点云数据
    void IdentifyPoints(QVector& vec);  // 标识点
    void Clear();   // 清空界面
    int CameraPosition(int angle, float length);    // 计算相机位置
private:
    Ui::QVTK3DWidget *ui;

    vtkSmartPointer m_camera;        // 相机
    vtkSmartPointer m_renderer;    // 渲染
    int m_angle;    // 相机角度
};

#endif // QVTK3DWIDGET_H

2.源文件

#include "qvtk3dwidget.h"
#include "ui_qvtk3dwidget.h"
#include "ccamerastyle.h"

#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 
#include 
#include 

#include 
#include 

QVTK3DWidget::QVTK3DWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::QVTK3DWidget),
    m_angle(60)
{
    vtkOutputWindow::SetGlobalWarningDisplay(0);    // 禁止弹出警告弹框
    ui->setupUi(this);

    m_camera = vtkSmartPointer::New();   // 相机
    m_camera->SetClippingRange(0.04, 100000);       // 前后裁剪平面
    m_camera->SetFocalPoint(0.0f, 0.0f, 0.0f);      // 设置焦点
    m_camera->SetPosition(0.0f, 0.0f, 10.0f);       // 设置位置
    m_camera->SetViewUp(0.0f, 1.0f, 0.0f);          // 设置相机的上向量
    m_camera->SetViewAngle(m_angle);                // 设置摄像机视角

    m_renderer = vtkSmartPointer::New();
    m_renderer->SetBackground(0, 0, 0); // 设置背景颜色
    m_renderer->SetActiveCamera(m_camera);

    // 设置鼠标操作交互
    vtkSmartPointer style = vtkSmartPointer::New();
    ui->widget_vtk->GetRenderWindow()->GetInteractor()->SetInteractorStyle(style);
    ui->widget_vtk->GetRenderWindow()->AddRenderer(m_renderer);
}

QVTK3DWidget::~QVTK3DWidget()
{
    delete ui;
}

void QVTK3DWidget::ShowPoints(QVector& vec)
{
    if(vec.isEmpty()) {
        return;
    }

    vtkSmartPointer points = vtkSmartPointer::New();
    for(auto& pt : vec) {
        points->InsertNextPoint(pt.x, -pt.y, pt.z);
    }

    vtkSmartPointer polydata = vtkSmartPointer::New();
    polydata->SetPoints(points);

    vtkSmartPointer glyphFilter = vtkSmartPointer::New();
    glyphFilter->SetInputData(polydata);
    glyphFilter->Update();

    vtkSmartPointer elevationFilter = vtkSmartPointer::New();
    elevationFilter->SetInputConnection(glyphFilter->GetOutputPort());
    auto bounds = polydata->GetBounds();
    //qDebug() << "xmin" << bounds[0] << "xmax" << bounds[1] << "ymin" <SetLowPoint(0, 0, bounds[5]);
    elevationFilter->SetHighPoint(0, 0, bounds[4]);

    vtkSmartPointer mapper = vtkSmartPointer::New();
    mapper->SetInputConnection(elevationFilter->GetOutputPort());

    vtkSmartPointer actor = vtkSmartPointer::New();
    actor->SetMapper(mapper);

    m_renderer->AddActor(actor);

    // 所有点的中心点
    auto xmid = (bounds[0] + bounds[1]) / 2.0;
    auto ymid = (bounds[2] + bounds[3]) / 2.0;
    auto zmid = (bounds[4] + bounds[5]) / 2.0;

    // 相机设置
    m_camera->SetFocalPoint(xmid, ymid, zmid);            // 设置焦点
    auto xl = CameraPosition(m_angle / 2, qAbs(bounds[1] - xmid));
    auto yl = CameraPosition(m_angle / 2, qAbs(bounds[3] - ymid));
    auto zl = xl > yl ? xl : yl;
    m_camera->SetPosition(xmid, ymid, bounds[5] + zl);    // 设置位置

//    auto xl = CameraPosition(m_angle / 2, qAbs(bounds[1] - xmid));
//    auto zl = CameraPosition(m_angle / 2, qAbs(bounds[5] - zmid));
//    auto yl = xl > zl ? xl : zl;
//    m_camera->SetPosition(xmid, ymid - yl, zmid);    // 设置位置

    // 刷新
    ui->widget_vtk->update();
    return;
}

void QVTK3DWidget::IdentifyPoints(QVector& vec)
{
    if(vec.isEmpty()) {
        return;
    }
    for(auto& it : vec) {
        vtkSmartPointer sphere = vtkSmartPointer::New();
        sphere->SetCenter(it.x, -it.y, it.z);
        sphere->SetRadius(50);
        sphere->SetThetaResolution(30);
        vtkSmartPointer mapper = vtkSmartPointer::New();
        mapper->SetInputConnection(sphere->GetOutputPort());
        vtkSmartPointer actor = vtkSmartPointer::New();
        actor->SetMapper(mapper);
        actor->GetProperty()->SetColor(1.0f, 1.0f, 1.0f);
        m_renderer->AddActor(actor);
    }
}

void QVTK3DWidget::Clear()
{
    if(m_renderer == nullptr) {
        return;
    }
    m_renderer->RemoveAllObservers();
    m_renderer->RemoveAllViewProps();

    ui->widget_vtk->update();
}

int QVTK3DWidget::CameraPosition(int angle, float length)
{
    return length  / qTan(angle * M_PI / 180);
}

你可能感兴趣的:(vtk设置相机位置和点云颜色)