VTK显示网格

 

#ifndef VTKHSWGRIDWIDGET_H
#define VTKHSWGRIDWIDGET_H
/*
 * ModelName: vtkHSWGridWidget
 * Description: 实现网格
 * Author: hsw
 * Date: 2020-03-22
 *
*/

#include 
#include 
#include 
#include 
#include 
#include 

class vtkHSWGridWidget
{
public:
    vtkHSWGridWidget();
    ~vtkHSWGridWidget();
public:
    //      间隔: (gridSizeX, gridSizeY)
    //      范围: (rangeX, rangeY)
    vtkSmartPointer createGrid2D(double* rangeX, double* rangeY,
                                            double gridSizeX, double gridSizeY);
    // 间隔: (gridSizeX, gridSizeY, gridSizeZ)
    // 范围: (rangeX, rangeY, rangeZ)
    vtkSmartPointer createGrid3D(double* rangeX, double* rangeY, double* rangeZ,
                                            double gridSizeX, double gridSizeY, double gridSizeZ);
private:
    vtkSmartPointer         _linePoints;
    vtkSmartPointer      _lineCellArray;
    vtkSmartPointer       _linePolyData;
    vtkSmartPointer  _linePolyMapper;
};

#endif // VTKHSWGRIDWIDGET_H
#include "vtkhswgridwidget.h"

vtkHSWGridWidget::vtkHSWGridWidget()
{
    _linePoints     = vtkSmartPointer::New();
    _lineCellArray  = vtkSmartPointer::New();
    _linePolyData   = vtkSmartPointer::New();
    _linePolyMapper = vtkSmartPointer::New();
}

vtkHSWGridWidget::~vtkHSWGridWidget()
{
    // TODO...
}

vtkSmartPointer vtkHSWGridWidget::createGrid2D(double *rangeX, double *rangeY,
                                                                  double gridSizeX, double gridSizeY)
{
    // 平行Y方向的直线
    for(double gridX = rangeX[0]; gridX < rangeX[1] + (gridSizeX / 2.0); gridX += gridSizeX)
    {
        double lineStart[3] = {gridX, rangeY[0], 0.0};
        double lineEnd[3]   = {gridX, rangeY[1], 0.0};

        vtkIdType pointIdStart = _linePoints->InsertNextPoint(lineStart);
        vtkIdType pointIdEnd   = _linePoints->InsertNextPoint(lineEnd);

        vtkIdType singleLineCell[2] = {pointIdStart, pointIdEnd};
        _lineCellArray->InsertNextCell(2, singleLineCell);
    }

    // 平行X方向的直线
    for(double gridY = rangeY[0]; gridY < rangeY[1] + (gridSizeY / 2.0); gridY += gridSizeY)
    {
        double lineStart[3] = {rangeX[0], gridY, 0.0};
        double lineEnd[3]   = {rangeX[1], gridY, 0.0};

        vtkIdType pointIdStart = _linePoints->InsertNextPoint(lineStart);
        vtkIdType pointIdEnd   = _linePoints->InsertNextPoint(lineEnd);

        vtkIdType singleLineCell[2] = {pointIdStart, pointIdEnd};
        _lineCellArray->InsertNextCell(2, singleLineCell);
    }

    _linePolyData->SetLines(_lineCellArray);
    _linePolyData->SetPoints(_linePoints);

    _linePolyMapper->SetInputData(_linePolyData);

    return _linePolyMapper;
}

vtkSmartPointer vtkHSWGridWidget::createGrid3D(double *rangeX, double *rangeY, double *rangeZ,
                                                                  double gridSizeX, double gridSizeY, double gridSizeZ)
{
    // Z方向切片
    for(double gridZ = rangeZ[0]; gridZ < rangeZ[1] + (gridSizeZ / 2.0); gridZ += gridSizeZ)
    {
        // 平行Y方向的直线
        for(double gridX = rangeX[0]; gridX < rangeX[1] + (gridSizeX / 2.0); gridX += gridSizeX)
        {
            double lineStart[3] = {gridX, rangeY[0], gridZ};
            double lineEnd[3]   = {gridX, rangeY[1], gridZ};

            vtkIdType pointIdStart = _linePoints->InsertNextPoint(lineStart);
            vtkIdType pointIdEnd   = _linePoints->InsertNextPoint(lineEnd);

            vtkIdType singleLineCell[2] = {pointIdStart, pointIdEnd};
            _lineCellArray->InsertNextCell(2, singleLineCell);
        }

        // 平行X方向的直线
        for(double gridY = rangeY[0]; gridY < rangeY[1] + (gridSizeY / 2.0); gridY += gridSizeY)
        {
            double lineStart[3] = {rangeX[0], gridY, gridZ};
            double lineEnd[3]   = {rangeX[1], gridY, gridZ};

            vtkIdType pointIdStart = _linePoints->InsertNextPoint(lineStart);
            vtkIdType pointIdEnd   = _linePoints->InsertNextPoint(lineEnd);

            vtkIdType singleLineCell[2] = {pointIdStart, pointIdEnd};
            _lineCellArray->InsertNextCell(2, singleLineCell);
        }
    }

    for(double gridX = rangeX[0]; gridX < rangeX[1] + (gridSizeX / 2.0); gridX += gridSizeX)
    {
        for(double gridY = rangeY[0]; gridY < rangeY[1] + (gridSizeY / 2.0); gridY += gridSizeY)
        {
            double lineStart[3] = {gridX, gridY, rangeZ[0]};
            double lineEnd[3]   = {gridX, gridY, rangeZ[1]};

            vtkIdType pointIdStart = _linePoints->InsertNextPoint(lineStart);
            vtkIdType pointIdEnd   = _linePoints->InsertNextPoint(lineEnd);

            vtkIdType singleLineCell[2] = {pointIdStart, pointIdEnd};
            _lineCellArray->InsertNextCell(2, singleLineCell);
        }
    }

    _linePolyData->SetLines(_lineCellArray);
    _linePolyData->SetPoints(_linePoints);

    _linePolyMapper->SetInputData(_linePolyData);

    return _linePolyMapper;
}

 

VTK显示网格_第1张图片

VTK显示网格_第2张图片 

你可能感兴趣的:(c++,QT,vtk)