Qt&Vtk-005-Arrays

简介: Qt&Vtk-005-Arrays

image.png文章目录

Qt&Vtk-Arrays


1 基础工作

1.1 新建界面设计师类

1.2 放置一个QWidget并提升为QVTKOpenGLWidget

2 代码搬运

2.1 marrays.h

2.2 marrays.cpp

3 运行效果

4 涉及知识点

4.1 vtkPoints

4.2 vtkNamedColors

4.3 vtkCellArray

4.4 vtkPointData

★ 源码 ★

Qt&Vtk-Arrays

今天我有来搬运代码了,今天搬运的是我们vtk中的***Arrays***,原来实例运行效果如下。

image.png

1 基础工作


1.1 新建界面设计师类


由于昨天在搬运第一个实例***AmbientSpheres***的时候,我对我的工程做了小小调整,今天我可以免去前面的那些步骤,直接新建一个Qt 界面设计师类就可以,就是下图中的这个。image.pngimage.png

2 代码搬运


2.1 marrays.h


#ifndef MARRAYS_H
#define MARRAYS_H
#include <QWidget>
#include "QVTKOpenGLWidget.h"               //新版本,旧版QVTKWidget
#include "vtkAutoInit.h"
#include "vtkActor.h"
#include "vtkCellArray.h"
#include "vtkDoubleArray.h"
#include "vtkFloatArray.h"
#include "vtkIntArray.h"
#include "vtkNamedColors.h"
#include "vtkNew.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include <array>
namespace Ui {
class MArrays;
}
class MArrays : public QWidget
{
    Q_OBJECT
public:
    explicit MArrays(QWidget *parent = 0);
    ~MArrays();
private:
    Ui::MArrays *ui;
    vtkNew<vtkNamedColors> colors;              //新建颜色对象
    vtkNew<vtkDoubleArray> pcoords;             //新建Double数组
    std::array<std::array<double,3>,4> pts = {{{{0.0, 0.0, 0.0}},
                                               {{0.0, 1.0, 0.0}},
                                               {{1.0, 0.0, 0.0}},
                                               {{1.0, 1.0, 0.0}}}};     //新建Double 二维数组
    vtkNew<vtkPoints> points;                   //新建坐标点
    vtkNew<vtkCellArray> strips;                //暂时不清楚
    vtkNew<vtkIntArray> temperature;            //暂时不了解
    vtkNew<vtkDoubleArray> vorticity;           //Double 数组
    vtkNew<vtkPolyData> polydata;               //PolyData格式的数据
    vtkNew<vtkPolyDataMapper> mapper;           //映射器
    vtkNew<vtkActor> actor;                     //就是Actor
    vtkNew<vtkRenderer> render;                 //渲染
};
#endif // MARRAYS_H

2.2 marrays.cpp

#include "marrays.h"
#include "ui_marrays.h"
#include <QDebug>
MArrays::MArrays(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MArrays)
{
    ui->setupUi(this);
    pcoords->SetNumberOfComponents(3);          //实例中说是设置其组件数为3,默认为1,这里还不明白
    pcoords->SetNumberOfTuples(4);              //设置pcoords可以容纳4个Tuples数据
    for (auto i = 0ul;i<pts.size();++i) {       //把数据加进去
        pcoords->SetTuple(i,pts[i].data());
    }
    points->SetData(pcoords);                   //我理解就是数据放进这个点数组里面
    strips->InsertNextCell(4);                  //这个干啥还不清楚
    strips->InsertCellPoint(0);
    strips->InsertCellPoint(1);
    strips->InsertCellPoint(2);
    strips->InsertCellPoint(3);
//    temperature->SetName("Temperature");      //这个为啥要设置名字,咱也不懂,先撸为敬
                                                //测试这个和颜色有关系
    temperature->InsertNextValue(10);
    temperature->InsertNextValue(20);
    temperature->InsertNextValue(50);
    temperature->InsertNextValue(80);
//    vorticity->SetName("Vorticity");          //这个为啥要设置名字,咱也不懂,先撸为敬
    vorticity->InsertNextValue(1.0);
    vorticity->InsertNextValue(1.0);
    vorticity->InsertNextValue(1.0);
    vorticity->InsertNextValue(1.0);
    polydata->SetPoints(points);
    polydata->SetStrips(strips);
    polydata->GetPointData()->SetScalars(temperature);      //官方文档中就一句话,设置标量数据,啥是标量数据呀,咋就能修改颜色了
    polydata->GetPointData()->AddArray(vorticity);
    mapper->SetInputData(polydata);         //映射器输入数据
    mapper->SetScalarRange(0,80);           //又一个Scalar 也能影响颜色,这尼玛,这个好像是要区我们temperature中的区一部分吧
    actor->SetMapper(mapper);               //
    render->AddActor(actor);                //添加actor
    render->SetBackground(colors->GetColor3d("DarkSlateGray").GetData());   //设置渲染背景
    ui->widget->GetRenderWindow()->AddRenderer(render);         //添加渲染器
}
MArrays::~MArrays()
{
    delete ui;
}

image.png

image.png

image.pngimage.pngimage.pngimage.pngvtkCellArray这个东西,我也没有找到啥有用的内容,咱也不知道这是个啥东西。这里暂时先引用官方的描述,待后面我逐渐明白了在做补存。


object to represent cell connectivity


vtkCellArray stores dataset topologies as an explicit connectivity table listing the point ids that make up each cell.


Internally, the connectivity table is represented as two arrays: Offsets and Connectivity.


Offsets is an array of [numCells+1] values indicating the index in the Connectivity array where each cell’s points start. The last value is always the length of the Connectivity array.


The Connectivity array stores the lists of point ids for each cell.


4.4 vtkPointData

参考链接:https://vtk.org/doc/nightly/html/classvtkPointData.html

image.png

image.png

目录
相关文章
Qt&Vtk-023-MultiView
Qt&Vtk-023-MultiView
145 0
Qt&Vtk-023-MultiView
Qt&Vtk-028-SGrid
Qt&Vtk-028-SGrid
158 0
Qt&Vtk-028-SGrid
Qt&Vtk-026-QScalarsToColors
Qt&Vtk-026-QScalarsToColors
213 0
Qt&Vtk-026-QScalarsToColors
Qt&Vtk-018-FixedPointVolumeRayCastMapperCT
Qt&Vtk-018-FixedPointVolumeRayCastMapperCT
156 0
Qt&Vtk-018-FixedPointVolumeRayCastMapperCT
Qt&Vtk-022-LabeledMesh
Qt&Vtk-022-LabeledMesh
162 0
Qt&Vtk-022-LabeledMesh
Qt&Vtk-008-Cone3
Qt&Vtk-008-Cone3
134 0
Qt&Vtk-008-Cone3
Qt&Vtk-011-Cone6
Qt&Vtk-011-Cone6
153 0
Qt&Vtk-011-Cone6
Qt&Vtk-016-DiffuseSpheres
Qt&Vtk-016-DiffuseSpheres
129 0
Qt&Vtk-016-DiffuseSpheres
Qt&Vtk-027-RGrid
Qt&Vtk-027-RGrid
153 0
Qt&Vtk-027-RGrid
Qt&Vtk-017-EasyView
Qt&Vtk-017-EasyView
197 0
Qt&Vtk-017-EasyView