天天看點

Qt&Vtk-005-Arrays

Qt&Vtk-005-Arrays

文章目錄

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

★ 源碼 ★

今天我有來搬運代碼了,今天搬運的是我們vtk中的***Arrays***,原來執行個體運作效果如下。

Qt&Vtk-005-Arrays

由于昨天在搬運第一個執行個體***AmbientSpheres***的時候,我對我的工程做了小小調整,今天我可以免去前面的那些步驟,直接建立一個Qt 界面設計師類就可以,就是下圖中的這個。

Qt&Vtk-005-Arrays
Qt&Vtk-005-Arrays

#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


      

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

      
Qt&amp;Vtk-005-Arrays
Qt&amp;Vtk-005-Arrays
Qt&amp;Vtk-005-Arrays
Qt&amp;Vtk-005-Arrays
Qt&amp;Vtk-005-Arrays
Qt&amp;Vtk-005-Arrays

vtkCellArray這個東西,我也沒有找到啥有用的内容,咱也不知道這是個啥東西。這裡暫時先引用官方的描述,待後面我逐漸明白了在做補存。

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.

參考連結:

https://vtk.org/doc/nightly/html/classvtkPointData.html
Qt&amp;Vtk-005-Arrays
Qt&amp;Vtk-005-Arrays

繼續閱讀