VTK 建模方法:建模基础
- VTK 建模方法:建模基础
- VTK 中模型的表达
- 实例1:自定义 vtkPolyData
- 实例2:vtkTubeFilter
- 实例3:vtkImplicitModeller
- 实例4:vtkRegularPolygonSource
- 实例5:vtkWarpTo
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,它生成一个多边形等值面。
新增代码:
// CirclevtkNew<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();
效果: