From 1b5d1e89ed632fec08e305260af5502f2931696e Mon Sep 17 00:00:00 2001 From: hxf0223 Date: Sat, 11 May 2024 15:03:45 +0800 Subject: [PATCH] add vtk notes --- ...how-hdf5-data-using-vtkUnstructuredGrid.md | 82 +++++++++++++++++++ _posts/2023-07-02-VTK-official-examples.md | 31 +++++++ _posts/2023-07-09-vtk-notes.md | 18 ++++ 3 files changed, 131 insertions(+) create mode 100644 _posts/2023-06-22-show-hdf5-data-using-vtkUnstructuredGrid.md create mode 100644 _posts/2023-07-02-VTK-official-examples.md create mode 100644 _posts/2023-07-09-vtk-notes.md diff --git a/_posts/2023-06-22-show-hdf5-data-using-vtkUnstructuredGrid.md b/_posts/2023-06-22-show-hdf5-data-using-vtkUnstructuredGrid.md new file mode 100644 index 0000000..febf40f --- /dev/null +++ b/_posts/2023-06-22-show-hdf5-data-using-vtkUnstructuredGrid.md @@ -0,0 +1,82 @@ +--- +title: vtkUnstructuredGrid 显示 HDF5 数据 +date: 2023-06-22 +0800 # 2022-01-01 13:14:15 +0800 只写日期也行;不写秒也行;这样也行 2022-03-09T00:55:42+08:00 +categories: [VTK] +tags: [VTK] # TAG names should always be lowercase + +# 以下默认false +math: true +mermaid: true +# pin: true +--- + +# vtkUnstructuredGrid 显示 HDF5 数据 + +## Reference + +* [VTK examples](https://github.com/Kitware/vtk-examples/blob/c1c8af1e70708e65d6fa4bf69fab814f03c99dc2/src/Cxx/GeometricObjects/Hexahedron.cxx) + +## 添加 field (dataset) 数据 + +### 第一步,创建VTK的`field`数据 + +```C++ +vtkSmartPointer fieldDataArray = vtkSmartPointer::New(); +fieldDataArray->SetNumberOfComponents(1); // assuming scalar data + +// Assuming "dataset" is your std::map> +for (const auto& pair : dataset) { + const std::string& fieldName = pair.first; + const std::vector& 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` + +```C++ +vtkSmartPointer mapper = vtkSmartPointer::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 lut = vtkSmartPointer::New(); +lut->SetRange(minValue, maxValue); // set range according to your data +mapper->SetLookupTable(lut); + +vtkSmartPointer actor = vtkSmartPointer::New(); +actor->SetMapper(mapper); + +vtkSmartPointer scalarBar = vtkSmartPointer::New(); +scalarBar->SetLookupTable(mapper->GetLookupTable()); + +vtkSmartPointer renderer = vtkSmartPointer::New(); +renderer->AddActor(actor); +renderer->AddActor2D(scalarBar); +``` + +* 根据`dataset`的属性`location type`属性是`vetex`还是`element`,`mapper`调用`SetScalarModeToUsePointData`或者`SetScalarModeToUseCellData`; + +## 颜色转换的另一种方式 + +使用`vtkColorTranslationFunction`转换: + +```C++ +// Create a vtkColorTransferFunction to map scalar values to colors +vtkSmartPointer colorTransferFunction = vtkSmartPointer::New(); +colorTransferFunction->AddRGBPoint(minScalarValue, r, g, b); // Add as many points as needed + +// Set the color transfer function for the mapper +mapper->SetLookupTable(colorTransferFunction); +``` diff --git a/_posts/2023-07-02-VTK-official-examples.md b/_posts/2023-07-02-VTK-official-examples.md new file mode 100644 index 0000000..c4c26f4 --- /dev/null +++ b/_posts/2023-07-02-VTK-official-examples.md @@ -0,0 +1,31 @@ +--- +title: VTK 官方examples记录,及优秀VTK blog列表 +date: 2023-05-24 +0800 # 2022-01-01 13:14:15 +0800 只写日期也行;不写秒也行;这样也行 2022-03-09T00:55:42+08:00 +categories: [VTK] +tags: [VTK] # TAG names should always be lowercase + +# 以下默认false +math: true +mermaid: true +# pin: true +--- + +# VTK 官方examples记录,及优秀VTK blog列表 + +## 概览列表 + +关于CXX的示例说明,在官方examples代码的相关README里面有简单介绍,路径:src/Cxx.md + +* github `HDF5` & `TDR`代码 [JosefWeinbub](https://github.com/JosefWeinbub?tab=stars) +* C++示例代码列表 [Cxx](https://examples.vtk.org/site/Cxx/) +* 演示`vtkUnstructureGrid`及mesh显示(`EdgeVisibilityOn`) [UGrid](https://examples.vtk.org/site/Cxx/UnstructuredGrid/UGrid/) + +## Color a mesh by dotting a vector from the origin to each point with a specified vector +* [SimpleElevationFilter](https://examples.vtk.org/site/Cxx/Meshes/SimpleElevationFilter/) + +## Vector field -- 显示矢量场箭头 +* [VectorField](https://examples.vtk.org/site/Cxx/Visualization/VectorField/) +* [VectorFieldNonZeroExtraction](https://examples.vtk.org/site/Cxx/Filtering/VectorFieldNonZeroExtraction/) + +## VTK blog +* [VTK入门范例2](https://www.michaelapp.com/posts/2019/2019-03-20-VTK%E5%85%A5%E9%97%A8%E8%8C%83%E4%BE%8B2/) diff --git a/_posts/2023-07-09-vtk-notes.md b/_posts/2023-07-09-vtk-notes.md new file mode 100644 index 0000000..9b6d57a --- /dev/null +++ b/_posts/2023-07-09-vtk-notes.md @@ -0,0 +1,18 @@ +--- +title: VTK 笔记 +date: 2023-07-09 +0800 # 2022-01-01 13:14:15 +0800 只写日期也行;不写秒也行;这样也行 2022-03-09T00:55:42+08:00 +categories: [VTK] +tags: [VTK] # TAG names should always be lowercase + +# 以下默认false +math: true +mermaid: true +# pin: true +--- + +# VTK 笔记 + +## vtkUnstructuredGrid 添加数据 + +* SetScalar、Vector = AddArray + SetActiveScalar\Vector 类似问题,看源码,不同的filter会取不同的activeAttribute,具体设置哪个active还是取决于filter +* setActive 不是2选一的关系