Skip to content

Commit

Permalink
Add read_VTK() so that we can also read non-xml *.vtk files
Browse files Browse the repository at this point in the history
  • Loading branch information
afabri committed Oct 25, 2023
1 parent d5c8582 commit 48ee7ca
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions Stream_support/include/CGAL/IO/VTK.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
#include <vtkCommand.h>
#include <vtkCell.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkDataSetReader.h>
#include <vtkPointSet.h>
#include <vtkPolyData.h>
#include <vtkUnstructuredGrid.h>
#endif

#if defined(CGAL_USE_VTK) || defined(DOXYGEN_RUNNING)
Expand Down Expand Up @@ -140,6 +142,62 @@ bool read_VTP(const std::string& fname, PointRange& points, PolygonRange& polygo
return read_VTP(fname, points, polygons, parameters::default_values());
}


template <typename PointRange, typename PolygonRange, typename NamedParameters>
bool read_VTK(const std::string& fname,
PointRange& points,
PolygonRange& polygons,
const NamedParameters& np)
{
std::ifstream test(fname);
if(!test.good())
{
std::cerr<<"File doesn't exist."<<std::endl;
return false;
}

vtkSmartPointer<vtkPointSet> data;
vtkSmartPointer<internal::ErrorObserverVtk> obs =
vtkSmartPointer<internal::ErrorObserverVtk>::New();
vtkSmartPointer<vtkDataSetReader> reader =
CGAL::IO::internal::read_vtk_file<vtkDataSetReader>(fname,obs);
data = vtkPolyData::SafeDownCast(reader->GetOutput());
if (!data)
data = vtkUnstructuredGrid::SafeDownCast(reader->GetOutput());

if (obs->GetError())
return false;

return internal::vtkPointSet_to_polygon_soup(data, points, polygons, np);
}

/*!
* \ingroup PkgStreamSupportIoFuncsVTP
*
* \brief reads the content of the input file into `points` and `polygons`, using the \ref IOStreamVTK.
*
* \attention The polygon soup is not cleared, and the data from the file are appended.
*
* \tparam PointRange a model of the concepts `RandomAccessContainer` and `BackInsertionSequence`
* whose value type is the point type
* \tparam PolygonRange a model of the concepts `SequenceContainer` and `BackInsertionSequence`
* whose `value_type` is itself a model of the concept `SequenceContainer`
* and `BackInsertionSequence` whose `value_type` is an unsigned integer type
* convertible to `std::size_t`
*
* \param fname the path to the input file
* \param points points of the soup of polygons
* \param polygons a range of polygons. Each element in it describes a polygon
* using the indices of the points in `points`.
*
* \returns `true` if the reading was successful, `false` otherwise.
*/
template <typename PointRange, typename PolygonRange>
bool read_VTK(const std::string& fname, PointRange& points, PolygonRange& polygons)
{
return read_VTK(fname, points, polygons, parameters::default_values());
}

////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
// Write
Expand Down

0 comments on commit 48ee7ca

Please sign in to comment.