You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用C++ VTK实现Gaussian cube格式文件的可视化展示

C++ VTK 实现.cube格式文件可视化方案

.cube格式为三维标量场+分子结构存储格式,你需要的效果为体渲染标量场+球棍分子模型叠加的可视化效果,可通过以下步骤实现:

1. 核心依赖说明

使用VTK 9.0+版本即可实现需求,需要用到的核心模块包括IOImage、RenderingVolume、RenderingCore、DomainsChemistry、InteractionStyle。

2. 完整实现代码

#include <vtkSmartPointer.h>
#include <vtkGaussianCubeReader.h>
#include <vtkImageData.h>
#include <vtkVolume.h>
#include <vtkSmartVolumeMapper.h>
#include <vtkVolumeProperty.h>
#include <vtkColorTransferFunction.h>
#include <vtkPiecewiseFunction.h>
#include <vtkMolecule.h>
#include <vtkMoleculeMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkNamedColors.h>

int main(int argc, char* argv[])
{
    if (argc < 2)
    {
        std::cerr << "Usage: " << argv[0] << " input.cube" << std::endl;
        return EXIT_FAILURE;
    }

    // 读取.cube格式文件,同时获取体数据和分子结构
    vtkNew<vtkGaussianCubeReader> reader;
    reader->SetFileName(argv[1]);
    reader->Update();

    // 体渲染管线配置
    double scalarRange[2];
    reader->GetOutput()->GetScalarRange(scalarRange);
    // 颜色传输函数,可自行调整节点匹配需求配色,默认是电子云常用蓝-白-红配色
    vtkNew<vtkColorTransferFunction> colorFunc;
    colorFunc->AddRGBPoint(scalarRange[0], 0.0, 0.0, 1.0);
    colorFunc->AddRGBPoint(0.0, 1.0, 1.0, 1.0);
    colorFunc->AddRGBPoint(scalarRange[1], 1.0, 0.0, 0.0);

    // 不透明度传输函数,低标量值区域设为完全透明
    vtkNew<vtkPiecewiseFunction> opacityFunc;
    opacityFunc->AddPoint(scalarRange[0], 0.0);
    opacityFunc->AddPoint(scalarRange[0] * 0.3, 0.1);
    opacityFunc->AddPoint(0.0, 0.0);
    opacityFunc->AddPoint(scalarRange[1] * 0.3, 0.1);
    opacityFunc->AddPoint(scalarRange[1], 0.8);

    vtkNew<vtkSmartVolumeMapper> volumeMapper;
    volumeMapper->SetInputData(reader->GetOutput());

    vtkNew<vtkVolumeProperty> volumeProperty;
    volumeProperty->SetColor(colorFunc);
    volumeProperty->SetScalarOpacity(opacityFunc);
    volumeProperty->SetInterpolationTypeToLinear();
    volumeProperty->ShadeOn(); // 开启体积光照提升质感
    volumeProperty->SetAmbient(0.2);
    volumeProperty->SetDiffuse(0.7);
    volumeProperty->SetSpecular(0.3);

    vtkNew<vtkVolume> volume;
    volume->SetMapper(volumeMapper);
    volume->SetProperty(volumeProperty);

    // 分子球棍模型渲染管线
    vtkNew<vtkMoleculeMapper> molMapper;
    molMapper->SetInputData(reader->GetMolecule());
    molMapper->UseBallAndStickSettings(); // 启用球棍显示模式
    molMapper->SetAtomicRadiusScaleFactor(0.3); // 调整原子球显示大小
    molMapper->SetBondRadius(0.1); // 调整化学键显示粗细

    vtkNew<vtkActor> molActor;
    molActor->SetMapper(molMapper);

    // 渲染窗口与交互配置
    vtkNew<vtkNamedColors> colors;
    vtkNew<vtkRenderer> renderer;
    renderer->AddVolume(volume);
    renderer->AddActor(molActor);
    renderer->SetBackground(colors->GetColor3d("White").GetData());

    vtkNew<vtkRenderWindow> renderWindow;
    renderWindow->AddRenderer(renderer);
    renderWindow->SetSize(800, 600);
    renderWindow->SetWindowName("Cube Visualization");

    vtkNew<vtkRenderWindowInteractor> interactor;
    interactor->SetRenderWindow(renderWindow);
    vtkNew<vtkInteractorStyleTrackballCamera> style;
    interactor->SetInteractorStyle(style);

    interactor->Initialize();
    renderWindow->Render();
    interactor->Start();

    return EXIT_SUCCESS;
}

3. 效果调整说明

  • 若渲染颜色和预期不符,修改vtkColorTransferFunction的RGB节点参数即可匹配需要的配色
  • 若体渲染透明程度不符合预期,调整vtkPiecewiseFunction的节点值,控制不同标量值区域的透明度
  • 若分子模型尺寸不合适,修改SetAtomicRadiusScaleFactor和SetBondRadius的入参即可

4. 编译参考配置

CMakeLists.txt参考写法如下:

cmake_minimum_required(VERSION 3.15)
project(CubeViz)

find_package(VTK REQUIRED COMPONENTS
  IOImage
  RenderingVolume
  RenderingCore
  DomainsChemistry
  InteractionStyle
)

add_executable(CubeViz main.cpp)
target_link_libraries(CubeViz PRIVATE ${VTK_LIBRARIES})
vtk_module_autoinit(TARGETS CubeViz MODULES ${VTK_LIBRARIES})

内容的提问来源于stack exchange,提问作者user3175032

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 19:36:01