VTK初学一,b_PolyVertex多个图形点的绘制

时间:2024-07-26 08:04:31
#ifndef INITIAL_OPENGL
#define INITIAL_OPENGL
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL)
VTK_MODULE_INIT(vtkInteractionStyle)
#endif

#include <iostream>
using namespace std;
#include "vtkPolyDataMapper.h"
#include "vtkWin32OpenGLRenderWindow.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkPoints.h"
#include "vtkWin32RenderWindowInteractor.h"
#include "vtkProperty.h"
#include "vtkFloatArray.h"
#include "vtkPolyData.h"
#include "vtkDataSetMapper.h"
#include "vtkActor2D.h"
#include "vtkContourFilter.h"
#include "vtkContourValues.h"
#include "vtkUnstructuredGrid.h"
#include "vtkPointData.h"
#include "vtkVertex.h"
#include "vtkPolyVertex.h"
#include "vtkInteractorStyleTrackballCamera.h"

void myShow(vtkSmartPointer<vtkUnstructuredGrid> aGrid);

int main()
{
    //几何数据
    vtkSmartPointer<vtkPoints> points=vtkSmartPointer<vtkPoints>::New();
//    points->SetNumberOfPoints(5);//此行可有可无
    points->InsertPoint(0,0,0,0);
    points->InsertPoint(1,0.5,0.5,0);
    points->InsertPoint(2,-0.3,-0.2,0);
    points->InsertPoint(3,0.8,-0.5,0);
    points->InsertPoint(4,1,0.5,0.3);
    //拓扑数据
    vtkSmartPointer<vtkPolyVertex>polyVertex=vtkSmartPointer<vtkPolyVertex>::New();
    polyVertex->GetPointIds()->SetNumberOfIds(5);//必须设置Id个数,否则可以编译,不能运行
    polyVertex->GetPointIds()->SetId(0,0);//第一个参数是几何point的ID号,第2个参数是拓扑中的Id号
    polyVertex->GetPointIds()->SetId(1,1);
    polyVertex->GetPointIds()->SetId(2,2);
    polyVertex->GetPointIds()->SetId(3,3);
    polyVertex->GetPointIds()->SetId(4,4);
    //属性数据
    vtkSmartPointer<vtkFloatArray>pointsScalars=vtkSmartPointer<vtkFloatArray>::New();
    pointsScalars->SetNumberOfTuples(5);//此行可有可无
    pointsScalars->InsertValue(0,0);//第1个参数是points点的Id,第2个参数是该点的属性值
    pointsScalars->InsertValue(1,0);//第1个参数是points点的Id,第2个参数是该点的属性值
    pointsScalars->InsertValue(2,0);//第1个参数是points点的Id,第2个参数是该点的属性值
    pointsScalars->InsertValue(3,0);//第1个参数是points点的Id,第2个参数是该点的属性值
    pointsScalars->InsertValue(4,0);//第1个参数是points点的Id,第2个参数是该点的属性值
    //将以上三部分数据组合成一个结构vtkUnstructureGrid
    vtkSmartPointer<vtkUnstructuredGrid> aGrid=vtkSmartPointer<vtkUnstructuredGrid>::New();
    aGrid->Allocate(1,1);
    aGrid->SetPoints(points);
    aGrid->GetPointData()->SetScalars(pointsScalars);
    aGrid->InsertNextCell(polyVertex->GetCellType(),polyVertex->GetPointIds());
    //在窗口中显示多个Vertex
    myShow(aGrid);
    return 0;
}

void myShow(vtkSmartPointer<vtkUnstructuredGrid> aGrid)
{
    //设置映射器
    vtkSmartPointer<vtkDataSetMapper> aMapper=vtkSmartPointer<vtkDataSetMapper>::New();
    aMapper->SetInputData(aGrid);
    aMapper->ScalarVisibilityOn();

    vtkSmartPointer<vtkActor> anActor=vtkSmartPointer<vtkActor>::New();
    anActor->SetMapper(aMapper);
    anActor->GetProperty()->SetRepresentationToPoints();

    anActor->GetProperty()->SetDiffuseColor(1,0,0);
    anActor->GetProperty()->SetPointSize(10);
 //创建显示窗口
    vtkSmartPointer<vtkRenderer> ren1=vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renWin=vtkSmartPointer<vtkRenderWindow>::New();
    ren1->AddActor(anActor);
    renWin->AddRenderer(ren1);

    vtkSmartPointer<vtkRenderWindowInteractor> iren=vtkSmartPointer<vtkRenderWindowInteractor>::New();
    vtkSmartPointer<vtkInteractorStyleTrackballCamera> style=vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
    iren->SetInteractorStyle(style);
    iren->SetRenderWindow(renWin);

    renWin->SetSize(700,700);
    ren1->ResetCamera();
    renWin->Render();
    iren->Start();
}

VTK初学一,b_PolyVertex多个图形点的绘制