文章

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的属性fieldscalar数据还是tensor数据,设置SetNumberOfComponents的参数;
  • 根据dataset的属性fieldscalar数据还是tensor数据,选择InsertNectValue或者InsertNextTuple
  • 根据dataset的属性location type属性是vetex还是elementgrid选取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还是elementmapper调用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 进行授权