Qt&Vtk-022-LabeledMesh

简介: Qt&Vtk-022-LabeledMesh

image.png 这个我也不知道是啥东西。

文章目录


1 官方示例展示


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

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);
}

image.pngimage.png

目录
相关文章
Qt&Vtk-009-Cone4
Qt&Vtk-009-Cone4
122 0
Qt&Vtk-009-Cone4
Qt&Vtk-028-SGrid
Qt&Vtk-028-SGrid
158 0
Qt&Vtk-028-SGrid
Qt&Vtk-019-GPURenderDemo
Qt&Vtk-019-GPURenderDemo
160 0
Qt&Vtk-019-GPURenderDemo
Qt&Vtk-018-FixedPointVolumeRayCastMapperCT
Qt&Vtk-018-FixedPointVolumeRayCastMapperCT
157 0
Qt&Vtk-018-FixedPointVolumeRayCastMapperCT
Qt&Vtk-012-CreateTree
Qt&Vtk-012-CreateTree
155 0
Qt&Vtk-012-CreateTree
Qt&Vtk-025-QChartTable
Qt&Vtk-025-QChartTable
171 0
Qt&Vtk-025-QChartTable
Qt&Vtk-011-Cone6
Qt&Vtk-011-Cone6
153 0
Qt&Vtk-011-Cone6
Qt&Vtk-024-PiecewiseItem
Qt&Vtk-024-PiecewiseItem
168 0
Qt&Vtk-024-PiecewiseItem
Qt&Vtk-015-Cylinder
Qt&Vtk-015-Cylinder
139 0
Qt&Vtk-015-Cylinder
Qt&Vtk-017-EasyView
Qt&Vtk-017-EasyView
197 0
Qt&Vtk-017-EasyView