Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mesh_3 Polyhedron demo : Added support for vtk images (vti format) #7729

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Polyhedron/demo/Polyhedron/Plugins/Mesh_3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ if(ITK_FOUND)
target_link_libraries(mesh_3_plugin PUBLIC CGAL::ITK_support)
endif(ITK_FOUND)

find_package(VTK QUIET COMPONENTS vtkImagingGeneral vtkIOImage NO_MODULE)
find_package(VTK QUIET COMPONENTS vtkImagingGeneral vtkIOImage vtkIOXML NO_MODULE)
if(VTK_FOUND)
if(VTK_USE_FILE)
include(${VTK_USE_FILE})
endif()
if("${VTK_VERSION_MAJOR}" GREATER "5" OR VTK_VERSION VERSION_GREATER 5)
if(TARGET VTK::IOImage)
set(VTK_LIBRARIES VTK::IOImage VTK::ImagingGeneral)
set(VTK_LIBRARIES VTK::IOImage VTK::ImagingGeneral VTK::IOXML)
endif()
if(NOT VTK_LIBRARIES)
message(STATUS "NOTICE: DICOM files (.dcm) require the VTK libraries, and will not be readable.")
Expand Down
73 changes: 51 additions & 22 deletions Polyhedron/demo/Polyhedron/Plugins/Mesh_3/Io_image_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@
#include <CGAL/IO/read_vtk_image_data.h>

#include <vtkNew.h>
#include <vtkSmartPointer.h>
#include <vtkStringArray.h>
#include <vtkImageData.h>
#include <vtkXMLImageDataReader.h>
#include <vtkDICOMImageReader.h>
#include <vtkBMPReader.h>
#include <vtkNIFTIImageReader.h>
Expand Down Expand Up @@ -332,6 +334,8 @@ class Io_image_plugin :
QString nameFilters() const override;
bool canLoad(QFileInfo) const override;
QList<Scene_item*> load(QFileInfo fileinfo, bool& ok, bool add_to_scene = true) override;
template <class vtkReader>
bool load_vtk_file(QFileInfo fileinfo, Image* image);

bool canSave(const CGAL::Three::Scene_item*) override;
bool save(QFileInfo fileinfo, QList<CGAL::Three::Scene_item*>& items ) override
Expand Down Expand Up @@ -1089,6 +1093,7 @@ QString Io_image_plugin::nameFilters() const
return QString("Inrimage files (*.inr *.inr.gz) ;; "
"Analyze files (*.hdr *.img *.img.gz) ;; "
"Stanford Exploration Project files (*.H *.HH) ;; "
"VTK image files (*.vti) ;; "
"NRRD image files (*.nrrd) ;; "
"NIFTI image files (*.nii *.nii.gz)");
}
Expand Down Expand Up @@ -1121,46 +1126,65 @@ void convert(Image* image)
image->image()->wordKind = WK_FLOAT;
}


template <class vtkReader>
bool
Io_image_plugin::load_vtk_file(QFileInfo fileinfo, Image* image)
{
#ifdef CGAL_USE_VTK
vtkNew<vtkReader> reader;
reader->SetFileName(fileinfo.filePath().toUtf8());
reader->Update();
auto vtk_image = reader->GetOutput();
vtk_image->Print(std::cerr);
*image = CGAL::IO::read_vtk_image_data(vtk_image); // copy the image data
return true;
#else
return false;
#endif
}
ange-clement marked this conversation as resolved.
Show resolved Hide resolved


QList<Scene_item*>
Io_image_plugin::load(QFileInfo fileinfo, bool& ok, bool add_to_scene)
{
ok = true;
QApplication::restoreOverrideCursor();
Image* image = new Image;

QString warningMessage;
// read a vti file
if(fileinfo.suffix() == "vti")
{
#ifdef CGAL_USE_VTK
ok = load_vtk_file<vtkXMLImageDataReader>(fileinfo, image);
#else
ok = false;
warningMessage = "VTK is required to read VTI files";
#endif
}

// read a nrrd file
if(fileinfo.suffix() == "nrrd")
else if(fileinfo.suffix() == "nrrd")
{
#ifdef CGAL_USE_VTK
vtkNew<vtkNrrdReader> reader;
reader->SetFileName(fileinfo.filePath().toUtf8());
reader->Update();
auto vtk_image = reader->GetOutput();
vtk_image->Print(std::cerr);
*image = CGAL::IO::read_vtk_image_data(vtk_image); // copy the image data
ok = load_vtk_file<vtkNrrdReader>(fileinfo, image);
#else
CGAL::Three::Three::warning("VTK is required to read NRRD files");
delete image;
return QList<Scene_item*>();
ok = false;
warningMessage = "VTK is required to read NRRD files";
#endif
}

// read a NIFTI file
if(fileinfo.suffix() == "nii"
else if(fileinfo.suffix() == "nii"
|| ( fileinfo.suffix() == "gz"
&& fileinfo.fileName().endsWith(QString(".nii.gz"), Qt::CaseInsensitive)))
{
#ifdef CGAL_USE_VTK
vtkNew<vtkNIFTIImageReader> reader;
reader->SetFileName(fileinfo.filePath().toUtf8());
reader->Update();
auto vtk_image = reader->GetOutput();
vtk_image->Print(std::cerr);
*image = CGAL::IO::read_vtk_image_data(vtk_image); // copy the image data
ok = load_vtk_file<vtkNIFTIImageReader>(fileinfo, image);
#else
CGAL::Three::Three::warning("VTK is required to read NIfTI files");
delete image;
return QList<Scene_item*>();
ok = false;
warningMessage = "VTK is required to read NifTI files";
#endif
}

Expand Down Expand Up @@ -1267,11 +1291,16 @@ Io_image_plugin::load(QFileInfo fileinfo, bool& ok, bool add_to_scene)
if(!success)
{
ok = false;
delete image;
return QList<Scene_item*>();
}
}

if (!ok) {
if (warningMessage.length() > 0)
CGAL::Three::Three::warning(warningMessage);
delete image;
return QList<Scene_item*>();
}

// Get display precision
QDialog dialog;
ui.setupUi(&dialog);
Expand Down