VTK实时添加数据并显示

需求,实时添加数据点,需要每个点颜色都可以设置
以下是测试代码,首先创建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();
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老了希望

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值