Skip to content

Commit

Permalink
Merge pull request CGAL#7729 from ange-clement/Polyhedron_demo_vti_im…
Browse files Browse the repository at this point in the history
…age_file_support_aclement

Mesh_3 Polyhedron demo : Added support for vtk images (vti format)
  • Loading branch information
lrineau committed Oct 5, 2023
2 parents da5e25e + a3ab2aa commit 619b043
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 24 deletions.
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
70 changes: 48 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 All @@ -77,6 +79,23 @@
#include <iostream>
#include <locale>

template <class vtkReader>
bool
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
}

// Covariant return types don't work for scalar types and we cannot
// have templates here, hence this unfortunate hack.

Expand Down Expand Up @@ -1089,6 +1108,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 +1141,47 @@ void convert(Image* image)
image->image()->wordKind = WK_FLOAT;
}


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 +1288,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

0 comments on commit 619b043

Please sign in to comment.