vtkUnstructuredGrid 显示 HDF5 数据
Reference
添加 field (dataset) 数据
第一步,创建VTK的field
数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
vtkSmartPointer<vtkDoubleArray> fieldDataArray = vtkSmartPointer<vtkDoubleArray>::New();
fieldDataArray->SetNumberOfComponents(1); // assuming scalar data
// Assuming "dataset" is your std::map<std::string, std::vector<double>>
for (const auto& pair : dataset) {
const std::string& fieldName = pair.first;
const std::vector<double>& fieldValues = pair.second;
fieldDataArray->SetName(fieldName.c_str());
for (double value : fieldValues) {
fieldDataArray->InsertNextValue(value);
}
// Assuming "grid" is your vtkUnstructuredGrid object
grid->GetPointData()->AddArray(fieldDataArray);
}
- 根据
dataset
的属性field
是scalar
数据还是tensor
数据,设置SetNumberOfComponents
的参数; - 根据
dataset
的属性field
是scalar
数据还是tensor
数据,选择InsertNectValue
或者InsertNextTuple
; - 根据
dataset
的属性location type
属性是vetex
还是element
,grid
选取GetPointData
或者GetCellData
;
第二步,添加VTK field
数据到mapper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New();
mapper->SetInputData(grid);
mapper->SetScalarModeToUsePointData(); // or SetScalarModeToUseCellData()
mapper->SelectColorArray("fieldName"); // replace "fieldName" with the name of the field you want to use for coloring
vtkSmartPointer<vtkLookupTable> lut = vtkSmartPointer<vtkLookupTable>::New();
lut->SetRange(minValue, maxValue); // set range according to your data
mapper->SetLookupTable(lut);
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
vtkSmartPointer<vtkScalarBarActor> scalarBar = vtkSmartPointer<vtkScalarBarActor>::New();
scalarBar->SetLookupTable(mapper->GetLookupTable());
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
renderer->AddActor2D(scalarBar);
- 根据
dataset
的属性location type
属性是vetex
还是element
,mapper
调用SetScalarModeToUsePointData
或者SetScalarModeToUseCellData
;
颜色转换的另一种方式
使用vtkColorTranslationFunction
转换:
1
2
3
4
5
6
// Create a vtkColorTransferFunction to map scalar values to colors
vtkSmartPointer<vtkColorTransferFunction> colorTransferFunction = vtkSmartPointer<vtkColorTransferFunction>::New();
colorTransferFunction->AddRGBPoint(minScalarValue, r, g, b); // Add as many points as needed
// Set the color transfer function for the mapper
mapper->SetLookupTable(colorTransferFunction);
本文由作者按照 CC BY 4.0 进行授权