From 55171d3b140f188645a10eefdc58cdb9b5f0b81e Mon Sep 17 00:00:00 2001 From: Achim Gsell Date: Wed, 30 Jan 2013 16:06:47 +0000 Subject: [PATCH] VTK to H5grid converter added --- .gitattributes | 2 + configure.ac | 24 ++++ tools/Makefile.am | 6 +- tools/vtk2h5grid/Makefile.am | 16 +++ tools/vtk2h5grid/vtk2h5grid.cc | 203 +++++++++++++++++++++++++++++++++ 5 files changed, 250 insertions(+), 1 deletion(-) create mode 100644 tools/vtk2h5grid/Makefile.am create mode 100644 tools/vtk2h5grid/vtk2h5grid.cc diff --git a/.gitattributes b/.gitattributes index ea43885..fd0b853 100644 --- a/.gitattributes +++ b/.gitattributes @@ -566,6 +566,8 @@ tools/h5hutcc.in -text tools/h5pAttrib.cc -text tools/h5pToGNUplot.cc -text tools/homdynToH5p.cc -text +tools/vtk2h5grid/Makefile.am -text +tools/vtk2h5grid/vtk2h5grid.cc -text visit_plugins/databases/H5Part/.depend -text visit_plugins/databases/H5Part/Makefile -text visit_plugins/databases/H5Part/README.txt -text diff --git a/configure.ac b/configure.ac index 4131aa8..d8e14bb 100644 --- a/configure.ac +++ b/configure.ac @@ -49,6 +49,13 @@ AC_ARG_ENABLE( [Compile the MPI/IO interface [default=no]])], [USE_PARALLEL=$enableval]) +AC_ARG_ENABLE( + [vtkconverter], + [AS_HELP_STRING([--enable-vtkconverter], + [Compile the vtk to H5hut converter for grids [default=no]])], + [ENABLE_VTKCONVERTER=$enableval]) +AM_CONDITIONAL([ENABLE_VTKCONVERTER], [test "$ENABLE_VTKCONVERTER" = "yes"]) + AC_ARG_WITH( [hdf5], [AC_HELP_STRING([--with-hdf5], @@ -67,6 +74,12 @@ AC_ARG_WITH( [path to lustre user API [default=""]])], [LUSTREPATH=$withval], [LUSTREPATH=""]) +AC_ARG_WITH( + [vtk], + [AS_HELP_STRING([--with-vtk], + [path to VTK installation [default=""]])], + [VTK_PREFIX=$withval], [VTK_PREFIX=""]) + ############################################################################### ############### PATH SERACH FUNCTION - to be used later... #################### ############################################################################### @@ -364,6 +377,16 @@ fi LIBS="$LIBS -lz -lm" +############################################################################### +# TOOLS +AC_MSG_CHECKING([if we have to compile the VTK to H5hut grid converter]) +if test "X$ENABLE_VTKCONVERTER" = "Xyes"; then + AC_MSG_RESULT([yes]) + # :TODO: add test whether we can compile/link a prog with vtk +else + AC_MSG_RESULT([no]) +fi + ############################################################################### ############## EXPORTING VARIABLES & CREATING OUTPUT FILES #################### ############################################################################### @@ -400,6 +423,7 @@ AC_CONFIG_FILES([ test/H5Fed/Makefile tools/Makefile tools/h5hutcc + tools/vtk2h5grid/Makefile ]) AC_OUTPUT diff --git a/tools/Makefile.am b/tools/Makefile.am index b8337d5..09f5dec 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -1,6 +1,10 @@ # tools level Makefile.am -OBJEXT=o +SUBDIRS = + +if ENABLE_VTKCONVERTER +SUBDIRS += vtk2h5grid +endif AM_LDFLAGS = -L../src/lib @LDFLAGS@ LIBS = -lH5hutC -lH5hut @LIBS@ diff --git a/tools/vtk2h5grid/Makefile.am b/tools/vtk2h5grid/Makefile.am new file mode 100644 index 0000000..19ceedb --- /dev/null +++ b/tools/vtk2h5grid/Makefile.am @@ -0,0 +1,16 @@ +AM_CPPFLAGS = -I${abs_top_builddir}/src/include -Wno-deprecated +FFLAGS += -cpp $(AM_CPPFLAGS) +LDFLAGS += -L${abs_top_builddir}/src/lib + +LDADD = + +LDADD += -lH5hut -lH5hutC -lvtkCommon -lvtkIO + + +bin_PROGRAMS = + +bin_PROGRAMS += vtk2h5grid +vtk2h5grid_SOURCES = vtk2h5grid.cc + +distclean-local: + $(RM) *~ diff --git a/tools/vtk2h5grid/vtk2h5grid.cc b/tools/vtk2h5grid/vtk2h5grid.cc new file mode 100644 index 0000000..a86121d --- /dev/null +++ b/tools/vtk2h5grid/vtk2h5grid.cc @@ -0,0 +1,203 @@ +#include +#include +#include +#include +#include + +#include + + +#include +#include +#include "vtkUnstructuredGrid.h" +#include "vtkUnstructuredGridReader.h" + +#include "H5hut.h" + +const char* version = "0.1.0"; +int convert_boundary = 1; +int convert_volume = 0; +const struct option longopts[] = { + {"version", no_argument, 0, 'v'}, + {"help", no_argument, 0, 'h'}, + {"boundary", no_argument, &convert_boundary, 1}, + {"volume", no_argument, &convert_volume, 1}, + {"no-boundary", no_argument, &convert_boundary, 0}, + {"no-volume", no_argument, &convert_volume, 0}, + {0,0,0,0}, +}; + +typedef std::map IdMap; + +static void +usage ( + char* cmd + ) { + std::cout << std::endl << "Usage: " << std::endl; + std::cout << " " << cmd << " [OPTIONS] FILE..." << std::endl << std::endl; + std::cout << "Options:" << std::endl; + std::cout << " --(no-)boundary do (not) convert boundary triangles." << std::endl; + std::cout << " default is yes" << std::endl; + std::cout << " --(no-)volume do (not) convert volume mesh." << std::endl; + std::cout << " default is no" << std::endl; + std::cout << std::endl; + exit (1); +} + +static void +print_version ( + char* cmd + ) { + std::cout << "Version: " << cmd << " " << version << std::endl; + exit (1); +} + +static int +init ( + int argc, + char* argv[] + ) { + int index; + int iarg = 0; + + //turn off getopt error message + opterr = 1; + + while(iarg != -1) { + iarg = getopt_long(argc, argv, "vh?", longopts, &index); + + switch (iarg) { + case 'h': + case '?': + usage (argv[0]); + break; + + case 'v': + print_version (argv[0]); + break; + } + } + return argc - optind; +} + +void +convert_vtk2h5grid ( + vtkUnstructuredGrid* vtk_grid, + h5t_mesh_t* h5_grid, + int cell_type + ) { + IdMap idmap; + vtkIdType num_vtk_cells = vtk_grid->GetNumberOfCells (); + h5_loc_idx_t (*cells)[4] = new h5_loc_idx_t[num_vtk_cells][4]; + int h5_cell_idx = 0; + int h5_vertex_idx = 0; + H5FedBeginStoreVertices (h5_grid, num_vtk_cells); + for (vtkIdType vtk_cell_id = 0; vtk_cell_id < num_vtk_cells; vtk_cell_id++) { + if (vtk_grid->GetCellType (vtk_cell_id) != cell_type) + continue; + + vtkIdType num_pts; + vtkIdType* pts; + vtk_grid->GetCellPoints (vtk_cell_id, num_pts, pts); + for (vtkIdType i = 0; i < num_pts; i++) { + IdMap::iterator it = idmap.find (pts[i]); + if (it == idmap.end ()) { + // add point to H5hut mesh + double pt[3]; + vtk_grid->GetPoint (pts[i], pt); + H5FedStoreVertex (h5_grid, -1, pt); + // map pt index in vtk file to pt index in H5hut file + idmap.insert (IdMap::value_type (pts[i], h5_vertex_idx)); + cells[h5_cell_idx][i] = h5_vertex_idx++; + } else { + cells[h5_cell_idx][i] = it->second; + } + } + h5_cell_idx++; + } + H5FedEndStoreVertices (h5_grid); + int num_h5_vertices = h5_vertex_idx; + int num_h5_cells = h5_cell_idx; + cout << " number of points in mesh: " << num_h5_vertices << endl; + cout << " number of cells in mesh: " << num_h5_cells << endl; + // add tetrahedra to H5hut file + H5FedBeginStoreElements (h5_grid, num_vtk_cells); + for (int i = 0; i < num_h5_cells; i++) { + H5FedStoreElement (h5_grid, cells[i]); + } + H5FedEndStoreElements (h5_grid); + delete cells; +} + +h5_err_t +H5Errorhandler ( + const char* fmt, + va_list ap + ) { + vfprintf (stderr, fmt, ap); + exit (42); + return -2; +} + +int +main ( + int argc, + char* argv[] + ) { + + argc = init (argc, argv); + if (argc == 0) { + std::cout << "No file(s) to convert?!" << std::endl; + usage (argv[0]); + } + argv += optind; + H5SetErrorHandler (H5Errorhandler); + for (int i = 0; i < argc; i++) { + std::string vtk_filename = argv[i]; + std::string h5grid_filename = vtk_filename.substr (0, vtk_filename.find_last_of ('.')) + ".h5"; + struct stat st; + if (stat (vtk_filename.c_str(), &st) < 0) { + perror (vtk_filename.c_str()); + exit (1); + } + if (!(st.st_mode & S_IRUSR)) { + std::cerr << vtk_filename << ": not readable" << std::endl; + exit (1); + } + if (stat (h5grid_filename.c_str(), &st) == 0) { + std::cerr << h5grid_filename << ": already exits" << std::endl; + exit (1); + } + if (errno != ENOENT) { + perror (h5grid_filename.c_str()); + exit (1); + } + std::cout << vtk_filename << ": converting ...." << std::endl; + + // read vtk file + vtkSmartPointer reader = + vtkSmartPointer::New (); + reader->SetFileName (vtk_filename.c_str ()); + reader->Update (); + vtkUnstructuredGrid* vtk_grid = reader->GetOutput (); + + // open new H5hut file + h5_file_t* f = H5OpenFile (h5grid_filename.c_str(), H5_O_WRONLY, 0); + + h5t_mesh_t* h5_grid; + if (convert_boundary) { + // add boundary mesh + H5FedAddTriangleMesh (f, "0", &h5_grid); + convert_vtk2h5grid (vtk_grid, h5_grid, VTK_TRIANGLE); + H5FedCloseMesh (h5_grid); + } + if (convert_volume) { + // add volume mesh + H5FedAddTetrahedralMesh (f, "0", &h5_grid); + convert_vtk2h5grid (vtk_grid, h5_grid, VTK_TETRA); + H5FedCloseMesh (h5_grid); + } + H5CloseFile (f); + } + return 0; +}