第01章 20 使用vtkSphereSource和vtkPolyData逐级构建球体表面数据

使用 vtkPolyData 构建球体表面

cpp 复制代码
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>

int main() {
    // 创建一个球体源
    vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New();
    sphereSource->SetRadius(50.0);  // 设置球体半径为50
    sphereSource->SetThetaResolution(50);  // 设置经度方向的分辨率
    sphereSource->SetPhiResolution(50);    // 设置纬度方向的分辨率
    sphereSource->Update();

    // 创建Mapper
    vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection(sphereSource->GetOutputPort());

    // 创建Actor
    vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);
    actor->GetProperty()->SetColor(0.0, 0.0, 1.0);  // 设置颜色为蓝色

    // 创建Renderer和RenderWindow
    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);

    // 创建RenderWindowInteractor
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    // 将Actor添加到Renderer
    renderer->AddActor(actor);
    renderer->SetBackground(0.1, 0.2, 0.3);  // 设置背景颜色

    // 渲染和交互
    renderWindow->Render();
    renderWindowInteractor->Start();

    return 0;
}

代码说明:

  1. vtkSphereSource :用于生成球体的几何数据。
    • SetRadius(50.0):设置球体的半径为50。
    • SetThetaResolutionSetPhiResolution:设置球体的分辨率(即细分程度)。
  2. vtkPolyDataMapper:将几何数据映射为可渲染的对象。
  3. vtkActor:表示场景中的对象,设置颜色为蓝色。
  4. vtkRenderervtkRenderWindow:用于渲染和显示场景。
  5. vtkRenderWindowInteractor:提供交互功能(如旋转、缩放)。
相关推荐
mahuifa13 小时前
(46)VTK C++开发示例 --- 加载CML文件
c++·3d·vtk·cml
BoBoZz192 天前
AlignTwoPolyDatas 基于ICP算法的配准和相机视角切换
python·vtk·图形渲染·图形处理
BoBoZz192 天前
MarchingCubes 网格数据体素化并提取等值面
python·vtk·图形渲染·图形处理
BoBoZz192 天前
MarchingCases marchingcubes算法15种情况的展示
python·vtk·图形渲染·图形处理
BoBoZz192 天前
SmoothDiscreteMarchingCubes 多边形网格数据的平滑
python·vtk·图形渲染·图形处理
BoBoZz193 天前
Hello 隐式建模
python·vtk·图形渲染·图形处理
明洞日记3 天前
【VTK手册024】高效等值面提取:vtkFlyingEdges3D 详解与实战
c++·图像处理·vtk·图形渲染
BoBoZz195 天前
ExtractData 椭球的并集与函数的裁剪
python·vtk·图形渲染·图形处理
BoBoZz195 天前
DiscreteMarchingCubes离散等值面提取算法
python·vtk·图形渲染·图形处理
BoBoZz195 天前
ExtractLargestIsosurface 提取最大连通域
python·vtk·图形渲染·图形处理