diff --git a/config/t8_vtk.m4 b/config/t8_vtk.m4 index c830e9528d..e3abb32506 100644 --- a/config/t8_vtk.m4 +++ b/config/t8_vtk.m4 @@ -32,8 +32,9 @@ T8_ARG_WITH([vtk], T8_VTK_LIBS="-lvtkIOXML-$t8_vtk_version -lvtkCommonExecutionModel-$t8_vtk_version \ -lvtkCommonDataModel-$t8_vtk_version -lvtkIOXMLParser-$t8_vtk_version \ --lvtkIOParallelXML-$t8_vtk_version -lvtkParallelMPI-$t8_vtk_version \ --lvtkCommonCore-$t8_vtk_version -lvtkzlib-$t8_vtk_version -lvtksys-$t8_vtk_version" +-lvtkIOParallelXML-$t8_vtk_version -lvtkParallelMPI-$t8_vtk_version -lvtkIOPLY-$t8_vtk_version \ +-lvtkCommonCore-$t8_vtk_version -lvtkzlib-$t8_vtk_version -lvtksys-$t8_vtk_version \ +-lvtkIOGeometry-$t8_vtk_version -lvtkIOMovie-$t8_vtk_version" if test "x$T8_WITH_VTK" != xyes ; then T8_VTK_LIBS="$T8_WITH_VTK" dnl AC_MSG_ERROR([Please provide --with-vtk without arguments]) diff --git a/src/Makefile.am b/src/Makefile.am index a3141548e2..950f0f7ba6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -78,6 +78,7 @@ libt8_internal_headers = \ src/t8_cmesh/t8_cmesh_types.h src/t8_cmesh/t8_cmesh_partition.h \ src/t8_cmesh/t8_cmesh_refine.h src/t8_cmesh/t8_cmesh_copy.h \ src/t8_cmesh/t8_cmesh_offset.h \ + src/t8_cmesh/t8_cmesh_vtk_to_t8/t8_cmesh_vtk_polydata.hxx \ src/t8_forest/t8_forest_cxx.h \ src/t8_forest/t8_forest_ghost.h \ src/t8_forest/t8_forest_balance.h src/t8_forest/t8_forest_types.h \ @@ -117,7 +118,8 @@ libt8_compiled_sources = \ src/t8_forest/t8_forest_netcdf.cxx \ src/t8_element_shape.c \ src/t8_netcdf.c \ - src/t8_cmesh/t8_cmesh_testcases.c \ + src/t8_cmesh/t8_cmesh_testcases.c \ + src/t8_cmesh/t8_cmesh_vtk_to_t8/t8_cmesh_vtk_polydata.cxx \ src/t8_cmesh/t8_cmesh_vtk_to_t8/t8_cmesh_vtk_unstructured.cxx diff --git a/src/t8_cmesh/t8_cmesh_vtk_to_t8/t8_cmesh_vtk_polydata.cxx b/src/t8_cmesh/t8_cmesh_vtk_to_t8/t8_cmesh_vtk_polydata.cxx new file mode 100644 index 0000000000..9363d3f653 --- /dev/null +++ b/src/t8_cmesh/t8_cmesh_vtk_to_t8/t8_cmesh_vtk_polydata.cxx @@ -0,0 +1,113 @@ +/* +This file is part of t8code. +t8code is a C library to manage a collection (a forest) of multiple +connected adaptive space-trees of general element classes in parallel. + +Copyright (C) 2015 the developers + +t8code is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +t8code is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with t8code; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include +#if T8_WITH_VTK +#include +#include +#include +#include +#include +#include +#include + +int +t8_read_poly (const char *filename, vtkSmartPointer < vtkPolyData > grid) +{ + char tmp[BUFSIZ]; + char *extension; + /* Get the file-extension to decide which reader to use. */ + strcpy (tmp, filename); + extension = strrchr (tmp, '.') + 1; + T8_ASSERT (strcmp (extension, "")); + + /* Check if we can open the file. */ + FILE *first_check; + first_check = fopen (filename, "r"); + if (first_check == NULL) { + t8_errorf ("Can not find the file %s\n", filename); + fclose (first_check); + return 1; + } + fclose (first_check); + + /* Read the file depending on the extension. Not all readers have + * a built-in check if the file is readable. */ + if (strcmp (extension, "ply") == 0) { + vtkNew < vtkPLYReader > reader; + reader->SetFileName (filename); + reader->Update (); + grid->ShallowCopy (reader->GetOutput ()); + return 0; + } + else if (strcmp (extension, "vtp") == 0) { + vtkNew < vtkXMLPolyDataReader > reader; + reader->SetFileName (filename); + if (!reader->CanReadFile (filename)) { + t8_errorf ("Unable to read file %s.\n", filename); + return 1; + } + reader->Update (); + grid->ShallowCopy (reader->GetOutput ()); + return 0; + } + else if (strcmp (extension, "obj") == 0) { + vtkNew < vtkOBJReader > reader; + reader->SetFileName (filename); + reader->Update (); + grid->ShallowCopy (reader->GetOutput ()); + return 0; + } + else if (strcmp (extension, "stl") == 0) { + vtkNew < vtkSTLReader > reader; + reader->SetFileName (filename); + reader->Update (); + grid->ShallowCopy (reader->GetOutput ()); + return 0; + } + else if (strcmp (extension, "vtk") == 0) { + vtkNew < vtkPolyDataReader > reader; + reader->SetFileName (filename); + reader->Update (); + if (!reader->IsFilePolyData ()) { + t8_errorf + ("File-content is not polydata. If it is a vtkUnstructuredGrid use the unstructured Grid reader."); + return 1; + } + grid->ShallowCopy (reader->GetOutput ()); + return 0; + } + else if (strcmp (extension, "g") == 0) { + vtkNew < vtkBYUReader > reader; + reader->SetGeometryFileName (filename); + reader->Update (); + grid->ShallowCopy (reader->GetOutput ()); + return 0; + } + else { + /* Return NULL if the reader is not used correctly. */ + t8_global_errorf + ("Please use .ply, .vtp, .obj, .stl, .vtk or .g file %s\n", filename); + return 1; + } +} +#endif /* T8_WITH_VTK */ diff --git a/src/t8_cmesh/t8_cmesh_vtk_to_t8/t8_cmesh_vtk_polydata.hxx b/src/t8_cmesh/t8_cmesh_vtk_to_t8/t8_cmesh_vtk_polydata.hxx new file mode 100644 index 0000000000..3bb0824b6a --- /dev/null +++ b/src/t8_cmesh/t8_cmesh_vtk_to_t8/t8_cmesh_vtk_polydata.hxx @@ -0,0 +1,53 @@ +/* +This file is part of t8code. +t8code is a C library to manage a collection (a forest) of multiple +connected adaptive space-trees of general element classes in parallel. + +Copyright (C) 2015 the developers + +t8code is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +t8code is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with t8code; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +/** + * This file contains all helper functions to translate vtk-polydata. + * + */ + +#ifndef T8_CMESH_VTK_POLYDATA +#define T8_CMESH_VTK_POLYDATA + +#include +#if T8_WITH_VTK +#include +#include +#include +#include + +/** + * Given a filename to a file containing Polydata, read + * the file using the vtk-library. + * + * \param[in] filename The name of the file + * \param[in, out] grid On input a vtkSmartPointer, that will hold the grid described in + * \a filename. + * \return 0 if the file was read successfully, non-zero otherwise. + * + */ +int t8_read_poly (const char *filename, + vtkSmartPointer < vtkPolyData > grid); + +#endif /* T8_WITH_VTK */ + +#endif /* T8_CMESH_VTK_POLYDATA */