feat (WIP): base VTK visualisation of Eulerian flow fields

This commit is contained in:
2024-04-22 15:01:33 +02:00
parent 4cd545c788
commit 985fd6b9ed
4 changed files with 194 additions and 0 deletions

55
vtk/src/CMakeLists.txt Normal file
View File

@@ -0,0 +1,55 @@
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(VtkBase)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
find_package(VTK COMPONENTS
CommonColor
CommonCore
CommonDataModel
FiltersGeneral
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
CommonColor
FiltersGeometry
RenderingCore)
if (NOT VTK_FOUND)
message(FATAL_ERROR "VtkBase: Unable to find the VTK build folder.")
endif()
# netcdf setup
find_package(netCDF REQUIRED)
add_executable(VtkBase MACOSX_BUNDLE main.cpp )
execute_process(
COMMAND nc-config --includedir
OUTPUT_VARIABLE NETCDF_INCLUDE_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE
)
execute_process(
COMMAND ncxx4-config --libdir
OUTPUT_VARIABLE NETCDFCXX_LIB_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE
)
target_include_directories(VtkBase PUBLIC ${netCDF_INCLUDE_DIR})
find_library(NETCDF_LIB NAMES netcdf-cxx4 netcdf_c++4 PATHS ${NETCDFCXX_LIB_DIR} NO_DEFAULT_PATH)
# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
target_link_libraries(VtkBase ${NETCDF_LIB} ${VTK_LIBRARIES})
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS VtkBase
MODULES ${VTK_LIBRARIES}
)

94
vtk/src/main.cpp Normal file
View File

@@ -0,0 +1,94 @@
#include <vtkActor.h>
#include <vtkActor2D.h>
#include <vtkCamera.h>
#include <vtkDoubleArray.h>
#include <vtkMapper2D.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkPolyDataMapper2D.h>
#include <vtkProperty.h>
#include <vtkRectilinearGrid.h>
#include <vtkRectilinearGridGeometryFilter.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <netcdf>
#define NLATS 67
#define NLONS 116
using namespace std;
using namespace netCDF;
template <typename T>
vector<T> getVarVector(const NcVar &var) {
int length = 1;
for (NcDim dim : var.getDims()) {
length *= dim.getSize();
}
vector<T> vec(length);
var.getVar(vec.data());
return vec;
}
tuple<vector<int>, vector<double>, vector<double>> readGrid() {
netCDF::NcFile data("../../../../data/grid.h5", netCDF::NcFile::read);
multimap< string, NcVar > vars = data.getVars();
vector<int> time = getVarVector<int>(vars.find("times")->second);
vector<double> longitude = getVarVector<double>(vars.find("longitude")->second);
vector<double> latitude = getVarVector<double>(vars.find("latitude")->second);
return {time, latitude, longitude};
}
int main() {
vtkNew<vtkNamedColors> colors;
vtkNew<vtkDoubleArray> pCoords;
pCoords->SetNumberOfComponents(3);
pCoords->SetNumberOfTuples(NLATS*NLONS);
auto [times, lats, lons] = readGrid();
double i = 0;
for (double lat : lats) {
for (double lon : lons) {
pCoords->InsertTuple3(i++, 0, lat, lon);
}
}
vtkNew<vtkPoints> points;
points->SetData(pCoords);
vtkNew<vtkPolyData> polydata;
polydata->SetPoints(points);
vtkNew<vtkPolyDataMapper2D> mapper;
mapper->SetInputData(polydata);
mapper->SetScalarRange(0, 40);
vtkNew<vtkActor2D> actor;
actor->SetMapper(mapper);
vtkNew<vtkRenderer> ren;
ren->AddActor(actor);
ren->SetBackground(colors->GetColor3d("DarkSlateGray").GetData());
vtkNew<vtkRenderWindow> renWin;
renWin->AddRenderer(ren);
renWin->SetSize(900, 600);
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin);
iren->Start();
return EXIT_SUCCESS;
}