1.在VTK自带的vtkStructuredGrid数据文件combq.bin和combxyz.bin
2.文件读取代码如下:
//读取数据文件
Create(vtkMultiBlockPLOT3DReader, reader);
reader->SetXYZFileName("G:/Temp/vtkTest/combxyz.bin");
reader->SetQFileName("G:/Temp/vtkTest/combq.bin");
reader->SetScalarFunctionNumber(100);//100代表密度数据
reader->SetVectorFunctionNumber(202);//202代表冲量数据
reader->Update();
vtkStructuredGrid *object = (vtkStructuredGrid *)reader->GetOutput()->GetBlock(0);
3.获得一个vtkStructuredGrid数据,带有标量数据和矢量数据,下面的代码提取一个维度面
//提取面
Create(vtkStructuredGridGeometryFilter, plane);
plane->SetInputData(object);
plane->SetExtent(0, 100, 0, 100, 7, 7);
数据的实际维度是57 33 2,可以用下面的代码查看
int extends[6]={0};
object->GetExtent(extends);
这里维度0 100 0 100 7 7,表示提取Z维度7这个面,XY维度数据设置大了没事,会自动裁剪。对于不知道数据维度的数据,就要设置一个大点的值。从下图可以明显看出Z维度7的含义。
4.将从文件中提取的数据保存到txt文件中,观察一下数据有哪些
Create(vtkStructuredGridWriter,writer);
writer->SetInputData(object);
writer->SetFileName("data.txt");
writer->Write();
4.1 文件开头如下,数据点的数据量就是维数的乘积。
4.2 搜索 POINT_DATA来到点数据
4.3 简单计算一下就能明白上面是数据点位,下面是数据标量值。
5.完整代码,数据文件已绑定上传
#include<vtkProperty.h>
#include<vtkAxesActor.h>
#include<vtkLookupTable.h>
#include<vtkDataSetMapper.h>
#include<vtkStructuredGrid.h>
#include<vtkMultiBlockDataSet.h>
#include<vtkStructuredGridWriter.h>
#include<vtkMultiBlockPLOT3DReader.h>
#include<vtkOrientationMarkerWidget.h>
#include<vtkStructuredGridGeometryFilter.h>
#include<vtkNew.h>
#include<vtkRenderer.h>
#include<vtkAutoInit.h>
#include<vtkRenderWindow.h>
#include<vtkPolyDataMapper.h>
#include<vtkRenderWindowInteractor.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
VTK_MODULE_INIT(vtkRenderingFreeType)
#define Create(type,name) vtkNew<type> name
int main()
{
//读取数据文件
Create(vtkMultiBlockPLOT3DReader, reader);
reader->SetXYZFileName("G:/Temp/vtkTest/combxyz.bin");
reader->SetQFileName("G:/Temp/vtkTest/combq.bin");
reader->SetScalarFunctionNumber(100);//100代表密度数据
reader->SetVectorFunctionNumber(202);//202代表冲量数据
reader->Update();
vtkStructuredGrid *object = (vtkStructuredGrid *)reader->GetOutput()->GetBlock(0);
Create(vtkStructuredGridWriter, writer);
writer->SetInputData(object);
writer->SetFileName("data.txt");
writer->Write();
int extends[6] = {0};
object->GetExtent(extends);
//提取面
Create(vtkStructuredGridGeometryFilter, plane);
plane->SetInputData(object);
plane->SetExtent(0, 100, 0, 100, 7, 7);
//默认的颜色表
Create(vtkLookupTable, look);
look->SetNumberOfColors(256);
look->Build();
//可视化面
Create(vtkPolyDataMapper, planeMapper);
planeMapper->SetLookupTable(look);
planeMapper->SetInputConnection(plane->GetOutputPort());
planeMapper->SetScalarRange(object->GetScalarRange());
Create(vtkActor, planeActor);
planeActor->SetMapper(planeMapper);
//可视化整个数据
Create(vtkDataSetMapper, gridMapper);
gridMapper->SetInputData(object);
Create(vtkActor, gridActor);
gridActor->SetMapper(gridMapper);
gridActor->GetProperty()->SetEdgeVisibility(true);
gridActor->GetProperty()->SetRepresentationToWireframe();
Create(vtkRenderer, render);
render->AddActor(planeActor);
render->AddActor(gridActor);
Create(vtkRenderWindow, window);
window->AddRenderer(render);
Create(vtkRenderWindowInteractor, inter);
inter->SetRenderWindow(window);
//坐标系显示
Create(vtkAxesActor, axes);
Create(vtkOrientationMarkerWidget, widget);
widget->SetOrientationMarker(axes);
widget->SetInteractor(inter);
widget->SetEnabled(true);
widget->InteractiveOn();
render->ResetCamera();
inter->Start();
return 0;
}