Bootstrap

vtk绘制渐变色polydata

创建VC++2015控制台程序,设置include路径:E:\VTK_8_0\include\vtk-8.0
设置lib路径:E:\VTK_8_0\lib*.lib

下面的代码是创建两个cell的polydata,并设置顶点的颜色,一般用来绘制后处理网格的颜色显示,颜色
可以表示场中顶点的温度,应力,浓度等物理量。

在这里插入图片描述

#pragma once

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include <vtkSmartPointer.h>
#include <vtkPoints.h>
#include <vtkPolygon.h>
#include <vtkTriangle.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>
#include <vtkUnsignedCharArray.h> //Attribution
#include <vtkPointData.h>
#include <vtkCellData.h>
///
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>

int vtktest00()
{
    //顶点坐标
    vtkSmartPointer<vtkPoints> pts =
        vtkSmartPointer<vtkPoints>::New();
    pts->InsertNextPoint(0.0, 0.0, 0.0);
    pts->InsertNextPoint(1.0, 0.0, 0.0);
    pts->InsertNextPoint(1.0, 1.0, 0.0);
    pts->InsertNextPoint(0.0, 1.0, 0.0);
    pts->InsertNextPoint(2.0, 0.0, 0.0);

    //cell0: 四边形
    vtkSmartPointer<vtkPolygon> polygon =
        vtkSmartPointer<vtkPolygon>::New();
    polygon->GetPointIds()->SetNumberOfIds(4);
    polygon->GetPointIds()->SetId(0, 0);
    polygon->GetPointIds()->SetId(1, 1);
    polygon->GetPointIds()->SetId(2, 2);
    polygon->GetPointIds()->SetId(3, 3);

    //cell1: 三角形
    vtkSmartPointer<vtkTriangle> triangle =
        vtkSmartPointer<vtkTriangle>::New();
    triangle->GetPointIds()->SetId(0, 1);
    triangle->GetPointIds()->SetId(1, 2);
    triangle->GetPointIds()->SetId(2, 4);

    //cells
    vtkSmartPointer<vtkCellArray> cells =
        vtkSmartPointer<vtkCellArray>::New();
    cells->InsertNextCell(polygon);
    cells->InsertNextCell(triangle);

    //polydata设置顶点和cells
    vtkSmartPointer<vtkPolyData> polygonPolyData =
        vtkSmartPointer<vtkPolyData>::New();
    polygonPolyData->SetPoints(pts);
    polygonPolyData->SetPolys(cells);

    //设置顶点的scalars(colors)
    unsigned char red[3] = { 255, 0, 0 };
    unsigned char green[3] = { 0, 255, 0 };
    unsigned char blue[3] = { 0, 0, 255 };
    vtkSmartPointer<vtkUnsignedCharArray> ptColor =
        vtkSmartPointer<vtkUnsignedCharArray>::New();
    ptColor->SetNumberOfComponents(3);
    ptColor->InsertNextTypedTuple (red);
    ptColor->InsertNextTypedTuple(green);
    ptColor->InsertNextTypedTuple(blue);
    ptColor->InsertNextTypedTuple(red);
    ptColor->InsertNextTypedTuple(green);
    polygonPolyData->GetPointData()->SetScalars(ptColor);

    //设置cell的scalars(colors)
    vtkSmartPointer<vtkUnsignedCharArray> cellColor =
        vtkSmartPointer<vtkUnsignedCharArray>::New();
    cellColor->SetNumberOfComponents(3);
    cellColor->InsertNextTypedTuple(blue);
    cellColor->InsertNextTypedTuple(red);
    polygonPolyData->GetCellData()->SetScalars(cellColor);

    //polydata 设给 mapper
    vtkSmartPointer<vtkPolyDataMapper> mapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputData(polygonPolyData);

     设置使用cell的scalar
    // mapper->SetScalarModeToUseCellData();
    
    // 设置使用顶点的scalar(颜色渐变)
    mapper->SetScalarModeToUsePointData();

    vtkSmartPointer<vtkActor> actor =
        vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);

    vtkSmartPointer<vtkRenderer> render =
        vtkSmartPointer<vtkRenderer>::New();
    render->AddActor(actor);
    render->SetBackground(0.2, 0.2, 0.4);

    vtkSmartPointer<vtkRenderWindow> rw =
        vtkSmartPointer<vtkRenderWindow>::New();
    rw->AddRenderer(render);
    rw->SetSize(640, 480);
    rw->SetWindowName("Creating PolyData Structure");

    vtkSmartPointer<vtkRenderWindowInteractor> rwi =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    rwi->SetRenderWindow(rw);
    rwi->Render();
    rwi->Start();

    return 0;
}
;