使用vtk创建立方体,设置顶点为不同颜色

该文展示了如何在Qt环境下,结合vtk9.2库构建一个显示不同颜色顶点的三维立方体。主要涉及的编程组件包括vtkPoints、vtkPolyDataMapper、vtkActor等,用于生成和渲染立方体,并通过vtkRenderWindowInteractor实现交互功能。
摘要由CSDN通过智能技术生成

引言

改示例为官网上的例子。创建了一个顶点是不同颜色的立方体。

示例

开发环境

使用QtCreator4.11.2,Qt5.14.2。使用的vtk9.2的库及其头文件。创建空项目。

示例代码

其pro文件中的内容:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11 vtk9.2

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0



SOUPDIR = $$PWD/../../SOUPdependency
vtk9.2 {
    contains(QT_ARCH, x86_64) {
        include($$SOUPDIR/vtk-9.2/vtk-9.2.pri)
    } else {
        include($$SOUPDIR/vtk-9.2-2017-omp-win32/vtk-9.2.pri)
    }
    DEFINES += vtkEventDataButton3D=vtkEventDataDevice3D
    DEFINES += vtkEventDataMove3D=vtkEventDataDevice3D
}

include($$SOUPDIR/dcmtk-3.6.4/dcmtk-3.6.4.pri)

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

SOURCES += \
    main.cpp

main.cpp

#include <vtkPoints.h>
#include <vtkFloatArray.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkCamera.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointData.h>

#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2)//渲染
VTK_MODULE_INIT(vtkInteractionStyle)//交互样式
VTK_MODULE_INIT(vtkRenderingFreeType)//文本图像
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2)//体素

int main(int argc,char*argv[])
{
    std::array<std::array<double,3>,8> pts = {{{{0,0,0}},{{1,0,0}},{{1,0,1}},{{0,0,1}},{{0,1,0}},{{1,1,0}},{{1,1,1}},{{0,1,1}}}};
    std::array<std::array<vtkIdType,4>,6> ordering = {{{{0,1,2,3}},{{4,5,6,7}},{{0,1,5,4}},{{1,2,6,5}},{{2,3,7,6}},{{3,0,4,7}}}};

    vtkNew<vtkPoints> points;
    vtkNew<vtkFloatArray> scalars;//设置顶点为不同颜色
    vtkNew<vtkCellArray> cellArray;
    vtkNew<vtkPolyData> polyData;

    for(int i = 0;i < pts.size();++i)
    {
        points->InsertPoint(i,pts[i].data());
        scalars->InsertTuple1(i,i);
    }
    for(auto &&i:ordering)
    {
        cellArray->InsertNextCell(vtkIdType(i.size()),i.data());
    }
    polyData->SetPoints(points);
    polyData->SetLines(cellArray);
    polyData->GetPointData()->SetScalars(scalars);

    vtkNew<vtkPolyDataMapper> mapper;
    mapper->SetInputData(polyData);
    mapper->SetScalarRange(polyData->GetScalarRange());

    vtkNew<vtkActor> actor;
    actor->SetMapper(mapper);

    vtkNew<vtkCamera> camera;
    camera->SetPosition(1,1,1);
    camera->SetFocalPoint(0,0,0);

    vtkNew<vtkNamedColors> colors;
    vtkNew<vtkRenderer> render;
    render->AddActor(actor);
    render->SetActiveCamera(camera);
    render->ResetCamera();
    render->SetBackground(colors->GetColor3d("Cornsilk").GetData());

    vtkNew<vtkRenderWindow> renderWindow;
    renderWindow->AddRenderer(render);
    renderWindow->SetWindowName("Cube");
    renderWindow->SetSize(600,600);
    renderWindow->Render();

    vtkNew<vtkRenderWindowInteractor> interactor;
    interactor->SetRenderWindow(renderWindow);
    interactor->Start();

    return 0;
}

运行结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

肩上风骋

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

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

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

打赏作者

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

抵扣说明:

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

余额充值