VTK 建模方法:建模基础

VTK 建模方法:建模基础

VTK 中模型的表达

  • 几何结构:存储在数据的 Points 里;
  • 拓扑结构:存储在数据的 Cells 里。

VTK 中常用的 Cell 类型:

在这里插入图片描述

实例1:自定义 vtkPolyData

设置好 Points 和 Cells 后,添加到 vtkPolyData 即可。

代码:

#include "VTKModeling.h"

#include <vtkPoints.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>

VTKModeling::VTKModeling(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);

    _pVTKWidget = new QVTKOpenGLNativeWidget();
    this->setCentralWidget(_pVTKWidget);
    vtkNew<vtkRenderer> renderer;
    this->_pVTKWidget->renderWindow()->AddRenderer(renderer);
    this->_pVTKWidget->renderWindow()->Render();

    vtkNew<vtkPoints> points;
    points->InsertNextPoint(0, 0, 0);
    points->InsertNextPoint(0, 2, 0);
    points->InsertNextPoint(2, 4, 0);
    points->InsertNextPoint(4, 4, 0);

    vtkNew<vtkCellArray> lines;
    lines->InsertNextCell(4);
    lines->InsertCellPoint(0);
    lines->InsertCellPoint(1);
    lines->InsertCellPoint(2);
    lines->InsertCellPoint(3);

    vtkNew<vtkPolyData> profile;
    profile->SetPoints(points);
    profile->SetLines(lines);

    vtkNew<vtkPolyDataMapper> mapper;
    mapper->SetInputData(profile);

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

    renderer->AddActor(actor);
}

VTKModeling::~VTKModeling()
{}

运行结果:

在这里插入图片描述

实例2:vtkTubeFilter

给之前代码中的 profile 加上一个 vtkTubeFilter,可以把 line 转换成一个 tube。

新增代码:

    vtkNew<vtkTubeFilter> tubeFilter;
    tubeFilter->SetInputData(profile);
    tubeFilter->SetRadius(0.5);
    tubeFilter->SetNumberOfSides(12);

    vtkNew<vtkPolyDataMapper> mapper;
    mapper->SetInputConnection(tubeFilter->GetOutputPort());

运行结果:

在这里插入图片描述

实例3:vtkImplicitModeller

vtkImplicitModeller类计算从线(profile)到输出(vtkImageData)中的点的距离(取到任何线的最近距离),并将此距离分配为数据集中每个点的标量值。然后将输出传递给vtkContourFilter,它生成一个多边形等值面。

新增代码:

	vtkNew<vtkImplicitModeller> impModeller;
	impModeller->SetInputData(profile);
	impModeller->SetSampleDimensions(100, 100, 100);
	impModeller->SetMaximumDistance(0.25);
	impModeller->SetModelBounds(-10, 10, -10, 10, -10, 10);
	vtkNew<vtkContourFilter> contour;
	contour->SetInputConnection(impModeller->GetOutputPort());
	contour->SetValue(0, 0.5);

	vtkNew<vtkPolyDataMapper> contourMapper;
	contourMapper->SetInputConnection(contour->GetOutputPort());

	vtkNew<vtkActor> contourActor;
	contourActor->SetMapper(contourMapper);
	contourActor->SetPosition(5, 0, 0);

	renderer->AddActor(contourActor);

运行结果:

在这里插入图片描述

按W切换成线框模式,可以看出vtkTubeFilter的采样很简单,三角面较大;而我们对vtkImplicitModeller设置的采样率生效了,等值面还包裹了线的两端:

在这里插入图片描述

实例4:vtkRegularPolygonSource

vtkRegularPolygonSource类用于生成规则多边形数据源,设置原点、半径和边数,就可以近似地模拟出一个圆面。用vtkImplicitModeller类计算从圆面(circle)到输出(vtkImageData)中的点的距离(取到任何线的最近距离),并将此距离分配为数据集中每个点的标量值。然后将输出传递给vtkContourFilter,它生成一个多边形等值面。

新增代码:

	// Circle
	vtkNew<vtkRegularPolygonSource> circle;
	circle->GeneratePolygonOn(); // 生成一个规则的多边形,不然只是折线
	circle->SetCenter(0, 0, 0);
	circle->SetRadius(2);
	circle->SetNumberOfSides(50);
	vtkNew<vtkImplicitModeller> circleImpModeller;
	circleImpModeller->SetInputConnection(circle->GetOutputPort());
	circleImpModeller->SetSampleDimensions(100, 100, 8);
	circleImpModeller->SetModelBounds(-3, 3, -3, 3, -0.2, 0.2);
	vtkNew<vtkContourFilter> circleContour;
	circleContour->SetInputConnection(circleImpModeller->GetOutputPort());
	circleContour->SetValue(0, 0.2);

	vtkNew<vtkPolyDataMapper> circleMapper;
	circleMapper->SetInputConnection(circleContour->GetOutputPort());

	vtkNew<vtkActor> circleActor;
	circleActor->SetMapper(circleMapper);
	circleActor->SetPosition(12, 0, 0);

	renderer->AddActor(circleActor);

运行结果:

在这里插入图片描述

按W进入线框模式,放大看看圆面的网格:

在这里插入图片描述

实例5:vtkWarpTo

vtkWarpTo类可以设置点和比例,通过朝着该点弯曲来修改点坐标。

新增代码:

	vtkNew<vtkWarpTo> circleWarpTo;
	circleWarpTo->SetInputConnection(circleContour->GetOutputPort());
	circleWarpTo->SetPosition(0, 0, 5);
	circleWarpTo->SetScaleFactor(1);
	circleWarpTo->AbsoluteOn();

效果:

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

UestcXiye

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

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

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

打赏作者

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

抵扣说明:

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

余额充值