C++ VTK 的可视化方法:流线、流管、流面、流带 - #include
VTK 的可视化方法:流线、流管、流面、流带
VTK 的可视化方法:流线、流管、流面、流带三种相似的可视化方法流线生成使用的类实例:单一流线实例:流管实例:多条流线实例:流面实例:流带完整代码
VTK 的可视化方法:流线、流管、流面、流带
2维流线示例:
本文章主要讲解3维的流线、流管、流面、流带的构造方法。
三种相似的可视化方法
流线(Streamlines):每个点速度切线方向连成的线。迹线(Pathlines):粒子实际的轨迹线。脉线(Streaklines):连续时刻出发的粒子在某一时刻的连线。
流线生成使用的类
vtkRungeKutta4:四阶龙格库塔 (Runge-Kutta) 求解微分。vtkStreamTracer:通过整合矢量场生成流线。
实例:单一流线
完整代码:
#include "VTKStreamline.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
VTKStreamline::VTKStreamline(QWidget* parent)
: QMainWindow(parent)
{
ui.setupUi(this);
_pVTKWidget = new QVTKOpenGLNativeWidget();
this->setCentralWidget(_pVTKWidget);
// this->showMaximized();
// 1. generate data
// vtkSmartPointercone = vtkSmartPointer::New();
// or, read data
// vtkMultiBlockPLOT3DReader 是一个读取器对象,用于读取 PLOT3D 格式的文件并在输出时生成结构化网格
vtkSmartPointerplot3dReader = vtkSmartPointer::New();
plot3dReader->SetXYZFileName("combxyz.bin");
plot3dReader->SetQFileName("combq.bin");
plot3dReader->SetScalarFunctionNumber(100);
plot3dReader->SetVectorFunctionNumber(202);
qDebug() << plot3dReader->GetOutput()->GetNumberOfBlocks(); // 0
// 反向更新管线
plot3dReader->Update();
qDebug() << plot3dReader->GetOutput()->GetNumberOfBlocks(); // 1
vtkDataSet* plot3dOutput = (vtkDataSet*)(plot3dReader->GetOutput()->GetBlock(0));
// 四阶龙格库塔 (Runge-Kutta) 求解微分
vtkSmartPointerinteg = vtkSmartPointer::New();
// 通过整合矢量场生成流线
vtkSmartPointerstreamer = vtkSmartPointer::New();
streamer->SetIntegrator(integ);
streamer->SetInputData(plot3dOutput);
streamer->SetStartPosition(15, 5, 32);
streamer->SetMaximumPropagation(100);
streamer->SetInitialIntegreationStep(0.1);
streamer->SetIntegreationDirectionToBackward();
// 2. filter
// 产生结构化栅格边界的一个线轮廓
vtkSmartPointeroutline = vtkSmartPointer::New();
outline->SetInputData(plot3dOutput);
// 3. mapper
vtkSmartPointeroutlineMapper = vtkSmartPointer::New();
vtkSmartPointersingleMapper = vtkSmartPointer::New();
singleMapper->SetScalarRange(plot3dOutput->GetPointData()->GetScalars()->GetRange());
// 4. actor
vtkSmartPointeroutlineActor = vtkSmartPointer::New();
vtkSmartPointersingleActor = vtkSmartPointer::New();
// 5. renderer
vtkSmartPointerrenderer = vtkSmartPointer::New();
renderer->SetBackground(0.3, 0.6, 0.3); // Background Color: Green
// 6. connect
outlineMapper->SetInputConnection(outline->GetOutputPort());
singleMapper->SetInputConnection(streamer->GetOutputPort());
outlineActor->SetMapper(outlineMapper);
singleActor->SetMapper(singleMapper);
renderer->AddActor(outlineActor);
renderer->AddActor(singleActor);
this->_pVTKWidget->renderWindow()->AddRenderer(renderer);
this->_pVTKWidget->renderWindow()->Render();
}
VTKStreamline::~VTKStreamline()
{}
运行效果:
实例:流管
我们再加上一个过滤器,把流线变成流管:
vtkSmartPointerstreamTube = vtkSmartPointer::New();
streamTube->SetInputConnection(streamer->GetOutputPort());
streamTube->SetRadius(0.06);
streamTube->SetNumberOfSides(12);
流管实际上是用一个圆柱面包裹住流线,流线依旧存在。这样的显示效果会更好一点:
实例:多条流线
在之前的代码中,新增:
vtkSmartPointerseeds = vtkSmartPointer::New();
// 设置线段的端点
seeds->SetPoint1(15, -5, 32);
seeds->SetPoint2(15, 5, 32);
seeds->SetResolution(21);
vtkSmartPointerstreamer2 = vtkSmartPointer::New();
streamer2->SetIntegrator(integ);
streamer2->SetInputData(plot3dOutput);
// streamer2->SetStartPosition(15, 5, 32);
streamer2->SetMaximumPropagation(100);
streamer2->SetInitialIntegreationStep(0.1);
streamer2->SetIntegreationDirectionToBackward();
streamer2->SetSourceConnection(seeds->GetOutputPort());
vtkSmartPointermultipleMapper = vtkSmartPointer::New();
multipleMapper->SetScalarRange(plot3dOutput->GetPointData()->GetScalars()->GetRange());
vtkSmartPointermultipleActor = vtkSmartPointer::New();
multipleMapper->SetInputConnection(streamer2->GetOutputPort());
multipleActor->SetMapper(multipleMapper);
renderer->AddActor(multipleActor);
实例:流面
新增一个过滤器 vtkRuledSurfaceFilter,把多条流线合并成一个流面: 拼多多帮助力入口
vtkSmartPointerscalarSurface = vtkSmartPointer::New();
scalarSurface->SetInputConnection(streamer2->GetOutputPort());
// 设置生成方法
scalarSurface->SetRuledModeToPointWalk();
// multipleMapper->SetInputConnection(streamer2->GetOutputPort());
multipleMapper->SetInputConnection(scalarSurface->GetOutputPort());
运行结果:
实例:流带
流带其实是按每条流线拓展而成的一条条带状的面,比起流面,更能便于展示走势。
我们只需要在前面的代码中新增一行代码:
vtkSmartPointerscalarSurface = vtkSmartPointer::New();
scalarSurface->SetInputConnection(streamer2->GetOutputPort());
// 设置生成方法
scalarSurface->SetRuledModeToPointWalk();
scalarSurface->SetOnRatio(2); // 新增代码
运行结果:
完整代码
#include "VTKStreamline.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
VTKStreamline::VTKStreamline(QWidget* parent)
: QMainWindow(parent)
{
ui.setupUi(this);
_pVTKWidget = new QVTKOpenGLNativeWidget();
this->setCentralWidget(_pVTKWidget);
// this->showMaximized();
// 1. generate data
vtkSmartPointerseeds = vtkSmartPointer::New();
// 设置线段的端点
seeds->SetPoint1(15, -5, 32);
seeds->SetPoint2(15, 5, 32);
seeds->SetResolution(21);
// or, read data
// vtkMultiBlockPLOT3DReader 是一个读取器对象,用于读取 PLOT3D 格式的文件并在输出时生成结构化网格
vtkSmartPointerplot3dReader = vtkSmartPointer::New();
plot3dReader->SetXYZFileName("combxyz.bin");
plot3dReader->SetQFileName("combq.bin");
plot3dReader->SetScalarFunctionNumber(100);
plot3dReader->SetVectorFunctionNumber(202);
qDebug() << plot3dReader->GetOutput()->GetNumberOfBlocks(); // 0
// 反向更新管线
plot3dReader->Update();
qDebug() << plot3dReader->GetOutput()->GetNumberOfBlocks(); // 1
vtkDataSet* plot3dOutput = (vtkDataSet*)(plot3dReader->GetOutput()->GetBlock(0));
// 四阶龙格库塔 (Runge-Kutta) 求解微分
vtkSmartPointerinteg = vtkSmartPointer::New();
// 通过整合矢量场生成流线
vtkSmartPointerstreamer = vtkSmartPointer::New();
streamer->SetIntegrator(integ);
streamer->SetInputData(plot3dOutput);
streamer->SetStartPosition(15, 5, 32);
streamer->SetMaximumPropagation(100);
streamer->SetInitialIntegreationStep(0.1);
streamer->SetIntegreationDirectionToBackward();
vtkSmartPointerstreamer2 = vtkSmartPointer::New();
streamer2->SetIntegrator(integ);
streamer2->SetInputData(plot3dOutput);
// streamer2->SetStartPosition(15, 5, 32);
streamer2->SetMaximumPropagation(100);
streamer2->SetInitialIntegreationStep(0.1);
streamer2->SetIntegreationDirectionToBackward();
streamer2->SetSourceConnection(seeds->GetOutputPort());
// 2. filter
// 产生结构化栅格边界的一个线轮廓
vtkSmartPointeroutline = vtkSmartPointer::New();
outline->SetInputData(plot3dOutput);
vtkSmartPointerstreamTube = vtkSmartPointer::New();
streamTube->SetInputConnection(streamer->GetOutputPort());
streamTube->SetRadius(0.06);
streamTube->SetNumberOfSides(12);
vtkSmartPointerscalarSurface = vtkSmartPointer::New();
scalarSurface->SetInputConnection(streamer2->GetOutputPort());
// 设置生成方法
scalarSurface->SetRuledModeToPointWalk();
scalarSurface->SetOnRatio(2);
// 3. mapper
vtkSmartPointeroutlineMapper = vtkSmartPointer::New();
vtkSmartPointersingleMapper = vtkSmartPointer::New();
singleMapper->SetScalarRange(plot3dOutput->GetPointData()->GetScalars()->GetRange());
vtkSmartPointermultipleMapper = vtkSmartPointer::New();
multipleMapper->SetScalarRange(plot3dOutput->GetPointData()->GetScalars()->GetRange());
// 4. actor
vtkSmartPointeroutlineActor = vtkSmartPointer::New();
vtkSmartPointersingleActor = vtkSmartPointer::New();
vtkSmartPointermultipleActor = vtkSmartPointer::New();
// 5. renderer
vtkSmartPointerrenderer = vtkSmartPointer::New();
renderer->SetBackground(0.3, 0.6, 0.3); // Background Color: Green
// 6. connect
outlineMapper->SetInputConnection(outline->GetOutputPort());
singleMapper->SetInputConnection(streamTube->GetOutputPort());
// multipleMapper->SetInputConnection(streamer2->GetOutputPort());
multipleMapper->SetInputConnection(scalarSurface->GetOutputPort());
outlineActor->SetMapper(outlineMapper);
singleActor->SetMapper(singleMapper);
multipleActor->SetMapper(multipleMapper);
renderer->AddActor(outlineActor);
renderer->AddActor(singleActor);
renderer->AddActor(multipleActor);
this->_pVTKWidget->renderWindow()->AddRenderer(renderer);
this->_pVTKWidget->renderWindow()->Render();
}
VTKStreamline::~VTKStreamline()
{}
文章链接