开发环境:
- Windows 11 家庭中文版
- Microsoft Visual Studio Community 2019
- VTK-9.3.0.rc0
- vtk-example
- 参考代码
代码逻辑:初始化数据集points -> 添加数据集到polydata -> 通过vtkVertexGlyphFilter过滤(带顶点、单元数据)polydata为顶点数据 -> 添加过滤后2D数据到vtkPolyDataMapper2D -> 添加到vtkActor2D
Actor2D:
#include <vtkActor2D.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper2D.h>
#include <vtkProperty2D.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkVertexGlyphFilter.h>int main(int, char*[])
{vtkNew<vtkNamedColors> colors;vtkNew<vtkPoints> points;points->InsertNextPoint(10, 10, 0);points->InsertNextPoint(100, 100, 0);points->InsertNextPoint(200, 200, 0);/*VtkPolyData 是一个数据对象,它是 vtkDataSet 的具体实现。VtkPolyData 表示由顶点、直线、多边形和/或三角形条组成的几何结构。点和单元属性值(例如,标量、向量等)也被表示。*/vtkNew<vtkPolyData> polydata;polydata->SetPoints(points);std::cout << "NumberOfCells: " << polydata->GetNumberOfCells() << std::endl;//0 此时还没有指定cellstd::cout << "NumberOfPoints: " << polydata->GetNumberOfPoints() << std::endl;//3/*这个过滤器丢弃输入中的所有单元格,并在每个点上用一个顶点替换它们。这个过滤器的预期用途大致相当于 vtkGlyph3D 过滤器,只不过这个过滤器是专门针对具有许多顶点的数据的,这使得渲染结果比字形过滤器更快,更少混乱。这个过滤器可以采用图形或点集作为输入。*/vtkNew<vtkVertexGlyphFilter> glyphFilter;glyphFilter->SetInputData(polydata);//vtkDataObject->vtkPolyDataglyphFilter->Update();/*VtkPolyDataMapper 是一个将多边形数据(即 vtkPolyData)映射到图形原语的类。VtkPolyDataMapper 作为特定于设备的多边形数据映射器的超类,它实际上执行到渲染/图形硬件/软件的映射。vtkPolyDataMapper用来把输入的数据进行转换为几何图元(点、线、多边形)进行渲染VtkPolyDataMapper2D 是一个映射器,它将3D 多边形数据(vtkPolyData)渲染到2D图像平面(即渲染器的 viewport)上。默认情况下,通过忽略 vtkPolyData 中3D 点的 z坐标,并将 x-y 值作为本地显示值(即像素坐标) ,将3D 数据转换为2D 数据。*/vtkNew<vtkPolyDataMapper2D> mapper;mapper->SetInputConnection(glyphFilter->GetOutputPort());mapper->Update();//VtkActor2D 与 vtkActor 类似,但是它用于二维图像和注释。vtkNew<vtkActor2D> actor;actor->SetMapper(mapper);actor->GetProperty()->SetColor(colors->GetColor3d("Gold").GetData());actor->GetProperty()->SetPointSize(8);// Create a renderer, render window, and interactor.vtkNew<vtkRenderer> renderer;vtkNew<vtkRenderWindow> renderWindow;renderWindow->AddRenderer(renderer);vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;renderWindowInteractor->SetRenderWindow(renderWindow);// Add the actor to the scenerenderer->AddActor(actor);renderer->SetBackground(colors->GetColor3d("DarkSlateGray").GetData());renderWindow->SetSize(300, 300);renderWindow->SetWindowName("Actor2D");// Render and interactrenderWindow->Render();renderWindowInteractor->Start();return EXIT_SUCCESS;
}