Qt&Vtk-027-RGrid

头图

​ 摘要

1 官方示例展示

​ 今天还是搬运代码,争取吧能搬运的都先搬运一下,今天搬运官方RGrid代码,官方实例如下

在这里插入图片描述

#2 代码搬运

2.1 rgrid.h

#ifndef RGRID_H
#define RGRID_H

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

#include "vtkActor.h"
#include "vtkCamera.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkNamedColors.h"
#include "vtkPolyDataMapper.h"
#include "vtkProperty.h"
#include "vtkRectilinearGrid.h"
#include "vtkRectilinearGridGeometryFilter.h"
#include "vtkFloatArray.h"

#include "array"

namespace Ui {
class RGrid;
}

class RGrid : public QWidget
{
    Q_OBJECT

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

private:
    Ui::RGrid *ui;

    vtkNew<vtkNamedColors> colors;
    //数据直接操作不来吧
    std::array<double, 47> x = {
        {-1.22396,  -1.17188,  -1.11979,  -1.06771,  -1.01562,  -0.963542,
         -0.911458, -0.859375, -0.807292, -0.755208, -0.703125, -0.651042,
         -0.598958, -0.546875, -0.494792, -0.442708, -0.390625, -0.338542,
         -0.286458, -0.234375, -0.182292, -0.130209, -0.078125, -0.026042,
         0.0260415, 0.078125,  0.130208,  0.182291,  0.234375,  0.286458,
         0.338542,  0.390625,  0.442708,  0.494792,  0.546875,  0.598958,
         0.651042,  0.703125,  0.755208,  0.807292,  0.859375,  0.911458,
         0.963542,  1.01562,   1.06771,   1.11979,   1.17188}};
    std::array<double, 33> y = {
        {-1.25,    -1.17188,  -1.09375, -1.01562,  -0.9375,  -0.859375,
         -0.78125, -0.703125, -0.625,   -0.546875, -0.46875, -0.390625,
         -0.3125,  -0.234375, -0.15625, -0.078125, 0,        0.078125,
         0.15625,  0.234375,  0.3125,   0.390625,  0.46875,  0.546875,
         0.625,    0.703125,  0.78125,  0.859375,  0.9375,   1.01562,
         1.09375,  1.17188,   1.25}};
    std::array<double, 44> z = {{0,   0.1,  0.2, 0.3,  0.4, 0.5,  0.6, 0.7, 0.75,
                                 0.8, 0.9,  1,   1.1,  1.2, 1.3,  1.4, 1.5, 1.6,
                                 1.7, 1.75, 1.8, 1.9,  2,   2.1,  2.2, 2.3, 2.4,
                                 2.5, 2.6,  2.7, 2.75, 2.8, 2.9,  3,   3.1, 3.2,
                                 3.3, 3.4,  3.5, 3.6,  3.7, 3.75, 3.8, 3.9}};


    vtkNew<vtkFloatArray> xCoords,yCoords,zCoords;

    vtkNew<vtkRectilinearGrid> rgrid;

    vtkNew<vtkRectilinearGridGeometryFilter> plane;

    vtkNew<vtkPolyDataMapper> mapper;

    vtkNew<vtkActor> actor;
};

#endif // RGRID_H

2.2 rgrid.cpp

#include "rgrid.h"
#include "ui_rgrid.h"

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



    for(auto&& i : x)
        xCoords->InsertNextValue(i);
    for(auto&& i : y)
        yCoords->InsertNextValue(i);
    for(auto&& i : z)
        zCoords->InsertNextValue(i);

    rgrid->SetDimensions(int(x.size()),int(y.size()),int(z.size()));
    rgrid->SetXCoordinates(xCoords);
    rgrid->SetYCoordinates(yCoords);
    rgrid->SetZCoordinates(zCoords);


    plane->SetInputData(rgrid);
    plane->SetExtent(0,46,16,16,0,43);


    mapper->SetInputConnection(plane->GetOutputPort());


    actor->SetMapper(mapper);
    actor->GetProperty()->SetRepresentationToWireframe();
    actor->GetProperty()->SetColor(colors->GetColor3d("red").GetData());

    vtkRenderer *render =  vtkRenderer::New();
    render->AddActor(actor);
    ui->widget->GetRenderWindow()->AddRenderer(render);

}

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

3 运行效果

在这里插入图片描述

★ 源码 ★

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

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

在这里插入图片描述


博客签名2021

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DreamLife.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值