Qt&Vtk-022-LabeledMesh

Posted DreamLife.

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt&Vtk-022-LabeledMesh相关的知识,希望对你有一定的参考价值。

​ 这个我也不知道是啥东西。

1 官方示例展示

​ 哎呀,这英语才是第一生产力,英语不好,是真的很烦人那。今天我们么接着搬运代码,官方示例如下

2 代码搬运

2.1 labeledmesh.h

#ifndef LABELEDMESH_H
#define LABELEDMESH_H

#include <QWidget>
#include "QVTKOpenGLWidget.h"               //新版本,旧版QVTKWidget
#include "vtkAutoInit.h"

#include "vtkSmartPointer.h"
#include "vtkPoints.h"
#include "vtkCellArray.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper2D.h"
#include "vtkActor2D.h"
#include "vtkSphereSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkIdFilter.h"
#include "vtkRenderer.h"
#include "vtkSelectVisiblePoints.h"
#include "vtkLabeledDataMapper.h"
#include "vtkCellCenters.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkTextProperty.h"

static int xLength;
static int yLength;
static vtkSmartPointer<vtkSelectVisiblePoints> visPts;
static vtkSmartPointer<vtkSelectVisiblePoints> visCells;
static vtkSmartPointer<vtkPoints> pts;
static vtkSmartPointer<vtkRenderWindow>renwin;





namespace Ui {
class LabeledMesh;
}

class LabeledMesh : public QWidget
{
    Q_OBJECT

public:
    explicit LabeledMesh(QWidget *parent = 0);
    ~LabeledMesh();

    void PlaceWindow(int xmin, int ymin);

    void MoveWindow();


private:
    Ui::LabeledMesh *ui;
};

#endif // LABELEDMESH_H

2.2 labeledmesh.cpp

#include "labeledmesh.h"
#include "ui_labeledmesh.h"

LabeledMesh::LabeledMesh(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LabeledMesh)
{
    ui->setupUi(this);




    int xmin = 200;
    xLength = 100;
    int xmax = xmin + xLength;

    int ymin = 200;
    yLength = 100;
    int ymax = xmin+yLength;

    pts = vtkSmartPointer<vtkPoints>::New();
    pts->InsertPoint(0,xmin,ymin,0);
    pts->InsertPoint(1,xmax,ymin,0);
    pts->InsertPoint(2,xmax,ymax,0);
    pts->InsertPoint(3,xmin,ymax,0);

    vtkSmartPointer<vtkCellArray> rect = vtkSmartPointer<vtkCellArray>::New();

    rect->InsertNextCell(5);
    rect->InsertCellPoint(0);
    rect->InsertCellPoint(1);
    rect->InsertCellPoint(2);
    rect->InsertCellPoint(3);
    rect->InsertCellPoint(0);

    vtkSmartPointer<vtkPolyData> selectRect = vtkSmartPointer<vtkPolyData>::New();
    selectRect->SetPoints(pts);
    selectRect->SetLines(rect);

    vtkSmartPointer<vtkPolyDataMapper2D> rectMapper = vtkSmartPointer<vtkPolyDataMapper2D>::New();

    rectMapper->SetInputData(selectRect);

    vtkSmartPointer<vtkActor2D> rectActor = vtkSmartPointer<vtkActor2D>::New();

    rectActor->SetMapper(rectMapper);

    vtkSmartPointer<vtkSphereSource> sphere = vtkSmartPointer<vtkSphereSource>::New();
    vtkSmartPointer<vtkPolyDataMapper> sphereMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    sphereMapper->SetInputConnection(sphere->GetOutputPort());


    vtkSmartPointer<vtkActor> sphereActor = vtkSmartPointer<vtkActor>::New();
    sphereActor->SetMapper(sphereMapper);


    vtkSmartPointer<vtkIdFilter> ids = vtkSmartPointer<vtkIdFilter>::New();
    ids->SetInputConnection(sphere->GetOutputPort());
    ids->PointIdsOn();
    ids->CellIdsOn();
    ids->FieldDataOn();


    vtkSmartPointer<vtkRenderer> ren1 = vtkSmartPointer<vtkRenderer>::New();

    visPts = vtkSmartPointer<vtkSelectVisiblePoints>::New();
    visPts->SetInputConnection(ids->GetOutputPort());
    visPts->SetRenderer(ren1);
    visPts->SelectionWindowOn();
    visPts->SetSelection(xmin,xmin+xLength,ymin,ymin+yLength);



    vtkSmartPointer<vtkLabeledDataMapper> ldm = vtkSmartPointer<vtkLabeledDataMapper>::New();
    ldm->SetInputConnection(visPts->GetOutputPort());
    ldm->SetLabelModeToLabelFieldData();


    vtkSmartPointer<vtkActor2D> pointLabels = vtkSmartPointer<vtkActor2D>::New();
    pointLabels->SetMapper(ldm);


    vtkSmartPointer<vtkCellCenters> cc = vtkSmartPointer<vtkCellCenters>::New();
    cc->SetInputConnection(ids->GetOutputPort());

    visCells = vtkSmartPointer<vtkSelectVisiblePoints>::New();
    visCells->SetInputConnection(cc->GetOutputPort());
    visCells->SetRenderer(ren1);
    visCells->SelectionWindowOn();
    visCells->SetSelection(xmin,xmin+xLength,ymin,ymin+ yLength);



    vtkSmartPointer<vtkLabeledDataMapper> cellmapper = vtkSmartPointer<vtkLabeledDataMapper>::New();
    cellmapper->SetInputConnection(visCells->GetOutputPort());
    cellmapper->SetLabelModeToLabelFieldData();
    cellmapper->GetLabelTextProperty()->SetColor(0,1,0);



    vtkSmartPointer<vtkActor2D> cellLabels = vtkSmartPointer<vtkActor2D>::New();
    cellLabels->SetMapper(cellmapper);

    ui->widget->GetRenderWindow()->AddRenderer(ren1);

    ren1->AddActor(sphereActor);
    ren1->AddActor2D(rectActor);
    ren1->AddActor2D(pointLabels);
    ren1->AddActor2D(cellLabels);


//    MoveWindow();
//    PlaceWindow(xmin,ymin);



}

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

void LabeledMesh::PlaceWindow(int xmin, int ymin)
{
    int xmax = xmin + xLength;
    int ymax = ymin + yLength;

    visPts->SetSelection(xmin,xmax,ymin,ymax);

    visCells->SetSelection(xmin,xmax,ymin,ymax);


    pts->InsertPoint(0,xmin,ymin,0);
    pts->InsertPoint(1,xmin,ymin,0);
    pts->InsertPoint(2,xmin,ymin,0);
    pts->InsertPoint(3,xmin,ymin,0);

    pts->Modified();
    renwin->Render();
}

void LabeledMesh::MoveWindow()
{
    for(int y = 100;y<300;y+=25)
        for(int x = 100;x<300;x+=25)
            PlaceWindow(x,y);
}

3运行效果

​ 在我这里,调用movewindow是有问题的,后面再解决吧,这几天撸代码有点多,现在有点撸不动的感觉。

★ 源码 ★

源码分享一时爽,一直分享一直爽, 链接如下:

自取:https://github.com/DreamLife-Jianwei/Qt-Vtk


以上是关于Qt&Vtk-022-LabeledMesh的主要内容,如果未能解决你的问题,请参考以下文章

Qt&Vtk-012-CreateTree

Qt&Vtk-010-Cone5

Qt编程基于Qt的词典开发系列&lt;九&gt;--JSON数据解析

Qt & QWT:QwtPlotCurve - 曲线未显示

002-Qt&Vtk-第一个VtkQt程序

Qt & OpenGL:如何尽可能频繁地渲染?