Skip to content

Commit

Permalink
Merge pull request DLR-AMR#465 from DLR-AMR/vtk_polydata_reader
Browse files Browse the repository at this point in the history
Add vtk_poly_data_reader [vtk_to_cmesh 4/6]
  • Loading branch information
sandro-elsweijer authored May 10, 2023
2 parents 9245a20 + dd21f4d commit c822c7c
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 3 deletions.
5 changes: 3 additions & 2 deletions config/t8_vtk.m4
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
4 changes: 3 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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


Expand Down
113 changes: 113 additions & 0 deletions src/t8_cmesh/t8_cmesh_vtk_to_t8/t8_cmesh_vtk_polydata.cxx
Original file line number Diff line number Diff line change
@@ -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 <t8_cmesh/t8_cmesh_vtk_to_t8/t8_cmesh_vtk_polydata.hxx>
#if T8_WITH_VTK
#include <vtkPolyData.h>
#include <vtkBYUReader.h>
#include <vtkOBJReader.h>
#include <vtkPLYReader.h>
#include <vtkPolyDataReader.h>
#include <vtkSTLReader.h>
#include <vtkXMLPolyDataReader.h>

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 */
53 changes: 53 additions & 0 deletions src/t8_cmesh/t8_cmesh_vtk_to_t8/t8_cmesh_vtk_polydata.hxx
Original file line number Diff line number Diff line change
@@ -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 <t8.h>
#if T8_WITH_VTK
#include <vtkPolyData.h>
#include <vtkSmartPointer.h>
#include <vtkCellData.h>
#include <vtkDataSet.h>

/**
* 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 */

0 comments on commit c822c7c

Please sign in to comment.