需求,实时添加数据点,需要每个点颜色都可以设置
以下是测试代码,首先创建10个点,然后后面的点需要自己添加,通过按钮点击添加数据
#include <QApplication>
#include <QMainWindow>
#include <QHBoxLayout>
#include <QPushButton>
#include <vtkSmartPointer.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkGenericOpenGLRenderWindow.h>
#include <vtkPolyDataMapper.h>
#include <vtkPolyData.h>
#include <vtkPoints.h>
#include <vtkCellArray.h>
#include <vtkVertexGlyphFilter.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkPolyLine.h>
#include <QVTKOpenGLNativeWidget.h>
#include <vtkNamedColors.h>
#include <QRandomGenerator>
#include <vtkUnsignedCharArray.h>
int main(int argc, char *argv[]) {
// QApplication
QApplication app(argc, argv);
QMainWindow mainWindow;
// Generate random points
auto points = vtkSmartPointer<vtkPoints>::New();
auto lines = vtkSmartPointer<vtkCellArray>::New();
auto colors = vtkSmartPointer<vtkUnsignedCharArray>::New();
colors->SetNumberOfComponents(3);
colors->SetName("Colors");
vtkIdType pid[2];
for (unsigned int i = 0; i < 10; i++) {
double x = QRandomGenerator::global()->bounded(10.0);
double y = QRandomGenerator::global()->bounded(10.0);
double z = QRandomGenerator::global()->bounded(10.0);
// create a color based on the point index
unsigned char color[3];
color[0] = QRandomGenerator::global()->bounded(255); // red
color[1] = QRandomGenerator::global()->bounded(255); // green
color[2] = QRandomGenerator::global()->bounded(255); // blue
pid[1] = points->InsertNextPoint(x, y, z);
if (i > 0) {
pid[0] = pid[1] - 1;
lines->InsertNextCell(2, pid); // insert lines between points
}
// insert color for the point
colors->InsertNextTypedTuple(color);
}
// Create poly data from points and line
auto polyData = vtkSmartPointer<vtkPolyData>::New();
polyData->SetPoints(points);
polyData->SetLines(lines); // only needed if you have lines
polyData->GetPointData()->SetScalars(colors); // attaches colors to points
// Create vertex glyphs from points
auto glyphFilter = vtkSmartPointer<vtkVertexGlyphFilter>::New();
glyphFilter->SetInputData(polyData);
glyphFilter->Update();
// Mapper and Actor for points
auto pointsMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
pointsMapper->SetInputConnection(glyphFilter->GetOutputPort());
pointsMapper->SetScalarModeToUsePointData(); // use the color we previously set
auto pointsActor = vtkSmartPointer<vtkActor>::New();
pointsActor->SetMapper(pointsMapper);
pointsActor->GetProperty()->SetPointSize(2); // set the size to 2 for visibility
// Set up the line
auto lineMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
lineMapper->SetInputData(polyData);
auto lineActor = vtkSmartPointer<vtkActor>::New();
lineActor->SetMapper(lineMapper);
lineActor->GetProperty()->SetLineWidth(2);
// VTK Renderer and RenderWindow
auto renderer = vtkSmartPointer<vtkRenderer>::New();
auto renderWindow = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
renderer->AddActor(pointsActor); // Add both actora
renderer->AddActor(lineActor);
renderWindow->AddRenderer(renderer);
// Add VTK to Qt GUI
auto vtkWidget = new QVTKOpenGLNativeWidget();
vtkWidget->setRenderWindow(renderWindow);
QPushButton *updateButton = new QPushButton("Add Random Point");
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(vtkWidget);
layout->addWidget(updateButton);
QWidget *widget = new QWidget;
widget->setLayout(layout);
mainWindow.setCentralWidget(widget);
QObject::connect(updateButton, &QPushButton::clicked, [&](){
// Add new point upon button click
double x = QRandomGenerator::global()->bounded(10.0);
double y = QRandomGenerator::global()->bounded(10.0);
double z = QRandomGenerator::global()->bounded(10.0);
unsigned char color[3];
color[0] = QRandomGenerator::global()->bounded(255);
color[1] = QRandomGenerator::global()->bounded(255);
color[2] = QRandomGenerator::global()->bounded(255);
pid[1] = points->InsertNextPoint(x, y, z);
// connect the last two points with a line
pid[0] = pid[1] - 1;
lines->InsertNextCell(2, pid);
// add point color
colors->InsertNextTypedTuple(color);
colors->Modified(); // ensure the colors are updated
points->Modified(); // ensure the points are updated
polyData->Modified(); // ensure data is update to render
renderer->Render(); // re-render
});
mainWindow.resize(800, 600);
mainWindow.show();
return app.exec();
}